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
);
821 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
823 /* Save space: see if the result is identical to one of the args. */
824 if (valtype
== TREE_TYPE (t1
) && ! p2
)
825 return cp_build_type_attribute_variant (t1
, attributes
);
826 if (valtype
== TREE_TYPE (t2
) && ! p1
)
827 return cp_build_type_attribute_variant (t2
, attributes
);
829 /* Simple way if one arg fails to specify argument types. */
830 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
832 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
835 parms
= commonparms (p1
, p2
);
837 rval
= build_function_type (valtype
, parms
);
838 gcc_assert (type_memfn_quals (t1
) == type_memfn_quals (t2
));
839 gcc_assert (type_memfn_rqual (t1
) == type_memfn_rqual (t2
));
840 rval
= apply_memfn_quals (rval
,
841 type_memfn_quals (t1
),
842 type_memfn_rqual (t1
));
843 raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
844 TYPE_RAISES_EXCEPTIONS (t2
));
845 t1
= build_exception_variant (rval
, raises
);
846 if (late_return_type_p
)
847 TYPE_HAS_LATE_RETURN_TYPE (t1
) = 1;
853 /* Get this value the long way, since TYPE_METHOD_BASETYPE
854 is just the main variant of this. */
855 tree basetype
= class_of_this_parm (t2
);
856 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
857 TYPE_RAISES_EXCEPTIONS (t2
));
858 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
860 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
861 bool late_return_type_2_p
= TYPE_HAS_LATE_RETURN_TYPE (t2
);
863 /* If this was a member function type, get back to the
864 original type of type member function (i.e., without
865 the class instance variable up front. */
866 t1
= build_function_type (TREE_TYPE (t1
),
867 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
868 t2
= build_function_type (TREE_TYPE (t2
),
869 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
870 t3
= merge_types (t1
, t2
);
871 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
872 TYPE_ARG_TYPES (t3
));
873 t1
= build_exception_variant (t3
, raises
);
874 t1
= build_ref_qualified_type (t1
, rqual
);
875 if (late_return_type_1_p
)
876 TYPE_HAS_LATE_RETURN_TYPE (t1
) = 1;
877 if (late_return_type_2_p
)
878 TYPE_HAS_LATE_RETURN_TYPE (t2
) = 1;
883 /* There is no need to merge attributes into a TYPENAME_TYPE.
884 When the type is instantiated it will have whatever
885 attributes result from the instantiation. */
891 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
893 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
896 return cp_build_type_attribute_variant (t1
, attributes
);
899 /* Return the ARRAY_TYPE type without its domain. */
902 strip_array_domain (tree type
)
905 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
906 if (TYPE_DOMAIN (type
) == NULL_TREE
)
908 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
909 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
912 /* Wrapper around cp_common_type that is used by c-common.c and other
913 front end optimizations that remove promotions.
915 Return the common type for two arithmetic types T1 and T2 under the
916 usual arithmetic conversions. The default conversions have already
917 been applied, and enumerated types converted to their compatible
921 common_type (tree t1
, tree t2
)
923 /* If one type is nonsense, use the other */
924 if (t1
== error_mark_node
)
926 if (t2
== error_mark_node
)
929 return cp_common_type (t1
, t2
);
932 /* Return the common type of two pointer types T1 and T2. This is the
933 type for the result of most arithmetic operations if the operands
934 have the given two types.
936 We assume that comp_target_types has already been done and returned
937 nonzero; if that isn't so, this may crash. */
940 common_pointer_type (tree t1
, tree t2
)
942 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
943 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
944 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
946 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
947 CPO_CONVERSION
, tf_warning_or_error
);
950 /* Compare two exception specifier types for exactness or subsetness, if
951 allowed. Returns false for mismatch, true for match (same, or
954 [except.spec] "If a class X ... objects of class X or any class publicly
955 and unambiguously derived from X. Similarly, if a pointer type Y * ...
956 exceptions of type Y * or that are pointers to any type publicly and
957 unambiguously derived from Y. Otherwise a function only allows exceptions
958 that have the same type ..."
959 This does not mention cv qualifiers and is different to what throw
960 [except.throw] and catch [except.catch] will do. They will ignore the
961 top level cv qualifiers, and allow qualifiers in the pointer to class
964 We implement the letter of the standard. */
967 comp_except_types (tree a
, tree b
, bool exact
)
969 if (same_type_p (a
, b
))
973 if (cp_type_quals (a
) || cp_type_quals (b
))
976 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
980 if (cp_type_quals (a
) || cp_type_quals (b
))
984 if (TREE_CODE (a
) != RECORD_TYPE
985 || TREE_CODE (b
) != RECORD_TYPE
)
988 if (publicly_uniquely_derived_p (a
, b
))
994 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
995 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
996 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
997 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
998 are unordered, but we've already filtered out duplicates. Most lists will
999 be in order, we should try to make use of that. */
1002 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1011 /* First handle noexcept. */
1012 if (exact
< ce_exact
)
1014 /* noexcept(false) is compatible with no exception-specification,
1015 and stricter than any spec. */
1016 if (t1
== noexcept_false_spec
)
1017 return t2
== NULL_TREE
|| exact
== ce_derived
;
1018 /* Even a derived noexcept(false) is compatible with no
1019 exception-specification. */
1020 if (t2
== noexcept_false_spec
)
1021 return t1
== NULL_TREE
;
1023 /* Otherwise, if we aren't looking for an exact match, noexcept is
1024 equivalent to throw(). */
1025 if (t1
== noexcept_true_spec
)
1026 t1
= empty_except_spec
;
1027 if (t2
== noexcept_true_spec
)
1028 t2
= empty_except_spec
;
1031 /* If any noexcept is left, it is only comparable to itself;
1032 either we're looking for an exact match or we're redeclaring a
1033 template with dependent noexcept. */
1034 if ((t1
&& TREE_PURPOSE (t1
))
1035 || (t2
&& TREE_PURPOSE (t2
)))
1037 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1039 if (t1
== NULL_TREE
) /* T1 is ... */
1040 return t2
== NULL_TREE
|| exact
== ce_derived
;
1041 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1042 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1043 if (t2
== NULL_TREE
) /* T2 is ... */
1045 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1046 return exact
== ce_derived
;
1048 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1049 Count how many we find, to determine exactness. For exact matching and
1050 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1052 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1054 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1056 tree a
= TREE_VALUE (probe
);
1057 tree b
= TREE_VALUE (t2
);
1059 if (comp_except_types (a
, b
, exact
))
1061 if (probe
== base
&& exact
> ce_derived
)
1062 base
= TREE_CHAIN (probe
);
1067 if (probe
== NULL_TREE
)
1070 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1073 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1074 [] can match [size]. */
1077 comp_array_types (const_tree t1
, const_tree t2
, bool allow_redeclaration
)
1086 /* The type of the array elements must be the same. */
1087 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1090 d1
= TYPE_DOMAIN (t1
);
1091 d2
= TYPE_DOMAIN (t2
);
1096 /* If one of the arrays is dimensionless, and the other has a
1097 dimension, they are of different types. However, it is valid to
1105 declarations for an array object can specify
1106 array types that differ by the presence or absence of a major
1107 array bound (_dcl.array_). */
1109 return allow_redeclaration
;
1111 /* Check that the dimensions are the same. */
1113 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1115 max1
= TYPE_MAX_VALUE (d1
);
1116 max2
= TYPE_MAX_VALUE (d2
);
1118 if (!cp_tree_equal (max1
, max2
))
1124 /* Compare the relative position of T1 and T2 into their respective
1125 template parameter list.
1126 T1 and T2 must be template parameter types.
1127 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1130 comp_template_parms_position (tree t1
, tree t2
)
1132 tree index1
, index2
;
1133 gcc_assert (t1
&& t2
1134 && TREE_CODE (t1
) == TREE_CODE (t2
)
1135 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1136 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1137 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1139 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1140 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1142 /* Then compare their relative position. */
1143 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1144 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1145 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1146 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1149 /* In C++14 we can end up comparing 'auto' to a normal template
1150 parameter. Don't confuse them. */
1151 if (cxx_dialect
>= cxx1y
&& (is_auto (t1
) || is_auto (t2
)))
1152 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1157 /* Subroutine in comptypes. */
1160 structural_comptypes (tree t1
, tree t2
, int strict
)
1165 /* Suppress errors caused by previously reported errors. */
1166 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1169 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1171 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1172 current instantiation. */
1173 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1174 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1176 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1177 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1179 if (TYPE_PTRMEMFUNC_P (t1
))
1180 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1181 if (TYPE_PTRMEMFUNC_P (t2
))
1182 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1184 /* Different classes of types can't be compatible. */
1185 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1188 /* Qualifiers must match. For array types, we will check when we
1189 recur on the array element types. */
1190 if (TREE_CODE (t1
) != ARRAY_TYPE
1191 && cp_type_quals (t1
) != cp_type_quals (t2
))
1193 if (TREE_CODE (t1
) == FUNCTION_TYPE
1194 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1196 /* Need to check this before TYPE_MAIN_VARIANT.
1197 FIXME function qualifiers should really change the main variant. */
1198 if ((TREE_CODE (t1
) == FUNCTION_TYPE
1199 || TREE_CODE (t1
) == METHOD_TYPE
)
1200 && type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1202 if (TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
1205 /* Allow for two different type nodes which have essentially the same
1206 definition. Note that we already checked for equality of the type
1207 qualifiers (just above). */
1209 if (TREE_CODE (t1
) != ARRAY_TYPE
1210 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1214 /* Compare the types. Break out if they could be the same. */
1215 switch (TREE_CODE (t1
))
1219 /* All void and bool types are the same. */
1223 case FIXED_POINT_TYPE
:
1225 /* With these nodes, we can't determine type equivalence by
1226 looking at what is stored in the nodes themselves, because
1227 two nodes might have different TYPE_MAIN_VARIANTs but still
1228 represent the same type. For example, wchar_t and int could
1229 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1230 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1231 and are distinct types. On the other hand, int and the
1234 typedef int INT __attribute((may_alias));
1236 have identical properties, different TYPE_MAIN_VARIANTs, but
1237 represent the same type. The canonical type system keeps
1238 track of equivalence in this case, so we fall back on it. */
1239 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1241 case TEMPLATE_TEMPLATE_PARM
:
1242 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1243 if (!comp_template_parms_position (t1
, t2
))
1245 if (!comp_template_parms
1246 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1247 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1249 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1251 /* Don't check inheritance. */
1252 strict
= COMPARE_STRICT
;
1257 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1258 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1259 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1260 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1263 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1265 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1271 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1272 strict
& ~COMPARE_REDECLARATION
))
1274 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1278 case REFERENCE_TYPE
:
1279 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1281 /* fall through to checks for pointer types */
1284 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1285 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
)
1286 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1292 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1294 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1299 /* Target types must match incl. qualifiers. */
1300 if (!comp_array_types (t1
, t2
, !!(strict
& COMPARE_REDECLARATION
)))
1304 case TEMPLATE_TYPE_PARM
:
1305 /* If T1 and T2 don't have the same relative position in their
1306 template parameters set, they can't be equal. */
1307 if (!comp_template_parms_position (t1
, t2
))
1312 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1313 TYPENAME_TYPE_FULLNAME (t2
)))
1315 /* Qualifiers don't matter on scopes. */
1316 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1321 case UNBOUND_CLASS_TEMPLATE
:
1322 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1324 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1329 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1334 if (TYPE_VECTOR_SUBPARTS (t1
) != TYPE_VECTOR_SUBPARTS (t2
)
1335 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1339 case TYPE_PACK_EXPANSION
:
1340 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1341 PACK_EXPANSION_PATTERN (t2
))
1342 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1343 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1346 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1347 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1348 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1349 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1350 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1351 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1352 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1353 DECLTYPE_TYPE_EXPR (t2
)))
1357 case UNDERLYING_TYPE
:
1358 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1359 UNDERLYING_TYPE_TYPE (t2
));
1365 /* If we get here, we know that from a target independent POV the
1366 types are the same. Make sure the target attributes are also
1368 return comp_type_attributes (t1
, t2
);
1371 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1372 is a bitwise-or of the COMPARE_* flags. */
1375 comptypes (tree t1
, tree t2
, int strict
)
1377 if (strict
== COMPARE_STRICT
)
1382 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1385 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1386 /* At least one of the types requires structural equality, so
1387 perform a deep check. */
1388 return structural_comptypes (t1
, t2
, strict
);
1390 #ifdef ENABLE_CHECKING
1391 if (USE_CANONICAL_TYPES
)
1393 bool result
= structural_comptypes (t1
, t2
, strict
);
1395 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1396 /* The two types are structurally equivalent, but their
1397 canonical types were different. This is a failure of the
1398 canonical type propagation code.*/
1400 ("canonical types differ for identical types %T and %T",
1402 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1403 /* Two types are structurally different, but the canonical
1404 types are the same. This means we were over-eager in
1405 assigning canonical types. */
1407 ("same canonical type node for different types %T and %T",
1413 if (USE_CANONICAL_TYPES
)
1414 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1417 return structural_comptypes (t1
, t2
, strict
);
1419 else if (strict
== COMPARE_STRUCTURAL
)
1420 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1422 return structural_comptypes (t1
, t2
, strict
);
1425 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1426 top-level qualifiers. */
1429 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1431 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1434 return same_type_p (TYPE_MAIN_VARIANT (type1
), TYPE_MAIN_VARIANT (type2
));
1437 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1440 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1442 int q1
= cp_type_quals (type1
);
1443 int q2
= cp_type_quals (type2
);
1445 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1446 return (q1
& q2
) == q2
;
1449 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1450 more cv-qualified that TYPE1, and 0 otherwise. */
1453 comp_cv_qualification (int q1
, int q2
)
1458 if ((q1
& q2
) == q2
)
1460 else if ((q1
& q2
) == q1
)
1467 comp_cv_qualification (const_tree type1
, const_tree type2
)
1469 int q1
= cp_type_quals (type1
);
1470 int q2
= cp_type_quals (type2
);
1471 return comp_cv_qualification (q1
, q2
);
1474 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1475 subset of the cv-qualification signature of TYPE2, and the types
1476 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1479 comp_cv_qual_signature (tree type1
, tree type2
)
1481 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1483 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1489 /* Subroutines of `comptypes'. */
1491 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1492 equivalent in the sense that functions with those parameter types
1493 can have equivalent types. The two lists must be equivalent,
1494 element by element. */
1497 compparms (const_tree parms1
, const_tree parms2
)
1501 /* An unspecified parmlist matches any specified parmlist
1502 whose argument types don't need default promotions. */
1504 for (t1
= parms1
, t2
= parms2
;
1506 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1508 /* If one parmlist is shorter than the other,
1509 they fail to match. */
1512 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1519 /* Process a sizeof or alignof expression where the operand is a
1523 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool complain
)
1528 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1529 if (type
== error_mark_node
)
1530 return error_mark_node
;
1532 type
= non_reference (type
);
1533 if (TREE_CODE (type
) == METHOD_TYPE
)
1536 pedwarn (input_location
, OPT_Wpointer_arith
,
1537 "invalid application of %qs to a member function",
1538 operator_name_info
[(int) op
].name
);
1539 value
= size_one_node
;
1542 dependent_p
= dependent_type_p (type
);
1544 complete_type (type
);
1546 /* VLA types will have a non-constant size. In the body of an
1547 uninstantiated template, we don't need to try to compute the
1548 value, because the sizeof expression is not an integral
1549 constant expression in that case. And, if we do try to
1550 compute the value, we'll likely end up with SAVE_EXPRs, which
1551 the template substitution machinery does not expect to see. */
1552 || (processing_template_decl
1553 && COMPLETE_TYPE_P (type
)
1554 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1556 value
= build_min (op
, size_type_node
, type
);
1557 TREE_READONLY (value
) = 1;
1561 if (cxx_dialect
>= cxx1y
&& array_of_runtime_bound_p (type
)
1562 && (flag_iso
|| warn_vla
> 0))
1564 if (complain
& tf_warning_or_error
)
1565 pedwarn (input_location
, OPT_Wvla
,
1566 "taking sizeof array of runtime bound");
1568 return error_mark_node
;
1571 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1572 op
== SIZEOF_EXPR
, false,
1576 /* Return the size of the type, without producing any warnings for
1577 types whose size cannot be taken. This routine should be used only
1578 in some other routine that has already produced a diagnostic about
1579 using the size of such a type. */
1581 cxx_sizeof_nowarn (tree type
)
1583 if (TREE_CODE (type
) == FUNCTION_TYPE
1584 || VOID_TYPE_P (type
)
1585 || TREE_CODE (type
) == ERROR_MARK
)
1586 return size_one_node
;
1587 else if (!COMPLETE_TYPE_P (type
))
1588 return size_zero_node
;
1590 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false);
1593 /* Process a sizeof expression where the operand is an expression. */
1596 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1598 if (e
== error_mark_node
)
1599 return error_mark_node
;
1601 if (processing_template_decl
)
1603 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1604 TREE_SIDE_EFFECTS (e
) = 0;
1605 TREE_READONLY (e
) = 1;
1610 /* To get the size of a static data member declared as an array of
1611 unknown bound, we need to instantiate it. */
1613 && VAR_HAD_UNKNOWN_BOUND (e
)
1614 && DECL_TEMPLATE_INSTANTIATION (e
))
1615 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1617 if (TREE_CODE (e
) == PARM_DECL
1618 && DECL_ARRAY_PARAMETER_P (e
)
1619 && (complain
& tf_warning
))
1621 if (warning (OPT_Wsizeof_array_argument
, "%<sizeof%> on array function "
1622 "parameter %qE will return size of %qT", e
, TREE_TYPE (e
)))
1623 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1626 e
= mark_type_use (e
);
1628 if (TREE_CODE (e
) == COMPONENT_REF
1629 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1630 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1632 if (complain
& tf_error
)
1633 error ("invalid application of %<sizeof%> to a bit-field");
1635 return error_mark_node
;
1638 else if (is_overloaded_fn (e
))
1640 if (complain
& tf_error
)
1641 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1644 return error_mark_node
;
1647 else if (type_unknown_p (e
))
1649 if (complain
& tf_error
)
1650 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1652 return error_mark_node
;
1658 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, complain
& tf_error
);
1661 /* Implement the __alignof keyword: Return the minimum required
1662 alignment of E, measured in bytes. For VAR_DECL's and
1663 FIELD_DECL's return DECL_ALIGN (which can be set from an
1664 "aligned" __attribute__ specification). */
1667 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1671 if (e
== error_mark_node
)
1672 return error_mark_node
;
1674 if (processing_template_decl
)
1676 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1677 TREE_SIDE_EFFECTS (e
) = 0;
1678 TREE_READONLY (e
) = 1;
1683 e
= mark_type_use (e
);
1686 t
= size_int (DECL_ALIGN_UNIT (e
));
1687 else if (TREE_CODE (e
) == COMPONENT_REF
1688 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1689 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1691 if (complain
& tf_error
)
1692 error ("invalid application of %<__alignof%> to a bit-field");
1694 return error_mark_node
;
1697 else if (TREE_CODE (e
) == COMPONENT_REF
1698 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1699 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1700 else if (is_overloaded_fn (e
))
1702 if (complain
& tf_error
)
1703 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1706 return error_mark_node
;
1707 if (TREE_CODE (e
) == FUNCTION_DECL
)
1708 t
= size_int (DECL_ALIGN_UNIT (e
));
1712 else if (type_unknown_p (e
))
1714 if (complain
& tf_error
)
1715 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1717 return error_mark_node
;
1721 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
,
1722 complain
& tf_error
);
1724 return fold_convert (size_type_node
, t
);
1727 /* Process a sizeof or alignof expression E with code OP where the operand
1728 is an expression. */
1731 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1733 if (op
== SIZEOF_EXPR
)
1734 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1736 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1739 /* Build a representation of an expression 'alignas(E).' Return the
1740 folded integer value of E if it is an integral constant expression
1741 that resolves to a valid alignment. If E depends on a template
1742 parameter, return a syntactic representation tree of kind
1743 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1744 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1747 cxx_alignas_expr (tree e
)
1749 if (e
== NULL_TREE
|| e
== error_mark_node
1750 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1756 When the alignment-specifier is of the form
1757 alignas(type-id ), it shall have the same effect as
1758 alignas(alignof(type-id )). */
1760 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, false);
1762 /* If we reach this point, it means the alignas expression if of
1763 the form "alignas(assignment-expression)", so we should follow
1764 what is stated by [dcl.align]/2. */
1766 if (value_dependent_expression_p (e
))
1767 /* Leave value-dependent expression alone for now. */
1770 e
= fold_non_dependent_expr (e
);
1771 e
= mark_rvalue_use (e
);
1773 /* [dcl.align]/2 says:
1775 the assignment-expression shall be an integral constant
1778 return cxx_constant_value (e
);
1782 /* EXPR is being used in a context that is not a function call.
1787 The expression can be used only as the left-hand operand of a
1788 member function call.
1790 [expr.mptr.operator]
1792 If the result of .* or ->* is a function, then that result can be
1793 used only as the operand for the function call operator ().
1795 by issuing an error message if appropriate. Returns true iff EXPR
1796 violates these rules. */
1799 invalid_nonstatic_memfn_p (tree expr
, tsubst_flags_t complain
)
1801 if (expr
== NULL_TREE
)
1803 /* Don't enforce this in MS mode. */
1804 if (flag_ms_extensions
)
1806 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1807 expr
= get_first_fn (expr
);
1808 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1810 if (complain
& tf_error
)
1811 error ("invalid use of non-static member function");
1817 /* If EXP is a reference to a bitfield, and the type of EXP does not
1818 match the declared type of the bitfield, return the declared type
1819 of the bitfield. Otherwise, return NULL_TREE. */
1822 is_bitfield_expr_with_lowered_type (const_tree exp
)
1824 switch (TREE_CODE (exp
))
1827 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1828 ? TREE_OPERAND (exp
, 1)
1829 : TREE_OPERAND (exp
, 0)))
1831 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1834 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1838 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1844 field
= TREE_OPERAND (exp
, 1);
1845 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1847 if (same_type_ignoring_top_level_qualifiers_p
1848 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1850 return DECL_BIT_FIELD_TYPE (field
);
1854 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1855 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1856 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1864 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1865 bitfield with a lowered type, the type of EXP is returned, rather
1869 unlowered_expr_type (const_tree exp
)
1872 tree etype
= TREE_TYPE (exp
);
1874 type
= is_bitfield_expr_with_lowered_type (exp
);
1876 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1883 /* Perform the conversions in [expr] that apply when an lvalue appears
1884 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1885 function-to-pointer conversions. In addition, manifest constants
1886 are replaced by their values, and bitfield references are converted
1887 to their declared types. Note that this function does not perform the
1888 lvalue-to-rvalue conversion for class types. If you need that conversion
1889 to for class types, then you probably need to use force_rvalue.
1891 Although the returned value is being used as an rvalue, this
1892 function does not wrap the returned expression in a
1893 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1894 that the return value is no longer an lvalue. */
1897 decay_conversion (tree exp
, tsubst_flags_t complain
)
1900 enum tree_code code
;
1901 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
1903 type
= TREE_TYPE (exp
);
1904 if (type
== error_mark_node
)
1905 return error_mark_node
;
1907 exp
= mark_rvalue_use (exp
);
1909 exp
= resolve_nondeduced_context (exp
);
1910 if (type_unknown_p (exp
))
1912 if (complain
& tf_error
)
1913 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
1914 return error_mark_node
;
1917 code
= TREE_CODE (type
);
1919 /* For an array decl decay_conversion should not try to return its
1921 if (code
!= ARRAY_TYPE
)
1923 /* FIXME remove? at least need to remember that this isn't really a
1924 constant expression if EXP isn't decl_constant_var_p, like with
1925 C_MAYBE_CONST_EXPR. */
1926 exp
= decl_constant_value_safe (exp
);
1927 if (error_operand_p (exp
))
1928 return error_mark_node
;
1931 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
1932 return nullptr_node
;
1934 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1935 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1936 if (code
== VOID_TYPE
)
1938 if (complain
& tf_error
)
1939 error_at (loc
, "void value not ignored as it ought to be");
1940 return error_mark_node
;
1942 if (invalid_nonstatic_memfn_p (exp
, complain
))
1943 return error_mark_node
;
1944 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1945 return cp_build_addr_expr (exp
, complain
);
1946 if (code
== ARRAY_TYPE
)
1951 if (INDIRECT_REF_P (exp
))
1952 return build_nop (build_pointer_type (TREE_TYPE (type
)),
1953 TREE_OPERAND (exp
, 0));
1955 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1957 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
1958 if (op1
== error_mark_node
)
1959 return error_mark_node
;
1960 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1961 TREE_OPERAND (exp
, 0), op1
);
1965 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1967 if (complain
& tf_error
)
1968 error_at (loc
, "invalid use of non-lvalue array");
1969 return error_mark_node
;
1972 /* Don't let an array compound literal decay to a pointer. It can
1973 still be used to initialize an array or bind to a reference. */
1974 if (TREE_CODE (exp
) == TARGET_EXPR
)
1976 if (complain
& tf_error
)
1977 error_at (loc
, "taking address of temporary array");
1978 return error_mark_node
;
1981 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1985 if (!cxx_mark_addressable (exp
))
1986 return error_mark_node
;
1987 adr
= build_nop (ptrtype
, build_address (exp
));
1990 /* This way is better for a COMPONENT_REF since it can
1991 simplify the offset for a component. */
1992 adr
= cp_build_addr_expr (exp
, complain
);
1993 return cp_convert (ptrtype
, adr
, complain
);
1996 /* If a bitfield is used in a context where integral promotion
1997 applies, then the caller is expected to have used
1998 default_conversion. That function promotes bitfields correctly
1999 before calling this function. At this point, if we have a
2000 bitfield referenced, we may assume that is not subject to
2001 promotion, and that, therefore, the type of the resulting rvalue
2002 is the declared type of the bitfield. */
2003 exp
= convert_bitfield_to_declared_type (exp
);
2005 /* We do not call rvalue() here because we do not want to wrap EXP
2006 in a NON_LVALUE_EXPR. */
2010 Non-class rvalues always have cv-unqualified types. */
2011 type
= TREE_TYPE (exp
);
2012 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2013 exp
= build_nop (cv_unqualified (type
), exp
);
2018 /* Perform preparatory conversions, as part of the "usual arithmetic
2019 conversions". In particular, as per [expr]:
2021 Whenever an lvalue expression appears as an operand of an
2022 operator that expects the rvalue for that operand, the
2023 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2024 standard conversions are applied to convert the expression to an
2027 In addition, we perform integral promotions here, as those are
2028 applied to both operands to a binary operator before determining
2029 what additional conversions should apply. */
2032 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2034 /* Check for target-specific promotions. */
2035 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2037 exp
= cp_convert (promoted_type
, exp
, complain
);
2038 /* Perform the integral promotions first so that bitfield
2039 expressions (which may promote to "int", even if the bitfield is
2040 declared "unsigned") are promoted correctly. */
2041 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2042 exp
= cp_perform_integral_promotions (exp
, complain
);
2043 /* Perform the other conversions. */
2044 exp
= decay_conversion (exp
, complain
);
2052 default_conversion (tree exp
)
2054 return cp_default_conversion (exp
, tf_warning_or_error
);
2057 /* EXPR is an expression with an integral or enumeration type.
2058 Perform the integral promotions in [conv.prom], and return the
2062 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2067 expr
= mark_rvalue_use (expr
);
2071 If the bitfield has an enumerated type, it is treated as any
2072 other value of that type for promotion purposes. */
2073 type
= is_bitfield_expr_with_lowered_type (expr
);
2074 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2075 type
= TREE_TYPE (expr
);
2076 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2077 /* Scoped enums don't promote. */
2078 if (SCOPED_ENUM_P (type
))
2080 promoted_type
= type_promotes_to (type
);
2081 if (type
!= promoted_type
)
2082 expr
= cp_convert (promoted_type
, expr
, complain
);
2089 perform_integral_promotions (tree expr
)
2091 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2094 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2095 decay_conversion to one. */
2098 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2102 if (!TYPE_PTR_P (totype
))
2105 t
= TREE_TYPE (totype
);
2106 if (!same_type_p (t
, char_type_node
)
2107 && !same_type_p (t
, char16_type_node
)
2108 && !same_type_p (t
, char32_type_node
)
2109 && !same_type_p (t
, wchar_type_node
))
2112 if (TREE_CODE (exp
) == STRING_CST
)
2114 /* Make sure that we don't try to convert between char and wide chars. */
2115 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2120 /* Is this a string constant which has decayed to 'const char *'? */
2121 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2122 if (!same_type_p (TREE_TYPE (exp
), t
))
2125 if (TREE_CODE (exp
) != ADDR_EXPR
2126 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2130 /* This warning is not very useful, as it complains about printf. */
2132 warning (OPT_Wwrite_strings
,
2133 "deprecated conversion from string constant to %qT",
2139 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2140 can, for example, use as an lvalue. This code used to be in
2141 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2142 expressions, where we're dealing with aggregates. But now it's again only
2143 called from unary_complex_lvalue. The case (in particular) that led to
2144 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2148 rationalize_conditional_expr (enum tree_code code
, tree t
,
2149 tsubst_flags_t complain
)
2151 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
2153 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2154 the first operand is always the one to be used if both operands
2155 are equal, so we know what conditional expression this used to be. */
2156 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2158 tree op0
= TREE_OPERAND (t
, 0);
2159 tree op1
= TREE_OPERAND (t
, 1);
2161 /* The following code is incorrect if either operand side-effects. */
2162 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2163 && !TREE_SIDE_EFFECTS (op1
));
2165 build_conditional_expr (loc
,
2166 build_x_binary_op (loc
,
2167 (TREE_CODE (t
) == MIN_EXPR
2168 ? LE_EXPR
: GE_EXPR
),
2169 op0
, TREE_CODE (op0
),
2170 op1
, TREE_CODE (op1
),
2173 cp_build_unary_op (code
, op0
, 0, complain
),
2174 cp_build_unary_op (code
, op1
, 0, complain
),
2179 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2180 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), 0,
2182 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), 0,
2187 /* Given the TYPE of an anonymous union field inside T, return the
2188 FIELD_DECL for the field. If not found return NULL_TREE. Because
2189 anonymous unions can nest, we must also search all anonymous unions
2190 that are directly reachable. */
2193 lookup_anon_field (tree t
, tree type
)
2197 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2199 if (TREE_STATIC (field
))
2201 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2204 /* If we find it directly, return the field. */
2205 if (DECL_NAME (field
) == NULL_TREE
2206 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2211 /* Otherwise, it could be nested, search harder. */
2212 if (DECL_NAME (field
) == NULL_TREE
2213 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2215 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2223 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2224 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2225 non-NULL, it indicates the path to the base used to name MEMBER.
2226 If PRESERVE_REFERENCE is true, the expression returned will have
2227 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2228 returned will have the type referred to by the reference.
2230 This function does not perform access control; that is either done
2231 earlier by the parser when the name of MEMBER is resolved to MEMBER
2232 itself, or later when overload resolution selects one of the
2233 functions indicated by MEMBER. */
2236 build_class_member_access_expr (tree object
, tree member
,
2237 tree access_path
, bool preserve_reference
,
2238 tsubst_flags_t complain
)
2242 tree result
= NULL_TREE
;
2243 tree using_decl
= NULL_TREE
;
2245 if (error_operand_p (object
) || error_operand_p (member
))
2246 return error_mark_node
;
2248 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2252 The type of the first expression shall be "class object" (of a
2254 object_type
= TREE_TYPE (object
);
2255 if (!currently_open_class (object_type
)
2256 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2257 return error_mark_node
;
2258 if (!CLASS_TYPE_P (object_type
))
2260 if (complain
& tf_error
)
2262 if (POINTER_TYPE_P (object_type
)
2263 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2264 error ("request for member %qD in %qE, which is of pointer "
2265 "type %qT (maybe you meant to use %<->%> ?)",
2266 member
, object
, object_type
);
2268 error ("request for member %qD in %qE, which is of non-class "
2269 "type %qT", member
, object
, object_type
);
2271 return error_mark_node
;
2274 /* The standard does not seem to actually say that MEMBER must be a
2275 member of OBJECT_TYPE. However, that is clearly what is
2277 if (DECL_P (member
))
2279 member_scope
= DECL_CLASS_CONTEXT (member
);
2281 if (TREE_DEPRECATED (member
))
2282 warn_deprecated_use (member
, NULL_TREE
);
2285 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2286 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2287 presently be the anonymous union. Go outwards until we find a
2288 type related to OBJECT_TYPE. */
2289 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2290 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2292 member_scope
= TYPE_CONTEXT (member_scope
);
2293 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2295 if (complain
& tf_error
)
2297 if (TREE_CODE (member
) == FIELD_DECL
)
2298 error ("invalid use of nonstatic data member %qE", member
);
2300 error ("%qD is not a member of %qT", member
, object_type
);
2302 return error_mark_node
;
2305 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2306 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2307 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2309 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2311 object
= cp_build_indirect_ref (temp
, RO_NULL
, complain
);
2314 /* In [expr.ref], there is an explicit list of the valid choices for
2315 MEMBER. We check for each of those cases here. */
2318 /* A static data member. */
2320 mark_exp_read (object
);
2321 /* If OBJECT has side-effects, they are supposed to occur. */
2322 if (TREE_SIDE_EFFECTS (object
))
2323 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2325 else if (TREE_CODE (member
) == FIELD_DECL
)
2327 /* A non-static data member. */
2332 null_object_p
= (INDIRECT_REF_P (object
)
2333 && integer_zerop (TREE_OPERAND (object
, 0)));
2335 /* Convert OBJECT to the type of MEMBER. */
2336 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2337 TYPE_MAIN_VARIANT (member_scope
)))
2342 binfo
= lookup_base (access_path
? access_path
: object_type
,
2343 member_scope
, ba_unique
, &kind
, complain
);
2344 if (binfo
== error_mark_node
)
2345 return error_mark_node
;
2347 /* It is invalid to try to get to a virtual base of a
2348 NULL object. The most common cause is invalid use of
2350 if (null_object_p
&& kind
== bk_via_virtual
)
2352 if (complain
& tf_error
)
2354 error ("invalid access to non-static data member %qD of "
2357 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2359 return error_mark_node
;
2362 /* Convert to the base. */
2363 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2364 /*nonnull=*/1, complain
);
2365 /* If we found the base successfully then we should be able
2366 to convert to it successfully. */
2367 gcc_assert (object
!= error_mark_node
);
2370 /* Complain about other invalid uses of offsetof, even though they will
2371 give the right answer. Note that we complain whether or not they
2372 actually used the offsetof macro, since there's no way to know at this
2373 point. So we just give a warning, instead of a pedwarn. */
2374 /* Do not produce this warning for base class field references, because
2375 we know for a fact that didn't come from offsetof. This does occur
2376 in various testsuite cases where a null object is passed where a
2377 vtable access is required. */
2378 if (null_object_p
&& warn_invalid_offsetof
2379 && CLASSTYPE_NON_STD_LAYOUT (object_type
)
2380 && !DECL_FIELD_IS_BASE (member
)
2381 && cp_unevaluated_operand
== 0
2382 && (complain
& tf_warning
))
2384 warning (OPT_Winvalid_offsetof
,
2385 "invalid access to non-static data member %qD "
2386 " of NULL object", member
);
2387 warning (OPT_Winvalid_offsetof
,
2388 "(perhaps the %<offsetof%> macro was used incorrectly)");
2391 /* If MEMBER is from an anonymous aggregate, we have converted
2392 OBJECT so that it refers to the class containing the
2393 anonymous union. Generate a reference to the anonymous union
2394 itself, and recur to find MEMBER. */
2395 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2396 /* When this code is called from build_field_call, the
2397 object already has the type of the anonymous union.
2398 That is because the COMPONENT_REF was already
2399 constructed, and was then disassembled before calling
2400 build_field_call. After the function-call code is
2401 cleaned up, this waste can be eliminated. */
2402 && (!same_type_ignoring_top_level_qualifiers_p
2403 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2405 tree anonymous_union
;
2407 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2408 DECL_CONTEXT (member
));
2409 object
= build_class_member_access_expr (object
,
2411 /*access_path=*/NULL_TREE
,
2416 /* Compute the type of the field, as described in [expr.ref]. */
2417 type_quals
= TYPE_UNQUALIFIED
;
2418 member_type
= TREE_TYPE (member
);
2419 if (TREE_CODE (member_type
) != REFERENCE_TYPE
)
2421 type_quals
= (cp_type_quals (member_type
)
2422 | cp_type_quals (object_type
));
2424 /* A field is const (volatile) if the enclosing object, or the
2425 field itself, is const (volatile). But, a mutable field is
2426 not const, even within a const object. */
2427 if (DECL_MUTABLE_P (member
))
2428 type_quals
&= ~TYPE_QUAL_CONST
;
2429 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2432 result
= build3 (COMPONENT_REF
, member_type
, object
, member
,
2434 result
= fold_if_not_in_template (result
);
2436 /* Mark the expression const or volatile, as appropriate. Even
2437 though we've dealt with the type above, we still have to mark the
2438 expression itself. */
2439 if (type_quals
& TYPE_QUAL_CONST
)
2440 TREE_READONLY (result
) = 1;
2441 if (type_quals
& TYPE_QUAL_VOLATILE
)
2442 TREE_THIS_VOLATILE (result
) = 1;
2444 else if (BASELINK_P (member
))
2446 /* The member is a (possibly overloaded) member function. */
2450 /* If the MEMBER is exactly one static member function, then we
2451 know the type of the expression. Otherwise, we must wait
2452 until overload resolution has been performed. */
2453 functions
= BASELINK_FUNCTIONS (member
);
2454 if (TREE_CODE (functions
) == FUNCTION_DECL
2455 && DECL_STATIC_FUNCTION_P (functions
))
2456 type
= TREE_TYPE (functions
);
2458 type
= unknown_type_node
;
2459 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2460 base. That will happen when the function is called. */
2461 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2463 else if (TREE_CODE (member
) == CONST_DECL
)
2465 /* The member is an enumerator. */
2467 /* If OBJECT has side-effects, they are supposed to occur. */
2468 if (TREE_SIDE_EFFECTS (object
))
2469 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2472 else if ((using_decl
= strip_using_decl (member
)) != member
)
2473 result
= build_class_member_access_expr (object
,
2475 access_path
, preserve_reference
,
2479 if (complain
& tf_error
)
2480 error ("invalid use of %qD", member
);
2481 return error_mark_node
;
2484 if (!preserve_reference
)
2487 If E2 is declared to have type "reference to T", then ... the
2488 type of E1.E2 is T. */
2489 result
= convert_from_reference (result
);
2494 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2495 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2498 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2499 tsubst_flags_t complain
)
2501 tree object_type
= TREE_TYPE (object
);
2502 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2505 /* We've already complained about this destructor. */
2506 if (dtor_type
== error_mark_node
)
2507 return error_mark_node
;
2509 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2511 if (complain
& tf_error
)
2512 error ("qualified type %qT does not match destructor name ~%qT",
2514 return error_mark_node
;
2516 if (is_auto (dtor_type
))
2517 dtor_type
= object_type
;
2518 else if (identifier_p (dtor_type
))
2520 /* In a template, names we can't find a match for are still accepted
2521 destructor names, and we check them here. */
2522 if (check_dtor_name (object_type
, dtor_type
))
2523 dtor_type
= object_type
;
2526 if (complain
& tf_error
)
2527 error ("object type %qT does not match destructor name ~%qT",
2528 object_type
, dtor_type
);
2529 return error_mark_node
;
2533 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2535 if (complain
& tf_error
)
2536 error ("the type being destroyed is %qT, but the destructor "
2537 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2538 return error_mark_node
;
2540 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2541 /*protect=*/1, /*want_type=*/false,
2542 tf_warning_or_error
);
2543 expr
= (adjust_result_of_qualified_name_lookup
2544 (expr
, dtor_type
, object_type
));
2545 if (scope
== NULL_TREE
)
2546 /* We need to call adjust_result_of_qualified_name_lookup in case the
2547 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2548 that we still get virtual function binding. */
2549 BASELINK_QUALIFIED_P (expr
) = false;
2553 /* An expression of the form "A::template B" has been resolved to
2554 DECL. Issue a diagnostic if B is not a template or template
2558 check_template_keyword (tree decl
)
2560 /* The standard says:
2564 If a name prefixed by the keyword template is not a member
2565 template, the program is ill-formed.
2567 DR 228 removed the restriction that the template be a member
2570 DR 96, if accepted would add the further restriction that explicit
2571 template arguments must be provided if the template keyword is
2572 used, but, as of 2005-10-16, that DR is still in "drafting". If
2573 this DR is accepted, then the semantic checks here can be
2574 simplified, as the entity named must in fact be a template
2575 specialization, rather than, as at present, a set of overloaded
2576 functions containing at least one template function. */
2577 if (TREE_CODE (decl
) != TEMPLATE_DECL
2578 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2580 if (!is_overloaded_fn (decl
))
2581 permerror (input_location
, "%qD is not a template", decl
);
2586 if (BASELINK_P (fns
))
2587 fns
= BASELINK_FUNCTIONS (fns
);
2590 tree fn
= OVL_CURRENT (fns
);
2591 if (TREE_CODE (fn
) == TEMPLATE_DECL
2592 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2594 if (TREE_CODE (fn
) == FUNCTION_DECL
2595 && DECL_USE_TEMPLATE (fn
)
2596 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
2598 fns
= OVL_NEXT (fns
);
2601 permerror (input_location
, "%qD is not a template", decl
);
2606 /* This function is called by the parser to process a class member
2607 access expression of the form OBJECT.NAME. NAME is a node used by
2608 the parser to represent a name; it is not yet a DECL. It may,
2609 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2610 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2611 there is no reason to do the lookup twice, so the parser keeps the
2612 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2613 be a template via the use of the "A::template B" syntax. */
2616 finish_class_member_access_expr (tree object
, tree name
, bool template_p
,
2617 tsubst_flags_t complain
)
2622 tree access_path
= NULL_TREE
;
2623 tree orig_object
= object
;
2624 tree orig_name
= name
;
2626 if (object
== error_mark_node
|| name
== error_mark_node
)
2627 return error_mark_node
;
2629 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2630 if (!objc_is_public (object
, name
))
2631 return error_mark_node
;
2633 object_type
= TREE_TYPE (object
);
2635 if (processing_template_decl
)
2637 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2638 dependent_type_p (object_type
)
2639 /* If NAME is just an IDENTIFIER_NODE, then the expression
2641 || identifier_p (object
)
2642 /* If NAME is "f<args>", where either 'f' or 'args' is
2643 dependent, then the expression is dependent. */
2644 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2645 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2646 TREE_OPERAND (name
, 1)))
2647 /* If NAME is "T::X" where "T" is dependent, then the
2648 expression is dependent. */
2649 || (TREE_CODE (name
) == SCOPE_REF
2650 && TYPE_P (TREE_OPERAND (name
, 0))
2651 && dependent_type_p (TREE_OPERAND (name
, 0))))
2652 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2653 object
, name
, NULL_TREE
);
2654 object
= build_non_dependent_expr (object
);
2656 else if (c_dialect_objc ()
2657 && identifier_p (name
)
2658 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2663 The type of the first expression shall be "class object" (of a
2665 if (!currently_open_class (object_type
)
2666 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2667 return error_mark_node
;
2668 if (!CLASS_TYPE_P (object_type
))
2670 if (complain
& tf_error
)
2672 if (POINTER_TYPE_P (object_type
)
2673 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2674 error ("request for member %qD in %qE, which is of pointer "
2675 "type %qT (maybe you meant to use %<->%> ?)",
2676 name
, object
, object_type
);
2678 error ("request for member %qD in %qE, which is of non-class "
2679 "type %qT", name
, object
, object_type
);
2681 return error_mark_node
;
2684 if (BASELINK_P (name
))
2685 /* A member function that has already been looked up. */
2689 bool is_template_id
= false;
2690 tree template_args
= NULL_TREE
;
2693 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2695 is_template_id
= true;
2696 template_args
= TREE_OPERAND (name
, 1);
2697 name
= TREE_OPERAND (name
, 0);
2699 if (TREE_CODE (name
) == OVERLOAD
)
2700 name
= DECL_NAME (get_first_fn (name
));
2701 else if (DECL_P (name
))
2702 name
= DECL_NAME (name
);
2705 if (TREE_CODE (name
) == SCOPE_REF
)
2707 /* A qualified name. The qualifying class or namespace `S'
2708 has already been looked up; it is either a TYPE or a
2710 scope
= TREE_OPERAND (name
, 0);
2711 name
= TREE_OPERAND (name
, 1);
2713 /* If SCOPE is a namespace, then the qualified name does not
2714 name a member of OBJECT_TYPE. */
2715 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2717 if (complain
& tf_error
)
2718 error ("%<%D::%D%> is not a member of %qT",
2719 scope
, name
, object_type
);
2720 return error_mark_node
;
2723 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2725 /* Looking up a member enumerator (c++/56793). */
2726 if (!TYPE_CLASS_SCOPE_P (scope
)
2727 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2729 if (complain
& tf_error
)
2730 error ("%<%D::%D%> is not a member of %qT",
2731 scope
, name
, object_type
);
2732 return error_mark_node
;
2734 tree val
= lookup_enumerator (scope
, name
);
2735 if (TREE_SIDE_EFFECTS (object
))
2736 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
2740 gcc_assert (CLASS_TYPE_P (scope
));
2741 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
2743 if (constructor_name_p (name
, scope
))
2745 if (complain
& tf_error
)
2746 error ("cannot call constructor %<%T::%D%> directly",
2748 return error_mark_node
;
2751 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2752 access_path
= lookup_base (object_type
, scope
, ba_check
,
2754 if (access_path
== error_mark_node
)
2755 return error_mark_node
;
2758 if (complain
& tf_error
)
2759 error ("%qT is not a base of %qT", scope
, object_type
);
2760 return error_mark_node
;
2766 access_path
= object_type
;
2769 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2770 member
= lookup_destructor (object
, scope
, name
, complain
);
2773 /* Look up the member. */
2774 member
= lookup_member (access_path
, name
, /*protect=*/1,
2775 /*want_type=*/false, complain
);
2776 if (member
== NULL_TREE
)
2778 if (complain
& tf_error
)
2779 error ("%qD has no member named %qE",
2780 TREE_CODE (access_path
) == TREE_BINFO
2781 ? TREE_TYPE (access_path
) : object_type
, name
);
2782 return error_mark_node
;
2784 if (member
== error_mark_node
)
2785 return error_mark_node
;
2790 tree templ
= member
;
2792 if (BASELINK_P (templ
))
2793 templ
= lookup_template_function (templ
, template_args
);
2796 if (complain
& tf_error
)
2797 error ("%qD is not a member template function", name
);
2798 return error_mark_node
;
2803 if (TREE_DEPRECATED (member
))
2804 warn_deprecated_use (member
, NULL_TREE
);
2807 check_template_keyword (member
);
2809 expr
= build_class_member_access_expr (object
, member
, access_path
,
2810 /*preserve_reference=*/false,
2812 if (processing_template_decl
&& expr
!= error_mark_node
)
2814 if (BASELINK_P (member
))
2816 if (TREE_CODE (orig_name
) == SCOPE_REF
)
2817 BASELINK_QUALIFIED_P (member
) = 1;
2820 return build_min_non_dep (COMPONENT_REF
, expr
,
2821 orig_object
, orig_name
,
2828 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2832 build_simple_component_ref (tree object
, tree member
)
2834 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
2835 cp_type_quals (TREE_TYPE (object
)));
2836 return fold_build3_loc (input_location
,
2837 COMPONENT_REF
, type
,
2838 object
, member
, NULL_TREE
);
2841 /* Return an expression for the MEMBER_NAME field in the internal
2842 representation of PTRMEM, a pointer-to-member function. (Each
2843 pointer-to-member function type gets its own RECORD_TYPE so it is
2844 more convenient to access the fields by name than by FIELD_DECL.)
2845 This routine converts the NAME to a FIELD_DECL and then creates the
2846 node for the complete expression. */
2849 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
2854 /* This code is a stripped down version of
2855 build_class_member_access_expr. It does not work to use that
2856 routine directly because it expects the object to be of class
2858 ptrmem_type
= TREE_TYPE (ptrmem
);
2859 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
2860 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
2861 member
= DECL_CHAIN (member
))
2862 if (DECL_NAME (member
) == member_name
)
2864 return build_simple_component_ref (ptrmem
, member
);
2867 /* Given an expression PTR for a pointer, return an expression
2868 for the value pointed to.
2869 ERRORSTRING is the name of the operator to appear in error messages.
2871 This function may need to overload OPERATOR_FNNAME.
2872 Must also handle REFERENCE_TYPEs for C++. */
2875 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
2876 tsubst_flags_t complain
)
2878 tree orig_expr
= expr
;
2881 if (processing_template_decl
)
2883 /* Retain the type if we know the operand is a pointer. */
2884 if (TREE_TYPE (expr
) && POINTER_TYPE_P (TREE_TYPE (expr
)))
2885 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
2886 if (type_dependent_expression_p (expr
))
2887 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
2888 expr
= build_non_dependent_expr (expr
);
2891 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
2892 NULL_TREE
, NULL_TREE
, /*overload=*/NULL
, complain
);
2894 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
2896 if (processing_template_decl
&& rval
!= error_mark_node
)
2897 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
2902 /* Helper function called from c-common. */
2904 build_indirect_ref (location_t
/*loc*/,
2905 tree ptr
, ref_operator errorstring
)
2907 return cp_build_indirect_ref (ptr
, errorstring
, tf_warning_or_error
);
2911 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
2912 tsubst_flags_t complain
)
2916 if (ptr
== current_class_ptr
2917 || (TREE_CODE (ptr
) == NOP_EXPR
2918 && TREE_OPERAND (ptr
, 0) == current_class_ptr
2919 && (same_type_ignoring_top_level_qualifiers_p
2920 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
2921 return current_class_ref
;
2923 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2924 ? ptr
: decay_conversion (ptr
, complain
));
2925 if (pointer
== error_mark_node
)
2926 return error_mark_node
;
2928 type
= TREE_TYPE (pointer
);
2930 if (POINTER_TYPE_P (type
))
2934 If the type of the expression is "pointer to T," the type
2935 of the result is "T." */
2936 tree t
= TREE_TYPE (type
);
2938 if ((CONVERT_EXPR_P (ptr
)
2939 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
2940 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
2942 /* If a warning is issued, mark it to avoid duplicates from
2943 the backend. This only needs to be done at
2944 warn_strict_aliasing > 2. */
2945 if (warn_strict_aliasing
> 2)
2946 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr
, 0)),
2947 type
, TREE_OPERAND (ptr
, 0)))
2948 TREE_NO_WARNING (ptr
) = 1;
2951 if (VOID_TYPE_P (t
))
2953 /* A pointer to incomplete type (other than cv void) can be
2954 dereferenced [expr.unary.op]/1 */
2955 if (complain
& tf_error
)
2956 error ("%qT is not a pointer-to-object type", type
);
2957 return error_mark_node
;
2959 else if (TREE_CODE (pointer
) == ADDR_EXPR
2960 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2961 /* The POINTER was something like `&x'. We simplify `*&x' to
2963 return TREE_OPERAND (pointer
, 0);
2966 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2968 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2969 so that we get the proper error message if the result is used
2970 to assign to. Also, &* is supposed to be a no-op. */
2971 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2972 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2973 TREE_SIDE_EFFECTS (ref
)
2974 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
2978 else if (!(complain
& tf_error
))
2979 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2981 /* `pointer' won't be an error_mark_node if we were given a
2982 pointer to member, so it's cool to check for this here. */
2983 else if (TYPE_PTRMEM_P (type
))
2984 switch (errorstring
)
2986 case RO_ARRAY_INDEXING
:
2987 error ("invalid use of array indexing on pointer to member");
2990 error ("invalid use of unary %<*%> on pointer to member");
2992 case RO_IMPLICIT_CONVERSION
:
2993 error ("invalid use of implicit conversion on pointer to member");
2996 error ("left hand operand of %<->*%> must be a pointer to class, "
2997 "but is a pointer to member of type %qT", type
);
3002 else if (pointer
!= error_mark_node
)
3003 invalid_indirection_error (input_location
, type
, errorstring
);
3005 return error_mark_node
;
3008 /* This handles expressions of the form "a[i]", which denotes
3011 This is logically equivalent in C to *(a+i), but we may do it differently.
3012 If A is a variable or a member, we generate a primitive ARRAY_REF.
3013 This avoids forcing the array out of registers, and can work on
3014 arrays that are not lvalues (for example, members of structures returned
3017 If INDEX is of some user-defined type, it must be converted to
3018 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3019 will inherit the type of the array, which will be some pointer type.
3021 LOC is the location to use in building the array reference. */
3024 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3025 tsubst_flags_t complain
)
3031 if (complain
& tf_error
)
3032 error_at (loc
, "subscript missing in array reference");
3033 return error_mark_node
;
3036 /* If an array's index is an array notation, then its rank cannot be
3037 greater than one. */
3038 if (flag_cilkplus
&& contains_array_notation_expr (idx
))
3042 /* If find_rank returns false, then it should have reported an error,
3043 thus it is unnecessary for repetition. */
3044 if (!find_rank (loc
, idx
, idx
, true, &rank
))
3045 return error_mark_node
;
3048 error_at (loc
, "rank of the array%'s index is greater than 1");
3049 return error_mark_node
;
3052 if (TREE_TYPE (array
) == error_mark_node
3053 || TREE_TYPE (idx
) == error_mark_node
)
3054 return error_mark_node
;
3056 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3058 switch (TREE_CODE (array
))
3062 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3064 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3065 TREE_OPERAND (array
, 0), value
);
3066 SET_EXPR_LOCATION (ret
, loc
);
3071 ret
= build_conditional_expr
3072 (loc
, TREE_OPERAND (array
, 0),
3073 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3075 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3078 protected_set_expr_location (ret
, loc
);
3085 convert_vector_to_pointer_for_subscript (loc
, &array
, idx
);
3087 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3091 warn_array_subscript_with_type_char (idx
);
3093 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3095 if (complain
& tf_error
)
3096 error_at (loc
, "array subscript is not an integer");
3097 return error_mark_node
;
3100 /* Apply integral promotions *after* noticing character types.
3101 (It is unclear why we do these promotions -- the standard
3102 does not say that we should. In fact, the natural thing would
3103 seem to be to convert IDX to ptrdiff_t; we're performing
3104 pointer arithmetic.) */
3105 idx
= cp_perform_integral_promotions (idx
, complain
);
3107 /* An array that is indexed by a non-constant
3108 cannot be stored in a register; we must be able to do
3109 address arithmetic on its address.
3110 Likewise an array of elements of variable size. */
3111 if (TREE_CODE (idx
) != INTEGER_CST
3112 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3113 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3116 if (!cxx_mark_addressable (array
))
3117 return error_mark_node
;
3120 /* An array that is indexed by a constant value which is not within
3121 the array bounds cannot be stored in a register either; because we
3122 would get a crash in store_bit_field/extract_bit_field when trying
3123 to access a non-existent part of the register. */
3124 if (TREE_CODE (idx
) == INTEGER_CST
3125 && TYPE_DOMAIN (TREE_TYPE (array
))
3126 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3128 if (!cxx_mark_addressable (array
))
3129 return error_mark_node
;
3132 if (!lvalue_p (array
) && (complain
& tf_error
))
3133 pedwarn (loc
, OPT_Wpedantic
,
3134 "ISO C++ forbids subscripting non-lvalue array");
3136 /* Note in C++ it is valid to subscript a `register' array, since
3137 it is valid to take the address of something with that
3138 storage specification. */
3142 while (TREE_CODE (foo
) == COMPONENT_REF
)
3143 foo
= TREE_OPERAND (foo
, 0);
3144 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3145 && (complain
& tf_warning
))
3146 warning_at (loc
, OPT_Wextra
,
3147 "subscripting array declared %<register%>");
3150 type
= TREE_TYPE (TREE_TYPE (array
));
3151 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3152 /* Array ref is const/volatile if the array elements are
3153 or if the array is.. */
3154 TREE_READONLY (rval
)
3155 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3156 TREE_SIDE_EFFECTS (rval
)
3157 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3158 TREE_THIS_VOLATILE (rval
)
3159 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3160 ret
= require_complete_type_sfinae (fold_if_not_in_template (rval
),
3162 protected_set_expr_location (ret
, loc
);
3167 tree ar
= cp_default_conversion (array
, complain
);
3168 tree ind
= cp_default_conversion (idx
, complain
);
3170 /* Put the integer in IND to simplify error checking. */
3171 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3178 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3179 return error_mark_node
;
3181 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3183 if (complain
& tf_error
)
3184 error_at (loc
, "subscripted value is neither array nor pointer");
3185 return error_mark_node
;
3187 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3189 if (complain
& tf_error
)
3190 error_at (loc
, "array subscript is not an integer");
3191 return error_mark_node
;
3194 warn_array_subscript_with_type_char (idx
);
3196 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3201 protected_set_expr_location (ret
, loc
);
3206 /* Entry point for Obj-C++. */
3209 build_array_ref (location_t loc
, tree array
, tree idx
)
3211 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3214 /* Resolve a pointer to member function. INSTANCE is the object
3215 instance to use, if the member points to a virtual member.
3217 This used to avoid checking for virtual functions if basetype
3218 has no virtual functions, according to an earlier ANSI draft.
3219 With the final ISO C++ rules, such an optimization is
3220 incorrect: A pointer to a derived member can be static_cast
3221 to pointer-to-base-member, as long as the dynamic object
3222 later has the right member. So now we only do this optimization
3223 when we know the dynamic type of the object. */
3226 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3227 tsubst_flags_t complain
)
3229 if (TREE_CODE (function
) == OFFSET_REF
)
3230 function
= TREE_OPERAND (function
, 1);
3232 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3234 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3236 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3237 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3239 tree instance_ptr
= *instance_ptrptr
;
3240 tree instance_save_expr
= 0;
3241 if (instance_ptr
== error_mark_node
)
3243 if (TREE_CODE (function
) == PTRMEM_CST
)
3245 /* Extracting the function address from a pmf is only
3246 allowed with -Wno-pmf-conversions. It only works for
3248 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3249 e1
= convert (fntype
, e1
);
3254 if (complain
& tf_error
)
3255 error ("object missing in use of %qE", function
);
3256 return error_mark_node
;
3260 /* True if we know that the dynamic type of the object doesn't have
3261 virtual functions, so we can assume the PFN field is a pointer. */
3262 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3263 && !TYPE_POLYMORPHIC_P (basetype
)
3264 && resolves_to_fixed_type_p (instance_ptr
, 0));
3266 /* If we don't really have an object (i.e. in an ill-formed
3267 conversion from PMF to pointer), we can't resolve virtual
3268 functions anyway. */
3269 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3272 if (TREE_SIDE_EFFECTS (instance_ptr
))
3273 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3275 if (TREE_SIDE_EFFECTS (function
))
3276 function
= save_expr (function
);
3278 /* Start by extracting all the information from the PMF itself. */
3279 e3
= pfn_from_ptrmemfunc (function
);
3280 delta
= delta_from_ptrmemfunc (function
);
3281 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3282 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3284 case ptrmemfunc_vbit_in_pfn
:
3285 e1
= cp_build_binary_op (input_location
,
3286 BIT_AND_EXPR
, idx
, integer_one_node
,
3288 idx
= cp_build_binary_op (input_location
,
3289 MINUS_EXPR
, idx
, integer_one_node
,
3291 if (idx
== error_mark_node
)
3292 return error_mark_node
;
3295 case ptrmemfunc_vbit_in_delta
:
3296 e1
= cp_build_binary_op (input_location
,
3297 BIT_AND_EXPR
, delta
, integer_one_node
,
3299 delta
= cp_build_binary_op (input_location
,
3300 RSHIFT_EXPR
, delta
, integer_one_node
,
3302 if (delta
== error_mark_node
)
3303 return error_mark_node
;
3310 if (e1
== error_mark_node
)
3311 return error_mark_node
;
3313 /* Convert down to the right base before using the instance. A
3314 special case is that in a pointer to member of class C, C may
3315 be incomplete. In that case, the function will of course be
3316 a member of C, and no conversion is required. In fact,
3317 lookup_base will fail in that case, because incomplete
3318 classes do not have BINFOs. */
3319 if (!same_type_ignoring_top_level_qualifiers_p
3320 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3322 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3323 basetype
, ba_check
, NULL
, complain
);
3324 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3326 if (instance_ptr
== error_mark_node
)
3327 return error_mark_node
;
3329 /* ...and then the delta in the PMF. */
3330 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3332 /* Hand back the adjusted 'this' argument to our caller. */
3333 *instance_ptrptr
= instance_ptr
;
3336 /* Now just return the pointer. */
3339 /* Next extract the vtable pointer from the object. */
3340 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3342 vtbl
= cp_build_indirect_ref (vtbl
, RO_NULL
, complain
);
3343 if (vtbl
== error_mark_node
)
3344 return error_mark_node
;
3346 /* Finally, extract the function pointer from the vtable. */
3347 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3348 e2
= cp_build_indirect_ref (e2
, RO_NULL
, complain
);
3349 if (e2
== error_mark_node
)
3350 return error_mark_node
;
3351 TREE_CONSTANT (e2
) = 1;
3353 /* When using function descriptors, the address of the
3354 vtable entry is treated as a function pointer. */
3355 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3356 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3357 cp_build_addr_expr (e2
, complain
));
3359 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3360 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3361 if (e1
== error_mark_node
)
3362 return error_mark_node
;
3364 /* Make sure this doesn't get evaluated first inside one of the
3365 branches of the COND_EXPR. */
3366 if (instance_save_expr
)
3367 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3368 instance_save_expr
, e1
);
3375 /* Used by the C-common bits. */
3377 build_function_call (location_t
/*loc*/,
3378 tree function
, tree params
)
3380 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3383 /* Used by the C-common bits. */
3385 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3386 tree function
, vec
<tree
, va_gc
> *params
,
3387 vec
<tree
, va_gc
> * /*origtypes*/)
3389 vec
<tree
, va_gc
> *orig_params
= params
;
3390 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3391 tf_warning_or_error
);
3393 /* cp_build_function_call_vec can reallocate PARAMS by adding
3394 default arguments. That should never happen here. Verify
3396 gcc_assert (params
== orig_params
);
3401 /* Build a function call using a tree list of arguments. */
3404 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3406 vec
<tree
, va_gc
> *vec
;
3409 vec
= make_tree_vector ();
3410 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3411 vec_safe_push (vec
, TREE_VALUE (params
));
3412 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3413 release_tree_vector (vec
);
3417 /* Build a function call using varargs. */
3420 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3422 vec
<tree
, va_gc
> *vec
;
3426 vec
= make_tree_vector ();
3427 va_start (args
, complain
);
3428 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3429 vec_safe_push (vec
, t
);
3431 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3432 release_tree_vector (vec
);
3436 /* Build a function call using a vector of arguments. PARAMS may be
3437 NULL if there are no parameters. This changes the contents of
3441 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3442 tsubst_flags_t complain
)
3444 tree fntype
, fndecl
;
3446 tree original
= function
;
3450 vec
<tree
, va_gc
> *allocated
= NULL
;
3453 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3454 expressions, like those used for ObjC messenger dispatches. */
3455 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3456 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3458 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3459 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3460 if (TREE_CODE (function
) == NOP_EXPR
3461 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3462 function
= TREE_OPERAND (function
, 0);
3464 if (TREE_CODE (function
) == FUNCTION_DECL
)
3466 mark_used (function
);
3469 /* Convert anything with function type to a pointer-to-function. */
3470 if (DECL_MAIN_P (function
) && (complain
& tf_error
))
3471 pedwarn (input_location
, OPT_Wpedantic
,
3472 "ISO C++ forbids calling %<::main%> from within program");
3474 function
= build_addr_func (function
, complain
);
3480 function
= build_addr_func (function
, complain
);
3483 if (function
== error_mark_node
)
3484 return error_mark_node
;
3486 fntype
= TREE_TYPE (function
);
3488 if (TYPE_PTRMEMFUNC_P (fntype
))
3490 if (complain
& tf_error
)
3491 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3492 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3493 original
, original
);
3494 return error_mark_node
;
3497 is_method
= (TYPE_PTR_P (fntype
)
3498 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3500 if (!(TYPE_PTRFN_P (fntype
)
3502 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3504 if (complain
& tf_error
)
3506 if (!flag_diagnostics_show_caret
)
3507 error_at (input_location
,
3508 "%qE cannot be used as a function", original
);
3509 else if (DECL_P (original
))
3510 error_at (input_location
,
3511 "%qD cannot be used as a function", original
);
3513 error_at (input_location
,
3514 "expression cannot be used as a function");
3517 return error_mark_node
;
3520 /* fntype now gets the type of function pointed to. */
3521 fntype
= TREE_TYPE (fntype
);
3522 parm_types
= TYPE_ARG_TYPES (fntype
);
3526 allocated
= make_tree_vector ();
3527 params
= &allocated
;
3530 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3533 return error_mark_node
;
3535 argarray
= (*params
)->address ();
3537 /* Check for errors in format strings and inappropriately
3539 check_function_arguments (fntype
, nargs
, argarray
);
3541 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3543 if (allocated
!= NULL
)
3544 release_tree_vector (allocated
);
3549 /* Subroutine of convert_arguments.
3550 Warn about wrong number of args are genereted. */
3553 warn_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3557 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3559 if (DECL_NAME (fndecl
) == NULL_TREE
3560 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3563 ? G_("too many arguments to constructor %q#D")
3564 : G_("too few arguments to constructor %q#D"),
3569 ? G_("too many arguments to member function %q#D")
3570 : G_("too few arguments to member function %q#D"),
3576 ? G_("too many arguments to function %q#D")
3577 : G_("too few arguments to function %q#D"),
3579 inform (DECL_SOURCE_LOCATION (fndecl
),
3584 if (c_dialect_objc () && objc_message_selector ())
3587 ? G_("too many arguments to method %q#D")
3588 : G_("too few arguments to method %q#D"),
3589 objc_message_selector ());
3591 error_at (loc
, too_many_p
? G_("too many arguments to function")
3592 : G_("too few arguments to function"));
3596 /* Convert the actual parameter expressions in the list VALUES to the
3597 types in the list TYPELIST. The converted expressions are stored
3598 back in the VALUES vector.
3599 If parmdecls is exhausted, or when an element has NULL as its type,
3600 perform the default conversions.
3602 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3604 This is also where warnings about wrong number of args are generated.
3606 Returns the actual number of arguments processed (which might be less
3607 than the length of the vector), or -1 on error.
3609 In C++, unspecified trailing parameters can be filled in with their
3610 default arguments, if such were specified. Do so here. */
3613 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3614 int flags
, tsubst_flags_t complain
)
3619 /* Argument passing is always copy-initialization. */
3620 flags
|= LOOKUP_ONLYCONVERTING
;
3622 for (i
= 0, typetail
= typelist
;
3623 i
< vec_safe_length (*values
);
3626 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3627 tree val
= (**values
)[i
];
3629 if (val
== error_mark_node
|| type
== error_mark_node
)
3632 if (type
== void_type_node
)
3634 if (complain
& tf_error
)
3636 warn_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3643 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3644 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3645 if (TREE_CODE (val
) == NOP_EXPR
3646 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3647 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3648 val
= TREE_OPERAND (val
, 0);
3650 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3652 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3653 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3654 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3655 val
= decay_conversion (val
, complain
);
3658 if (val
== error_mark_node
)
3663 /* Formal parm type is specified by a function prototype. */
3666 if (!COMPLETE_TYPE_P (complete_type (type
)))
3668 if (complain
& tf_error
)
3671 error ("parameter %P of %qD has incomplete type %qT",
3674 error ("parameter %P has incomplete type %qT", i
, type
);
3676 parmval
= error_mark_node
;
3680 parmval
= convert_for_initialization
3681 (NULL_TREE
, type
, val
, flags
,
3682 ICR_ARGPASS
, fndecl
, i
, complain
);
3683 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
3686 if (parmval
== error_mark_node
)
3689 (**values
)[i
] = parmval
;
3693 if (fndecl
&& magic_varargs_p (fndecl
))
3694 /* Don't do ellipsis conversion for __built_in_constant_p
3695 as this will result in spurious errors for non-trivial
3697 val
= require_complete_type_sfinae (val
, complain
);
3699 val
= convert_arg_to_ellipsis (val
, complain
);
3701 (**values
)[i
] = val
;
3705 typetail
= TREE_CHAIN (typetail
);
3708 if (typetail
!= 0 && typetail
!= void_list_node
)
3710 /* See if there are default arguments that can be used. Because
3711 we hold default arguments in the FUNCTION_TYPE (which is so
3712 wrong), we can see default parameters here from deduced
3713 contexts (and via typeof) for indirect function calls.
3714 Fortunately we know whether we have a function decl to
3715 provide default arguments in a language conformant
3717 if (fndecl
&& TREE_PURPOSE (typetail
)
3718 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
3720 for (; typetail
!= void_list_node
; ++i
)
3723 = convert_default_arg (TREE_VALUE (typetail
),
3724 TREE_PURPOSE (typetail
),
3725 fndecl
, i
, complain
);
3727 if (parmval
== error_mark_node
)
3730 vec_safe_push (*values
, parmval
);
3731 typetail
= TREE_CHAIN (typetail
);
3732 /* ends with `...'. */
3733 if (typetail
== NULL_TREE
)
3739 if (complain
& tf_error
)
3740 warn_args_num (input_location
, fndecl
, /*too_many_p=*/false);
3748 /* Build a binary-operation expression, after performing default
3749 conversions on the operands. CODE is the kind of expression to
3750 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3751 are the tree codes which correspond to ARG1 and ARG2 when issuing
3752 warnings about possibly misplaced parentheses. They may differ
3753 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3754 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3755 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3756 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3757 ARG2_CODE as ERROR_MARK. */
3760 build_x_binary_op (location_t loc
, enum tree_code code
, tree arg1
,
3761 enum tree_code arg1_code
, tree arg2
,
3762 enum tree_code arg2_code
, tree
*overload
,
3763 tsubst_flags_t complain
)
3772 if (processing_template_decl
)
3774 if (type_dependent_expression_p (arg1
)
3775 || type_dependent_expression_p (arg2
))
3776 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
3777 arg1
= build_non_dependent_expr (arg1
);
3778 arg2
= build_non_dependent_expr (arg2
);
3781 if (code
== DOTSTAR_EXPR
)
3782 expr
= build_m_component_ref (arg1
, arg2
, complain
);
3784 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
3785 overload
, complain
);
3787 /* Check for cases such as x+y<<z which users are likely to
3788 misinterpret. But don't warn about obj << x + y, since that is a
3789 common idiom for I/O. */
3790 if (warn_parentheses
3791 && (complain
& tf_warning
)
3792 && !processing_template_decl
3793 && !error_operand_p (arg1
)
3794 && !error_operand_p (arg2
)
3795 && (code
!= LSHIFT_EXPR
3796 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
3797 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
3798 arg2_code
, orig_arg2
);
3800 if (processing_template_decl
&& expr
!= error_mark_node
)
3801 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
3806 /* Build and return an ARRAY_REF expression. */
3809 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
3810 tsubst_flags_t complain
)
3812 tree orig_arg1
= arg1
;
3813 tree orig_arg2
= arg2
;
3816 if (processing_template_decl
)
3818 if (type_dependent_expression_p (arg1
)
3819 || type_dependent_expression_p (arg2
))
3820 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
3821 NULL_TREE
, NULL_TREE
);
3822 arg1
= build_non_dependent_expr (arg1
);
3823 arg2
= build_non_dependent_expr (arg2
);
3826 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
3827 NULL_TREE
, /*overload=*/NULL
, complain
);
3829 if (processing_template_decl
&& expr
!= error_mark_node
)
3830 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
3831 NULL_TREE
, NULL_TREE
);
3835 /* Return whether OP is an expression of enum type cast to integer
3836 type. In C++ even unsigned enum types are cast to signed integer
3837 types. We do not want to issue warnings about comparisons between
3838 signed and unsigned types when one of the types is an enum type.
3839 Those warnings are always false positives in practice. */
3842 enum_cast_to_int (tree op
)
3844 if (TREE_CODE (op
) == NOP_EXPR
3845 && TREE_TYPE (op
) == integer_type_node
3846 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
3847 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
3850 /* The cast may have been pushed into a COND_EXPR. */
3851 if (TREE_CODE (op
) == COND_EXPR
)
3852 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
3853 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
3858 /* For the c-common bits. */
3860 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
3863 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
3867 /* Build a binary-operation expression without default conversions.
3868 CODE is the kind of expression to build.
3869 LOCATION is the location_t of the operator in the source code.
3870 This function differs from `build' in several ways:
3871 the data type of the result is computed and recorded in it,
3872 warnings are generated if arg data types are invalid,
3873 special handling for addition and subtraction of pointers is known,
3874 and some optimization is done (operations on narrow ints
3875 are done in the narrower type when that gives the same result).
3876 Constant folding is also done before the result is returned.
3878 Note that the operands will never have enumeral types
3879 because either they have just had the default conversions performed
3880 or they have both just been converted to some other type in which
3881 the arithmetic is to be done.
3883 C++: must do special pointer arithmetic when implementing
3884 multiple inheritance, and deal with pointer to member functions. */
3887 cp_build_binary_op (location_t location
,
3888 enum tree_code code
, tree orig_op0
, tree orig_op1
,
3889 tsubst_flags_t complain
)
3892 enum tree_code code0
, code1
;
3894 const char *invalid_op_diag
;
3896 /* Expression code to give to the expression when it is built.
3897 Normally this is CODE, which is what the caller asked for,
3898 but in some special cases we change it. */
3899 enum tree_code resultcode
= code
;
3901 /* Data type in which the computation is to be performed.
3902 In the simplest cases this is the common type of the arguments. */
3903 tree result_type
= NULL
;
3905 /* Nonzero means operands have already been type-converted
3906 in whatever way is necessary.
3907 Zero means they need to be converted to RESULT_TYPE. */
3910 /* Nonzero means create the expression with this type, rather than
3912 tree build_type
= 0;
3914 /* Nonzero means after finally constructing the expression
3915 convert it to this type. */
3916 tree final_type
= 0;
3919 tree orig_type
= NULL
;
3921 /* Nonzero if this is an operation like MIN or MAX which can
3922 safely be computed in short if both args are promoted shorts.
3923 Also implies COMMON.
3924 -1 indicates a bitwise operation; this makes a difference
3925 in the exact conditions for when it is safe to do the operation
3926 in a narrower mode. */
3929 /* Nonzero if this is a comparison operation;
3930 if both args are promoted shorts, compare the original shorts.
3931 Also implies COMMON. */
3932 int short_compare
= 0;
3934 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3937 /* True if both operands have arithmetic type. */
3938 bool arithmetic_types_p
;
3940 /* Apply default conversions. */
3944 /* Remember whether we're doing / or %. */
3945 bool doing_div_or_mod
= false;
3947 /* Remember whether we're doing << or >>. */
3948 bool doing_shift
= false;
3950 /* Tree holding instrumentation expression. */
3951 tree instrument_expr
= NULL
;
3953 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3954 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3955 || code
== TRUTH_XOR_EXPR
)
3957 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3958 op0
= decay_conversion (op0
, complain
);
3959 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3960 op1
= decay_conversion (op1
, complain
);
3964 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3965 op0
= cp_default_conversion (op0
, complain
);
3966 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3967 op1
= cp_default_conversion (op1
, complain
);
3970 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3971 STRIP_TYPE_NOPS (op0
);
3972 STRIP_TYPE_NOPS (op1
);
3974 /* DTRT if one side is an overloaded function, but complain about it. */
3975 if (type_unknown_p (op0
))
3977 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
3978 if (t
!= error_mark_node
)
3980 if (complain
& tf_error
)
3981 permerror (input_location
, "assuming cast to type %qT from overloaded function",
3986 if (type_unknown_p (op1
))
3988 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
3989 if (t
!= error_mark_node
)
3991 if (complain
& tf_error
)
3992 permerror (input_location
, "assuming cast to type %qT from overloaded function",
3998 type0
= TREE_TYPE (op0
);
3999 type1
= TREE_TYPE (op1
);
4001 /* The expression codes of the data types of the arguments tell us
4002 whether the arguments are integers, floating, pointers, etc. */
4003 code0
= TREE_CODE (type0
);
4004 code1
= TREE_CODE (type1
);
4006 /* If an error was already reported for one of the arguments,
4007 avoid reporting another error. */
4008 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4009 return error_mark_node
;
4011 if ((invalid_op_diag
4012 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4014 if (complain
& tf_error
)
4015 error (invalid_op_diag
);
4016 return error_mark_node
;
4019 /* Issue warnings about peculiar, but valid, uses of NULL. */
4020 if ((orig_op0
== null_node
|| orig_op1
== null_node
)
4021 /* It's reasonable to use pointer values as operands of &&
4022 and ||, so NULL is no exception. */
4023 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4024 && ( /* Both are NULL (or 0) and the operation was not a
4025 comparison or a pointer subtraction. */
4026 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4027 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4028 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4029 || (!null_ptr_cst_p (orig_op0
)
4030 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4031 || (!null_ptr_cst_p (orig_op1
)
4032 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4033 && (complain
& tf_warning
))
4035 source_location loc
=
4036 expansion_point_location_if_in_system_header (input_location
);
4038 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4041 /* In case when one of the operands of the binary operation is
4042 a vector and another is a scalar -- convert scalar to vector. */
4043 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4045 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4046 complain
& tf_error
);
4048 switch (convert_flag
)
4051 return error_mark_node
;
4054 op0
= save_expr (op0
);
4055 op0
= convert (TREE_TYPE (type1
), op0
);
4056 op0
= build_vector_from_val (type1
, op0
);
4057 type0
= TREE_TYPE (op0
);
4058 code0
= TREE_CODE (type0
);
4064 op1
= save_expr (op1
);
4065 op1
= convert (TREE_TYPE (type0
), op1
);
4066 op1
= build_vector_from_val (type0
, op1
);
4067 type1
= TREE_TYPE (op1
);
4068 code1
= TREE_CODE (type1
);
4080 /* Subtraction of two similar pointers.
4081 We must subtract them as integers, then divide by object size. */
4082 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4083 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4085 return pointer_diff (op0
, op1
, common_pointer_type (type0
, type1
),
4087 /* In all other cases except pointer - int, the usual arithmetic
4089 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4094 /* The pointer - int case is just like pointer + int; fall
4097 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4098 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4102 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4103 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4104 if (processing_template_decl
)
4106 result_type
= TREE_TYPE (ptr_operand
);
4109 return cp_pointer_int_sum (code
,
4121 case TRUNC_DIV_EXPR
:
4123 case FLOOR_DIV_EXPR
:
4124 case ROUND_DIV_EXPR
:
4125 case EXACT_DIV_EXPR
:
4126 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4127 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4128 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4129 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4131 enum tree_code tcode0
= code0
, tcode1
= code1
;
4132 tree cop1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4133 cop1
= maybe_constant_value (cop1
);
4134 doing_div_or_mod
= true;
4135 warn_for_div_by_zero (location
, cop1
);
4137 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4138 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4139 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4140 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4142 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4143 resultcode
= RDIV_EXPR
;
4145 /* When dividing two signed integers, we have to promote to int.
4146 unless we divide by a constant != -1. Note that default
4147 conversion will have been performed on the operands at this
4148 point, so we have to dig out the original type to find out if
4150 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4151 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4152 || (TREE_CODE (op1
) == INTEGER_CST
4153 && ! integer_all_onesp (op1
)));
4162 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4163 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4164 && !VECTOR_FLOAT_TYPE_P (type0
)
4165 && !VECTOR_FLOAT_TYPE_P (type1
)))
4169 case TRUNC_MOD_EXPR
:
4170 case FLOOR_MOD_EXPR
:
4172 tree cop1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4173 cop1
= maybe_constant_value (cop1
);
4174 doing_div_or_mod
= true;
4175 warn_for_div_by_zero (location
, cop1
);
4178 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4179 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4180 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4182 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4184 /* Although it would be tempting to shorten always here, that loses
4185 on some targets, since the modulo instruction is undefined if the
4186 quotient can't be represented in the computation mode. We shorten
4187 only if unsigned or if dividing by something we know != -1. */
4188 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4189 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4190 || (TREE_CODE (op1
) == INTEGER_CST
4191 && ! integer_all_onesp (op1
)));
4196 case TRUTH_ANDIF_EXPR
:
4197 case TRUTH_ORIF_EXPR
:
4198 case TRUTH_AND_EXPR
:
4200 if (VECTOR_TYPE_P (type0
) || VECTOR_TYPE_P (type1
))
4202 sorry ("logical operation on vector type");
4203 return error_mark_node
;
4205 result_type
= boolean_type_node
;
4208 /* Shift operations: result has same type as first operand;
4209 always convert second operand to int.
4210 Also set SHORT_SHIFT if shifting rightward. */
4213 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4214 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4216 result_type
= type0
;
4219 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4220 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4221 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4222 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4224 result_type
= type0
;
4227 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4229 tree const_op1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4230 const_op1
= maybe_constant_value (const_op1
);
4231 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4233 result_type
= type0
;
4235 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4237 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4239 if ((complain
& tf_warning
)
4240 && c_inhibit_evaluation_warnings
== 0)
4241 warning (0, "right shift count is negative");
4245 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4246 && (complain
& tf_warning
)
4247 && c_inhibit_evaluation_warnings
== 0)
4248 warning (0, "right shift count >= width of type");
4251 /* Convert the shift-count to an integer, regardless of
4252 size of value being shifted. */
4253 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4254 op1
= cp_convert (integer_type_node
, op1
, complain
);
4255 /* Avoid converting op1 to result_type later. */
4261 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4262 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4264 result_type
= type0
;
4267 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4268 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4269 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4270 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4272 result_type
= type0
;
4275 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4277 tree const_op1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4278 const_op1
= maybe_constant_value (const_op1
);
4279 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4281 result_type
= type0
;
4283 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4285 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4287 if ((complain
& tf_warning
)
4288 && c_inhibit_evaluation_warnings
== 0)
4289 warning (0, "left shift count is negative");
4291 else if (compare_tree_int (const_op1
,
4292 TYPE_PRECISION (type0
)) >= 0)
4294 if ((complain
& tf_warning
)
4295 && c_inhibit_evaluation_warnings
== 0)
4296 warning (0, "left shift count >= width of type");
4299 /* Convert the shift-count to an integer, regardless of
4300 size of value being shifted. */
4301 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4302 op1
= cp_convert (integer_type_node
, op1
, complain
);
4303 /* Avoid converting op1 to result_type later. */
4310 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4312 result_type
= type0
;
4313 if (TREE_CODE (op1
) == INTEGER_CST
)
4315 if (tree_int_cst_lt (op1
, integer_zero_node
))
4317 if (complain
& tf_warning
)
4318 warning (0, (code
== LROTATE_EXPR
)
4319 ? G_("left rotate count is negative")
4320 : G_("right rotate count is negative"));
4322 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4324 if (complain
& tf_warning
)
4325 warning (0, (code
== LROTATE_EXPR
)
4326 ? G_("left rotate count >= width of type")
4327 : G_("right rotate count >= width of type"));
4330 /* Convert the shift-count to an integer, regardless of
4331 size of value being shifted. */
4332 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4333 op1
= cp_convert (integer_type_node
, op1
, complain
);
4339 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4340 goto vector_compare
;
4341 if ((complain
& tf_warning
)
4342 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4343 warning (OPT_Wfloat_equal
,
4344 "comparing floating point with == or != is unsafe");
4345 if ((complain
& tf_warning
)
4346 && ((TREE_CODE (orig_op0
) == STRING_CST
&& !integer_zerop (op1
))
4347 || (TREE_CODE (orig_op1
) == STRING_CST
&& !integer_zerop (op0
))))
4348 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4350 build_type
= boolean_type_node
;
4351 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4352 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4353 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4354 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4356 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4357 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4358 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4359 CPO_COMPARISON
, complain
);
4360 else if ((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4361 && null_ptr_cst_p (op1
))
4363 if (TREE_CODE (op0
) == ADDR_EXPR
4364 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
4366 if ((complain
& tf_warning
)
4367 && c_inhibit_evaluation_warnings
== 0)
4368 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4369 TREE_OPERAND (op0
, 0));
4371 result_type
= type0
;
4373 else if ((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4374 && null_ptr_cst_p (op0
))
4376 if (TREE_CODE (op1
) == ADDR_EXPR
4377 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
4379 if ((complain
& tf_warning
)
4380 && c_inhibit_evaluation_warnings
== 0)
4381 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4382 TREE_OPERAND (op1
, 0));
4384 result_type
= type1
;
4386 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4387 /* One of the operands must be of nullptr_t type. */
4388 result_type
= TREE_TYPE (nullptr_node
);
4389 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4391 result_type
= type0
;
4392 if (complain
& tf_error
)
4393 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4395 return error_mark_node
;
4397 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4399 result_type
= type1
;
4400 if (complain
& tf_error
)
4401 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4403 return error_mark_node
;
4405 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (op1
))
4407 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4408 == ptrmemfunc_vbit_in_delta
)
4410 tree pfn0
, delta0
, e1
, e2
;
4412 if (TREE_SIDE_EFFECTS (op0
))
4413 op0
= save_expr (op0
);
4415 pfn0
= pfn_from_ptrmemfunc (op0
);
4416 delta0
= delta_from_ptrmemfunc (op0
);
4417 e1
= cp_build_binary_op (location
,
4420 build_zero_cst (TREE_TYPE (pfn0
)),
4422 e2
= cp_build_binary_op (location
,
4428 if (complain
& tf_warning
)
4429 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4431 e2
= cp_build_binary_op (location
,
4432 EQ_EXPR
, e2
, integer_zero_node
,
4434 op0
= cp_build_binary_op (location
,
4435 TRUTH_ANDIF_EXPR
, e1
, e2
,
4437 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4441 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4442 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4444 result_type
= TREE_TYPE (op0
);
4446 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (op0
))
4447 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4448 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4451 /* E will be the final comparison. */
4453 /* E1 and E2 are for scratch. */
4461 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4462 CPO_COMPARISON
, complain
);
4464 if (!same_type_p (TREE_TYPE (op0
), type
))
4465 op0
= cp_convert_and_check (type
, op0
, complain
);
4466 if (!same_type_p (TREE_TYPE (op1
), type
))
4467 op1
= cp_convert_and_check (type
, op1
, complain
);
4469 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4470 return error_mark_node
;
4472 if (TREE_SIDE_EFFECTS (op0
))
4473 op0
= save_expr (op0
);
4474 if (TREE_SIDE_EFFECTS (op1
))
4475 op1
= save_expr (op1
);
4477 pfn0
= pfn_from_ptrmemfunc (op0
);
4478 pfn1
= pfn_from_ptrmemfunc (op1
);
4479 delta0
= delta_from_ptrmemfunc (op0
);
4480 delta1
= delta_from_ptrmemfunc (op1
);
4481 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4482 == ptrmemfunc_vbit_in_delta
)
4487 && ((op0.delta == op1.delta)
4488 || (!op0.pfn && op0.delta & 1 == 0
4489 && op1.delta & 1 == 0))
4491 The reason for the `!op0.pfn' bit is that a NULL
4492 pointer-to-member is any member with a zero PFN and
4493 LSB of the DELTA field is 0. */
4495 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4499 e1
= cp_build_binary_op (location
,
4500 EQ_EXPR
, e1
, integer_zero_node
,
4502 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4506 e2
= cp_build_binary_op (location
,
4507 EQ_EXPR
, e2
, integer_zero_node
,
4509 e1
= cp_build_binary_op (location
,
4510 TRUTH_ANDIF_EXPR
, e2
, e1
,
4512 e2
= cp_build_binary_op (location
, EQ_EXPR
,
4514 build_zero_cst (TREE_TYPE (pfn0
)),
4516 e2
= cp_build_binary_op (location
,
4517 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4518 e1
= cp_build_binary_op (location
,
4519 EQ_EXPR
, delta0
, delta1
, complain
);
4520 e1
= cp_build_binary_op (location
,
4521 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4528 && (!op0.pfn || op0.delta == op1.delta))
4530 The reason for the `!op0.pfn' bit is that a NULL
4531 pointer-to-member is any member with a zero PFN; the
4532 DELTA field is unspecified. */
4534 e1
= cp_build_binary_op (location
,
4535 EQ_EXPR
, delta0
, delta1
, complain
);
4536 e2
= cp_build_binary_op (location
,
4539 build_zero_cst (TREE_TYPE (pfn0
)),
4541 e1
= cp_build_binary_op (location
,
4542 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4544 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
4545 e
= cp_build_binary_op (location
,
4546 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4547 if (code
== EQ_EXPR
)
4549 return cp_build_binary_op (location
,
4550 EQ_EXPR
, e
, integer_zero_node
, complain
);
4554 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
4555 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
4557 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
4558 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
4566 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
4567 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
4569 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4570 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4571 CPO_COMPARISON
, complain
);
4578 if (TREE_CODE (orig_op0
) == STRING_CST
4579 || TREE_CODE (orig_op1
) == STRING_CST
)
4581 if (complain
& tf_warning
)
4582 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4585 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4589 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4591 && !vector_types_compatible_elements_p (type0
, type1
))
4593 if (complain
& tf_error
)
4595 error_at (location
, "comparing vectors with different "
4597 inform (location
, "operand types are %qT and %qT",
4600 return error_mark_node
;
4603 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
4605 if (complain
& tf_error
)
4607 error_at (location
, "comparing vectors with different "
4608 "number of elements");
4609 inform (location
, "operand types are %qT and %qT",
4612 return error_mark_node
;
4615 /* Always construct signed integer vector type. */
4616 intt
= c_common_type_for_size (GET_MODE_BITSIZE
4617 (TYPE_MODE (TREE_TYPE (type0
))), 0);
4620 if (complain
& tf_error
)
4621 error_at (location
, "could not find an integer type "
4622 "of the same size as %qT", TREE_TYPE (type0
));
4623 return error_mark_node
;
4625 result_type
= build_opaque_vector_type (intt
,
4626 TYPE_VECTOR_SUBPARTS (type0
));
4630 build_type
= boolean_type_node
;
4631 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4632 || code0
== ENUMERAL_TYPE
)
4633 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4634 || code1
== ENUMERAL_TYPE
))
4636 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4637 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4638 CPO_COMPARISON
, complain
);
4639 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
4641 result_type
= type0
;
4642 if (extra_warnings
&& (complain
& tf_warning
))
4643 warning (OPT_Wextra
,
4644 "ordered comparison of pointer with integer zero");
4646 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op0
))
4648 result_type
= type1
;
4649 if (extra_warnings
&& (complain
& tf_warning
))
4650 warning (OPT_Wextra
,
4651 "ordered comparison of pointer with integer zero");
4653 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4654 /* One of the operands must be of nullptr_t type. */
4655 result_type
= TREE_TYPE (nullptr_node
);
4656 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4658 result_type
= type0
;
4659 if (complain
& tf_error
)
4660 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4662 return error_mark_node
;
4664 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4666 result_type
= type1
;
4667 if (complain
& tf_error
)
4668 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4670 return error_mark_node
;
4674 case UNORDERED_EXPR
:
4681 build_type
= integer_type_node
;
4682 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
4684 if (complain
& tf_error
)
4685 error ("unordered comparison on non-floating point argument");
4686 return error_mark_node
;
4695 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
4696 || code0
== ENUMERAL_TYPE
)
4697 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4698 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
4699 arithmetic_types_p
= 1;
4702 arithmetic_types_p
= 0;
4703 /* Vector arithmetic is only allowed when both sides are vectors. */
4704 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4706 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
4707 || !vector_types_compatible_elements_p (type0
, type1
))
4709 if (complain
& tf_error
)
4710 binary_op_error (location
, code
, type0
, type1
);
4711 return error_mark_node
;
4713 arithmetic_types_p
= 1;
4716 /* Determine the RESULT_TYPE, if it is not already known. */
4718 && arithmetic_types_p
4719 && (shorten
|| common
|| short_compare
))
4721 result_type
= cp_common_type (type0
, type1
);
4722 if (complain
& tf_warning
)
4723 do_warn_double_promotion (result_type
, type0
, type1
,
4724 "implicit conversion from %qT to %qT "
4725 "to match other operand of binary "
4732 if (complain
& tf_error
)
4733 error ("invalid operands of types %qT and %qT to binary %qO",
4734 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
4735 return error_mark_node
;
4738 /* If we're in a template, the only thing we need to know is the
4740 if (processing_template_decl
)
4742 /* Since the middle-end checks the type when doing a build2, we
4743 need to build the tree in pieces. This built tree will never
4744 get out of the front-end as we replace it when instantiating
4746 tree tmp
= build2 (resultcode
,
4747 build_type
? build_type
: result_type
,
4749 TREE_OPERAND (tmp
, 0) = op0
;
4753 if (arithmetic_types_p
)
4755 bool first_complex
= (code0
== COMPLEX_TYPE
);
4756 bool second_complex
= (code1
== COMPLEX_TYPE
);
4757 int none_complex
= (!first_complex
&& !second_complex
);
4759 /* Adapted from patch for c/24581. */
4760 if (first_complex
!= second_complex
4761 && (code
== PLUS_EXPR
4762 || code
== MINUS_EXPR
4763 || code
== MULT_EXPR
4764 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
4765 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
4766 && flag_signed_zeros
)
4768 /* An operation on mixed real/complex operands must be
4769 handled specially, but the language-independent code can
4770 more easily optimize the plain complex arithmetic if
4771 -fno-signed-zeros. */
4772 tree real_type
= TREE_TYPE (result_type
);
4776 if (TREE_TYPE (op0
) != result_type
)
4777 op0
= cp_convert_and_check (result_type
, op0
, complain
);
4778 if (TREE_TYPE (op1
) != real_type
)
4779 op1
= cp_convert_and_check (real_type
, op1
, complain
);
4783 if (TREE_TYPE (op0
) != real_type
)
4784 op0
= cp_convert_and_check (real_type
, op0
, complain
);
4785 if (TREE_TYPE (op1
) != result_type
)
4786 op1
= cp_convert_and_check (result_type
, op1
, complain
);
4788 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
4789 return error_mark_node
;
4792 op0
= save_expr (op0
);
4793 real
= cp_build_unary_op (REALPART_EXPR
, op0
, 1, complain
);
4794 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, 1, complain
);
4798 case TRUNC_DIV_EXPR
:
4799 op1
= save_expr (op1
);
4800 imag
= build2 (resultcode
, real_type
, imag
, op1
);
4804 real
= build2 (resultcode
, real_type
, real
, op1
);
4812 op1
= save_expr (op1
);
4813 real
= cp_build_unary_op (REALPART_EXPR
, op1
, 1, complain
);
4814 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, 1, complain
);
4818 op0
= save_expr (op0
);
4819 imag
= build2 (resultcode
, real_type
, op0
, imag
);
4822 real
= build2 (resultcode
, real_type
, op0
, real
);
4825 real
= build2 (resultcode
, real_type
, op0
, real
);
4826 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
4832 real
= fold_if_not_in_template (real
);
4833 imag
= fold_if_not_in_template (imag
);
4834 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
4835 result
= fold_if_not_in_template (result
);
4839 /* For certain operations (which identify themselves by shorten != 0)
4840 if both args were extended from the same smaller type,
4841 do the arithmetic in that type and then extend.
4843 shorten !=0 and !=1 indicates a bitwise operation.
4844 For them, this optimization is safe only if
4845 both args are zero-extended or both are sign-extended.
4846 Otherwise, we might change the result.
4847 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4848 but calculated in (unsigned short) it would be (unsigned short)-1. */
4850 if (shorten
&& none_complex
)
4852 orig_type
= result_type
;
4853 final_type
= result_type
;
4854 result_type
= shorten_binary_op (result_type
, op0
, op1
,
4858 /* Comparison operations are shortened too but differently.
4859 They identify themselves by setting short_compare = 1. */
4863 /* Don't write &op0, etc., because that would prevent op0
4864 from being kept in a register.
4865 Instead, make copies of the our local variables and
4866 pass the copies by reference, then copy them back afterward. */
4867 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
4868 enum tree_code xresultcode
= resultcode
;
4870 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
4873 return cp_convert (boolean_type_node
, val
, complain
);
4874 op0
= xop0
, op1
= xop1
;
4876 resultcode
= xresultcode
;
4879 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
4880 && warn_sign_compare
4881 /* Do not warn until the template is instantiated; we cannot
4882 bound the ranges of the arguments until that point. */
4883 && !processing_template_decl
4884 && (complain
& tf_warning
)
4885 && c_inhibit_evaluation_warnings
== 0
4886 /* Even unsigned enum types promote to signed int. We don't
4887 want to issue -Wsign-compare warnings for this case. */
4888 && !enum_cast_to_int (orig_op0
)
4889 && !enum_cast_to_int (orig_op1
))
4891 tree oop0
= maybe_constant_value (orig_op0
);
4892 tree oop1
= maybe_constant_value (orig_op1
);
4894 if (TREE_CODE (oop0
) != INTEGER_CST
)
4896 if (TREE_CODE (oop1
) != INTEGER_CST
)
4898 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
4899 result_type
, resultcode
);
4903 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4904 Then the expression will be built.
4905 It will be given type FINAL_TYPE if that is nonzero;
4906 otherwise, it will be given type RESULT_TYPE. */
4909 if (TREE_TYPE (op0
) != result_type
)
4910 op0
= cp_convert_and_check (result_type
, op0
, complain
);
4911 if (TREE_TYPE (op1
) != result_type
)
4912 op1
= cp_convert_and_check (result_type
, op1
, complain
);
4914 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4915 return error_mark_node
;
4918 if (build_type
== NULL_TREE
)
4919 build_type
= result_type
;
4921 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
4922 | SANITIZE_FLOAT_DIVIDE
))
4923 && !processing_template_decl
4924 && current_function_decl
!= 0
4925 && !lookup_attribute ("no_sanitize_undefined",
4926 DECL_ATTRIBUTES (current_function_decl
))
4927 && (doing_div_or_mod
|| doing_shift
))
4929 /* OP0 and/or OP1 might have side-effects. */
4930 op0
= cp_save_expr (op0
);
4931 op1
= cp_save_expr (op1
);
4932 op0
= maybe_constant_value (fold_non_dependent_expr_sfinae (op0
,
4934 op1
= maybe_constant_value (fold_non_dependent_expr_sfinae (op1
,
4936 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
4937 | SANITIZE_FLOAT_DIVIDE
)))
4939 /* For diagnostics we want to use the promoted types without
4940 shorten_binary_op. So convert the arguments to the
4941 original result_type. */
4944 if (orig_type
!= NULL
&& result_type
!= orig_type
)
4946 cop0
= cp_convert (orig_type
, op0
, complain
);
4947 cop1
= cp_convert (orig_type
, op1
, complain
);
4949 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
4951 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
4952 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
4955 result
= build2 (resultcode
, build_type
, op0
, op1
);
4956 result
= fold_if_not_in_template (result
);
4957 if (final_type
!= 0)
4958 result
= cp_convert (final_type
, result
, complain
);
4960 if (TREE_OVERFLOW_P (result
)
4961 && !TREE_OVERFLOW_P (op0
)
4962 && !TREE_OVERFLOW_P (op1
))
4963 overflow_warning (location
, result
);
4965 if (instrument_expr
!= NULL
)
4966 result
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
4967 instrument_expr
, result
);
4972 /* Build a VEC_PERM_EXPR.
4973 This is a simple wrapper for c_build_vec_perm_expr. */
4975 build_x_vec_perm_expr (location_t loc
,
4976 tree arg0
, tree arg1
, tree arg2
,
4977 tsubst_flags_t complain
)
4979 tree orig_arg0
= arg0
;
4980 tree orig_arg1
= arg1
;
4981 tree orig_arg2
= arg2
;
4982 if (processing_template_decl
)
4984 if (type_dependent_expression_p (arg0
)
4985 || type_dependent_expression_p (arg1
)
4986 || type_dependent_expression_p (arg2
))
4987 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
4988 arg0
= build_non_dependent_expr (arg0
);
4990 arg1
= build_non_dependent_expr (arg1
);
4991 arg2
= build_non_dependent_expr (arg2
);
4993 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
4994 if (processing_template_decl
&& exp
!= error_mark_node
)
4995 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
4996 orig_arg1
, orig_arg2
);
5000 /* Return a tree for the sum or difference (RESULTCODE says which)
5001 of pointer PTROP and integer INTOP. */
5004 cp_pointer_int_sum (enum tree_code resultcode
, tree ptrop
, tree intop
,
5005 tsubst_flags_t complain
)
5007 tree res_type
= TREE_TYPE (ptrop
);
5009 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5010 in certain circumstance (when it's valid to do so). So we need
5011 to make sure it's complete. We don't need to check here, if we
5012 can actually complete it at all, as those checks will be done in
5013 pointer_int_sum() anyway. */
5014 complete_type (TREE_TYPE (res_type
));
5016 return pointer_int_sum (input_location
, resultcode
, ptrop
,
5017 fold_if_not_in_template (intop
),
5018 complain
& tf_warning_or_error
);
5021 /* Return a tree for the difference of pointers OP0 and OP1.
5022 The resulting tree has type int. */
5025 pointer_diff (tree op0
, tree op1
, tree ptrtype
, tsubst_flags_t complain
)
5028 tree restype
= ptrdiff_type_node
;
5029 tree target_type
= TREE_TYPE (ptrtype
);
5031 if (!complete_type_or_else (target_type
, NULL_TREE
))
5032 return error_mark_node
;
5034 if (VOID_TYPE_P (target_type
))
5036 if (complain
& tf_error
)
5037 permerror (input_location
, "ISO C++ forbids using pointer of "
5038 "type %<void *%> in subtraction");
5040 return error_mark_node
;
5042 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5044 if (complain
& tf_error
)
5045 permerror (input_location
, "ISO C++ forbids using pointer to "
5046 "a function in subtraction");
5048 return error_mark_node
;
5050 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5052 if (complain
& tf_error
)
5053 permerror (input_location
, "ISO C++ forbids using pointer to "
5054 "a method in subtraction");
5056 return error_mark_node
;
5059 /* First do the subtraction as integers;
5060 then drop through to build the divide operator. */
5062 op0
= cp_build_binary_op (input_location
,
5064 cp_convert (restype
, op0
, complain
),
5065 cp_convert (restype
, op1
, complain
),
5068 /* This generates an error if op1 is a pointer to an incomplete type. */
5069 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5071 if (complain
& tf_error
)
5072 error ("invalid use of a pointer to an incomplete type in "
5073 "pointer arithmetic");
5075 return error_mark_node
;
5078 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5080 if (complain
& tf_error
)
5081 error ("arithmetic on pointer to an empty aggregate");
5083 return error_mark_node
;
5086 op1
= (TYPE_PTROB_P (ptrtype
)
5087 ? size_in_bytes (target_type
)
5088 : integer_one_node
);
5090 /* Do the division. */
5092 result
= build2 (EXACT_DIV_EXPR
, restype
, op0
,
5093 cp_convert (restype
, op1
, complain
));
5094 return fold_if_not_in_template (result
);
5097 /* Construct and perhaps optimize a tree representation
5098 for a unary operation. CODE, a tree_code, specifies the operation
5099 and XARG is the operand. */
5102 build_x_unary_op (location_t loc
, enum tree_code code
, tree xarg
,
5103 tsubst_flags_t complain
)
5105 tree orig_expr
= xarg
;
5109 if (processing_template_decl
)
5111 if (type_dependent_expression_p (xarg
))
5112 return build_min_nt_loc (loc
, code
, xarg
, NULL_TREE
);
5114 xarg
= build_non_dependent_expr (xarg
);
5119 /* [expr.unary.op] says:
5121 The address of an object of incomplete type can be taken.
5123 (And is just the ordinary address operator, not an overloaded
5124 "operator &".) However, if the type is a template
5125 specialization, we must complete the type at this point so that
5126 an overloaded "operator &" will be available if required. */
5127 if (code
== ADDR_EXPR
5128 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5129 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5130 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5131 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5132 /* Don't look for a function. */;
5134 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5135 NULL_TREE
, /*overload=*/NULL
, complain
);
5136 if (!exp
&& code
== ADDR_EXPR
)
5138 if (is_overloaded_fn (xarg
))
5140 tree fn
= get_first_fn (xarg
);
5141 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5143 if (complain
& tf_error
)
5144 error (DECL_CONSTRUCTOR_P (fn
)
5145 ? G_("taking address of constructor %qE")
5146 : G_("taking address of destructor %qE"),
5148 return error_mark_node
;
5152 /* A pointer to member-function can be formed only by saying
5154 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5155 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5157 if (TREE_CODE (xarg
) != OFFSET_REF
5158 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5160 if (complain
& tf_error
)
5162 error ("invalid use of %qE to form a "
5163 "pointer-to-member-function", xarg
);
5164 if (TREE_CODE (xarg
) != OFFSET_REF
)
5165 inform (input_location
, " a qualified-id is required");
5167 return error_mark_node
;
5171 if (complain
& tf_error
)
5172 error ("parentheses around %qE cannot be used to form a"
5173 " pointer-to-member-function",
5176 return error_mark_node
;
5177 PTRMEM_OK_P (xarg
) = 1;
5181 if (TREE_CODE (xarg
) == OFFSET_REF
)
5183 ptrmem
= PTRMEM_OK_P (xarg
);
5185 if (!ptrmem
&& !flag_ms_extensions
5186 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5188 /* A single non-static member, make sure we don't allow a
5189 pointer-to-member. */
5190 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5191 TREE_OPERAND (xarg
, 0),
5192 ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
));
5193 PTRMEM_OK_P (xarg
) = ptrmem
;
5197 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5200 if (processing_template_decl
&& exp
!= error_mark_node
)
5201 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5202 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5203 if (TREE_CODE (exp
) == ADDR_EXPR
)
5204 PTRMEM_OK_P (exp
) = ptrmem
;
5208 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5209 constants, where a null value is represented by an INTEGER_CST of
5213 cp_truthvalue_conversion (tree expr
)
5215 tree type
= TREE_TYPE (expr
);
5216 if (TYPE_PTRDATAMEM_P (type
)
5217 /* Avoid ICE on invalid use of non-static member function. */
5218 || TREE_CODE (expr
) == FUNCTION_DECL
)
5219 return build_binary_op (EXPR_LOCATION (expr
),
5220 NE_EXPR
, expr
, nullptr_node
, 1);
5221 else if (TYPE_PTR_P (type
) || TYPE_PTRMEMFUNC_P (type
))
5223 /* With -Wzero-as-null-pointer-constant do not warn for an
5224 'if (p)' or a 'while (!p)', where p is a pointer. */
5226 ++c_inhibit_evaluation_warnings
;
5227 ret
= c_common_truthvalue_conversion (input_location
, expr
);
5228 --c_inhibit_evaluation_warnings
;
5232 return c_common_truthvalue_conversion (input_location
, expr
);
5235 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5238 condition_conversion (tree expr
)
5241 if (processing_template_decl
)
5243 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5244 tf_warning_or_error
, LOOKUP_NORMAL
);
5245 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5249 /* Returns the address of T. This function will fold away
5250 ADDR_EXPR of INDIRECT_REF. */
5253 build_address (tree t
)
5255 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5256 return error_mark_node
;
5257 t
= build_fold_addr_expr (t
);
5258 if (TREE_CODE (t
) != ADDR_EXPR
)
5263 /* Returns the address of T with type TYPE. */
5266 build_typed_address (tree t
, tree type
)
5268 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5269 return error_mark_node
;
5270 t
= build_fold_addr_expr_with_type (t
, type
);
5271 if (TREE_CODE (t
) != ADDR_EXPR
)
5276 /* Return a NOP_EXPR converting EXPR to TYPE. */
5279 build_nop (tree type
, tree expr
)
5281 if (type
== error_mark_node
|| error_operand_p (expr
))
5283 return build1 (NOP_EXPR
, type
, expr
);
5286 /* Take the address of ARG, whatever that means under C++ semantics.
5287 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5288 and class rvalues as well.
5290 Nothing should call this function directly; instead, callers should use
5291 cp_build_addr_expr or cp_build_addr_expr_strict. */
5294 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5299 if (!arg
|| error_operand_p (arg
))
5300 return error_mark_node
;
5302 arg
= mark_lvalue_use (arg
);
5303 argtype
= lvalue_type (arg
);
5305 gcc_assert (!identifier_p (arg
) || !IDENTIFIER_OPNAME_P (arg
));
5307 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5308 && !really_overloaded_fn (TREE_OPERAND (arg
, 1)))
5310 /* They're trying to take the address of a unique non-static
5311 member function. This is ill-formed (except in MS-land),
5312 but let's try to DTRT.
5313 Note: We only handle unique functions here because we don't
5314 want to complain if there's a static overload; non-unique
5315 cases will be handled by instantiate_type. But we need to
5316 handle this case here to allow casts on the resulting PMF.
5317 We could defer this in non-MS mode, but it's easier to give
5318 a useful error here. */
5320 /* Inside constant member functions, the `this' pointer
5321 contains an extra const qualifier. TYPE_MAIN_VARIANT
5322 is used here to remove this const from the diagnostics
5323 and the created OFFSET_REF. */
5324 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5325 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5328 if (! flag_ms_extensions
)
5330 tree name
= DECL_NAME (fn
);
5331 if (!(complain
& tf_error
))
5332 return error_mark_node
;
5333 else if (current_class_type
5334 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5335 /* An expression like &memfn. */
5336 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5337 " or parenthesized non-static member function to form"
5338 " a pointer to member function. Say %<&%T::%D%>",
5341 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5342 " function to form a pointer to member function."
5346 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5349 /* Uninstantiated types are all functions. Taking the
5350 address of a function is a no-op, so just return the
5352 if (type_unknown_p (arg
))
5353 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5355 if (TREE_CODE (arg
) == OFFSET_REF
)
5356 /* We want a pointer to member; bypass all the code for actually taking
5357 the address of something. */
5360 /* Anything not already handled and not a true memory reference
5362 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5363 && TREE_CODE (argtype
) != METHOD_TYPE
)
5365 cp_lvalue_kind kind
= lvalue_kind (arg
);
5366 if (kind
== clk_none
)
5368 if (complain
& tf_error
)
5369 lvalue_error (input_location
, lv_addressof
);
5370 return error_mark_node
;
5372 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
5374 if (!(complain
& tf_error
))
5375 return error_mark_node
;
5376 if (kind
& clk_class
)
5377 /* Make this a permerror because we used to accept it. */
5378 permerror (input_location
, "taking address of temporary");
5380 error ("taking address of xvalue (rvalue reference)");
5384 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
5386 tree type
= build_pointer_type (TREE_TYPE (argtype
));
5387 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5390 else if (pedantic
&& DECL_MAIN_P (arg
))
5393 /* Apparently a lot of autoconf scripts for C++ packages do this,
5394 so only complain if -Wpedantic. */
5395 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
5396 pedwarn (input_location
, OPT_Wpedantic
,
5397 "ISO C++ forbids taking address of function %<::main%>");
5398 else if (flag_pedantic_errors
)
5399 return error_mark_node
;
5402 /* Let &* cancel out to simplify resulting code. */
5403 if (INDIRECT_REF_P (arg
))
5405 /* We don't need to have `current_class_ptr' wrapped in a
5406 NON_LVALUE_EXPR node. */
5407 if (arg
== current_class_ref
)
5408 return current_class_ptr
;
5410 arg
= TREE_OPERAND (arg
, 0);
5411 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
5413 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
5414 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5417 /* Don't let this be an lvalue. */
5422 /* ??? Cope with user tricks that amount to offsetof. */
5423 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5424 && TREE_CODE (argtype
) != METHOD_TYPE
5425 && argtype
!= unknown_type_node
5426 && (val
= get_base_address (arg
))
5427 && COMPLETE_TYPE_P (TREE_TYPE (val
))
5428 && INDIRECT_REF_P (val
)
5429 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
5431 tree type
= build_pointer_type (argtype
);
5432 return fold_convert (type
, fold_offsetof_1 (arg
));
5435 /* Handle complex lvalues (when permitted)
5436 by reduction to simpler cases. */
5437 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
5441 switch (TREE_CODE (arg
))
5445 case FIX_TRUNC_EXPR
:
5446 /* Even if we're not being pedantic, we cannot allow this
5447 extension when we're instantiating in a SFINAE
5449 if (! lvalue_p (arg
) && complain
== tf_none
)
5451 if (complain
& tf_error
)
5452 permerror (input_location
, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5454 return error_mark_node
;
5459 arg
= BASELINK_FUNCTIONS (arg
);
5463 arg
= OVL_CURRENT (arg
);
5468 /* Turn a reference to a non-static data member into a
5469 pointer-to-member. */
5474 gcc_assert (PTRMEM_OK_P (arg
));
5476 t
= TREE_OPERAND (arg
, 1);
5477 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5479 if (complain
& tf_error
)
5480 error ("cannot create pointer to reference member %qD", t
);
5481 return error_mark_node
;
5484 type
= build_ptrmem_type (context_for_name_lookup (t
),
5486 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
5494 if (argtype
!= error_mark_node
)
5496 if (cxx_dialect
>= cxx1y
&& array_of_runtime_bound_p (argtype
)
5497 && (flag_iso
|| warn_vla
> 0))
5499 if (complain
& tf_warning_or_error
)
5500 pedwarn (input_location
, OPT_Wvla
,
5501 "taking address of array of runtime bound");
5503 return error_mark_node
;
5505 argtype
= build_pointer_type (argtype
);
5508 /* In a template, we are processing a non-dependent expression
5509 so we can just form an ADDR_EXPR with the correct type. */
5510 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
5512 val
= build_address (arg
);
5513 if (TREE_CODE (arg
) == OFFSET_REF
)
5514 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
5516 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
5518 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
5520 /* We can only get here with a single static member
5522 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
5523 && DECL_STATIC_FUNCTION_P (fn
));
5525 val
= build_address (fn
);
5526 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
5527 /* Do not lose object's side effects. */
5528 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
5529 TREE_OPERAND (arg
, 0), val
);
5531 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
5533 if (complain
& tf_error
)
5534 error ("attempt to take address of bit-field structure member %qD",
5535 TREE_OPERAND (arg
, 1));
5536 return error_mark_node
;
5540 tree object
= TREE_OPERAND (arg
, 0);
5541 tree field
= TREE_OPERAND (arg
, 1);
5542 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5543 (TREE_TYPE (object
), decl_type_context (field
)));
5544 val
= build_address (arg
);
5547 if (TYPE_PTR_P (argtype
)
5548 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
5550 build_ptrmemfunc_type (argtype
);
5551 val
= build_ptrmemfunc (argtype
, val
, 0,
5559 /* Take the address of ARG if it has one, even if it's an rvalue. */
5562 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
5564 return cp_build_addr_expr_1 (arg
, 0, complain
);
5567 /* Take the address of ARG, but only if it's an lvalue. */
5570 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
5572 return cp_build_addr_expr_1 (arg
, 1, complain
);
5575 /* C++: Must handle pointers to members.
5577 Perhaps type instantiation should be extended to handle conversion
5578 from aggregates to types we don't yet know we want? (Or are those
5579 cases typically errors which should be reported?)
5581 NOCONVERT nonzero suppresses the default promotions
5582 (such as from short to int). */
5585 cp_build_unary_op (enum tree_code code
, tree xarg
, int noconvert
,
5586 tsubst_flags_t complain
)
5588 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5591 const char *errstring
= NULL
;
5593 const char *invalid_op_diag
;
5595 if (!arg
|| error_operand_p (arg
))
5596 return error_mark_node
;
5598 if ((invalid_op_diag
5599 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
5604 if (complain
& tf_error
)
5605 error (invalid_op_diag
);
5606 return error_mark_node
;
5611 case UNARY_PLUS_EXPR
:
5614 int flags
= WANT_ARITH
| WANT_ENUM
;
5615 /* Unary plus (but not unary minus) is allowed on pointers. */
5616 if (code
== UNARY_PLUS_EXPR
)
5617 flags
|= WANT_POINTER
;
5618 arg
= build_expr_type_conversion (flags
, arg
, true);
5620 errstring
= (code
== NEGATE_EXPR
5621 ? _("wrong type argument to unary minus")
5622 : _("wrong type argument to unary plus"));
5625 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5626 arg
= cp_perform_integral_promotions (arg
, complain
);
5628 /* Make sure the result is not an lvalue: a unary plus or minus
5629 expression is always a rvalue. */
5636 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5641 arg
= cp_default_conversion (arg
, complain
);
5642 if (arg
== error_mark_node
)
5643 return error_mark_node
;
5646 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
5647 | WANT_VECTOR_OR_COMPLEX
,
5649 errstring
= _("wrong type argument to bit-complement");
5650 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5651 arg
= cp_perform_integral_promotions (arg
, complain
);
5655 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5656 errstring
= _("wrong type argument to abs");
5657 else if (!noconvert
)
5659 arg
= cp_default_conversion (arg
, complain
);
5660 if (arg
== error_mark_node
)
5661 return error_mark_node
;
5666 /* Conjugating a real value is a no-op, but allow it anyway. */
5667 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5668 errstring
= _("wrong type argument to conjugation");
5669 else if (!noconvert
)
5671 arg
= cp_default_conversion (arg
, complain
);
5672 if (arg
== error_mark_node
)
5673 return error_mark_node
;
5677 case TRUTH_NOT_EXPR
:
5678 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
5680 val
= invert_truthvalue_loc (input_location
, arg
);
5681 if (arg
!= error_mark_node
)
5683 errstring
= _("in argument to unary !");
5691 arg
= build_real_imag_expr (input_location
, code
, arg
);
5692 if (arg
== error_mark_node
)
5695 return fold_if_not_in_template (arg
);
5697 case PREINCREMENT_EXPR
:
5698 case POSTINCREMENT_EXPR
:
5699 case PREDECREMENT_EXPR
:
5700 case POSTDECREMENT_EXPR
:
5701 /* Handle complex lvalues (when permitted)
5702 by reduction to simpler cases. */
5704 val
= unary_complex_lvalue (code
, arg
);
5708 arg
= mark_lvalue_use (arg
);
5710 /* Increment or decrement the real part of the value,
5711 and don't change the imaginary part. */
5712 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5716 arg
= stabilize_reference (arg
);
5717 real
= cp_build_unary_op (REALPART_EXPR
, arg
, 1, complain
);
5718 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, 1, complain
);
5719 real
= cp_build_unary_op (code
, real
, 1, complain
);
5720 if (real
== error_mark_node
|| imag
== error_mark_node
)
5721 return error_mark_node
;
5722 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
5726 /* Report invalid types. */
5728 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
5731 if (code
== PREINCREMENT_EXPR
)
5732 errstring
= _("no pre-increment operator for type");
5733 else if (code
== POSTINCREMENT_EXPR
)
5734 errstring
= _("no post-increment operator for type");
5735 else if (code
== PREDECREMENT_EXPR
)
5736 errstring
= _("no pre-decrement operator for type");
5738 errstring
= _("no post-decrement operator for type");
5741 else if (arg
== error_mark_node
)
5742 return error_mark_node
;
5744 /* Report something read-only. */
5746 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
5747 || TREE_READONLY (arg
))
5749 if (complain
& tf_error
)
5750 cxx_readonly_error (arg
, ((code
== PREINCREMENT_EXPR
5751 || code
== POSTINCREMENT_EXPR
)
5752 ? lv_increment
: lv_decrement
));
5754 return error_mark_node
;
5759 tree declared_type
= unlowered_expr_type (arg
);
5761 argtype
= TREE_TYPE (arg
);
5763 /* ARM $5.2.5 last annotation says this should be forbidden. */
5764 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
5766 if (complain
& tf_error
)
5767 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
5768 ? G_("ISO C++ forbids incrementing an enum")
5769 : G_("ISO C++ forbids decrementing an enum"));
5771 return error_mark_node
;
5774 /* Compute the increment. */
5776 if (TYPE_PTR_P (argtype
))
5778 tree type
= complete_type (TREE_TYPE (argtype
));
5780 if (!COMPLETE_OR_VOID_TYPE_P (type
))
5782 if (complain
& tf_error
)
5783 error (((code
== PREINCREMENT_EXPR
5784 || code
== POSTINCREMENT_EXPR
))
5785 ? G_("cannot increment a pointer to incomplete type %qT")
5786 : G_("cannot decrement a pointer to incomplete type %qT"),
5787 TREE_TYPE (argtype
));
5789 return error_mark_node
;
5791 else if (!TYPE_PTROB_P (argtype
))
5793 if (complain
& tf_error
)
5794 pedwarn (input_location
, OPT_Wpointer_arith
,
5795 (code
== PREINCREMENT_EXPR
5796 || code
== POSTINCREMENT_EXPR
)
5797 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5798 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5801 return error_mark_node
;
5804 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
5807 inc
= VECTOR_TYPE_P (argtype
)
5808 ? build_one_cst (argtype
)
5811 inc
= cp_convert (argtype
, inc
, complain
);
5813 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5814 need to ask Objective-C to build the increment or decrement
5815 expression for it. */
5816 if (objc_is_property_ref (arg
))
5817 return objc_build_incr_expr_for_property_ref (input_location
, code
,
5820 /* Complain about anything else that is not a true lvalue. */
5821 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
5822 || code
== POSTINCREMENT_EXPR
)
5823 ? lv_increment
: lv_decrement
),
5825 return error_mark_node
;
5827 /* Forbid using -- on `bool'. */
5828 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
5830 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5832 if (complain
& tf_error
)
5833 error ("invalid use of Boolean expression as operand "
5834 "to %<operator--%>");
5835 return error_mark_node
;
5837 val
= boolean_increment (code
, arg
);
5839 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
5840 /* An rvalue has no cv-qualifiers. */
5841 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
5843 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
5845 TREE_SIDE_EFFECTS (val
) = 1;
5850 /* Note that this operation never does default_conversion
5851 regardless of NOCONVERT. */
5852 return cp_build_addr_expr (arg
, complain
);
5861 argtype
= TREE_TYPE (arg
);
5862 return fold_if_not_in_template (build1 (code
, argtype
, arg
));
5865 if (complain
& tf_error
)
5866 error ("%s", errstring
);
5867 return error_mark_node
;
5870 /* Hook for the c-common bits that build a unary op. */
5872 build_unary_op (location_t
/*location*/,
5873 enum tree_code code
, tree xarg
, int noconvert
)
5875 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
5878 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5879 for certain kinds of expressions which are not really lvalues
5880 but which we can accept as lvalues.
5882 If ARG is not a kind of expression we can handle, return
5886 unary_complex_lvalue (enum tree_code code
, tree arg
)
5888 /* Inside a template, making these kinds of adjustments is
5889 pointless; we are only concerned with the type of the
5891 if (processing_template_decl
)
5894 /* Handle (a, b) used as an "lvalue". */
5895 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
5897 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), 0,
5898 tf_warning_or_error
);
5899 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
5900 TREE_OPERAND (arg
, 0), real_result
);
5903 /* Handle (a ? b : c) used as an "lvalue". */
5904 if (TREE_CODE (arg
) == COND_EXPR
5905 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
5906 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
5908 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5909 if (TREE_CODE (arg
) == MODIFY_EXPR
5910 || TREE_CODE (arg
) == PREINCREMENT_EXPR
5911 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
5913 tree lvalue
= TREE_OPERAND (arg
, 0);
5914 if (TREE_SIDE_EFFECTS (lvalue
))
5916 lvalue
= stabilize_reference (lvalue
);
5917 arg
= build2 (TREE_CODE (arg
), TREE_TYPE (arg
),
5918 lvalue
, TREE_OPERAND (arg
, 1));
5920 return unary_complex_lvalue
5921 (code
, build2 (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
5924 if (code
!= ADDR_EXPR
)
5927 /* Handle (a = b) used as an "lvalue" for `&'. */
5928 if (TREE_CODE (arg
) == MODIFY_EXPR
5929 || TREE_CODE (arg
) == INIT_EXPR
)
5931 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), 0,
5932 tf_warning_or_error
);
5933 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
5935 TREE_NO_WARNING (arg
) = 1;
5939 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
5940 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
5941 || TREE_CODE (arg
) == OFFSET_REF
)
5944 /* We permit compiler to make function calls returning
5945 objects of aggregate type look like lvalues. */
5949 if (TREE_CODE (targ
) == SAVE_EXPR
)
5950 targ
= TREE_OPERAND (targ
, 0);
5952 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
5954 if (TREE_CODE (arg
) == SAVE_EXPR
)
5957 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
5958 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
5961 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
5962 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
5963 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
5966 /* Don't let anything else be handled specially. */
5970 /* Mark EXP saying that we need to be able to take the
5971 address of it; it should not be allocated in a register.
5972 Value is true if successful.
5974 C++: we do not allow `current_class_ptr' to be addressable. */
5977 cxx_mark_addressable (tree exp
)
5982 switch (TREE_CODE (x
))
5989 x
= TREE_OPERAND (x
, 0);
5993 if (x
== current_class_ptr
)
5995 error ("cannot take the address of %<this%>, which is an rvalue expression");
5996 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6002 /* Caller should not be trying to mark initialized
6003 constant fields addressable. */
6004 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6005 || DECL_IN_AGGR_P (x
) == 0
6007 || DECL_EXTERNAL (x
));
6011 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6012 && !DECL_ARTIFICIAL (x
))
6014 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6017 ("address of explicit register variable %qD requested", x
);
6020 else if (extra_warnings
)
6022 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6024 TREE_ADDRESSABLE (x
) = 1;
6029 TREE_ADDRESSABLE (x
) = 1;
6033 TREE_ADDRESSABLE (x
) = 1;
6037 TREE_ADDRESSABLE (x
) = 1;
6038 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6046 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6049 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6050 tsubst_flags_t complain
)
6052 tree orig_ifexp
= ifexp
;
6053 tree orig_op1
= op1
;
6054 tree orig_op2
= op2
;
6057 if (processing_template_decl
)
6059 /* The standard says that the expression is type-dependent if
6060 IFEXP is type-dependent, even though the eventual type of the
6061 expression doesn't dependent on IFEXP. */
6062 if (type_dependent_expression_p (ifexp
)
6063 /* As a GNU extension, the middle operand may be omitted. */
6064 || (op1
&& type_dependent_expression_p (op1
))
6065 || type_dependent_expression_p (op2
))
6066 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6067 ifexp
= build_non_dependent_expr (ifexp
);
6069 op1
= build_non_dependent_expr (op1
);
6070 op2
= build_non_dependent_expr (op2
);
6073 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6074 if (processing_template_decl
&& expr
!= error_mark_node
6075 && TREE_CODE (expr
) != VEC_COND_EXPR
)
6077 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6078 orig_ifexp
, orig_op1
, orig_op2
);
6079 /* In C++11, remember that the result is an lvalue or xvalue.
6080 In C++98, lvalue_kind can just assume lvalue in a template. */
6081 if (cxx_dialect
>= cxx11
6082 && lvalue_or_rvalue_with_address_p (expr
)
6083 && !lvalue_or_rvalue_with_address_p (min
))
6084 TREE_TYPE (min
) = cp_build_reference_type (TREE_TYPE (min
),
6085 !real_lvalue_p (expr
));
6086 expr
= convert_from_reference (min
);
6091 /* Given a list of expressions, return a compound expression
6092 that performs them all and returns the value of the last of them. */
6095 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6096 tsubst_flags_t complain
)
6098 tree expr
= TREE_VALUE (list
);
6100 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6101 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6103 if (complain
& tf_error
)
6104 pedwarn (EXPR_LOC_OR_LOC (expr
, input_location
), 0,
6105 "list-initializer for non-class type must not "
6106 "be parenthesized");
6108 return error_mark_node
;
6111 if (TREE_CHAIN (list
))
6113 if (complain
& tf_error
)
6117 permerror (input_location
, "expression list treated as compound "
6118 "expression in initializer");
6121 permerror (input_location
, "expression list treated as compound "
6122 "expression in mem-initializer");
6125 permerror (input_location
, "expression list treated as compound "
6126 "expression in functional cast");
6132 return error_mark_node
;
6134 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6135 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6136 expr
, TREE_VALUE (list
), complain
);
6142 /* Like build_x_compound_expr_from_list, but using a VEC. */
6145 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6146 tsubst_flags_t complain
)
6148 if (vec_safe_is_empty (vec
))
6150 else if (vec
->length () == 1)
6160 if (complain
& tf_error
)
6161 permerror (input_location
,
6162 "%s expression list treated as compound expression",
6165 return error_mark_node
;
6169 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6170 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6177 /* Handle overloading of the ',' operator when needed. */
6180 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6181 tsubst_flags_t complain
)
6184 tree orig_op1
= op1
;
6185 tree orig_op2
= op2
;
6187 if (processing_template_decl
)
6189 if (type_dependent_expression_p (op1
)
6190 || type_dependent_expression_p (op2
))
6191 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6192 op1
= build_non_dependent_expr (op1
);
6193 op2
= build_non_dependent_expr (op2
);
6196 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6197 NULL_TREE
, /*overload=*/NULL
, complain
);
6199 result
= cp_build_compound_expr (op1
, op2
, complain
);
6201 if (processing_template_decl
&& result
!= error_mark_node
)
6202 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6207 /* Like cp_build_compound_expr, but for the c-common bits. */
6210 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6212 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6215 /* Build a compound expression. */
6218 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6220 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6222 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6223 return error_mark_node
;
6226 && (TREE_CODE (lhs
) == CILK_SPAWN_STMT
6227 || TREE_CODE (rhs
) == CILK_SPAWN_STMT
))
6229 location_t loc
= (EXPR_HAS_LOCATION (lhs
) ? EXPR_LOCATION (lhs
)
6230 : EXPR_LOCATION (rhs
));
6232 "spawned function call cannot be part of a comma expression");
6233 return error_mark_node
;
6236 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6238 /* If the rhs is a TARGET_EXPR, then build the compound
6239 expression inside the target_expr's initializer. This
6240 helps the compiler to eliminate unnecessary temporaries. */
6241 tree init
= TREE_OPERAND (rhs
, 1);
6243 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6244 TREE_OPERAND (rhs
, 1) = init
;
6249 if (type_unknown_p (rhs
))
6251 if (complain
& tf_error
)
6252 error ("no context to resolve type of %qE", rhs
);
6253 return error_mark_node
;
6256 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6259 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6260 casts away constness. CAST gives the type of cast. Returns true
6261 if the cast is ill-formed, false if it is well-formed.
6263 ??? This function warns for casting away any qualifier not just
6264 const. We would like to specify exactly what qualifiers are casted
6269 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6270 enum tree_code cast
, tsubst_flags_t complain
)
6272 /* C-style casts are allowed to cast away constness. With
6273 WARN_CAST_QUAL, we still want to issue a warning. */
6274 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6277 if (!casts_away_constness (src_type
, dest_type
, complain
))
6283 if (complain
& tf_warning
)
6284 warning (OPT_Wcast_qual
,
6285 "cast from type %qT to type %qT casts away qualifiers",
6286 src_type
, dest_type
);
6289 case STATIC_CAST_EXPR
:
6290 if (complain
& tf_error
)
6291 error ("static_cast from type %qT to type %qT casts away qualifiers",
6292 src_type
, dest_type
);
6295 case REINTERPRET_CAST_EXPR
:
6296 if (complain
& tf_error
)
6297 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6298 src_type
, dest_type
);
6307 Warns if the cast from expression EXPR to type TYPE is useless.
6310 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6312 if (warn_useless_cast
6313 && complain
& tf_warning
)
6315 /* In C++14 mode, this interacts badly with force_paren_expr. And it
6316 isn't necessary in any mode, because the code below handles
6317 glvalues properly. For 4.9, just skip it in C++14 mode. */
6318 if (cxx_dialect
< cxx1y
&& REFERENCE_REF_P (expr
))
6319 expr
= TREE_OPERAND (expr
, 0);
6321 if ((TREE_CODE (type
) == REFERENCE_TYPE
6322 && (TYPE_REF_IS_RVALUE (type
)
6323 ? xvalue_p (expr
) : real_lvalue_p (expr
))
6324 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6325 || same_type_p (TREE_TYPE (expr
), type
))
6326 warning (OPT_Wuseless_cast
, "useless cast to type %qT", type
);
6330 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6331 (another pointer-to-member type in the same hierarchy) and return
6332 the converted expression. If ALLOW_INVERSE_P is permitted, a
6333 pointer-to-derived may be converted to pointer-to-base; otherwise,
6334 only the other direction is permitted. If C_CAST_P is true, this
6335 conversion is taking place as part of a C-style cast. */
6338 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6339 bool c_cast_p
, tsubst_flags_t complain
)
6341 if (TYPE_PTRDATAMEM_P (type
))
6345 if (TREE_CODE (expr
) == PTRMEM_CST
)
6346 expr
= cplus_expand_constant (expr
);
6347 delta
= get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
)),
6348 TYPE_PTRMEM_CLASS_TYPE (type
),
6350 c_cast_p
, complain
);
6351 if (delta
== error_mark_node
)
6352 return error_mark_node
;
6354 if (!integer_zerop (delta
))
6356 tree cond
, op1
, op2
;
6358 cond
= cp_build_binary_op (input_location
,
6361 build_int_cst (TREE_TYPE (expr
), -1),
6363 op1
= build_nop (ptrdiff_type_node
, expr
);
6364 op2
= cp_build_binary_op (input_location
,
6365 PLUS_EXPR
, op1
, delta
,
6368 expr
= fold_build3_loc (input_location
,
6369 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
6373 return build_nop (type
, expr
);
6376 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
6377 allow_inverse_p
, c_cast_p
, complain
);
6380 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6381 this static_cast is being attempted as one of the possible casts
6382 allowed by a C-style cast. (In that case, accessibility of base
6383 classes is not considered, and it is OK to cast away
6384 constness.) Return the result of the cast. *VALID_P is set to
6385 indicate whether or not the cast was valid. */
6388 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6389 bool *valid_p
, tsubst_flags_t complain
)
6395 /* Assume the cast is valid. */
6398 intype
= unlowered_expr_type (expr
);
6400 /* Save casted types in the function's used types hash table. */
6401 used_types_insert (type
);
6403 /* [expr.static.cast]
6405 An lvalue of type "cv1 B", where B is a class type, can be cast
6406 to type "reference to cv2 D", where D is a class derived (clause
6407 _class.derived_) from B, if a valid standard conversion from
6408 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6409 same cv-qualification as, or greater cv-qualification than, cv1,
6410 and B is not a virtual base class of D. */
6411 /* We check this case before checking the validity of "TYPE t =
6412 EXPR;" below because for this case:
6415 struct D : public B { D(const B&); };
6417 void f() { static_cast<const D&>(b); }
6419 we want to avoid constructing a new D. The standard is not
6420 completely clear about this issue, but our interpretation is
6421 consistent with other compilers. */
6422 if (TREE_CODE (type
) == REFERENCE_TYPE
6423 && CLASS_TYPE_P (TREE_TYPE (type
))
6424 && CLASS_TYPE_P (intype
)
6425 && (TYPE_REF_IS_RVALUE (type
) || real_lvalue_p (expr
))
6426 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
6427 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
6428 build_pointer_type (TYPE_MAIN_VARIANT
6429 (TREE_TYPE (type
))),
6432 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6436 /* There is a standard conversion from "D*" to "B*" even if "B"
6437 is ambiguous or inaccessible. If this is really a
6438 static_cast, then we check both for inaccessibility and
6439 ambiguity. However, if this is a static_cast being performed
6440 because the user wrote a C-style cast, then accessibility is
6442 base
= lookup_base (TREE_TYPE (type
), intype
,
6443 c_cast_p
? ba_unique
: ba_check
,
6446 /* Convert from "B*" to "D*". This function will check that "B"
6447 is not a virtual base of "D". */
6448 expr
= build_base_path (MINUS_EXPR
, build_address (expr
),
6449 base
, /*nonnull=*/false, complain
);
6450 /* Convert the pointer to a reference -- but then remember that
6451 there are no expressions with reference type in C++.
6453 We call rvalue so that there's an actual tree code
6454 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6455 is a variable with the same type, the conversion would get folded
6456 away, leaving just the variable and causing lvalue_kind to give
6457 the wrong answer. */
6458 return convert_from_reference (rvalue (cp_fold_convert (type
, expr
)));
6461 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6462 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6463 if (TREE_CODE (type
) == REFERENCE_TYPE
6464 && TYPE_REF_IS_RVALUE (type
)
6465 && (clk
= real_lvalue_p (expr
))
6466 && reference_related_p (TREE_TYPE (type
), intype
)
6467 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6469 if (clk
== clk_ordinary
)
6471 /* Handle the (non-bit-field) lvalue case here by casting to
6472 lvalue reference and then changing it to an rvalue reference.
6473 Casting an xvalue to rvalue reference will be handled by the
6475 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
6476 result
= (perform_direct_initialization_if_possible
6477 (lref
, expr
, c_cast_p
, complain
));
6478 result
= cp_fold_convert (type
, result
);
6479 /* Make sure we don't fold back down to a named rvalue reference,
6480 because that would be an lvalue. */
6481 if (DECL_P (result
))
6482 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
6483 return convert_from_reference (result
);
6486 /* For a bit-field or packed field, bind to a temporary. */
6487 expr
= rvalue (expr
);
6490 /* Resolve overloaded address here rather than once in
6491 implicit_conversion and again in the inverse code below. */
6492 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
6494 expr
= instantiate_type (type
, expr
, complain
);
6495 intype
= TREE_TYPE (expr
);
6498 /* [expr.static.cast]
6500 Any expression can be explicitly converted to type cv void. */
6501 if (VOID_TYPE_P (type
))
6502 return convert_to_void (expr
, ICV_CAST
, complain
);
6505 An abstract class shall not be used ... as the type of an explicit
6507 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
6508 return error_mark_node
;
6510 /* [expr.static.cast]
6512 An expression e can be explicitly converted to a type T using a
6513 static_cast of the form static_cast<T>(e) if the declaration T
6514 t(e);" is well-formed, for some invented temporary variable
6516 result
= perform_direct_initialization_if_possible (type
, expr
,
6517 c_cast_p
, complain
);
6520 result
= convert_from_reference (result
);
6522 /* [expr.static.cast]
6524 If T is a reference type, the result is an lvalue; otherwise,
6525 the result is an rvalue. */
6526 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6527 result
= rvalue (result
);
6531 /* [expr.static.cast]
6533 The inverse of any standard conversion sequence (clause _conv_),
6534 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6535 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6536 (_conv.bool_) conversions, can be performed explicitly using
6537 static_cast subject to the restriction that the explicit
6538 conversion does not cast away constness (_expr.const.cast_), and
6539 the following additional rules for specific cases: */
6540 /* For reference, the conversions not excluded are: integral
6541 promotions, floating point promotion, integral conversions,
6542 floating point conversions, floating-integral conversions,
6543 pointer conversions, and pointer to member conversions. */
6546 A value of integral _or enumeration_ type can be explicitly
6547 converted to an enumeration type. */
6548 /* The effect of all that is that any conversion between any two
6549 types which are integral, floating, or enumeration types can be
6551 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6552 || SCALAR_FLOAT_TYPE_P (type
))
6553 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
6554 || SCALAR_FLOAT_TYPE_P (intype
)))
6555 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
6557 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
6558 && CLASS_TYPE_P (TREE_TYPE (type
))
6559 && CLASS_TYPE_P (TREE_TYPE (intype
))
6560 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6561 (TREE_TYPE (intype
))),
6562 build_pointer_type (TYPE_MAIN_VARIANT
6563 (TREE_TYPE (type
))),
6569 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6571 return error_mark_node
;
6572 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
6573 c_cast_p
? ba_unique
: ba_check
,
6575 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6577 return cp_fold_convert(type
, expr
);
6580 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6581 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6588 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
6589 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
6591 if (TYPE_PTRDATAMEM_P (type
))
6593 t1
= (build_ptrmem_type
6595 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
6596 t2
= (build_ptrmem_type
6598 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
6605 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
6608 && check_for_casting_away_constness (intype
, type
,
6611 return error_mark_node
;
6612 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
6613 c_cast_p
, complain
);
6617 /* [expr.static.cast]
6619 An rvalue of type "pointer to cv void" can be explicitly
6620 converted to a pointer to object type. A value of type pointer
6621 to object converted to "pointer to cv void" and back to the
6622 original pointer type will have its original value. */
6623 if (TYPE_PTR_P (intype
)
6624 && VOID_TYPE_P (TREE_TYPE (intype
))
6625 && TYPE_PTROB_P (type
))
6628 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6630 return error_mark_node
;
6631 return build_nop (type
, expr
);
6635 return error_mark_node
;
6638 /* Return an expression representing static_cast<TYPE>(EXPR). */
6641 build_static_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6646 if (type
== error_mark_node
|| expr
== error_mark_node
)
6647 return error_mark_node
;
6649 if (processing_template_decl
)
6651 expr
= build_min (STATIC_CAST_EXPR
, type
, expr
);
6652 /* We don't know if it will or will not have side effects. */
6653 TREE_SIDE_EFFECTS (expr
) = 1;
6654 return convert_from_reference (expr
);
6657 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6658 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6659 if (TREE_CODE (type
) != REFERENCE_TYPE
6660 && TREE_CODE (expr
) == NOP_EXPR
6661 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6662 expr
= TREE_OPERAND (expr
, 0);
6664 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
6668 if (result
!= error_mark_node
)
6669 maybe_warn_about_useless_cast (type
, expr
, complain
);
6673 if (complain
& tf_error
)
6674 error ("invalid static_cast from type %qT to type %qT",
6675 TREE_TYPE (expr
), type
);
6676 return error_mark_node
;
6679 /* EXPR is an expression with member function or pointer-to-member
6680 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6681 not permitted by ISO C++, but we accept it in some modes. If we
6682 are not in one of those modes, issue a diagnostic. Return the
6683 converted expression. */
6686 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
6691 intype
= TREE_TYPE (expr
);
6692 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
6693 || TREE_CODE (intype
) == METHOD_TYPE
);
6695 if (!(complain
& tf_warning_or_error
))
6696 return error_mark_node
;
6698 if (pedantic
|| warn_pmf2ptr
)
6699 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
6700 "converting from %qT to %qT", intype
, type
);
6702 if (TREE_CODE (intype
) == METHOD_TYPE
)
6703 expr
= build_addr_func (expr
, complain
);
6704 else if (TREE_CODE (expr
) == PTRMEM_CST
)
6705 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
6708 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
6709 decl
= build_address (decl
);
6710 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
6713 if (expr
== error_mark_node
)
6714 return error_mark_node
;
6716 return build_nop (type
, expr
);
6719 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6720 If C_CAST_P is true, this reinterpret cast is being done as part of
6721 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6722 indicate whether or not reinterpret_cast was valid. */
6725 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6726 bool *valid_p
, tsubst_flags_t complain
)
6730 /* Assume the cast is invalid. */
6734 if (type
== error_mark_node
|| error_operand_p (expr
))
6735 return error_mark_node
;
6737 intype
= TREE_TYPE (expr
);
6739 /* Save casted types in the function's used types hash table. */
6740 used_types_insert (type
);
6742 /* [expr.reinterpret.cast]
6743 An lvalue expression of type T1 can be cast to the type
6744 "reference to T2" if an expression of type "pointer to T1" can be
6745 explicitly converted to the type "pointer to T2" using a
6746 reinterpret_cast. */
6747 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6749 if (! real_lvalue_p (expr
))
6751 if (complain
& tf_error
)
6752 error ("invalid cast of an rvalue expression of type "
6755 return error_mark_node
;
6758 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6759 "B" are related class types; the reinterpret_cast does not
6760 adjust the pointer. */
6761 if (TYPE_PTR_P (intype
)
6762 && (complain
& tf_warning
)
6763 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
6764 COMPARE_BASE
| COMPARE_DERIVED
)))
6765 warning (0, "casting %qT to %qT does not dereference pointer",
6768 expr
= cp_build_addr_expr (expr
, complain
);
6770 if (warn_strict_aliasing
> 2)
6771 strict_aliasing_warning (TREE_TYPE (expr
), type
, expr
);
6773 if (expr
!= error_mark_node
)
6774 expr
= build_reinterpret_cast_1
6775 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
6777 if (expr
!= error_mark_node
)
6778 /* cp_build_indirect_ref isn't right for rvalue refs. */
6779 expr
= convert_from_reference (fold_convert (type
, expr
));
6783 /* As a G++ extension, we consider conversions from member
6784 functions, and pointers to member functions to
6785 pointer-to-function and pointer-to-void types. If
6786 -Wno-pmf-conversions has not been specified,
6787 convert_member_func_to_ptr will issue an error message. */
6788 if ((TYPE_PTRMEMFUNC_P (intype
)
6789 || TREE_CODE (intype
) == METHOD_TYPE
)
6790 && TYPE_PTR_P (type
)
6791 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6792 || VOID_TYPE_P (TREE_TYPE (type
))))
6793 return convert_member_func_to_ptr (type
, expr
, complain
);
6795 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6796 array-to-pointer, and function-to-pointer conversions are
6798 expr
= decay_conversion (expr
, complain
);
6800 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6801 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6802 if (TREE_CODE (expr
) == NOP_EXPR
6803 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6804 expr
= TREE_OPERAND (expr
, 0);
6806 if (error_operand_p (expr
))
6807 return error_mark_node
;
6809 intype
= TREE_TYPE (expr
);
6811 /* [expr.reinterpret.cast]
6812 A pointer can be converted to any integral type large enough to
6813 hold it. ... A value of type std::nullptr_t can be converted to
6814 an integral type; the conversion has the same meaning and
6815 validity as a conversion of (void*)0 to the integral type. */
6816 if (CP_INTEGRAL_TYPE_P (type
)
6817 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
6819 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
6821 if (complain
& tf_error
)
6822 permerror (input_location
, "cast from %qT to %qT loses precision",
6825 return error_mark_node
;
6827 if (NULLPTR_TYPE_P (intype
))
6828 return build_int_cst (type
, 0);
6830 /* [expr.reinterpret.cast]
6831 A value of integral or enumeration type can be explicitly
6832 converted to a pointer. */
6833 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
6836 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6837 || TYPE_PTR_OR_PTRMEM_P (type
))
6838 && same_type_p (type
, intype
))
6840 return fold_if_not_in_template (build_nop (type
, expr
));
6841 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
6842 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6843 return fold_if_not_in_template (build_nop (type
, expr
));
6844 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6845 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
6850 && check_for_casting_away_constness (intype
, type
,
6851 REINTERPRET_CAST_EXPR
,
6853 return error_mark_node
;
6854 /* Warn about possible alignment problems. */
6855 if (STRICT_ALIGNMENT
&& warn_cast_align
6856 && (complain
& tf_warning
)
6857 && !VOID_TYPE_P (type
)
6858 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
6859 && COMPLETE_TYPE_P (TREE_TYPE (type
))
6860 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
6861 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (intype
)))
6862 warning (OPT_Wcast_align
, "cast from %qT to %qT "
6863 "increases required alignment of target type", intype
, type
);
6865 /* We need to strip nops here, because the front end likes to
6866 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6868 if (warn_strict_aliasing
<= 2)
6869 strict_aliasing_warning (intype
, type
, sexpr
);
6871 return fold_if_not_in_template (build_nop (type
, expr
));
6873 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
6874 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
6876 if (complain
& tf_warning
)
6877 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6878 object pointer type or vice versa is conditionally-supported." */
6879 warning (OPT_Wconditionally_supported
,
6880 "casting between pointer-to-function and pointer-to-object "
6881 "is conditionally-supported");
6882 return fold_if_not_in_template (build_nop (type
, expr
));
6884 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6885 return fold_if_not_in_template (convert_to_vector (type
, expr
));
6886 else if (TREE_CODE (intype
) == VECTOR_TYPE
6887 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6888 return fold_if_not_in_template (convert_to_integer (type
, expr
));
6893 if (complain
& tf_error
)
6894 error ("invalid cast from type %qT to type %qT", intype
, type
);
6895 return error_mark_node
;
6898 return cp_convert (type
, expr
, complain
);
6902 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6906 if (type
== error_mark_node
|| expr
== error_mark_node
)
6907 return error_mark_node
;
6909 if (processing_template_decl
)
6911 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
6913 if (!TREE_SIDE_EFFECTS (t
)
6914 && type_dependent_expression_p (expr
))
6915 /* There might turn out to be side effects inside expr. */
6916 TREE_SIDE_EFFECTS (t
) = 1;
6917 return convert_from_reference (t
);
6920 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
6921 /*valid_p=*/NULL
, complain
);
6922 if (r
!= error_mark_node
)
6923 maybe_warn_about_useless_cast (type
, expr
, complain
);
6927 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6928 return an appropriate expression. Otherwise, return
6929 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6930 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6931 performing a C-style cast, its value upon return will indicate
6932 whether or not the conversion succeeded. */
6935 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
6939 tree reference_type
;
6941 /* Callers are responsible for handling error_mark_node as a
6942 destination type. */
6943 gcc_assert (dst_type
!= error_mark_node
);
6944 /* In a template, callers should be building syntactic
6945 representations of casts, not using this machinery. */
6946 gcc_assert (!processing_template_decl
);
6948 /* Assume the conversion is invalid. */
6952 if (!POINTER_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
6954 if (complain
& tf_error
)
6955 error ("invalid use of const_cast with type %qT, "
6956 "which is not a pointer, "
6957 "reference, nor a pointer-to-data-member type", dst_type
);
6958 return error_mark_node
;
6961 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
6963 if (complain
& tf_error
)
6964 error ("invalid use of const_cast with type %qT, which is a pointer "
6965 "or reference to a function type", dst_type
);
6966 return error_mark_node
;
6969 /* Save casted types in the function's used types hash table. */
6970 used_types_insert (dst_type
);
6972 src_type
= TREE_TYPE (expr
);
6973 /* Expressions do not really have reference types. */
6974 if (TREE_CODE (src_type
) == REFERENCE_TYPE
)
6975 src_type
= TREE_TYPE (src_type
);
6977 /* [expr.const.cast]
6979 For two object types T1 and T2, if a pointer to T1 can be explicitly
6980 converted to the type "pointer to T2" using a const_cast, then the
6981 following conversions can also be made:
6983 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6984 type T2 using the cast const_cast<T2&>;
6986 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6987 type T2 using the cast const_cast<T2&&>; and
6989 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6990 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6992 if (TREE_CODE (dst_type
) == REFERENCE_TYPE
)
6994 reference_type
= dst_type
;
6995 if (!TYPE_REF_IS_RVALUE (dst_type
)
6996 ? real_lvalue_p (expr
)
6997 : (CLASS_TYPE_P (TREE_TYPE (dst_type
))
6999 : lvalue_or_rvalue_with_address_p (expr
)))
7003 if (complain
& tf_error
)
7004 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7005 src_type
, dst_type
);
7006 return error_mark_node
;
7008 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7009 src_type
= build_pointer_type (src_type
);
7013 reference_type
= NULL_TREE
;
7014 /* If the destination type is not a reference type, the
7015 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7016 conversions are performed. */
7017 src_type
= type_decays_to (src_type
);
7018 if (src_type
== error_mark_node
)
7019 return error_mark_node
;
7022 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7024 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7029 /* This cast is actually a C-style cast. Issue a warning if
7030 the user is making a potentially unsafe cast. */
7031 check_for_casting_away_constness (src_type
, dst_type
,
7032 CAST_EXPR
, complain
);
7036 expr
= cp_build_addr_expr (expr
, complain
);
7037 if (expr
== error_mark_node
)
7038 return error_mark_node
;
7039 expr
= build_nop (reference_type
, expr
);
7040 return convert_from_reference (expr
);
7044 expr
= decay_conversion (expr
, complain
);
7045 if (expr
== error_mark_node
)
7046 return error_mark_node
;
7048 /* build_c_cast puts on a NOP_EXPR to make the result not an
7049 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7050 non-lvalue context. */
7051 if (TREE_CODE (expr
) == NOP_EXPR
7052 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7053 expr
= TREE_OPERAND (expr
, 0);
7054 return build_nop (dst_type
, expr
);
7058 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7059 TREE_TYPE (src_type
)))
7060 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7064 if (complain
& tf_error
)
7065 error ("invalid const_cast from type %qT to type %qT",
7066 src_type
, dst_type
);
7067 return error_mark_node
;
7071 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7075 if (type
== error_mark_node
|| error_operand_p (expr
))
7076 return error_mark_node
;
7078 if (processing_template_decl
)
7080 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7082 if (!TREE_SIDE_EFFECTS (t
)
7083 && type_dependent_expression_p (expr
))
7084 /* There might turn out to be side effects inside expr. */
7085 TREE_SIDE_EFFECTS (t
) = 1;
7086 return convert_from_reference (t
);
7089 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7090 if (r
!= error_mark_node
)
7091 maybe_warn_about_useless_cast (type
, expr
, complain
);
7095 /* Like cp_build_c_cast, but for the c-common bits. */
7098 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7100 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7103 /* Build an expression representing an explicit C-style cast to type
7104 TYPE of expression EXPR. */
7107 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7113 if (type
== error_mark_node
|| error_operand_p (expr
))
7114 return error_mark_node
;
7116 if (processing_template_decl
)
7118 tree t
= build_min (CAST_EXPR
, type
,
7119 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7120 /* We don't know if it will or will not have side effects. */
7121 TREE_SIDE_EFFECTS (t
) = 1;
7122 return convert_from_reference (t
);
7125 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7126 'Class') should always be retained, because this information aids
7127 in method lookup. */
7128 if (objc_is_object_ptr (type
)
7129 && objc_is_object_ptr (TREE_TYPE (expr
)))
7130 return build_nop (type
, expr
);
7132 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7133 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7134 if (TREE_CODE (type
) != REFERENCE_TYPE
7135 && TREE_CODE (value
) == NOP_EXPR
7136 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7137 value
= TREE_OPERAND (value
, 0);
7139 if (TREE_CODE (type
) == ARRAY_TYPE
)
7141 /* Allow casting from T1* to T2[] because Cfront allows it.
7142 NIHCL uses it. It is not valid ISO C++ however. */
7143 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7145 if (complain
& tf_error
)
7146 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7148 return error_mark_node
;
7149 type
= build_pointer_type (TREE_TYPE (type
));
7153 if (complain
& tf_error
)
7154 error ("ISO C++ forbids casting to an array type %qT", type
);
7155 return error_mark_node
;
7159 if (TREE_CODE (type
) == FUNCTION_TYPE
7160 || TREE_CODE (type
) == METHOD_TYPE
)
7162 if (complain
& tf_error
)
7163 error ("invalid cast to function type %qT", type
);
7164 return error_mark_node
;
7167 if (TYPE_PTR_P (type
)
7168 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7169 /* Casting to an integer of smaller size is an error detected elsewhere. */
7170 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7171 /* Don't warn about converting any constant. */
7172 && !TREE_CONSTANT (value
))
7173 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7174 "cast to pointer from integer of different size");
7176 /* A C-style cast can be a const_cast. */
7177 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7181 if (result
!= error_mark_node
)
7182 maybe_warn_about_useless_cast (type
, value
, complain
);
7186 /* Or a static cast. */
7187 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7188 &valid_p
, complain
);
7189 /* Or a reinterpret_cast. */
7191 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7192 &valid_p
, complain
);
7193 /* The static_cast or reinterpret_cast may be followed by a
7196 /* A valid cast may result in errors if, for example, a
7197 conversion to an ambiguous base class is required. */
7198 && !error_operand_p (result
))
7202 maybe_warn_about_useless_cast (type
, value
, complain
);
7204 /* Non-class rvalues always have cv-unqualified type. */
7205 if (!CLASS_TYPE_P (type
))
7206 type
= TYPE_MAIN_VARIANT (type
);
7207 result_type
= TREE_TYPE (result
);
7208 if (!CLASS_TYPE_P (result_type
) && TREE_CODE (type
) != REFERENCE_TYPE
)
7209 result_type
= TYPE_MAIN_VARIANT (result_type
);
7210 /* If the type of RESULT does not match TYPE, perform a
7211 const_cast to make it match. If the static_cast or
7212 reinterpret_cast succeeded, we will differ by at most
7213 cv-qualification, so the follow-on const_cast is guaranteed
7215 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
7217 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
7218 gcc_assert (valid_p
);
7223 return error_mark_node
;
7226 /* For use from the C common bits. */
7228 build_modify_expr (location_t
/*location*/,
7229 tree lhs
, tree
/*lhs_origtype*/,
7230 enum tree_code modifycode
,
7231 location_t
/*rhs_location*/, tree rhs
,
7232 tree
/*rhs_origtype*/)
7234 return cp_build_modify_expr (lhs
, modifycode
, rhs
, tf_warning_or_error
);
7237 /* Build an assignment expression of lvalue LHS from value RHS.
7238 MODIFYCODE is the code for a binary operator that we use
7239 to combine the old value of LHS with RHS to get the new value.
7240 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7242 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7245 cp_build_modify_expr (tree lhs
, enum tree_code modifycode
, tree rhs
,
7246 tsubst_flags_t complain
)
7250 tree lhstype
= TREE_TYPE (lhs
);
7251 tree olhstype
= lhstype
;
7252 bool plain_assign
= (modifycode
== NOP_EXPR
);
7254 /* Avoid duplicate error messages from operands that had errors. */
7255 if (error_operand_p (lhs
) || error_operand_p (rhs
))
7256 return error_mark_node
;
7258 /* Handle control structure constructs used as "lvalues". */
7259 switch (TREE_CODE (lhs
))
7261 /* Handle --foo = 5; as these are valid constructs in C++. */
7262 case PREDECREMENT_EXPR
:
7263 case PREINCREMENT_EXPR
:
7264 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7265 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7266 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7267 TREE_OPERAND (lhs
, 1));
7268 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0),
7269 modifycode
, rhs
, complain
);
7270 if (newrhs
== error_mark_node
)
7271 return error_mark_node
;
7272 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7274 /* Handle (a, b) used as an "lvalue". */
7276 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7277 modifycode
, rhs
, complain
);
7278 if (newrhs
== error_mark_node
)
7279 return error_mark_node
;
7280 return build2 (COMPOUND_EXPR
, lhstype
,
7281 TREE_OPERAND (lhs
, 0), newrhs
);
7284 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7285 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7286 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7287 TREE_OPERAND (lhs
, 1));
7288 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
,
7290 if (newrhs
== error_mark_node
)
7291 return error_mark_node
;
7292 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7296 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7297 when neither operand has side-effects. */
7298 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7299 return error_mark_node
;
7301 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
7302 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
7304 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
7305 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
7307 TREE_OPERAND (lhs
, 0),
7308 TREE_OPERAND (lhs
, 1)),
7309 TREE_OPERAND (lhs
, 0),
7310 TREE_OPERAND (lhs
, 1));
7313 /* Handle (a ? b : c) used as an "lvalue". */
7316 /* Produce (a ? (b = rhs) : (c = rhs))
7317 except that the RHS goes through a save-expr
7318 so the code to compute it is only emitted once. */
7320 tree preeval
= NULL_TREE
;
7322 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
7324 if (complain
& tf_error
)
7325 error ("void value not ignored as it ought to be");
7326 return error_mark_node
;
7329 rhs
= stabilize_expr (rhs
, &preeval
);
7331 /* Check this here to avoid odd errors when trying to convert
7332 a throw to the type of the COND_EXPR. */
7333 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7334 return error_mark_node
;
7336 cond
= build_conditional_expr
7337 (input_location
, TREE_OPERAND (lhs
, 0),
7338 cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7339 modifycode
, rhs
, complain
),
7340 cp_build_modify_expr (TREE_OPERAND (lhs
, 2),
7341 modifycode
, rhs
, complain
),
7344 if (cond
== error_mark_node
)
7346 /* Make sure the code to compute the rhs comes out
7347 before the split. */
7349 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (lhs
), preeval
, cond
);
7357 if (modifycode
== INIT_EXPR
)
7359 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7360 /* Do the default thing. */;
7361 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
7363 /* Compound literal. */
7364 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
7365 /* Call convert to generate an error; see PR 11063. */
7366 rhs
= convert (lhstype
, rhs
);
7367 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
7368 TREE_SIDE_EFFECTS (result
) = 1;
7371 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
7372 /* Do the default thing. */;
7375 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
7376 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
7377 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
7379 release_tree_vector (rhs_vec
);
7380 if (result
== NULL_TREE
)
7381 return error_mark_node
;
7387 lhs
= require_complete_type_sfinae (lhs
, complain
);
7388 if (lhs
== error_mark_node
)
7389 return error_mark_node
;
7391 if (modifycode
== NOP_EXPR
)
7393 if (c_dialect_objc ())
7395 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
7400 /* `operator=' is not an inheritable operator. */
7401 if (! MAYBE_CLASS_TYPE_P (lhstype
))
7402 /* Do the default thing. */;
7405 result
= build_new_op (input_location
, MODIFY_EXPR
,
7406 LOOKUP_NORMAL
, lhs
, rhs
,
7407 make_node (NOP_EXPR
), /*overload=*/NULL
,
7409 if (result
== NULL_TREE
)
7410 return error_mark_node
;
7417 tree init
= NULL_TREE
;
7419 /* A binary op has been requested. Combine the old LHS
7420 value with the RHS producing the value we should actually
7421 store into the LHS. */
7422 gcc_assert (!((TREE_CODE (lhstype
) == REFERENCE_TYPE
7423 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
7424 || MAYBE_CLASS_TYPE_P (lhstype
)));
7426 /* Preevaluate the RHS to make sure its evaluation is complete
7427 before the lvalue-to-rvalue conversion of the LHS:
7429 [expr.ass] With respect to an indeterminately-sequenced
7430 function call, the operation of a compound assignment is a
7431 single evaluation. [ Note: Therefore, a function call shall
7432 not intervene between the lvalue-to-rvalue conversion and the
7433 side effect associated with any single compound assignment
7434 operator. -- end note ] */
7435 lhs
= stabilize_reference (lhs
);
7437 rhs
= stabilize_expr (rhs
, &init
);
7438 newrhs
= cp_build_binary_op (input_location
,
7439 modifycode
, lhs
, rhs
,
7441 if (newrhs
== error_mark_node
)
7443 if (complain
& tf_error
)
7444 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
7445 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
7446 return error_mark_node
;
7450 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
7452 /* Now it looks like a plain assignment. */
7453 modifycode
= NOP_EXPR
;
7454 if (c_dialect_objc ())
7456 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
7461 gcc_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
);
7462 gcc_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
);
7465 /* The left-hand side must be an lvalue. */
7466 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7467 return error_mark_node
;
7469 /* Warn about modifying something that is `const'. Don't warn if
7470 this is initialization. */
7471 if (modifycode
!= INIT_EXPR
7472 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
7473 /* Functions are not modifiable, even though they are
7475 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
7476 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
7477 /* If it's an aggregate and any field is const, then it is
7478 effectively const. */
7479 || (CLASS_TYPE_P (lhstype
)
7480 && C_TYPE_FIELDS_READONLY (lhstype
))))
7482 if (complain
& tf_error
)
7483 cxx_readonly_error (lhs
, lv_assign
);
7485 return error_mark_node
;
7488 /* If storing into a structure or union member, it may have been given a
7489 lowered bitfield type. We need to convert to the declared type first,
7490 so retrieve it now. */
7492 olhstype
= unlowered_expr_type (lhs
);
7494 /* Convert new value to destination type. */
7496 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
7500 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
7502 if (modifycode
!= INIT_EXPR
)
7504 if (complain
& tf_error
)
7505 error ("assigning to an array from an initializer list");
7506 return error_mark_node
;
7508 if (check_array_initializer (lhs
, lhstype
, newrhs
))
7509 return error_mark_node
;
7510 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7511 if (newrhs
== error_mark_node
)
7512 return error_mark_node
;
7515 /* C++11 8.5/17: "If the destination type is an array of characters,
7516 an array of char16_t, an array of char32_t, or an array of wchar_t,
7517 and the initializer is a string literal...". */
7518 else if (TREE_CODE (newrhs
) == STRING_CST
7519 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
7520 && modifycode
== INIT_EXPR
)
7522 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7523 if (newrhs
== error_mark_node
)
7524 return error_mark_node
;
7527 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
7528 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
7530 if (complain
& tf_error
)
7531 error ("incompatible types in assignment of %qT to %qT",
7532 TREE_TYPE (rhs
), lhstype
);
7533 return error_mark_node
;
7536 /* Allow array assignment in compiler-generated code. */
7537 else if (!current_function_decl
7538 || !DECL_DEFAULTED_FN (current_function_decl
))
7540 /* This routine is used for both initialization and assignment.
7541 Make sure the diagnostic message differentiates the context. */
7542 if (complain
& tf_error
)
7544 if (modifycode
== INIT_EXPR
)
7545 error ("array used as initializer");
7547 error ("invalid array assignment");
7549 return error_mark_node
;
7552 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
7553 ? 1 + (modifycode
!= INIT_EXPR
): 0;
7554 return build_vec_init (lhs
, NULL_TREE
, newrhs
,
7555 /*explicit_value_init_p=*/false,
7556 from_array
, complain
);
7559 if (modifycode
== INIT_EXPR
)
7560 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7561 LOOKUP_ONLYCONVERTING. */
7562 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
7563 ICR_INIT
, NULL_TREE
, 0,
7566 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
7567 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
7569 if (!same_type_p (lhstype
, olhstype
))
7570 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
7572 if (modifycode
!= INIT_EXPR
)
7574 if (TREE_CODE (newrhs
) == CALL_EXPR
7575 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
7576 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
7578 /* Can't initialize directly from a TARGET_EXPR, since that would
7579 cause the lhs to be constructed twice, and possibly result in
7580 accidental self-initialization. So we force the TARGET_EXPR to be
7581 expanded without a target. */
7582 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
7583 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
7584 TREE_OPERAND (newrhs
, 0));
7587 if (newrhs
== error_mark_node
)
7588 return error_mark_node
;
7590 if (c_dialect_objc () && flag_objc_gc
)
7592 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
7598 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
7599 lhstype
, lhs
, newrhs
);
7601 TREE_SIDE_EFFECTS (result
) = 1;
7603 TREE_NO_WARNING (result
) = 1;
7609 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7610 tree rhs
, tsubst_flags_t complain
)
7612 if (processing_template_decl
)
7613 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
7614 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
7617 if (modifycode
!= NOP_EXPR
)
7619 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
7620 make_node (modifycode
), /*overload=*/NULL
,
7624 TREE_NO_WARNING (rval
) = 1;
7628 return cp_build_modify_expr (lhs
, modifycode
, rhs
, complain
);
7631 /* Helper function for get_delta_difference which assumes FROM is a base
7632 class of TO. Returns a delta for the conversion of pointer-to-member
7633 of FROM to pointer-to-member of TO. If the conversion is invalid and
7634 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7635 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7636 If C_CAST_P is true, this conversion is taking place as part of a
7640 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
7641 tsubst_flags_t complain
)
7646 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
7649 if (binfo
== error_mark_node
)
7651 if (!(complain
& tf_error
))
7652 return error_mark_node
;
7654 error (" in pointer to member function conversion");
7655 return size_zero_node
;
7659 if (kind
!= bk_via_virtual
)
7660 return BINFO_OFFSET (binfo
);
7662 /* FROM is a virtual base class of TO. Issue an error or warning
7663 depending on whether or not this is a reinterpret cast. */
7665 if (!(complain
& tf_error
))
7666 return error_mark_node
;
7668 error ("pointer to member conversion via virtual base %qT",
7669 BINFO_TYPE (binfo_from_vbase (binfo
)));
7671 return size_zero_node
;
7678 /* Get difference in deltas for different pointer to member function
7679 types. If the conversion is invalid and tf_error is not set in
7680 COMPLAIN, returns error_mark_node, otherwise returns an integer
7681 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7682 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7683 conversions as well. If C_CAST_P is true this conversion is taking
7684 place as part of a C-style cast.
7686 Note that the naming of FROM and TO is kind of backwards; the return
7687 value is what we add to a TO in order to get a FROM. They are named
7688 this way because we call this function to find out how to convert from
7689 a pointer to member of FROM to a pointer to member of TO. */
7692 get_delta_difference (tree from
, tree to
,
7693 bool allow_inverse_p
,
7694 bool c_cast_p
, tsubst_flags_t complain
)
7698 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
7699 /* Pointer to member of incomplete class is permitted*/
7700 result
= size_zero_node
;
7702 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
7704 if (result
== error_mark_node
)
7705 return error_mark_node
;
7709 if (!allow_inverse_p
)
7711 if (!(complain
& tf_error
))
7712 return error_mark_node
;
7714 error_not_base_type (from
, to
);
7715 error (" in pointer to member conversion");
7716 result
= size_zero_node
;
7720 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
7722 if (result
== error_mark_node
)
7723 return error_mark_node
;
7726 result
= size_diffop_loc (input_location
,
7727 size_zero_node
, result
);
7730 if (!(complain
& tf_error
))
7731 return error_mark_node
;
7733 error_not_base_type (from
, to
);
7734 error (" in pointer to member conversion");
7735 result
= size_zero_node
;
7740 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node
,
7744 /* Return a constructor for the pointer-to-member-function TYPE using
7745 the other components as specified. */
7748 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
7753 vec
<constructor_elt
, va_gc
> *v
;
7755 /* Pull the FIELD_DECLs out of the type. */
7756 pfn_field
= TYPE_FIELDS (type
);
7757 delta_field
= DECL_CHAIN (pfn_field
);
7759 /* Make sure DELTA has the type we want. */
7760 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
7762 /* Convert to the correct target type if necessary. */
7763 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
7765 /* Finish creating the initializer. */
7767 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
7768 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
7769 u
= build_constructor (type
, v
);
7770 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
7771 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
7772 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
7774 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
7779 /* Build a constructor for a pointer to member function. It can be
7780 used to initialize global variables, local variable, or used
7781 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7784 If FORCE is nonzero, then force this conversion, even if
7785 we would rather not do it. Usually set when using an explicit
7786 cast. A C-style cast is being processed iff C_CAST_P is true.
7788 Return error_mark_node, if something goes wrong. */
7791 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
7792 tsubst_flags_t complain
)
7798 if (error_operand_p (pfn
))
7799 return error_mark_node
;
7801 pfn_type
= TREE_TYPE (pfn
);
7802 to_type
= build_ptrmemfunc_type (type
);
7804 /* Handle multiple conversions of pointer to member functions. */
7805 if (TYPE_PTRMEMFUNC_P (pfn_type
))
7807 tree delta
= NULL_TREE
;
7808 tree npfn
= NULL_TREE
;
7812 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
7813 LOOKUP_NORMAL
, complain
))
7815 if (complain
& tf_error
)
7816 error ("invalid conversion to type %qT from type %qT",
7819 return error_mark_node
;
7822 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
7823 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
7825 c_cast_p
, complain
);
7826 if (n
== error_mark_node
)
7827 return error_mark_node
;
7829 /* We don't have to do any conversion to convert a
7830 pointer-to-member to its own type. But, we don't want to
7831 just return a PTRMEM_CST if there's an explicit cast; that
7832 cast should make the expression an invalid template argument. */
7833 if (TREE_CODE (pfn
) != PTRMEM_CST
)
7835 if (same_type_p (to_type
, pfn_type
))
7837 else if (integer_zerop (n
))
7838 return build_reinterpret_cast (to_type
, pfn
,
7842 if (TREE_SIDE_EFFECTS (pfn
))
7843 pfn
= save_expr (pfn
);
7845 /* Obtain the function pointer and the current DELTA. */
7846 if (TREE_CODE (pfn
) == PTRMEM_CST
)
7847 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
7850 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
7851 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
7854 /* Just adjust the DELTA field. */
7855 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7856 (TREE_TYPE (delta
), ptrdiff_type_node
));
7857 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
7858 n
= cp_build_binary_op (input_location
,
7859 LSHIFT_EXPR
, n
, integer_one_node
,
7861 delta
= cp_build_binary_op (input_location
,
7862 PLUS_EXPR
, delta
, n
, complain
);
7863 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
7866 /* Handle null pointer to member function conversions. */
7867 if (null_ptr_cst_p (pfn
))
7869 pfn
= cp_build_c_cast (type
, pfn
, complain
);
7870 return build_ptrmemfunc1 (to_type
,
7875 if (type_unknown_p (pfn
))
7876 return instantiate_type (type
, pfn
, complain
);
7878 fn
= TREE_OPERAND (pfn
, 0);
7879 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
7880 /* In a template, we will have preserved the
7882 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
7883 return make_ptrmem_cst (to_type
, fn
);
7886 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7889 ??? There is no consistency as to the types returned for the above
7890 values. Some code acts as if it were a sizetype and some as if it were
7891 integer_type_node. */
7894 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
7896 tree type
= TREE_TYPE (cst
);
7897 tree fn
= PTRMEM_CST_MEMBER (cst
);
7898 tree ptr_class
, fn_class
;
7900 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
7902 /* The class that the function belongs to. */
7903 fn_class
= DECL_CONTEXT (fn
);
7905 /* The class that we're creating a pointer to member of. */
7906 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
7908 /* First, calculate the adjustment to the function's class. */
7909 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
7910 /*c_cast_p=*/0, tf_warning_or_error
);
7912 if (!DECL_VIRTUAL_P (fn
))
7913 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
7914 build_addr_func (fn
, tf_warning_or_error
));
7917 /* If we're dealing with a virtual function, we have to adjust 'this'
7918 again, to point to the base which provides the vtable entry for
7919 fn; the call will do the opposite adjustment. */
7920 tree orig_class
= DECL_CONTEXT (fn
);
7921 tree binfo
= binfo_or_else (orig_class
, fn_class
);
7922 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
7923 *delta
, BINFO_OFFSET (binfo
));
7924 *delta
= fold_if_not_in_template (*delta
);
7926 /* We set PFN to the vtable offset at which the function can be
7927 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7928 case delta is shifted left, and then incremented). */
7929 *pfn
= DECL_VINDEX (fn
);
7930 *pfn
= build2 (MULT_EXPR
, integer_type_node
, *pfn
,
7931 TYPE_SIZE_UNIT (vtable_entry_type
));
7932 *pfn
= fold_if_not_in_template (*pfn
);
7934 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
7936 case ptrmemfunc_vbit_in_pfn
:
7937 *pfn
= build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
7939 *pfn
= fold_if_not_in_template (*pfn
);
7942 case ptrmemfunc_vbit_in_delta
:
7943 *delta
= build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
7944 *delta
, integer_one_node
);
7945 *delta
= fold_if_not_in_template (*delta
);
7946 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
7947 *delta
, integer_one_node
);
7948 *delta
= fold_if_not_in_template (*delta
);
7955 *pfn
= build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
7956 *pfn
= fold_if_not_in_template (*pfn
);
7960 /* Return an expression for PFN from the pointer-to-member function
7964 pfn_from_ptrmemfunc (tree t
)
7966 if (TREE_CODE (t
) == PTRMEM_CST
)
7971 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
7976 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
7979 /* Return an expression for DELTA from the pointer-to-member function
7983 delta_from_ptrmemfunc (tree t
)
7985 if (TREE_CODE (t
) == PTRMEM_CST
)
7990 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
7995 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
7998 /* Convert value RHS to type TYPE as preparation for an assignment to
7999 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8000 implicit conversion is. If FNDECL is non-NULL, we are doing the
8001 conversion in order to pass the PARMNUMth argument of FNDECL.
8002 If FNDECL is NULL, we are doing the conversion in function pointer
8003 argument passing, conversion in initialization, etc. */
8006 convert_for_assignment (tree type
, tree rhs
,
8007 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8008 tsubst_flags_t complain
, int flags
)
8011 enum tree_code coder
;
8013 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8014 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
8015 rhs
= TREE_OPERAND (rhs
, 0);
8017 rhstype
= TREE_TYPE (rhs
);
8018 coder
= TREE_CODE (rhstype
);
8020 if (TREE_CODE (type
) == VECTOR_TYPE
&& coder
== VECTOR_TYPE
8021 && vector_types_convertible_p (type
, rhstype
, true))
8023 rhs
= mark_rvalue_use (rhs
);
8024 return convert (type
, rhs
);
8027 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8028 return error_mark_node
;
8029 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8030 return error_mark_node
;
8032 /* The RHS of an assignment cannot have void type. */
8033 if (coder
== VOID_TYPE
)
8035 if (complain
& tf_error
)
8036 error ("void value not ignored as it ought to be");
8037 return error_mark_node
;
8040 if (c_dialect_objc ())
8044 tree rname
= fndecl
;
8055 selector
= objc_message_selector ();
8057 if (selector
&& parmno
> 1)
8064 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8066 rhs
= mark_rvalue_use (rhs
);
8067 return convert (type
, rhs
);
8073 The expression is implicitly converted (clause _conv_) to the
8074 cv-unqualified type of the left operand.
8076 We allow bad conversions here because by the time we get to this point
8077 we are committed to doing the conversion. If we end up doing a bad
8078 conversion, convert_like will complain. */
8079 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8081 /* When -Wno-pmf-conversions is use, we just silently allow
8082 conversions from pointers-to-members to plain pointers. If
8083 the conversion doesn't work, cp_convert will complain. */
8085 && TYPE_PTR_P (type
)
8086 && TYPE_PTRMEMFUNC_P (rhstype
))
8087 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8090 if (complain
& tf_error
)
8092 /* If the right-hand side has unknown type, then it is an
8093 overloaded function. Call instantiate_type to get error
8095 if (rhstype
== unknown_type_node
)
8096 instantiate_type (type
, rhs
, tf_warning_or_error
);
8098 error ("cannot convert %qT to %qT for argument %qP to %qD",
8099 rhstype
, type
, parmnum
, fndecl
);
8103 case ICR_DEFAULT_ARGUMENT
:
8104 error ("cannot convert %qT to %qT in default argument",
8108 error ("cannot convert %qT to %qT in argument passing",
8111 case ICR_CONVERTING
:
8112 error ("cannot convert %qT to %qT",
8116 error ("cannot convert %qT to %qT in initialization",
8120 error ("cannot convert %qT to %qT in return",
8124 error ("cannot convert %qT to %qT in assignment",
8130 if (TYPE_PTR_P (rhstype
)
8131 && TYPE_PTR_P (type
)
8132 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
8133 && CLASS_TYPE_P (TREE_TYPE (type
))
8134 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
8135 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8136 (TREE_TYPE (rhstype
))),
8137 "class type %qT is incomplete", TREE_TYPE (rhstype
));
8139 return error_mark_node
;
8142 if (warn_suggest_attribute_format
)
8144 const enum tree_code codel
= TREE_CODE (type
);
8145 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8147 && check_missing_format_attribute (type
, rhstype
)
8148 && (complain
& tf_warning
))
8152 case ICR_DEFAULT_ARGUMENT
:
8154 warning (OPT_Wsuggest_attribute_format
,
8155 "parameter %qP of %qD might be a candidate "
8156 "for a format attribute", parmnum
, fndecl
);
8158 warning (OPT_Wsuggest_attribute_format
,
8159 "parameter might be a candidate "
8160 "for a format attribute");
8162 case ICR_CONVERTING
:
8163 warning (OPT_Wsuggest_attribute_format
,
8164 "target of conversion might be a candidate "
8165 "for a format attribute");
8168 warning (OPT_Wsuggest_attribute_format
,
8169 "target of initialization might be a candidate "
8170 "for a format attribute");
8173 warning (OPT_Wsuggest_attribute_format
,
8174 "return type might be a candidate "
8175 "for a format attribute");
8178 warning (OPT_Wsuggest_attribute_format
,
8179 "left-hand side of assignment might be a candidate "
8180 "for a format attribute");
8187 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8189 if (warn_parentheses
8190 && TREE_CODE (type
) == BOOLEAN_TYPE
8191 && TREE_CODE (rhs
) == MODIFY_EXPR
8192 && !TREE_NO_WARNING (rhs
)
8193 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
8194 && (complain
& tf_warning
))
8196 location_t loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8198 warning_at (loc
, OPT_Wparentheses
,
8199 "suggest parentheses around assignment used as truth value");
8200 TREE_NO_WARNING (rhs
) = 1;
8203 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
8207 /* Convert RHS to be of type TYPE.
8208 If EXP is nonzero, it is the target of the initialization.
8209 ERRTYPE indicates what kind of error the implicit conversion is.
8211 Two major differences between the behavior of
8212 `convert_for_assignment' and `convert_for_initialization'
8213 are that references are bashed in the former, while
8214 copied in the latter, and aggregates are assigned in
8215 the former (operator=) while initialized in the
8218 If using constructor make sure no conversion operator exists, if one does
8219 exist, an ambiguity exists. */
8222 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
8223 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8224 tsubst_flags_t complain
)
8226 enum tree_code codel
= TREE_CODE (type
);
8228 enum tree_code coder
;
8230 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8231 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8232 if (TREE_CODE (rhs
) == NOP_EXPR
8233 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
8234 && codel
!= REFERENCE_TYPE
)
8235 rhs
= TREE_OPERAND (rhs
, 0);
8237 if (type
== error_mark_node
8238 || rhs
== error_mark_node
8239 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
8240 return error_mark_node
;
8242 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
8243 && TREE_CODE (type
) != ARRAY_TYPE
8244 && (TREE_CODE (type
) != REFERENCE_TYPE
8245 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
8246 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
8247 && !TYPE_REFFN_P (type
))
8248 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
8249 rhs
= decay_conversion (rhs
, complain
);
8251 rhstype
= TREE_TYPE (rhs
);
8252 coder
= TREE_CODE (rhstype
);
8254 if (coder
== ERROR_MARK
)
8255 return error_mark_node
;
8257 /* We accept references to incomplete types, so we can
8258 return here before checking if RHS is of complete type. */
8260 if (codel
== REFERENCE_TYPE
)
8262 /* This should eventually happen in convert_arguments. */
8263 int savew
= 0, savee
= 0;
8266 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
8267 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
8270 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
8271 inform (input_location
,
8272 "in passing argument %P of %q+D", parmnum
, fndecl
);
8278 exp
= require_complete_type_sfinae (exp
, complain
);
8279 if (exp
== error_mark_node
)
8280 return error_mark_node
;
8282 rhstype
= non_reference (rhstype
);
8284 type
= complete_type (type
);
8286 if (DIRECT_INIT_EXPR_P (type
, rhs
))
8287 /* Don't try to do copy-initialization if we already have
8288 direct-initialization. */
8291 if (MAYBE_CLASS_TYPE_P (type
))
8292 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
8294 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
8298 /* If RETVAL is the address of, or a reference to, a local variable or
8299 temporary give an appropriate warning. */
8302 maybe_warn_about_returning_address_of_local (tree retval
)
8304 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
8305 tree whats_returned
= retval
;
8309 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
8310 whats_returned
= TREE_OPERAND (whats_returned
, 1);
8311 else if (CONVERT_EXPR_P (whats_returned
)
8312 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
8313 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8318 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
8320 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8322 while (TREE_CODE (whats_returned
) == COMPONENT_REF
8323 || TREE_CODE (whats_returned
) == ARRAY_REF
)
8324 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8326 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8328 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
8329 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
8331 warning (OPT_Wreturn_local_addr
, "returning reference to temporary");
8334 if (VAR_P (whats_returned
)
8335 && DECL_NAME (whats_returned
)
8336 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
8338 warning (OPT_Wreturn_local_addr
, "reference to non-lvalue returned");
8343 if (DECL_P (whats_returned
)
8344 && DECL_NAME (whats_returned
)
8345 && DECL_FUNCTION_SCOPE_P (whats_returned
)
8346 && !is_capture_proxy (whats_returned
)
8347 && !(TREE_STATIC (whats_returned
)
8348 || TREE_PUBLIC (whats_returned
)))
8350 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8351 warning (OPT_Wreturn_local_addr
, "reference to local variable %q+D returned",
8353 else if (TREE_CODE (whats_returned
) == LABEL_DECL
)
8354 warning (OPT_Wreturn_local_addr
, "address of label %q+D returned",
8357 warning (OPT_Wreturn_local_addr
, "address of local variable %q+D "
8358 "returned", whats_returned
);
8363 /* Check that returning RETVAL from the current function is valid.
8364 Return an expression explicitly showing all conversions required to
8365 change RETVAL into the function return type, and to assign it to
8366 the DECL_RESULT for the function. Set *NO_WARNING to true if
8367 code reaches end of non-void function warning shouldn't be issued
8368 on this RETURN_EXPR. */
8371 check_return_expr (tree retval
, bool *no_warning
)
8374 /* The type actually returned by the function. */
8376 /* The type the function is declared to return, or void if
8377 the declared type is incomplete. */
8379 int fn_returns_value_p
;
8380 bool named_return_value_okay_p
;
8382 *no_warning
= false;
8384 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
8386 error_at (EXPR_LOCATION (retval
), "use of %<_Cilk_spawn%> in a return "
8387 "statement is not allowed");
8391 /* A `volatile' function is one that isn't supposed to return, ever.
8392 (This is a G++ extension, used to get better code for functions
8393 that call the `volatile' function.) */
8394 if (TREE_THIS_VOLATILE (current_function_decl
))
8395 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8397 /* Check for various simple errors. */
8398 if (DECL_DESTRUCTOR_P (current_function_decl
))
8401 error ("returning a value from a destructor");
8404 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
8406 if (in_function_try_handler
)
8407 /* If a return statement appears in a handler of the
8408 function-try-block of a constructor, the program is ill-formed. */
8409 error ("cannot return from a handler of a function-try-block of a constructor");
8411 /* You can't return a value from a constructor. */
8412 error ("returning a value from a constructor");
8416 if (processing_template_decl
)
8418 current_function_returns_value
= 1;
8419 if (check_for_bare_parameter_packs (retval
))
8420 retval
= error_mark_node
;
8424 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8426 /* Deduce auto return type from a return statement. */
8427 if (current_function_auto_return_pattern
)
8432 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
8434 /* Give a helpful error message. */
8435 error ("return-statement with no value, in function returning %qT",
8436 current_function_auto_return_pattern
);
8437 inform (input_location
, "only plain %<auto%> return type can be "
8438 "deduced to %<void%>");
8439 type
= error_mark_node
;
8441 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
8443 error ("returning initializer list");
8444 type
= error_mark_node
;
8450 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
8451 type
= do_auto_deduction (current_function_auto_return_pattern
,
8455 if (type
== error_mark_node
)
8457 else if (functype
== current_function_auto_return_pattern
)
8458 apply_deduced_return_type (current_function_decl
, type
);
8460 /* A mismatch should have been diagnosed in do_auto_deduction. */
8461 gcc_assert (same_type_p (type
, functype
));
8465 /* When no explicit return-value is given in a function with a named
8466 return value, the named return value is used. */
8467 result
= DECL_RESULT (current_function_decl
);
8468 valtype
= TREE_TYPE (result
);
8469 gcc_assert (valtype
!= NULL_TREE
);
8470 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
8471 if (!retval
&& DECL_NAME (result
) && fn_returns_value_p
)
8474 /* Check for a return statement with no return value in a function
8475 that's supposed to return a value. */
8476 if (!retval
&& fn_returns_value_p
)
8478 if (functype
!= error_mark_node
)
8479 permerror (input_location
, "return-statement with no value, in "
8480 "function returning %qT", valtype
);
8481 /* Remember that this function did return. */
8482 current_function_returns_value
= 1;
8483 /* And signal caller that TREE_NO_WARNING should be set on the
8484 RETURN_EXPR to avoid control reaches end of non-void function
8485 warnings in tree-cfg.c. */
8488 /* Check for a return statement with a value in a function that
8489 isn't supposed to return a value. */
8490 else if (retval
&& !fn_returns_value_p
)
8492 if (VOID_TYPE_P (TREE_TYPE (retval
)))
8493 /* You can return a `void' value from a function of `void'
8494 type. In that case, we have to evaluate the expression for
8495 its side-effects. */
8496 finish_expr_stmt (retval
);
8498 permerror (input_location
, "return-statement with a value, in function "
8499 "returning 'void'");
8500 current_function_returns_null
= 1;
8502 /* There's really no value to return, after all. */
8506 /* Remember that this function can sometimes return without a
8508 current_function_returns_null
= 1;
8510 /* Remember that this function did return a value. */
8511 current_function_returns_value
= 1;
8513 /* Check for erroneous operands -- but after giving ourselves a
8514 chance to provide an error about returning a value from a void
8516 if (error_operand_p (retval
))
8518 current_function_return_value
= error_mark_node
;
8519 return error_mark_node
;
8522 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8523 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
8524 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
8525 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
8527 && retval
&& null_ptr_cst_p (retval
))
8528 warning (0, "%<operator new%> must not return NULL unless it is "
8529 "declared %<throw()%> (or -fcheck-new is in effect)");
8531 /* Effective C++ rule 15. See also start_function. */
8533 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
))
8537 /* The function return type must be a reference to the current
8539 if (TREE_CODE (valtype
) == REFERENCE_TYPE
8540 && same_type_ignoring_top_level_qualifiers_p
8541 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
8543 /* Returning '*this' is obviously OK. */
8544 if (retval
== current_class_ref
)
8546 /* If we are calling a function whose return type is the same of
8547 the current class reference, it is ok. */
8548 else if (INDIRECT_REF_P (retval
)
8549 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
8554 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
8557 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8559 [...] For a function with a class return type, if the expression
8560 in the return statement is the name of a local object, and the cv-
8561 unqualified type of the local object is the same as the function
8562 return type, an implementation is permitted to omit creating the tem-
8563 porary object to hold the function return value [...]
8565 So, if this is a value-returning function that always returns the same
8566 local variable, remember it.
8568 It might be nice to be more flexible, and choose the first suitable
8569 variable even if the function sometimes returns something else, but
8570 then we run the risk of clobbering the variable we chose if the other
8571 returned expression uses the chosen variable somehow. And people expect
8572 this restriction, anyway. (jason 2000-11-19)
8574 See finish_function and finalize_nrv for the rest of this optimization. */
8576 named_return_value_okay_p
=
8577 (retval
!= NULL_TREE
8578 /* Must be a local, automatic variable. */
8580 && DECL_CONTEXT (retval
) == current_function_decl
8581 && ! TREE_STATIC (retval
)
8582 /* And not a lambda or anonymous union proxy. */
8583 && !DECL_HAS_VALUE_EXPR_P (retval
)
8584 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
8585 /* The cv-unqualified type of the returned value must be the
8586 same as the cv-unqualified return type of the
8588 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8589 (TYPE_MAIN_VARIANT (functype
)))
8590 /* And the returned value must be non-volatile. */
8591 && ! TYPE_VOLATILE (TREE_TYPE (retval
)));
8593 if (fn_returns_value_p
&& flag_elide_constructors
)
8595 if (named_return_value_okay_p
8596 && (current_function_return_value
== NULL_TREE
8597 || current_function_return_value
== retval
))
8598 current_function_return_value
= retval
;
8600 current_function_return_value
= error_mark_node
;
8603 /* We don't need to do any conversions when there's nothing being
8608 /* Do any required conversions. */
8609 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
8610 /* No conversions are required. */
8614 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
8616 /* The functype's return type will have been set to void, if it
8617 was an incomplete type. Just treat this as 'return;' */
8618 if (VOID_TYPE_P (functype
))
8619 return error_mark_node
;
8621 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8622 treated as an rvalue for the purposes of overload resolution to
8623 favor move constructors over copy constructors.
8625 Note that these conditions are similar to, but not as strict as,
8626 the conditions for the named return value optimization. */
8627 if ((cxx_dialect
!= cxx98
)
8628 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
8629 || TREE_CODE (retval
) == PARM_DECL
)
8630 && DECL_CONTEXT (retval
) == current_function_decl
8631 && !TREE_STATIC (retval
)
8632 /* This is only interesting for class type. */
8633 && CLASS_TYPE_P (functype
))
8634 flags
= flags
| LOOKUP_PREFER_RVALUE
;
8636 /* First convert the value to the function's return type, then
8637 to the type of return value's location to handle the
8638 case that functype is smaller than the valtype. */
8639 retval
= convert_for_initialization
8640 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
8641 tf_warning_or_error
);
8642 retval
= convert (valtype
, retval
);
8644 /* If the conversion failed, treat this just like `return;'. */
8645 if (retval
== error_mark_node
)
8647 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8648 else if (! cfun
->returns_struct
8649 && TREE_CODE (retval
) == TARGET_EXPR
8650 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
8651 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
8652 TREE_OPERAND (retval
, 0));
8654 maybe_warn_about_returning_address_of_local (retval
);
8657 /* Actually copy the value returned into the appropriate location. */
8658 if (retval
&& retval
!= result
)
8659 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
8665 /* Returns nonzero if the pointer-type FROM can be converted to the
8666 pointer-type TO via a qualification conversion. If CONSTP is -1,
8667 then we return nonzero if the pointers are similar, and the
8668 cv-qualification signature of FROM is a proper subset of that of TO.
8670 If CONSTP is positive, then all outer pointers have been
8674 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
8676 bool to_more_cv_qualified
= false;
8677 bool is_opaque_pointer
= false;
8679 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8681 if (TREE_CODE (to
) != TREE_CODE (from
))
8684 if (TREE_CODE (from
) == OFFSET_TYPE
8685 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
8686 TYPE_OFFSET_BASETYPE (to
)))
8689 /* Const and volatile mean something different for function types,
8690 so the usual checks are not appropriate. */
8691 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
8693 if (!at_least_as_qualified_p (to
, from
))
8696 if (!at_least_as_qualified_p (from
, to
))
8700 to_more_cv_qualified
= true;
8704 constp
&= TYPE_READONLY (to
);
8707 if (TREE_CODE (to
) == VECTOR_TYPE
)
8708 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8710 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
8711 return ((constp
>= 0 || to_more_cv_qualified
)
8712 && (is_opaque_pointer
8713 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
8717 /* When comparing, say, char ** to char const **, this function takes
8718 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8719 types to this function. */
8722 comp_ptr_ttypes (tree to
, tree from
)
8724 return comp_ptr_ttypes_real (to
, from
, 1);
8727 /* Returns true iff FNTYPE is a non-class type that involves
8728 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8729 if a parameter type is ill-formed. */
8732 error_type_p (const_tree type
)
8736 switch (TREE_CODE (type
))
8742 case REFERENCE_TYPE
:
8744 return error_type_p (TREE_TYPE (type
));
8748 if (error_type_p (TREE_TYPE (type
)))
8750 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
8751 if (error_type_p (TREE_VALUE (t
)))
8756 if (TYPE_PTRMEMFUNC_P (type
))
8757 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
8765 /* Returns true if to and from are (possibly multi-level) pointers to the same
8766 type or inheritance-related types, regardless of cv-quals. */
8769 ptr_reasonably_similar (const_tree to
, const_tree from
)
8771 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8773 /* Any target type is similar enough to void. */
8774 if (VOID_TYPE_P (to
))
8775 return !error_type_p (from
);
8776 if (VOID_TYPE_P (from
))
8777 return !error_type_p (to
);
8779 if (TREE_CODE (to
) != TREE_CODE (from
))
8782 if (TREE_CODE (from
) == OFFSET_TYPE
8783 && comptypes (TYPE_OFFSET_BASETYPE (to
),
8784 TYPE_OFFSET_BASETYPE (from
),
8785 COMPARE_BASE
| COMPARE_DERIVED
))
8788 if (TREE_CODE (to
) == VECTOR_TYPE
8789 && vector_types_convertible_p (to
, from
, false))
8792 if (TREE_CODE (to
) == INTEGER_TYPE
8793 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
8796 if (TREE_CODE (to
) == FUNCTION_TYPE
)
8797 return !error_type_p (to
) && !error_type_p (from
);
8799 if (!TYPE_PTR_P (to
))
8801 /* When either type is incomplete avoid DERIVED_FROM_P,
8802 which may call complete_type (c++/57942). */
8803 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
8805 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
8806 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
8811 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8812 pointer-to-member types) are the same, ignoring cv-qualification at
8816 comp_ptr_ttypes_const (tree to
, tree from
)
8818 bool is_opaque_pointer
= false;
8820 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8822 if (TREE_CODE (to
) != TREE_CODE (from
))
8825 if (TREE_CODE (from
) == OFFSET_TYPE
8826 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
8827 TYPE_OFFSET_BASETYPE (to
)))
8830 if (TREE_CODE (to
) == VECTOR_TYPE
)
8831 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8833 if (!TYPE_PTR_P (to
))
8834 return (is_opaque_pointer
8835 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
8839 /* Returns the type qualifiers for this type, including the qualifiers on the
8840 elements for an array type. */
8843 cp_type_quals (const_tree type
)
8846 /* This CONST_CAST is okay because strip_array_types returns its
8847 argument unmodified and we assign it to a const_tree. */
8848 type
= strip_array_types (CONST_CAST_TREE (type
));
8849 if (type
== error_mark_node
8850 /* Quals on a FUNCTION_TYPE are memfn quals. */
8851 || TREE_CODE (type
) == FUNCTION_TYPE
)
8852 return TYPE_UNQUALIFIED
;
8853 quals
= TYPE_QUALS (type
);
8854 /* METHOD and REFERENCE_TYPEs should never have quals. */
8855 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
8856 && TREE_CODE (type
) != REFERENCE_TYPE
)
8857 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
8858 == TYPE_UNQUALIFIED
));
8862 /* Returns the function-ref-qualifier for TYPE */
8865 type_memfn_rqual (const_tree type
)
8867 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
8868 || TREE_CODE (type
) == METHOD_TYPE
);
8870 if (!FUNCTION_REF_QUALIFIED (type
))
8871 return REF_QUAL_NONE
;
8872 else if (FUNCTION_RVALUE_QUALIFIED (type
))
8873 return REF_QUAL_RVALUE
;
8875 return REF_QUAL_LVALUE
;
8878 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8882 type_memfn_quals (const_tree type
)
8884 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8885 return TYPE_QUALS (type
);
8886 else if (TREE_CODE (type
) == METHOD_TYPE
)
8887 return cp_type_quals (class_of_this_parm (type
));
8892 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8893 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8896 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
8898 /* Could handle METHOD_TYPE here if necessary. */
8899 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
8900 if (TYPE_QUALS (type
) == memfn_quals
8901 && type_memfn_rqual (type
) == rqual
)
8904 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8906 tree result
= build_qualified_type (type
, memfn_quals
);
8907 result
= build_exception_variant (result
, TYPE_RAISES_EXCEPTIONS (type
));
8908 return build_ref_qualified_type (result
, rqual
);
8911 /* Returns nonzero if TYPE is const or volatile. */
8914 cv_qualified_p (const_tree type
)
8916 int quals
= cp_type_quals (type
);
8917 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
8920 /* Returns nonzero if the TYPE contains a mutable member. */
8923 cp_has_mutable_p (const_tree type
)
8925 /* This CONST_CAST is okay because strip_array_types returns its
8926 argument unmodified and we assign it to a const_tree. */
8927 type
= strip_array_types (CONST_CAST_TREE(type
));
8929 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
8932 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8933 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8934 approximation. In particular, consider:
8937 struct S { int i; };
8938 const S s = { f(); }
8940 Here, we will make "s" as TREE_READONLY (because it is declared
8941 "const") -- only to reverse ourselves upon seeing that the
8942 initializer is non-constant. */
8945 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
8947 tree type
= TREE_TYPE (decl
);
8949 if (type
== error_mark_node
)
8952 if (TREE_CODE (decl
) == TYPE_DECL
)
8955 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
8956 && type_quals
!= TYPE_UNQUALIFIED
));
8958 /* Avoid setting TREE_READONLY incorrectly. */
8959 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8960 constructor can produce constant init, so rely on cp_finish_decl to
8961 clear TREE_READONLY if the variable has non-constant init. */
8963 /* If the type has (or might have) a mutable component, that component
8964 might be modified. */
8965 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
8966 type_quals
&= ~TYPE_QUAL_CONST
;
8968 c_apply_type_quals_to_decl (type_quals
, decl
);
8971 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8972 exemplar types such that casting T1 to T2 is casting away constness
8973 if and only if there is no implicit conversion from T1 to T2. */
8976 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
8981 /* [expr.const.cast]
8983 For multi-level pointer to members and multi-level mixed pointers
8984 and pointers to members (conv.qual), the "member" aspect of a
8985 pointer to member level is ignored when determining if a const
8986 cv-qualifier has been cast away. */
8987 /* [expr.const.cast]
8989 For two pointer types:
8991 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8992 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8995 casting from X1 to X2 casts away constness if, for a non-pointer
8996 type T there does not exist an implicit conversion (clause
8999 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9003 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9004 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
9005 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
9007 *t1
= cp_build_qualified_type (void_type_node
,
9008 cp_type_quals (*t1
));
9009 *t2
= cp_build_qualified_type (void_type_node
,
9010 cp_type_quals (*t2
));
9014 quals1
= cp_type_quals (*t1
);
9015 quals2
= cp_type_quals (*t2
);
9017 if (TYPE_PTRDATAMEM_P (*t1
))
9018 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
9020 *t1
= TREE_TYPE (*t1
);
9021 if (TYPE_PTRDATAMEM_P (*t2
))
9022 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
9024 *t2
= TREE_TYPE (*t2
);
9026 casts_away_constness_r (t1
, t2
, complain
);
9027 *t1
= build_pointer_type (*t1
);
9028 *t2
= build_pointer_type (*t2
);
9029 *t1
= cp_build_qualified_type (*t1
, quals1
);
9030 *t2
= cp_build_qualified_type (*t2
, quals2
);
9033 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9036 ??? This function returns non-zero if casting away qualifiers not
9037 just const. We would like to return to the caller exactly which
9038 qualifiers are casted away to give more accurate diagnostics.
9042 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
9044 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
9046 /* [expr.const.cast]
9048 Casting from an lvalue of type T1 to an lvalue of type T2
9049 using a reference cast casts away constness if a cast from an
9050 rvalue of type "pointer to T1" to the type "pointer to T2"
9051 casts away constness. */
9052 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
? TREE_TYPE (t1
) : t1
);
9053 return casts_away_constness (build_pointer_type (t1
),
9054 build_pointer_type (TREE_TYPE (t2
)),
9058 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
9059 /* [expr.const.cast]
9061 Casting from an rvalue of type "pointer to data member of X
9062 of type T1" to the type "pointer to data member of Y of type
9063 T2" casts away constness if a cast from an rvalue of type
9064 "pointer to T1" to the type "pointer to T2" casts away
9066 return casts_away_constness
9067 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
9068 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
9071 /* Casting away constness is only something that makes sense for
9072 pointer or reference types. */
9073 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
9076 /* Top-level qualifiers don't matter. */
9077 t1
= TYPE_MAIN_VARIANT (t1
);
9078 t2
= TYPE_MAIN_VARIANT (t2
);
9079 casts_away_constness_r (&t1
, &t2
, complain
);
9080 if (!can_convert (t2
, t1
, complain
))
9086 /* If T is a REFERENCE_TYPE return the type to which T refers.
9087 Otherwise, return T itself. */
9090 non_reference (tree t
)
9092 if (t
&& TREE_CODE (t
) == REFERENCE_TYPE
)
9098 /* Return nonzero if REF is an lvalue valid for this language;
9099 otherwise, print an error message and return zero. USE says
9100 how the lvalue is being used and so selects the error message. */
9103 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
9105 cp_lvalue_kind kind
= lvalue_kind (ref
);
9107 if (kind
== clk_none
)
9109 if (complain
& tf_error
)
9110 lvalue_error (input_location
, use
);
9113 else if (kind
& (clk_rvalueref
|clk_class
))
9115 if (!(complain
& tf_error
))
9117 if (kind
& clk_class
)
9118 /* Make this a permerror because we used to accept it. */
9119 permerror (input_location
, "using temporary as lvalue");
9121 error ("using xvalue (rvalue reference) as lvalue");
9126 /* Return true if a user-defined literal operator is a raw operator. */
9129 check_raw_literal_operator (const_tree decl
)
9131 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9134 bool maybe_raw_p
= false;
9136 /* Count the number and type of arguments and check for ellipsis. */
9137 for (argtype
= argtypes
, arity
= 0;
9138 argtype
&& argtype
!= void_list_node
;
9139 ++arity
, argtype
= TREE_CHAIN (argtype
))
9141 tree t
= TREE_VALUE (argtype
);
9143 if (same_type_p (t
, const_string_type_node
))
9147 return false; /* Found ellipsis. */
9149 if (!maybe_raw_p
|| arity
!= 1)
9156 /* Return true if a user-defined literal operator has one of the allowed
9160 check_literal_operator_args (const_tree decl
,
9161 bool *long_long_unsigned_p
, bool *long_double_p
)
9163 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9165 *long_long_unsigned_p
= false;
9166 *long_double_p
= false;
9167 if (processing_template_decl
|| processing_specialization
)
9168 return argtypes
== void_list_node
;
9175 /* Count the number and type of arguments and check for ellipsis. */
9176 for (argtype
= argtypes
, arity
= 0;
9177 argtype
&& argtype
!= void_list_node
;
9178 argtype
= TREE_CHAIN (argtype
))
9180 tree t
= TREE_VALUE (argtype
);
9185 bool maybe_raw_p
= false;
9187 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
9189 t
= TYPE_MAIN_VARIANT (t
);
9190 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
9191 || same_type_p (t
, wchar_type_node
)
9192 || same_type_p (t
, char16_type_node
)
9193 || same_type_p (t
, char32_type_node
))
9195 argtype
= TREE_CHAIN (argtype
);
9198 t
= TREE_VALUE (argtype
);
9199 if (maybe_raw_p
&& argtype
== void_list_node
)
9201 else if (same_type_p (t
, size_type_node
))
9210 else if (same_type_p (t
, long_long_unsigned_type_node
))
9213 *long_long_unsigned_p
= true;
9215 else if (same_type_p (t
, long_double_type_node
))
9218 *long_double_p
= true;
9220 else if (same_type_p (t
, char_type_node
))
9222 else if (same_type_p (t
, wchar_type_node
))
9224 else if (same_type_p (t
, char16_type_node
))
9226 else if (same_type_p (t
, char32_type_node
))
9232 return false; /* Found ellipsis. */
9234 if (arity
!= max_arity
)