1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
29 #include "coretypes.h"
32 #include "stor-layout.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
39 #include "gcc-rich-location.h"
41 static tree
cp_build_addr_expr_strict (tree
, tsubst_flags_t
);
42 static tree
cp_build_function_call (tree
, tree
, tsubst_flags_t
);
43 static tree
pfn_from_ptrmemfunc (tree
);
44 static tree
delta_from_ptrmemfunc (tree
);
45 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
47 static tree
cp_pointer_int_sum (location_t
, enum tree_code
, tree
, tree
,
49 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
51 static int comp_ptr_ttypes_real (tree
, tree
, int);
52 static bool comp_except_types (tree
, tree
, bool);
53 static bool comp_array_types (const_tree
, const_tree
, bool);
54 static tree
pointer_diff (location_t
, tree
, tree
, tree
, tsubst_flags_t
);
55 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
56 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
57 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
58 static bool maybe_warn_about_returning_address_of_local (tree
);
59 static tree
lookup_destructor (tree
, tree
, tree
, tsubst_flags_t
);
60 static void error_args_num (location_t
, tree
, bool);
61 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
64 /* Do `exp = require_complete_type (exp);' to make sure exp
65 does not have an incomplete type. (That includes void types.)
66 Returns error_mark_node if the VALUE does not have
67 complete type when this function returns. */
70 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
74 if (processing_template_decl
|| value
== error_mark_node
)
77 if (TREE_CODE (value
) == OVERLOAD
)
78 type
= unknown_type_node
;
80 type
= TREE_TYPE (value
);
82 if (type
== error_mark_node
)
83 return error_mark_node
;
85 /* First, detect a valid value with a complete type. */
86 if (COMPLETE_TYPE_P (type
))
89 if (complete_type_or_maybe_complain (type
, value
, complain
))
92 return error_mark_node
;
96 require_complete_type (tree value
)
98 return require_complete_type_sfinae (value
, tf_warning_or_error
);
101 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
102 a template instantiation, do the instantiation. Returns TYPE,
103 whether or not it could be completed, unless something goes
104 horribly wrong, in which case the error_mark_node is returned. */
107 complete_type (tree type
)
109 if (type
== NULL_TREE
)
110 /* Rather than crash, we return something sure to cause an error
112 return error_mark_node
;
114 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
116 else if (TREE_CODE (type
) == ARRAY_TYPE
)
118 tree t
= complete_type (TREE_TYPE (type
));
119 unsigned int needs_constructing
, has_nontrivial_dtor
;
120 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
123 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
125 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
126 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
128 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
129 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
132 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
133 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
138 /* Like complete_type, but issue an error if the TYPE cannot be completed.
139 VALUE is used for informative diagnostics.
140 Returns NULL_TREE if the type cannot be made complete. */
143 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
145 type
= complete_type (type
);
146 if (type
== error_mark_node
)
147 /* We already issued an error. */
149 else if (!COMPLETE_TYPE_P (type
))
151 if (complain
& tf_error
)
152 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
160 complete_type_or_else (tree type
, tree value
)
162 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
165 /* Return truthvalue of whether type of EXP is instantiated. */
168 type_unknown_p (const_tree exp
)
170 return (TREE_CODE (exp
) == TREE_LIST
171 || TREE_TYPE (exp
) == unknown_type_node
);
175 /* Return the common type of two parameter lists.
176 We assume that comptypes has already been done and returned 1;
177 if that isn't so, this may crash.
179 As an optimization, free the space we allocate if the parameter
180 lists are already common. */
183 commonparms (tree p1
, tree p2
)
185 tree oldargs
= p1
, newargs
, n
;
189 len
= list_length (p1
);
190 newargs
= tree_last (p1
);
192 if (newargs
== void_list_node
)
201 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
206 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
208 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
210 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
213 else if (! TREE_PURPOSE (p1
))
215 if (TREE_PURPOSE (p2
))
217 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
223 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
225 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
227 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
230 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
233 TREE_VALUE (n
) = TREE_VALUE (p1
);
241 /* Given a type, perhaps copied for a typedef,
242 find the "original" version of it. */
244 original_type (tree t
)
246 int quals
= cp_type_quals (t
);
247 while (t
!= error_mark_node
248 && TYPE_NAME (t
) != NULL_TREE
)
250 tree x
= TYPE_NAME (t
);
251 if (TREE_CODE (x
) != TYPE_DECL
)
253 x
= DECL_ORIGINAL_TYPE (x
);
258 return cp_build_qualified_type (t
, quals
);
261 /* Return the common type for two arithmetic types T1 and T2 under the
262 usual arithmetic conversions. The default conversions have already
263 been applied, and enumerated types converted to their compatible
267 cp_common_type (tree t1
, tree t2
)
269 enum tree_code code1
= TREE_CODE (t1
);
270 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 || VECTOR_TYPE_P (t1
)
291 || UNSCOPED_ENUM_P (t1
));
292 gcc_assert (ARITHMETIC_TYPE_P (t2
)
293 || VECTOR_TYPE_P (t2
)
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
);
368 /* Go through the same procedure, but for longs. */
369 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
370 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
371 return build_type_attribute_variant (long_unsigned_type_node
,
373 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
374 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
376 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
377 ? long_unsigned_type_node
: long_integer_type_node
);
378 return build_type_attribute_variant (t
, attributes
);
381 /* For __intN types, either the type is __int128 (and is lower
382 priority than the types checked above, but higher than other
383 128-bit types) or it's known to not be the same size as other
384 types (enforced in toplev.c). Prefer the unsigned type. */
385 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
387 if (int_n_enabled_p
[i
]
388 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
389 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
390 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
391 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
393 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
394 ? int_n_trees
[i
].unsigned_type
395 : int_n_trees
[i
].signed_type
);
396 return build_type_attribute_variant (t
, attributes
);
400 /* Otherwise prefer the unsigned one. */
401 if (TYPE_UNSIGNED (t1
))
402 return build_type_attribute_variant (t1
, attributes
);
404 return build_type_attribute_variant (t2
, attributes
);
408 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
409 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
410 return build_type_attribute_variant (long_double_type_node
,
412 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
413 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
414 return build_type_attribute_variant (double_type_node
,
416 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
417 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
418 return build_type_attribute_variant (float_type_node
,
421 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
422 the standard C++ floating-point types. Logic earlier in this
423 function has already eliminated the possibility that
424 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
425 compelling reason to choose one or the other. */
426 return build_type_attribute_variant (t1
, attributes
);
430 /* T1 and T2 are arithmetic or enumeration types. Return the type
431 that will result from the "usual arithmetic conversions" on T1 and
432 T2 as described in [expr]. */
435 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
437 gcc_assert (ARITHMETIC_TYPE_P (t1
)
438 || VECTOR_TYPE_P (t1
)
439 || UNSCOPED_ENUM_P (t1
));
440 gcc_assert (ARITHMETIC_TYPE_P (t2
)
441 || VECTOR_TYPE_P (t2
)
442 || UNSCOPED_ENUM_P (t2
));
444 /* Perform the integral promotions. We do not promote real types here. */
445 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
446 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
448 t1
= type_promotes_to (t1
);
449 t2
= type_promotes_to (t2
);
452 return cp_common_type (t1
, t2
);
456 composite_pointer_error (diagnostic_t kind
, tree t1
, tree t2
,
457 composite_pointer_operation operation
)
462 emit_diagnostic (kind
, input_location
, 0,
463 "comparison between "
464 "distinct pointer types %qT and %qT lacks a cast",
468 emit_diagnostic (kind
, input_location
, 0,
469 "conversion between "
470 "distinct pointer types %qT and %qT lacks a cast",
473 case CPO_CONDITIONAL_EXPR
:
474 emit_diagnostic (kind
, input_location
, 0,
475 "conditional expression between "
476 "distinct pointer types %qT and %qT lacks a cast",
484 /* Subroutine of composite_pointer_type to implement the recursive
485 case. See that function for documentation of the parameters. */
488 composite_pointer_type_r (tree t1
, tree t2
,
489 composite_pointer_operation operation
,
490 tsubst_flags_t complain
)
497 /* Determine the types pointed to by T1 and T2. */
500 pointee1
= TREE_TYPE (t1
);
501 pointee2
= TREE_TYPE (t2
);
505 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
506 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
511 Otherwise, the composite pointer type is a pointer type
512 similar (_conv.qual_) to the type of one of the operands,
513 with a cv-qualification signature (_conv.qual_) that is the
514 union of the cv-qualification signatures of the operand
516 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
517 result_type
= pointee1
;
518 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
519 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
521 result_type
= composite_pointer_type_r (pointee1
, pointee2
, operation
,
523 if (result_type
== error_mark_node
)
524 return error_mark_node
;
528 if (complain
& tf_error
)
529 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
531 return error_mark_node
;
532 result_type
= void_type_node
;
534 result_type
= cp_build_qualified_type (result_type
,
535 (cp_type_quals (pointee1
)
536 | cp_type_quals (pointee2
)));
537 /* If the original types were pointers to members, so is the
539 if (TYPE_PTRMEM_P (t1
))
541 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
542 TYPE_PTRMEM_CLASS_TYPE (t2
)))
544 if (complain
& tf_error
)
545 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
547 return error_mark_node
;
549 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
553 result_type
= build_pointer_type (result_type
);
555 /* Merge the attributes. */
556 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
557 return build_type_attribute_variant (result_type
, attributes
);
560 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
561 ARG1 and ARG2 are the values with those types. The OPERATION is to
562 describe the operation between the pointer types,
563 in case an error occurs.
565 This routine also implements the computation of a common type for
566 pointers-to-members as per [expr.eq]. */
569 composite_pointer_type (tree t1
, tree t2
, tree arg1
, tree arg2
,
570 composite_pointer_operation operation
,
571 tsubst_flags_t complain
)
578 If one operand is a null pointer constant, the composite pointer
579 type is the type of the other operand. */
580 if (null_ptr_cst_p (arg1
))
582 if (null_ptr_cst_p (arg2
))
589 If one of the operands has type "pointer to cv1 void*", then
590 the other has type "pointer to cv2T", and the composite pointer
591 type is "pointer to cv12 void", where cv12 is the union of cv1
594 If either type is a pointer to void, make sure it is T1. */
595 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
598 /* Now, if T1 is a pointer to void, merge the qualifiers. */
599 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
604 if (TYPE_PTRFN_P (t2
))
606 if (complain
& tf_error
)
611 pedwarn (input_location
, OPT_Wpedantic
,
612 "ISO C++ forbids comparison between pointer "
613 "of type %<void *%> and pointer-to-function");
616 pedwarn (input_location
, OPT_Wpedantic
,
617 "ISO C++ forbids conversion between pointer "
618 "of type %<void *%> and pointer-to-function");
620 case CPO_CONDITIONAL_EXPR
:
621 pedwarn (input_location
, OPT_Wpedantic
,
622 "ISO C++ forbids conditional expression between "
623 "pointer of type %<void *%> and "
624 "pointer-to-function");
631 return error_mark_node
;
634 = cp_build_qualified_type (void_type_node
,
635 (cp_type_quals (TREE_TYPE (t1
))
636 | cp_type_quals (TREE_TYPE (t2
))));
637 result_type
= build_pointer_type (result_type
);
638 /* Merge the attributes. */
639 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
640 return build_type_attribute_variant (result_type
, attributes
);
643 if (c_dialect_objc () && TYPE_PTR_P (t1
)
646 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
647 return objc_common_type (t1
, t2
);
650 /* if T1 or T2 is "pointer to noexcept function" and the other type is
651 "pointer to function", where the function types are otherwise the same,
652 "pointer to function" */
653 if (fnptr_conv_p (t1
, t2
))
655 if (fnptr_conv_p (t2
, t1
))
658 /* [expr.eq] permits the application of a pointer conversion to
659 bring the pointers to a common type. */
660 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
661 && CLASS_TYPE_P (TREE_TYPE (t1
))
662 && CLASS_TYPE_P (TREE_TYPE (t2
))
663 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
666 class1
= TREE_TYPE (t1
);
667 class2
= TREE_TYPE (t2
);
669 if (DERIVED_FROM_P (class1
, class2
))
670 t2
= (build_pointer_type
671 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
672 else if (DERIVED_FROM_P (class2
, class1
))
673 t1
= (build_pointer_type
674 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
677 if (complain
& tf_error
)
678 composite_pointer_error (DK_ERROR
, t1
, t2
, operation
);
679 return error_mark_node
;
682 /* [expr.eq] permits the application of a pointer-to-member
683 conversion to change the class type of one of the types. */
684 else if (TYPE_PTRMEM_P (t1
)
685 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
686 TYPE_PTRMEM_CLASS_TYPE (t2
)))
688 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
689 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
691 if (DERIVED_FROM_P (class1
, class2
))
692 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
693 else if (DERIVED_FROM_P (class2
, class1
))
694 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
697 if (complain
& tf_error
)
701 error ("comparison between distinct "
702 "pointer-to-member types %qT and %qT lacks a cast",
706 error ("conversion between distinct "
707 "pointer-to-member types %qT and %qT lacks a cast",
710 case CPO_CONDITIONAL_EXPR
:
711 error ("conditional expression between distinct "
712 "pointer-to-member types %qT and %qT lacks a cast",
718 return error_mark_node
;
722 return composite_pointer_type_r (t1
, t2
, operation
, complain
);
725 /* Return the merged type of two types.
726 We assume that comptypes has already been done and returned 1;
727 if that isn't so, this may crash.
729 This just combines attributes and default arguments; any other
730 differences would cause the two types to compare unalike. */
733 merge_types (tree t1
, tree t2
)
735 enum tree_code code1
;
736 enum tree_code code2
;
739 /* Save time if the two types are the same. */
742 if (original_type (t1
) == original_type (t2
))
745 /* If one type is nonsense, use the other. */
746 if (t1
== error_mark_node
)
748 if (t2
== error_mark_node
)
751 /* Handle merging an auto redeclaration with a previous deduced
756 /* Merge the attributes. */
757 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
759 if (TYPE_PTRMEMFUNC_P (t1
))
760 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
761 if (TYPE_PTRMEMFUNC_P (t2
))
762 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
764 code1
= TREE_CODE (t1
);
765 code2
= TREE_CODE (t2
);
768 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
769 if (code1
== TYPENAME_TYPE
)
771 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
772 code1
= TREE_CODE (t1
);
776 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
777 code2
= TREE_CODE (t2
);
785 /* For two pointers, do this recursively on the target type. */
787 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
788 int quals
= cp_type_quals (t1
);
790 if (code1
== POINTER_TYPE
)
792 t1
= build_pointer_type (target
);
793 if (TREE_CODE (target
) == METHOD_TYPE
)
794 t1
= build_ptrmemfunc_type (t1
);
797 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
798 t1
= build_type_attribute_variant (t1
, attributes
);
799 t1
= cp_build_qualified_type (t1
, quals
);
808 quals
= cp_type_quals (t1
);
809 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
810 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
811 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
813 t1
= cp_build_qualified_type (t1
, quals
);
819 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
820 /* Save space: see if the result is identical to one of the args. */
821 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
822 return build_type_attribute_variant (t1
, attributes
);
823 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
824 return build_type_attribute_variant (t2
, attributes
);
825 /* Merge the element types, and have a size if either arg has one. */
826 t1
= build_cplus_array_type
827 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
832 /* Function types: prefer the one that specified arg types.
833 If both do, merge the arg types. Also merge the return types. */
835 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
836 tree p1
= TYPE_ARG_TYPES (t1
);
837 tree p2
= TYPE_ARG_TYPES (t2
);
840 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
842 /* Save space: see if the result is identical to one of the args. */
843 if (valtype
== TREE_TYPE (t1
) && ! p2
)
844 return cp_build_type_attribute_variant (t1
, attributes
);
845 if (valtype
== TREE_TYPE (t2
) && ! p1
)
846 return cp_build_type_attribute_variant (t2
, attributes
);
848 /* Simple way if one arg fails to specify argument types. */
849 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
851 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
854 parms
= commonparms (p1
, p2
);
856 rval
= build_function_type (valtype
, parms
);
857 gcc_assert (type_memfn_quals (t1
) == type_memfn_quals (t2
));
858 gcc_assert (type_memfn_rqual (t1
) == type_memfn_rqual (t2
));
859 rval
= apply_memfn_quals (rval
,
860 type_memfn_quals (t1
),
861 type_memfn_rqual (t1
));
862 raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
863 TYPE_RAISES_EXCEPTIONS (t2
));
864 t1
= build_exception_variant (rval
, raises
);
865 if (late_return_type_p
)
866 TYPE_HAS_LATE_RETURN_TYPE (t1
) = 1;
872 /* Get this value the long way, since TYPE_METHOD_BASETYPE
873 is just the main variant of this. */
874 tree basetype
= class_of_this_parm (t2
);
875 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
876 TYPE_RAISES_EXCEPTIONS (t2
));
877 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
879 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
880 bool late_return_type_2_p
= TYPE_HAS_LATE_RETURN_TYPE (t2
);
882 /* If this was a member function type, get back to the
883 original type of type member function (i.e., without
884 the class instance variable up front. */
885 t1
= build_function_type (TREE_TYPE (t1
),
886 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
887 t2
= build_function_type (TREE_TYPE (t2
),
888 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
889 t3
= merge_types (t1
, t2
);
890 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
891 TYPE_ARG_TYPES (t3
));
892 t1
= build_exception_variant (t3
, raises
);
893 t1
= build_ref_qualified_type (t1
, rqual
);
894 if (late_return_type_1_p
)
895 TYPE_HAS_LATE_RETURN_TYPE (t1
) = 1;
896 if (late_return_type_2_p
)
897 TYPE_HAS_LATE_RETURN_TYPE (t2
) = 1;
902 /* There is no need to merge attributes into a TYPENAME_TYPE.
903 When the type is instantiated it will have whatever
904 attributes result from the instantiation. */
910 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
912 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
915 return cp_build_type_attribute_variant (t1
, attributes
);
918 /* Return the ARRAY_TYPE type without its domain. */
921 strip_array_domain (tree type
)
924 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
925 if (TYPE_DOMAIN (type
) == NULL_TREE
)
927 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
928 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
931 /* Wrapper around cp_common_type that is used by c-common.c and other
932 front end optimizations that remove promotions.
934 Return the common type for two arithmetic types T1 and T2 under the
935 usual arithmetic conversions. The default conversions have already
936 been applied, and enumerated types converted to their compatible
940 common_type (tree t1
, tree t2
)
942 /* If one type is nonsense, use the other */
943 if (t1
== error_mark_node
)
945 if (t2
== error_mark_node
)
948 return cp_common_type (t1
, t2
);
951 /* Return the common type of two pointer types T1 and T2. This is the
952 type for the result of most arithmetic operations if the operands
953 have the given two types.
955 We assume that comp_target_types has already been done and returned
956 nonzero; if that isn't so, this may crash. */
959 common_pointer_type (tree t1
, tree t2
)
961 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
962 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
963 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
965 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
966 CPO_CONVERSION
, tf_warning_or_error
);
969 /* Compare two exception specifier types for exactness or subsetness, if
970 allowed. Returns false for mismatch, true for match (same, or
973 [except.spec] "If a class X ... objects of class X or any class publicly
974 and unambiguously derived from X. Similarly, if a pointer type Y * ...
975 exceptions of type Y * or that are pointers to any type publicly and
976 unambiguously derived from Y. Otherwise a function only allows exceptions
977 that have the same type ..."
978 This does not mention cv qualifiers and is different to what throw
979 [except.throw] and catch [except.catch] will do. They will ignore the
980 top level cv qualifiers, and allow qualifiers in the pointer to class
983 We implement the letter of the standard. */
986 comp_except_types (tree a
, tree b
, bool exact
)
988 if (same_type_p (a
, b
))
992 if (cp_type_quals (a
) || cp_type_quals (b
))
995 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
999 if (cp_type_quals (a
) || cp_type_quals (b
))
1003 if (TREE_CODE (a
) != RECORD_TYPE
1004 || TREE_CODE (b
) != RECORD_TYPE
)
1007 if (publicly_uniquely_derived_p (a
, b
))
1013 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1014 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1015 If EXACT is ce_type, the C++17 type compatibility rules apply.
1016 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1017 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1018 are unordered, but we've already filtered out duplicates. Most lists will
1019 be in order, we should try to make use of that. */
1022 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1031 /* First handle noexcept. */
1032 if (exact
< ce_exact
)
1034 if (exact
== ce_type
1035 && (canonical_eh_spec (CONST_CAST_TREE (t1
))
1036 == canonical_eh_spec (CONST_CAST_TREE (t2
))))
1039 /* noexcept(false) is compatible with no exception-specification,
1040 and less strict than any spec. */
1041 if (t1
== noexcept_false_spec
)
1042 return t2
== NULL_TREE
|| exact
== ce_derived
;
1043 /* Even a derived noexcept(false) is compatible with no
1044 exception-specification. */
1045 if (t2
== noexcept_false_spec
)
1046 return t1
== NULL_TREE
;
1048 /* Otherwise, if we aren't looking for an exact match, noexcept is
1049 equivalent to throw(). */
1050 if (t1
== noexcept_true_spec
)
1051 t1
= empty_except_spec
;
1052 if (t2
== noexcept_true_spec
)
1053 t2
= empty_except_spec
;
1056 /* If any noexcept is left, it is only comparable to itself;
1057 either we're looking for an exact match or we're redeclaring a
1058 template with dependent noexcept. */
1059 if ((t1
&& TREE_PURPOSE (t1
))
1060 || (t2
&& TREE_PURPOSE (t2
)))
1062 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1064 if (t1
== NULL_TREE
) /* T1 is ... */
1065 return t2
== NULL_TREE
|| exact
== ce_derived
;
1066 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1067 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1068 if (t2
== NULL_TREE
) /* T2 is ... */
1070 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1071 return exact
== ce_derived
;
1073 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1074 Count how many we find, to determine exactness. For exact matching and
1075 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1077 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1079 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1081 tree a
= TREE_VALUE (probe
);
1082 tree b
= TREE_VALUE (t2
);
1084 if (comp_except_types (a
, b
, exact
))
1086 if (probe
== base
&& exact
> ce_derived
)
1087 base
= TREE_CHAIN (probe
);
1092 if (probe
== NULL_TREE
)
1095 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1098 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1099 [] can match [size]. */
1102 comp_array_types (const_tree t1
, const_tree t2
, bool allow_redeclaration
)
1111 /* The type of the array elements must be the same. */
1112 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1115 d1
= TYPE_DOMAIN (t1
);
1116 d2
= TYPE_DOMAIN (t2
);
1121 /* If one of the arrays is dimensionless, and the other has a
1122 dimension, they are of different types. However, it is valid to
1130 declarations for an array object can specify
1131 array types that differ by the presence or absence of a major
1132 array bound (_dcl.array_). */
1134 return allow_redeclaration
;
1136 /* Check that the dimensions are the same. */
1138 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1140 max1
= TYPE_MAX_VALUE (d1
);
1141 max2
= TYPE_MAX_VALUE (d2
);
1143 if (!cp_tree_equal (max1
, max2
))
1149 /* Compare the relative position of T1 and T2 into their respective
1150 template parameter list.
1151 T1 and T2 must be template parameter types.
1152 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1155 comp_template_parms_position (tree t1
, tree t2
)
1157 tree index1
, index2
;
1158 gcc_assert (t1
&& t2
1159 && TREE_CODE (t1
) == TREE_CODE (t2
)
1160 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1161 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1162 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1164 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1165 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1167 /* Then compare their relative position. */
1168 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1169 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1170 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1171 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1174 /* In C++14 we can end up comparing 'auto' to a normal template
1175 parameter. Don't confuse them. */
1176 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1177 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1182 /* Subroutine in comptypes. */
1185 structural_comptypes (tree t1
, tree t2
, int strict
)
1190 /* Suppress errors caused by previously reported errors. */
1191 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1194 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1196 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1197 current instantiation. */
1198 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1199 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1201 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1202 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1204 if (TYPE_PTRMEMFUNC_P (t1
))
1205 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1206 if (TYPE_PTRMEMFUNC_P (t2
))
1207 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1209 /* Different classes of types can't be compatible. */
1210 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1213 /* Qualifiers must match. For array types, we will check when we
1214 recur on the array element types. */
1215 if (TREE_CODE (t1
) != ARRAY_TYPE
1216 && cp_type_quals (t1
) != cp_type_quals (t2
))
1218 if (TREE_CODE (t1
) == FUNCTION_TYPE
1219 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1221 /* Need to check this before TYPE_MAIN_VARIANT.
1222 FIXME function qualifiers should really change the main variant. */
1223 if (TREE_CODE (t1
) == FUNCTION_TYPE
1224 || TREE_CODE (t1
) == METHOD_TYPE
)
1226 if (type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1228 if (flag_noexcept_type
1229 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1230 TYPE_RAISES_EXCEPTIONS (t2
),
1235 /* Allow for two different type nodes which have essentially the same
1236 definition. Note that we already checked for equality of the type
1237 qualifiers (just above). */
1239 if (TREE_CODE (t1
) != ARRAY_TYPE
1240 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1244 /* Compare the types. Break out if they could be the same. */
1245 switch (TREE_CODE (t1
))
1249 /* All void and bool types are the same. */
1253 case FIXED_POINT_TYPE
:
1255 /* With these nodes, we can't determine type equivalence by
1256 looking at what is stored in the nodes themselves, because
1257 two nodes might have different TYPE_MAIN_VARIANTs but still
1258 represent the same type. For example, wchar_t and int could
1259 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1260 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1261 and are distinct types. On the other hand, int and the
1264 typedef int INT __attribute((may_alias));
1266 have identical properties, different TYPE_MAIN_VARIANTs, but
1267 represent the same type. The canonical type system keeps
1268 track of equivalence in this case, so we fall back on it. */
1269 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1271 case TEMPLATE_TEMPLATE_PARM
:
1272 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1273 if (!comp_template_parms_position (t1
, t2
))
1275 if (!comp_template_parms
1276 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1277 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1279 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1281 /* Don't check inheritance. */
1282 strict
= COMPARE_STRICT
;
1287 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1288 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1289 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1290 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1293 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1295 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1301 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1302 strict
& ~COMPARE_REDECLARATION
))
1304 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1308 case REFERENCE_TYPE
:
1309 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1311 /* fall through to checks for pointer types */
1315 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1316 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1322 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1324 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1329 /* Target types must match incl. qualifiers. */
1330 if (!comp_array_types (t1
, t2
, !!(strict
& COMPARE_REDECLARATION
)))
1334 case TEMPLATE_TYPE_PARM
:
1335 /* If T1 and T2 don't have the same relative position in their
1336 template parameters set, they can't be equal. */
1337 if (!comp_template_parms_position (t1
, t2
))
1339 /* Constrained 'auto's are distinct from parms that don't have the same
1341 if (!equivalent_placeholder_constraints (t1
, t2
))
1346 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1347 TYPENAME_TYPE_FULLNAME (t2
)))
1349 /* Qualifiers don't matter on scopes. */
1350 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1355 case UNBOUND_CLASS_TEMPLATE
:
1356 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1358 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1363 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1368 if (TYPE_VECTOR_SUBPARTS (t1
) != TYPE_VECTOR_SUBPARTS (t2
)
1369 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1373 case TYPE_PACK_EXPANSION
:
1374 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1375 PACK_EXPANSION_PATTERN (t2
))
1376 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1377 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1380 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1381 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1382 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1383 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1384 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1385 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1386 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1387 DECLTYPE_TYPE_EXPR (t2
)))
1391 case UNDERLYING_TYPE
:
1392 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1393 UNDERLYING_TYPE_TYPE (t2
));
1399 /* If we get here, we know that from a target independent POV the
1400 types are the same. Make sure the target attributes are also
1402 return comp_type_attributes (t1
, t2
);
1405 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1406 is a bitwise-or of the COMPARE_* flags. */
1409 comptypes (tree t1
, tree t2
, int strict
)
1411 if (strict
== COMPARE_STRICT
)
1416 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1419 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1420 /* At least one of the types requires structural equality, so
1421 perform a deep check. */
1422 return structural_comptypes (t1
, t2
, strict
);
1424 if (flag_checking
&& USE_CANONICAL_TYPES
)
1426 bool result
= structural_comptypes (t1
, t2
, strict
);
1428 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1429 /* The two types are structurally equivalent, but their
1430 canonical types were different. This is a failure of the
1431 canonical type propagation code.*/
1433 ("canonical types differ for identical types %T and %T",
1435 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1436 /* Two types are structurally different, but the canonical
1437 types are the same. This means we were over-eager in
1438 assigning canonical types. */
1440 ("same canonical type node for different types %T and %T",
1445 if (!flag_checking
&& USE_CANONICAL_TYPES
)
1446 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1448 return structural_comptypes (t1
, t2
, strict
);
1450 else if (strict
== COMPARE_STRUCTURAL
)
1451 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1453 return structural_comptypes (t1
, t2
, strict
);
1456 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1457 top-level qualifiers. */
1460 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1462 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1465 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1466 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1467 return same_type_p (type1
, type2
);
1470 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1473 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1475 int q1
= cp_type_quals (type1
);
1476 int q2
= cp_type_quals (type2
);
1478 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1479 return (q1
& q2
) == q2
;
1482 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1483 more cv-qualified that TYPE1, and 0 otherwise. */
1486 comp_cv_qualification (int q1
, int q2
)
1491 if ((q1
& q2
) == q2
)
1493 else if ((q1
& q2
) == q1
)
1500 comp_cv_qualification (const_tree type1
, const_tree type2
)
1502 int q1
= cp_type_quals (type1
);
1503 int q2
= cp_type_quals (type2
);
1504 return comp_cv_qualification (q1
, q2
);
1507 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1508 subset of the cv-qualification signature of TYPE2, and the types
1509 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1512 comp_cv_qual_signature (tree type1
, tree type2
)
1514 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1516 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1522 /* Subroutines of `comptypes'. */
1524 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1525 equivalent in the sense that functions with those parameter types
1526 can have equivalent types. The two lists must be equivalent,
1527 element by element. */
1530 compparms (const_tree parms1
, const_tree parms2
)
1534 /* An unspecified parmlist matches any specified parmlist
1535 whose argument types don't need default promotions. */
1537 for (t1
= parms1
, t2
= parms2
;
1539 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1541 /* If one parmlist is shorter than the other,
1542 they fail to match. */
1545 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1552 /* Process a sizeof or alignof expression where the operand is a
1556 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool complain
)
1561 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1562 if (type
== error_mark_node
)
1563 return error_mark_node
;
1565 type
= non_reference (type
);
1566 if (TREE_CODE (type
) == METHOD_TYPE
)
1569 pedwarn (input_location
, OPT_Wpointer_arith
,
1570 "invalid application of %qs to a member function",
1571 operator_name_info
[(int) op
].name
);
1573 return error_mark_node
;
1574 value
= size_one_node
;
1577 dependent_p
= dependent_type_p (type
);
1579 complete_type (type
);
1581 /* VLA types will have a non-constant size. In the body of an
1582 uninstantiated template, we don't need to try to compute the
1583 value, because the sizeof expression is not an integral
1584 constant expression in that case. And, if we do try to
1585 compute the value, we'll likely end up with SAVE_EXPRs, which
1586 the template substitution machinery does not expect to see. */
1587 || (processing_template_decl
1588 && COMPLETE_TYPE_P (type
)
1589 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1591 value
= build_min (op
, size_type_node
, type
);
1592 TREE_READONLY (value
) = 1;
1596 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1597 op
== SIZEOF_EXPR
, false,
1601 /* Return the size of the type, without producing any warnings for
1602 types whose size cannot be taken. This routine should be used only
1603 in some other routine that has already produced a diagnostic about
1604 using the size of such a type. */
1606 cxx_sizeof_nowarn (tree type
)
1608 if (TREE_CODE (type
) == FUNCTION_TYPE
1609 || VOID_TYPE_P (type
)
1610 || TREE_CODE (type
) == ERROR_MARK
)
1611 return size_one_node
;
1612 else if (!COMPLETE_TYPE_P (type
))
1613 return size_zero_node
;
1615 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false);
1618 /* Process a sizeof expression where the operand is an expression. */
1621 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1623 if (e
== error_mark_node
)
1624 return error_mark_node
;
1626 if (processing_template_decl
)
1628 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1629 TREE_SIDE_EFFECTS (e
) = 0;
1630 TREE_READONLY (e
) = 1;
1635 /* To get the size of a static data member declared as an array of
1636 unknown bound, we need to instantiate it. */
1638 && VAR_HAD_UNKNOWN_BOUND (e
)
1639 && DECL_TEMPLATE_INSTANTIATION (e
))
1640 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1642 if (TREE_CODE (e
) == PARM_DECL
1643 && DECL_ARRAY_PARAMETER_P (e
)
1644 && (complain
& tf_warning
))
1646 if (warning (OPT_Wsizeof_array_argument
, "%<sizeof%> on array function "
1647 "parameter %qE will return size of %qT", e
, TREE_TYPE (e
)))
1648 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1651 e
= mark_type_use (e
);
1655 if (complain
& tf_error
)
1656 error ("invalid application of %<sizeof%> to a bit-field");
1658 return error_mark_node
;
1661 else if (is_overloaded_fn (e
))
1663 if (complain
& tf_error
)
1664 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1667 return error_mark_node
;
1670 else if (type_unknown_p (e
))
1672 if (complain
& tf_error
)
1673 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1675 return error_mark_node
;
1681 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, complain
& tf_error
);
1684 /* Implement the __alignof keyword: Return the minimum required
1685 alignment of E, measured in bytes. For VAR_DECL's and
1686 FIELD_DECL's return DECL_ALIGN (which can be set from an
1687 "aligned" __attribute__ specification). */
1690 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1694 if (e
== error_mark_node
)
1695 return error_mark_node
;
1697 if (processing_template_decl
)
1699 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1700 TREE_SIDE_EFFECTS (e
) = 0;
1701 TREE_READONLY (e
) = 1;
1706 e
= mark_type_use (e
);
1709 t
= size_int (DECL_ALIGN_UNIT (e
));
1710 else if (bitfield_p (e
))
1712 if (complain
& tf_error
)
1713 error ("invalid application of %<__alignof%> to a bit-field");
1715 return error_mark_node
;
1718 else if (TREE_CODE (e
) == COMPONENT_REF
1719 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1720 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1721 else if (is_overloaded_fn (e
))
1723 if (complain
& tf_error
)
1724 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1727 return error_mark_node
;
1728 if (TREE_CODE (e
) == FUNCTION_DECL
)
1729 t
= size_int (DECL_ALIGN_UNIT (e
));
1733 else if (type_unknown_p (e
))
1735 if (complain
& tf_error
)
1736 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1738 return error_mark_node
;
1742 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
,
1743 complain
& tf_error
);
1745 return fold_convert (size_type_node
, t
);
1748 /* Process a sizeof or alignof expression E with code OP where the operand
1749 is an expression. */
1752 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1754 if (op
== SIZEOF_EXPR
)
1755 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1757 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1760 /* Build a representation of an expression 'alignas(E).' Return the
1761 folded integer value of E if it is an integral constant expression
1762 that resolves to a valid alignment. If E depends on a template
1763 parameter, return a syntactic representation tree of kind
1764 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1765 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1768 cxx_alignas_expr (tree e
)
1770 if (e
== NULL_TREE
|| e
== error_mark_node
1771 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1777 When the alignment-specifier is of the form
1778 alignas(type-id ), it shall have the same effect as
1779 alignas(alignof(type-id )). */
1781 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, false);
1783 /* If we reach this point, it means the alignas expression if of
1784 the form "alignas(assignment-expression)", so we should follow
1785 what is stated by [dcl.align]/2. */
1787 if (value_dependent_expression_p (e
))
1788 /* Leave value-dependent expression alone for now. */
1791 e
= instantiate_non_dependent_expr (e
);
1792 e
= mark_rvalue_use (e
);
1794 /* [dcl.align]/2 says:
1796 the assignment-expression shall be an integral constant
1799 return cxx_constant_value (e
);
1803 /* EXPR is being used in a context that is not a function call.
1808 The expression can be used only as the left-hand operand of a
1809 member function call.
1811 [expr.mptr.operator]
1813 If the result of .* or ->* is a function, then that result can be
1814 used only as the operand for the function call operator ().
1816 by issuing an error message if appropriate. Returns true iff EXPR
1817 violates these rules. */
1820 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1822 if (expr
== NULL_TREE
)
1824 /* Don't enforce this in MS mode. */
1825 if (flag_ms_extensions
)
1827 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1828 expr
= get_first_fn (expr
);
1829 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1831 if (complain
& tf_error
)
1835 error_at (loc
, "invalid use of non-static member function %qD",
1837 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
1840 error_at (loc
, "invalid use of non-static member function of "
1841 "type %qT", TREE_TYPE (expr
));
1848 /* If EXP is a reference to a bitfield, and the type of EXP does not
1849 match the declared type of the bitfield, return the declared type
1850 of the bitfield. Otherwise, return NULL_TREE. */
1853 is_bitfield_expr_with_lowered_type (const_tree exp
)
1855 switch (TREE_CODE (exp
))
1858 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1859 ? TREE_OPERAND (exp
, 1)
1860 : TREE_OPERAND (exp
, 0)))
1862 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1865 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1869 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1875 field
= TREE_OPERAND (exp
, 1);
1876 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1878 if (same_type_ignoring_top_level_qualifiers_p
1879 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1881 return DECL_BIT_FIELD_TYPE (field
);
1885 if (DECL_HAS_VALUE_EXPR_P (exp
))
1886 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1887 (CONST_CAST_TREE (exp
)));
1891 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1892 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1893 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1901 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1902 bitfield with a lowered type, the type of EXP is returned, rather
1906 unlowered_expr_type (const_tree exp
)
1909 tree etype
= TREE_TYPE (exp
);
1911 type
= is_bitfield_expr_with_lowered_type (exp
);
1913 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1920 /* Perform the conversions in [expr] that apply when an lvalue appears
1921 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1922 function-to-pointer conversions. In addition, bitfield references are
1923 converted to their declared types. Note that this function does not perform
1924 the lvalue-to-rvalue conversion for class types. If you need that conversion
1925 for class types, then you probably need to use force_rvalue.
1927 Although the returned value is being used as an rvalue, this
1928 function does not wrap the returned expression in a
1929 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1930 that the return value is no longer an lvalue. */
1933 decay_conversion (tree exp
,
1934 tsubst_flags_t complain
,
1935 bool reject_builtin
/* = true */)
1938 enum tree_code code
;
1939 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
1941 type
= TREE_TYPE (exp
);
1942 if (type
== error_mark_node
)
1943 return error_mark_node
;
1945 exp
= resolve_nondeduced_context (exp
, complain
);
1946 if (type_unknown_p (exp
))
1948 if (complain
& tf_error
)
1949 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
1950 return error_mark_node
;
1953 code
= TREE_CODE (type
);
1955 if (error_operand_p (exp
))
1956 return error_mark_node
;
1958 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
1959 return nullptr_node
;
1961 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1962 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1963 if (code
== VOID_TYPE
)
1965 if (complain
& tf_error
)
1966 error_at (loc
, "void value not ignored as it ought to be");
1967 return error_mark_node
;
1969 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
1970 return error_mark_node
;
1971 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1973 exp
= mark_lvalue_use (exp
);
1974 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
1975 return error_mark_node
;
1976 return cp_build_addr_expr (exp
, complain
);
1978 if (code
== ARRAY_TYPE
)
1983 exp
= mark_lvalue_use (exp
);
1985 if (INDIRECT_REF_P (exp
))
1986 return build_nop (build_pointer_type (TREE_TYPE (type
)),
1987 TREE_OPERAND (exp
, 0));
1989 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1991 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
1992 if (op1
== error_mark_node
)
1993 return error_mark_node
;
1994 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1995 TREE_OPERAND (exp
, 0), op1
);
1998 if (!obvalue_p (exp
)
1999 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2001 if (complain
& tf_error
)
2002 error_at (loc
, "invalid use of non-lvalue array");
2003 return error_mark_node
;
2006 /* Don't let an array compound literal decay to a pointer. It can
2007 still be used to initialize an array or bind to a reference. */
2008 if (TREE_CODE (exp
) == TARGET_EXPR
)
2010 if (complain
& tf_error
)
2011 error_at (loc
, "taking address of temporary array");
2012 return error_mark_node
;
2015 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2019 if (!cxx_mark_addressable (exp
))
2020 return error_mark_node
;
2021 adr
= build_nop (ptrtype
, build_address (exp
));
2024 /* This way is better for a COMPONENT_REF since it can
2025 simplify the offset for a component. */
2026 adr
= cp_build_addr_expr (exp
, complain
);
2027 return cp_convert (ptrtype
, adr
, complain
);
2030 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2031 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2033 /* If a bitfield is used in a context where integral promotion
2034 applies, then the caller is expected to have used
2035 default_conversion. That function promotes bitfields correctly
2036 before calling this function. At this point, if we have a
2037 bitfield referenced, we may assume that is not subject to
2038 promotion, and that, therefore, the type of the resulting rvalue
2039 is the declared type of the bitfield. */
2040 exp
= convert_bitfield_to_declared_type (exp
);
2042 /* We do not call rvalue() here because we do not want to wrap EXP
2043 in a NON_LVALUE_EXPR. */
2047 Non-class rvalues always have cv-unqualified types. */
2048 type
= TREE_TYPE (exp
);
2049 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2050 exp
= build_nop (cv_unqualified (type
), exp
);
2052 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2053 return error_mark_node
;
2058 /* Perform preparatory conversions, as part of the "usual arithmetic
2059 conversions". In particular, as per [expr]:
2061 Whenever an lvalue expression appears as an operand of an
2062 operator that expects the rvalue for that operand, the
2063 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2064 standard conversions are applied to convert the expression to an
2067 In addition, we perform integral promotions here, as those are
2068 applied to both operands to a binary operator before determining
2069 what additional conversions should apply. */
2072 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2074 /* Check for target-specific promotions. */
2075 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2077 exp
= cp_convert (promoted_type
, exp
, complain
);
2078 /* Perform the integral promotions first so that bitfield
2079 expressions (which may promote to "int", even if the bitfield is
2080 declared "unsigned") are promoted correctly. */
2081 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2082 exp
= cp_perform_integral_promotions (exp
, complain
);
2083 /* Perform the other conversions. */
2084 exp
= decay_conversion (exp
, complain
);
2092 default_conversion (tree exp
)
2094 return cp_default_conversion (exp
, tf_warning_or_error
);
2097 /* EXPR is an expression with an integral or enumeration type.
2098 Perform the integral promotions in [conv.prom], and return the
2102 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2107 expr
= mark_rvalue_use (expr
);
2111 If the bitfield has an enumerated type, it is treated as any
2112 other value of that type for promotion purposes. */
2113 type
= is_bitfield_expr_with_lowered_type (expr
);
2114 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2115 type
= TREE_TYPE (expr
);
2116 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2117 /* Scoped enums don't promote. */
2118 if (SCOPED_ENUM_P (type
))
2120 promoted_type
= type_promotes_to (type
);
2121 if (type
!= promoted_type
)
2122 expr
= cp_convert (promoted_type
, expr
, complain
);
2129 perform_integral_promotions (tree expr
)
2131 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2134 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2135 decay_conversion to one. */
2138 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2142 if (!TYPE_PTR_P (totype
))
2145 t
= TREE_TYPE (totype
);
2146 if (!same_type_p (t
, char_type_node
)
2147 && !same_type_p (t
, char16_type_node
)
2148 && !same_type_p (t
, char32_type_node
)
2149 && !same_type_p (t
, wchar_type_node
))
2152 if (TREE_CODE (exp
) == STRING_CST
)
2154 /* Make sure that we don't try to convert between char and wide chars. */
2155 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2160 /* Is this a string constant which has decayed to 'const char *'? */
2161 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2162 if (!same_type_p (TREE_TYPE (exp
), t
))
2165 if (TREE_CODE (exp
) != ADDR_EXPR
2166 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2171 if (cxx_dialect
>= cxx11
)
2172 pedwarn (input_location
,
2173 pedantic
? OPT_Wpedantic
: OPT_Wwrite_strings
,
2174 "ISO C++ forbids converting a string constant to %qT",
2177 warning (OPT_Wwrite_strings
,
2178 "deprecated conversion from string constant to %qT",
2185 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2186 can, for example, use as an lvalue. This code used to be in
2187 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2188 expressions, where we're dealing with aggregates. But now it's again only
2189 called from unary_complex_lvalue. The case (in particular) that led to
2190 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2194 rationalize_conditional_expr (enum tree_code code
, tree t
,
2195 tsubst_flags_t complain
)
2197 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
2199 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2200 the first operand is always the one to be used if both operands
2201 are equal, so we know what conditional expression this used to be. */
2202 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2204 tree op0
= TREE_OPERAND (t
, 0);
2205 tree op1
= TREE_OPERAND (t
, 1);
2207 /* The following code is incorrect if either operand side-effects. */
2208 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2209 && !TREE_SIDE_EFFECTS (op1
));
2211 build_conditional_expr (loc
,
2212 build_x_binary_op (loc
,
2213 (TREE_CODE (t
) == MIN_EXPR
2214 ? LE_EXPR
: GE_EXPR
),
2215 op0
, TREE_CODE (op0
),
2216 op1
, TREE_CODE (op1
),
2219 cp_build_unary_op (code
, op0
, false, complain
),
2220 cp_build_unary_op (code
, op1
, false, complain
),
2225 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2226 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), false,
2228 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), false,
2233 /* Given the TYPE of an anonymous union field inside T, return the
2234 FIELD_DECL for the field. If not found return NULL_TREE. Because
2235 anonymous unions can nest, we must also search all anonymous unions
2236 that are directly reachable. */
2239 lookup_anon_field (tree t
, tree type
)
2243 t
= TYPE_MAIN_VARIANT (t
);
2245 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2247 if (TREE_STATIC (field
))
2249 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2252 /* If we find it directly, return the field. */
2253 if (DECL_NAME (field
) == NULL_TREE
2254 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2259 /* Otherwise, it could be nested, search harder. */
2260 if (DECL_NAME (field
) == NULL_TREE
2261 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2263 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2271 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2272 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2273 non-NULL, it indicates the path to the base used to name MEMBER.
2274 If PRESERVE_REFERENCE is true, the expression returned will have
2275 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2276 returned will have the type referred to by the reference.
2278 This function does not perform access control; that is either done
2279 earlier by the parser when the name of MEMBER is resolved to MEMBER
2280 itself, or later when overload resolution selects one of the
2281 functions indicated by MEMBER. */
2284 build_class_member_access_expr (cp_expr object
, tree member
,
2285 tree access_path
, bool preserve_reference
,
2286 tsubst_flags_t complain
)
2290 tree result
= NULL_TREE
;
2291 tree using_decl
= NULL_TREE
;
2293 if (error_operand_p (object
) || error_operand_p (member
))
2294 return error_mark_node
;
2296 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2300 The type of the first expression shall be "class object" (of a
2302 object_type
= TREE_TYPE (object
);
2303 if (!currently_open_class (object_type
)
2304 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2305 return error_mark_node
;
2306 if (!CLASS_TYPE_P (object_type
))
2308 if (complain
& tf_error
)
2310 if (POINTER_TYPE_P (object_type
)
2311 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2312 error ("request for member %qD in %qE, which is of pointer "
2313 "type %qT (maybe you meant to use %<->%> ?)",
2314 member
, object
.get_value (), object_type
);
2316 error ("request for member %qD in %qE, which is of non-class "
2317 "type %qT", member
, object
.get_value (), object_type
);
2319 return error_mark_node
;
2322 /* The standard does not seem to actually say that MEMBER must be a
2323 member of OBJECT_TYPE. However, that is clearly what is
2325 if (DECL_P (member
))
2327 member_scope
= DECL_CLASS_CONTEXT (member
);
2328 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2329 return error_mark_node
;
2330 if (TREE_DEPRECATED (member
))
2331 warn_deprecated_use (member
, NULL_TREE
);
2334 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2335 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2336 presently be the anonymous union. Go outwards until we find a
2337 type related to OBJECT_TYPE. */
2338 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2339 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2341 member_scope
= TYPE_CONTEXT (member_scope
);
2342 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2344 if (complain
& tf_error
)
2346 if (TREE_CODE (member
) == FIELD_DECL
)
2347 error ("invalid use of nonstatic data member %qE", member
);
2349 error ("%qD is not a member of %qT", member
, object_type
);
2351 return error_mark_node
;
2354 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2355 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2356 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2358 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2360 object
= cp_build_indirect_ref (temp
, RO_NULL
, complain
);
2363 /* In [expr.ref], there is an explicit list of the valid choices for
2364 MEMBER. We check for each of those cases here. */
2367 /* A static data member. */
2369 mark_exp_read (object
);
2370 /* If OBJECT has side-effects, they are supposed to occur. */
2371 if (TREE_SIDE_EFFECTS (object
))
2372 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2374 else if (TREE_CODE (member
) == FIELD_DECL
)
2376 /* A non-static data member. */
2381 if (INDIRECT_REF_P (object
))
2383 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2385 null_object_p
= false;
2387 /* Convert OBJECT to the type of MEMBER. */
2388 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2389 TYPE_MAIN_VARIANT (member_scope
)))
2394 binfo
= lookup_base (access_path
? access_path
: object_type
,
2395 member_scope
, ba_unique
, &kind
, complain
);
2396 if (binfo
== error_mark_node
)
2397 return error_mark_node
;
2399 /* It is invalid to try to get to a virtual base of a
2400 NULL object. The most common cause is invalid use of
2402 if (null_object_p
&& kind
== bk_via_virtual
)
2404 if (complain
& tf_error
)
2406 error ("invalid access to non-static data member %qD in "
2407 "virtual base of NULL object", member
);
2409 return error_mark_node
;
2412 /* Convert to the base. */
2413 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2414 /*nonnull=*/1, complain
);
2415 /* If we found the base successfully then we should be able
2416 to convert to it successfully. */
2417 gcc_assert (object
!= error_mark_node
);
2420 /* If MEMBER is from an anonymous aggregate, we have converted
2421 OBJECT so that it refers to the class containing the
2422 anonymous union. Generate a reference to the anonymous union
2423 itself, and recur to find MEMBER. */
2424 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2425 /* When this code is called from build_field_call, the
2426 object already has the type of the anonymous union.
2427 That is because the COMPONENT_REF was already
2428 constructed, and was then disassembled before calling
2429 build_field_call. After the function-call code is
2430 cleaned up, this waste can be eliminated. */
2431 && (!same_type_ignoring_top_level_qualifiers_p
2432 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2434 tree anonymous_union
;
2436 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2437 DECL_CONTEXT (member
));
2438 object
= build_class_member_access_expr (object
,
2440 /*access_path=*/NULL_TREE
,
2445 /* Compute the type of the field, as described in [expr.ref]. */
2446 type_quals
= TYPE_UNQUALIFIED
;
2447 member_type
= TREE_TYPE (member
);
2448 if (TREE_CODE (member_type
) != REFERENCE_TYPE
)
2450 type_quals
= (cp_type_quals (member_type
)
2451 | cp_type_quals (object_type
));
2453 /* A field is const (volatile) if the enclosing object, or the
2454 field itself, is const (volatile). But, a mutable field is
2455 not const, even within a const object. */
2456 if (DECL_MUTABLE_P (member
))
2457 type_quals
&= ~TYPE_QUAL_CONST
;
2458 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2461 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2462 object
, member
, NULL_TREE
);
2464 /* Mark the expression const or volatile, as appropriate. Even
2465 though we've dealt with the type above, we still have to mark the
2466 expression itself. */
2467 if (type_quals
& TYPE_QUAL_CONST
)
2468 TREE_READONLY (result
) = 1;
2469 if (type_quals
& TYPE_QUAL_VOLATILE
)
2470 TREE_THIS_VOLATILE (result
) = 1;
2472 else if (BASELINK_P (member
))
2474 /* The member is a (possibly overloaded) member function. */
2478 /* If the MEMBER is exactly one static member function, then we
2479 know the type of the expression. Otherwise, we must wait
2480 until overload resolution has been performed. */
2481 functions
= BASELINK_FUNCTIONS (member
);
2482 if (TREE_CODE (functions
) == FUNCTION_DECL
2483 && DECL_STATIC_FUNCTION_P (functions
))
2484 type
= TREE_TYPE (functions
);
2486 type
= unknown_type_node
;
2487 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2488 base. That will happen when the function is called. */
2489 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2491 else if (TREE_CODE (member
) == CONST_DECL
)
2493 /* The member is an enumerator. */
2495 /* If OBJECT has side-effects, they are supposed to occur. */
2496 if (TREE_SIDE_EFFECTS (object
))
2497 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2500 else if ((using_decl
= strip_using_decl (member
)) != member
)
2501 result
= build_class_member_access_expr (object
,
2503 access_path
, preserve_reference
,
2507 if (complain
& tf_error
)
2508 error ("invalid use of %qD", member
);
2509 return error_mark_node
;
2512 if (!preserve_reference
)
2515 If E2 is declared to have type "reference to T", then ... the
2516 type of E1.E2 is T. */
2517 result
= convert_from_reference (result
);
2522 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2523 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2526 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2527 tsubst_flags_t complain
)
2529 tree object_type
= TREE_TYPE (object
);
2530 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2533 /* We've already complained about this destructor. */
2534 if (dtor_type
== error_mark_node
)
2535 return error_mark_node
;
2537 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2539 if (complain
& tf_error
)
2540 error ("qualified type %qT does not match destructor name ~%qT",
2542 return error_mark_node
;
2544 if (is_auto (dtor_type
))
2545 dtor_type
= object_type
;
2546 else if (identifier_p (dtor_type
))
2548 /* In a template, names we can't find a match for are still accepted
2549 destructor names, and we check them here. */
2550 if (check_dtor_name (object_type
, dtor_type
))
2551 dtor_type
= object_type
;
2554 if (complain
& tf_error
)
2555 error ("object type %qT does not match destructor name ~%qT",
2556 object_type
, dtor_type
);
2557 return error_mark_node
;
2561 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2563 if (complain
& tf_error
)
2564 error ("the type being destroyed is %qT, but the destructor "
2565 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2566 return error_mark_node
;
2568 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2569 /*protect=*/1, /*want_type=*/false,
2570 tf_warning_or_error
);
2573 if (complain
& tf_error
)
2574 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2575 return error_mark_node
;
2577 expr
= (adjust_result_of_qualified_name_lookup
2578 (expr
, dtor_type
, object_type
));
2579 if (scope
== NULL_TREE
)
2580 /* We need to call adjust_result_of_qualified_name_lookup in case the
2581 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2582 that we still get virtual function binding. */
2583 BASELINK_QUALIFIED_P (expr
) = false;
2587 /* An expression of the form "A::template B" has been resolved to
2588 DECL. Issue a diagnostic if B is not a template or template
2592 check_template_keyword (tree decl
)
2594 /* The standard says:
2598 If a name prefixed by the keyword template is not a member
2599 template, the program is ill-formed.
2601 DR 228 removed the restriction that the template be a member
2604 DR 96, if accepted would add the further restriction that explicit
2605 template arguments must be provided if the template keyword is
2606 used, but, as of 2005-10-16, that DR is still in "drafting". If
2607 this DR is accepted, then the semantic checks here can be
2608 simplified, as the entity named must in fact be a template
2609 specialization, rather than, as at present, a set of overloaded
2610 functions containing at least one template function. */
2611 if (TREE_CODE (decl
) != TEMPLATE_DECL
2612 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2616 if (DECL_USE_TEMPLATE (decl
)
2617 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2620 permerror (input_location
, "%qD is not a template", decl
);
2622 else if (!is_overloaded_fn (decl
))
2623 permerror (input_location
, "%qD is not a template", decl
);
2628 if (BASELINK_P (fns
))
2629 fns
= BASELINK_FUNCTIONS (fns
);
2632 tree fn
= OVL_CURRENT (fns
);
2633 if (TREE_CODE (fn
) == TEMPLATE_DECL
2634 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2636 if (TREE_CODE (fn
) == FUNCTION_DECL
2637 && DECL_USE_TEMPLATE (fn
)
2638 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
2640 fns
= OVL_NEXT (fns
);
2643 permerror (input_location
, "%qD is not a template", decl
);
2648 /* This function is called by the parser to process a class member
2649 access expression of the form OBJECT.NAME. NAME is a node used by
2650 the parser to represent a name; it is not yet a DECL. It may,
2651 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2652 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2653 there is no reason to do the lookup twice, so the parser keeps the
2654 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2655 be a template via the use of the "A::template B" syntax. */
2658 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
2659 tsubst_flags_t complain
)
2664 tree access_path
= NULL_TREE
;
2665 tree orig_object
= object
;
2666 tree orig_name
= name
;
2668 if (object
== error_mark_node
|| name
== error_mark_node
)
2669 return error_mark_node
;
2671 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2672 if (!objc_is_public (object
, name
))
2673 return error_mark_node
;
2675 object_type
= TREE_TYPE (object
);
2677 if (processing_template_decl
)
2679 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2680 type_dependent_object_expression_p (object
)
2681 /* If NAME is "f<args>", where either 'f' or 'args' is
2682 dependent, then the expression is dependent. */
2683 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2684 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2685 TREE_OPERAND (name
, 1)))
2686 /* If NAME is "T::X" where "T" is dependent, then the
2687 expression is dependent. */
2688 || (TREE_CODE (name
) == SCOPE_REF
2689 && TYPE_P (TREE_OPERAND (name
, 0))
2690 && dependent_scope_p (TREE_OPERAND (name
, 0))))
2693 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2694 orig_object
, orig_name
, NULL_TREE
);
2696 object
= build_non_dependent_expr (object
);
2698 else if (c_dialect_objc ()
2699 && identifier_p (name
)
2700 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2705 The type of the first expression shall be "class object" (of a
2707 if (!currently_open_class (object_type
)
2708 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2709 return error_mark_node
;
2710 if (!CLASS_TYPE_P (object_type
))
2712 if (complain
& tf_error
)
2714 if (POINTER_TYPE_P (object_type
)
2715 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2716 error ("request for member %qD in %qE, which is of pointer "
2717 "type %qT (maybe you meant to use %<->%> ?)",
2718 name
, object
.get_value (), object_type
);
2720 error ("request for member %qD in %qE, which is of non-class "
2721 "type %qT", name
, object
.get_value (), object_type
);
2723 return error_mark_node
;
2726 if (BASELINK_P (name
))
2727 /* A member function that has already been looked up. */
2731 bool is_template_id
= false;
2732 tree template_args
= NULL_TREE
;
2735 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2737 is_template_id
= true;
2738 template_args
= TREE_OPERAND (name
, 1);
2739 name
= TREE_OPERAND (name
, 0);
2741 if (TREE_CODE (name
) == OVERLOAD
)
2742 name
= DECL_NAME (get_first_fn (name
));
2743 else if (DECL_P (name
))
2744 name
= DECL_NAME (name
);
2747 if (TREE_CODE (name
) == SCOPE_REF
)
2749 /* A qualified name. The qualifying class or namespace `S'
2750 has already been looked up; it is either a TYPE or a
2752 scope
= TREE_OPERAND (name
, 0);
2753 name
= TREE_OPERAND (name
, 1);
2755 /* If SCOPE is a namespace, then the qualified name does not
2756 name a member of OBJECT_TYPE. */
2757 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2759 if (complain
& tf_error
)
2760 error ("%<%D::%D%> is not a member of %qT",
2761 scope
, name
, object_type
);
2762 return error_mark_node
;
2765 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2767 /* Looking up a member enumerator (c++/56793). */
2768 if (!TYPE_CLASS_SCOPE_P (scope
)
2769 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2771 if (complain
& tf_error
)
2772 error ("%<%D::%D%> is not a member of %qT",
2773 scope
, name
, object_type
);
2774 return error_mark_node
;
2776 tree val
= lookup_enumerator (scope
, name
);
2779 if (complain
& tf_error
)
2780 error ("%qD is not a member of %qD",
2782 return error_mark_node
;
2785 if (TREE_SIDE_EFFECTS (object
))
2786 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
2790 gcc_assert (CLASS_TYPE_P (scope
));
2791 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
2793 if (constructor_name_p (name
, scope
))
2795 if (complain
& tf_error
)
2796 error ("cannot call constructor %<%T::%D%> directly",
2798 return error_mark_node
;
2801 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2802 access_path
= lookup_base (object_type
, scope
, ba_check
,
2804 if (access_path
== error_mark_node
)
2805 return error_mark_node
;
2808 if (any_dependent_bases_p (object_type
))
2810 if (complain
& tf_error
)
2811 error ("%qT is not a base of %qT", scope
, object_type
);
2812 return error_mark_node
;
2818 access_path
= object_type
;
2821 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2823 if (dependent_type_p (object_type
))
2824 /* The destructor isn't declared yet. */
2826 member
= lookup_destructor (object
, scope
, name
, complain
);
2830 /* Look up the member. */
2831 member
= lookup_member (access_path
, name
, /*protect=*/1,
2832 /*want_type=*/false, complain
);
2833 if (member
== NULL_TREE
)
2835 if (dependent_type_p (object_type
))
2836 /* Try again at instantiation time. */
2838 if (complain
& tf_error
)
2840 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
2841 /*want_type=*/false);
2844 location_t bogus_component_loc
= input_location
;
2845 gcc_rich_location
rich_loc (bogus_component_loc
);
2846 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
2850 "%q#T has no member named %qE; did you mean %qE?",
2851 TREE_CODE (access_path
) == TREE_BINFO
2852 ? TREE_TYPE (access_path
) : object_type
, name
,
2856 error ("%q#T has no member named %qE",
2857 TREE_CODE (access_path
) == TREE_BINFO
2858 ? TREE_TYPE (access_path
) : object_type
, name
);
2860 return error_mark_node
;
2862 if (member
== error_mark_node
)
2863 return error_mark_node
;
2865 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
2866 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2867 wrong, so don't use it. */
2869 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
2875 tree templ
= member
;
2877 if (BASELINK_P (templ
))
2878 templ
= lookup_template_function (templ
, template_args
);
2881 if (complain
& tf_error
)
2882 error ("%qD is not a member template function", name
);
2883 return error_mark_node
;
2888 if (TREE_DEPRECATED (member
))
2889 warn_deprecated_use (member
, NULL_TREE
);
2892 check_template_keyword (member
);
2894 expr
= build_class_member_access_expr (object
, member
, access_path
,
2895 /*preserve_reference=*/false,
2897 if (processing_template_decl
&& expr
!= error_mark_node
)
2899 if (BASELINK_P (member
))
2901 if (TREE_CODE (orig_name
) == SCOPE_REF
)
2902 BASELINK_QUALIFIED_P (member
) = 1;
2905 return build_min_non_dep (COMPONENT_REF
, expr
,
2906 orig_object
, orig_name
,
2913 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2917 build_simple_component_ref (tree object
, tree member
)
2919 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
2920 cp_type_quals (TREE_TYPE (object
)));
2921 return build3_loc (input_location
,
2922 COMPONENT_REF
, type
,
2923 object
, member
, NULL_TREE
);
2926 /* Return an expression for the MEMBER_NAME field in the internal
2927 representation of PTRMEM, a pointer-to-member function. (Each
2928 pointer-to-member function type gets its own RECORD_TYPE so it is
2929 more convenient to access the fields by name than by FIELD_DECL.)
2930 This routine converts the NAME to a FIELD_DECL and then creates the
2931 node for the complete expression. */
2934 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
2939 /* This code is a stripped down version of
2940 build_class_member_access_expr. It does not work to use that
2941 routine directly because it expects the object to be of class
2943 ptrmem_type
= TREE_TYPE (ptrmem
);
2944 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
2945 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
2946 member
= DECL_CHAIN (member
))
2947 if (DECL_NAME (member
) == member_name
)
2949 return build_simple_component_ref (ptrmem
, member
);
2952 /* Given an expression PTR for a pointer, return an expression
2953 for the value pointed to.
2954 ERRORSTRING is the name of the operator to appear in error messages.
2956 This function may need to overload OPERATOR_FNNAME.
2957 Must also handle REFERENCE_TYPEs for C++. */
2960 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
2961 tsubst_flags_t complain
)
2963 tree orig_expr
= expr
;
2965 tree overload
= NULL_TREE
;
2967 if (processing_template_decl
)
2969 /* Retain the type if we know the operand is a pointer. */
2970 if (TREE_TYPE (expr
) && POINTER_TYPE_P (TREE_TYPE (expr
)))
2971 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
2972 if (type_dependent_expression_p (expr
))
2973 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
2974 expr
= build_non_dependent_expr (expr
);
2977 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
2978 NULL_TREE
, NULL_TREE
, &overload
, complain
);
2980 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
2982 if (processing_template_decl
&& rval
!= error_mark_node
)
2984 if (overload
!= NULL_TREE
)
2985 return (build_min_non_dep_op_overload
2986 (INDIRECT_REF
, rval
, overload
, orig_expr
));
2988 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
2994 /* Helper function called from c-common. */
2996 build_indirect_ref (location_t
/*loc*/,
2997 tree ptr
, ref_operator errorstring
)
2999 return cp_build_indirect_ref (ptr
, errorstring
, tf_warning_or_error
);
3003 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
3004 tsubst_flags_t complain
)
3008 if (ptr
== current_class_ptr
3009 || (TREE_CODE (ptr
) == NOP_EXPR
3010 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3011 && (same_type_ignoring_top_level_qualifiers_p
3012 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3013 return current_class_ref
;
3015 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
3016 ? ptr
: decay_conversion (ptr
, complain
));
3017 if (pointer
== error_mark_node
)
3018 return error_mark_node
;
3020 type
= TREE_TYPE (pointer
);
3022 if (POINTER_TYPE_P (type
))
3026 If the type of the expression is "pointer to T," the type
3027 of the result is "T." */
3028 tree t
= TREE_TYPE (type
);
3030 if ((CONVERT_EXPR_P (ptr
)
3031 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3032 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3034 /* If a warning is issued, mark it to avoid duplicates from
3035 the backend. This only needs to be done at
3036 warn_strict_aliasing > 2. */
3037 if (warn_strict_aliasing
> 2)
3038 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr
, 0)),
3039 type
, TREE_OPERAND (ptr
, 0)))
3040 TREE_NO_WARNING (ptr
) = 1;
3043 if (VOID_TYPE_P (t
))
3045 /* A pointer to incomplete type (other than cv void) can be
3046 dereferenced [expr.unary.op]/1 */
3047 if (complain
& tf_error
)
3048 error ("%qT is not a pointer-to-object type", type
);
3049 return error_mark_node
;
3051 else if (TREE_CODE (pointer
) == ADDR_EXPR
3052 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3053 /* The POINTER was something like `&x'. We simplify `*&x' to
3055 return TREE_OPERAND (pointer
, 0);
3058 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3060 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3061 so that we get the proper error message if the result is used
3062 to assign to. Also, &* is supposed to be a no-op. */
3063 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3064 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3065 TREE_SIDE_EFFECTS (ref
)
3066 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3070 else if (!(complain
& tf_error
))
3071 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3073 /* `pointer' won't be an error_mark_node if we were given a
3074 pointer to member, so it's cool to check for this here. */
3075 else if (TYPE_PTRMEM_P (type
))
3076 switch (errorstring
)
3078 case RO_ARRAY_INDEXING
:
3079 error ("invalid use of array indexing on pointer to member");
3082 error ("invalid use of unary %<*%> on pointer to member");
3084 case RO_IMPLICIT_CONVERSION
:
3085 error ("invalid use of implicit conversion on pointer to member");
3088 error ("left hand operand of %<->*%> must be a pointer to class, "
3089 "but is a pointer to member of type %qT", type
);
3094 else if (pointer
!= error_mark_node
)
3095 invalid_indirection_error (input_location
, type
, errorstring
);
3097 return error_mark_node
;
3100 /* This handles expressions of the form "a[i]", which denotes
3103 This is logically equivalent in C to *(a+i), but we may do it differently.
3104 If A is a variable or a member, we generate a primitive ARRAY_REF.
3105 This avoids forcing the array out of registers, and can work on
3106 arrays that are not lvalues (for example, members of structures returned
3109 If INDEX is of some user-defined type, it must be converted to
3110 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3111 will inherit the type of the array, which will be some pointer type.
3113 LOC is the location to use in building the array reference. */
3116 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3117 tsubst_flags_t complain
)
3123 if (complain
& tf_error
)
3124 error_at (loc
, "subscript missing in array reference");
3125 return error_mark_node
;
3128 /* If an array's index is an array notation, then its rank cannot be
3129 greater than one. */
3130 if (flag_cilkplus
&& contains_array_notation_expr (idx
))
3134 /* If find_rank returns false, then it should have reported an error,
3135 thus it is unnecessary for repetition. */
3136 if (!find_rank (loc
, idx
, idx
, true, &rank
))
3137 return error_mark_node
;
3140 error_at (loc
, "rank of the array%'s index is greater than 1");
3141 return error_mark_node
;
3144 if (TREE_TYPE (array
) == error_mark_node
3145 || TREE_TYPE (idx
) == error_mark_node
)
3146 return error_mark_node
;
3148 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3150 switch (TREE_CODE (array
))
3154 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3156 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3157 TREE_OPERAND (array
, 0), value
);
3158 SET_EXPR_LOCATION (ret
, loc
);
3163 ret
= build_conditional_expr
3164 (loc
, TREE_OPERAND (array
, 0),
3165 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3167 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3170 protected_set_expr_location (ret
, loc
);
3177 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3179 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3183 warn_array_subscript_with_type_char (loc
, idx
);
3185 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3187 if (complain
& tf_error
)
3188 error_at (loc
, "array subscript is not an integer");
3189 return error_mark_node
;
3192 /* Apply integral promotions *after* noticing character types.
3193 (It is unclear why we do these promotions -- the standard
3194 does not say that we should. In fact, the natural thing would
3195 seem to be to convert IDX to ptrdiff_t; we're performing
3196 pointer arithmetic.) */
3197 idx
= cp_perform_integral_promotions (idx
, complain
);
3199 /* An array that is indexed by a non-constant
3200 cannot be stored in a register; we must be able to do
3201 address arithmetic on its address.
3202 Likewise an array of elements of variable size. */
3203 if (TREE_CODE (idx
) != INTEGER_CST
3204 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3205 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3208 if (!cxx_mark_addressable (array
))
3209 return error_mark_node
;
3212 /* An array that is indexed by a constant value which is not within
3213 the array bounds cannot be stored in a register either; because we
3214 would get a crash in store_bit_field/extract_bit_field when trying
3215 to access a non-existent part of the register. */
3216 if (TREE_CODE (idx
) == INTEGER_CST
3217 && TYPE_DOMAIN (TREE_TYPE (array
))
3218 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3220 if (!cxx_mark_addressable (array
))
3221 return error_mark_node
;
3224 /* Note in C++ it is valid to subscript a `register' array, since
3225 it is valid to take the address of something with that
3226 storage specification. */
3230 while (TREE_CODE (foo
) == COMPONENT_REF
)
3231 foo
= TREE_OPERAND (foo
, 0);
3232 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3233 && (complain
& tf_warning
))
3234 warning_at (loc
, OPT_Wextra
,
3235 "subscripting array declared %<register%>");
3238 type
= TREE_TYPE (TREE_TYPE (array
));
3239 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3240 /* Array ref is const/volatile if the array elements are
3241 or if the array is.. */
3242 TREE_READONLY (rval
)
3243 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3244 TREE_SIDE_EFFECTS (rval
)
3245 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3246 TREE_THIS_VOLATILE (rval
)
3247 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3248 ret
= require_complete_type_sfinae (rval
, complain
);
3249 protected_set_expr_location (ret
, loc
);
3251 ret
= non_lvalue_loc (loc
, ret
);
3256 tree ar
= cp_default_conversion (array
, complain
);
3257 tree ind
= cp_default_conversion (idx
, complain
);
3259 /* Put the integer in IND to simplify error checking. */
3260 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3261 std::swap (ar
, ind
);
3263 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3264 return error_mark_node
;
3266 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3268 if (complain
& tf_error
)
3269 error_at (loc
, "subscripted value is neither array nor pointer");
3270 return error_mark_node
;
3272 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3274 if (complain
& tf_error
)
3275 error_at (loc
, "array subscript is not an integer");
3276 return error_mark_node
;
3279 warn_array_subscript_with_type_char (loc
, idx
);
3281 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3286 protected_set_expr_location (ret
, loc
);
3288 ret
= non_lvalue_loc (loc
, ret
);
3293 /* Entry point for Obj-C++. */
3296 build_array_ref (location_t loc
, tree array
, tree idx
)
3298 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3301 /* Resolve a pointer to member function. INSTANCE is the object
3302 instance to use, if the member points to a virtual member.
3304 This used to avoid checking for virtual functions if basetype
3305 has no virtual functions, according to an earlier ANSI draft.
3306 With the final ISO C++ rules, such an optimization is
3307 incorrect: A pointer to a derived member can be static_cast
3308 to pointer-to-base-member, as long as the dynamic object
3309 later has the right member. So now we only do this optimization
3310 when we know the dynamic type of the object. */
3313 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3314 tsubst_flags_t complain
)
3316 if (TREE_CODE (function
) == OFFSET_REF
)
3317 function
= TREE_OPERAND (function
, 1);
3319 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3321 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3323 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3324 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3326 tree instance_ptr
= *instance_ptrptr
;
3327 tree instance_save_expr
= 0;
3328 if (instance_ptr
== error_mark_node
)
3330 if (TREE_CODE (function
) == PTRMEM_CST
)
3332 /* Extracting the function address from a pmf is only
3333 allowed with -Wno-pmf-conversions. It only works for
3335 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3336 e1
= convert (fntype
, e1
);
3341 if (complain
& tf_error
)
3342 error ("object missing in use of %qE", function
);
3343 return error_mark_node
;
3347 /* True if we know that the dynamic type of the object doesn't have
3348 virtual functions, so we can assume the PFN field is a pointer. */
3349 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3350 && !TYPE_POLYMORPHIC_P (basetype
)
3351 && resolves_to_fixed_type_p (instance_ptr
, 0));
3353 /* If we don't really have an object (i.e. in an ill-formed
3354 conversion from PMF to pointer), we can't resolve virtual
3355 functions anyway. */
3356 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3359 if (TREE_SIDE_EFFECTS (instance_ptr
))
3360 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3362 if (TREE_SIDE_EFFECTS (function
))
3363 function
= save_expr (function
);
3365 /* Start by extracting all the information from the PMF itself. */
3366 e3
= pfn_from_ptrmemfunc (function
);
3367 delta
= delta_from_ptrmemfunc (function
);
3368 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3369 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3371 int flag_sanitize_save
;
3372 case ptrmemfunc_vbit_in_pfn
:
3373 e1
= cp_build_binary_op (input_location
,
3374 BIT_AND_EXPR
, idx
, integer_one_node
,
3376 idx
= cp_build_binary_op (input_location
,
3377 MINUS_EXPR
, idx
, integer_one_node
,
3379 if (idx
== error_mark_node
)
3380 return error_mark_node
;
3383 case ptrmemfunc_vbit_in_delta
:
3384 e1
= cp_build_binary_op (input_location
,
3385 BIT_AND_EXPR
, delta
, integer_one_node
,
3387 /* Don't instrument the RSHIFT_EXPR we're about to create because
3388 we're going to use DELTA number of times, and that wouldn't play
3389 well with SAVE_EXPRs therein. */
3390 flag_sanitize_save
= flag_sanitize
;
3392 delta
= cp_build_binary_op (input_location
,
3393 RSHIFT_EXPR
, delta
, integer_one_node
,
3395 flag_sanitize
= flag_sanitize_save
;
3396 if (delta
== error_mark_node
)
3397 return error_mark_node
;
3404 if (e1
== error_mark_node
)
3405 return error_mark_node
;
3407 /* Convert down to the right base before using the instance. A
3408 special case is that in a pointer to member of class C, C may
3409 be incomplete. In that case, the function will of course be
3410 a member of C, and no conversion is required. In fact,
3411 lookup_base will fail in that case, because incomplete
3412 classes do not have BINFOs. */
3413 if (!same_type_ignoring_top_level_qualifiers_p
3414 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3416 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3417 basetype
, ba_check
, NULL
, complain
);
3418 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3420 if (instance_ptr
== error_mark_node
)
3421 return error_mark_node
;
3423 /* ...and then the delta in the PMF. */
3424 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3426 /* Hand back the adjusted 'this' argument to our caller. */
3427 *instance_ptrptr
= instance_ptr
;
3430 /* Now just return the pointer. */
3433 /* Next extract the vtable pointer from the object. */
3434 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3436 vtbl
= cp_build_indirect_ref (vtbl
, RO_NULL
, complain
);
3437 if (vtbl
== error_mark_node
)
3438 return error_mark_node
;
3440 /* Finally, extract the function pointer from the vtable. */
3441 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3442 e2
= cp_build_indirect_ref (e2
, RO_NULL
, complain
);
3443 if (e2
== error_mark_node
)
3444 return error_mark_node
;
3445 TREE_CONSTANT (e2
) = 1;
3447 /* When using function descriptors, the address of the
3448 vtable entry is treated as a function pointer. */
3449 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3450 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3451 cp_build_addr_expr (e2
, complain
));
3453 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3454 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3455 if (e1
== error_mark_node
)
3456 return error_mark_node
;
3458 /* Make sure this doesn't get evaluated first inside one of the
3459 branches of the COND_EXPR. */
3460 if (instance_save_expr
)
3461 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3462 instance_save_expr
, e1
);
3469 /* Used by the C-common bits. */
3471 build_function_call (location_t
/*loc*/,
3472 tree function
, tree params
)
3474 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3477 /* Used by the C-common bits. */
3479 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3480 tree function
, vec
<tree
, va_gc
> *params
,
3481 vec
<tree
, va_gc
> * /*origtypes*/)
3483 vec
<tree
, va_gc
> *orig_params
= params
;
3484 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3485 tf_warning_or_error
);
3487 /* cp_build_function_call_vec can reallocate PARAMS by adding
3488 default arguments. That should never happen here. Verify
3490 gcc_assert (params
== orig_params
);
3495 /* Build a function call using a tree list of arguments. */
3498 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3500 vec
<tree
, va_gc
> *vec
;
3503 vec
= make_tree_vector ();
3504 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3505 vec_safe_push (vec
, TREE_VALUE (params
));
3506 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3507 release_tree_vector (vec
);
3511 /* Build a function call using varargs. */
3514 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3516 vec
<tree
, va_gc
> *vec
;
3520 vec
= make_tree_vector ();
3521 va_start (args
, complain
);
3522 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3523 vec_safe_push (vec
, t
);
3525 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3526 release_tree_vector (vec
);
3530 /* Build a function call using a vector of arguments. PARAMS may be
3531 NULL if there are no parameters. This changes the contents of
3535 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3536 tsubst_flags_t complain
)
3538 tree fntype
, fndecl
;
3540 tree original
= function
;
3544 vec
<tree
, va_gc
> *allocated
= NULL
;
3547 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3548 expressions, like those used for ObjC messenger dispatches. */
3549 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3550 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3552 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3553 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3554 if (TREE_CODE (function
) == NOP_EXPR
3555 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3556 function
= TREE_OPERAND (function
, 0);
3558 if (TREE_CODE (function
) == FUNCTION_DECL
)
3560 /* If the function is a non-template member function
3561 or a non-template friend, then we need to check the
3564 Note that if overload resolution failed with a single
3565 candidate this function will be used to explicitly diagnose
3566 the failure for the single call expression. The check is
3567 technically redundant since we also would have failed in
3568 add_function_candidate. */
3570 && (complain
& tf_error
)
3571 && !constraints_satisfied_p (function
))
3573 error ("cannot call function %qD", function
);
3574 location_t loc
= DECL_SOURCE_LOCATION (function
);
3575 diagnose_constraints (loc
, function
, NULL_TREE
);
3576 return error_mark_node
;
3579 if (!mark_used (function
, complain
) && !(complain
& tf_error
))
3580 return error_mark_node
;
3583 /* Convert anything with function type to a pointer-to-function. */
3584 if (DECL_MAIN_P (function
))
3586 if (complain
& tf_error
)
3587 pedwarn (input_location
, OPT_Wpedantic
,
3588 "ISO C++ forbids calling %<::main%> from within program");
3590 return error_mark_node
;
3592 function
= build_addr_func (function
, complain
);
3598 function
= build_addr_func (function
, complain
);
3601 if (function
== error_mark_node
)
3602 return error_mark_node
;
3604 fntype
= TREE_TYPE (function
);
3606 if (TYPE_PTRMEMFUNC_P (fntype
))
3608 if (complain
& tf_error
)
3609 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3610 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3611 original
, original
);
3612 return error_mark_node
;
3615 is_method
= (TYPE_PTR_P (fntype
)
3616 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3618 if (!(TYPE_PTRFN_P (fntype
)
3620 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3622 if (complain
& tf_error
)
3624 if (!flag_diagnostics_show_caret
)
3625 error_at (input_location
,
3626 "%qE cannot be used as a function", original
);
3627 else if (DECL_P (original
))
3628 error_at (input_location
,
3629 "%qD cannot be used as a function", original
);
3631 error_at (input_location
,
3632 "expression cannot be used as a function");
3635 return error_mark_node
;
3638 /* fntype now gets the type of function pointed to. */
3639 fntype
= TREE_TYPE (fntype
);
3640 parm_types
= TYPE_ARG_TYPES (fntype
);
3644 allocated
= make_tree_vector ();
3645 params
= &allocated
;
3648 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3651 return error_mark_node
;
3653 argarray
= (*params
)->address ();
3655 /* Check for errors in format strings and inappropriately
3657 bool warned_p
= check_function_arguments (input_location
, fntype
,
3660 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3664 tree c
= extract_call_expr (ret
);
3665 if (TREE_CODE (c
) == CALL_EXPR
)
3666 TREE_NO_WARNING (c
) = 1;
3669 if (allocated
!= NULL
)
3670 release_tree_vector (allocated
);
3675 /* Subroutine of convert_arguments.
3676 Print an error message about a wrong number of arguments. */
3679 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3683 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3685 if (DECL_NAME (fndecl
) == NULL_TREE
3686 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3689 ? G_("too many arguments to constructor %q#D")
3690 : G_("too few arguments to constructor %q#D"),
3695 ? G_("too many arguments to member function %q#D")
3696 : G_("too few arguments to member function %q#D"),
3702 ? G_("too many arguments to function %q#D")
3703 : G_("too few arguments to function %q#D"),
3705 if (!DECL_IS_BUILTIN (fndecl
))
3706 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
3710 if (c_dialect_objc () && objc_message_selector ())
3713 ? G_("too many arguments to method %q#D")
3714 : G_("too few arguments to method %q#D"),
3715 objc_message_selector ());
3717 error_at (loc
, too_many_p
? G_("too many arguments to function")
3718 : G_("too few arguments to function"));
3722 /* Convert the actual parameter expressions in the list VALUES to the
3723 types in the list TYPELIST. The converted expressions are stored
3724 back in the VALUES vector.
3725 If parmdecls is exhausted, or when an element has NULL as its type,
3726 perform the default conversions.
3728 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3730 This is also where warnings about wrong number of args are generated.
3732 Returns the actual number of arguments processed (which might be less
3733 than the length of the vector), or -1 on error.
3735 In C++, unspecified trailing parameters can be filled in with their
3736 default arguments, if such were specified. Do so here. */
3739 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3740 int flags
, tsubst_flags_t complain
)
3745 /* Argument passing is always copy-initialization. */
3746 flags
|= LOOKUP_ONLYCONVERTING
;
3748 for (i
= 0, typetail
= typelist
;
3749 i
< vec_safe_length (*values
);
3752 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3753 tree val
= (**values
)[i
];
3755 if (val
== error_mark_node
|| type
== error_mark_node
)
3758 if (type
== void_type_node
)
3760 if (complain
& tf_error
)
3762 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3769 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3770 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3771 if (TREE_CODE (val
) == NOP_EXPR
3772 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3773 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3774 val
= TREE_OPERAND (val
, 0);
3776 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3778 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3779 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3780 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3781 val
= decay_conversion (val
, complain
);
3784 if (val
== error_mark_node
)
3789 /* Formal parm type is specified by a function prototype. */
3792 if (!COMPLETE_TYPE_P (complete_type (type
)))
3794 if (complain
& tf_error
)
3797 error ("parameter %P of %qD has incomplete type %qT",
3800 error ("parameter %P has incomplete type %qT", i
, type
);
3802 parmval
= error_mark_node
;
3806 parmval
= convert_for_initialization
3807 (NULL_TREE
, type
, val
, flags
,
3808 ICR_ARGPASS
, fndecl
, i
, complain
);
3809 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
3812 if (parmval
== error_mark_node
)
3815 (**values
)[i
] = parmval
;
3819 if (fndecl
&& magic_varargs_p (fndecl
))
3820 /* Don't do ellipsis conversion for __built_in_constant_p
3821 as this will result in spurious errors for non-trivial
3823 val
= require_complete_type_sfinae (val
, complain
);
3825 val
= convert_arg_to_ellipsis (val
, complain
);
3827 (**values
)[i
] = val
;
3831 typetail
= TREE_CHAIN (typetail
);
3834 if (typetail
!= 0 && typetail
!= void_list_node
)
3836 /* See if there are default arguments that can be used. Because
3837 we hold default arguments in the FUNCTION_TYPE (which is so
3838 wrong), we can see default parameters here from deduced
3839 contexts (and via typeof) for indirect function calls.
3840 Fortunately we know whether we have a function decl to
3841 provide default arguments in a language conformant
3843 if (fndecl
&& TREE_PURPOSE (typetail
)
3844 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
3846 for (; typetail
!= void_list_node
; ++i
)
3848 /* After DR777, with explicit template args we can end up with a
3849 default argument followed by no default argument. */
3850 if (!TREE_PURPOSE (typetail
))
3853 = convert_default_arg (TREE_VALUE (typetail
),
3854 TREE_PURPOSE (typetail
),
3855 fndecl
, i
, complain
);
3857 if (parmval
== error_mark_node
)
3860 vec_safe_push (*values
, parmval
);
3861 typetail
= TREE_CHAIN (typetail
);
3862 /* ends with `...'. */
3863 if (typetail
== NULL_TREE
)
3868 if (typetail
&& typetail
!= void_list_node
)
3870 if (complain
& tf_error
)
3871 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
3879 /* Build a binary-operation expression, after performing default
3880 conversions on the operands. CODE is the kind of expression to
3881 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3882 are the tree codes which correspond to ARG1 and ARG2 when issuing
3883 warnings about possibly misplaced parentheses. They may differ
3884 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3885 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3886 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3887 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3888 ARG2_CODE as ERROR_MARK. */
3891 build_x_binary_op (location_t loc
, enum tree_code code
, tree arg1
,
3892 enum tree_code arg1_code
, tree arg2
,
3893 enum tree_code arg2_code
, tree
*overload_p
,
3894 tsubst_flags_t complain
)
3899 tree overload
= NULL_TREE
;
3904 if (processing_template_decl
)
3906 if (type_dependent_expression_p (arg1
)
3907 || type_dependent_expression_p (arg2
))
3908 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
3909 arg1
= build_non_dependent_expr (arg1
);
3910 arg2
= build_non_dependent_expr (arg2
);
3913 if (code
== DOTSTAR_EXPR
)
3914 expr
= build_m_component_ref (arg1
, arg2
, complain
);
3916 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
3917 &overload
, complain
);
3919 if (overload_p
!= NULL
)
3920 *overload_p
= overload
;
3922 /* Check for cases such as x+y<<z which users are likely to
3923 misinterpret. But don't warn about obj << x + y, since that is a
3924 common idiom for I/O. */
3925 if (warn_parentheses
3926 && (complain
& tf_warning
)
3927 && !processing_template_decl
3928 && !error_operand_p (arg1
)
3929 && !error_operand_p (arg2
)
3930 && (code
!= LSHIFT_EXPR
3931 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
3932 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
3933 arg2_code
, orig_arg2
);
3935 if (processing_template_decl
&& expr
!= error_mark_node
)
3937 if (overload
!= NULL_TREE
)
3938 return (build_min_non_dep_op_overload
3939 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
3941 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
3947 /* Build and return an ARRAY_REF expression. */
3950 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
3951 tsubst_flags_t complain
)
3953 tree orig_arg1
= arg1
;
3954 tree orig_arg2
= arg2
;
3956 tree overload
= NULL_TREE
;
3958 if (processing_template_decl
)
3960 if (type_dependent_expression_p (arg1
)
3961 || type_dependent_expression_p (arg2
))
3962 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
3963 NULL_TREE
, NULL_TREE
);
3964 arg1
= build_non_dependent_expr (arg1
);
3965 arg2
= build_non_dependent_expr (arg2
);
3968 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
3969 NULL_TREE
, &overload
, complain
);
3971 if (processing_template_decl
&& expr
!= error_mark_node
)
3973 if (overload
!= NULL_TREE
)
3974 return (build_min_non_dep_op_overload
3975 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
3977 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
3978 NULL_TREE
, NULL_TREE
);
3983 /* Return whether OP is an expression of enum type cast to integer
3984 type. In C++ even unsigned enum types are cast to signed integer
3985 types. We do not want to issue warnings about comparisons between
3986 signed and unsigned types when one of the types is an enum type.
3987 Those warnings are always false positives in practice. */
3990 enum_cast_to_int (tree op
)
3992 if (CONVERT_EXPR_P (op
)
3993 && TREE_TYPE (op
) == integer_type_node
3994 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
3995 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
3998 /* The cast may have been pushed into a COND_EXPR. */
3999 if (TREE_CODE (op
) == COND_EXPR
)
4000 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4001 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4006 /* For the c-common bits. */
4008 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4011 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4014 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4015 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4018 build_vec_cmp (tree_code code
, tree type
,
4019 tree arg0
, tree arg1
)
4021 tree zero_vec
= build_zero_cst (type
);
4022 tree minus_one_vec
= build_minus_one_cst (type
);
4023 tree cmp_type
= build_same_sized_truth_vector_type(type
);
4024 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4025 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4028 /* Possibly warn about an address never being NULL. */
4031 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4034 || (complain
& tf_warning
) == 0
4035 || c_inhibit_evaluation_warnings
!= 0
4036 || TREE_NO_WARNING (op
))
4039 tree cop
= fold_non_dependent_expr (op
);
4041 if (TREE_CODE (cop
) == ADDR_EXPR
4042 && decl_with_nonnull_addr_p (TREE_OPERAND (cop
, 0))
4043 && !TREE_NO_WARNING (cop
))
4044 warning_at (location
, OPT_Waddress
, "the address of %qD will never "
4045 "be NULL", TREE_OPERAND (cop
, 0));
4047 if (CONVERT_EXPR_P (op
)
4048 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == REFERENCE_TYPE
)
4051 STRIP_NOPS (inner_op
);
4053 if (DECL_P (inner_op
))
4054 warning_at (location
, OPT_Waddress
,
4055 "the compiler can assume that the address of "
4056 "%qD will never be NULL", inner_op
);
4060 /* Build a binary-operation expression without default conversions.
4061 CODE is the kind of expression to build.
4062 LOCATION is the location_t of the operator in the source code.
4063 This function differs from `build' in several ways:
4064 the data type of the result is computed and recorded in it,
4065 warnings are generated if arg data types are invalid,
4066 special handling for addition and subtraction of pointers is known,
4067 and some optimization is done (operations on narrow ints
4068 are done in the narrower type when that gives the same result).
4069 Constant folding is also done before the result is returned.
4071 Note that the operands will never have enumeral types
4072 because either they have just had the default conversions performed
4073 or they have both just been converted to some other type in which
4074 the arithmetic is to be done.
4076 C++: must do special pointer arithmetic when implementing
4077 multiple inheritance, and deal with pointer to member functions. */
4080 cp_build_binary_op (location_t location
,
4081 enum tree_code code
, tree orig_op0
, tree orig_op1
,
4082 tsubst_flags_t complain
)
4085 enum tree_code code0
, code1
;
4087 const char *invalid_op_diag
;
4089 /* Expression code to give to the expression when it is built.
4090 Normally this is CODE, which is what the caller asked for,
4091 but in some special cases we change it. */
4092 enum tree_code resultcode
= code
;
4094 /* Data type in which the computation is to be performed.
4095 In the simplest cases this is the common type of the arguments. */
4096 tree result_type
= NULL
;
4098 /* Nonzero means operands have already been type-converted
4099 in whatever way is necessary.
4100 Zero means they need to be converted to RESULT_TYPE. */
4103 /* Nonzero means create the expression with this type, rather than
4105 tree build_type
= 0;
4107 /* Nonzero means after finally constructing the expression
4108 convert it to this type. */
4109 tree final_type
= 0;
4111 tree result
, result_ovl
;
4112 tree orig_type
= NULL
;
4114 /* Nonzero if this is an operation like MIN or MAX which can
4115 safely be computed in short if both args are promoted shorts.
4116 Also implies COMMON.
4117 -1 indicates a bitwise operation; this makes a difference
4118 in the exact conditions for when it is safe to do the operation
4119 in a narrower mode. */
4122 /* Nonzero if this is a comparison operation;
4123 if both args are promoted shorts, compare the original shorts.
4124 Also implies COMMON. */
4125 int short_compare
= 0;
4127 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4130 /* True if both operands have arithmetic type. */
4131 bool arithmetic_types_p
;
4133 /* Apply default conversions. */
4137 /* Remember whether we're doing / or %. */
4138 bool doing_div_or_mod
= false;
4140 /* Remember whether we're doing << or >>. */
4141 bool doing_shift
= false;
4143 /* Tree holding instrumentation expression. */
4144 tree instrument_expr
= NULL
;
4146 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
4147 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
4148 || code
== TRUTH_XOR_EXPR
)
4150 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4151 op0
= decay_conversion (op0
, complain
);
4152 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4153 op1
= decay_conversion (op1
, complain
);
4157 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4158 op0
= cp_default_conversion (op0
, complain
);
4159 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4160 op1
= cp_default_conversion (op1
, complain
);
4163 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4164 STRIP_TYPE_NOPS (op0
);
4165 STRIP_TYPE_NOPS (op1
);
4167 /* DTRT if one side is an overloaded function, but complain about it. */
4168 if (type_unknown_p (op0
))
4170 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
4171 if (t
!= error_mark_node
)
4173 if (complain
& tf_error
)
4174 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4179 if (type_unknown_p (op1
))
4181 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4182 if (t
!= error_mark_node
)
4184 if (complain
& tf_error
)
4185 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4191 type0
= TREE_TYPE (op0
);
4192 type1
= TREE_TYPE (op1
);
4194 /* The expression codes of the data types of the arguments tell us
4195 whether the arguments are integers, floating, pointers, etc. */
4196 code0
= TREE_CODE (type0
);
4197 code1
= TREE_CODE (type1
);
4199 /* If an error was already reported for one of the arguments,
4200 avoid reporting another error. */
4201 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4202 return error_mark_node
;
4204 if ((invalid_op_diag
4205 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4207 if (complain
& tf_error
)
4208 error (invalid_op_diag
);
4209 return error_mark_node
;
4212 /* Issue warnings about peculiar, but valid, uses of NULL. */
4213 if ((orig_op0
== null_node
|| orig_op1
== null_node
)
4214 /* It's reasonable to use pointer values as operands of &&
4215 and ||, so NULL is no exception. */
4216 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4217 && ( /* Both are NULL (or 0) and the operation was not a
4218 comparison or a pointer subtraction. */
4219 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4220 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4221 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4222 || (!null_ptr_cst_p (orig_op0
)
4223 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4224 || (!null_ptr_cst_p (orig_op1
)
4225 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4226 && (complain
& tf_warning
))
4228 source_location loc
=
4229 expansion_point_location_if_in_system_header (input_location
);
4231 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4234 /* In case when one of the operands of the binary operation is
4235 a vector and another is a scalar -- convert scalar to vector. */
4236 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4238 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4239 complain
& tf_error
);
4241 switch (convert_flag
)
4244 return error_mark_node
;
4247 op0
= convert (TREE_TYPE (type1
), op0
);
4248 op0
= save_expr (op0
);
4249 op0
= build_vector_from_val (type1
, op0
);
4250 type0
= TREE_TYPE (op0
);
4251 code0
= TREE_CODE (type0
);
4257 op1
= convert (TREE_TYPE (type0
), op1
);
4258 op1
= save_expr (op1
);
4259 op1
= build_vector_from_val (type0
, op1
);
4260 type1
= TREE_TYPE (op1
);
4261 code1
= TREE_CODE (type1
);
4273 /* Subtraction of two similar pointers.
4274 We must subtract them as integers, then divide by object size. */
4275 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4276 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4278 return pointer_diff (location
, op0
, op1
,
4279 common_pointer_type (type0
, type1
), complain
);
4280 /* In all other cases except pointer - int, the usual arithmetic
4282 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4287 /* The pointer - int case is just like pointer + int; fall
4291 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4292 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4296 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4297 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4298 if (processing_template_decl
)
4300 result_type
= TREE_TYPE (ptr_operand
);
4303 return cp_pointer_int_sum (location
, code
,
4315 case TRUNC_DIV_EXPR
:
4317 case FLOOR_DIV_EXPR
:
4318 case ROUND_DIV_EXPR
:
4319 case EXACT_DIV_EXPR
:
4320 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4321 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4322 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4323 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4325 enum tree_code tcode0
= code0
, tcode1
= code1
;
4326 tree cop1
= fold_non_dependent_expr (op1
);
4327 doing_div_or_mod
= true;
4328 warn_for_div_by_zero (location
, cop1
);
4330 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4331 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4332 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4333 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4335 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4336 resultcode
= RDIV_EXPR
;
4338 /* When dividing two signed integers, we have to promote to int.
4339 unless we divide by a constant != -1. Note that default
4340 conversion will have been performed on the operands at this
4341 point, so we have to dig out the original type to find out if
4343 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4344 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4345 || (TREE_CODE (op1
) == INTEGER_CST
4346 && ! integer_all_onesp (op1
)));
4355 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4356 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4357 && !VECTOR_FLOAT_TYPE_P (type0
)
4358 && !VECTOR_FLOAT_TYPE_P (type1
)))
4362 case TRUNC_MOD_EXPR
:
4363 case FLOOR_MOD_EXPR
:
4365 tree cop1
= fold_non_dependent_expr (op1
);
4366 doing_div_or_mod
= true;
4367 warn_for_div_by_zero (location
, cop1
);
4370 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4371 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4372 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4374 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4376 /* Although it would be tempting to shorten always here, that loses
4377 on some targets, since the modulo instruction is undefined if the
4378 quotient can't be represented in the computation mode. We shorten
4379 only if unsigned or if dividing by something we know != -1. */
4380 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4381 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4382 || (TREE_CODE (op1
) == INTEGER_CST
4383 && ! integer_all_onesp (op1
)));
4388 case TRUTH_ANDIF_EXPR
:
4389 case TRUTH_ORIF_EXPR
:
4390 case TRUTH_AND_EXPR
:
4392 if (!VECTOR_TYPE_P (type0
) && VECTOR_TYPE_P (type1
))
4394 if (!COMPARISON_CLASS_P (op1
))
4395 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4396 build_zero_cst (type1
), complain
);
4397 if (code
== TRUTH_ANDIF_EXPR
)
4399 tree z
= build_zero_cst (TREE_TYPE (op1
));
4400 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4402 else if (code
== TRUTH_ORIF_EXPR
)
4404 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4405 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4410 if (VECTOR_TYPE_P (type0
))
4412 if (!COMPARISON_CLASS_P (op0
))
4413 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4414 build_zero_cst (type0
), complain
);
4415 if (!VECTOR_TYPE_P (type1
))
4417 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4418 tree z
= build_zero_cst (TREE_TYPE (op0
));
4419 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
4421 else if (!COMPARISON_CLASS_P (op1
))
4422 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4423 build_zero_cst (type1
), complain
);
4425 if (code
== TRUTH_ANDIF_EXPR
)
4426 code
= BIT_AND_EXPR
;
4427 else if (code
== TRUTH_ORIF_EXPR
)
4428 code
= BIT_IOR_EXPR
;
4432 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4435 result_type
= boolean_type_node
;
4438 /* Shift operations: result has same type as first operand;
4439 always convert second operand to int.
4440 Also set SHORT_SHIFT if shifting rightward. */
4443 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4444 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4446 result_type
= type0
;
4449 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4450 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4451 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4452 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4454 result_type
= type0
;
4457 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4459 tree const_op1
= fold_non_dependent_expr (op1
);
4460 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4462 result_type
= type0
;
4464 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4466 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4468 if ((complain
& tf_warning
)
4469 && c_inhibit_evaluation_warnings
== 0)
4470 warning (OPT_Wshift_count_negative
,
4471 "right shift count is negative");
4475 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4476 && (complain
& tf_warning
)
4477 && c_inhibit_evaluation_warnings
== 0)
4478 warning (OPT_Wshift_count_overflow
,
4479 "right shift count >= width of type");
4482 /* Avoid converting op1 to result_type later. */
4488 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4489 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4491 result_type
= type0
;
4494 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4495 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4496 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4497 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4499 result_type
= type0
;
4502 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4504 tree const_op0
= fold_non_dependent_expr (op0
);
4505 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4507 tree const_op1
= fold_non_dependent_expr (op1
);
4508 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4510 result_type
= type0
;
4512 if (TREE_CODE (const_op0
) == INTEGER_CST
4513 && tree_int_cst_sgn (const_op0
) < 0
4514 && (complain
& tf_warning
)
4515 && c_inhibit_evaluation_warnings
== 0)
4516 warning (OPT_Wshift_negative_value
,
4517 "left shift of negative value");
4518 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4520 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4522 if ((complain
& tf_warning
)
4523 && c_inhibit_evaluation_warnings
== 0)
4524 warning (OPT_Wshift_count_negative
,
4525 "left shift count is negative");
4527 else if (compare_tree_int (const_op1
,
4528 TYPE_PRECISION (type0
)) >= 0)
4530 if ((complain
& tf_warning
)
4531 && c_inhibit_evaluation_warnings
== 0)
4532 warning (OPT_Wshift_count_overflow
,
4533 "left shift count >= width of type");
4535 else if (TREE_CODE (const_op0
) == INTEGER_CST
4536 && (complain
& tf_warning
))
4537 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4539 /* Avoid converting op1 to result_type later. */
4546 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4548 result_type
= type0
;
4549 if (TREE_CODE (op1
) == INTEGER_CST
)
4551 if (tree_int_cst_lt (op1
, integer_zero_node
))
4553 if (complain
& tf_warning
)
4554 warning (0, (code
== LROTATE_EXPR
)
4555 ? G_("left rotate count is negative")
4556 : G_("right rotate count is negative"));
4558 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4560 if (complain
& tf_warning
)
4561 warning (0, (code
== LROTATE_EXPR
)
4562 ? G_("left rotate count >= width of type")
4563 : G_("right rotate count >= width of type"));
4566 /* Convert the shift-count to an integer, regardless of
4567 size of value being shifted. */
4568 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4569 op1
= cp_convert (integer_type_node
, op1
, complain
);
4575 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4576 goto vector_compare
;
4577 if ((complain
& tf_warning
)
4578 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4579 warning (OPT_Wfloat_equal
,
4580 "comparing floating point with == or != is unsafe");
4581 if ((complain
& tf_warning
)
4582 && ((TREE_CODE (orig_op0
) == STRING_CST
4583 && !integer_zerop (cp_fully_fold (op1
)))
4584 || (TREE_CODE (orig_op1
) == STRING_CST
4585 && !integer_zerop (cp_fully_fold (op0
)))))
4586 warning (OPT_Waddress
, "comparison with string literal results "
4587 "in unspecified behavior");
4589 build_type
= boolean_type_node
;
4590 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4591 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4592 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4593 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4595 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4596 && null_ptr_cst_p (orig_op1
))
4597 /* Handle, eg, (void*)0 (c++/43906), and more. */
4598 || (code0
== POINTER_TYPE
4599 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
4601 if (TYPE_PTR_P (type1
))
4602 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4603 CPO_COMPARISON
, complain
);
4605 result_type
= type0
;
4607 if (char_type_p (TREE_TYPE (orig_op1
))
4608 && warning (OPT_Wpointer_compare
,
4609 "comparison between pointer and zero character "
4611 inform (input_location
,
4612 "did you mean to dereference the pointer?");
4613 warn_for_null_address (location
, op0
, complain
);
4615 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4616 && null_ptr_cst_p (orig_op0
))
4617 /* Handle, eg, (void*)0 (c++/43906), and more. */
4618 || (code1
== POINTER_TYPE
4619 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
4621 if (TYPE_PTR_P (type0
))
4622 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4623 CPO_COMPARISON
, complain
);
4625 result_type
= type1
;
4627 if (char_type_p (TREE_TYPE (orig_op0
))
4628 && warning (OPT_Wpointer_compare
,
4629 "comparison between pointer and zero character "
4631 inform (input_location
,
4632 "did you mean to dereference the pointer?");
4633 warn_for_null_address (location
, op1
, complain
);
4635 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4636 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4637 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4638 CPO_COMPARISON
, complain
);
4639 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4640 /* One of the operands must be of nullptr_t type. */
4641 result_type
= TREE_TYPE (nullptr_node
);
4642 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4644 result_type
= type0
;
4645 if (complain
& tf_error
)
4646 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4648 return error_mark_node
;
4650 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4652 result_type
= type1
;
4653 if (complain
& tf_error
)
4654 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4656 return error_mark_node
;
4658 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
4660 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4661 == ptrmemfunc_vbit_in_delta
)
4663 tree pfn0
, delta0
, e1
, e2
;
4665 if (TREE_SIDE_EFFECTS (op0
))
4666 op0
= save_expr (op0
);
4668 pfn0
= pfn_from_ptrmemfunc (op0
);
4669 delta0
= delta_from_ptrmemfunc (op0
);
4670 e1
= cp_build_binary_op (location
,
4673 build_zero_cst (TREE_TYPE (pfn0
)),
4675 e2
= cp_build_binary_op (location
,
4681 if (complain
& tf_warning
)
4682 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4684 e2
= cp_build_binary_op (location
,
4685 EQ_EXPR
, e2
, integer_zero_node
,
4687 op0
= cp_build_binary_op (location
,
4688 TRUTH_ANDIF_EXPR
, e1
, e2
,
4690 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4694 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4695 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4697 result_type
= TREE_TYPE (op0
);
4699 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
4700 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4701 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4704 /* E will be the final comparison. */
4706 /* E1 and E2 are for scratch. */
4714 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4715 CPO_COMPARISON
, complain
);
4717 if (!same_type_p (TREE_TYPE (op0
), type
))
4718 op0
= cp_convert_and_check (type
, op0
, complain
);
4719 if (!same_type_p (TREE_TYPE (op1
), type
))
4720 op1
= cp_convert_and_check (type
, op1
, complain
);
4722 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4723 return error_mark_node
;
4725 if (TREE_SIDE_EFFECTS (op0
))
4726 op0
= save_expr (op0
);
4727 if (TREE_SIDE_EFFECTS (op1
))
4728 op1
= save_expr (op1
);
4730 pfn0
= pfn_from_ptrmemfunc (op0
);
4731 pfn0
= cp_fully_fold (pfn0
);
4732 /* Avoid -Waddress warnings (c++/64877). */
4733 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
4734 TREE_NO_WARNING (pfn0
) = 1;
4735 pfn1
= pfn_from_ptrmemfunc (op1
);
4736 pfn1
= cp_fully_fold (pfn1
);
4737 delta0
= delta_from_ptrmemfunc (op0
);
4738 delta1
= delta_from_ptrmemfunc (op1
);
4739 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4740 == ptrmemfunc_vbit_in_delta
)
4745 && ((op0.delta == op1.delta)
4746 || (!op0.pfn && op0.delta & 1 == 0
4747 && op1.delta & 1 == 0))
4749 The reason for the `!op0.pfn' bit is that a NULL
4750 pointer-to-member is any member with a zero PFN and
4751 LSB of the DELTA field is 0. */
4753 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4757 e1
= cp_build_binary_op (location
,
4758 EQ_EXPR
, e1
, integer_zero_node
,
4760 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4764 e2
= cp_build_binary_op (location
,
4765 EQ_EXPR
, e2
, integer_zero_node
,
4767 e1
= cp_build_binary_op (location
,
4768 TRUTH_ANDIF_EXPR
, e2
, e1
,
4770 e2
= cp_build_binary_op (location
, EQ_EXPR
,
4772 build_zero_cst (TREE_TYPE (pfn0
)),
4774 e2
= cp_build_binary_op (location
,
4775 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4776 e1
= cp_build_binary_op (location
,
4777 EQ_EXPR
, delta0
, delta1
, complain
);
4778 e1
= cp_build_binary_op (location
,
4779 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4786 && (!op0.pfn || op0.delta == op1.delta))
4788 The reason for the `!op0.pfn' bit is that a NULL
4789 pointer-to-member is any member with a zero PFN; the
4790 DELTA field is unspecified. */
4792 e1
= cp_build_binary_op (location
,
4793 EQ_EXPR
, delta0
, delta1
, complain
);
4794 e2
= cp_build_binary_op (location
,
4797 build_zero_cst (TREE_TYPE (pfn0
)),
4799 e1
= cp_build_binary_op (location
,
4800 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4802 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
4803 e
= cp_build_binary_op (location
,
4804 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4805 if (code
== EQ_EXPR
)
4807 return cp_build_binary_op (location
,
4808 EQ_EXPR
, e
, integer_zero_node
, complain
);
4812 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
4813 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
4815 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
4816 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
4824 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
4825 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
4827 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4828 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4829 CPO_COMPARISON
, complain
);
4836 if (TREE_CODE (orig_op0
) == STRING_CST
4837 || TREE_CODE (orig_op1
) == STRING_CST
)
4839 if (complain
& tf_warning
)
4840 warning (OPT_Waddress
, "comparison with string literal results "
4841 "in unspecified behavior");
4844 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4848 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4850 && !vector_types_compatible_elements_p (type0
, type1
))
4852 if (complain
& tf_error
)
4854 error_at (location
, "comparing vectors with different "
4856 inform (location
, "operand types are %qT and %qT",
4859 return error_mark_node
;
4862 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
4864 if (complain
& tf_error
)
4866 error_at (location
, "comparing vectors with different "
4867 "number of elements");
4868 inform (location
, "operand types are %qT and %qT",
4871 return error_mark_node
;
4874 /* It's not precisely specified how the usual arithmetic
4875 conversions apply to the vector types. Here, we use
4876 the unsigned type if one of the operands is signed and
4877 the other one is unsigned. */
4878 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
4880 if (!TYPE_UNSIGNED (type0
))
4881 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
4883 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
4884 warning_at (location
, OPT_Wsign_compare
, "comparison between "
4885 "types %qT and %qT", type0
, type1
);
4888 /* Always construct signed integer vector type. */
4889 intt
= c_common_type_for_size (GET_MODE_BITSIZE
4890 (TYPE_MODE (TREE_TYPE (type0
))), 0);
4893 if (complain
& tf_error
)
4894 error_at (location
, "could not find an integer type "
4895 "of the same size as %qT", TREE_TYPE (type0
));
4896 return error_mark_node
;
4898 result_type
= build_opaque_vector_type (intt
,
4899 TYPE_VECTOR_SUBPARTS (type0
));
4901 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
4903 build_type
= boolean_type_node
;
4904 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4905 || code0
== ENUMERAL_TYPE
)
4906 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4907 || code1
== ENUMERAL_TYPE
))
4909 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4910 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4911 CPO_COMPARISON
, complain
);
4912 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
4914 result_type
= type0
;
4915 if (extra_warnings
&& (complain
& tf_warning
))
4916 warning (OPT_Wextra
,
4917 "ordered comparison of pointer with integer zero");
4919 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
4921 result_type
= type1
;
4922 if (extra_warnings
&& (complain
& tf_warning
))
4923 warning (OPT_Wextra
,
4924 "ordered comparison of pointer with integer zero");
4926 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4927 /* One of the operands must be of nullptr_t type. */
4928 result_type
= TREE_TYPE (nullptr_node
);
4929 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4931 result_type
= type0
;
4932 if (complain
& tf_error
)
4933 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4935 return error_mark_node
;
4937 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4939 result_type
= type1
;
4940 if (complain
& tf_error
)
4941 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4943 return error_mark_node
;
4947 case UNORDERED_EXPR
:
4954 build_type
= integer_type_node
;
4955 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
4957 if (complain
& tf_error
)
4958 error ("unordered comparison on non-floating point argument");
4959 return error_mark_node
;
4968 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
4969 || code0
== ENUMERAL_TYPE
)
4970 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4971 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
4972 arithmetic_types_p
= 1;
4975 arithmetic_types_p
= 0;
4976 /* Vector arithmetic is only allowed when both sides are vectors. */
4977 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4979 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
4980 || !vector_types_compatible_elements_p (type0
, type1
))
4982 if (complain
& tf_error
)
4984 /* "location" already embeds the locations of the
4985 operands, so we don't need to add them separately
4987 rich_location
richloc (line_table
, location
);
4988 binary_op_error (&richloc
, code
, type0
, type1
);
4990 return error_mark_node
;
4992 arithmetic_types_p
= 1;
4995 /* Determine the RESULT_TYPE, if it is not already known. */
4997 && arithmetic_types_p
4998 && (shorten
|| common
|| short_compare
))
5000 result_type
= cp_common_type (type0
, type1
);
5001 if (complain
& tf_warning
)
5002 do_warn_double_promotion (result_type
, type0
, type1
,
5003 "implicit conversion from %qT to %qT "
5004 "to match other operand of binary "
5011 if (complain
& tf_error
)
5013 "invalid operands of types %qT and %qT to binary %qO",
5014 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
5015 return error_mark_node
;
5018 /* If we're in a template, the only thing we need to know is the
5020 if (processing_template_decl
)
5022 /* Since the middle-end checks the type when doing a build2, we
5023 need to build the tree in pieces. This built tree will never
5024 get out of the front-end as we replace it when instantiating
5026 tree tmp
= build2 (resultcode
,
5027 build_type
? build_type
: result_type
,
5029 TREE_OPERAND (tmp
, 0) = op0
;
5033 if (arithmetic_types_p
)
5035 bool first_complex
= (code0
== COMPLEX_TYPE
);
5036 bool second_complex
= (code1
== COMPLEX_TYPE
);
5037 int none_complex
= (!first_complex
&& !second_complex
);
5039 /* Adapted from patch for c/24581. */
5040 if (first_complex
!= second_complex
5041 && (code
== PLUS_EXPR
5042 || code
== MINUS_EXPR
5043 || code
== MULT_EXPR
5044 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
5045 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
5046 && flag_signed_zeros
)
5048 /* An operation on mixed real/complex operands must be
5049 handled specially, but the language-independent code can
5050 more easily optimize the plain complex arithmetic if
5051 -fno-signed-zeros. */
5052 tree real_type
= TREE_TYPE (result_type
);
5056 if (TREE_TYPE (op0
) != result_type
)
5057 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5058 if (TREE_TYPE (op1
) != real_type
)
5059 op1
= cp_convert_and_check (real_type
, op1
, complain
);
5063 if (TREE_TYPE (op0
) != real_type
)
5064 op0
= cp_convert_and_check (real_type
, op0
, complain
);
5065 if (TREE_TYPE (op1
) != result_type
)
5066 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5068 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
5069 return error_mark_node
;
5072 op0
= save_expr (op0
);
5073 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
5074 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
5078 case TRUNC_DIV_EXPR
:
5079 op1
= save_expr (op1
);
5080 imag
= build2 (resultcode
, real_type
, imag
, op1
);
5084 real
= build2 (resultcode
, real_type
, real
, op1
);
5092 op1
= save_expr (op1
);
5093 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
5094 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
5098 op0
= save_expr (op0
);
5099 imag
= build2 (resultcode
, real_type
, op0
, imag
);
5102 real
= build2 (resultcode
, real_type
, op0
, real
);
5105 real
= build2 (resultcode
, real_type
, op0
, real
);
5106 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
5112 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
5116 /* For certain operations (which identify themselves by shorten != 0)
5117 if both args were extended from the same smaller type,
5118 do the arithmetic in that type and then extend.
5120 shorten !=0 and !=1 indicates a bitwise operation.
5121 For them, this optimization is safe only if
5122 both args are zero-extended or both are sign-extended.
5123 Otherwise, we might change the result.
5124 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5125 but calculated in (unsigned short) it would be (unsigned short)-1. */
5127 if (shorten
&& none_complex
)
5129 orig_type
= result_type
;
5130 final_type
= result_type
;
5131 result_type
= shorten_binary_op (result_type
, op0
, op1
,
5135 /* Comparison operations are shortened too but differently.
5136 They identify themselves by setting short_compare = 1. */
5140 /* We call shorten_compare only for diagnostic-reason. */
5141 tree xop0
= fold_simple (op0
), xop1
= fold_simple (op1
),
5142 xresult_type
= result_type
;
5143 enum tree_code xresultcode
= resultcode
;
5144 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
5148 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
5149 && warn_sign_compare
5150 /* Do not warn until the template is instantiated; we cannot
5151 bound the ranges of the arguments until that point. */
5152 && !processing_template_decl
5153 && (complain
& tf_warning
)
5154 && c_inhibit_evaluation_warnings
== 0
5155 /* Even unsigned enum types promote to signed int. We don't
5156 want to issue -Wsign-compare warnings for this case. */
5157 && !enum_cast_to_int (orig_op0
)
5158 && !enum_cast_to_int (orig_op1
))
5160 tree oop0
= maybe_constant_value (orig_op0
);
5161 tree oop1
= maybe_constant_value (orig_op1
);
5163 if (TREE_CODE (oop0
) != INTEGER_CST
)
5164 oop0
= cp_fully_fold (orig_op0
);
5165 if (TREE_CODE (oop1
) != INTEGER_CST
)
5166 oop1
= cp_fully_fold (orig_op1
);
5167 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
5168 result_type
, resultcode
);
5172 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5173 Then the expression will be built.
5174 It will be given type FINAL_TYPE if that is nonzero;
5175 otherwise, it will be given type RESULT_TYPE. */
5178 if (TREE_TYPE (op0
) != result_type
)
5179 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5180 if (TREE_TYPE (op1
) != result_type
)
5181 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5183 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5184 return error_mark_node
;
5187 if (build_type
== NULL_TREE
)
5188 build_type
= result_type
;
5190 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
5191 | SANITIZE_FLOAT_DIVIDE
))
5192 && !processing_template_decl
5193 && do_ubsan_in_current_function ()
5194 && (doing_div_or_mod
|| doing_shift
))
5196 /* OP0 and/or OP1 might have side-effects. */
5197 op0
= cp_save_expr (op0
);
5198 op1
= cp_save_expr (op1
);
5199 op0
= fold_non_dependent_expr (op0
);
5200 op1
= fold_non_dependent_expr (op1
);
5201 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
5202 | SANITIZE_FLOAT_DIVIDE
)))
5204 /* For diagnostics we want to use the promoted types without
5205 shorten_binary_op. So convert the arguments to the
5206 original result_type. */
5209 if (orig_type
!= NULL
&& result_type
!= orig_type
)
5211 cop0
= cp_convert (orig_type
, op0
, complain
);
5212 cop1
= cp_convert (orig_type
, op1
, complain
);
5214 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
5216 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
5217 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5220 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
5221 if (final_type
!= 0)
5222 result
= cp_convert (final_type
, result
, complain
);
5224 if (instrument_expr
!= NULL
)
5225 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5226 instrument_expr
, result
);
5228 if (!processing_template_decl
)
5230 op0
= cp_fully_fold (op0
);
5231 /* Only consider the second argument if the first isn't overflowed. */
5232 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
5234 op1
= cp_fully_fold (op1
);
5235 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
5238 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
5239 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
5242 result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
5243 if (TREE_OVERFLOW_P (result_ovl
))
5244 overflow_warning (location
, result_ovl
);
5249 /* Build a VEC_PERM_EXPR.
5250 This is a simple wrapper for c_build_vec_perm_expr. */
5252 build_x_vec_perm_expr (location_t loc
,
5253 tree arg0
, tree arg1
, tree arg2
,
5254 tsubst_flags_t complain
)
5256 tree orig_arg0
= arg0
;
5257 tree orig_arg1
= arg1
;
5258 tree orig_arg2
= arg2
;
5259 if (processing_template_decl
)
5261 if (type_dependent_expression_p (arg0
)
5262 || type_dependent_expression_p (arg1
)
5263 || type_dependent_expression_p (arg2
))
5264 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5265 arg0
= build_non_dependent_expr (arg0
);
5267 arg1
= build_non_dependent_expr (arg1
);
5268 arg2
= build_non_dependent_expr (arg2
);
5270 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5271 if (processing_template_decl
&& exp
!= error_mark_node
)
5272 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5273 orig_arg1
, orig_arg2
);
5277 /* Return a tree for the sum or difference (RESULTCODE says which)
5278 of pointer PTROP and integer INTOP. */
5281 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
5282 tree intop
, tsubst_flags_t complain
)
5284 tree res_type
= TREE_TYPE (ptrop
);
5286 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5287 in certain circumstance (when it's valid to do so). So we need
5288 to make sure it's complete. We don't need to check here, if we
5289 can actually complete it at all, as those checks will be done in
5290 pointer_int_sum() anyway. */
5291 complete_type (TREE_TYPE (res_type
));
5293 return pointer_int_sum (loc
, resultcode
, ptrop
,
5294 intop
, complain
& tf_warning_or_error
);
5297 /* Return a tree for the difference of pointers OP0 and OP1.
5298 The resulting tree has type int. */
5301 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
5302 tsubst_flags_t complain
)
5305 tree restype
= ptrdiff_type_node
;
5306 tree target_type
= TREE_TYPE (ptrtype
);
5308 if (!complete_type_or_else (target_type
, NULL_TREE
))
5309 return error_mark_node
;
5311 if (VOID_TYPE_P (target_type
))
5313 if (complain
& tf_error
)
5314 permerror (loc
, "ISO C++ forbids using pointer of "
5315 "type %<void *%> in subtraction");
5317 return error_mark_node
;
5319 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5321 if (complain
& tf_error
)
5322 permerror (loc
, "ISO C++ forbids using pointer to "
5323 "a function in subtraction");
5325 return error_mark_node
;
5327 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5329 if (complain
& tf_error
)
5330 permerror (loc
, "ISO C++ forbids using pointer to "
5331 "a method in subtraction");
5333 return error_mark_node
;
5336 /* First do the subtraction as integers;
5337 then drop through to build the divide operator. */
5339 op0
= cp_build_binary_op (loc
,
5341 cp_convert (restype
, op0
, complain
),
5342 cp_convert (restype
, op1
, complain
),
5345 /* This generates an error if op1 is a pointer to an incomplete type. */
5346 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5348 if (complain
& tf_error
)
5349 error_at (loc
, "invalid use of a pointer to an incomplete type in "
5350 "pointer arithmetic");
5352 return error_mark_node
;
5355 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5357 if (complain
& tf_error
)
5358 error_at (loc
, "arithmetic on pointer to an empty aggregate");
5360 return error_mark_node
;
5363 op1
= (TYPE_PTROB_P (ptrtype
)
5364 ? size_in_bytes_loc (loc
, target_type
)
5365 : integer_one_node
);
5367 /* Do the division. */
5369 result
= build2_loc (loc
, EXACT_DIV_EXPR
, restype
, op0
,
5370 cp_convert (restype
, op1
, complain
));
5374 /* Construct and perhaps optimize a tree representation
5375 for a unary operation. CODE, a tree_code, specifies the operation
5376 and XARG is the operand. */
5379 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
5380 tsubst_flags_t complain
)
5382 tree orig_expr
= xarg
;
5385 tree overload
= NULL_TREE
;
5387 if (processing_template_decl
)
5389 if (type_dependent_expression_p (xarg
))
5390 return build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
5392 xarg
= build_non_dependent_expr (xarg
);
5397 /* [expr.unary.op] says:
5399 The address of an object of incomplete type can be taken.
5401 (And is just the ordinary address operator, not an overloaded
5402 "operator &".) However, if the type is a template
5403 specialization, we must complete the type at this point so that
5404 an overloaded "operator &" will be available if required. */
5405 if (code
== ADDR_EXPR
5406 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5407 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5408 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5409 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5410 /* Don't look for a function. */;
5412 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5413 NULL_TREE
, &overload
, complain
);
5415 if (!exp
&& code
== ADDR_EXPR
)
5417 if (is_overloaded_fn (xarg
))
5419 tree fn
= get_first_fn (xarg
);
5420 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5422 if (complain
& tf_error
)
5423 error (DECL_CONSTRUCTOR_P (fn
)
5424 ? G_("taking address of constructor %qE")
5425 : G_("taking address of destructor %qE"),
5427 return error_mark_node
;
5431 /* A pointer to member-function can be formed only by saying
5433 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5434 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5436 if (TREE_CODE (xarg
) != OFFSET_REF
5437 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5439 if (complain
& tf_error
)
5441 error ("invalid use of %qE to form a "
5442 "pointer-to-member-function", xarg
.get_value ());
5443 if (TREE_CODE (xarg
) != OFFSET_REF
)
5444 inform (input_location
, " a qualified-id is required");
5446 return error_mark_node
;
5450 if (complain
& tf_error
)
5451 error ("parentheses around %qE cannot be used to form a"
5452 " pointer-to-member-function",
5455 return error_mark_node
;
5456 PTRMEM_OK_P (xarg
) = 1;
5460 if (TREE_CODE (xarg
) == OFFSET_REF
)
5462 ptrmem
= PTRMEM_OK_P (xarg
);
5464 if (!ptrmem
&& !flag_ms_extensions
5465 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5467 /* A single non-static member, make sure we don't allow a
5468 pointer-to-member. */
5469 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5470 TREE_OPERAND (xarg
, 0),
5471 ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
));
5472 PTRMEM_OK_P (xarg
) = ptrmem
;
5476 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5479 if (processing_template_decl
&& exp
!= error_mark_node
)
5481 if (overload
!= NULL_TREE
)
5482 return (build_min_non_dep_op_overload
5483 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
5485 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5486 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5488 if (TREE_CODE (exp
) == ADDR_EXPR
)
5489 PTRMEM_OK_P (exp
) = ptrmem
;
5493 /* Construct and perhaps optimize a tree representation
5494 for __builtin_addressof operation. ARG specifies the operand. */
5497 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
5499 tree orig_expr
= arg
;
5501 if (processing_template_decl
)
5503 if (type_dependent_expression_p (arg
))
5504 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
5506 arg
= build_non_dependent_expr (arg
);
5509 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
5511 if (processing_template_decl
&& exp
!= error_mark_node
)
5512 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
5516 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5517 constants, where a null value is represented by an INTEGER_CST of
5521 cp_truthvalue_conversion (tree expr
)
5523 tree type
= TREE_TYPE (expr
);
5524 if (TYPE_PTR_OR_PTRMEM_P (type
)
5525 /* Avoid ICE on invalid use of non-static member function. */
5526 || TREE_CODE (expr
) == FUNCTION_DECL
)
5527 return build_binary_op (input_location
, NE_EXPR
, expr
, nullptr_node
, 1);
5529 return c_common_truthvalue_conversion (input_location
, expr
);
5532 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5535 condition_conversion (tree expr
)
5538 if (processing_template_decl
)
5540 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5541 tf_warning_or_error
, LOOKUP_NORMAL
);
5542 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5546 /* Returns the address of T. This function will fold away
5547 ADDR_EXPR of INDIRECT_REF. */
5550 build_address (tree t
)
5552 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5553 return error_mark_node
;
5554 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
);
5555 t
= build_fold_addr_expr (t
);
5556 if (TREE_CODE (t
) != ADDR_EXPR
)
5561 /* Return a NOP_EXPR converting EXPR to TYPE. */
5564 build_nop (tree type
, tree expr
)
5566 if (type
== error_mark_node
|| error_operand_p (expr
))
5568 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
5571 /* Take the address of ARG, whatever that means under C++ semantics.
5572 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5573 and class rvalues as well.
5575 Nothing should call this function directly; instead, callers should use
5576 cp_build_addr_expr or cp_build_addr_expr_strict. */
5579 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5584 if (!arg
|| error_operand_p (arg
))
5585 return error_mark_node
;
5587 arg
= mark_lvalue_use (arg
);
5588 argtype
= lvalue_type (arg
);
5590 gcc_assert (!identifier_p (arg
) || !IDENTIFIER_OPNAME_P (arg
));
5592 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5593 && !really_overloaded_fn (TREE_OPERAND (arg
, 1)))
5595 /* They're trying to take the address of a unique non-static
5596 member function. This is ill-formed (except in MS-land),
5597 but let's try to DTRT.
5598 Note: We only handle unique functions here because we don't
5599 want to complain if there's a static overload; non-unique
5600 cases will be handled by instantiate_type. But we need to
5601 handle this case here to allow casts on the resulting PMF.
5602 We could defer this in non-MS mode, but it's easier to give
5603 a useful error here. */
5605 /* Inside constant member functions, the `this' pointer
5606 contains an extra const qualifier. TYPE_MAIN_VARIANT
5607 is used here to remove this const from the diagnostics
5608 and the created OFFSET_REF. */
5609 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5610 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5611 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5612 return error_mark_node
;
5614 if (! flag_ms_extensions
)
5616 tree name
= DECL_NAME (fn
);
5617 if (!(complain
& tf_error
))
5618 return error_mark_node
;
5619 else if (current_class_type
5620 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5621 /* An expression like &memfn. */
5622 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5623 " or parenthesized non-static member function to form"
5624 " a pointer to member function. Say %<&%T::%D%>",
5627 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5628 " function to form a pointer to member function."
5632 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5635 /* Uninstantiated types are all functions. Taking the
5636 address of a function is a no-op, so just return the
5638 if (type_unknown_p (arg
))
5639 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5641 if (TREE_CODE (arg
) == OFFSET_REF
)
5642 /* We want a pointer to member; bypass all the code for actually taking
5643 the address of something. */
5646 /* Anything not already handled and not a true memory reference
5648 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5649 && TREE_CODE (argtype
) != METHOD_TYPE
)
5651 cp_lvalue_kind kind
= lvalue_kind (arg
);
5652 if (kind
== clk_none
)
5654 if (complain
& tf_error
)
5655 lvalue_error (input_location
, lv_addressof
);
5656 return error_mark_node
;
5658 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
5660 if (!(complain
& tf_error
))
5661 return error_mark_node
;
5662 if (kind
& clk_class
)
5663 /* Make this a permerror because we used to accept it. */
5664 permerror (input_location
, "taking address of temporary");
5666 error ("taking address of xvalue (rvalue reference)");
5670 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
5672 tree type
= build_pointer_type (TREE_TYPE (argtype
));
5673 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5676 else if (pedantic
&& DECL_MAIN_P (arg
))
5679 /* Apparently a lot of autoconf scripts for C++ packages do this,
5680 so only complain if -Wpedantic. */
5681 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
5682 pedwarn (input_location
, OPT_Wpedantic
,
5683 "ISO C++ forbids taking address of function %<::main%>");
5684 else if (flag_pedantic_errors
)
5685 return error_mark_node
;
5688 /* Let &* cancel out to simplify resulting code. */
5689 if (INDIRECT_REF_P (arg
))
5691 arg
= TREE_OPERAND (arg
, 0);
5692 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
5694 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
5695 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5698 /* Don't let this be an lvalue. */
5703 /* ??? Cope with user tricks that amount to offsetof. */
5704 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5705 && TREE_CODE (argtype
) != METHOD_TYPE
5706 && argtype
!= unknown_type_node
5707 && (val
= get_base_address (arg
))
5708 && COMPLETE_TYPE_P (TREE_TYPE (val
))
5709 && INDIRECT_REF_P (val
)
5710 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
5712 tree type
= build_pointer_type (argtype
);
5713 return fold_convert (type
, fold_offsetof_1 (arg
));
5716 /* Handle complex lvalues (when permitted)
5717 by reduction to simpler cases. */
5718 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
5722 switch (TREE_CODE (arg
))
5726 case FIX_TRUNC_EXPR
:
5727 /* We should have handled this above in the lvalue_kind check. */
5732 arg
= BASELINK_FUNCTIONS (arg
);
5736 arg
= OVL_CURRENT (arg
);
5741 /* Turn a reference to a non-static data member into a
5742 pointer-to-member. */
5747 gcc_assert (PTRMEM_OK_P (arg
));
5749 t
= TREE_OPERAND (arg
, 1);
5750 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5752 if (complain
& tf_error
)
5753 error ("cannot create pointer to reference member %qD", t
);
5754 return error_mark_node
;
5757 type
= build_ptrmem_type (context_for_name_lookup (t
),
5759 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
5767 if (argtype
!= error_mark_node
)
5768 argtype
= build_pointer_type (argtype
);
5770 if (bitfield_p (arg
))
5772 if (complain
& tf_error
)
5773 error ("attempt to take address of bit-field");
5774 return error_mark_node
;
5777 /* In a template, we are processing a non-dependent expression
5778 so we can just form an ADDR_EXPR with the correct type. */
5779 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
5781 val
= build_address (arg
);
5782 if (TREE_CODE (arg
) == OFFSET_REF
)
5783 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
5785 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
5787 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
5789 /* We can only get here with a single static member
5791 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
5792 && DECL_STATIC_FUNCTION_P (fn
));
5793 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5794 return error_mark_node
;
5795 val
= build_address (fn
);
5796 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
5797 /* Do not lose object's side effects. */
5798 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
5799 TREE_OPERAND (arg
, 0), val
);
5803 tree object
= TREE_OPERAND (arg
, 0);
5804 tree field
= TREE_OPERAND (arg
, 1);
5805 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5806 (TREE_TYPE (object
), decl_type_context (field
)));
5807 val
= build_address (arg
);
5810 if (TYPE_PTR_P (argtype
)
5811 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
5813 build_ptrmemfunc_type (argtype
);
5814 val
= build_ptrmemfunc (argtype
, val
, 0,
5822 /* Take the address of ARG if it has one, even if it's an rvalue. */
5825 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
5827 return cp_build_addr_expr_1 (arg
, 0, complain
);
5830 /* Take the address of ARG, but only if it's an lvalue. */
5833 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
5835 return cp_build_addr_expr_1 (arg
, 1, complain
);
5838 /* C++: Must handle pointers to members.
5840 Perhaps type instantiation should be extended to handle conversion
5841 from aggregates to types we don't yet know we want? (Or are those
5842 cases typically errors which should be reported?)
5844 NOCONVERT suppresses the default promotions (such as from short to int). */
5847 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
5848 tsubst_flags_t complain
)
5850 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5852 location_t location
= EXPR_LOC_OR_LOC (arg
, input_location
);
5854 const char *errstring
= NULL
;
5856 const char *invalid_op_diag
;
5858 if (!arg
|| error_operand_p (arg
))
5859 return error_mark_node
;
5861 if ((invalid_op_diag
5862 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
5867 if (complain
& tf_error
)
5868 error (invalid_op_diag
);
5869 return error_mark_node
;
5874 case UNARY_PLUS_EXPR
:
5877 int flags
= WANT_ARITH
| WANT_ENUM
;
5878 /* Unary plus (but not unary minus) is allowed on pointers. */
5879 if (code
== UNARY_PLUS_EXPR
)
5880 flags
|= WANT_POINTER
;
5881 arg
= build_expr_type_conversion (flags
, arg
, true);
5883 errstring
= (code
== NEGATE_EXPR
5884 ? _("wrong type argument to unary minus")
5885 : _("wrong type argument to unary plus"));
5888 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5889 arg
= cp_perform_integral_promotions (arg
, complain
);
5891 /* Make sure the result is not an lvalue: a unary plus or minus
5892 expression is always a rvalue. */
5899 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5904 arg
= cp_default_conversion (arg
, complain
);
5905 if (arg
== error_mark_node
)
5906 return error_mark_node
;
5909 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
5910 | WANT_VECTOR_OR_COMPLEX
,
5912 errstring
= _("wrong type argument to bit-complement");
5913 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5915 /* Warn if the expression has boolean value. */
5916 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
5917 && warning_at (location
, OPT_Wbool_operation
,
5918 "%<~%> on an expression of type bool"))
5919 inform (location
, "did you mean to use logical not (%<!%>)?");
5920 arg
= cp_perform_integral_promotions (arg
, complain
);
5922 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
5923 arg
= mark_rvalue_use (arg
);
5927 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5928 errstring
= _("wrong type argument to abs");
5929 else if (!noconvert
)
5931 arg
= cp_default_conversion (arg
, complain
);
5932 if (arg
== error_mark_node
)
5933 return error_mark_node
;
5938 /* Conjugating a real value is a no-op, but allow it anyway. */
5939 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5940 errstring
= _("wrong type argument to conjugation");
5941 else if (!noconvert
)
5943 arg
= cp_default_conversion (arg
, complain
);
5944 if (arg
== error_mark_node
)
5945 return error_mark_node
;
5949 case TRUTH_NOT_EXPR
:
5950 if (VECTOR_TYPE_P (TREE_TYPE (arg
)))
5951 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
5952 build_zero_cst (TREE_TYPE (arg
)), complain
);
5953 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
5955 val
= invert_truthvalue_loc (input_location
, arg
);
5956 if (arg
!= error_mark_node
)
5958 errstring
= _("in argument to unary !");
5966 arg
= build_real_imag_expr (input_location
, code
, arg
);
5969 case PREINCREMENT_EXPR
:
5970 case POSTINCREMENT_EXPR
:
5971 case PREDECREMENT_EXPR
:
5972 case POSTDECREMENT_EXPR
:
5973 /* Handle complex lvalues (when permitted)
5974 by reduction to simpler cases. */
5976 val
= unary_complex_lvalue (code
, arg
);
5980 arg
= mark_lvalue_use (arg
);
5982 /* Increment or decrement the real part of the value,
5983 and don't change the imaginary part. */
5984 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5988 arg
= cp_stabilize_reference (arg
);
5989 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
5990 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
5991 real
= cp_build_unary_op (code
, real
, true, complain
);
5992 if (real
== error_mark_node
|| imag
== error_mark_node
)
5993 return error_mark_node
;
5994 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
5998 /* Report invalid types. */
6000 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
6003 if (code
== PREINCREMENT_EXPR
)
6004 errstring
= _("no pre-increment operator for type");
6005 else if (code
== POSTINCREMENT_EXPR
)
6006 errstring
= _("no post-increment operator for type");
6007 else if (code
== PREDECREMENT_EXPR
)
6008 errstring
= _("no pre-decrement operator for type");
6010 errstring
= _("no post-decrement operator for type");
6013 else if (arg
== error_mark_node
)
6014 return error_mark_node
;
6016 /* Report something read-only. */
6018 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
6019 || TREE_READONLY (arg
))
6021 if (complain
& tf_error
)
6022 cxx_readonly_error (arg
, ((code
== PREINCREMENT_EXPR
6023 || code
== POSTINCREMENT_EXPR
)
6024 ? lv_increment
: lv_decrement
));
6026 return error_mark_node
;
6031 tree declared_type
= unlowered_expr_type (arg
);
6033 argtype
= TREE_TYPE (arg
);
6035 /* ARM $5.2.5 last annotation says this should be forbidden. */
6036 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
6038 if (complain
& tf_error
)
6039 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
6040 ? G_("ISO C++ forbids incrementing an enum")
6041 : G_("ISO C++ forbids decrementing an enum"));
6043 return error_mark_node
;
6046 /* Compute the increment. */
6048 if (TYPE_PTR_P (argtype
))
6050 tree type
= complete_type (TREE_TYPE (argtype
));
6052 if (!COMPLETE_OR_VOID_TYPE_P (type
))
6054 if (complain
& tf_error
)
6055 error (((code
== PREINCREMENT_EXPR
6056 || code
== POSTINCREMENT_EXPR
))
6057 ? G_("cannot increment a pointer to incomplete type %qT")
6058 : G_("cannot decrement a pointer to incomplete type %qT"),
6059 TREE_TYPE (argtype
));
6061 return error_mark_node
;
6063 else if (!TYPE_PTROB_P (argtype
))
6065 if (complain
& tf_error
)
6066 pedwarn (input_location
, OPT_Wpointer_arith
,
6067 (code
== PREINCREMENT_EXPR
6068 || code
== POSTINCREMENT_EXPR
)
6069 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6070 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6073 return error_mark_node
;
6076 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
6079 inc
= VECTOR_TYPE_P (argtype
)
6080 ? build_one_cst (argtype
)
6083 inc
= cp_convert (argtype
, inc
, complain
);
6085 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6086 need to ask Objective-C to build the increment or decrement
6087 expression for it. */
6088 if (objc_is_property_ref (arg
))
6089 return objc_build_incr_expr_for_property_ref (input_location
, code
,
6092 /* Complain about anything else that is not a true lvalue. */
6093 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
6094 || code
== POSTINCREMENT_EXPR
)
6095 ? lv_increment
: lv_decrement
),
6097 return error_mark_node
;
6099 /* Forbid using -- or ++ in C++17 on `bool'. */
6100 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
6102 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6104 if (complain
& tf_error
)
6105 error ("use of an operand of type %qT in %<operator--%> "
6106 "is forbidden", boolean_type_node
);
6107 return error_mark_node
;
6111 if (cxx_dialect
>= cxx1z
)
6113 if (complain
& tf_error
)
6114 error ("use of an operand of type %qT in "
6115 "%<operator++%> is forbidden in C++1z",
6117 return error_mark_node
;
6119 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6120 else if (!in_system_header_at (input_location
))
6121 warning (OPT_Wdeprecated
, "use of an operand of type %qT "
6122 "in %<operator++%> is deprecated",
6125 val
= boolean_increment (code
, arg
);
6127 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6128 /* An rvalue has no cv-qualifiers. */
6129 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
6131 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
6133 TREE_SIDE_EFFECTS (val
) = 1;
6138 /* Note that this operation never does default_conversion
6139 regardless of NOCONVERT. */
6140 return cp_build_addr_expr (arg
, complain
);
6149 argtype
= TREE_TYPE (arg
);
6150 return build1 (code
, argtype
, arg
);
6153 if (complain
& tf_error
)
6154 error ("%s", errstring
);
6155 return error_mark_node
;
6158 /* Hook for the c-common bits that build a unary op. */
6160 build_unary_op (location_t
/*location*/,
6161 enum tree_code code
, tree xarg
, bool noconvert
)
6163 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
6166 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6167 for certain kinds of expressions which are not really lvalues
6168 but which we can accept as lvalues.
6170 If ARG is not a kind of expression we can handle, return
6174 unary_complex_lvalue (enum tree_code code
, tree arg
)
6176 /* Inside a template, making these kinds of adjustments is
6177 pointless; we are only concerned with the type of the
6179 if (processing_template_decl
)
6182 /* Handle (a, b) used as an "lvalue". */
6183 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
6185 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
6186 tf_warning_or_error
);
6187 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6188 TREE_OPERAND (arg
, 0), real_result
);
6191 /* Handle (a ? b : c) used as an "lvalue". */
6192 if (TREE_CODE (arg
) == COND_EXPR
6193 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6194 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6196 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6197 if (TREE_CODE (arg
) == MODIFY_EXPR
6198 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6199 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6201 tree lvalue
= TREE_OPERAND (arg
, 0);
6202 if (TREE_SIDE_EFFECTS (lvalue
))
6204 lvalue
= cp_stabilize_reference (lvalue
);
6205 arg
= build2 (TREE_CODE (arg
), TREE_TYPE (arg
),
6206 lvalue
, TREE_OPERAND (arg
, 1));
6208 return unary_complex_lvalue
6209 (code
, build2 (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
6212 if (code
!= ADDR_EXPR
)
6215 /* Handle (a = b) used as an "lvalue" for `&'. */
6216 if (TREE_CODE (arg
) == MODIFY_EXPR
6217 || TREE_CODE (arg
) == INIT_EXPR
)
6219 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
6220 tf_warning_or_error
);
6221 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6223 TREE_NO_WARNING (arg
) = 1;
6227 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
6228 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
6229 || TREE_CODE (arg
) == OFFSET_REF
)
6232 /* We permit compiler to make function calls returning
6233 objects of aggregate type look like lvalues. */
6237 if (TREE_CODE (targ
) == SAVE_EXPR
)
6238 targ
= TREE_OPERAND (targ
, 0);
6240 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6242 if (TREE_CODE (arg
) == SAVE_EXPR
)
6245 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6246 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6249 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6250 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6251 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6254 /* Don't let anything else be handled specially. */
6258 /* Mark EXP saying that we need to be able to take the
6259 address of it; it should not be allocated in a register.
6260 Value is true if successful.
6262 C++: we do not allow `current_class_ptr' to be addressable. */
6265 cxx_mark_addressable (tree exp
)
6270 switch (TREE_CODE (x
))
6277 x
= TREE_OPERAND (x
, 0);
6281 if (x
== current_class_ptr
)
6283 error ("cannot take the address of %<this%>, which is an rvalue expression");
6284 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6290 /* Caller should not be trying to mark initialized
6291 constant fields addressable. */
6292 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6293 || DECL_IN_AGGR_P (x
) == 0
6295 || DECL_EXTERNAL (x
));
6299 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6300 && !DECL_ARTIFICIAL (x
))
6302 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6305 ("address of explicit register variable %qD requested", x
);
6308 else if (extra_warnings
)
6310 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6312 TREE_ADDRESSABLE (x
) = 1;
6317 TREE_ADDRESSABLE (x
) = 1;
6321 TREE_ADDRESSABLE (x
) = 1;
6325 TREE_ADDRESSABLE (x
) = 1;
6326 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6334 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6337 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6338 tsubst_flags_t complain
)
6340 tree orig_ifexp
= ifexp
;
6341 tree orig_op1
= op1
;
6342 tree orig_op2
= op2
;
6345 if (processing_template_decl
)
6347 /* The standard says that the expression is type-dependent if
6348 IFEXP is type-dependent, even though the eventual type of the
6349 expression doesn't dependent on IFEXP. */
6350 if (type_dependent_expression_p (ifexp
)
6351 /* As a GNU extension, the middle operand may be omitted. */
6352 || (op1
&& type_dependent_expression_p (op1
))
6353 || type_dependent_expression_p (op2
))
6354 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6355 ifexp
= build_non_dependent_expr (ifexp
);
6357 op1
= build_non_dependent_expr (op1
);
6358 op2
= build_non_dependent_expr (op2
);
6361 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6362 if (processing_template_decl
&& expr
!= error_mark_node
)
6364 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6365 orig_ifexp
, orig_op1
, orig_op2
);
6366 /* Remember that the result is an lvalue or xvalue. */
6367 if (glvalue_p (expr
) && !glvalue_p (min
))
6368 TREE_TYPE (min
) = cp_build_reference_type (TREE_TYPE (min
),
6370 expr
= convert_from_reference (min
);
6375 /* Given a list of expressions, return a compound expression
6376 that performs them all and returns the value of the last of them. */
6379 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6380 tsubst_flags_t complain
)
6382 tree expr
= TREE_VALUE (list
);
6384 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6385 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6387 if (complain
& tf_error
)
6388 pedwarn (EXPR_LOC_OR_LOC (expr
, input_location
), 0,
6389 "list-initializer for non-class type must not "
6390 "be parenthesized");
6392 return error_mark_node
;
6395 if (TREE_CHAIN (list
))
6397 if (complain
& tf_error
)
6401 permerror (input_location
, "expression list treated as compound "
6402 "expression in initializer");
6405 permerror (input_location
, "expression list treated as compound "
6406 "expression in mem-initializer");
6409 permerror (input_location
, "expression list treated as compound "
6410 "expression in functional cast");
6416 return error_mark_node
;
6418 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6419 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6420 expr
, TREE_VALUE (list
), complain
);
6426 /* Like build_x_compound_expr_from_list, but using a VEC. */
6429 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6430 tsubst_flags_t complain
)
6432 if (vec_safe_is_empty (vec
))
6434 else if (vec
->length () == 1)
6444 if (complain
& tf_error
)
6445 permerror (input_location
,
6446 "%s expression list treated as compound expression",
6449 return error_mark_node
;
6453 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6454 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6461 /* Handle overloading of the ',' operator when needed. */
6464 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6465 tsubst_flags_t complain
)
6468 tree orig_op1
= op1
;
6469 tree orig_op2
= op2
;
6470 tree overload
= NULL_TREE
;
6472 if (processing_template_decl
)
6474 if (type_dependent_expression_p (op1
)
6475 || type_dependent_expression_p (op2
))
6476 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6477 op1
= build_non_dependent_expr (op1
);
6478 op2
= build_non_dependent_expr (op2
);
6481 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6482 NULL_TREE
, &overload
, complain
);
6484 result
= cp_build_compound_expr (op1
, op2
, complain
);
6486 if (processing_template_decl
&& result
!= error_mark_node
)
6488 if (overload
!= NULL_TREE
)
6489 return (build_min_non_dep_op_overload
6490 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
6492 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6498 /* Like cp_build_compound_expr, but for the c-common bits. */
6501 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6503 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6506 /* Build a compound expression. */
6509 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6511 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6513 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6514 return error_mark_node
;
6517 && (TREE_CODE (lhs
) == CILK_SPAWN_STMT
6518 || TREE_CODE (rhs
) == CILK_SPAWN_STMT
))
6520 location_t loc
= (EXPR_HAS_LOCATION (lhs
) ? EXPR_LOCATION (lhs
)
6521 : EXPR_LOCATION (rhs
));
6523 "spawned function call cannot be part of a comma expression");
6524 return error_mark_node
;
6527 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6529 /* If the rhs is a TARGET_EXPR, then build the compound
6530 expression inside the target_expr's initializer. This
6531 helps the compiler to eliminate unnecessary temporaries. */
6532 tree init
= TREE_OPERAND (rhs
, 1);
6534 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6535 TREE_OPERAND (rhs
, 1) = init
;
6540 if (type_unknown_p (rhs
))
6542 if (complain
& tf_error
)
6543 error ("no context to resolve type of %qE", rhs
);
6544 return error_mark_node
;
6547 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6550 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6551 casts away constness. CAST gives the type of cast. Returns true
6552 if the cast is ill-formed, false if it is well-formed.
6554 ??? This function warns for casting away any qualifier not just
6555 const. We would like to specify exactly what qualifiers are casted
6560 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6561 enum tree_code cast
, tsubst_flags_t complain
)
6563 /* C-style casts are allowed to cast away constness. With
6564 WARN_CAST_QUAL, we still want to issue a warning. */
6565 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6568 if (!casts_away_constness (src_type
, dest_type
, complain
))
6574 if (complain
& tf_warning
)
6575 warning (OPT_Wcast_qual
,
6576 "cast from type %qT to type %qT casts away qualifiers",
6577 src_type
, dest_type
);
6580 case STATIC_CAST_EXPR
:
6581 if (complain
& tf_error
)
6582 error ("static_cast from type %qT to type %qT casts away qualifiers",
6583 src_type
, dest_type
);
6586 case REINTERPRET_CAST_EXPR
:
6587 if (complain
& tf_error
)
6588 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6589 src_type
, dest_type
);
6598 Warns if the cast from expression EXPR to type TYPE is useless.
6601 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6603 if (warn_useless_cast
6604 && complain
& tf_warning
)
6606 if ((TREE_CODE (type
) == REFERENCE_TYPE
6607 && (TYPE_REF_IS_RVALUE (type
)
6608 ? xvalue_p (expr
) : lvalue_p (expr
))
6609 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6610 || same_type_p (TREE_TYPE (expr
), type
))
6611 warning (OPT_Wuseless_cast
, "useless cast to type %qT", type
);
6615 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6616 (another pointer-to-member type in the same hierarchy) and return
6617 the converted expression. If ALLOW_INVERSE_P is permitted, a
6618 pointer-to-derived may be converted to pointer-to-base; otherwise,
6619 only the other direction is permitted. If C_CAST_P is true, this
6620 conversion is taking place as part of a C-style cast. */
6623 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6624 bool c_cast_p
, tsubst_flags_t complain
)
6626 if (TYPE_PTRDATAMEM_P (type
))
6630 if (TREE_CODE (expr
) == PTRMEM_CST
)
6631 expr
= cplus_expand_constant (expr
);
6632 delta
= get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
)),
6633 TYPE_PTRMEM_CLASS_TYPE (type
),
6635 c_cast_p
, complain
);
6636 if (delta
== error_mark_node
)
6637 return error_mark_node
;
6639 if (!integer_zerop (delta
))
6641 tree cond
, op1
, op2
;
6643 cond
= cp_build_binary_op (input_location
,
6646 build_int_cst (TREE_TYPE (expr
), -1),
6648 op1
= build_nop (ptrdiff_type_node
, expr
);
6649 op2
= cp_build_binary_op (input_location
,
6650 PLUS_EXPR
, op1
, delta
,
6653 expr
= fold_build3_loc (input_location
,
6654 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
6658 return build_nop (type
, expr
);
6661 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
6662 allow_inverse_p
, c_cast_p
, complain
);
6665 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6666 this static_cast is being attempted as one of the possible casts
6667 allowed by a C-style cast. (In that case, accessibility of base
6668 classes is not considered, and it is OK to cast away
6669 constness.) Return the result of the cast. *VALID_P is set to
6670 indicate whether or not the cast was valid. */
6673 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6674 bool *valid_p
, tsubst_flags_t complain
)
6680 /* Assume the cast is valid. */
6683 intype
= unlowered_expr_type (expr
);
6685 /* Save casted types in the function's used types hash table. */
6686 used_types_insert (type
);
6688 /* [expr.static.cast]
6690 An lvalue of type "cv1 B", where B is a class type, can be cast
6691 to type "reference to cv2 D", where D is a class derived (clause
6692 _class.derived_) from B, if a valid standard conversion from
6693 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6694 same cv-qualification as, or greater cv-qualification than, cv1,
6695 and B is not a virtual base class of D. */
6696 /* We check this case before checking the validity of "TYPE t =
6697 EXPR;" below because for this case:
6700 struct D : public B { D(const B&); };
6702 void f() { static_cast<const D&>(b); }
6704 we want to avoid constructing a new D. The standard is not
6705 completely clear about this issue, but our interpretation is
6706 consistent with other compilers. */
6707 if (TREE_CODE (type
) == REFERENCE_TYPE
6708 && CLASS_TYPE_P (TREE_TYPE (type
))
6709 && CLASS_TYPE_P (intype
)
6710 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
6711 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
6712 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
6713 build_pointer_type (TYPE_MAIN_VARIANT
6714 (TREE_TYPE (type
))),
6717 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6721 /* There is a standard conversion from "D*" to "B*" even if "B"
6722 is ambiguous or inaccessible. If this is really a
6723 static_cast, then we check both for inaccessibility and
6724 ambiguity. However, if this is a static_cast being performed
6725 because the user wrote a C-style cast, then accessibility is
6727 base
= lookup_base (TREE_TYPE (type
), intype
,
6728 c_cast_p
? ba_unique
: ba_check
,
6730 expr
= build_address (expr
);
6732 if (flag_sanitize
& SANITIZE_VPTR
)
6735 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
6741 /* Convert from "B*" to "D*". This function will check that "B"
6742 is not a virtual base of "D". */
6743 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6746 /* Convert the pointer to a reference -- but then remember that
6747 there are no expressions with reference type in C++.
6749 We call rvalue so that there's an actual tree code
6750 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6751 is a variable with the same type, the conversion would get folded
6752 away, leaving just the variable and causing lvalue_kind to give
6753 the wrong answer. */
6754 return convert_from_reference (rvalue (cp_fold_convert (type
, expr
)));
6757 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6758 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6759 if (TREE_CODE (type
) == REFERENCE_TYPE
6760 && TYPE_REF_IS_RVALUE (type
)
6761 && (clk
= real_lvalue_p (expr
))
6762 && reference_related_p (TREE_TYPE (type
), intype
)
6763 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6765 if (clk
== clk_ordinary
)
6767 /* Handle the (non-bit-field) lvalue case here by casting to
6768 lvalue reference and then changing it to an rvalue reference.
6769 Casting an xvalue to rvalue reference will be handled by the
6771 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
6772 result
= (perform_direct_initialization_if_possible
6773 (lref
, expr
, c_cast_p
, complain
));
6774 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
6775 return convert_from_reference (result
);
6778 /* For a bit-field or packed field, bind to a temporary. */
6779 expr
= rvalue (expr
);
6782 /* Resolve overloaded address here rather than once in
6783 implicit_conversion and again in the inverse code below. */
6784 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
6786 expr
= instantiate_type (type
, expr
, complain
);
6787 intype
= TREE_TYPE (expr
);
6790 /* [expr.static.cast]
6792 Any expression can be explicitly converted to type cv void. */
6793 if (VOID_TYPE_P (type
))
6794 return convert_to_void (expr
, ICV_CAST
, complain
);
6797 An abstract class shall not be used ... as the type of an explicit
6799 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
6800 return error_mark_node
;
6802 /* [expr.static.cast]
6804 An expression e can be explicitly converted to a type T using a
6805 static_cast of the form static_cast<T>(e) if the declaration T
6806 t(e);" is well-formed, for some invented temporary variable
6808 result
= perform_direct_initialization_if_possible (type
, expr
,
6809 c_cast_p
, complain
);
6812 result
= convert_from_reference (result
);
6814 /* [expr.static.cast]
6816 If T is a reference type, the result is an lvalue; otherwise,
6817 the result is an rvalue. */
6818 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6820 result
= rvalue (result
);
6822 if (result
== expr
&& SCALAR_TYPE_P (type
))
6823 /* Leave some record of the cast. */
6824 result
= build_nop (type
, expr
);
6829 /* [expr.static.cast]
6831 The inverse of any standard conversion sequence (clause _conv_),
6832 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6833 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6834 (_conv.bool_) conversions, can be performed explicitly using
6835 static_cast subject to the restriction that the explicit
6836 conversion does not cast away constness (_expr.const.cast_), and
6837 the following additional rules for specific cases: */
6838 /* For reference, the conversions not excluded are: integral
6839 promotions, floating point promotion, integral conversions,
6840 floating point conversions, floating-integral conversions,
6841 pointer conversions, and pointer to member conversions. */
6844 A value of integral _or enumeration_ type can be explicitly
6845 converted to an enumeration type. */
6846 /* The effect of all that is that any conversion between any two
6847 types which are integral, floating, or enumeration types can be
6849 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6850 || SCALAR_FLOAT_TYPE_P (type
))
6851 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
6852 || SCALAR_FLOAT_TYPE_P (intype
)))
6853 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
6855 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
6856 && CLASS_TYPE_P (TREE_TYPE (type
))
6857 && CLASS_TYPE_P (TREE_TYPE (intype
))
6858 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6859 (TREE_TYPE (intype
))),
6860 build_pointer_type (TYPE_MAIN_VARIANT
6861 (TREE_TYPE (type
))),
6867 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6869 return error_mark_node
;
6870 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
6871 c_cast_p
? ba_unique
: ba_check
,
6873 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6876 if (flag_sanitize
& SANITIZE_VPTR
)
6879 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
6885 return cp_fold_convert (type
, expr
);
6888 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6889 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6896 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
6897 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
6899 if (TYPE_PTRDATAMEM_P (type
))
6901 t1
= (build_ptrmem_type
6903 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
6904 t2
= (build_ptrmem_type
6906 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
6913 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
6916 && check_for_casting_away_constness (intype
, type
,
6919 return error_mark_node
;
6920 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
6921 c_cast_p
, complain
);
6925 /* [expr.static.cast]
6927 An rvalue of type "pointer to cv void" can be explicitly
6928 converted to a pointer to object type. A value of type pointer
6929 to object converted to "pointer to cv void" and back to the
6930 original pointer type will have its original value. */
6931 if (TYPE_PTR_P (intype
)
6932 && VOID_TYPE_P (TREE_TYPE (intype
))
6933 && TYPE_PTROB_P (type
))
6936 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6938 return error_mark_node
;
6939 return build_nop (type
, expr
);
6943 return error_mark_node
;
6946 /* Return an expression representing static_cast<TYPE>(EXPR). */
6949 build_static_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6954 if (type
== error_mark_node
|| expr
== error_mark_node
)
6955 return error_mark_node
;
6957 if (processing_template_decl
)
6959 expr
= build_min (STATIC_CAST_EXPR
, type
, expr
);
6960 /* We don't know if it will or will not have side effects. */
6961 TREE_SIDE_EFFECTS (expr
) = 1;
6962 return convert_from_reference (expr
);
6965 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6966 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6967 if (TREE_CODE (type
) != REFERENCE_TYPE
6968 && TREE_CODE (expr
) == NOP_EXPR
6969 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6970 expr
= TREE_OPERAND (expr
, 0);
6972 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
6976 if (result
!= error_mark_node
)
6977 maybe_warn_about_useless_cast (type
, expr
, complain
);
6981 if (complain
& tf_error
)
6982 error ("invalid static_cast from type %qT to type %qT",
6983 TREE_TYPE (expr
), type
);
6984 return error_mark_node
;
6987 /* EXPR is an expression with member function or pointer-to-member
6988 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6989 not permitted by ISO C++, but we accept it in some modes. If we
6990 are not in one of those modes, issue a diagnostic. Return the
6991 converted expression. */
6994 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
6999 intype
= TREE_TYPE (expr
);
7000 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
7001 || TREE_CODE (intype
) == METHOD_TYPE
);
7003 if (!(complain
& tf_warning_or_error
))
7004 return error_mark_node
;
7006 if (pedantic
|| warn_pmf2ptr
)
7007 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
7008 "converting from %qT to %qT", intype
, type
);
7010 if (TREE_CODE (intype
) == METHOD_TYPE
)
7011 expr
= build_addr_func (expr
, complain
);
7012 else if (TREE_CODE (expr
) == PTRMEM_CST
)
7013 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
7016 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
7017 decl
= build_address (decl
);
7018 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
7021 if (expr
== error_mark_node
)
7022 return error_mark_node
;
7024 return build_nop (type
, expr
);
7027 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7028 If C_CAST_P is true, this reinterpret cast is being done as part of
7029 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7030 indicate whether or not reinterpret_cast was valid. */
7033 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7034 bool *valid_p
, tsubst_flags_t complain
)
7038 /* Assume the cast is invalid. */
7042 if (type
== error_mark_node
|| error_operand_p (expr
))
7043 return error_mark_node
;
7045 intype
= TREE_TYPE (expr
);
7047 /* Save casted types in the function's used types hash table. */
7048 used_types_insert (type
);
7050 /* [expr.reinterpret.cast]
7051 An lvalue expression of type T1 can be cast to the type
7052 "reference to T2" if an expression of type "pointer to T1" can be
7053 explicitly converted to the type "pointer to T2" using a
7054 reinterpret_cast. */
7055 if (TREE_CODE (type
) == REFERENCE_TYPE
)
7057 if (! lvalue_p (expr
))
7059 if (complain
& tf_error
)
7060 error ("invalid cast of an rvalue expression of type "
7063 return error_mark_node
;
7066 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7067 "B" are related class types; the reinterpret_cast does not
7068 adjust the pointer. */
7069 if (TYPE_PTR_P (intype
)
7070 && (complain
& tf_warning
)
7071 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
7072 COMPARE_BASE
| COMPARE_DERIVED
)))
7073 warning (0, "casting %qT to %qT does not dereference pointer",
7076 expr
= cp_build_addr_expr (expr
, complain
);
7078 if (warn_strict_aliasing
> 2)
7079 strict_aliasing_warning (TREE_TYPE (expr
), type
, expr
);
7081 if (expr
!= error_mark_node
)
7082 expr
= build_reinterpret_cast_1
7083 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
7085 if (expr
!= error_mark_node
)
7086 /* cp_build_indirect_ref isn't right for rvalue refs. */
7087 expr
= convert_from_reference (fold_convert (type
, expr
));
7091 /* As a G++ extension, we consider conversions from member
7092 functions, and pointers to member functions to
7093 pointer-to-function and pointer-to-void types. If
7094 -Wno-pmf-conversions has not been specified,
7095 convert_member_func_to_ptr will issue an error message. */
7096 if ((TYPE_PTRMEMFUNC_P (intype
)
7097 || TREE_CODE (intype
) == METHOD_TYPE
)
7098 && TYPE_PTR_P (type
)
7099 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
7100 || VOID_TYPE_P (TREE_TYPE (type
))))
7101 return convert_member_func_to_ptr (type
, expr
, complain
);
7103 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7104 array-to-pointer, and function-to-pointer conversions are
7106 expr
= decay_conversion (expr
, complain
);
7108 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7109 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7110 if (TREE_CODE (expr
) == NOP_EXPR
7111 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7112 expr
= TREE_OPERAND (expr
, 0);
7114 if (error_operand_p (expr
))
7115 return error_mark_node
;
7117 intype
= TREE_TYPE (expr
);
7119 /* [expr.reinterpret.cast]
7120 A pointer can be converted to any integral type large enough to
7121 hold it. ... A value of type std::nullptr_t can be converted to
7122 an integral type; the conversion has the same meaning and
7123 validity as a conversion of (void*)0 to the integral type. */
7124 if (CP_INTEGRAL_TYPE_P (type
)
7125 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
7127 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
7129 if (complain
& tf_error
)
7130 permerror (input_location
, "cast from %qT to %qT loses precision",
7133 return error_mark_node
;
7135 if (NULLPTR_TYPE_P (intype
))
7136 return build_int_cst (type
, 0);
7138 /* [expr.reinterpret.cast]
7139 A value of integral or enumeration type can be explicitly
7140 converted to a pointer. */
7141 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
7144 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7145 || TYPE_PTR_OR_PTRMEM_P (type
))
7146 && same_type_p (type
, intype
))
7148 return rvalue (expr
);
7149 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
7150 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7151 return build_nop (type
, expr
);
7152 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7153 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
7158 && check_for_casting_away_constness (intype
, type
,
7159 REINTERPRET_CAST_EXPR
,
7161 return error_mark_node
;
7162 /* Warn about possible alignment problems. */
7163 if (STRICT_ALIGNMENT
&& warn_cast_align
7164 && (complain
& tf_warning
)
7165 && !VOID_TYPE_P (type
)
7166 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
7167 && COMPLETE_TYPE_P (TREE_TYPE (type
))
7168 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
7169 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (intype
)))
7170 warning (OPT_Wcast_align
, "cast from %qT to %qT "
7171 "increases required alignment of target type", intype
, type
);
7173 /* We need to strip nops here, because the front end likes to
7174 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7176 if (warn_strict_aliasing
<= 2)
7177 strict_aliasing_warning (intype
, type
, sexpr
);
7179 return build_nop (type
, expr
);
7181 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
7182 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
7184 if (complain
& tf_warning
)
7185 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7186 object pointer type or vice versa is conditionally-supported." */
7187 warning (OPT_Wconditionally_supported
,
7188 "casting between pointer-to-function and pointer-to-object "
7189 "is conditionally-supported");
7190 return build_nop (type
, expr
);
7192 else if (VECTOR_TYPE_P (type
))
7193 return convert_to_vector (type
, expr
);
7194 else if (VECTOR_TYPE_P (intype
)
7195 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7196 return convert_to_integer_nofold (type
, expr
);
7201 if (complain
& tf_error
)
7202 error ("invalid cast from type %qT to type %qT", intype
, type
);
7203 return error_mark_node
;
7206 return cp_convert (type
, expr
, complain
);
7210 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7214 if (type
== error_mark_node
|| expr
== error_mark_node
)
7215 return error_mark_node
;
7217 if (processing_template_decl
)
7219 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7221 if (!TREE_SIDE_EFFECTS (t
)
7222 && type_dependent_expression_p (expr
))
7223 /* There might turn out to be side effects inside expr. */
7224 TREE_SIDE_EFFECTS (t
) = 1;
7225 return convert_from_reference (t
);
7228 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
7229 /*valid_p=*/NULL
, complain
);
7230 if (r
!= error_mark_node
)
7231 maybe_warn_about_useless_cast (type
, expr
, complain
);
7235 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7236 return an appropriate expression. Otherwise, return
7237 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7238 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7239 performing a C-style cast, its value upon return will indicate
7240 whether or not the conversion succeeded. */
7243 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
7247 tree reference_type
;
7249 /* Callers are responsible for handling error_mark_node as a
7250 destination type. */
7251 gcc_assert (dst_type
!= error_mark_node
);
7252 /* In a template, callers should be building syntactic
7253 representations of casts, not using this machinery. */
7254 gcc_assert (!processing_template_decl
);
7256 /* Assume the conversion is invalid. */
7260 if (!POINTER_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
7262 if (complain
& tf_error
)
7263 error ("invalid use of const_cast with type %qT, "
7264 "which is not a pointer, "
7265 "reference, nor a pointer-to-data-member type", dst_type
);
7266 return error_mark_node
;
7269 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
7271 if (complain
& tf_error
)
7272 error ("invalid use of const_cast with type %qT, which is a pointer "
7273 "or reference to a function type", dst_type
);
7274 return error_mark_node
;
7277 /* Save casted types in the function's used types hash table. */
7278 used_types_insert (dst_type
);
7280 src_type
= TREE_TYPE (expr
);
7281 /* Expressions do not really have reference types. */
7282 if (TREE_CODE (src_type
) == REFERENCE_TYPE
)
7283 src_type
= TREE_TYPE (src_type
);
7285 /* [expr.const.cast]
7287 For two object types T1 and T2, if a pointer to T1 can be explicitly
7288 converted to the type "pointer to T2" using a const_cast, then the
7289 following conversions can also be made:
7291 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7292 type T2 using the cast const_cast<T2&>;
7294 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7295 type T2 using the cast const_cast<T2&&>; and
7297 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7298 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7300 if (TREE_CODE (dst_type
) == REFERENCE_TYPE
)
7302 reference_type
= dst_type
;
7303 if (!TYPE_REF_IS_RVALUE (dst_type
)
7309 if (complain
& tf_error
)
7310 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7311 src_type
, dst_type
);
7312 return error_mark_node
;
7314 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7315 src_type
= build_pointer_type (src_type
);
7319 reference_type
= NULL_TREE
;
7320 /* If the destination type is not a reference type, the
7321 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7322 conversions are performed. */
7323 src_type
= type_decays_to (src_type
);
7324 if (src_type
== error_mark_node
)
7325 return error_mark_node
;
7328 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7330 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7335 /* This cast is actually a C-style cast. Issue a warning if
7336 the user is making a potentially unsafe cast. */
7337 check_for_casting_away_constness (src_type
, dst_type
,
7338 CAST_EXPR
, complain
);
7342 expr
= cp_build_addr_expr (expr
, complain
);
7343 if (expr
== error_mark_node
)
7344 return error_mark_node
;
7345 expr
= build_nop (reference_type
, expr
);
7346 return convert_from_reference (expr
);
7350 expr
= decay_conversion (expr
, complain
);
7351 if (expr
== error_mark_node
)
7352 return error_mark_node
;
7354 /* build_c_cast puts on a NOP_EXPR to make the result not an
7355 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7356 non-lvalue context. */
7357 if (TREE_CODE (expr
) == NOP_EXPR
7358 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7359 expr
= TREE_OPERAND (expr
, 0);
7360 return build_nop (dst_type
, expr
);
7364 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7365 TREE_TYPE (src_type
)))
7366 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7370 if (complain
& tf_error
)
7371 error ("invalid const_cast from type %qT to type %qT",
7372 src_type
, dst_type
);
7373 return error_mark_node
;
7377 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7381 if (type
== error_mark_node
|| error_operand_p (expr
))
7382 return error_mark_node
;
7384 if (processing_template_decl
)
7386 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7388 if (!TREE_SIDE_EFFECTS (t
)
7389 && type_dependent_expression_p (expr
))
7390 /* There might turn out to be side effects inside expr. */
7391 TREE_SIDE_EFFECTS (t
) = 1;
7392 return convert_from_reference (t
);
7395 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7396 if (r
!= error_mark_node
)
7397 maybe_warn_about_useless_cast (type
, expr
, complain
);
7401 /* Like cp_build_c_cast, but for the c-common bits. */
7404 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7406 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7409 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7410 preserve location information even for tree nodes that don't
7414 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
7416 cp_expr result
= cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7417 result
.set_location (loc
);
7421 /* Build an expression representing an explicit C-style cast to type
7422 TYPE of expression EXPR. */
7425 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7431 if (type
== error_mark_node
|| error_operand_p (expr
))
7432 return error_mark_node
;
7434 if (processing_template_decl
)
7436 tree t
= build_min (CAST_EXPR
, type
,
7437 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7438 /* We don't know if it will or will not have side effects. */
7439 TREE_SIDE_EFFECTS (t
) = 1;
7440 return convert_from_reference (t
);
7443 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7444 'Class') should always be retained, because this information aids
7445 in method lookup. */
7446 if (objc_is_object_ptr (type
)
7447 && objc_is_object_ptr (TREE_TYPE (expr
)))
7448 return build_nop (type
, expr
);
7450 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7451 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7452 if (TREE_CODE (type
) != REFERENCE_TYPE
7453 && TREE_CODE (value
) == NOP_EXPR
7454 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7455 value
= TREE_OPERAND (value
, 0);
7457 if (TREE_CODE (type
) == ARRAY_TYPE
)
7459 /* Allow casting from T1* to T2[] because Cfront allows it.
7460 NIHCL uses it. It is not valid ISO C++ however. */
7461 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7463 if (complain
& tf_error
)
7464 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7466 return error_mark_node
;
7467 type
= build_pointer_type (TREE_TYPE (type
));
7471 if (complain
& tf_error
)
7472 error ("ISO C++ forbids casting to an array type %qT", type
);
7473 return error_mark_node
;
7477 if (TREE_CODE (type
) == FUNCTION_TYPE
7478 || TREE_CODE (type
) == METHOD_TYPE
)
7480 if (complain
& tf_error
)
7481 error ("invalid cast to function type %qT", type
);
7482 return error_mark_node
;
7485 if (TYPE_PTR_P (type
)
7486 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7487 /* Casting to an integer of smaller size is an error detected elsewhere. */
7488 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7489 /* Don't warn about converting any constant. */
7490 && !TREE_CONSTANT (value
))
7491 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7492 "cast to pointer from integer of different size");
7494 /* A C-style cast can be a const_cast. */
7495 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7499 if (result
!= error_mark_node
)
7500 maybe_warn_about_useless_cast (type
, value
, complain
);
7504 /* Or a static cast. */
7505 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7506 &valid_p
, complain
);
7507 /* Or a reinterpret_cast. */
7509 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7510 &valid_p
, complain
);
7511 /* The static_cast or reinterpret_cast may be followed by a
7514 /* A valid cast may result in errors if, for example, a
7515 conversion to an ambiguous base class is required. */
7516 && !error_operand_p (result
))
7520 maybe_warn_about_useless_cast (type
, value
, complain
);
7522 /* Non-class rvalues always have cv-unqualified type. */
7523 if (!CLASS_TYPE_P (type
))
7524 type
= TYPE_MAIN_VARIANT (type
);
7525 result_type
= TREE_TYPE (result
);
7526 if (!CLASS_TYPE_P (result_type
) && TREE_CODE (type
) != REFERENCE_TYPE
)
7527 result_type
= TYPE_MAIN_VARIANT (result_type
);
7528 /* If the type of RESULT does not match TYPE, perform a
7529 const_cast to make it match. If the static_cast or
7530 reinterpret_cast succeeded, we will differ by at most
7531 cv-qualification, so the follow-on const_cast is guaranteed
7533 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
7535 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
7536 gcc_assert (valid_p
);
7541 return error_mark_node
;
7544 /* For use from the C common bits. */
7546 build_modify_expr (location_t location
,
7547 tree lhs
, tree
/*lhs_origtype*/,
7548 enum tree_code modifycode
,
7549 location_t
/*rhs_location*/, tree rhs
,
7550 tree
/*rhs_origtype*/)
7552 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
7553 tf_warning_or_error
);
7556 /* Build an assignment expression of lvalue LHS from value RHS.
7557 MODIFYCODE is the code for a binary operator that we use
7558 to combine the old value of LHS with RHS to get the new value.
7559 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7561 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7564 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7565 tree rhs
, tsubst_flags_t complain
)
7569 tree lhstype
= TREE_TYPE (lhs
);
7570 tree olhstype
= lhstype
;
7571 bool plain_assign
= (modifycode
== NOP_EXPR
);
7573 /* Avoid duplicate error messages from operands that had errors. */
7574 if (error_operand_p (lhs
) || error_operand_p (rhs
))
7575 return error_mark_node
;
7577 /* Handle control structure constructs used as "lvalues". Note that we
7578 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7579 switch (TREE_CODE (lhs
))
7581 /* Handle --foo = 5; as these are valid constructs in C++. */
7582 case PREDECREMENT_EXPR
:
7583 case PREINCREMENT_EXPR
:
7584 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7585 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7586 cp_stabilize_reference (TREE_OPERAND (lhs
, 0)),
7587 TREE_OPERAND (lhs
, 1));
7588 lhs
= build2 (COMPOUND_EXPR
, lhstype
, lhs
, TREE_OPERAND (lhs
, 0));
7592 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7593 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7594 cp_stabilize_reference (TREE_OPERAND (lhs
, 0)),
7595 TREE_OPERAND (lhs
, 1));
7596 lhs
= build2 (COMPOUND_EXPR
, lhstype
, lhs
, TREE_OPERAND (lhs
, 0));
7601 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7602 when neither operand has side-effects. */
7603 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7604 return error_mark_node
;
7606 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
7607 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
7609 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
7610 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
7612 TREE_OPERAND (lhs
, 0),
7613 TREE_OPERAND (lhs
, 1)),
7614 TREE_OPERAND (lhs
, 0),
7615 TREE_OPERAND (lhs
, 1));
7618 /* Handle (a ? b : c) used as an "lvalue". */
7621 /* Produce (a ? (b = rhs) : (c = rhs))
7622 except that the RHS goes through a save-expr
7623 so the code to compute it is only emitted once. */
7625 tree preeval
= NULL_TREE
;
7627 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
7629 if (complain
& tf_error
)
7630 error ("void value not ignored as it ought to be");
7631 return error_mark_node
;
7634 rhs
= stabilize_expr (rhs
, &preeval
);
7636 /* Check this here to avoid odd errors when trying to convert
7637 a throw to the type of the COND_EXPR. */
7638 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7639 return error_mark_node
;
7641 cond
= build_conditional_expr
7642 (input_location
, TREE_OPERAND (lhs
, 0),
7643 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 1),
7644 modifycode
, rhs
, complain
),
7645 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 2),
7646 modifycode
, rhs
, complain
),
7649 if (cond
== error_mark_node
)
7651 /* Make sure the code to compute the rhs comes out
7652 before the split. */
7654 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (lhs
), preeval
, cond
);
7662 if (modifycode
== INIT_EXPR
)
7664 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7665 /* Do the default thing. */;
7666 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
7668 /* Compound literal. */
7669 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
7670 /* Call convert to generate an error; see PR 11063. */
7671 rhs
= convert (lhstype
, rhs
);
7672 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
7673 TREE_SIDE_EFFECTS (result
) = 1;
7676 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
7677 /* Do the default thing. */;
7680 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
7681 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
7682 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
7684 release_tree_vector (rhs_vec
);
7685 if (result
== NULL_TREE
)
7686 return error_mark_node
;
7692 lhs
= require_complete_type_sfinae (lhs
, complain
);
7693 if (lhs
== error_mark_node
)
7694 return error_mark_node
;
7696 if (modifycode
== NOP_EXPR
)
7698 if (c_dialect_objc ())
7700 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
7705 /* `operator=' is not an inheritable operator. */
7706 if (! MAYBE_CLASS_TYPE_P (lhstype
))
7707 /* Do the default thing. */;
7710 result
= build_new_op (input_location
, MODIFY_EXPR
,
7711 LOOKUP_NORMAL
, lhs
, rhs
,
7712 make_node (NOP_EXPR
), /*overload=*/NULL
,
7714 if (result
== NULL_TREE
)
7715 return error_mark_node
;
7722 tree init
= NULL_TREE
;
7724 /* A binary op has been requested. Combine the old LHS
7725 value with the RHS producing the value we should actually
7726 store into the LHS. */
7727 gcc_assert (!((TREE_CODE (lhstype
) == REFERENCE_TYPE
7728 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
7729 || MAYBE_CLASS_TYPE_P (lhstype
)));
7731 /* Preevaluate the RHS to make sure its evaluation is complete
7732 before the lvalue-to-rvalue conversion of the LHS:
7734 [expr.ass] With respect to an indeterminately-sequenced
7735 function call, the operation of a compound assignment is a
7736 single evaluation. [ Note: Therefore, a function call shall
7737 not intervene between the lvalue-to-rvalue conversion and the
7738 side effect associated with any single compound assignment
7739 operator. -- end note ] */
7740 lhs
= cp_stabilize_reference (lhs
);
7742 rhs
= stabilize_expr (rhs
, &init
);
7743 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
7744 if (newrhs
== error_mark_node
)
7746 if (complain
& tf_error
)
7747 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
7748 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
7749 return error_mark_node
;
7753 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
7755 /* Now it looks like a plain assignment. */
7756 modifycode
= NOP_EXPR
;
7757 if (c_dialect_objc ())
7759 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
7764 gcc_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
);
7765 gcc_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
);
7768 /* The left-hand side must be an lvalue. */
7769 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7770 return error_mark_node
;
7772 /* Warn about modifying something that is `const'. Don't warn if
7773 this is initialization. */
7774 if (modifycode
!= INIT_EXPR
7775 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
7776 /* Functions are not modifiable, even though they are
7778 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
7779 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
7780 /* If it's an aggregate and any field is const, then it is
7781 effectively const. */
7782 || (CLASS_TYPE_P (lhstype
)
7783 && C_TYPE_FIELDS_READONLY (lhstype
))))
7785 if (complain
& tf_error
)
7786 cxx_readonly_error (lhs
, lv_assign
);
7788 return error_mark_node
;
7791 /* If storing into a structure or union member, it may have been given a
7792 lowered bitfield type. We need to convert to the declared type first,
7793 so retrieve it now. */
7795 olhstype
= unlowered_expr_type (lhs
);
7797 /* Convert new value to destination type. */
7799 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
7803 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
7805 if (modifycode
!= INIT_EXPR
)
7807 if (complain
& tf_error
)
7808 error ("assigning to an array from an initializer list");
7809 return error_mark_node
;
7811 if (check_array_initializer (lhs
, lhstype
, newrhs
))
7812 return error_mark_node
;
7813 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7814 if (newrhs
== error_mark_node
)
7815 return error_mark_node
;
7818 /* C++11 8.5/17: "If the destination type is an array of characters,
7819 an array of char16_t, an array of char32_t, or an array of wchar_t,
7820 and the initializer is a string literal...". */
7821 else if (TREE_CODE (newrhs
) == STRING_CST
7822 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
7823 && modifycode
== INIT_EXPR
)
7825 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7826 if (newrhs
== error_mark_node
)
7827 return error_mark_node
;
7830 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
7831 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
7833 if (complain
& tf_error
)
7834 error ("incompatible types in assignment of %qT to %qT",
7835 TREE_TYPE (rhs
), lhstype
);
7836 return error_mark_node
;
7839 /* Allow array assignment in compiler-generated code. */
7840 else if (!current_function_decl
7841 || !DECL_DEFAULTED_FN (current_function_decl
))
7843 /* This routine is used for both initialization and assignment.
7844 Make sure the diagnostic message differentiates the context. */
7845 if (complain
& tf_error
)
7847 if (modifycode
== INIT_EXPR
)
7848 error ("array used as initializer");
7850 error ("invalid array assignment");
7852 return error_mark_node
;
7855 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
7856 ? 1 + (modifycode
!= INIT_EXPR
): 0;
7857 return build_vec_init (lhs
, NULL_TREE
, newrhs
,
7858 /*explicit_value_init_p=*/false,
7859 from_array
, complain
);
7862 if (modifycode
== INIT_EXPR
)
7863 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7864 LOOKUP_ONLYCONVERTING. */
7865 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
7866 ICR_INIT
, NULL_TREE
, 0,
7869 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
7870 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
7872 if (!same_type_p (lhstype
, olhstype
))
7873 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
7875 if (modifycode
!= INIT_EXPR
)
7877 if (TREE_CODE (newrhs
) == CALL_EXPR
7878 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
7879 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
7881 /* Can't initialize directly from a TARGET_EXPR, since that would
7882 cause the lhs to be constructed twice, and possibly result in
7883 accidental self-initialization. So we force the TARGET_EXPR to be
7884 expanded without a target. */
7885 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
7886 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
7887 TREE_OPERAND (newrhs
, 0));
7890 if (newrhs
== error_mark_node
)
7891 return error_mark_node
;
7893 if (c_dialect_objc () && flag_objc_gc
)
7895 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
7901 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
7902 lhstype
, lhs
, newrhs
);
7904 TREE_SIDE_EFFECTS (result
) = 1;
7906 TREE_NO_WARNING (result
) = 1;
7912 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7913 tree rhs
, tsubst_flags_t complain
)
7915 tree orig_lhs
= lhs
;
7916 tree orig_rhs
= rhs
;
7917 tree overload
= NULL_TREE
;
7918 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
7920 if (processing_template_decl
)
7922 if (modifycode
== NOP_EXPR
7923 || type_dependent_expression_p (lhs
)
7924 || type_dependent_expression_p (rhs
))
7925 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
7926 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
7929 lhs
= build_non_dependent_expr (lhs
);
7930 rhs
= build_non_dependent_expr (rhs
);
7933 if (modifycode
!= NOP_EXPR
)
7935 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
7936 lhs
, rhs
, op
, &overload
, complain
);
7939 if (rval
== error_mark_node
)
7941 TREE_NO_WARNING (rval
) = 1;
7942 if (processing_template_decl
)
7944 if (overload
!= NULL_TREE
)
7945 return (build_min_non_dep_op_overload
7946 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
7948 return (build_min_non_dep
7949 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
7954 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
7957 /* Helper function for get_delta_difference which assumes FROM is a base
7958 class of TO. Returns a delta for the conversion of pointer-to-member
7959 of FROM to pointer-to-member of TO. If the conversion is invalid and
7960 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7961 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7962 If C_CAST_P is true, this conversion is taking place as part of a
7966 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
7967 tsubst_flags_t complain
)
7972 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
7975 if (binfo
== error_mark_node
)
7977 if (!(complain
& tf_error
))
7978 return error_mark_node
;
7980 error (" in pointer to member function conversion");
7981 return size_zero_node
;
7985 if (kind
!= bk_via_virtual
)
7986 return BINFO_OFFSET (binfo
);
7988 /* FROM is a virtual base class of TO. Issue an error or warning
7989 depending on whether or not this is a reinterpret cast. */
7991 if (!(complain
& tf_error
))
7992 return error_mark_node
;
7994 error ("pointer to member conversion via virtual base %qT",
7995 BINFO_TYPE (binfo_from_vbase (binfo
)));
7997 return size_zero_node
;
8004 /* Get difference in deltas for different pointer to member function
8005 types. If the conversion is invalid and tf_error is not set in
8006 COMPLAIN, returns error_mark_node, otherwise returns an integer
8007 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8008 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8009 conversions as well. If C_CAST_P is true this conversion is taking
8010 place as part of a C-style cast.
8012 Note that the naming of FROM and TO is kind of backwards; the return
8013 value is what we add to a TO in order to get a FROM. They are named
8014 this way because we call this function to find out how to convert from
8015 a pointer to member of FROM to a pointer to member of TO. */
8018 get_delta_difference (tree from
, tree to
,
8019 bool allow_inverse_p
,
8020 bool c_cast_p
, tsubst_flags_t complain
)
8024 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
8025 /* Pointer to member of incomplete class is permitted*/
8026 result
= size_zero_node
;
8028 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
8030 if (result
== error_mark_node
)
8031 return error_mark_node
;
8035 if (!allow_inverse_p
)
8037 if (!(complain
& tf_error
))
8038 return error_mark_node
;
8040 error_not_base_type (from
, to
);
8041 error (" in pointer to member conversion");
8042 result
= size_zero_node
;
8046 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
8048 if (result
== error_mark_node
)
8049 return error_mark_node
;
8052 result
= size_diffop_loc (input_location
,
8053 size_zero_node
, result
);
8056 if (!(complain
& tf_error
))
8057 return error_mark_node
;
8059 error_not_base_type (from
, to
);
8060 error (" in pointer to member conversion");
8061 result
= size_zero_node
;
8066 return convert_to_integer (ptrdiff_type_node
, result
);
8069 /* Return a constructor for the pointer-to-member-function TYPE using
8070 the other components as specified. */
8073 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
8078 vec
<constructor_elt
, va_gc
> *v
;
8080 /* Pull the FIELD_DECLs out of the type. */
8081 pfn_field
= TYPE_FIELDS (type
);
8082 delta_field
= DECL_CHAIN (pfn_field
);
8084 /* Make sure DELTA has the type we want. */
8085 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
8087 /* Convert to the correct target type if necessary. */
8088 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
8090 /* Finish creating the initializer. */
8092 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
8093 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
8094 u
= build_constructor (type
, v
);
8095 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
8096 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
8097 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
8099 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
8104 /* Build a constructor for a pointer to member function. It can be
8105 used to initialize global variables, local variable, or used
8106 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8109 If FORCE is nonzero, then force this conversion, even if
8110 we would rather not do it. Usually set when using an explicit
8111 cast. A C-style cast is being processed iff C_CAST_P is true.
8113 Return error_mark_node, if something goes wrong. */
8116 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
8117 tsubst_flags_t complain
)
8123 if (error_operand_p (pfn
))
8124 return error_mark_node
;
8126 pfn_type
= TREE_TYPE (pfn
);
8127 to_type
= build_ptrmemfunc_type (type
);
8129 /* Handle multiple conversions of pointer to member functions. */
8130 if (TYPE_PTRMEMFUNC_P (pfn_type
))
8132 tree delta
= NULL_TREE
;
8133 tree npfn
= NULL_TREE
;
8137 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
8138 LOOKUP_NORMAL
, complain
))
8140 if (complain
& tf_error
)
8141 error ("invalid conversion to type %qT from type %qT",
8144 return error_mark_node
;
8147 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
8148 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
8150 c_cast_p
, complain
);
8151 if (n
== error_mark_node
)
8152 return error_mark_node
;
8154 /* We don't have to do any conversion to convert a
8155 pointer-to-member to its own type. But, we don't want to
8156 just return a PTRMEM_CST if there's an explicit cast; that
8157 cast should make the expression an invalid template argument. */
8158 if (TREE_CODE (pfn
) != PTRMEM_CST
)
8160 if (same_type_p (to_type
, pfn_type
))
8162 else if (integer_zerop (n
))
8163 return build_reinterpret_cast (to_type
, pfn
,
8167 if (TREE_SIDE_EFFECTS (pfn
))
8168 pfn
= save_expr (pfn
);
8170 /* Obtain the function pointer and the current DELTA. */
8171 if (TREE_CODE (pfn
) == PTRMEM_CST
)
8172 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
8175 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
8176 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
8179 /* Just adjust the DELTA field. */
8180 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8181 (TREE_TYPE (delta
), ptrdiff_type_node
));
8182 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
8183 n
= cp_build_binary_op (input_location
,
8184 LSHIFT_EXPR
, n
, integer_one_node
,
8186 delta
= cp_build_binary_op (input_location
,
8187 PLUS_EXPR
, delta
, n
, complain
);
8188 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
8191 /* Handle null pointer to member function conversions. */
8192 if (null_ptr_cst_p (pfn
))
8194 pfn
= cp_build_c_cast (type
, pfn
, complain
);
8195 return build_ptrmemfunc1 (to_type
,
8200 if (type_unknown_p (pfn
))
8201 return instantiate_type (type
, pfn
, complain
);
8203 fn
= TREE_OPERAND (pfn
, 0);
8204 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8205 /* In a template, we will have preserved the
8207 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
8208 return make_ptrmem_cst (to_type
, fn
);
8211 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8214 ??? There is no consistency as to the types returned for the above
8215 values. Some code acts as if it were a sizetype and some as if it were
8216 integer_type_node. */
8219 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
8221 tree type
= TREE_TYPE (cst
);
8222 tree fn
= PTRMEM_CST_MEMBER (cst
);
8223 tree ptr_class
, fn_class
;
8225 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8227 /* The class that the function belongs to. */
8228 fn_class
= DECL_CONTEXT (fn
);
8230 /* The class that we're creating a pointer to member of. */
8231 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
8233 /* First, calculate the adjustment to the function's class. */
8234 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
8235 /*c_cast_p=*/0, tf_warning_or_error
);
8237 if (!DECL_VIRTUAL_P (fn
))
8238 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
8239 build_addr_func (fn
, tf_warning_or_error
));
8242 /* If we're dealing with a virtual function, we have to adjust 'this'
8243 again, to point to the base which provides the vtable entry for
8244 fn; the call will do the opposite adjustment. */
8245 tree orig_class
= DECL_CONTEXT (fn
);
8246 tree binfo
= binfo_or_else (orig_class
, fn_class
);
8247 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8248 *delta
, BINFO_OFFSET (binfo
));
8250 /* We set PFN to the vtable offset at which the function can be
8251 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8252 case delta is shifted left, and then incremented). */
8253 *pfn
= DECL_VINDEX (fn
);
8254 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
8255 TYPE_SIZE_UNIT (vtable_entry_type
));
8257 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
8259 case ptrmemfunc_vbit_in_pfn
:
8260 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
8264 case ptrmemfunc_vbit_in_delta
:
8265 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
8266 *delta
, integer_one_node
);
8267 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8268 *delta
, integer_one_node
);
8275 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
8279 /* Return an expression for PFN from the pointer-to-member function
8283 pfn_from_ptrmemfunc (tree t
)
8285 if (TREE_CODE (t
) == PTRMEM_CST
)
8290 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8295 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
8298 /* Return an expression for DELTA from the pointer-to-member function
8302 delta_from_ptrmemfunc (tree t
)
8304 if (TREE_CODE (t
) == PTRMEM_CST
)
8309 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8314 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
8317 /* Convert value RHS to type TYPE as preparation for an assignment to
8318 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8319 implicit conversion is. If FNDECL is non-NULL, we are doing the
8320 conversion in order to pass the PARMNUMth argument of FNDECL.
8321 If FNDECL is NULL, we are doing the conversion in function pointer
8322 argument passing, conversion in initialization, etc. */
8325 convert_for_assignment (tree type
, tree rhs
,
8326 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8327 tsubst_flags_t complain
, int flags
)
8330 enum tree_code coder
;
8332 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8333 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
8334 rhs
= TREE_OPERAND (rhs
, 0);
8336 /* Handle [dcl.init.list] direct-list-initialization from
8337 single element of enumeration with a fixed underlying type. */
8338 if (is_direct_enum_init (type
, rhs
))
8340 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
8341 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
8342 rhs
= cp_build_c_cast (type
, elt
, complain
);
8344 rhs
= error_mark_node
;
8347 rhstype
= TREE_TYPE (rhs
);
8348 coder
= TREE_CODE (rhstype
);
8350 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
8351 && vector_types_convertible_p (type
, rhstype
, true))
8353 rhs
= mark_rvalue_use (rhs
);
8354 return convert (type
, rhs
);
8357 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8358 return error_mark_node
;
8359 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8360 return error_mark_node
;
8362 /* The RHS of an assignment cannot have void type. */
8363 if (coder
== VOID_TYPE
)
8365 if (complain
& tf_error
)
8366 error ("void value not ignored as it ought to be");
8367 return error_mark_node
;
8370 if (c_dialect_objc ())
8374 tree rname
= fndecl
;
8385 selector
= objc_message_selector ();
8387 if (selector
&& parmno
> 1)
8394 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8396 rhs
= mark_rvalue_use (rhs
);
8397 return convert (type
, rhs
);
8403 The expression is implicitly converted (clause _conv_) to the
8404 cv-unqualified type of the left operand.
8406 We allow bad conversions here because by the time we get to this point
8407 we are committed to doing the conversion. If we end up doing a bad
8408 conversion, convert_like will complain. */
8409 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8411 /* When -Wno-pmf-conversions is use, we just silently allow
8412 conversions from pointers-to-members to plain pointers. If
8413 the conversion doesn't work, cp_convert will complain. */
8415 && TYPE_PTR_P (type
)
8416 && TYPE_PTRMEMFUNC_P (rhstype
))
8417 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8420 if (complain
& tf_error
)
8422 /* If the right-hand side has unknown type, then it is an
8423 overloaded function. Call instantiate_type to get error
8425 if (rhstype
== unknown_type_node
)
8426 instantiate_type (type
, rhs
, tf_warning_or_error
);
8428 error ("cannot convert %qT to %qT for argument %qP to %qD",
8429 rhstype
, type
, parmnum
, fndecl
);
8433 case ICR_DEFAULT_ARGUMENT
:
8434 error ("cannot convert %qT to %qT in default argument",
8438 error ("cannot convert %qT to %qT in argument passing",
8441 case ICR_CONVERTING
:
8442 error ("cannot convert %qT to %qT",
8446 error ("cannot convert %qT to %qT in initialization",
8450 error ("cannot convert %qT to %qT in return",
8454 error ("cannot convert %qT to %qT in assignment",
8460 if (TYPE_PTR_P (rhstype
)
8461 && TYPE_PTR_P (type
)
8462 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
8463 && CLASS_TYPE_P (TREE_TYPE (type
))
8464 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
8465 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8466 (TREE_TYPE (rhstype
))),
8467 "class type %qT is incomplete", TREE_TYPE (rhstype
));
8469 return error_mark_node
;
8472 if (warn_suggest_attribute_format
)
8474 const enum tree_code codel
= TREE_CODE (type
);
8475 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8477 && check_missing_format_attribute (type
, rhstype
)
8478 && (complain
& tf_warning
))
8482 case ICR_DEFAULT_ARGUMENT
:
8484 warning (OPT_Wsuggest_attribute_format
,
8485 "parameter %qP of %qD might be a candidate "
8486 "for a format attribute", parmnum
, fndecl
);
8488 warning (OPT_Wsuggest_attribute_format
,
8489 "parameter might be a candidate "
8490 "for a format attribute");
8492 case ICR_CONVERTING
:
8493 warning (OPT_Wsuggest_attribute_format
,
8494 "target of conversion might be a candidate "
8495 "for a format attribute");
8498 warning (OPT_Wsuggest_attribute_format
,
8499 "target of initialization might be a candidate "
8500 "for a format attribute");
8503 warning (OPT_Wsuggest_attribute_format
,
8504 "return type might be a candidate "
8505 "for a format attribute");
8508 warning (OPT_Wsuggest_attribute_format
,
8509 "left-hand side of assignment might be a candidate "
8510 "for a format attribute");
8517 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8519 if (warn_parentheses
8520 && TREE_CODE (type
) == BOOLEAN_TYPE
8521 && TREE_CODE (rhs
) == MODIFY_EXPR
8522 && !TREE_NO_WARNING (rhs
)
8523 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
8524 && (complain
& tf_warning
))
8526 location_t loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8528 warning_at (loc
, OPT_Wparentheses
,
8529 "suggest parentheses around assignment used as truth value");
8530 TREE_NO_WARNING (rhs
) = 1;
8533 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
8537 /* Convert RHS to be of type TYPE.
8538 If EXP is nonzero, it is the target of the initialization.
8539 ERRTYPE indicates what kind of error the implicit conversion is.
8541 Two major differences between the behavior of
8542 `convert_for_assignment' and `convert_for_initialization'
8543 are that references are bashed in the former, while
8544 copied in the latter, and aggregates are assigned in
8545 the former (operator=) while initialized in the
8548 If using constructor make sure no conversion operator exists, if one does
8549 exist, an ambiguity exists. */
8552 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
8553 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8554 tsubst_flags_t complain
)
8556 enum tree_code codel
= TREE_CODE (type
);
8558 enum tree_code coder
;
8560 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8561 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8562 if (TREE_CODE (rhs
) == NOP_EXPR
8563 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
8564 && codel
!= REFERENCE_TYPE
)
8565 rhs
= TREE_OPERAND (rhs
, 0);
8567 if (type
== error_mark_node
8568 || rhs
== error_mark_node
8569 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
8570 return error_mark_node
;
8572 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
8574 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
8575 && TREE_CODE (type
) != ARRAY_TYPE
8576 && (TREE_CODE (type
) != REFERENCE_TYPE
8577 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
8578 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
8579 && !TYPE_REFFN_P (type
))
8580 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
8581 rhs
= decay_conversion (rhs
, complain
);
8583 rhstype
= TREE_TYPE (rhs
);
8584 coder
= TREE_CODE (rhstype
);
8586 if (coder
== ERROR_MARK
)
8587 return error_mark_node
;
8589 /* We accept references to incomplete types, so we can
8590 return here before checking if RHS is of complete type. */
8592 if (codel
== REFERENCE_TYPE
)
8594 /* This should eventually happen in convert_arguments. */
8595 int savew
= 0, savee
= 0;
8598 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
8599 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
8602 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
8603 inform (DECL_SOURCE_LOCATION (fndecl
),
8604 "in passing argument %P of %qD", parmnum
, fndecl
);
8610 exp
= require_complete_type_sfinae (exp
, complain
);
8611 if (exp
== error_mark_node
)
8612 return error_mark_node
;
8614 rhstype
= non_reference (rhstype
);
8616 type
= complete_type (type
);
8618 if (DIRECT_INIT_EXPR_P (type
, rhs
))
8619 /* Don't try to do copy-initialization if we already have
8620 direct-initialization. */
8623 if (MAYBE_CLASS_TYPE_P (type
))
8624 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
8626 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
8630 /* If RETVAL is the address of, or a reference to, a local variable or
8631 temporary give an appropriate warning and return true. */
8634 maybe_warn_about_returning_address_of_local (tree retval
)
8636 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
8637 tree whats_returned
= fold_for_warn (retval
);
8641 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
8642 whats_returned
= TREE_OPERAND (whats_returned
, 1);
8643 else if (CONVERT_EXPR_P (whats_returned
)
8644 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
8645 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8650 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
8652 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8654 while (TREE_CODE (whats_returned
) == COMPONENT_REF
8655 || TREE_CODE (whats_returned
) == ARRAY_REF
)
8656 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8658 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8660 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
8661 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
8663 warning (OPT_Wreturn_local_addr
, "returning reference to temporary");
8666 if (VAR_P (whats_returned
)
8667 && DECL_NAME (whats_returned
)
8668 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
8670 warning (OPT_Wreturn_local_addr
, "reference to non-lvalue returned");
8675 if (DECL_P (whats_returned
)
8676 && DECL_NAME (whats_returned
)
8677 && DECL_FUNCTION_SCOPE_P (whats_returned
)
8678 && !is_capture_proxy (whats_returned
)
8679 && !(TREE_STATIC (whats_returned
)
8680 || TREE_PUBLIC (whats_returned
)))
8682 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8683 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8684 OPT_Wreturn_local_addr
,
8685 "reference to local variable %qD returned",
8687 else if (TREE_CODE (whats_returned
) == LABEL_DECL
)
8688 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8689 OPT_Wreturn_local_addr
, "address of label %qD returned",
8692 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8693 OPT_Wreturn_local_addr
, "address of local variable %qD "
8694 "returned", whats_returned
);
8701 /* Check that returning RETVAL from the current function is valid.
8702 Return an expression explicitly showing all conversions required to
8703 change RETVAL into the function return type, and to assign it to
8704 the DECL_RESULT for the function. Set *NO_WARNING to true if
8705 code reaches end of non-void function warning shouldn't be issued
8706 on this RETURN_EXPR. */
8709 check_return_expr (tree retval
, bool *no_warning
)
8712 /* The type actually returned by the function. */
8714 /* The type the function is declared to return, or void if
8715 the declared type is incomplete. */
8717 int fn_returns_value_p
;
8718 bool named_return_value_okay_p
;
8720 *no_warning
= false;
8722 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
8724 error_at (EXPR_LOCATION (retval
), "use of %<_Cilk_spawn%> in a return "
8725 "statement is not allowed");
8729 /* A `volatile' function is one that isn't supposed to return, ever.
8730 (This is a G++ extension, used to get better code for functions
8731 that call the `volatile' function.) */
8732 if (TREE_THIS_VOLATILE (current_function_decl
))
8733 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8735 /* Check for various simple errors. */
8736 if (DECL_DESTRUCTOR_P (current_function_decl
))
8739 error ("returning a value from a destructor");
8742 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
8744 if (in_function_try_handler
)
8745 /* If a return statement appears in a handler of the
8746 function-try-block of a constructor, the program is ill-formed. */
8747 error ("cannot return from a handler of a function-try-block of a constructor");
8749 /* You can't return a value from a constructor. */
8750 error ("returning a value from a constructor");
8754 const tree saved_retval
= retval
;
8756 if (processing_template_decl
)
8758 current_function_returns_value
= 1;
8760 if (check_for_bare_parameter_packs (retval
))
8761 return error_mark_node
;
8763 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
8764 || (retval
!= NULL_TREE
8765 && type_dependent_expression_p (retval
)))
8769 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8771 /* Deduce auto return type from a return statement. */
8772 if (current_function_auto_return_pattern
)
8777 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
8779 /* Give a helpful error message. */
8780 error ("return-statement with no value, in function returning %qT",
8781 current_function_auto_return_pattern
);
8782 inform (input_location
, "only plain %<auto%> return type can be "
8783 "deduced to %<void%>");
8784 type
= error_mark_node
;
8786 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
8788 error ("returning initializer list");
8789 type
= error_mark_node
;
8795 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
8796 type
= do_auto_deduction (current_function_auto_return_pattern
,
8800 if (type
== error_mark_node
)
8802 else if (functype
== current_function_auto_return_pattern
)
8803 apply_deduced_return_type (current_function_decl
, type
);
8804 else if (!same_type_p (type
, functype
))
8806 if (LAMBDA_FUNCTION_P (current_function_decl
))
8807 error ("inconsistent types %qT and %qT deduced for "
8808 "lambda return type", functype
, type
);
8810 error ("inconsistent deduction for auto return type: "
8811 "%qT and then %qT", functype
, type
);
8816 result
= DECL_RESULT (current_function_decl
);
8817 valtype
= TREE_TYPE (result
);
8818 gcc_assert (valtype
!= NULL_TREE
);
8819 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
8821 /* Check for a return statement with no return value in a function
8822 that's supposed to return a value. */
8823 if (!retval
&& fn_returns_value_p
)
8825 if (functype
!= error_mark_node
)
8826 permerror (input_location
, "return-statement with no value, in "
8827 "function returning %qT", valtype
);
8828 /* Remember that this function did return. */
8829 current_function_returns_value
= 1;
8830 /* And signal caller that TREE_NO_WARNING should be set on the
8831 RETURN_EXPR to avoid control reaches end of non-void function
8832 warnings in tree-cfg.c. */
8835 /* Check for a return statement with a value in a function that
8836 isn't supposed to return a value. */
8837 else if (retval
&& !fn_returns_value_p
)
8839 if (VOID_TYPE_P (TREE_TYPE (retval
)))
8840 /* You can return a `void' value from a function of `void'
8841 type. In that case, we have to evaluate the expression for
8842 its side-effects. */
8843 finish_expr_stmt (retval
);
8845 permerror (input_location
, "return-statement with a value, in function "
8846 "returning 'void'");
8847 current_function_returns_null
= 1;
8849 /* There's really no value to return, after all. */
8853 /* Remember that this function can sometimes return without a
8855 current_function_returns_null
= 1;
8857 /* Remember that this function did return a value. */
8858 current_function_returns_value
= 1;
8860 /* Check for erroneous operands -- but after giving ourselves a
8861 chance to provide an error about returning a value from a void
8863 if (error_operand_p (retval
))
8865 current_function_return_value
= error_mark_node
;
8866 return error_mark_node
;
8869 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8870 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
8871 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
8872 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
8874 && retval
&& null_ptr_cst_p (retval
))
8875 warning (0, "%<operator new%> must not return NULL unless it is "
8876 "declared %<throw()%> (or -fcheck-new is in effect)");
8878 /* Effective C++ rule 15. See also start_function. */
8880 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
))
8884 /* The function return type must be a reference to the current
8886 if (TREE_CODE (valtype
) == REFERENCE_TYPE
8887 && same_type_ignoring_top_level_qualifiers_p
8888 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
8890 /* Returning '*this' is obviously OK. */
8891 if (retval
== current_class_ref
)
8893 /* If we are calling a function whose return type is the same of
8894 the current class reference, it is ok. */
8895 else if (INDIRECT_REF_P (retval
)
8896 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
8901 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
8904 if (processing_template_decl
)
8906 /* We should not have changed the return value. */
8907 gcc_assert (retval
== saved_retval
);
8911 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8913 [...] For a function with a class return type, if the expression
8914 in the return statement is the name of a local object, and the cv-
8915 unqualified type of the local object is the same as the function
8916 return type, an implementation is permitted to omit creating the tem-
8917 porary object to hold the function return value [...]
8919 So, if this is a value-returning function that always returns the same
8920 local variable, remember it.
8922 It might be nice to be more flexible, and choose the first suitable
8923 variable even if the function sometimes returns something else, but
8924 then we run the risk of clobbering the variable we chose if the other
8925 returned expression uses the chosen variable somehow. And people expect
8926 this restriction, anyway. (jason 2000-11-19)
8928 See finish_function and finalize_nrv for the rest of this optimization. */
8930 named_return_value_okay_p
=
8931 (retval
!= NULL_TREE
8932 /* Must be a local, automatic variable. */
8934 && DECL_CONTEXT (retval
) == current_function_decl
8935 && ! TREE_STATIC (retval
)
8936 /* And not a lambda or anonymous union proxy. */
8937 && !DECL_HAS_VALUE_EXPR_P (retval
)
8938 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
8939 /* The cv-unqualified type of the returned value must be the
8940 same as the cv-unqualified return type of the
8942 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8943 (TYPE_MAIN_VARIANT (functype
)))
8944 /* And the returned value must be non-volatile. */
8945 && ! TYPE_VOLATILE (TREE_TYPE (retval
)));
8947 if (fn_returns_value_p
&& flag_elide_constructors
)
8949 if (named_return_value_okay_p
8950 && (current_function_return_value
== NULL_TREE
8951 || current_function_return_value
== retval
))
8952 current_function_return_value
= retval
;
8954 current_function_return_value
= error_mark_node
;
8957 /* We don't need to do any conversions when there's nothing being
8962 /* Do any required conversions. */
8963 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
8964 /* No conversions are required. */
8968 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
8970 /* The functype's return type will have been set to void, if it
8971 was an incomplete type. Just treat this as 'return;' */
8972 if (VOID_TYPE_P (functype
))
8973 return error_mark_node
;
8975 /* If we had an id-expression obfuscated by force_paren_expr, we need
8976 to undo it so we can try to treat it as an rvalue below. */
8977 retval
= maybe_undo_parenthesized_ref (retval
);
8979 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8980 treated as an rvalue for the purposes of overload resolution to
8981 favor move constructors over copy constructors.
8983 Note that these conditions are similar to, but not as strict as,
8984 the conditions for the named return value optimization. */
8985 if ((cxx_dialect
!= cxx98
)
8986 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
8987 || TREE_CODE (retval
) == PARM_DECL
)
8988 && DECL_CONTEXT (retval
) == current_function_decl
8989 && !TREE_STATIC (retval
)
8990 /* This is only interesting for class type. */
8991 && CLASS_TYPE_P (functype
))
8992 flags
= flags
| LOOKUP_PREFER_RVALUE
;
8994 /* First convert the value to the function's return type, then
8995 to the type of return value's location to handle the
8996 case that functype is smaller than the valtype. */
8997 retval
= convert_for_initialization
8998 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
8999 tf_warning_or_error
);
9000 retval
= convert (valtype
, retval
);
9002 /* If the conversion failed, treat this just like `return;'. */
9003 if (retval
== error_mark_node
)
9005 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9006 else if (! cfun
->returns_struct
9007 && TREE_CODE (retval
) == TARGET_EXPR
9008 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
9009 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9010 TREE_OPERAND (retval
, 0));
9011 else if (maybe_warn_about_returning_address_of_local (retval
))
9012 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9013 build_zero_cst (TREE_TYPE (retval
)));
9016 /* Actually copy the value returned into the appropriate location. */
9017 if (retval
&& retval
!= result
)
9018 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
9024 /* Returns nonzero if the pointer-type FROM can be converted to the
9025 pointer-type TO via a qualification conversion. If CONSTP is -1,
9026 then we return nonzero if the pointers are similar, and the
9027 cv-qualification signature of FROM is a proper subset of that of TO.
9029 If CONSTP is positive, then all outer pointers have been
9033 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
9035 bool to_more_cv_qualified
= false;
9036 bool is_opaque_pointer
= false;
9038 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9040 if (TREE_CODE (to
) != TREE_CODE (from
))
9043 if (TREE_CODE (from
) == OFFSET_TYPE
9044 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
9045 TYPE_OFFSET_BASETYPE (to
)))
9048 /* Const and volatile mean something different for function types,
9049 so the usual checks are not appropriate. */
9050 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
9052 if (!at_least_as_qualified_p (to
, from
))
9055 if (!at_least_as_qualified_p (from
, to
))
9059 to_more_cv_qualified
= true;
9063 constp
&= TYPE_READONLY (to
);
9066 if (VECTOR_TYPE_P (to
))
9067 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9069 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
9070 return ((constp
>= 0 || to_more_cv_qualified
)
9071 && (is_opaque_pointer
9072 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
9076 /* When comparing, say, char ** to char const **, this function takes
9077 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9078 types to this function. */
9081 comp_ptr_ttypes (tree to
, tree from
)
9083 return comp_ptr_ttypes_real (to
, from
, 1);
9086 /* Returns true iff FNTYPE is a non-class type that involves
9087 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9088 if a parameter type is ill-formed. */
9091 error_type_p (const_tree type
)
9095 switch (TREE_CODE (type
))
9101 case REFERENCE_TYPE
:
9103 return error_type_p (TREE_TYPE (type
));
9107 if (error_type_p (TREE_TYPE (type
)))
9109 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
9110 if (error_type_p (TREE_VALUE (t
)))
9115 if (TYPE_PTRMEMFUNC_P (type
))
9116 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
9124 /* Returns true if to and from are (possibly multi-level) pointers to the same
9125 type or inheritance-related types, regardless of cv-quals. */
9128 ptr_reasonably_similar (const_tree to
, const_tree from
)
9130 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9132 /* Any target type is similar enough to void. */
9133 if (VOID_TYPE_P (to
))
9134 return !error_type_p (from
);
9135 if (VOID_TYPE_P (from
))
9136 return !error_type_p (to
);
9138 if (TREE_CODE (to
) != TREE_CODE (from
))
9141 if (TREE_CODE (from
) == OFFSET_TYPE
9142 && comptypes (TYPE_OFFSET_BASETYPE (to
),
9143 TYPE_OFFSET_BASETYPE (from
),
9144 COMPARE_BASE
| COMPARE_DERIVED
))
9147 if (VECTOR_TYPE_P (to
)
9148 && vector_types_convertible_p (to
, from
, false))
9151 if (TREE_CODE (to
) == INTEGER_TYPE
9152 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
9155 if (TREE_CODE (to
) == FUNCTION_TYPE
)
9156 return !error_type_p (to
) && !error_type_p (from
);
9158 if (!TYPE_PTR_P (to
))
9160 /* When either type is incomplete avoid DERIVED_FROM_P,
9161 which may call complete_type (c++/57942). */
9162 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
9164 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
9165 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
9170 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9171 pointer-to-member types) are the same, ignoring cv-qualification at
9175 comp_ptr_ttypes_const (tree to
, tree from
)
9177 bool is_opaque_pointer
= false;
9179 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9181 if (TREE_CODE (to
) != TREE_CODE (from
))
9184 if (TREE_CODE (from
) == OFFSET_TYPE
9185 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
9186 TYPE_OFFSET_BASETYPE (to
)))
9189 if (VECTOR_TYPE_P (to
))
9190 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9192 if (!TYPE_PTR_P (to
))
9193 return (is_opaque_pointer
9194 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
9198 /* Returns the type qualifiers for this type, including the qualifiers on the
9199 elements for an array type. */
9202 cp_type_quals (const_tree type
)
9205 /* This CONST_CAST is okay because strip_array_types returns its
9206 argument unmodified and we assign it to a const_tree. */
9207 type
= strip_array_types (CONST_CAST_TREE (type
));
9208 if (type
== error_mark_node
9209 /* Quals on a FUNCTION_TYPE are memfn quals. */
9210 || TREE_CODE (type
) == FUNCTION_TYPE
)
9211 return TYPE_UNQUALIFIED
;
9212 quals
= TYPE_QUALS (type
);
9213 /* METHOD and REFERENCE_TYPEs should never have quals. */
9214 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
9215 && TREE_CODE (type
) != REFERENCE_TYPE
)
9216 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
9217 == TYPE_UNQUALIFIED
));
9221 /* Returns the function-ref-qualifier for TYPE */
9224 type_memfn_rqual (const_tree type
)
9226 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
9227 || TREE_CODE (type
) == METHOD_TYPE
);
9229 if (!FUNCTION_REF_QUALIFIED (type
))
9230 return REF_QUAL_NONE
;
9231 else if (FUNCTION_RVALUE_QUALIFIED (type
))
9232 return REF_QUAL_RVALUE
;
9234 return REF_QUAL_LVALUE
;
9237 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9241 type_memfn_quals (const_tree type
)
9243 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9244 return TYPE_QUALS (type
);
9245 else if (TREE_CODE (type
) == METHOD_TYPE
)
9246 return cp_type_quals (class_of_this_parm (type
));
9251 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9252 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9255 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
9257 /* Could handle METHOD_TYPE here if necessary. */
9258 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
9259 if (TYPE_QUALS (type
) == memfn_quals
9260 && type_memfn_rqual (type
) == rqual
)
9263 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9265 tree result
= build_qualified_type (type
, memfn_quals
);
9266 return build_ref_qualified_type (result
, rqual
);
9269 /* Returns nonzero if TYPE is const or volatile. */
9272 cv_qualified_p (const_tree type
)
9274 int quals
= cp_type_quals (type
);
9275 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
9278 /* Returns nonzero if the TYPE contains a mutable member. */
9281 cp_has_mutable_p (const_tree type
)
9283 /* This CONST_CAST is okay because strip_array_types returns its
9284 argument unmodified and we assign it to a const_tree. */
9285 type
= strip_array_types (CONST_CAST_TREE(type
));
9287 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
9290 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9291 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9292 approximation. In particular, consider:
9295 struct S { int i; };
9296 const S s = { f(); }
9298 Here, we will make "s" as TREE_READONLY (because it is declared
9299 "const") -- only to reverse ourselves upon seeing that the
9300 initializer is non-constant. */
9303 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
9305 tree type
= TREE_TYPE (decl
);
9307 if (type
== error_mark_node
)
9310 if (TREE_CODE (decl
) == TYPE_DECL
)
9313 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
9314 && type_quals
!= TYPE_UNQUALIFIED
));
9316 /* Avoid setting TREE_READONLY incorrectly. */
9317 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9318 constructor can produce constant init, so rely on cp_finish_decl to
9319 clear TREE_READONLY if the variable has non-constant init. */
9321 /* If the type has (or might have) a mutable component, that component
9322 might be modified. */
9323 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
9324 type_quals
&= ~TYPE_QUAL_CONST
;
9326 c_apply_type_quals_to_decl (type_quals
, decl
);
9329 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9330 exemplar types such that casting T1 to T2 is casting away constness
9331 if and only if there is no implicit conversion from T1 to T2. */
9334 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
9339 /* [expr.const.cast]
9341 For multi-level pointer to members and multi-level mixed pointers
9342 and pointers to members (conv.qual), the "member" aspect of a
9343 pointer to member level is ignored when determining if a const
9344 cv-qualifier has been cast away. */
9345 /* [expr.const.cast]
9347 For two pointer types:
9349 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9350 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9353 casting from X1 to X2 casts away constness if, for a non-pointer
9354 type T there does not exist an implicit conversion (clause
9357 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9361 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9362 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
9363 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
9365 *t1
= cp_build_qualified_type (void_type_node
,
9366 cp_type_quals (*t1
));
9367 *t2
= cp_build_qualified_type (void_type_node
,
9368 cp_type_quals (*t2
));
9372 quals1
= cp_type_quals (*t1
);
9373 quals2
= cp_type_quals (*t2
);
9375 if (TYPE_PTRDATAMEM_P (*t1
))
9376 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
9378 *t1
= TREE_TYPE (*t1
);
9379 if (TYPE_PTRDATAMEM_P (*t2
))
9380 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
9382 *t2
= TREE_TYPE (*t2
);
9384 casts_away_constness_r (t1
, t2
, complain
);
9385 *t1
= build_pointer_type (*t1
);
9386 *t2
= build_pointer_type (*t2
);
9387 *t1
= cp_build_qualified_type (*t1
, quals1
);
9388 *t2
= cp_build_qualified_type (*t2
, quals2
);
9391 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9394 ??? This function returns non-zero if casting away qualifiers not
9395 just const. We would like to return to the caller exactly which
9396 qualifiers are casted away to give more accurate diagnostics.
9400 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
9402 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
9404 /* [expr.const.cast]
9406 Casting from an lvalue of type T1 to an lvalue of type T2
9407 using a reference cast casts away constness if a cast from an
9408 rvalue of type "pointer to T1" to the type "pointer to T2"
9409 casts away constness. */
9410 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
? TREE_TYPE (t1
) : t1
);
9411 return casts_away_constness (build_pointer_type (t1
),
9412 build_pointer_type (TREE_TYPE (t2
)),
9416 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
9417 /* [expr.const.cast]
9419 Casting from an rvalue of type "pointer to data member of X
9420 of type T1" to the type "pointer to data member of Y of type
9421 T2" casts away constness if a cast from an rvalue of type
9422 "pointer to T1" to the type "pointer to T2" casts away
9424 return casts_away_constness
9425 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
9426 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
9429 /* Casting away constness is only something that makes sense for
9430 pointer or reference types. */
9431 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
9434 /* Top-level qualifiers don't matter. */
9435 t1
= TYPE_MAIN_VARIANT (t1
);
9436 t2
= TYPE_MAIN_VARIANT (t2
);
9437 casts_away_constness_r (&t1
, &t2
, complain
);
9438 if (!can_convert (t2
, t1
, complain
))
9444 /* If T is a REFERENCE_TYPE return the type to which T refers.
9445 Otherwise, return T itself. */
9448 non_reference (tree t
)
9450 if (t
&& TREE_CODE (t
) == REFERENCE_TYPE
)
9456 /* Return nonzero if REF is an lvalue valid for this language;
9457 otherwise, print an error message and return zero. USE says
9458 how the lvalue is being used and so selects the error message. */
9461 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
9463 cp_lvalue_kind kind
= lvalue_kind (ref
);
9465 if (kind
== clk_none
)
9467 if (complain
& tf_error
)
9468 lvalue_error (input_location
, use
);
9471 else if (kind
& (clk_rvalueref
|clk_class
))
9473 if (!(complain
& tf_error
))
9475 if (kind
& clk_class
)
9476 /* Make this a permerror because we used to accept it. */
9477 permerror (input_location
, "using temporary as lvalue");
9479 error ("using xvalue (rvalue reference) as lvalue");
9484 /* Return true if a user-defined literal operator is a raw operator. */
9487 check_raw_literal_operator (const_tree decl
)
9489 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9492 bool maybe_raw_p
= false;
9494 /* Count the number and type of arguments and check for ellipsis. */
9495 for (argtype
= argtypes
, arity
= 0;
9496 argtype
&& argtype
!= void_list_node
;
9497 ++arity
, argtype
= TREE_CHAIN (argtype
))
9499 tree t
= TREE_VALUE (argtype
);
9501 if (same_type_p (t
, const_string_type_node
))
9505 return false; /* Found ellipsis. */
9507 if (!maybe_raw_p
|| arity
!= 1)
9514 /* Return true if a user-defined literal operator has one of the allowed
9518 check_literal_operator_args (const_tree decl
,
9519 bool *long_long_unsigned_p
, bool *long_double_p
)
9521 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9523 *long_long_unsigned_p
= false;
9524 *long_double_p
= false;
9525 if (processing_template_decl
|| processing_specialization
)
9526 return argtypes
== void_list_node
;
9533 /* Count the number and type of arguments and check for ellipsis. */
9534 for (argtype
= argtypes
, arity
= 0;
9535 argtype
&& argtype
!= void_list_node
;
9536 argtype
= TREE_CHAIN (argtype
))
9538 tree t
= TREE_VALUE (argtype
);
9543 bool maybe_raw_p
= false;
9545 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
9547 t
= TYPE_MAIN_VARIANT (t
);
9548 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
9549 || same_type_p (t
, wchar_type_node
)
9550 || same_type_p (t
, char16_type_node
)
9551 || same_type_p (t
, char32_type_node
))
9553 argtype
= TREE_CHAIN (argtype
);
9556 t
= TREE_VALUE (argtype
);
9557 if (maybe_raw_p
&& argtype
== void_list_node
)
9559 else if (same_type_p (t
, size_type_node
))
9568 else if (same_type_p (t
, long_long_unsigned_type_node
))
9571 *long_long_unsigned_p
= true;
9573 else if (same_type_p (t
, long_double_type_node
))
9576 *long_double_p
= true;
9578 else if (same_type_p (t
, char_type_node
))
9580 else if (same_type_p (t
, wchar_type_node
))
9582 else if (same_type_p (t
, char16_type_node
))
9584 else if (same_type_p (t
, char32_type_node
))
9590 return false; /* Found ellipsis. */
9592 if (arity
!= max_arity
)
9599 /* Always returns false since unlike C90, C++ has no concept of implicit
9600 function declarations. */
9603 c_decl_implicit (const_tree
)