1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2016 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
);
1653 if (TREE_CODE (e
) == COMPONENT_REF
1654 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1655 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1657 if (complain
& tf_error
)
1658 error ("invalid application of %<sizeof%> to a bit-field");
1660 return error_mark_node
;
1663 else if (is_overloaded_fn (e
))
1665 if (complain
& tf_error
)
1666 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1669 return error_mark_node
;
1672 else if (type_unknown_p (e
))
1674 if (complain
& tf_error
)
1675 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1677 return error_mark_node
;
1683 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, complain
& tf_error
);
1686 /* Implement the __alignof keyword: Return the minimum required
1687 alignment of E, measured in bytes. For VAR_DECL's and
1688 FIELD_DECL's return DECL_ALIGN (which can be set from an
1689 "aligned" __attribute__ specification). */
1692 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1696 if (e
== error_mark_node
)
1697 return error_mark_node
;
1699 if (processing_template_decl
)
1701 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1702 TREE_SIDE_EFFECTS (e
) = 0;
1703 TREE_READONLY (e
) = 1;
1708 e
= mark_type_use (e
);
1711 t
= size_int (DECL_ALIGN_UNIT (e
));
1712 else if (TREE_CODE (e
) == COMPONENT_REF
1713 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1714 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1716 if (complain
& tf_error
)
1717 error ("invalid application of %<__alignof%> to a bit-field");
1719 return error_mark_node
;
1722 else if (TREE_CODE (e
) == COMPONENT_REF
1723 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1724 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1725 else if (is_overloaded_fn (e
))
1727 if (complain
& tf_error
)
1728 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1731 return error_mark_node
;
1732 if (TREE_CODE (e
) == FUNCTION_DECL
)
1733 t
= size_int (DECL_ALIGN_UNIT (e
));
1737 else if (type_unknown_p (e
))
1739 if (complain
& tf_error
)
1740 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1742 return error_mark_node
;
1746 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
,
1747 complain
& tf_error
);
1749 return fold_convert (size_type_node
, t
);
1752 /* Process a sizeof or alignof expression E with code OP where the operand
1753 is an expression. */
1756 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1758 if (op
== SIZEOF_EXPR
)
1759 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1761 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1764 /* Build a representation of an expression 'alignas(E).' Return the
1765 folded integer value of E if it is an integral constant expression
1766 that resolves to a valid alignment. If E depends on a template
1767 parameter, return a syntactic representation tree of kind
1768 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1769 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1772 cxx_alignas_expr (tree e
)
1774 if (e
== NULL_TREE
|| e
== error_mark_node
1775 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1781 When the alignment-specifier is of the form
1782 alignas(type-id ), it shall have the same effect as
1783 alignas(alignof(type-id )). */
1785 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, false);
1787 /* If we reach this point, it means the alignas expression if of
1788 the form "alignas(assignment-expression)", so we should follow
1789 what is stated by [dcl.align]/2. */
1791 if (value_dependent_expression_p (e
))
1792 /* Leave value-dependent expression alone for now. */
1795 e
= instantiate_non_dependent_expr (e
);
1796 e
= mark_rvalue_use (e
);
1798 /* [dcl.align]/2 says:
1800 the assignment-expression shall be an integral constant
1803 return cxx_constant_value (e
);
1807 /* EXPR is being used in a context that is not a function call.
1812 The expression can be used only as the left-hand operand of a
1813 member function call.
1815 [expr.mptr.operator]
1817 If the result of .* or ->* is a function, then that result can be
1818 used only as the operand for the function call operator ().
1820 by issuing an error message if appropriate. Returns true iff EXPR
1821 violates these rules. */
1824 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1826 if (expr
== NULL_TREE
)
1828 /* Don't enforce this in MS mode. */
1829 if (flag_ms_extensions
)
1831 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1832 expr
= get_first_fn (expr
);
1833 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1835 if (complain
& tf_error
)
1839 error_at (loc
, "invalid use of non-static member function %qD",
1841 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
1844 error_at (loc
, "invalid use of non-static member function of "
1845 "type %qT", TREE_TYPE (expr
));
1852 /* If EXP is a reference to a bitfield, and the type of EXP does not
1853 match the declared type of the bitfield, return the declared type
1854 of the bitfield. Otherwise, return NULL_TREE. */
1857 is_bitfield_expr_with_lowered_type (const_tree exp
)
1859 switch (TREE_CODE (exp
))
1862 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1863 ? TREE_OPERAND (exp
, 1)
1864 : TREE_OPERAND (exp
, 0)))
1866 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1869 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1873 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1879 field
= TREE_OPERAND (exp
, 1);
1880 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1882 if (same_type_ignoring_top_level_qualifiers_p
1883 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1885 return DECL_BIT_FIELD_TYPE (field
);
1889 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1890 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1891 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1899 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1900 bitfield with a lowered type, the type of EXP is returned, rather
1904 unlowered_expr_type (const_tree exp
)
1907 tree etype
= TREE_TYPE (exp
);
1909 type
= is_bitfield_expr_with_lowered_type (exp
);
1911 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1918 /* Perform the conversions in [expr] that apply when an lvalue appears
1919 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1920 function-to-pointer conversions. In addition, bitfield references are
1921 converted to their declared types. Note that this function does not perform
1922 the lvalue-to-rvalue conversion for class types. If you need that conversion
1923 for class types, then you probably need to use force_rvalue.
1925 Although the returned value is being used as an rvalue, this
1926 function does not wrap the returned expression in a
1927 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1928 that the return value is no longer an lvalue. */
1931 decay_conversion (tree exp
,
1932 tsubst_flags_t complain
,
1933 bool reject_builtin
/* = true */)
1936 enum tree_code code
;
1937 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
1939 type
= TREE_TYPE (exp
);
1940 if (type
== error_mark_node
)
1941 return error_mark_node
;
1943 exp
= resolve_nondeduced_context (exp
, complain
);
1944 if (type_unknown_p (exp
))
1946 if (complain
& tf_error
)
1947 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
1948 return error_mark_node
;
1951 code
= TREE_CODE (type
);
1953 if (error_operand_p (exp
))
1954 return error_mark_node
;
1956 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
1957 return nullptr_node
;
1959 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1960 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1961 if (code
== VOID_TYPE
)
1963 if (complain
& tf_error
)
1964 error_at (loc
, "void value not ignored as it ought to be");
1965 return error_mark_node
;
1967 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
1968 return error_mark_node
;
1969 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1971 exp
= mark_lvalue_use (exp
);
1972 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
1973 return error_mark_node
;
1974 return cp_build_addr_expr (exp
, complain
);
1976 if (code
== ARRAY_TYPE
)
1981 exp
= mark_lvalue_use (exp
);
1983 if (INDIRECT_REF_P (exp
))
1984 return build_nop (build_pointer_type (TREE_TYPE (type
)),
1985 TREE_OPERAND (exp
, 0));
1987 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1989 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
1990 if (op1
== error_mark_node
)
1991 return error_mark_node
;
1992 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1993 TREE_OPERAND (exp
, 0), op1
);
1996 if (!obvalue_p (exp
)
1997 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1999 if (complain
& tf_error
)
2000 error_at (loc
, "invalid use of non-lvalue array");
2001 return error_mark_node
;
2004 /* Don't let an array compound literal decay to a pointer. It can
2005 still be used to initialize an array or bind to a reference. */
2006 if (TREE_CODE (exp
) == TARGET_EXPR
)
2008 if (complain
& tf_error
)
2009 error_at (loc
, "taking address of temporary array");
2010 return error_mark_node
;
2013 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2017 if (!cxx_mark_addressable (exp
))
2018 return error_mark_node
;
2019 adr
= build_nop (ptrtype
, build_address (exp
));
2022 /* This way is better for a COMPONENT_REF since it can
2023 simplify the offset for a component. */
2024 adr
= cp_build_addr_expr (exp
, complain
);
2025 return cp_convert (ptrtype
, adr
, complain
);
2028 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2029 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2031 /* If a bitfield is used in a context where integral promotion
2032 applies, then the caller is expected to have used
2033 default_conversion. That function promotes bitfields correctly
2034 before calling this function. At this point, if we have a
2035 bitfield referenced, we may assume that is not subject to
2036 promotion, and that, therefore, the type of the resulting rvalue
2037 is the declared type of the bitfield. */
2038 exp
= convert_bitfield_to_declared_type (exp
);
2040 /* We do not call rvalue() here because we do not want to wrap EXP
2041 in a NON_LVALUE_EXPR. */
2045 Non-class rvalues always have cv-unqualified types. */
2046 type
= TREE_TYPE (exp
);
2047 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2048 exp
= build_nop (cv_unqualified (type
), exp
);
2050 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2051 return error_mark_node
;
2056 /* Perform preparatory conversions, as part of the "usual arithmetic
2057 conversions". In particular, as per [expr]:
2059 Whenever an lvalue expression appears as an operand of an
2060 operator that expects the rvalue for that operand, the
2061 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2062 standard conversions are applied to convert the expression to an
2065 In addition, we perform integral promotions here, as those are
2066 applied to both operands to a binary operator before determining
2067 what additional conversions should apply. */
2070 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2072 /* Check for target-specific promotions. */
2073 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2075 exp
= cp_convert (promoted_type
, exp
, complain
);
2076 /* Perform the integral promotions first so that bitfield
2077 expressions (which may promote to "int", even if the bitfield is
2078 declared "unsigned") are promoted correctly. */
2079 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2080 exp
= cp_perform_integral_promotions (exp
, complain
);
2081 /* Perform the other conversions. */
2082 exp
= decay_conversion (exp
, complain
);
2090 default_conversion (tree exp
)
2092 return cp_default_conversion (exp
, tf_warning_or_error
);
2095 /* EXPR is an expression with an integral or enumeration type.
2096 Perform the integral promotions in [conv.prom], and return the
2100 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2105 expr
= mark_rvalue_use (expr
);
2109 If the bitfield has an enumerated type, it is treated as any
2110 other value of that type for promotion purposes. */
2111 type
= is_bitfield_expr_with_lowered_type (expr
);
2112 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2113 type
= TREE_TYPE (expr
);
2114 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2115 /* Scoped enums don't promote. */
2116 if (SCOPED_ENUM_P (type
))
2118 promoted_type
= type_promotes_to (type
);
2119 if (type
!= promoted_type
)
2120 expr
= cp_convert (promoted_type
, expr
, complain
);
2127 perform_integral_promotions (tree expr
)
2129 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2132 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2133 decay_conversion to one. */
2136 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2140 if (!TYPE_PTR_P (totype
))
2143 t
= TREE_TYPE (totype
);
2144 if (!same_type_p (t
, char_type_node
)
2145 && !same_type_p (t
, char16_type_node
)
2146 && !same_type_p (t
, char32_type_node
)
2147 && !same_type_p (t
, wchar_type_node
))
2150 if (TREE_CODE (exp
) == STRING_CST
)
2152 /* Make sure that we don't try to convert between char and wide chars. */
2153 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2158 /* Is this a string constant which has decayed to 'const char *'? */
2159 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2160 if (!same_type_p (TREE_TYPE (exp
), t
))
2163 if (TREE_CODE (exp
) != ADDR_EXPR
2164 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2169 if (cxx_dialect
>= cxx11
)
2170 pedwarn (input_location
,
2171 pedantic
? OPT_Wpedantic
: OPT_Wwrite_strings
,
2172 "ISO C++ forbids converting a string constant to %qT",
2175 warning (OPT_Wwrite_strings
,
2176 "deprecated conversion from string constant to %qT",
2183 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2184 can, for example, use as an lvalue. This code used to be in
2185 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2186 expressions, where we're dealing with aggregates. But now it's again only
2187 called from unary_complex_lvalue. The case (in particular) that led to
2188 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2192 rationalize_conditional_expr (enum tree_code code
, tree t
,
2193 tsubst_flags_t complain
)
2195 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
2197 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2198 the first operand is always the one to be used if both operands
2199 are equal, so we know what conditional expression this used to be. */
2200 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2202 tree op0
= TREE_OPERAND (t
, 0);
2203 tree op1
= TREE_OPERAND (t
, 1);
2205 /* The following code is incorrect if either operand side-effects. */
2206 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2207 && !TREE_SIDE_EFFECTS (op1
));
2209 build_conditional_expr (loc
,
2210 build_x_binary_op (loc
,
2211 (TREE_CODE (t
) == MIN_EXPR
2212 ? LE_EXPR
: GE_EXPR
),
2213 op0
, TREE_CODE (op0
),
2214 op1
, TREE_CODE (op1
),
2217 cp_build_unary_op (code
, op0
, false, complain
),
2218 cp_build_unary_op (code
, op1
, false, complain
),
2223 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2224 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), false,
2226 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), false,
2231 /* Given the TYPE of an anonymous union field inside T, return the
2232 FIELD_DECL for the field. If not found return NULL_TREE. Because
2233 anonymous unions can nest, we must also search all anonymous unions
2234 that are directly reachable. */
2237 lookup_anon_field (tree t
, tree type
)
2241 t
= TYPE_MAIN_VARIANT (t
);
2243 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2245 if (TREE_STATIC (field
))
2247 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2250 /* If we find it directly, return the field. */
2251 if (DECL_NAME (field
) == NULL_TREE
2252 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2257 /* Otherwise, it could be nested, search harder. */
2258 if (DECL_NAME (field
) == NULL_TREE
2259 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2261 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2269 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2270 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2271 non-NULL, it indicates the path to the base used to name MEMBER.
2272 If PRESERVE_REFERENCE is true, the expression returned will have
2273 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2274 returned will have the type referred to by the reference.
2276 This function does not perform access control; that is either done
2277 earlier by the parser when the name of MEMBER is resolved to MEMBER
2278 itself, or later when overload resolution selects one of the
2279 functions indicated by MEMBER. */
2282 build_class_member_access_expr (cp_expr object
, tree member
,
2283 tree access_path
, bool preserve_reference
,
2284 tsubst_flags_t complain
)
2288 tree result
= NULL_TREE
;
2289 tree using_decl
= NULL_TREE
;
2291 if (error_operand_p (object
) || error_operand_p (member
))
2292 return error_mark_node
;
2294 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2298 The type of the first expression shall be "class object" (of a
2300 object_type
= TREE_TYPE (object
);
2301 if (!currently_open_class (object_type
)
2302 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2303 return error_mark_node
;
2304 if (!CLASS_TYPE_P (object_type
))
2306 if (complain
& tf_error
)
2308 if (POINTER_TYPE_P (object_type
)
2309 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2310 error ("request for member %qD in %qE, which is of pointer "
2311 "type %qT (maybe you meant to use %<->%> ?)",
2312 member
, object
.get_value (), object_type
);
2314 error ("request for member %qD in %qE, which is of non-class "
2315 "type %qT", member
, object
.get_value (), object_type
);
2317 return error_mark_node
;
2320 /* The standard does not seem to actually say that MEMBER must be a
2321 member of OBJECT_TYPE. However, that is clearly what is
2323 if (DECL_P (member
))
2325 member_scope
= DECL_CLASS_CONTEXT (member
);
2326 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2327 return error_mark_node
;
2328 if (TREE_DEPRECATED (member
))
2329 warn_deprecated_use (member
, NULL_TREE
);
2332 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2333 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2334 presently be the anonymous union. Go outwards until we find a
2335 type related to OBJECT_TYPE. */
2336 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2337 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2339 member_scope
= TYPE_CONTEXT (member_scope
);
2340 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2342 if (complain
& tf_error
)
2344 if (TREE_CODE (member
) == FIELD_DECL
)
2345 error ("invalid use of nonstatic data member %qE", member
);
2347 error ("%qD is not a member of %qT", member
, object_type
);
2349 return error_mark_node
;
2352 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2353 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2354 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2356 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2358 object
= cp_build_indirect_ref (temp
, RO_NULL
, complain
);
2361 /* In [expr.ref], there is an explicit list of the valid choices for
2362 MEMBER. We check for each of those cases here. */
2365 /* A static data member. */
2367 mark_exp_read (object
);
2368 /* If OBJECT has side-effects, they are supposed to occur. */
2369 if (TREE_SIDE_EFFECTS (object
))
2370 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2372 else if (TREE_CODE (member
) == FIELD_DECL
)
2374 /* A non-static data member. */
2379 if (INDIRECT_REF_P (object
))
2381 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2383 null_object_p
= false;
2385 /* Convert OBJECT to the type of MEMBER. */
2386 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2387 TYPE_MAIN_VARIANT (member_scope
)))
2392 binfo
= lookup_base (access_path
? access_path
: object_type
,
2393 member_scope
, ba_unique
, &kind
, complain
);
2394 if (binfo
== error_mark_node
)
2395 return error_mark_node
;
2397 /* It is invalid to try to get to a virtual base of a
2398 NULL object. The most common cause is invalid use of
2400 if (null_object_p
&& kind
== bk_via_virtual
)
2402 if (complain
& tf_error
)
2404 error ("invalid access to non-static data member %qD in "
2405 "virtual base of NULL object", member
);
2407 return error_mark_node
;
2410 /* Convert to the base. */
2411 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2412 /*nonnull=*/1, complain
);
2413 /* If we found the base successfully then we should be able
2414 to convert to it successfully. */
2415 gcc_assert (object
!= error_mark_node
);
2418 /* If MEMBER is from an anonymous aggregate, we have converted
2419 OBJECT so that it refers to the class containing the
2420 anonymous union. Generate a reference to the anonymous union
2421 itself, and recur to find MEMBER. */
2422 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2423 /* When this code is called from build_field_call, the
2424 object already has the type of the anonymous union.
2425 That is because the COMPONENT_REF was already
2426 constructed, and was then disassembled before calling
2427 build_field_call. After the function-call code is
2428 cleaned up, this waste can be eliminated. */
2429 && (!same_type_ignoring_top_level_qualifiers_p
2430 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2432 tree anonymous_union
;
2434 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2435 DECL_CONTEXT (member
));
2436 object
= build_class_member_access_expr (object
,
2438 /*access_path=*/NULL_TREE
,
2443 /* Compute the type of the field, as described in [expr.ref]. */
2444 type_quals
= TYPE_UNQUALIFIED
;
2445 member_type
= TREE_TYPE (member
);
2446 if (TREE_CODE (member_type
) != REFERENCE_TYPE
)
2448 type_quals
= (cp_type_quals (member_type
)
2449 | cp_type_quals (object_type
));
2451 /* A field is const (volatile) if the enclosing object, or the
2452 field itself, is const (volatile). But, a mutable field is
2453 not const, even within a const object. */
2454 if (DECL_MUTABLE_P (member
))
2455 type_quals
&= ~TYPE_QUAL_CONST
;
2456 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2459 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2460 object
, member
, NULL_TREE
);
2462 /* Mark the expression const or volatile, as appropriate. Even
2463 though we've dealt with the type above, we still have to mark the
2464 expression itself. */
2465 if (type_quals
& TYPE_QUAL_CONST
)
2466 TREE_READONLY (result
) = 1;
2467 if (type_quals
& TYPE_QUAL_VOLATILE
)
2468 TREE_THIS_VOLATILE (result
) = 1;
2470 else if (BASELINK_P (member
))
2472 /* The member is a (possibly overloaded) member function. */
2476 /* If the MEMBER is exactly one static member function, then we
2477 know the type of the expression. Otherwise, we must wait
2478 until overload resolution has been performed. */
2479 functions
= BASELINK_FUNCTIONS (member
);
2480 if (TREE_CODE (functions
) == FUNCTION_DECL
2481 && DECL_STATIC_FUNCTION_P (functions
))
2482 type
= TREE_TYPE (functions
);
2484 type
= unknown_type_node
;
2485 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2486 base. That will happen when the function is called. */
2487 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2489 else if (TREE_CODE (member
) == CONST_DECL
)
2491 /* The member is an enumerator. */
2493 /* If OBJECT has side-effects, they are supposed to occur. */
2494 if (TREE_SIDE_EFFECTS (object
))
2495 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2498 else if ((using_decl
= strip_using_decl (member
)) != member
)
2499 result
= build_class_member_access_expr (object
,
2501 access_path
, preserve_reference
,
2505 if (complain
& tf_error
)
2506 error ("invalid use of %qD", member
);
2507 return error_mark_node
;
2510 if (!preserve_reference
)
2513 If E2 is declared to have type "reference to T", then ... the
2514 type of E1.E2 is T. */
2515 result
= convert_from_reference (result
);
2520 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2521 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2524 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2525 tsubst_flags_t complain
)
2527 tree object_type
= TREE_TYPE (object
);
2528 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2531 /* We've already complained about this destructor. */
2532 if (dtor_type
== error_mark_node
)
2533 return error_mark_node
;
2535 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2537 if (complain
& tf_error
)
2538 error ("qualified type %qT does not match destructor name ~%qT",
2540 return error_mark_node
;
2542 if (is_auto (dtor_type
))
2543 dtor_type
= object_type
;
2544 else if (identifier_p (dtor_type
))
2546 /* In a template, names we can't find a match for are still accepted
2547 destructor names, and we check them here. */
2548 if (check_dtor_name (object_type
, dtor_type
))
2549 dtor_type
= object_type
;
2552 if (complain
& tf_error
)
2553 error ("object type %qT does not match destructor name ~%qT",
2554 object_type
, dtor_type
);
2555 return error_mark_node
;
2559 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2561 if (complain
& tf_error
)
2562 error ("the type being destroyed is %qT, but the destructor "
2563 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2564 return error_mark_node
;
2566 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2567 /*protect=*/1, /*want_type=*/false,
2568 tf_warning_or_error
);
2571 if (complain
& tf_error
)
2572 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2573 return error_mark_node
;
2575 expr
= (adjust_result_of_qualified_name_lookup
2576 (expr
, dtor_type
, object_type
));
2577 if (scope
== NULL_TREE
)
2578 /* We need to call adjust_result_of_qualified_name_lookup in case the
2579 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2580 that we still get virtual function binding. */
2581 BASELINK_QUALIFIED_P (expr
) = false;
2585 /* An expression of the form "A::template B" has been resolved to
2586 DECL. Issue a diagnostic if B is not a template or template
2590 check_template_keyword (tree decl
)
2592 /* The standard says:
2596 If a name prefixed by the keyword template is not a member
2597 template, the program is ill-formed.
2599 DR 228 removed the restriction that the template be a member
2602 DR 96, if accepted would add the further restriction that explicit
2603 template arguments must be provided if the template keyword is
2604 used, but, as of 2005-10-16, that DR is still in "drafting". If
2605 this DR is accepted, then the semantic checks here can be
2606 simplified, as the entity named must in fact be a template
2607 specialization, rather than, as at present, a set of overloaded
2608 functions containing at least one template function. */
2609 if (TREE_CODE (decl
) != TEMPLATE_DECL
2610 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2614 if (DECL_USE_TEMPLATE (decl
)
2615 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2618 permerror (input_location
, "%qD is not a template", decl
);
2620 else if (!is_overloaded_fn (decl
))
2621 permerror (input_location
, "%qD is not a template", decl
);
2626 if (BASELINK_P (fns
))
2627 fns
= BASELINK_FUNCTIONS (fns
);
2630 tree fn
= OVL_CURRENT (fns
);
2631 if (TREE_CODE (fn
) == TEMPLATE_DECL
2632 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2634 if (TREE_CODE (fn
) == FUNCTION_DECL
2635 && DECL_USE_TEMPLATE (fn
)
2636 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
2638 fns
= OVL_NEXT (fns
);
2641 permerror (input_location
, "%qD is not a template", decl
);
2646 /* This function is called by the parser to process a class member
2647 access expression of the form OBJECT.NAME. NAME is a node used by
2648 the parser to represent a name; it is not yet a DECL. It may,
2649 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2650 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2651 there is no reason to do the lookup twice, so the parser keeps the
2652 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2653 be a template via the use of the "A::template B" syntax. */
2656 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
2657 tsubst_flags_t complain
)
2662 tree access_path
= NULL_TREE
;
2663 tree orig_object
= object
;
2664 tree orig_name
= name
;
2666 if (object
== error_mark_node
|| name
== error_mark_node
)
2667 return error_mark_node
;
2669 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2670 if (!objc_is_public (object
, name
))
2671 return error_mark_node
;
2673 object_type
= TREE_TYPE (object
);
2675 if (processing_template_decl
)
2677 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2678 type_dependent_object_expression_p (object
)
2679 /* If NAME is "f<args>", where either 'f' or 'args' is
2680 dependent, then the expression is dependent. */
2681 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2682 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2683 TREE_OPERAND (name
, 1)))
2684 /* If NAME is "T::X" where "T" is dependent, then the
2685 expression is dependent. */
2686 || (TREE_CODE (name
) == SCOPE_REF
2687 && TYPE_P (TREE_OPERAND (name
, 0))
2688 && dependent_scope_p (TREE_OPERAND (name
, 0))))
2691 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2692 orig_object
, orig_name
, NULL_TREE
);
2694 object
= build_non_dependent_expr (object
);
2696 else if (c_dialect_objc ()
2697 && identifier_p (name
)
2698 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2703 The type of the first expression shall be "class object" (of a
2705 if (!currently_open_class (object_type
)
2706 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2707 return error_mark_node
;
2708 if (!CLASS_TYPE_P (object_type
))
2710 if (complain
& tf_error
)
2712 if (POINTER_TYPE_P (object_type
)
2713 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2714 error ("request for member %qD in %qE, which is of pointer "
2715 "type %qT (maybe you meant to use %<->%> ?)",
2716 name
, object
.get_value (), object_type
);
2718 error ("request for member %qD in %qE, which is of non-class "
2719 "type %qT", name
, object
.get_value (), object_type
);
2721 return error_mark_node
;
2724 if (BASELINK_P (name
))
2725 /* A member function that has already been looked up. */
2729 bool is_template_id
= false;
2730 tree template_args
= NULL_TREE
;
2733 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2735 is_template_id
= true;
2736 template_args
= TREE_OPERAND (name
, 1);
2737 name
= TREE_OPERAND (name
, 0);
2739 if (TREE_CODE (name
) == OVERLOAD
)
2740 name
= DECL_NAME (get_first_fn (name
));
2741 else if (DECL_P (name
))
2742 name
= DECL_NAME (name
);
2745 if (TREE_CODE (name
) == SCOPE_REF
)
2747 /* A qualified name. The qualifying class or namespace `S'
2748 has already been looked up; it is either a TYPE or a
2750 scope
= TREE_OPERAND (name
, 0);
2751 name
= TREE_OPERAND (name
, 1);
2753 /* If SCOPE is a namespace, then the qualified name does not
2754 name a member of OBJECT_TYPE. */
2755 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2757 if (complain
& tf_error
)
2758 error ("%<%D::%D%> is not a member of %qT",
2759 scope
, name
, object_type
);
2760 return error_mark_node
;
2763 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2765 /* Looking up a member enumerator (c++/56793). */
2766 if (!TYPE_CLASS_SCOPE_P (scope
)
2767 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2769 if (complain
& tf_error
)
2770 error ("%<%D::%D%> is not a member of %qT",
2771 scope
, name
, object_type
);
2772 return error_mark_node
;
2774 tree val
= lookup_enumerator (scope
, name
);
2777 if (complain
& tf_error
)
2778 error ("%qD is not a member of %qD",
2780 return error_mark_node
;
2783 if (TREE_SIDE_EFFECTS (object
))
2784 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
2788 gcc_assert (CLASS_TYPE_P (scope
));
2789 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
2791 if (constructor_name_p (name
, scope
))
2793 if (complain
& tf_error
)
2794 error ("cannot call constructor %<%T::%D%> directly",
2796 return error_mark_node
;
2799 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2800 access_path
= lookup_base (object_type
, scope
, ba_check
,
2802 if (access_path
== error_mark_node
)
2803 return error_mark_node
;
2806 if (any_dependent_bases_p (object_type
))
2808 if (complain
& tf_error
)
2809 error ("%qT is not a base of %qT", scope
, object_type
);
2810 return error_mark_node
;
2816 access_path
= object_type
;
2819 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2821 if (dependent_type_p (object_type
))
2822 /* The destructor isn't declared yet. */
2824 member
= lookup_destructor (object
, scope
, name
, complain
);
2828 /* Look up the member. */
2829 member
= lookup_member (access_path
, name
, /*protect=*/1,
2830 /*want_type=*/false, complain
);
2831 if (member
== NULL_TREE
)
2833 if (dependent_type_p (object_type
))
2834 /* Try again at instantiation time. */
2836 if (complain
& tf_error
)
2838 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
2839 /*want_type=*/false);
2842 location_t bogus_component_loc
= input_location
;
2843 gcc_rich_location
rich_loc (bogus_component_loc
);
2844 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
2848 "%q#T has no member named %qE; did you mean %qE?",
2849 TREE_CODE (access_path
) == TREE_BINFO
2850 ? TREE_TYPE (access_path
) : object_type
, name
,
2854 error ("%q#T has no member named %qE",
2855 TREE_CODE (access_path
) == TREE_BINFO
2856 ? TREE_TYPE (access_path
) : object_type
, name
);
2858 return error_mark_node
;
2860 if (member
== error_mark_node
)
2861 return error_mark_node
;
2863 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
2864 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2865 wrong, so don't use it. */
2867 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
2873 tree templ
= member
;
2875 if (BASELINK_P (templ
))
2876 templ
= lookup_template_function (templ
, template_args
);
2879 if (complain
& tf_error
)
2880 error ("%qD is not a member template function", name
);
2881 return error_mark_node
;
2886 if (TREE_DEPRECATED (member
))
2887 warn_deprecated_use (member
, NULL_TREE
);
2890 check_template_keyword (member
);
2892 expr
= build_class_member_access_expr (object
, member
, access_path
,
2893 /*preserve_reference=*/false,
2895 if (processing_template_decl
&& expr
!= error_mark_node
)
2897 if (BASELINK_P (member
))
2899 if (TREE_CODE (orig_name
) == SCOPE_REF
)
2900 BASELINK_QUALIFIED_P (member
) = 1;
2903 return build_min_non_dep (COMPONENT_REF
, expr
,
2904 orig_object
, orig_name
,
2911 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2915 build_simple_component_ref (tree object
, tree member
)
2917 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
2918 cp_type_quals (TREE_TYPE (object
)));
2919 return build3_loc (input_location
,
2920 COMPONENT_REF
, type
,
2921 object
, member
, NULL_TREE
);
2924 /* Return an expression for the MEMBER_NAME field in the internal
2925 representation of PTRMEM, a pointer-to-member function. (Each
2926 pointer-to-member function type gets its own RECORD_TYPE so it is
2927 more convenient to access the fields by name than by FIELD_DECL.)
2928 This routine converts the NAME to a FIELD_DECL and then creates the
2929 node for the complete expression. */
2932 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
2937 /* This code is a stripped down version of
2938 build_class_member_access_expr. It does not work to use that
2939 routine directly because it expects the object to be of class
2941 ptrmem_type
= TREE_TYPE (ptrmem
);
2942 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
2943 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
2944 member
= DECL_CHAIN (member
))
2945 if (DECL_NAME (member
) == member_name
)
2947 return build_simple_component_ref (ptrmem
, member
);
2950 /* Given an expression PTR for a pointer, return an expression
2951 for the value pointed to.
2952 ERRORSTRING is the name of the operator to appear in error messages.
2954 This function may need to overload OPERATOR_FNNAME.
2955 Must also handle REFERENCE_TYPEs for C++. */
2958 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
2959 tsubst_flags_t complain
)
2961 tree orig_expr
= expr
;
2963 tree overload
= NULL_TREE
;
2965 if (processing_template_decl
)
2967 /* Retain the type if we know the operand is a pointer. */
2968 if (TREE_TYPE (expr
) && POINTER_TYPE_P (TREE_TYPE (expr
)))
2969 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
2970 if (type_dependent_expression_p (expr
))
2971 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
2972 expr
= build_non_dependent_expr (expr
);
2975 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
2976 NULL_TREE
, NULL_TREE
, &overload
, complain
);
2978 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
2980 if (processing_template_decl
&& rval
!= error_mark_node
)
2982 if (overload
!= NULL_TREE
)
2983 return (build_min_non_dep_op_overload
2984 (INDIRECT_REF
, rval
, overload
, orig_expr
));
2986 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
2992 /* Helper function called from c-common. */
2994 build_indirect_ref (location_t
/*loc*/,
2995 tree ptr
, ref_operator errorstring
)
2997 return cp_build_indirect_ref (ptr
, errorstring
, tf_warning_or_error
);
3001 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
3002 tsubst_flags_t complain
)
3006 if (ptr
== current_class_ptr
3007 || (TREE_CODE (ptr
) == NOP_EXPR
3008 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3009 && (same_type_ignoring_top_level_qualifiers_p
3010 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3011 return current_class_ref
;
3013 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
3014 ? ptr
: decay_conversion (ptr
, complain
));
3015 if (pointer
== error_mark_node
)
3016 return error_mark_node
;
3018 type
= TREE_TYPE (pointer
);
3020 if (POINTER_TYPE_P (type
))
3024 If the type of the expression is "pointer to T," the type
3025 of the result is "T." */
3026 tree t
= TREE_TYPE (type
);
3028 if ((CONVERT_EXPR_P (ptr
)
3029 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3030 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3032 /* If a warning is issued, mark it to avoid duplicates from
3033 the backend. This only needs to be done at
3034 warn_strict_aliasing > 2. */
3035 if (warn_strict_aliasing
> 2)
3036 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr
, 0)),
3037 type
, TREE_OPERAND (ptr
, 0)))
3038 TREE_NO_WARNING (ptr
) = 1;
3041 if (VOID_TYPE_P (t
))
3043 /* A pointer to incomplete type (other than cv void) can be
3044 dereferenced [expr.unary.op]/1 */
3045 if (complain
& tf_error
)
3046 error ("%qT is not a pointer-to-object type", type
);
3047 return error_mark_node
;
3049 else if (TREE_CODE (pointer
) == ADDR_EXPR
3050 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3051 /* The POINTER was something like `&x'. We simplify `*&x' to
3053 return TREE_OPERAND (pointer
, 0);
3056 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3058 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3059 so that we get the proper error message if the result is used
3060 to assign to. Also, &* is supposed to be a no-op. */
3061 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3062 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3063 TREE_SIDE_EFFECTS (ref
)
3064 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3068 else if (!(complain
& tf_error
))
3069 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3071 /* `pointer' won't be an error_mark_node if we were given a
3072 pointer to member, so it's cool to check for this here. */
3073 else if (TYPE_PTRMEM_P (type
))
3074 switch (errorstring
)
3076 case RO_ARRAY_INDEXING
:
3077 error ("invalid use of array indexing on pointer to member");
3080 error ("invalid use of unary %<*%> on pointer to member");
3082 case RO_IMPLICIT_CONVERSION
:
3083 error ("invalid use of implicit conversion on pointer to member");
3086 error ("left hand operand of %<->*%> must be a pointer to class, "
3087 "but is a pointer to member of type %qT", type
);
3092 else if (pointer
!= error_mark_node
)
3093 invalid_indirection_error (input_location
, type
, errorstring
);
3095 return error_mark_node
;
3098 /* This handles expressions of the form "a[i]", which denotes
3101 This is logically equivalent in C to *(a+i), but we may do it differently.
3102 If A is a variable or a member, we generate a primitive ARRAY_REF.
3103 This avoids forcing the array out of registers, and can work on
3104 arrays that are not lvalues (for example, members of structures returned
3107 If INDEX is of some user-defined type, it must be converted to
3108 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3109 will inherit the type of the array, which will be some pointer type.
3111 LOC is the location to use in building the array reference. */
3114 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3115 tsubst_flags_t complain
)
3121 if (complain
& tf_error
)
3122 error_at (loc
, "subscript missing in array reference");
3123 return error_mark_node
;
3126 /* If an array's index is an array notation, then its rank cannot be
3127 greater than one. */
3128 if (flag_cilkplus
&& contains_array_notation_expr (idx
))
3132 /* If find_rank returns false, then it should have reported an error,
3133 thus it is unnecessary for repetition. */
3134 if (!find_rank (loc
, idx
, idx
, true, &rank
))
3135 return error_mark_node
;
3138 error_at (loc
, "rank of the array%'s index is greater than 1");
3139 return error_mark_node
;
3142 if (TREE_TYPE (array
) == error_mark_node
3143 || TREE_TYPE (idx
) == error_mark_node
)
3144 return error_mark_node
;
3146 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3148 switch (TREE_CODE (array
))
3152 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3154 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3155 TREE_OPERAND (array
, 0), value
);
3156 SET_EXPR_LOCATION (ret
, loc
);
3161 ret
= build_conditional_expr
3162 (loc
, TREE_OPERAND (array
, 0),
3163 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3165 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3168 protected_set_expr_location (ret
, loc
);
3175 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3177 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3181 warn_array_subscript_with_type_char (loc
, idx
);
3183 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3185 if (complain
& tf_error
)
3186 error_at (loc
, "array subscript is not an integer");
3187 return error_mark_node
;
3190 /* Apply integral promotions *after* noticing character types.
3191 (It is unclear why we do these promotions -- the standard
3192 does not say that we should. In fact, the natural thing would
3193 seem to be to convert IDX to ptrdiff_t; we're performing
3194 pointer arithmetic.) */
3195 idx
= cp_perform_integral_promotions (idx
, complain
);
3197 /* An array that is indexed by a non-constant
3198 cannot be stored in a register; we must be able to do
3199 address arithmetic on its address.
3200 Likewise an array of elements of variable size. */
3201 if (TREE_CODE (idx
) != INTEGER_CST
3202 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3203 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3206 if (!cxx_mark_addressable (array
))
3207 return error_mark_node
;
3210 /* An array that is indexed by a constant value which is not within
3211 the array bounds cannot be stored in a register either; because we
3212 would get a crash in store_bit_field/extract_bit_field when trying
3213 to access a non-existent part of the register. */
3214 if (TREE_CODE (idx
) == INTEGER_CST
3215 && TYPE_DOMAIN (TREE_TYPE (array
))
3216 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3218 if (!cxx_mark_addressable (array
))
3219 return error_mark_node
;
3222 /* Note in C++ it is valid to subscript a `register' array, since
3223 it is valid to take the address of something with that
3224 storage specification. */
3228 while (TREE_CODE (foo
) == COMPONENT_REF
)
3229 foo
= TREE_OPERAND (foo
, 0);
3230 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3231 && (complain
& tf_warning
))
3232 warning_at (loc
, OPT_Wextra
,
3233 "subscripting array declared %<register%>");
3236 type
= TREE_TYPE (TREE_TYPE (array
));
3237 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3238 /* Array ref is const/volatile if the array elements are
3239 or if the array is.. */
3240 TREE_READONLY (rval
)
3241 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3242 TREE_SIDE_EFFECTS (rval
)
3243 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3244 TREE_THIS_VOLATILE (rval
)
3245 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3246 ret
= require_complete_type_sfinae (rval
, complain
);
3247 protected_set_expr_location (ret
, loc
);
3249 ret
= non_lvalue_loc (loc
, ret
);
3254 tree ar
= cp_default_conversion (array
, complain
);
3255 tree ind
= cp_default_conversion (idx
, complain
);
3257 /* Put the integer in IND to simplify error checking. */
3258 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3259 std::swap (ar
, ind
);
3261 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3262 return error_mark_node
;
3264 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3266 if (complain
& tf_error
)
3267 error_at (loc
, "subscripted value is neither array nor pointer");
3268 return error_mark_node
;
3270 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3272 if (complain
& tf_error
)
3273 error_at (loc
, "array subscript is not an integer");
3274 return error_mark_node
;
3277 warn_array_subscript_with_type_char (loc
, idx
);
3279 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3284 protected_set_expr_location (ret
, loc
);
3286 ret
= non_lvalue_loc (loc
, ret
);
3291 /* Entry point for Obj-C++. */
3294 build_array_ref (location_t loc
, tree array
, tree idx
)
3296 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3299 /* Resolve a pointer to member function. INSTANCE is the object
3300 instance to use, if the member points to a virtual member.
3302 This used to avoid checking for virtual functions if basetype
3303 has no virtual functions, according to an earlier ANSI draft.
3304 With the final ISO C++ rules, such an optimization is
3305 incorrect: A pointer to a derived member can be static_cast
3306 to pointer-to-base-member, as long as the dynamic object
3307 later has the right member. So now we only do this optimization
3308 when we know the dynamic type of the object. */
3311 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3312 tsubst_flags_t complain
)
3314 if (TREE_CODE (function
) == OFFSET_REF
)
3315 function
= TREE_OPERAND (function
, 1);
3317 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3319 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3321 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3322 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3324 tree instance_ptr
= *instance_ptrptr
;
3325 tree instance_save_expr
= 0;
3326 if (instance_ptr
== error_mark_node
)
3328 if (TREE_CODE (function
) == PTRMEM_CST
)
3330 /* Extracting the function address from a pmf is only
3331 allowed with -Wno-pmf-conversions. It only works for
3333 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3334 e1
= convert (fntype
, e1
);
3339 if (complain
& tf_error
)
3340 error ("object missing in use of %qE", function
);
3341 return error_mark_node
;
3345 /* True if we know that the dynamic type of the object doesn't have
3346 virtual functions, so we can assume the PFN field is a pointer. */
3347 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3348 && !TYPE_POLYMORPHIC_P (basetype
)
3349 && resolves_to_fixed_type_p (instance_ptr
, 0));
3351 /* If we don't really have an object (i.e. in an ill-formed
3352 conversion from PMF to pointer), we can't resolve virtual
3353 functions anyway. */
3354 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3357 if (TREE_SIDE_EFFECTS (instance_ptr
))
3358 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3360 if (TREE_SIDE_EFFECTS (function
))
3361 function
= save_expr (function
);
3363 /* Start by extracting all the information from the PMF itself. */
3364 e3
= pfn_from_ptrmemfunc (function
);
3365 delta
= delta_from_ptrmemfunc (function
);
3366 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3367 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3369 int flag_sanitize_save
;
3370 case ptrmemfunc_vbit_in_pfn
:
3371 e1
= cp_build_binary_op (input_location
,
3372 BIT_AND_EXPR
, idx
, integer_one_node
,
3374 idx
= cp_build_binary_op (input_location
,
3375 MINUS_EXPR
, idx
, integer_one_node
,
3377 if (idx
== error_mark_node
)
3378 return error_mark_node
;
3381 case ptrmemfunc_vbit_in_delta
:
3382 e1
= cp_build_binary_op (input_location
,
3383 BIT_AND_EXPR
, delta
, integer_one_node
,
3385 /* Don't instrument the RSHIFT_EXPR we're about to create because
3386 we're going to use DELTA number of times, and that wouldn't play
3387 well with SAVE_EXPRs therein. */
3388 flag_sanitize_save
= flag_sanitize
;
3390 delta
= cp_build_binary_op (input_location
,
3391 RSHIFT_EXPR
, delta
, integer_one_node
,
3393 flag_sanitize
= flag_sanitize_save
;
3394 if (delta
== error_mark_node
)
3395 return error_mark_node
;
3402 if (e1
== error_mark_node
)
3403 return error_mark_node
;
3405 /* Convert down to the right base before using the instance. A
3406 special case is that in a pointer to member of class C, C may
3407 be incomplete. In that case, the function will of course be
3408 a member of C, and no conversion is required. In fact,
3409 lookup_base will fail in that case, because incomplete
3410 classes do not have BINFOs. */
3411 if (!same_type_ignoring_top_level_qualifiers_p
3412 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3414 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3415 basetype
, ba_check
, NULL
, complain
);
3416 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3418 if (instance_ptr
== error_mark_node
)
3419 return error_mark_node
;
3421 /* ...and then the delta in the PMF. */
3422 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3424 /* Hand back the adjusted 'this' argument to our caller. */
3425 *instance_ptrptr
= instance_ptr
;
3428 /* Now just return the pointer. */
3431 /* Next extract the vtable pointer from the object. */
3432 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3434 vtbl
= cp_build_indirect_ref (vtbl
, RO_NULL
, complain
);
3435 if (vtbl
== error_mark_node
)
3436 return error_mark_node
;
3438 /* Finally, extract the function pointer from the vtable. */
3439 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3440 e2
= cp_build_indirect_ref (e2
, RO_NULL
, complain
);
3441 if (e2
== error_mark_node
)
3442 return error_mark_node
;
3443 TREE_CONSTANT (e2
) = 1;
3445 /* When using function descriptors, the address of the
3446 vtable entry is treated as a function pointer. */
3447 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3448 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3449 cp_build_addr_expr (e2
, complain
));
3451 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3452 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3453 if (e1
== error_mark_node
)
3454 return error_mark_node
;
3456 /* Make sure this doesn't get evaluated first inside one of the
3457 branches of the COND_EXPR. */
3458 if (instance_save_expr
)
3459 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3460 instance_save_expr
, e1
);
3467 /* Used by the C-common bits. */
3469 build_function_call (location_t
/*loc*/,
3470 tree function
, tree params
)
3472 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3475 /* Used by the C-common bits. */
3477 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3478 tree function
, vec
<tree
, va_gc
> *params
,
3479 vec
<tree
, va_gc
> * /*origtypes*/)
3481 vec
<tree
, va_gc
> *orig_params
= params
;
3482 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3483 tf_warning_or_error
);
3485 /* cp_build_function_call_vec can reallocate PARAMS by adding
3486 default arguments. That should never happen here. Verify
3488 gcc_assert (params
== orig_params
);
3493 /* Build a function call using a tree list of arguments. */
3496 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3498 vec
<tree
, va_gc
> *vec
;
3501 vec
= make_tree_vector ();
3502 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3503 vec_safe_push (vec
, TREE_VALUE (params
));
3504 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3505 release_tree_vector (vec
);
3509 /* Build a function call using varargs. */
3512 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3514 vec
<tree
, va_gc
> *vec
;
3518 vec
= make_tree_vector ();
3519 va_start (args
, complain
);
3520 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3521 vec_safe_push (vec
, t
);
3523 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3524 release_tree_vector (vec
);
3528 /* Build a function call using a vector of arguments. PARAMS may be
3529 NULL if there are no parameters. This changes the contents of
3533 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3534 tsubst_flags_t complain
)
3536 tree fntype
, fndecl
;
3538 tree original
= function
;
3542 vec
<tree
, va_gc
> *allocated
= NULL
;
3545 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3546 expressions, like those used for ObjC messenger dispatches. */
3547 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3548 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3550 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3551 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3552 if (TREE_CODE (function
) == NOP_EXPR
3553 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3554 function
= TREE_OPERAND (function
, 0);
3556 if (TREE_CODE (function
) == FUNCTION_DECL
)
3558 /* If the function is a non-template member function
3559 or a non-template friend, then we need to check the
3562 Note that if overload resolution failed with a single
3563 candidate this function will be used to explicitly diagnose
3564 the failure for the single call expression. The check is
3565 technically redundant since we also would have failed in
3566 add_function_candidate. */
3568 && (complain
& tf_error
)
3569 && !constraints_satisfied_p (function
))
3571 error ("cannot call function %qD", function
);
3572 location_t loc
= DECL_SOURCE_LOCATION (function
);
3573 diagnose_constraints (loc
, function
, NULL_TREE
);
3574 return error_mark_node
;
3577 if (!mark_used (function
, complain
) && !(complain
& tf_error
))
3578 return error_mark_node
;
3581 /* Convert anything with function type to a pointer-to-function. */
3582 if (DECL_MAIN_P (function
))
3584 if (complain
& tf_error
)
3585 pedwarn (input_location
, OPT_Wpedantic
,
3586 "ISO C++ forbids calling %<::main%> from within program");
3588 return error_mark_node
;
3590 function
= build_addr_func (function
, complain
);
3596 function
= build_addr_func (function
, complain
);
3599 if (function
== error_mark_node
)
3600 return error_mark_node
;
3602 fntype
= TREE_TYPE (function
);
3604 if (TYPE_PTRMEMFUNC_P (fntype
))
3606 if (complain
& tf_error
)
3607 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3608 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3609 original
, original
);
3610 return error_mark_node
;
3613 is_method
= (TYPE_PTR_P (fntype
)
3614 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3616 if (!(TYPE_PTRFN_P (fntype
)
3618 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3620 if (complain
& tf_error
)
3622 if (!flag_diagnostics_show_caret
)
3623 error_at (input_location
,
3624 "%qE cannot be used as a function", original
);
3625 else if (DECL_P (original
))
3626 error_at (input_location
,
3627 "%qD cannot be used as a function", original
);
3629 error_at (input_location
,
3630 "expression cannot be used as a function");
3633 return error_mark_node
;
3636 /* fntype now gets the type of function pointed to. */
3637 fntype
= TREE_TYPE (fntype
);
3638 parm_types
= TYPE_ARG_TYPES (fntype
);
3642 allocated
= make_tree_vector ();
3643 params
= &allocated
;
3646 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3649 return error_mark_node
;
3651 argarray
= (*params
)->address ();
3653 /* Check for errors in format strings and inappropriately
3655 check_function_arguments (input_location
, fntype
, nargs
, argarray
);
3657 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3659 if (allocated
!= NULL
)
3660 release_tree_vector (allocated
);
3665 /* Subroutine of convert_arguments.
3666 Print an error message about a wrong number of arguments. */
3669 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3673 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3675 if (DECL_NAME (fndecl
) == NULL_TREE
3676 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3679 ? G_("too many arguments to constructor %q#D")
3680 : G_("too few arguments to constructor %q#D"),
3685 ? G_("too many arguments to member function %q#D")
3686 : G_("too few arguments to member function %q#D"),
3692 ? G_("too many arguments to function %q#D")
3693 : G_("too few arguments to function %q#D"),
3695 if (!DECL_IS_BUILTIN (fndecl
))
3696 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
3700 if (c_dialect_objc () && objc_message_selector ())
3703 ? G_("too many arguments to method %q#D")
3704 : G_("too few arguments to method %q#D"),
3705 objc_message_selector ());
3707 error_at (loc
, too_many_p
? G_("too many arguments to function")
3708 : G_("too few arguments to function"));
3712 /* Convert the actual parameter expressions in the list VALUES to the
3713 types in the list TYPELIST. The converted expressions are stored
3714 back in the VALUES vector.
3715 If parmdecls is exhausted, or when an element has NULL as its type,
3716 perform the default conversions.
3718 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3720 This is also where warnings about wrong number of args are generated.
3722 Returns the actual number of arguments processed (which might be less
3723 than the length of the vector), or -1 on error.
3725 In C++, unspecified trailing parameters can be filled in with their
3726 default arguments, if such were specified. Do so here. */
3729 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3730 int flags
, tsubst_flags_t complain
)
3735 /* Argument passing is always copy-initialization. */
3736 flags
|= LOOKUP_ONLYCONVERTING
;
3738 for (i
= 0, typetail
= typelist
;
3739 i
< vec_safe_length (*values
);
3742 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3743 tree val
= (**values
)[i
];
3745 if (val
== error_mark_node
|| type
== error_mark_node
)
3748 if (type
== void_type_node
)
3750 if (complain
& tf_error
)
3752 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3759 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3760 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3761 if (TREE_CODE (val
) == NOP_EXPR
3762 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3763 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3764 val
= TREE_OPERAND (val
, 0);
3766 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3768 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3769 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3770 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3771 val
= decay_conversion (val
, complain
);
3774 if (val
== error_mark_node
)
3779 /* Formal parm type is specified by a function prototype. */
3782 if (!COMPLETE_TYPE_P (complete_type (type
)))
3784 if (complain
& tf_error
)
3787 error ("parameter %P of %qD has incomplete type %qT",
3790 error ("parameter %P has incomplete type %qT", i
, type
);
3792 parmval
= error_mark_node
;
3796 parmval
= convert_for_initialization
3797 (NULL_TREE
, type
, val
, flags
,
3798 ICR_ARGPASS
, fndecl
, i
, complain
);
3799 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
3802 if (parmval
== error_mark_node
)
3805 (**values
)[i
] = parmval
;
3809 if (fndecl
&& magic_varargs_p (fndecl
))
3810 /* Don't do ellipsis conversion for __built_in_constant_p
3811 as this will result in spurious errors for non-trivial
3813 val
= require_complete_type_sfinae (val
, complain
);
3815 val
= convert_arg_to_ellipsis (val
, complain
);
3817 (**values
)[i
] = val
;
3821 typetail
= TREE_CHAIN (typetail
);
3824 if (typetail
!= 0 && typetail
!= void_list_node
)
3826 /* See if there are default arguments that can be used. Because
3827 we hold default arguments in the FUNCTION_TYPE (which is so
3828 wrong), we can see default parameters here from deduced
3829 contexts (and via typeof) for indirect function calls.
3830 Fortunately we know whether we have a function decl to
3831 provide default arguments in a language conformant
3833 if (fndecl
&& TREE_PURPOSE (typetail
)
3834 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
3836 for (; typetail
!= void_list_node
; ++i
)
3839 = convert_default_arg (TREE_VALUE (typetail
),
3840 TREE_PURPOSE (typetail
),
3841 fndecl
, i
, complain
);
3843 if (parmval
== error_mark_node
)
3846 vec_safe_push (*values
, parmval
);
3847 typetail
= TREE_CHAIN (typetail
);
3848 /* ends with `...'. */
3849 if (typetail
== NULL_TREE
)
3855 if (complain
& tf_error
)
3856 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
3864 /* Build a binary-operation expression, after performing default
3865 conversions on the operands. CODE is the kind of expression to
3866 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3867 are the tree codes which correspond to ARG1 and ARG2 when issuing
3868 warnings about possibly misplaced parentheses. They may differ
3869 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3870 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3871 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3872 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3873 ARG2_CODE as ERROR_MARK. */
3876 build_x_binary_op (location_t loc
, enum tree_code code
, tree arg1
,
3877 enum tree_code arg1_code
, tree arg2
,
3878 enum tree_code arg2_code
, tree
*overload_p
,
3879 tsubst_flags_t complain
)
3884 tree overload
= NULL_TREE
;
3889 if (processing_template_decl
)
3891 if (type_dependent_expression_p (arg1
)
3892 || type_dependent_expression_p (arg2
))
3893 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
3894 arg1
= build_non_dependent_expr (arg1
);
3895 arg2
= build_non_dependent_expr (arg2
);
3898 if (code
== DOTSTAR_EXPR
)
3899 expr
= build_m_component_ref (arg1
, arg2
, complain
);
3901 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
3902 &overload
, complain
);
3904 if (overload_p
!= NULL
)
3905 *overload_p
= overload
;
3907 /* Check for cases such as x+y<<z which users are likely to
3908 misinterpret. But don't warn about obj << x + y, since that is a
3909 common idiom for I/O. */
3910 if (warn_parentheses
3911 && (complain
& tf_warning
)
3912 && !processing_template_decl
3913 && !error_operand_p (arg1
)
3914 && !error_operand_p (arg2
)
3915 && (code
!= LSHIFT_EXPR
3916 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
3917 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
3918 arg2_code
, orig_arg2
);
3920 if (processing_template_decl
&& expr
!= error_mark_node
)
3922 if (overload
!= NULL_TREE
)
3923 return (build_min_non_dep_op_overload
3924 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
3926 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
3932 /* Build and return an ARRAY_REF expression. */
3935 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
3936 tsubst_flags_t complain
)
3938 tree orig_arg1
= arg1
;
3939 tree orig_arg2
= arg2
;
3941 tree overload
= NULL_TREE
;
3943 if (processing_template_decl
)
3945 if (type_dependent_expression_p (arg1
)
3946 || type_dependent_expression_p (arg2
))
3947 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
3948 NULL_TREE
, NULL_TREE
);
3949 arg1
= build_non_dependent_expr (arg1
);
3950 arg2
= build_non_dependent_expr (arg2
);
3953 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
3954 NULL_TREE
, &overload
, complain
);
3956 if (processing_template_decl
&& expr
!= error_mark_node
)
3958 if (overload
!= NULL_TREE
)
3959 return (build_min_non_dep_op_overload
3960 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
3962 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
3963 NULL_TREE
, NULL_TREE
);
3968 /* Return whether OP is an expression of enum type cast to integer
3969 type. In C++ even unsigned enum types are cast to signed integer
3970 types. We do not want to issue warnings about comparisons between
3971 signed and unsigned types when one of the types is an enum type.
3972 Those warnings are always false positives in practice. */
3975 enum_cast_to_int (tree op
)
3977 if (CONVERT_EXPR_P (op
)
3978 && TREE_TYPE (op
) == integer_type_node
3979 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
3980 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
3983 /* The cast may have been pushed into a COND_EXPR. */
3984 if (TREE_CODE (op
) == COND_EXPR
)
3985 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
3986 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
3991 /* For the c-common bits. */
3993 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
3996 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
3999 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4000 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4003 build_vec_cmp (tree_code code
, tree type
,
4004 tree arg0
, tree arg1
)
4006 tree zero_vec
= build_zero_cst (type
);
4007 tree minus_one_vec
= build_minus_one_cst (type
);
4008 tree cmp_type
= build_same_sized_truth_vector_type(type
);
4009 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4010 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4013 /* Possibly warn about an address never being NULL. */
4016 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4019 || (complain
& tf_warning
) == 0
4020 || c_inhibit_evaluation_warnings
!= 0
4021 || TREE_NO_WARNING (op
))
4024 tree cop
= fold_non_dependent_expr (op
);
4026 if (TREE_CODE (cop
) == ADDR_EXPR
4027 && decl_with_nonnull_addr_p (TREE_OPERAND (cop
, 0))
4028 && !TREE_NO_WARNING (cop
))
4029 warning_at (location
, OPT_Waddress
, "the address of %qD will never "
4030 "be NULL", TREE_OPERAND (cop
, 0));
4032 if (CONVERT_EXPR_P (op
)
4033 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == REFERENCE_TYPE
)
4036 STRIP_NOPS (inner_op
);
4038 if (DECL_P (inner_op
))
4039 warning_at (location
, OPT_Waddress
,
4040 "the compiler can assume that the address of "
4041 "%qD will never be NULL", inner_op
);
4045 /* Build a binary-operation expression without default conversions.
4046 CODE is the kind of expression to build.
4047 LOCATION is the location_t of the operator in the source code.
4048 This function differs from `build' in several ways:
4049 the data type of the result is computed and recorded in it,
4050 warnings are generated if arg data types are invalid,
4051 special handling for addition and subtraction of pointers is known,
4052 and some optimization is done (operations on narrow ints
4053 are done in the narrower type when that gives the same result).
4054 Constant folding is also done before the result is returned.
4056 Note that the operands will never have enumeral types
4057 because either they have just had the default conversions performed
4058 or they have both just been converted to some other type in which
4059 the arithmetic is to be done.
4061 C++: must do special pointer arithmetic when implementing
4062 multiple inheritance, and deal with pointer to member functions. */
4065 cp_build_binary_op (location_t location
,
4066 enum tree_code code
, tree orig_op0
, tree orig_op1
,
4067 tsubst_flags_t complain
)
4070 enum tree_code code0
, code1
;
4072 const char *invalid_op_diag
;
4074 /* Expression code to give to the expression when it is built.
4075 Normally this is CODE, which is what the caller asked for,
4076 but in some special cases we change it. */
4077 enum tree_code resultcode
= code
;
4079 /* Data type in which the computation is to be performed.
4080 In the simplest cases this is the common type of the arguments. */
4081 tree result_type
= NULL
;
4083 /* Nonzero means operands have already been type-converted
4084 in whatever way is necessary.
4085 Zero means they need to be converted to RESULT_TYPE. */
4088 /* Nonzero means create the expression with this type, rather than
4090 tree build_type
= 0;
4092 /* Nonzero means after finally constructing the expression
4093 convert it to this type. */
4094 tree final_type
= 0;
4096 tree result
, result_ovl
;
4097 tree orig_type
= NULL
;
4099 /* Nonzero if this is an operation like MIN or MAX which can
4100 safely be computed in short if both args are promoted shorts.
4101 Also implies COMMON.
4102 -1 indicates a bitwise operation; this makes a difference
4103 in the exact conditions for when it is safe to do the operation
4104 in a narrower mode. */
4107 /* Nonzero if this is a comparison operation;
4108 if both args are promoted shorts, compare the original shorts.
4109 Also implies COMMON. */
4110 int short_compare
= 0;
4112 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4115 /* True if both operands have arithmetic type. */
4116 bool arithmetic_types_p
;
4118 /* Apply default conversions. */
4122 /* Remember whether we're doing / or %. */
4123 bool doing_div_or_mod
= false;
4125 /* Remember whether we're doing << or >>. */
4126 bool doing_shift
= false;
4128 /* Tree holding instrumentation expression. */
4129 tree instrument_expr
= NULL
;
4131 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
4132 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
4133 || code
== TRUTH_XOR_EXPR
)
4135 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4136 op0
= decay_conversion (op0
, complain
);
4137 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4138 op1
= decay_conversion (op1
, complain
);
4142 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4143 op0
= cp_default_conversion (op0
, complain
);
4144 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4145 op1
= cp_default_conversion (op1
, complain
);
4148 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4149 STRIP_TYPE_NOPS (op0
);
4150 STRIP_TYPE_NOPS (op1
);
4152 /* DTRT if one side is an overloaded function, but complain about it. */
4153 if (type_unknown_p (op0
))
4155 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
4156 if (t
!= error_mark_node
)
4158 if (complain
& tf_error
)
4159 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4164 if (type_unknown_p (op1
))
4166 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4167 if (t
!= error_mark_node
)
4169 if (complain
& tf_error
)
4170 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4176 type0
= TREE_TYPE (op0
);
4177 type1
= TREE_TYPE (op1
);
4179 /* The expression codes of the data types of the arguments tell us
4180 whether the arguments are integers, floating, pointers, etc. */
4181 code0
= TREE_CODE (type0
);
4182 code1
= TREE_CODE (type1
);
4184 /* If an error was already reported for one of the arguments,
4185 avoid reporting another error. */
4186 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4187 return error_mark_node
;
4189 if ((invalid_op_diag
4190 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4192 if (complain
& tf_error
)
4193 error (invalid_op_diag
);
4194 return error_mark_node
;
4197 /* Issue warnings about peculiar, but valid, uses of NULL. */
4198 if ((orig_op0
== null_node
|| orig_op1
== null_node
)
4199 /* It's reasonable to use pointer values as operands of &&
4200 and ||, so NULL is no exception. */
4201 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4202 && ( /* Both are NULL (or 0) and the operation was not a
4203 comparison or a pointer subtraction. */
4204 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4205 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4206 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4207 || (!null_ptr_cst_p (orig_op0
)
4208 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4209 || (!null_ptr_cst_p (orig_op1
)
4210 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4211 && (complain
& tf_warning
))
4213 source_location loc
=
4214 expansion_point_location_if_in_system_header (input_location
);
4216 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4219 /* In case when one of the operands of the binary operation is
4220 a vector and another is a scalar -- convert scalar to vector. */
4221 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4223 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4224 complain
& tf_error
);
4226 switch (convert_flag
)
4229 return error_mark_node
;
4232 op0
= convert (TREE_TYPE (type1
), op0
);
4233 op0
= save_expr (op0
);
4234 op0
= build_vector_from_val (type1
, op0
);
4235 type0
= TREE_TYPE (op0
);
4236 code0
= TREE_CODE (type0
);
4242 op1
= convert (TREE_TYPE (type0
), op1
);
4243 op1
= save_expr (op1
);
4244 op1
= build_vector_from_val (type0
, op1
);
4245 type1
= TREE_TYPE (op1
);
4246 code1
= TREE_CODE (type1
);
4258 /* Subtraction of two similar pointers.
4259 We must subtract them as integers, then divide by object size. */
4260 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4261 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4263 return pointer_diff (location
, op0
, op1
,
4264 common_pointer_type (type0
, type1
), complain
);
4265 /* In all other cases except pointer - int, the usual arithmetic
4267 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4272 /* The pointer - int case is just like pointer + int; fall
4276 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4277 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4281 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4282 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4283 if (processing_template_decl
)
4285 result_type
= TREE_TYPE (ptr_operand
);
4288 return cp_pointer_int_sum (location
, code
,
4300 case TRUNC_DIV_EXPR
:
4302 case FLOOR_DIV_EXPR
:
4303 case ROUND_DIV_EXPR
:
4304 case EXACT_DIV_EXPR
:
4305 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4306 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4307 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4308 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4310 enum tree_code tcode0
= code0
, tcode1
= code1
;
4311 tree cop1
= fold_non_dependent_expr (op1
);
4312 doing_div_or_mod
= true;
4313 warn_for_div_by_zero (location
, cop1
);
4315 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4316 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4317 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4318 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4320 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4321 resultcode
= RDIV_EXPR
;
4323 /* When dividing two signed integers, we have to promote to int.
4324 unless we divide by a constant != -1. Note that default
4325 conversion will have been performed on the operands at this
4326 point, so we have to dig out the original type to find out if
4328 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4329 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4330 || (TREE_CODE (op1
) == INTEGER_CST
4331 && ! integer_all_onesp (op1
)));
4340 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4341 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4342 && !VECTOR_FLOAT_TYPE_P (type0
)
4343 && !VECTOR_FLOAT_TYPE_P (type1
)))
4347 case TRUNC_MOD_EXPR
:
4348 case FLOOR_MOD_EXPR
:
4350 tree cop1
= fold_non_dependent_expr (op1
);
4351 doing_div_or_mod
= true;
4352 warn_for_div_by_zero (location
, cop1
);
4355 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4356 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4357 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4359 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4361 /* Although it would be tempting to shorten always here, that loses
4362 on some targets, since the modulo instruction is undefined if the
4363 quotient can't be represented in the computation mode. We shorten
4364 only if unsigned or if dividing by something we know != -1. */
4365 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4366 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4367 || (TREE_CODE (op1
) == INTEGER_CST
4368 && ! integer_all_onesp (op1
)));
4373 case TRUTH_ANDIF_EXPR
:
4374 case TRUTH_ORIF_EXPR
:
4375 case TRUTH_AND_EXPR
:
4377 if (!VECTOR_TYPE_P (type0
) && VECTOR_TYPE_P (type1
))
4379 if (!COMPARISON_CLASS_P (op1
))
4380 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4381 build_zero_cst (type1
), complain
);
4382 if (code
== TRUTH_ANDIF_EXPR
)
4384 tree z
= build_zero_cst (TREE_TYPE (op1
));
4385 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4387 else if (code
== TRUTH_ORIF_EXPR
)
4389 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4390 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4395 if (VECTOR_TYPE_P (type0
))
4397 if (!COMPARISON_CLASS_P (op0
))
4398 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4399 build_zero_cst (type0
), complain
);
4400 if (!VECTOR_TYPE_P (type1
))
4402 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4403 tree z
= build_zero_cst (TREE_TYPE (op0
));
4404 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
4406 else if (!COMPARISON_CLASS_P (op1
))
4407 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4408 build_zero_cst (type1
), complain
);
4410 if (code
== TRUTH_ANDIF_EXPR
)
4411 code
= BIT_AND_EXPR
;
4412 else if (code
== TRUTH_ORIF_EXPR
)
4413 code
= BIT_IOR_EXPR
;
4417 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4420 result_type
= boolean_type_node
;
4423 /* Shift operations: result has same type as first operand;
4424 always convert second operand to int.
4425 Also set SHORT_SHIFT if shifting rightward. */
4428 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4429 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4431 result_type
= type0
;
4434 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4435 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4436 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4437 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4439 result_type
= type0
;
4442 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4444 tree const_op1
= fold_non_dependent_expr (op1
);
4445 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4447 result_type
= type0
;
4449 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4451 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4453 if ((complain
& tf_warning
)
4454 && c_inhibit_evaluation_warnings
== 0)
4455 warning (OPT_Wshift_count_negative
,
4456 "right shift count is negative");
4460 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4461 && (complain
& tf_warning
)
4462 && c_inhibit_evaluation_warnings
== 0)
4463 warning (OPT_Wshift_count_overflow
,
4464 "right shift count >= width of type");
4467 /* Avoid converting op1 to result_type later. */
4473 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4474 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4476 result_type
= type0
;
4479 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4480 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4481 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4482 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4484 result_type
= type0
;
4487 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4489 tree const_op0
= fold_non_dependent_expr (op0
);
4490 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4492 tree const_op1
= fold_non_dependent_expr (op1
);
4493 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4495 result_type
= type0
;
4497 if (TREE_CODE (const_op0
) == INTEGER_CST
4498 && tree_int_cst_sgn (const_op0
) < 0
4499 && (complain
& tf_warning
)
4500 && c_inhibit_evaluation_warnings
== 0)
4501 warning (OPT_Wshift_negative_value
,
4502 "left shift of negative value");
4503 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4505 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4507 if ((complain
& tf_warning
)
4508 && c_inhibit_evaluation_warnings
== 0)
4509 warning (OPT_Wshift_count_negative
,
4510 "left shift count is negative");
4512 else if (compare_tree_int (const_op1
,
4513 TYPE_PRECISION (type0
)) >= 0)
4515 if ((complain
& tf_warning
)
4516 && c_inhibit_evaluation_warnings
== 0)
4517 warning (OPT_Wshift_count_overflow
,
4518 "left shift count >= width of type");
4520 else if (TREE_CODE (const_op0
) == INTEGER_CST
4521 && (complain
& tf_warning
))
4522 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4524 /* Avoid converting op1 to result_type later. */
4531 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4533 result_type
= type0
;
4534 if (TREE_CODE (op1
) == INTEGER_CST
)
4536 if (tree_int_cst_lt (op1
, integer_zero_node
))
4538 if (complain
& tf_warning
)
4539 warning (0, (code
== LROTATE_EXPR
)
4540 ? G_("left rotate count is negative")
4541 : G_("right rotate count is negative"));
4543 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4545 if (complain
& tf_warning
)
4546 warning (0, (code
== LROTATE_EXPR
)
4547 ? G_("left rotate count >= width of type")
4548 : G_("right rotate count >= width of type"));
4551 /* Convert the shift-count to an integer, regardless of
4552 size of value being shifted. */
4553 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4554 op1
= cp_convert (integer_type_node
, op1
, complain
);
4560 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4561 goto vector_compare
;
4562 if ((complain
& tf_warning
)
4563 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4564 warning (OPT_Wfloat_equal
,
4565 "comparing floating point with == or != is unsafe");
4566 if ((complain
& tf_warning
)
4567 && ((TREE_CODE (orig_op0
) == STRING_CST
4568 && !integer_zerop (cp_fully_fold (op1
)))
4569 || (TREE_CODE (orig_op1
) == STRING_CST
4570 && !integer_zerop (cp_fully_fold (op0
)))))
4571 warning (OPT_Waddress
, "comparison with string literal results "
4572 "in unspecified behavior");
4574 build_type
= boolean_type_node
;
4575 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4576 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4577 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4578 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4580 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4581 && null_ptr_cst_p (orig_op1
))
4582 /* Handle, eg, (void*)0 (c++/43906), and more. */
4583 || (code0
== POINTER_TYPE
4584 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
4586 if (TYPE_PTR_P (type1
))
4587 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4588 CPO_COMPARISON
, complain
);
4590 result_type
= type0
;
4592 warn_for_null_address (location
, op0
, complain
);
4594 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4595 && null_ptr_cst_p (orig_op0
))
4596 /* Handle, eg, (void*)0 (c++/43906), and more. */
4597 || (code1
== POINTER_TYPE
4598 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
4600 if (TYPE_PTR_P (type0
))
4601 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4602 CPO_COMPARISON
, complain
);
4604 result_type
= type1
;
4606 warn_for_null_address (location
, op1
, complain
);
4608 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4609 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4610 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4611 CPO_COMPARISON
, complain
);
4612 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4613 /* One of the operands must be of nullptr_t type. */
4614 result_type
= TREE_TYPE (nullptr_node
);
4615 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4617 result_type
= type0
;
4618 if (complain
& tf_error
)
4619 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4621 return error_mark_node
;
4623 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4625 result_type
= type1
;
4626 if (complain
& tf_error
)
4627 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4629 return error_mark_node
;
4631 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
4633 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4634 == ptrmemfunc_vbit_in_delta
)
4636 tree pfn0
, delta0
, e1
, e2
;
4638 if (TREE_SIDE_EFFECTS (op0
))
4639 op0
= save_expr (op0
);
4641 pfn0
= pfn_from_ptrmemfunc (op0
);
4642 delta0
= delta_from_ptrmemfunc (op0
);
4643 e1
= cp_build_binary_op (location
,
4646 build_zero_cst (TREE_TYPE (pfn0
)),
4648 e2
= cp_build_binary_op (location
,
4654 if (complain
& tf_warning
)
4655 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4657 e2
= cp_build_binary_op (location
,
4658 EQ_EXPR
, e2
, integer_zero_node
,
4660 op0
= cp_build_binary_op (location
,
4661 TRUTH_ANDIF_EXPR
, e1
, e2
,
4663 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4667 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4668 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4670 result_type
= TREE_TYPE (op0
);
4672 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
4673 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4674 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4677 /* E will be the final comparison. */
4679 /* E1 and E2 are for scratch. */
4687 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4688 CPO_COMPARISON
, complain
);
4690 if (!same_type_p (TREE_TYPE (op0
), type
))
4691 op0
= cp_convert_and_check (type
, op0
, complain
);
4692 if (!same_type_p (TREE_TYPE (op1
), type
))
4693 op1
= cp_convert_and_check (type
, op1
, complain
);
4695 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4696 return error_mark_node
;
4698 if (TREE_SIDE_EFFECTS (op0
))
4699 op0
= save_expr (op0
);
4700 if (TREE_SIDE_EFFECTS (op1
))
4701 op1
= save_expr (op1
);
4703 pfn0
= pfn_from_ptrmemfunc (op0
);
4704 pfn0
= cp_fully_fold (pfn0
);
4705 /* Avoid -Waddress warnings (c++/64877). */
4706 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
4707 TREE_NO_WARNING (pfn0
) = 1;
4708 pfn1
= pfn_from_ptrmemfunc (op1
);
4709 pfn1
= cp_fully_fold (pfn1
);
4710 delta0
= delta_from_ptrmemfunc (op0
);
4711 delta1
= delta_from_ptrmemfunc (op1
);
4712 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4713 == ptrmemfunc_vbit_in_delta
)
4718 && ((op0.delta == op1.delta)
4719 || (!op0.pfn && op0.delta & 1 == 0
4720 && op1.delta & 1 == 0))
4722 The reason for the `!op0.pfn' bit is that a NULL
4723 pointer-to-member is any member with a zero PFN and
4724 LSB of the DELTA field is 0. */
4726 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4730 e1
= cp_build_binary_op (location
,
4731 EQ_EXPR
, e1
, integer_zero_node
,
4733 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4737 e2
= cp_build_binary_op (location
,
4738 EQ_EXPR
, e2
, integer_zero_node
,
4740 e1
= cp_build_binary_op (location
,
4741 TRUTH_ANDIF_EXPR
, e2
, e1
,
4743 e2
= cp_build_binary_op (location
, EQ_EXPR
,
4745 build_zero_cst (TREE_TYPE (pfn0
)),
4747 e2
= cp_build_binary_op (location
,
4748 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4749 e1
= cp_build_binary_op (location
,
4750 EQ_EXPR
, delta0
, delta1
, complain
);
4751 e1
= cp_build_binary_op (location
,
4752 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4759 && (!op0.pfn || op0.delta == op1.delta))
4761 The reason for the `!op0.pfn' bit is that a NULL
4762 pointer-to-member is any member with a zero PFN; the
4763 DELTA field is unspecified. */
4765 e1
= cp_build_binary_op (location
,
4766 EQ_EXPR
, delta0
, delta1
, complain
);
4767 e2
= cp_build_binary_op (location
,
4770 build_zero_cst (TREE_TYPE (pfn0
)),
4772 e1
= cp_build_binary_op (location
,
4773 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4775 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
4776 e
= cp_build_binary_op (location
,
4777 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4778 if (code
== EQ_EXPR
)
4780 return cp_build_binary_op (location
,
4781 EQ_EXPR
, e
, integer_zero_node
, complain
);
4785 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
4786 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
4788 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
4789 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
4797 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
4798 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
4800 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4801 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4802 CPO_COMPARISON
, complain
);
4809 if (TREE_CODE (orig_op0
) == STRING_CST
4810 || TREE_CODE (orig_op1
) == STRING_CST
)
4812 if (complain
& tf_warning
)
4813 warning (OPT_Waddress
, "comparison with string literal results "
4814 "in unspecified behavior");
4817 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4821 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4823 && !vector_types_compatible_elements_p (type0
, type1
))
4825 if (complain
& tf_error
)
4827 error_at (location
, "comparing vectors with different "
4829 inform (location
, "operand types are %qT and %qT",
4832 return error_mark_node
;
4835 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
4837 if (complain
& tf_error
)
4839 error_at (location
, "comparing vectors with different "
4840 "number of elements");
4841 inform (location
, "operand types are %qT and %qT",
4844 return error_mark_node
;
4847 /* It's not precisely specified how the usual arithmetic
4848 conversions apply to the vector types. Here, we use
4849 the unsigned type if one of the operands is signed and
4850 the other one is unsigned. */
4851 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
4853 if (!TYPE_UNSIGNED (type0
))
4854 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
4856 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
4857 warning_at (location
, OPT_Wsign_compare
, "comparison between "
4858 "types %qT and %qT", type0
, type1
);
4861 /* Always construct signed integer vector type. */
4862 intt
= c_common_type_for_size (GET_MODE_BITSIZE
4863 (TYPE_MODE (TREE_TYPE (type0
))), 0);
4866 if (complain
& tf_error
)
4867 error_at (location
, "could not find an integer type "
4868 "of the same size as %qT", TREE_TYPE (type0
));
4869 return error_mark_node
;
4871 result_type
= build_opaque_vector_type (intt
,
4872 TYPE_VECTOR_SUBPARTS (type0
));
4874 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
4876 build_type
= boolean_type_node
;
4877 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4878 || code0
== ENUMERAL_TYPE
)
4879 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4880 || code1
== ENUMERAL_TYPE
))
4882 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4883 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4884 CPO_COMPARISON
, complain
);
4885 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
4887 result_type
= type0
;
4888 if (extra_warnings
&& (complain
& tf_warning
))
4889 warning (OPT_Wextra
,
4890 "ordered comparison of pointer with integer zero");
4892 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
4894 result_type
= type1
;
4895 if (extra_warnings
&& (complain
& tf_warning
))
4896 warning (OPT_Wextra
,
4897 "ordered comparison of pointer with integer zero");
4899 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4900 /* One of the operands must be of nullptr_t type. */
4901 result_type
= TREE_TYPE (nullptr_node
);
4902 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4904 result_type
= type0
;
4905 if (complain
& tf_error
)
4906 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4908 return error_mark_node
;
4910 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4912 result_type
= type1
;
4913 if (complain
& tf_error
)
4914 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4916 return error_mark_node
;
4920 case UNORDERED_EXPR
:
4927 build_type
= integer_type_node
;
4928 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
4930 if (complain
& tf_error
)
4931 error ("unordered comparison on non-floating point argument");
4932 return error_mark_node
;
4941 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
4942 || code0
== ENUMERAL_TYPE
)
4943 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4944 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
4945 arithmetic_types_p
= 1;
4948 arithmetic_types_p
= 0;
4949 /* Vector arithmetic is only allowed when both sides are vectors. */
4950 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4952 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
4953 || !vector_types_compatible_elements_p (type0
, type1
))
4955 if (complain
& tf_error
)
4957 /* "location" already embeds the locations of the
4958 operands, so we don't need to add them separately
4960 rich_location
richloc (line_table
, location
);
4961 binary_op_error (&richloc
, code
, type0
, type1
);
4963 return error_mark_node
;
4965 arithmetic_types_p
= 1;
4968 /* Determine the RESULT_TYPE, if it is not already known. */
4970 && arithmetic_types_p
4971 && (shorten
|| common
|| short_compare
))
4973 result_type
= cp_common_type (type0
, type1
);
4974 if (complain
& tf_warning
)
4975 do_warn_double_promotion (result_type
, type0
, type1
,
4976 "implicit conversion from %qT to %qT "
4977 "to match other operand of binary "
4984 if (complain
& tf_error
)
4986 "invalid operands of types %qT and %qT to binary %qO",
4987 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
4988 return error_mark_node
;
4991 /* If we're in a template, the only thing we need to know is the
4993 if (processing_template_decl
)
4995 /* Since the middle-end checks the type when doing a build2, we
4996 need to build the tree in pieces. This built tree will never
4997 get out of the front-end as we replace it when instantiating
4999 tree tmp
= build2 (resultcode
,
5000 build_type
? build_type
: result_type
,
5002 TREE_OPERAND (tmp
, 0) = op0
;
5006 if (arithmetic_types_p
)
5008 bool first_complex
= (code0
== COMPLEX_TYPE
);
5009 bool second_complex
= (code1
== COMPLEX_TYPE
);
5010 int none_complex
= (!first_complex
&& !second_complex
);
5012 /* Adapted from patch for c/24581. */
5013 if (first_complex
!= second_complex
5014 && (code
== PLUS_EXPR
5015 || code
== MINUS_EXPR
5016 || code
== MULT_EXPR
5017 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
5018 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
5019 && flag_signed_zeros
)
5021 /* An operation on mixed real/complex operands must be
5022 handled specially, but the language-independent code can
5023 more easily optimize the plain complex arithmetic if
5024 -fno-signed-zeros. */
5025 tree real_type
= TREE_TYPE (result_type
);
5029 if (TREE_TYPE (op0
) != result_type
)
5030 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5031 if (TREE_TYPE (op1
) != real_type
)
5032 op1
= cp_convert_and_check (real_type
, op1
, complain
);
5036 if (TREE_TYPE (op0
) != real_type
)
5037 op0
= cp_convert_and_check (real_type
, op0
, complain
);
5038 if (TREE_TYPE (op1
) != result_type
)
5039 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5041 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
5042 return error_mark_node
;
5045 op0
= save_expr (op0
);
5046 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
5047 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
5051 case TRUNC_DIV_EXPR
:
5052 op1
= save_expr (op1
);
5053 imag
= build2 (resultcode
, real_type
, imag
, op1
);
5057 real
= build2 (resultcode
, real_type
, real
, op1
);
5065 op1
= save_expr (op1
);
5066 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
5067 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
5071 op0
= save_expr (op0
);
5072 imag
= build2 (resultcode
, real_type
, op0
, imag
);
5075 real
= build2 (resultcode
, real_type
, op0
, real
);
5078 real
= build2 (resultcode
, real_type
, op0
, real
);
5079 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
5085 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
5089 /* For certain operations (which identify themselves by shorten != 0)
5090 if both args were extended from the same smaller type,
5091 do the arithmetic in that type and then extend.
5093 shorten !=0 and !=1 indicates a bitwise operation.
5094 For them, this optimization is safe only if
5095 both args are zero-extended or both are sign-extended.
5096 Otherwise, we might change the result.
5097 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5098 but calculated in (unsigned short) it would be (unsigned short)-1. */
5100 if (shorten
&& none_complex
)
5102 orig_type
= result_type
;
5103 final_type
= result_type
;
5104 result_type
= shorten_binary_op (result_type
, op0
, op1
,
5108 /* Comparison operations are shortened too but differently.
5109 They identify themselves by setting short_compare = 1. */
5113 /* We call shorten_compare only for diagnostic-reason. */
5114 tree xop0
= fold_simple (op0
), xop1
= fold_simple (op1
),
5115 xresult_type
= result_type
;
5116 enum tree_code xresultcode
= resultcode
;
5117 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
5121 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
5122 && warn_sign_compare
5123 /* Do not warn until the template is instantiated; we cannot
5124 bound the ranges of the arguments until that point. */
5125 && !processing_template_decl
5126 && (complain
& tf_warning
)
5127 && c_inhibit_evaluation_warnings
== 0
5128 /* Even unsigned enum types promote to signed int. We don't
5129 want to issue -Wsign-compare warnings for this case. */
5130 && !enum_cast_to_int (orig_op0
)
5131 && !enum_cast_to_int (orig_op1
))
5133 tree oop0
= maybe_constant_value (orig_op0
);
5134 tree oop1
= maybe_constant_value (orig_op1
);
5136 if (TREE_CODE (oop0
) != INTEGER_CST
)
5137 oop0
= cp_fully_fold (orig_op0
);
5138 if (TREE_CODE (oop1
) != INTEGER_CST
)
5139 oop1
= cp_fully_fold (orig_op1
);
5140 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
5141 result_type
, resultcode
);
5145 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5146 Then the expression will be built.
5147 It will be given type FINAL_TYPE if that is nonzero;
5148 otherwise, it will be given type RESULT_TYPE. */
5151 if (TREE_TYPE (op0
) != result_type
)
5152 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5153 if (TREE_TYPE (op1
) != result_type
)
5154 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5156 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5157 return error_mark_node
;
5160 if (build_type
== NULL_TREE
)
5161 build_type
= result_type
;
5163 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
5164 | SANITIZE_FLOAT_DIVIDE
))
5165 && !processing_template_decl
5166 && do_ubsan_in_current_function ()
5167 && (doing_div_or_mod
|| doing_shift
))
5169 /* OP0 and/or OP1 might have side-effects. */
5170 op0
= cp_save_expr (op0
);
5171 op1
= cp_save_expr (op1
);
5172 op0
= fold_non_dependent_expr (op0
);
5173 op1
= fold_non_dependent_expr (op1
);
5174 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
5175 | SANITIZE_FLOAT_DIVIDE
)))
5177 /* For diagnostics we want to use the promoted types without
5178 shorten_binary_op. So convert the arguments to the
5179 original result_type. */
5182 if (orig_type
!= NULL
&& result_type
!= orig_type
)
5184 cop0
= cp_convert (orig_type
, op0
, complain
);
5185 cop1
= cp_convert (orig_type
, op1
, complain
);
5187 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
5189 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
5190 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5193 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
5194 if (final_type
!= 0)
5195 result
= cp_convert (final_type
, result
, complain
);
5197 if (instrument_expr
!= NULL
)
5198 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5199 instrument_expr
, result
);
5201 if (!processing_template_decl
)
5203 op0
= cp_fully_fold (op0
);
5204 /* Only consider the second argument if the first isn't overflowed. */
5205 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
5207 op1
= cp_fully_fold (op1
);
5208 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
5211 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
5212 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
5215 result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
5216 if (TREE_OVERFLOW_P (result_ovl
))
5217 overflow_warning (location
, result_ovl
);
5222 /* Build a VEC_PERM_EXPR.
5223 This is a simple wrapper for c_build_vec_perm_expr. */
5225 build_x_vec_perm_expr (location_t loc
,
5226 tree arg0
, tree arg1
, tree arg2
,
5227 tsubst_flags_t complain
)
5229 tree orig_arg0
= arg0
;
5230 tree orig_arg1
= arg1
;
5231 tree orig_arg2
= arg2
;
5232 if (processing_template_decl
)
5234 if (type_dependent_expression_p (arg0
)
5235 || type_dependent_expression_p (arg1
)
5236 || type_dependent_expression_p (arg2
))
5237 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5238 arg0
= build_non_dependent_expr (arg0
);
5240 arg1
= build_non_dependent_expr (arg1
);
5241 arg2
= build_non_dependent_expr (arg2
);
5243 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5244 if (processing_template_decl
&& exp
!= error_mark_node
)
5245 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5246 orig_arg1
, orig_arg2
);
5250 /* Return a tree for the sum or difference (RESULTCODE says which)
5251 of pointer PTROP and integer INTOP. */
5254 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
5255 tree intop
, tsubst_flags_t complain
)
5257 tree res_type
= TREE_TYPE (ptrop
);
5259 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5260 in certain circumstance (when it's valid to do so). So we need
5261 to make sure it's complete. We don't need to check here, if we
5262 can actually complete it at all, as those checks will be done in
5263 pointer_int_sum() anyway. */
5264 complete_type (TREE_TYPE (res_type
));
5266 return pointer_int_sum (loc
, resultcode
, ptrop
,
5267 intop
, complain
& tf_warning_or_error
);
5270 /* Return a tree for the difference of pointers OP0 and OP1.
5271 The resulting tree has type int. */
5274 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
5275 tsubst_flags_t complain
)
5278 tree restype
= ptrdiff_type_node
;
5279 tree target_type
= TREE_TYPE (ptrtype
);
5281 if (!complete_type_or_else (target_type
, NULL_TREE
))
5282 return error_mark_node
;
5284 if (VOID_TYPE_P (target_type
))
5286 if (complain
& tf_error
)
5287 permerror (loc
, "ISO C++ forbids using pointer of "
5288 "type %<void *%> in subtraction");
5290 return error_mark_node
;
5292 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5294 if (complain
& tf_error
)
5295 permerror (loc
, "ISO C++ forbids using pointer to "
5296 "a function in subtraction");
5298 return error_mark_node
;
5300 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5302 if (complain
& tf_error
)
5303 permerror (loc
, "ISO C++ forbids using pointer to "
5304 "a method in subtraction");
5306 return error_mark_node
;
5309 /* First do the subtraction as integers;
5310 then drop through to build the divide operator. */
5312 op0
= cp_build_binary_op (loc
,
5314 cp_convert (restype
, op0
, complain
),
5315 cp_convert (restype
, op1
, complain
),
5318 /* This generates an error if op1 is a pointer to an incomplete type. */
5319 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5321 if (complain
& tf_error
)
5322 error_at (loc
, "invalid use of a pointer to an incomplete type in "
5323 "pointer arithmetic");
5325 return error_mark_node
;
5328 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5330 if (complain
& tf_error
)
5331 error_at (loc
, "arithmetic on pointer to an empty aggregate");
5333 return error_mark_node
;
5336 op1
= (TYPE_PTROB_P (ptrtype
)
5337 ? size_in_bytes_loc (loc
, target_type
)
5338 : integer_one_node
);
5340 /* Do the division. */
5342 result
= build2_loc (loc
, EXACT_DIV_EXPR
, restype
, op0
,
5343 cp_convert (restype
, op1
, complain
));
5347 /* Construct and perhaps optimize a tree representation
5348 for a unary operation. CODE, a tree_code, specifies the operation
5349 and XARG is the operand. */
5352 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
5353 tsubst_flags_t complain
)
5355 tree orig_expr
= xarg
;
5358 tree overload
= NULL_TREE
;
5360 if (processing_template_decl
)
5362 if (type_dependent_expression_p (xarg
))
5363 return build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
5365 xarg
= build_non_dependent_expr (xarg
);
5370 /* [expr.unary.op] says:
5372 The address of an object of incomplete type can be taken.
5374 (And is just the ordinary address operator, not an overloaded
5375 "operator &".) However, if the type is a template
5376 specialization, we must complete the type at this point so that
5377 an overloaded "operator &" will be available if required. */
5378 if (code
== ADDR_EXPR
5379 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5380 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5381 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5382 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5383 /* Don't look for a function. */;
5385 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5386 NULL_TREE
, &overload
, complain
);
5388 if (!exp
&& code
== ADDR_EXPR
)
5390 if (is_overloaded_fn (xarg
))
5392 tree fn
= get_first_fn (xarg
);
5393 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5395 if (complain
& tf_error
)
5396 error (DECL_CONSTRUCTOR_P (fn
)
5397 ? G_("taking address of constructor %qE")
5398 : G_("taking address of destructor %qE"),
5400 return error_mark_node
;
5404 /* A pointer to member-function can be formed only by saying
5406 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5407 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5409 if (TREE_CODE (xarg
) != OFFSET_REF
5410 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5412 if (complain
& tf_error
)
5414 error ("invalid use of %qE to form a "
5415 "pointer-to-member-function", xarg
.get_value ());
5416 if (TREE_CODE (xarg
) != OFFSET_REF
)
5417 inform (input_location
, " a qualified-id is required");
5419 return error_mark_node
;
5423 if (complain
& tf_error
)
5424 error ("parentheses around %qE cannot be used to form a"
5425 " pointer-to-member-function",
5428 return error_mark_node
;
5429 PTRMEM_OK_P (xarg
) = 1;
5433 if (TREE_CODE (xarg
) == OFFSET_REF
)
5435 ptrmem
= PTRMEM_OK_P (xarg
);
5437 if (!ptrmem
&& !flag_ms_extensions
5438 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5440 /* A single non-static member, make sure we don't allow a
5441 pointer-to-member. */
5442 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5443 TREE_OPERAND (xarg
, 0),
5444 ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
));
5445 PTRMEM_OK_P (xarg
) = ptrmem
;
5449 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5452 if (processing_template_decl
&& exp
!= error_mark_node
)
5454 if (overload
!= NULL_TREE
)
5455 return (build_min_non_dep_op_overload
5456 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
5458 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5459 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5461 if (TREE_CODE (exp
) == ADDR_EXPR
)
5462 PTRMEM_OK_P (exp
) = ptrmem
;
5466 /* Construct and perhaps optimize a tree representation
5467 for __builtin_addressof operation. ARG specifies the operand. */
5470 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
5472 tree orig_expr
= arg
;
5474 if (processing_template_decl
)
5476 if (type_dependent_expression_p (arg
))
5477 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
5479 arg
= build_non_dependent_expr (arg
);
5482 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
5484 if (processing_template_decl
&& exp
!= error_mark_node
)
5485 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
5489 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5490 constants, where a null value is represented by an INTEGER_CST of
5494 cp_truthvalue_conversion (tree expr
)
5496 tree type
= TREE_TYPE (expr
);
5497 if (TYPE_PTR_OR_PTRMEM_P (type
)
5498 /* Avoid ICE on invalid use of non-static member function. */
5499 || TREE_CODE (expr
) == FUNCTION_DECL
)
5500 return build_binary_op (input_location
, NE_EXPR
, expr
, nullptr_node
, 1);
5502 return c_common_truthvalue_conversion (input_location
, expr
);
5505 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5508 condition_conversion (tree expr
)
5511 if (processing_template_decl
)
5513 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5514 tf_warning_or_error
, LOOKUP_NORMAL
);
5515 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5519 /* Returns the address of T. This function will fold away
5520 ADDR_EXPR of INDIRECT_REF. */
5523 build_address (tree t
)
5525 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5526 return error_mark_node
;
5527 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
);
5528 t
= build_fold_addr_expr (t
);
5529 if (TREE_CODE (t
) != ADDR_EXPR
)
5534 /* Return a NOP_EXPR converting EXPR to TYPE. */
5537 build_nop (tree type
, tree expr
)
5539 if (type
== error_mark_node
|| error_operand_p (expr
))
5541 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
5544 /* Take the address of ARG, whatever that means under C++ semantics.
5545 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5546 and class rvalues as well.
5548 Nothing should call this function directly; instead, callers should use
5549 cp_build_addr_expr or cp_build_addr_expr_strict. */
5552 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5557 if (!arg
|| error_operand_p (arg
))
5558 return error_mark_node
;
5560 arg
= mark_lvalue_use (arg
);
5561 argtype
= lvalue_type (arg
);
5563 gcc_assert (!identifier_p (arg
) || !IDENTIFIER_OPNAME_P (arg
));
5565 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5566 && !really_overloaded_fn (TREE_OPERAND (arg
, 1)))
5568 /* They're trying to take the address of a unique non-static
5569 member function. This is ill-formed (except in MS-land),
5570 but let's try to DTRT.
5571 Note: We only handle unique functions here because we don't
5572 want to complain if there's a static overload; non-unique
5573 cases will be handled by instantiate_type. But we need to
5574 handle this case here to allow casts on the resulting PMF.
5575 We could defer this in non-MS mode, but it's easier to give
5576 a useful error here. */
5578 /* Inside constant member functions, the `this' pointer
5579 contains an extra const qualifier. TYPE_MAIN_VARIANT
5580 is used here to remove this const from the diagnostics
5581 and the created OFFSET_REF. */
5582 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5583 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5584 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5585 return error_mark_node
;
5587 if (! flag_ms_extensions
)
5589 tree name
= DECL_NAME (fn
);
5590 if (!(complain
& tf_error
))
5591 return error_mark_node
;
5592 else if (current_class_type
5593 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5594 /* An expression like &memfn. */
5595 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5596 " or parenthesized non-static member function to form"
5597 " a pointer to member function. Say %<&%T::%D%>",
5600 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5601 " function to form a pointer to member function."
5605 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5608 /* Uninstantiated types are all functions. Taking the
5609 address of a function is a no-op, so just return the
5611 if (type_unknown_p (arg
))
5612 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5614 if (TREE_CODE (arg
) == OFFSET_REF
)
5615 /* We want a pointer to member; bypass all the code for actually taking
5616 the address of something. */
5619 /* Anything not already handled and not a true memory reference
5621 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5622 && TREE_CODE (argtype
) != METHOD_TYPE
)
5624 cp_lvalue_kind kind
= lvalue_kind (arg
);
5625 if (kind
== clk_none
)
5627 if (complain
& tf_error
)
5628 lvalue_error (input_location
, lv_addressof
);
5629 return error_mark_node
;
5631 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
5633 if (!(complain
& tf_error
))
5634 return error_mark_node
;
5635 if (kind
& clk_class
)
5636 /* Make this a permerror because we used to accept it. */
5637 permerror (input_location
, "taking address of temporary");
5639 error ("taking address of xvalue (rvalue reference)");
5643 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
5645 tree type
= build_pointer_type (TREE_TYPE (argtype
));
5646 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5649 else if (pedantic
&& DECL_MAIN_P (arg
))
5652 /* Apparently a lot of autoconf scripts for C++ packages do this,
5653 so only complain if -Wpedantic. */
5654 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
5655 pedwarn (input_location
, OPT_Wpedantic
,
5656 "ISO C++ forbids taking address of function %<::main%>");
5657 else if (flag_pedantic_errors
)
5658 return error_mark_node
;
5661 /* Let &* cancel out to simplify resulting code. */
5662 if (INDIRECT_REF_P (arg
))
5664 arg
= TREE_OPERAND (arg
, 0);
5665 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
5667 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
5668 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5671 /* Don't let this be an lvalue. */
5676 /* ??? Cope with user tricks that amount to offsetof. */
5677 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5678 && TREE_CODE (argtype
) != METHOD_TYPE
5679 && argtype
!= unknown_type_node
5680 && (val
= get_base_address (arg
))
5681 && COMPLETE_TYPE_P (TREE_TYPE (val
))
5682 && INDIRECT_REF_P (val
)
5683 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
5685 tree type
= build_pointer_type (argtype
);
5686 return fold_convert (type
, fold_offsetof_1 (arg
));
5689 /* Handle complex lvalues (when permitted)
5690 by reduction to simpler cases. */
5691 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
5695 switch (TREE_CODE (arg
))
5699 case FIX_TRUNC_EXPR
:
5700 /* We should have handled this above in the lvalue_kind check. */
5705 arg
= BASELINK_FUNCTIONS (arg
);
5709 arg
= OVL_CURRENT (arg
);
5714 /* Turn a reference to a non-static data member into a
5715 pointer-to-member. */
5720 gcc_assert (PTRMEM_OK_P (arg
));
5722 t
= TREE_OPERAND (arg
, 1);
5723 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5725 if (complain
& tf_error
)
5726 error ("cannot create pointer to reference member %qD", t
);
5727 return error_mark_node
;
5730 type
= build_ptrmem_type (context_for_name_lookup (t
),
5732 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
5740 if (argtype
!= error_mark_node
)
5741 argtype
= build_pointer_type (argtype
);
5743 /* In a template, we are processing a non-dependent expression
5744 so we can just form an ADDR_EXPR with the correct type. */
5745 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
5747 val
= build_address (arg
);
5748 if (TREE_CODE (arg
) == OFFSET_REF
)
5749 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
5751 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
5753 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
5755 /* We can only get here with a single static member
5757 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
5758 && DECL_STATIC_FUNCTION_P (fn
));
5759 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5760 return error_mark_node
;
5761 val
= build_address (fn
);
5762 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
5763 /* Do not lose object's side effects. */
5764 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
5765 TREE_OPERAND (arg
, 0), val
);
5767 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
5769 if (complain
& tf_error
)
5770 error ("attempt to take address of bit-field structure member %qD",
5771 TREE_OPERAND (arg
, 1));
5772 return error_mark_node
;
5776 tree object
= TREE_OPERAND (arg
, 0);
5777 tree field
= TREE_OPERAND (arg
, 1);
5778 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5779 (TREE_TYPE (object
), decl_type_context (field
)));
5780 val
= build_address (arg
);
5783 if (TYPE_PTR_P (argtype
)
5784 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
5786 build_ptrmemfunc_type (argtype
);
5787 val
= build_ptrmemfunc (argtype
, val
, 0,
5795 /* Take the address of ARG if it has one, even if it's an rvalue. */
5798 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
5800 return cp_build_addr_expr_1 (arg
, 0, complain
);
5803 /* Take the address of ARG, but only if it's an lvalue. */
5806 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
5808 return cp_build_addr_expr_1 (arg
, 1, complain
);
5811 /* C++: Must handle pointers to members.
5813 Perhaps type instantiation should be extended to handle conversion
5814 from aggregates to types we don't yet know we want? (Or are those
5815 cases typically errors which should be reported?)
5817 NOCONVERT suppresses the default promotions (such as from short to int). */
5820 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
5821 tsubst_flags_t complain
)
5823 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5825 location_t location
= EXPR_LOC_OR_LOC (arg
, input_location
);
5827 const char *errstring
= NULL
;
5829 const char *invalid_op_diag
;
5831 if (!arg
|| error_operand_p (arg
))
5832 return error_mark_node
;
5834 if ((invalid_op_diag
5835 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
5840 if (complain
& tf_error
)
5841 error (invalid_op_diag
);
5842 return error_mark_node
;
5847 case UNARY_PLUS_EXPR
:
5850 int flags
= WANT_ARITH
| WANT_ENUM
;
5851 /* Unary plus (but not unary minus) is allowed on pointers. */
5852 if (code
== UNARY_PLUS_EXPR
)
5853 flags
|= WANT_POINTER
;
5854 arg
= build_expr_type_conversion (flags
, arg
, true);
5856 errstring
= (code
== NEGATE_EXPR
5857 ? _("wrong type argument to unary minus")
5858 : _("wrong type argument to unary plus"));
5861 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5862 arg
= cp_perform_integral_promotions (arg
, complain
);
5864 /* Make sure the result is not an lvalue: a unary plus or minus
5865 expression is always a rvalue. */
5872 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5877 arg
= cp_default_conversion (arg
, complain
);
5878 if (arg
== error_mark_node
)
5879 return error_mark_node
;
5882 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
5883 | WANT_VECTOR_OR_COMPLEX
,
5885 errstring
= _("wrong type argument to bit-complement");
5886 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5888 /* Warn if the expression has boolean value. */
5889 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
5890 && warning_at (location
, OPT_Wbool_operation
,
5891 "%<~%> on an expression of type bool"))
5892 inform (location
, "did you mean to use logical not (%<!%>)?");
5893 arg
= cp_perform_integral_promotions (arg
, complain
);
5898 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5899 errstring
= _("wrong type argument to abs");
5900 else if (!noconvert
)
5902 arg
= cp_default_conversion (arg
, complain
);
5903 if (arg
== error_mark_node
)
5904 return error_mark_node
;
5909 /* Conjugating a real value is a no-op, but allow it anyway. */
5910 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5911 errstring
= _("wrong type argument to conjugation");
5912 else if (!noconvert
)
5914 arg
= cp_default_conversion (arg
, complain
);
5915 if (arg
== error_mark_node
)
5916 return error_mark_node
;
5920 case TRUTH_NOT_EXPR
:
5921 if (VECTOR_TYPE_P (TREE_TYPE (arg
)))
5922 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
5923 build_zero_cst (TREE_TYPE (arg
)), complain
);
5924 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
5926 val
= invert_truthvalue_loc (input_location
, arg
);
5927 if (arg
!= error_mark_node
)
5929 errstring
= _("in argument to unary !");
5937 arg
= build_real_imag_expr (input_location
, code
, arg
);
5940 case PREINCREMENT_EXPR
:
5941 case POSTINCREMENT_EXPR
:
5942 case PREDECREMENT_EXPR
:
5943 case POSTDECREMENT_EXPR
:
5944 /* Handle complex lvalues (when permitted)
5945 by reduction to simpler cases. */
5947 val
= unary_complex_lvalue (code
, arg
);
5951 arg
= mark_lvalue_use (arg
);
5953 /* Increment or decrement the real part of the value,
5954 and don't change the imaginary part. */
5955 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5959 arg
= cp_stabilize_reference (arg
);
5960 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
5961 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
5962 real
= cp_build_unary_op (code
, real
, true, complain
);
5963 if (real
== error_mark_node
|| imag
== error_mark_node
)
5964 return error_mark_node
;
5965 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
5969 /* Report invalid types. */
5971 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
5974 if (code
== PREINCREMENT_EXPR
)
5975 errstring
= _("no pre-increment operator for type");
5976 else if (code
== POSTINCREMENT_EXPR
)
5977 errstring
= _("no post-increment operator for type");
5978 else if (code
== PREDECREMENT_EXPR
)
5979 errstring
= _("no pre-decrement operator for type");
5981 errstring
= _("no post-decrement operator for type");
5984 else if (arg
== error_mark_node
)
5985 return error_mark_node
;
5987 /* Report something read-only. */
5989 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
5990 || TREE_READONLY (arg
))
5992 if (complain
& tf_error
)
5993 cxx_readonly_error (arg
, ((code
== PREINCREMENT_EXPR
5994 || code
== POSTINCREMENT_EXPR
)
5995 ? lv_increment
: lv_decrement
));
5997 return error_mark_node
;
6002 tree declared_type
= unlowered_expr_type (arg
);
6004 argtype
= TREE_TYPE (arg
);
6006 /* ARM $5.2.5 last annotation says this should be forbidden. */
6007 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
6009 if (complain
& tf_error
)
6010 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
6011 ? G_("ISO C++ forbids incrementing an enum")
6012 : G_("ISO C++ forbids decrementing an enum"));
6014 return error_mark_node
;
6017 /* Compute the increment. */
6019 if (TYPE_PTR_P (argtype
))
6021 tree type
= complete_type (TREE_TYPE (argtype
));
6023 if (!COMPLETE_OR_VOID_TYPE_P (type
))
6025 if (complain
& tf_error
)
6026 error (((code
== PREINCREMENT_EXPR
6027 || code
== POSTINCREMENT_EXPR
))
6028 ? G_("cannot increment a pointer to incomplete type %qT")
6029 : G_("cannot decrement a pointer to incomplete type %qT"),
6030 TREE_TYPE (argtype
));
6032 return error_mark_node
;
6034 else if (!TYPE_PTROB_P (argtype
))
6036 if (complain
& tf_error
)
6037 pedwarn (input_location
, OPT_Wpointer_arith
,
6038 (code
== PREINCREMENT_EXPR
6039 || code
== POSTINCREMENT_EXPR
)
6040 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6041 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6044 return error_mark_node
;
6047 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
6050 inc
= VECTOR_TYPE_P (argtype
)
6051 ? build_one_cst (argtype
)
6054 inc
= cp_convert (argtype
, inc
, complain
);
6056 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6057 need to ask Objective-C to build the increment or decrement
6058 expression for it. */
6059 if (objc_is_property_ref (arg
))
6060 return objc_build_incr_expr_for_property_ref (input_location
, code
,
6063 /* Complain about anything else that is not a true lvalue. */
6064 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
6065 || code
== POSTINCREMENT_EXPR
)
6066 ? lv_increment
: lv_decrement
),
6068 return error_mark_node
;
6070 /* Forbid using -- or ++ in C++17 on `bool'. */
6071 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
6073 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6075 if (complain
& tf_error
)
6076 error ("use of an operand of type %qT in %<operator--%> "
6077 "is forbidden", boolean_type_node
);
6078 return error_mark_node
;
6082 if (cxx_dialect
>= cxx1z
)
6084 if (complain
& tf_error
)
6085 error ("use of an operand of type %qT in "
6086 "%<operator++%> is forbidden in C++1z",
6088 return error_mark_node
;
6090 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6091 else if (!in_system_header_at (input_location
))
6092 warning (OPT_Wdeprecated
, "use of an operand of type %qT "
6093 "in %<operator++%> is deprecated",
6096 val
= boolean_increment (code
, arg
);
6098 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6099 /* An rvalue has no cv-qualifiers. */
6100 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
6102 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
6104 TREE_SIDE_EFFECTS (val
) = 1;
6109 /* Note that this operation never does default_conversion
6110 regardless of NOCONVERT. */
6111 return cp_build_addr_expr (arg
, complain
);
6120 argtype
= TREE_TYPE (arg
);
6121 return build1 (code
, argtype
, arg
);
6124 if (complain
& tf_error
)
6125 error ("%s", errstring
);
6126 return error_mark_node
;
6129 /* Hook for the c-common bits that build a unary op. */
6131 build_unary_op (location_t
/*location*/,
6132 enum tree_code code
, tree xarg
, bool noconvert
)
6134 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
6137 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6138 for certain kinds of expressions which are not really lvalues
6139 but which we can accept as lvalues.
6141 If ARG is not a kind of expression we can handle, return
6145 unary_complex_lvalue (enum tree_code code
, tree arg
)
6147 /* Inside a template, making these kinds of adjustments is
6148 pointless; we are only concerned with the type of the
6150 if (processing_template_decl
)
6153 /* Handle (a, b) used as an "lvalue". */
6154 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
6156 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
6157 tf_warning_or_error
);
6158 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6159 TREE_OPERAND (arg
, 0), real_result
);
6162 /* Handle (a ? b : c) used as an "lvalue". */
6163 if (TREE_CODE (arg
) == COND_EXPR
6164 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6165 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6167 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6168 if (TREE_CODE (arg
) == MODIFY_EXPR
6169 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6170 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6172 tree lvalue
= TREE_OPERAND (arg
, 0);
6173 if (TREE_SIDE_EFFECTS (lvalue
))
6175 lvalue
= cp_stabilize_reference (lvalue
);
6176 arg
= build2 (TREE_CODE (arg
), TREE_TYPE (arg
),
6177 lvalue
, TREE_OPERAND (arg
, 1));
6179 return unary_complex_lvalue
6180 (code
, build2 (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
6183 if (code
!= ADDR_EXPR
)
6186 /* Handle (a = b) used as an "lvalue" for `&'. */
6187 if (TREE_CODE (arg
) == MODIFY_EXPR
6188 || TREE_CODE (arg
) == INIT_EXPR
)
6190 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
6191 tf_warning_or_error
);
6192 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6194 TREE_NO_WARNING (arg
) = 1;
6198 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
6199 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
6200 || TREE_CODE (arg
) == OFFSET_REF
)
6203 /* We permit compiler to make function calls returning
6204 objects of aggregate type look like lvalues. */
6208 if (TREE_CODE (targ
) == SAVE_EXPR
)
6209 targ
= TREE_OPERAND (targ
, 0);
6211 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6213 if (TREE_CODE (arg
) == SAVE_EXPR
)
6216 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6217 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6220 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6221 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6222 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6225 /* Don't let anything else be handled specially. */
6229 /* Mark EXP saying that we need to be able to take the
6230 address of it; it should not be allocated in a register.
6231 Value is true if successful.
6233 C++: we do not allow `current_class_ptr' to be addressable. */
6236 cxx_mark_addressable (tree exp
)
6241 switch (TREE_CODE (x
))
6248 x
= TREE_OPERAND (x
, 0);
6252 if (x
== current_class_ptr
)
6254 error ("cannot take the address of %<this%>, which is an rvalue expression");
6255 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6261 /* Caller should not be trying to mark initialized
6262 constant fields addressable. */
6263 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6264 || DECL_IN_AGGR_P (x
) == 0
6266 || DECL_EXTERNAL (x
));
6270 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6271 && !DECL_ARTIFICIAL (x
))
6273 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6276 ("address of explicit register variable %qD requested", x
);
6279 else if (extra_warnings
)
6281 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6283 TREE_ADDRESSABLE (x
) = 1;
6288 TREE_ADDRESSABLE (x
) = 1;
6292 TREE_ADDRESSABLE (x
) = 1;
6296 TREE_ADDRESSABLE (x
) = 1;
6297 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6305 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6308 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6309 tsubst_flags_t complain
)
6311 tree orig_ifexp
= ifexp
;
6312 tree orig_op1
= op1
;
6313 tree orig_op2
= op2
;
6316 if (processing_template_decl
)
6318 /* The standard says that the expression is type-dependent if
6319 IFEXP is type-dependent, even though the eventual type of the
6320 expression doesn't dependent on IFEXP. */
6321 if (type_dependent_expression_p (ifexp
)
6322 /* As a GNU extension, the middle operand may be omitted. */
6323 || (op1
&& type_dependent_expression_p (op1
))
6324 || type_dependent_expression_p (op2
))
6325 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6326 ifexp
= build_non_dependent_expr (ifexp
);
6328 op1
= build_non_dependent_expr (op1
);
6329 op2
= build_non_dependent_expr (op2
);
6332 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6333 if (processing_template_decl
&& expr
!= error_mark_node
)
6335 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6336 orig_ifexp
, orig_op1
, orig_op2
);
6337 /* Remember that the result is an lvalue or xvalue. */
6338 if (glvalue_p (expr
) && !glvalue_p (min
))
6339 TREE_TYPE (min
) = cp_build_reference_type (TREE_TYPE (min
),
6341 expr
= convert_from_reference (min
);
6346 /* Given a list of expressions, return a compound expression
6347 that performs them all and returns the value of the last of them. */
6350 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6351 tsubst_flags_t complain
)
6353 tree expr
= TREE_VALUE (list
);
6355 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6356 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6358 if (complain
& tf_error
)
6359 pedwarn (EXPR_LOC_OR_LOC (expr
, input_location
), 0,
6360 "list-initializer for non-class type must not "
6361 "be parenthesized");
6363 return error_mark_node
;
6366 if (TREE_CHAIN (list
))
6368 if (complain
& tf_error
)
6372 permerror (input_location
, "expression list treated as compound "
6373 "expression in initializer");
6376 permerror (input_location
, "expression list treated as compound "
6377 "expression in mem-initializer");
6380 permerror (input_location
, "expression list treated as compound "
6381 "expression in functional cast");
6387 return error_mark_node
;
6389 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6390 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6391 expr
, TREE_VALUE (list
), complain
);
6397 /* Like build_x_compound_expr_from_list, but using a VEC. */
6400 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6401 tsubst_flags_t complain
)
6403 if (vec_safe_is_empty (vec
))
6405 else if (vec
->length () == 1)
6415 if (complain
& tf_error
)
6416 permerror (input_location
,
6417 "%s expression list treated as compound expression",
6420 return error_mark_node
;
6424 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6425 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6432 /* Handle overloading of the ',' operator when needed. */
6435 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6436 tsubst_flags_t complain
)
6439 tree orig_op1
= op1
;
6440 tree orig_op2
= op2
;
6441 tree overload
= NULL_TREE
;
6443 if (processing_template_decl
)
6445 if (type_dependent_expression_p (op1
)
6446 || type_dependent_expression_p (op2
))
6447 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6448 op1
= build_non_dependent_expr (op1
);
6449 op2
= build_non_dependent_expr (op2
);
6452 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6453 NULL_TREE
, &overload
, complain
);
6455 result
= cp_build_compound_expr (op1
, op2
, complain
);
6457 if (processing_template_decl
&& result
!= error_mark_node
)
6459 if (overload
!= NULL_TREE
)
6460 return (build_min_non_dep_op_overload
6461 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
6463 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6469 /* Like cp_build_compound_expr, but for the c-common bits. */
6472 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6474 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6477 /* Build a compound expression. */
6480 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6482 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6484 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6485 return error_mark_node
;
6488 && (TREE_CODE (lhs
) == CILK_SPAWN_STMT
6489 || TREE_CODE (rhs
) == CILK_SPAWN_STMT
))
6491 location_t loc
= (EXPR_HAS_LOCATION (lhs
) ? EXPR_LOCATION (lhs
)
6492 : EXPR_LOCATION (rhs
));
6494 "spawned function call cannot be part of a comma expression");
6495 return error_mark_node
;
6498 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6500 /* If the rhs is a TARGET_EXPR, then build the compound
6501 expression inside the target_expr's initializer. This
6502 helps the compiler to eliminate unnecessary temporaries. */
6503 tree init
= TREE_OPERAND (rhs
, 1);
6505 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6506 TREE_OPERAND (rhs
, 1) = init
;
6511 if (type_unknown_p (rhs
))
6513 if (complain
& tf_error
)
6514 error ("no context to resolve type of %qE", rhs
);
6515 return error_mark_node
;
6518 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6521 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6522 casts away constness. CAST gives the type of cast. Returns true
6523 if the cast is ill-formed, false if it is well-formed.
6525 ??? This function warns for casting away any qualifier not just
6526 const. We would like to specify exactly what qualifiers are casted
6531 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6532 enum tree_code cast
, tsubst_flags_t complain
)
6534 /* C-style casts are allowed to cast away constness. With
6535 WARN_CAST_QUAL, we still want to issue a warning. */
6536 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6539 if (!casts_away_constness (src_type
, dest_type
, complain
))
6545 if (complain
& tf_warning
)
6546 warning (OPT_Wcast_qual
,
6547 "cast from type %qT to type %qT casts away qualifiers",
6548 src_type
, dest_type
);
6551 case STATIC_CAST_EXPR
:
6552 if (complain
& tf_error
)
6553 error ("static_cast from type %qT to type %qT casts away qualifiers",
6554 src_type
, dest_type
);
6557 case REINTERPRET_CAST_EXPR
:
6558 if (complain
& tf_error
)
6559 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6560 src_type
, dest_type
);
6569 Warns if the cast from expression EXPR to type TYPE is useless.
6572 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6574 if (warn_useless_cast
6575 && complain
& tf_warning
)
6577 if ((TREE_CODE (type
) == REFERENCE_TYPE
6578 && (TYPE_REF_IS_RVALUE (type
)
6579 ? xvalue_p (expr
) : lvalue_p (expr
))
6580 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6581 || same_type_p (TREE_TYPE (expr
), type
))
6582 warning (OPT_Wuseless_cast
, "useless cast to type %qT", type
);
6586 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6587 (another pointer-to-member type in the same hierarchy) and return
6588 the converted expression. If ALLOW_INVERSE_P is permitted, a
6589 pointer-to-derived may be converted to pointer-to-base; otherwise,
6590 only the other direction is permitted. If C_CAST_P is true, this
6591 conversion is taking place as part of a C-style cast. */
6594 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6595 bool c_cast_p
, tsubst_flags_t complain
)
6597 if (TYPE_PTRDATAMEM_P (type
))
6601 if (TREE_CODE (expr
) == PTRMEM_CST
)
6602 expr
= cplus_expand_constant (expr
);
6603 delta
= get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
)),
6604 TYPE_PTRMEM_CLASS_TYPE (type
),
6606 c_cast_p
, complain
);
6607 if (delta
== error_mark_node
)
6608 return error_mark_node
;
6610 if (!integer_zerop (delta
))
6612 tree cond
, op1
, op2
;
6614 cond
= cp_build_binary_op (input_location
,
6617 build_int_cst (TREE_TYPE (expr
), -1),
6619 op1
= build_nop (ptrdiff_type_node
, expr
);
6620 op2
= cp_build_binary_op (input_location
,
6621 PLUS_EXPR
, op1
, delta
,
6624 expr
= fold_build3_loc (input_location
,
6625 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
6629 return build_nop (type
, expr
);
6632 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
6633 allow_inverse_p
, c_cast_p
, complain
);
6636 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6637 this static_cast is being attempted as one of the possible casts
6638 allowed by a C-style cast. (In that case, accessibility of base
6639 classes is not considered, and it is OK to cast away
6640 constness.) Return the result of the cast. *VALID_P is set to
6641 indicate whether or not the cast was valid. */
6644 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6645 bool *valid_p
, tsubst_flags_t complain
)
6651 /* Assume the cast is valid. */
6654 intype
= unlowered_expr_type (expr
);
6656 /* Save casted types in the function's used types hash table. */
6657 used_types_insert (type
);
6659 /* [expr.static.cast]
6661 An lvalue of type "cv1 B", where B is a class type, can be cast
6662 to type "reference to cv2 D", where D is a class derived (clause
6663 _class.derived_) from B, if a valid standard conversion from
6664 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6665 same cv-qualification as, or greater cv-qualification than, cv1,
6666 and B is not a virtual base class of D. */
6667 /* We check this case before checking the validity of "TYPE t =
6668 EXPR;" below because for this case:
6671 struct D : public B { D(const B&); };
6673 void f() { static_cast<const D&>(b); }
6675 we want to avoid constructing a new D. The standard is not
6676 completely clear about this issue, but our interpretation is
6677 consistent with other compilers. */
6678 if (TREE_CODE (type
) == REFERENCE_TYPE
6679 && CLASS_TYPE_P (TREE_TYPE (type
))
6680 && CLASS_TYPE_P (intype
)
6681 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
6682 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
6683 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
6684 build_pointer_type (TYPE_MAIN_VARIANT
6685 (TREE_TYPE (type
))),
6688 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6692 /* There is a standard conversion from "D*" to "B*" even if "B"
6693 is ambiguous or inaccessible. If this is really a
6694 static_cast, then we check both for inaccessibility and
6695 ambiguity. However, if this is a static_cast being performed
6696 because the user wrote a C-style cast, then accessibility is
6698 base
= lookup_base (TREE_TYPE (type
), intype
,
6699 c_cast_p
? ba_unique
: ba_check
,
6701 expr
= build_address (expr
);
6703 if (flag_sanitize
& SANITIZE_VPTR
)
6706 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
6712 /* Convert from "B*" to "D*". This function will check that "B"
6713 is not a virtual base of "D". */
6714 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6717 /* Convert the pointer to a reference -- but then remember that
6718 there are no expressions with reference type in C++.
6720 We call rvalue so that there's an actual tree code
6721 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6722 is a variable with the same type, the conversion would get folded
6723 away, leaving just the variable and causing lvalue_kind to give
6724 the wrong answer. */
6725 return convert_from_reference (rvalue (cp_fold_convert (type
, expr
)));
6728 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6729 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6730 if (TREE_CODE (type
) == REFERENCE_TYPE
6731 && TYPE_REF_IS_RVALUE (type
)
6732 && (clk
= real_lvalue_p (expr
))
6733 && reference_related_p (TREE_TYPE (type
), intype
)
6734 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6736 if (clk
== clk_ordinary
)
6738 /* Handle the (non-bit-field) lvalue case here by casting to
6739 lvalue reference and then changing it to an rvalue reference.
6740 Casting an xvalue to rvalue reference will be handled by the
6742 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
6743 result
= (perform_direct_initialization_if_possible
6744 (lref
, expr
, c_cast_p
, complain
));
6745 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
6746 return convert_from_reference (result
);
6749 /* For a bit-field or packed field, bind to a temporary. */
6750 expr
= rvalue (expr
);
6753 /* Resolve overloaded address here rather than once in
6754 implicit_conversion and again in the inverse code below. */
6755 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
6757 expr
= instantiate_type (type
, expr
, complain
);
6758 intype
= TREE_TYPE (expr
);
6761 /* [expr.static.cast]
6763 Any expression can be explicitly converted to type cv void. */
6764 if (VOID_TYPE_P (type
))
6765 return convert_to_void (expr
, ICV_CAST
, complain
);
6768 An abstract class shall not be used ... as the type of an explicit
6770 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
6771 return error_mark_node
;
6773 /* [expr.static.cast]
6775 An expression e can be explicitly converted to a type T using a
6776 static_cast of the form static_cast<T>(e) if the declaration T
6777 t(e);" is well-formed, for some invented temporary variable
6779 result
= perform_direct_initialization_if_possible (type
, expr
,
6780 c_cast_p
, complain
);
6783 result
= convert_from_reference (result
);
6785 /* [expr.static.cast]
6787 If T is a reference type, the result is an lvalue; otherwise,
6788 the result is an rvalue. */
6789 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6791 result
= rvalue (result
);
6793 if (result
== expr
&& SCALAR_TYPE_P (type
))
6794 /* Leave some record of the cast. */
6795 result
= build_nop (type
, expr
);
6800 /* [expr.static.cast]
6802 The inverse of any standard conversion sequence (clause _conv_),
6803 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6804 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6805 (_conv.bool_) conversions, can be performed explicitly using
6806 static_cast subject to the restriction that the explicit
6807 conversion does not cast away constness (_expr.const.cast_), and
6808 the following additional rules for specific cases: */
6809 /* For reference, the conversions not excluded are: integral
6810 promotions, floating point promotion, integral conversions,
6811 floating point conversions, floating-integral conversions,
6812 pointer conversions, and pointer to member conversions. */
6815 A value of integral _or enumeration_ type can be explicitly
6816 converted to an enumeration type. */
6817 /* The effect of all that is that any conversion between any two
6818 types which are integral, floating, or enumeration types can be
6820 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6821 || SCALAR_FLOAT_TYPE_P (type
))
6822 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
6823 || SCALAR_FLOAT_TYPE_P (intype
)))
6824 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
6826 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
6827 && CLASS_TYPE_P (TREE_TYPE (type
))
6828 && CLASS_TYPE_P (TREE_TYPE (intype
))
6829 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6830 (TREE_TYPE (intype
))),
6831 build_pointer_type (TYPE_MAIN_VARIANT
6832 (TREE_TYPE (type
))),
6838 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6840 return error_mark_node
;
6841 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
6842 c_cast_p
? ba_unique
: ba_check
,
6844 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6847 if (flag_sanitize
& SANITIZE_VPTR
)
6850 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
6856 return cp_fold_convert (type
, expr
);
6859 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6860 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6867 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
6868 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
6870 if (TYPE_PTRDATAMEM_P (type
))
6872 t1
= (build_ptrmem_type
6874 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
6875 t2
= (build_ptrmem_type
6877 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
6884 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
6887 && check_for_casting_away_constness (intype
, type
,
6890 return error_mark_node
;
6891 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
6892 c_cast_p
, complain
);
6896 /* [expr.static.cast]
6898 An rvalue of type "pointer to cv void" can be explicitly
6899 converted to a pointer to object type. A value of type pointer
6900 to object converted to "pointer to cv void" and back to the
6901 original pointer type will have its original value. */
6902 if (TYPE_PTR_P (intype
)
6903 && VOID_TYPE_P (TREE_TYPE (intype
))
6904 && TYPE_PTROB_P (type
))
6907 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6909 return error_mark_node
;
6910 return build_nop (type
, expr
);
6914 return error_mark_node
;
6917 /* Return an expression representing static_cast<TYPE>(EXPR). */
6920 build_static_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6925 if (type
== error_mark_node
|| expr
== error_mark_node
)
6926 return error_mark_node
;
6928 if (processing_template_decl
)
6930 expr
= build_min (STATIC_CAST_EXPR
, type
, expr
);
6931 /* We don't know if it will or will not have side effects. */
6932 TREE_SIDE_EFFECTS (expr
) = 1;
6933 return convert_from_reference (expr
);
6936 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6937 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6938 if (TREE_CODE (type
) != REFERENCE_TYPE
6939 && TREE_CODE (expr
) == NOP_EXPR
6940 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6941 expr
= TREE_OPERAND (expr
, 0);
6943 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
6947 if (result
!= error_mark_node
)
6948 maybe_warn_about_useless_cast (type
, expr
, complain
);
6952 if (complain
& tf_error
)
6953 error ("invalid static_cast from type %qT to type %qT",
6954 TREE_TYPE (expr
), type
);
6955 return error_mark_node
;
6958 /* EXPR is an expression with member function or pointer-to-member
6959 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6960 not permitted by ISO C++, but we accept it in some modes. If we
6961 are not in one of those modes, issue a diagnostic. Return the
6962 converted expression. */
6965 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
6970 intype
= TREE_TYPE (expr
);
6971 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
6972 || TREE_CODE (intype
) == METHOD_TYPE
);
6974 if (!(complain
& tf_warning_or_error
))
6975 return error_mark_node
;
6977 if (pedantic
|| warn_pmf2ptr
)
6978 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
6979 "converting from %qT to %qT", intype
, type
);
6981 if (TREE_CODE (intype
) == METHOD_TYPE
)
6982 expr
= build_addr_func (expr
, complain
);
6983 else if (TREE_CODE (expr
) == PTRMEM_CST
)
6984 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
6987 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
6988 decl
= build_address (decl
);
6989 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
6992 if (expr
== error_mark_node
)
6993 return error_mark_node
;
6995 return build_nop (type
, expr
);
6998 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6999 If C_CAST_P is true, this reinterpret cast is being done as part of
7000 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7001 indicate whether or not reinterpret_cast was valid. */
7004 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7005 bool *valid_p
, tsubst_flags_t complain
)
7009 /* Assume the cast is invalid. */
7013 if (type
== error_mark_node
|| error_operand_p (expr
))
7014 return error_mark_node
;
7016 intype
= TREE_TYPE (expr
);
7018 /* Save casted types in the function's used types hash table. */
7019 used_types_insert (type
);
7021 /* [expr.reinterpret.cast]
7022 An lvalue expression of type T1 can be cast to the type
7023 "reference to T2" if an expression of type "pointer to T1" can be
7024 explicitly converted to the type "pointer to T2" using a
7025 reinterpret_cast. */
7026 if (TREE_CODE (type
) == REFERENCE_TYPE
)
7028 if (! lvalue_p (expr
))
7030 if (complain
& tf_error
)
7031 error ("invalid cast of an rvalue expression of type "
7034 return error_mark_node
;
7037 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7038 "B" are related class types; the reinterpret_cast does not
7039 adjust the pointer. */
7040 if (TYPE_PTR_P (intype
)
7041 && (complain
& tf_warning
)
7042 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
7043 COMPARE_BASE
| COMPARE_DERIVED
)))
7044 warning (0, "casting %qT to %qT does not dereference pointer",
7047 expr
= cp_build_addr_expr (expr
, complain
);
7049 if (warn_strict_aliasing
> 2)
7050 strict_aliasing_warning (TREE_TYPE (expr
), type
, expr
);
7052 if (expr
!= error_mark_node
)
7053 expr
= build_reinterpret_cast_1
7054 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
7056 if (expr
!= error_mark_node
)
7057 /* cp_build_indirect_ref isn't right for rvalue refs. */
7058 expr
= convert_from_reference (fold_convert (type
, expr
));
7062 /* As a G++ extension, we consider conversions from member
7063 functions, and pointers to member functions to
7064 pointer-to-function and pointer-to-void types. If
7065 -Wno-pmf-conversions has not been specified,
7066 convert_member_func_to_ptr will issue an error message. */
7067 if ((TYPE_PTRMEMFUNC_P (intype
)
7068 || TREE_CODE (intype
) == METHOD_TYPE
)
7069 && TYPE_PTR_P (type
)
7070 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
7071 || VOID_TYPE_P (TREE_TYPE (type
))))
7072 return convert_member_func_to_ptr (type
, expr
, complain
);
7074 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7075 array-to-pointer, and function-to-pointer conversions are
7077 expr
= decay_conversion (expr
, complain
);
7079 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7080 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7081 if (TREE_CODE (expr
) == NOP_EXPR
7082 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7083 expr
= TREE_OPERAND (expr
, 0);
7085 if (error_operand_p (expr
))
7086 return error_mark_node
;
7088 intype
= TREE_TYPE (expr
);
7090 /* [expr.reinterpret.cast]
7091 A pointer can be converted to any integral type large enough to
7092 hold it. ... A value of type std::nullptr_t can be converted to
7093 an integral type; the conversion has the same meaning and
7094 validity as a conversion of (void*)0 to the integral type. */
7095 if (CP_INTEGRAL_TYPE_P (type
)
7096 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
7098 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
7100 if (complain
& tf_error
)
7101 permerror (input_location
, "cast from %qT to %qT loses precision",
7104 return error_mark_node
;
7106 if (NULLPTR_TYPE_P (intype
))
7107 return build_int_cst (type
, 0);
7109 /* [expr.reinterpret.cast]
7110 A value of integral or enumeration type can be explicitly
7111 converted to a pointer. */
7112 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
7115 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7116 || TYPE_PTR_OR_PTRMEM_P (type
))
7117 && same_type_p (type
, intype
))
7119 return rvalue (expr
);
7120 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
7121 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7122 return build_nop (type
, expr
);
7123 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7124 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
7129 && check_for_casting_away_constness (intype
, type
,
7130 REINTERPRET_CAST_EXPR
,
7132 return error_mark_node
;
7133 /* Warn about possible alignment problems. */
7134 if (STRICT_ALIGNMENT
&& warn_cast_align
7135 && (complain
& tf_warning
)
7136 && !VOID_TYPE_P (type
)
7137 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
7138 && COMPLETE_TYPE_P (TREE_TYPE (type
))
7139 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
7140 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (intype
)))
7141 warning (OPT_Wcast_align
, "cast from %qT to %qT "
7142 "increases required alignment of target type", intype
, type
);
7144 /* We need to strip nops here, because the front end likes to
7145 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7147 if (warn_strict_aliasing
<= 2)
7148 strict_aliasing_warning (intype
, type
, sexpr
);
7150 return build_nop (type
, expr
);
7152 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
7153 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
7155 if (complain
& tf_warning
)
7156 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7157 object pointer type or vice versa is conditionally-supported." */
7158 warning (OPT_Wconditionally_supported
,
7159 "casting between pointer-to-function and pointer-to-object "
7160 "is conditionally-supported");
7161 return build_nop (type
, expr
);
7163 else if (VECTOR_TYPE_P (type
))
7164 return convert_to_vector (type
, expr
);
7165 else if (VECTOR_TYPE_P (intype
)
7166 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7167 return convert_to_integer_nofold (type
, expr
);
7172 if (complain
& tf_error
)
7173 error ("invalid cast from type %qT to type %qT", intype
, type
);
7174 return error_mark_node
;
7177 return cp_convert (type
, expr
, complain
);
7181 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7185 if (type
== error_mark_node
|| expr
== error_mark_node
)
7186 return error_mark_node
;
7188 if (processing_template_decl
)
7190 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7192 if (!TREE_SIDE_EFFECTS (t
)
7193 && type_dependent_expression_p (expr
))
7194 /* There might turn out to be side effects inside expr. */
7195 TREE_SIDE_EFFECTS (t
) = 1;
7196 return convert_from_reference (t
);
7199 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
7200 /*valid_p=*/NULL
, complain
);
7201 if (r
!= error_mark_node
)
7202 maybe_warn_about_useless_cast (type
, expr
, complain
);
7206 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7207 return an appropriate expression. Otherwise, return
7208 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7209 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7210 performing a C-style cast, its value upon return will indicate
7211 whether or not the conversion succeeded. */
7214 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
7218 tree reference_type
;
7220 /* Callers are responsible for handling error_mark_node as a
7221 destination type. */
7222 gcc_assert (dst_type
!= error_mark_node
);
7223 /* In a template, callers should be building syntactic
7224 representations of casts, not using this machinery. */
7225 gcc_assert (!processing_template_decl
);
7227 /* Assume the conversion is invalid. */
7231 if (!POINTER_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
7233 if (complain
& tf_error
)
7234 error ("invalid use of const_cast with type %qT, "
7235 "which is not a pointer, "
7236 "reference, nor a pointer-to-data-member type", dst_type
);
7237 return error_mark_node
;
7240 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
7242 if (complain
& tf_error
)
7243 error ("invalid use of const_cast with type %qT, which is a pointer "
7244 "or reference to a function type", dst_type
);
7245 return error_mark_node
;
7248 /* Save casted types in the function's used types hash table. */
7249 used_types_insert (dst_type
);
7251 src_type
= TREE_TYPE (expr
);
7252 /* Expressions do not really have reference types. */
7253 if (TREE_CODE (src_type
) == REFERENCE_TYPE
)
7254 src_type
= TREE_TYPE (src_type
);
7256 /* [expr.const.cast]
7258 For two object types T1 and T2, if a pointer to T1 can be explicitly
7259 converted to the type "pointer to T2" using a const_cast, then the
7260 following conversions can also be made:
7262 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7263 type T2 using the cast const_cast<T2&>;
7265 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7266 type T2 using the cast const_cast<T2&&>; and
7268 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7269 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7271 if (TREE_CODE (dst_type
) == REFERENCE_TYPE
)
7273 reference_type
= dst_type
;
7274 if (!TYPE_REF_IS_RVALUE (dst_type
)
7280 if (complain
& tf_error
)
7281 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7282 src_type
, dst_type
);
7283 return error_mark_node
;
7285 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7286 src_type
= build_pointer_type (src_type
);
7290 reference_type
= NULL_TREE
;
7291 /* If the destination type is not a reference type, the
7292 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7293 conversions are performed. */
7294 src_type
= type_decays_to (src_type
);
7295 if (src_type
== error_mark_node
)
7296 return error_mark_node
;
7299 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7301 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7306 /* This cast is actually a C-style cast. Issue a warning if
7307 the user is making a potentially unsafe cast. */
7308 check_for_casting_away_constness (src_type
, dst_type
,
7309 CAST_EXPR
, complain
);
7313 expr
= cp_build_addr_expr (expr
, complain
);
7314 if (expr
== error_mark_node
)
7315 return error_mark_node
;
7316 expr
= build_nop (reference_type
, expr
);
7317 return convert_from_reference (expr
);
7321 expr
= decay_conversion (expr
, complain
);
7322 if (expr
== error_mark_node
)
7323 return error_mark_node
;
7325 /* build_c_cast puts on a NOP_EXPR to make the result not an
7326 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7327 non-lvalue context. */
7328 if (TREE_CODE (expr
) == NOP_EXPR
7329 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7330 expr
= TREE_OPERAND (expr
, 0);
7331 return build_nop (dst_type
, expr
);
7335 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7336 TREE_TYPE (src_type
)))
7337 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7341 if (complain
& tf_error
)
7342 error ("invalid const_cast from type %qT to type %qT",
7343 src_type
, dst_type
);
7344 return error_mark_node
;
7348 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7352 if (type
== error_mark_node
|| error_operand_p (expr
))
7353 return error_mark_node
;
7355 if (processing_template_decl
)
7357 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7359 if (!TREE_SIDE_EFFECTS (t
)
7360 && type_dependent_expression_p (expr
))
7361 /* There might turn out to be side effects inside expr. */
7362 TREE_SIDE_EFFECTS (t
) = 1;
7363 return convert_from_reference (t
);
7366 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7367 if (r
!= error_mark_node
)
7368 maybe_warn_about_useless_cast (type
, expr
, complain
);
7372 /* Like cp_build_c_cast, but for the c-common bits. */
7375 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7377 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7380 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7381 preserve location information even for tree nodes that don't
7385 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
7387 cp_expr result
= cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7388 result
.set_location (loc
);
7392 /* Build an expression representing an explicit C-style cast to type
7393 TYPE of expression EXPR. */
7396 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7402 if (type
== error_mark_node
|| error_operand_p (expr
))
7403 return error_mark_node
;
7405 if (processing_template_decl
)
7407 tree t
= build_min (CAST_EXPR
, type
,
7408 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7409 /* We don't know if it will or will not have side effects. */
7410 TREE_SIDE_EFFECTS (t
) = 1;
7411 return convert_from_reference (t
);
7414 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7415 'Class') should always be retained, because this information aids
7416 in method lookup. */
7417 if (objc_is_object_ptr (type
)
7418 && objc_is_object_ptr (TREE_TYPE (expr
)))
7419 return build_nop (type
, expr
);
7421 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7422 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7423 if (TREE_CODE (type
) != REFERENCE_TYPE
7424 && TREE_CODE (value
) == NOP_EXPR
7425 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7426 value
= TREE_OPERAND (value
, 0);
7428 if (TREE_CODE (type
) == ARRAY_TYPE
)
7430 /* Allow casting from T1* to T2[] because Cfront allows it.
7431 NIHCL uses it. It is not valid ISO C++ however. */
7432 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7434 if (complain
& tf_error
)
7435 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7437 return error_mark_node
;
7438 type
= build_pointer_type (TREE_TYPE (type
));
7442 if (complain
& tf_error
)
7443 error ("ISO C++ forbids casting to an array type %qT", type
);
7444 return error_mark_node
;
7448 if (TREE_CODE (type
) == FUNCTION_TYPE
7449 || TREE_CODE (type
) == METHOD_TYPE
)
7451 if (complain
& tf_error
)
7452 error ("invalid cast to function type %qT", type
);
7453 return error_mark_node
;
7456 if (TYPE_PTR_P (type
)
7457 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7458 /* Casting to an integer of smaller size is an error detected elsewhere. */
7459 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7460 /* Don't warn about converting any constant. */
7461 && !TREE_CONSTANT (value
))
7462 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7463 "cast to pointer from integer of different size");
7465 /* A C-style cast can be a const_cast. */
7466 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7470 if (result
!= error_mark_node
)
7471 maybe_warn_about_useless_cast (type
, value
, complain
);
7475 /* Or a static cast. */
7476 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7477 &valid_p
, complain
);
7478 /* Or a reinterpret_cast. */
7480 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7481 &valid_p
, complain
);
7482 /* The static_cast or reinterpret_cast may be followed by a
7485 /* A valid cast may result in errors if, for example, a
7486 conversion to an ambiguous base class is required. */
7487 && !error_operand_p (result
))
7491 maybe_warn_about_useless_cast (type
, value
, complain
);
7493 /* Non-class rvalues always have cv-unqualified type. */
7494 if (!CLASS_TYPE_P (type
))
7495 type
= TYPE_MAIN_VARIANT (type
);
7496 result_type
= TREE_TYPE (result
);
7497 if (!CLASS_TYPE_P (result_type
) && TREE_CODE (type
) != REFERENCE_TYPE
)
7498 result_type
= TYPE_MAIN_VARIANT (result_type
);
7499 /* If the type of RESULT does not match TYPE, perform a
7500 const_cast to make it match. If the static_cast or
7501 reinterpret_cast succeeded, we will differ by at most
7502 cv-qualification, so the follow-on const_cast is guaranteed
7504 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
7506 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
7507 gcc_assert (valid_p
);
7512 return error_mark_node
;
7515 /* For use from the C common bits. */
7517 build_modify_expr (location_t location
,
7518 tree lhs
, tree
/*lhs_origtype*/,
7519 enum tree_code modifycode
,
7520 location_t
/*rhs_location*/, tree rhs
,
7521 tree
/*rhs_origtype*/)
7523 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
7524 tf_warning_or_error
);
7527 /* Build an assignment expression of lvalue LHS from value RHS.
7528 MODIFYCODE is the code for a binary operator that we use
7529 to combine the old value of LHS with RHS to get the new value.
7530 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7532 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7535 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7536 tree rhs
, tsubst_flags_t complain
)
7540 tree lhstype
= TREE_TYPE (lhs
);
7541 tree olhstype
= lhstype
;
7542 bool plain_assign
= (modifycode
== NOP_EXPR
);
7544 /* Avoid duplicate error messages from operands that had errors. */
7545 if (error_operand_p (lhs
) || error_operand_p (rhs
))
7546 return error_mark_node
;
7548 /* Handle control structure constructs used as "lvalues". Note that we
7549 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7550 switch (TREE_CODE (lhs
))
7552 /* Handle --foo = 5; as these are valid constructs in C++. */
7553 case PREDECREMENT_EXPR
:
7554 case PREINCREMENT_EXPR
:
7555 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7556 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7557 cp_stabilize_reference (TREE_OPERAND (lhs
, 0)),
7558 TREE_OPERAND (lhs
, 1));
7559 lhs
= build2 (COMPOUND_EXPR
, lhstype
, lhs
, TREE_OPERAND (lhs
, 0));
7563 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7564 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7565 cp_stabilize_reference (TREE_OPERAND (lhs
, 0)),
7566 TREE_OPERAND (lhs
, 1));
7567 lhs
= build2 (COMPOUND_EXPR
, lhstype
, lhs
, TREE_OPERAND (lhs
, 0));
7572 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7573 when neither operand has side-effects. */
7574 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7575 return error_mark_node
;
7577 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
7578 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
7580 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
7581 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
7583 TREE_OPERAND (lhs
, 0),
7584 TREE_OPERAND (lhs
, 1)),
7585 TREE_OPERAND (lhs
, 0),
7586 TREE_OPERAND (lhs
, 1));
7589 /* Handle (a ? b : c) used as an "lvalue". */
7592 /* Produce (a ? (b = rhs) : (c = rhs))
7593 except that the RHS goes through a save-expr
7594 so the code to compute it is only emitted once. */
7596 tree preeval
= NULL_TREE
;
7598 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
7600 if (complain
& tf_error
)
7601 error ("void value not ignored as it ought to be");
7602 return error_mark_node
;
7605 rhs
= stabilize_expr (rhs
, &preeval
);
7607 /* Check this here to avoid odd errors when trying to convert
7608 a throw to the type of the COND_EXPR. */
7609 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7610 return error_mark_node
;
7612 cond
= build_conditional_expr
7613 (input_location
, TREE_OPERAND (lhs
, 0),
7614 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 1),
7615 modifycode
, rhs
, complain
),
7616 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 2),
7617 modifycode
, rhs
, complain
),
7620 if (cond
== error_mark_node
)
7622 /* Make sure the code to compute the rhs comes out
7623 before the split. */
7625 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (lhs
), preeval
, cond
);
7633 if (modifycode
== INIT_EXPR
)
7635 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7636 /* Do the default thing. */;
7637 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
7639 /* Compound literal. */
7640 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
7641 /* Call convert to generate an error; see PR 11063. */
7642 rhs
= convert (lhstype
, rhs
);
7643 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
7644 TREE_SIDE_EFFECTS (result
) = 1;
7647 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
7648 /* Do the default thing. */;
7651 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
7652 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
7653 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
7655 release_tree_vector (rhs_vec
);
7656 if (result
== NULL_TREE
)
7657 return error_mark_node
;
7663 lhs
= require_complete_type_sfinae (lhs
, complain
);
7664 if (lhs
== error_mark_node
)
7665 return error_mark_node
;
7667 if (modifycode
== NOP_EXPR
)
7669 if (c_dialect_objc ())
7671 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
7676 /* `operator=' is not an inheritable operator. */
7677 if (! MAYBE_CLASS_TYPE_P (lhstype
))
7678 /* Do the default thing. */;
7681 result
= build_new_op (input_location
, MODIFY_EXPR
,
7682 LOOKUP_NORMAL
, lhs
, rhs
,
7683 make_node (NOP_EXPR
), /*overload=*/NULL
,
7685 if (result
== NULL_TREE
)
7686 return error_mark_node
;
7693 tree init
= NULL_TREE
;
7695 /* A binary op has been requested. Combine the old LHS
7696 value with the RHS producing the value we should actually
7697 store into the LHS. */
7698 gcc_assert (!((TREE_CODE (lhstype
) == REFERENCE_TYPE
7699 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
7700 || MAYBE_CLASS_TYPE_P (lhstype
)));
7702 /* Preevaluate the RHS to make sure its evaluation is complete
7703 before the lvalue-to-rvalue conversion of the LHS:
7705 [expr.ass] With respect to an indeterminately-sequenced
7706 function call, the operation of a compound assignment is a
7707 single evaluation. [ Note: Therefore, a function call shall
7708 not intervene between the lvalue-to-rvalue conversion and the
7709 side effect associated with any single compound assignment
7710 operator. -- end note ] */
7711 lhs
= cp_stabilize_reference (lhs
);
7713 rhs
= stabilize_expr (rhs
, &init
);
7714 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
7715 if (newrhs
== error_mark_node
)
7717 if (complain
& tf_error
)
7718 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
7719 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
7720 return error_mark_node
;
7724 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
7726 /* Now it looks like a plain assignment. */
7727 modifycode
= NOP_EXPR
;
7728 if (c_dialect_objc ())
7730 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
7735 gcc_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
);
7736 gcc_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
);
7739 /* The left-hand side must be an lvalue. */
7740 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7741 return error_mark_node
;
7743 /* Warn about modifying something that is `const'. Don't warn if
7744 this is initialization. */
7745 if (modifycode
!= INIT_EXPR
7746 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
7747 /* Functions are not modifiable, even though they are
7749 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
7750 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
7751 /* If it's an aggregate and any field is const, then it is
7752 effectively const. */
7753 || (CLASS_TYPE_P (lhstype
)
7754 && C_TYPE_FIELDS_READONLY (lhstype
))))
7756 if (complain
& tf_error
)
7757 cxx_readonly_error (lhs
, lv_assign
);
7759 return error_mark_node
;
7762 /* If storing into a structure or union member, it may have been given a
7763 lowered bitfield type. We need to convert to the declared type first,
7764 so retrieve it now. */
7766 olhstype
= unlowered_expr_type (lhs
);
7768 /* Convert new value to destination type. */
7770 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
7774 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
7776 if (modifycode
!= INIT_EXPR
)
7778 if (complain
& tf_error
)
7779 error ("assigning to an array from an initializer list");
7780 return error_mark_node
;
7782 if (check_array_initializer (lhs
, lhstype
, newrhs
))
7783 return error_mark_node
;
7784 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7785 if (newrhs
== error_mark_node
)
7786 return error_mark_node
;
7789 /* C++11 8.5/17: "If the destination type is an array of characters,
7790 an array of char16_t, an array of char32_t, or an array of wchar_t,
7791 and the initializer is a string literal...". */
7792 else if (TREE_CODE (newrhs
) == STRING_CST
7793 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
7794 && modifycode
== INIT_EXPR
)
7796 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7797 if (newrhs
== error_mark_node
)
7798 return error_mark_node
;
7801 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
7802 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
7804 if (complain
& tf_error
)
7805 error ("incompatible types in assignment of %qT to %qT",
7806 TREE_TYPE (rhs
), lhstype
);
7807 return error_mark_node
;
7810 /* Allow array assignment in compiler-generated code. */
7811 else if (!current_function_decl
7812 || !DECL_DEFAULTED_FN (current_function_decl
))
7814 /* This routine is used for both initialization and assignment.
7815 Make sure the diagnostic message differentiates the context. */
7816 if (complain
& tf_error
)
7818 if (modifycode
== INIT_EXPR
)
7819 error ("array used as initializer");
7821 error ("invalid array assignment");
7823 return error_mark_node
;
7826 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
7827 ? 1 + (modifycode
!= INIT_EXPR
): 0;
7828 return build_vec_init (lhs
, NULL_TREE
, newrhs
,
7829 /*explicit_value_init_p=*/false,
7830 from_array
, complain
);
7833 if (modifycode
== INIT_EXPR
)
7834 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7835 LOOKUP_ONLYCONVERTING. */
7836 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
7837 ICR_INIT
, NULL_TREE
, 0,
7840 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
7841 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
7843 if (!same_type_p (lhstype
, olhstype
))
7844 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
7846 if (modifycode
!= INIT_EXPR
)
7848 if (TREE_CODE (newrhs
) == CALL_EXPR
7849 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
7850 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
7852 /* Can't initialize directly from a TARGET_EXPR, since that would
7853 cause the lhs to be constructed twice, and possibly result in
7854 accidental self-initialization. So we force the TARGET_EXPR to be
7855 expanded without a target. */
7856 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
7857 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
7858 TREE_OPERAND (newrhs
, 0));
7861 if (newrhs
== error_mark_node
)
7862 return error_mark_node
;
7864 if (c_dialect_objc () && flag_objc_gc
)
7866 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
7872 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
7873 lhstype
, lhs
, newrhs
);
7875 TREE_SIDE_EFFECTS (result
) = 1;
7877 TREE_NO_WARNING (result
) = 1;
7883 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7884 tree rhs
, tsubst_flags_t complain
)
7886 tree orig_lhs
= lhs
;
7887 tree orig_rhs
= rhs
;
7888 tree overload
= NULL_TREE
;
7889 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
7891 if (processing_template_decl
)
7893 if (modifycode
== NOP_EXPR
7894 || type_dependent_expression_p (lhs
)
7895 || type_dependent_expression_p (rhs
))
7896 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
7897 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
7900 lhs
= build_non_dependent_expr (lhs
);
7901 rhs
= build_non_dependent_expr (rhs
);
7904 if (modifycode
!= NOP_EXPR
)
7906 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
7907 lhs
, rhs
, op
, &overload
, complain
);
7910 if (rval
== error_mark_node
)
7912 TREE_NO_WARNING (rval
) = 1;
7913 if (processing_template_decl
)
7915 if (overload
!= NULL_TREE
)
7916 return (build_min_non_dep_op_overload
7917 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
7919 return (build_min_non_dep
7920 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
7925 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
7928 /* Helper function for get_delta_difference which assumes FROM is a base
7929 class of TO. Returns a delta for the conversion of pointer-to-member
7930 of FROM to pointer-to-member of TO. If the conversion is invalid and
7931 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7932 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7933 If C_CAST_P is true, this conversion is taking place as part of a
7937 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
7938 tsubst_flags_t complain
)
7943 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
7946 if (binfo
== error_mark_node
)
7948 if (!(complain
& tf_error
))
7949 return error_mark_node
;
7951 error (" in pointer to member function conversion");
7952 return size_zero_node
;
7956 if (kind
!= bk_via_virtual
)
7957 return BINFO_OFFSET (binfo
);
7959 /* FROM is a virtual base class of TO. Issue an error or warning
7960 depending on whether or not this is a reinterpret cast. */
7962 if (!(complain
& tf_error
))
7963 return error_mark_node
;
7965 error ("pointer to member conversion via virtual base %qT",
7966 BINFO_TYPE (binfo_from_vbase (binfo
)));
7968 return size_zero_node
;
7975 /* Get difference in deltas for different pointer to member function
7976 types. If the conversion is invalid and tf_error is not set in
7977 COMPLAIN, returns error_mark_node, otherwise returns an integer
7978 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7979 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7980 conversions as well. If C_CAST_P is true this conversion is taking
7981 place as part of a C-style cast.
7983 Note that the naming of FROM and TO is kind of backwards; the return
7984 value is what we add to a TO in order to get a FROM. They are named
7985 this way because we call this function to find out how to convert from
7986 a pointer to member of FROM to a pointer to member of TO. */
7989 get_delta_difference (tree from
, tree to
,
7990 bool allow_inverse_p
,
7991 bool c_cast_p
, tsubst_flags_t complain
)
7995 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
7996 /* Pointer to member of incomplete class is permitted*/
7997 result
= size_zero_node
;
7999 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
8001 if (result
== error_mark_node
)
8002 return error_mark_node
;
8006 if (!allow_inverse_p
)
8008 if (!(complain
& tf_error
))
8009 return error_mark_node
;
8011 error_not_base_type (from
, to
);
8012 error (" in pointer to member conversion");
8013 result
= size_zero_node
;
8017 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
8019 if (result
== error_mark_node
)
8020 return error_mark_node
;
8023 result
= size_diffop_loc (input_location
,
8024 size_zero_node
, result
);
8027 if (!(complain
& tf_error
))
8028 return error_mark_node
;
8030 error_not_base_type (from
, to
);
8031 error (" in pointer to member conversion");
8032 result
= size_zero_node
;
8037 return convert_to_integer (ptrdiff_type_node
, result
);
8040 /* Return a constructor for the pointer-to-member-function TYPE using
8041 the other components as specified. */
8044 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
8049 vec
<constructor_elt
, va_gc
> *v
;
8051 /* Pull the FIELD_DECLs out of the type. */
8052 pfn_field
= TYPE_FIELDS (type
);
8053 delta_field
= DECL_CHAIN (pfn_field
);
8055 /* Make sure DELTA has the type we want. */
8056 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
8058 /* Convert to the correct target type if necessary. */
8059 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
8061 /* Finish creating the initializer. */
8063 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
8064 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
8065 u
= build_constructor (type
, v
);
8066 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
8067 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
8068 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
8070 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
8075 /* Build a constructor for a pointer to member function. It can be
8076 used to initialize global variables, local variable, or used
8077 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8080 If FORCE is nonzero, then force this conversion, even if
8081 we would rather not do it. Usually set when using an explicit
8082 cast. A C-style cast is being processed iff C_CAST_P is true.
8084 Return error_mark_node, if something goes wrong. */
8087 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
8088 tsubst_flags_t complain
)
8094 if (error_operand_p (pfn
))
8095 return error_mark_node
;
8097 pfn_type
= TREE_TYPE (pfn
);
8098 to_type
= build_ptrmemfunc_type (type
);
8100 /* Handle multiple conversions of pointer to member functions. */
8101 if (TYPE_PTRMEMFUNC_P (pfn_type
))
8103 tree delta
= NULL_TREE
;
8104 tree npfn
= NULL_TREE
;
8108 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
8109 LOOKUP_NORMAL
, complain
))
8111 if (complain
& tf_error
)
8112 error ("invalid conversion to type %qT from type %qT",
8115 return error_mark_node
;
8118 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
8119 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
8121 c_cast_p
, complain
);
8122 if (n
== error_mark_node
)
8123 return error_mark_node
;
8125 /* We don't have to do any conversion to convert a
8126 pointer-to-member to its own type. But, we don't want to
8127 just return a PTRMEM_CST if there's an explicit cast; that
8128 cast should make the expression an invalid template argument. */
8129 if (TREE_CODE (pfn
) != PTRMEM_CST
)
8131 if (same_type_p (to_type
, pfn_type
))
8133 else if (integer_zerop (n
))
8134 return build_reinterpret_cast (to_type
, pfn
,
8138 if (TREE_SIDE_EFFECTS (pfn
))
8139 pfn
= save_expr (pfn
);
8141 /* Obtain the function pointer and the current DELTA. */
8142 if (TREE_CODE (pfn
) == PTRMEM_CST
)
8143 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
8146 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
8147 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
8150 /* Just adjust the DELTA field. */
8151 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8152 (TREE_TYPE (delta
), ptrdiff_type_node
));
8153 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
8154 n
= cp_build_binary_op (input_location
,
8155 LSHIFT_EXPR
, n
, integer_one_node
,
8157 delta
= cp_build_binary_op (input_location
,
8158 PLUS_EXPR
, delta
, n
, complain
);
8159 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
8162 /* Handle null pointer to member function conversions. */
8163 if (null_ptr_cst_p (pfn
))
8165 pfn
= cp_build_c_cast (type
, pfn
, complain
);
8166 return build_ptrmemfunc1 (to_type
,
8171 if (type_unknown_p (pfn
))
8172 return instantiate_type (type
, pfn
, complain
);
8174 fn
= TREE_OPERAND (pfn
, 0);
8175 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8176 /* In a template, we will have preserved the
8178 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
8179 return make_ptrmem_cst (to_type
, fn
);
8182 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8185 ??? There is no consistency as to the types returned for the above
8186 values. Some code acts as if it were a sizetype and some as if it were
8187 integer_type_node. */
8190 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
8192 tree type
= TREE_TYPE (cst
);
8193 tree fn
= PTRMEM_CST_MEMBER (cst
);
8194 tree ptr_class
, fn_class
;
8196 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8198 /* The class that the function belongs to. */
8199 fn_class
= DECL_CONTEXT (fn
);
8201 /* The class that we're creating a pointer to member of. */
8202 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
8204 /* First, calculate the adjustment to the function's class. */
8205 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
8206 /*c_cast_p=*/0, tf_warning_or_error
);
8208 if (!DECL_VIRTUAL_P (fn
))
8209 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
8210 build_addr_func (fn
, tf_warning_or_error
));
8213 /* If we're dealing with a virtual function, we have to adjust 'this'
8214 again, to point to the base which provides the vtable entry for
8215 fn; the call will do the opposite adjustment. */
8216 tree orig_class
= DECL_CONTEXT (fn
);
8217 tree binfo
= binfo_or_else (orig_class
, fn_class
);
8218 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8219 *delta
, BINFO_OFFSET (binfo
));
8221 /* We set PFN to the vtable offset at which the function can be
8222 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8223 case delta is shifted left, and then incremented). */
8224 *pfn
= DECL_VINDEX (fn
);
8225 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
8226 TYPE_SIZE_UNIT (vtable_entry_type
));
8228 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
8230 case ptrmemfunc_vbit_in_pfn
:
8231 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
8235 case ptrmemfunc_vbit_in_delta
:
8236 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
8237 *delta
, integer_one_node
);
8238 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8239 *delta
, integer_one_node
);
8246 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
8250 /* Return an expression for PFN from the pointer-to-member function
8254 pfn_from_ptrmemfunc (tree t
)
8256 if (TREE_CODE (t
) == PTRMEM_CST
)
8261 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8266 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
8269 /* Return an expression for DELTA from the pointer-to-member function
8273 delta_from_ptrmemfunc (tree t
)
8275 if (TREE_CODE (t
) == PTRMEM_CST
)
8280 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8285 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
8288 /* Convert value RHS to type TYPE as preparation for an assignment to
8289 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8290 implicit conversion is. If FNDECL is non-NULL, we are doing the
8291 conversion in order to pass the PARMNUMth argument of FNDECL.
8292 If FNDECL is NULL, we are doing the conversion in function pointer
8293 argument passing, conversion in initialization, etc. */
8296 convert_for_assignment (tree type
, tree rhs
,
8297 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8298 tsubst_flags_t complain
, int flags
)
8301 enum tree_code coder
;
8303 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8304 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
8305 rhs
= TREE_OPERAND (rhs
, 0);
8307 /* Handle [dcl.init.list] direct-list-initialization from
8308 single element of enumeration with a fixed underlying type. */
8309 if (is_direct_enum_init (type
, rhs
))
8311 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
8312 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
8313 rhs
= cp_build_c_cast (type
, elt
, complain
);
8315 rhs
= error_mark_node
;
8318 rhstype
= TREE_TYPE (rhs
);
8319 coder
= TREE_CODE (rhstype
);
8321 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
8322 && vector_types_convertible_p (type
, rhstype
, true))
8324 rhs
= mark_rvalue_use (rhs
);
8325 return convert (type
, rhs
);
8328 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8329 return error_mark_node
;
8330 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8331 return error_mark_node
;
8333 /* The RHS of an assignment cannot have void type. */
8334 if (coder
== VOID_TYPE
)
8336 if (complain
& tf_error
)
8337 error ("void value not ignored as it ought to be");
8338 return error_mark_node
;
8341 if (c_dialect_objc ())
8345 tree rname
= fndecl
;
8356 selector
= objc_message_selector ();
8358 if (selector
&& parmno
> 1)
8365 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8367 rhs
= mark_rvalue_use (rhs
);
8368 return convert (type
, rhs
);
8374 The expression is implicitly converted (clause _conv_) to the
8375 cv-unqualified type of the left operand.
8377 We allow bad conversions here because by the time we get to this point
8378 we are committed to doing the conversion. If we end up doing a bad
8379 conversion, convert_like will complain. */
8380 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8382 /* When -Wno-pmf-conversions is use, we just silently allow
8383 conversions from pointers-to-members to plain pointers. If
8384 the conversion doesn't work, cp_convert will complain. */
8386 && TYPE_PTR_P (type
)
8387 && TYPE_PTRMEMFUNC_P (rhstype
))
8388 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8391 if (complain
& tf_error
)
8393 /* If the right-hand side has unknown type, then it is an
8394 overloaded function. Call instantiate_type to get error
8396 if (rhstype
== unknown_type_node
)
8397 instantiate_type (type
, rhs
, tf_warning_or_error
);
8399 error ("cannot convert %qT to %qT for argument %qP to %qD",
8400 rhstype
, type
, parmnum
, fndecl
);
8404 case ICR_DEFAULT_ARGUMENT
:
8405 error ("cannot convert %qT to %qT in default argument",
8409 error ("cannot convert %qT to %qT in argument passing",
8412 case ICR_CONVERTING
:
8413 error ("cannot convert %qT to %qT",
8417 error ("cannot convert %qT to %qT in initialization",
8421 error ("cannot convert %qT to %qT in return",
8425 error ("cannot convert %qT to %qT in assignment",
8431 if (TYPE_PTR_P (rhstype
)
8432 && TYPE_PTR_P (type
)
8433 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
8434 && CLASS_TYPE_P (TREE_TYPE (type
))
8435 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
8436 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8437 (TREE_TYPE (rhstype
))),
8438 "class type %qT is incomplete", TREE_TYPE (rhstype
));
8440 return error_mark_node
;
8443 if (warn_suggest_attribute_format
)
8445 const enum tree_code codel
= TREE_CODE (type
);
8446 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8448 && check_missing_format_attribute (type
, rhstype
)
8449 && (complain
& tf_warning
))
8453 case ICR_DEFAULT_ARGUMENT
:
8455 warning (OPT_Wsuggest_attribute_format
,
8456 "parameter %qP of %qD might be a candidate "
8457 "for a format attribute", parmnum
, fndecl
);
8459 warning (OPT_Wsuggest_attribute_format
,
8460 "parameter might be a candidate "
8461 "for a format attribute");
8463 case ICR_CONVERTING
:
8464 warning (OPT_Wsuggest_attribute_format
,
8465 "target of conversion might be a candidate "
8466 "for a format attribute");
8469 warning (OPT_Wsuggest_attribute_format
,
8470 "target of initialization might be a candidate "
8471 "for a format attribute");
8474 warning (OPT_Wsuggest_attribute_format
,
8475 "return type might be a candidate "
8476 "for a format attribute");
8479 warning (OPT_Wsuggest_attribute_format
,
8480 "left-hand side of assignment might be a candidate "
8481 "for a format attribute");
8488 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8490 if (warn_parentheses
8491 && TREE_CODE (type
) == BOOLEAN_TYPE
8492 && TREE_CODE (rhs
) == MODIFY_EXPR
8493 && !TREE_NO_WARNING (rhs
)
8494 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
8495 && (complain
& tf_warning
))
8497 location_t loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8499 warning_at (loc
, OPT_Wparentheses
,
8500 "suggest parentheses around assignment used as truth value");
8501 TREE_NO_WARNING (rhs
) = 1;
8504 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
8508 /* Convert RHS to be of type TYPE.
8509 If EXP is nonzero, it is the target of the initialization.
8510 ERRTYPE indicates what kind of error the implicit conversion is.
8512 Two major differences between the behavior of
8513 `convert_for_assignment' and `convert_for_initialization'
8514 are that references are bashed in the former, while
8515 copied in the latter, and aggregates are assigned in
8516 the former (operator=) while initialized in the
8519 If using constructor make sure no conversion operator exists, if one does
8520 exist, an ambiguity exists. */
8523 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
8524 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8525 tsubst_flags_t complain
)
8527 enum tree_code codel
= TREE_CODE (type
);
8529 enum tree_code coder
;
8531 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8532 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8533 if (TREE_CODE (rhs
) == NOP_EXPR
8534 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
8535 && codel
!= REFERENCE_TYPE
)
8536 rhs
= TREE_OPERAND (rhs
, 0);
8538 if (type
== error_mark_node
8539 || rhs
== error_mark_node
8540 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
8541 return error_mark_node
;
8543 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
8545 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
8546 && TREE_CODE (type
) != ARRAY_TYPE
8547 && (TREE_CODE (type
) != REFERENCE_TYPE
8548 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
8549 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
8550 && !TYPE_REFFN_P (type
))
8551 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
8552 rhs
= decay_conversion (rhs
, complain
);
8554 rhstype
= TREE_TYPE (rhs
);
8555 coder
= TREE_CODE (rhstype
);
8557 if (coder
== ERROR_MARK
)
8558 return error_mark_node
;
8560 /* We accept references to incomplete types, so we can
8561 return here before checking if RHS is of complete type. */
8563 if (codel
== REFERENCE_TYPE
)
8565 /* This should eventually happen in convert_arguments. */
8566 int savew
= 0, savee
= 0;
8569 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
8570 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
8573 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
8574 inform (DECL_SOURCE_LOCATION (fndecl
),
8575 "in passing argument %P of %qD", parmnum
, fndecl
);
8581 exp
= require_complete_type_sfinae (exp
, complain
);
8582 if (exp
== error_mark_node
)
8583 return error_mark_node
;
8585 rhstype
= non_reference (rhstype
);
8587 type
= complete_type (type
);
8589 if (DIRECT_INIT_EXPR_P (type
, rhs
))
8590 /* Don't try to do copy-initialization if we already have
8591 direct-initialization. */
8594 if (MAYBE_CLASS_TYPE_P (type
))
8595 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
8597 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
8601 /* If RETVAL is the address of, or a reference to, a local variable or
8602 temporary give an appropriate warning and return true. */
8605 maybe_warn_about_returning_address_of_local (tree retval
)
8607 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
8608 tree whats_returned
= retval
;
8612 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
8613 whats_returned
= TREE_OPERAND (whats_returned
, 1);
8614 else if (CONVERT_EXPR_P (whats_returned
)
8615 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
8616 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8621 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
8623 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8625 while (TREE_CODE (whats_returned
) == COMPONENT_REF
8626 || TREE_CODE (whats_returned
) == ARRAY_REF
)
8627 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8629 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8631 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
8632 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
8634 warning (OPT_Wreturn_local_addr
, "returning reference to temporary");
8637 if (VAR_P (whats_returned
)
8638 && DECL_NAME (whats_returned
)
8639 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
8641 warning (OPT_Wreturn_local_addr
, "reference to non-lvalue returned");
8646 if (DECL_P (whats_returned
)
8647 && DECL_NAME (whats_returned
)
8648 && DECL_FUNCTION_SCOPE_P (whats_returned
)
8649 && !is_capture_proxy (whats_returned
)
8650 && !(TREE_STATIC (whats_returned
)
8651 || TREE_PUBLIC (whats_returned
)))
8653 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8654 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8655 OPT_Wreturn_local_addr
,
8656 "reference to local variable %qD returned",
8658 else if (TREE_CODE (whats_returned
) == LABEL_DECL
)
8659 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8660 OPT_Wreturn_local_addr
, "address of label %qD returned",
8663 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8664 OPT_Wreturn_local_addr
, "address of local variable %qD "
8665 "returned", whats_returned
);
8672 /* Check that returning RETVAL from the current function is valid.
8673 Return an expression explicitly showing all conversions required to
8674 change RETVAL into the function return type, and to assign it to
8675 the DECL_RESULT for the function. Set *NO_WARNING to true if
8676 code reaches end of non-void function warning shouldn't be issued
8677 on this RETURN_EXPR. */
8680 check_return_expr (tree retval
, bool *no_warning
)
8683 /* The type actually returned by the function. */
8685 /* The type the function is declared to return, or void if
8686 the declared type is incomplete. */
8688 int fn_returns_value_p
;
8689 bool named_return_value_okay_p
;
8691 *no_warning
= false;
8693 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
8695 error_at (EXPR_LOCATION (retval
), "use of %<_Cilk_spawn%> in a return "
8696 "statement is not allowed");
8700 /* A `volatile' function is one that isn't supposed to return, ever.
8701 (This is a G++ extension, used to get better code for functions
8702 that call the `volatile' function.) */
8703 if (TREE_THIS_VOLATILE (current_function_decl
))
8704 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8706 /* Check for various simple errors. */
8707 if (DECL_DESTRUCTOR_P (current_function_decl
))
8710 error ("returning a value from a destructor");
8713 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
8715 if (in_function_try_handler
)
8716 /* If a return statement appears in a handler of the
8717 function-try-block of a constructor, the program is ill-formed. */
8718 error ("cannot return from a handler of a function-try-block of a constructor");
8720 /* You can't return a value from a constructor. */
8721 error ("returning a value from a constructor");
8725 const tree saved_retval
= retval
;
8727 if (processing_template_decl
)
8729 current_function_returns_value
= 1;
8731 if (check_for_bare_parameter_packs (retval
))
8732 return error_mark_node
;
8734 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
8735 || (retval
!= NULL_TREE
8736 && type_dependent_expression_p (retval
)))
8740 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8742 /* Deduce auto return type from a return statement. */
8743 if (current_function_auto_return_pattern
)
8748 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
8750 /* Give a helpful error message. */
8751 error ("return-statement with no value, in function returning %qT",
8752 current_function_auto_return_pattern
);
8753 inform (input_location
, "only plain %<auto%> return type can be "
8754 "deduced to %<void%>");
8755 type
= error_mark_node
;
8757 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
8759 error ("returning initializer list");
8760 type
= error_mark_node
;
8766 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
8767 type
= do_auto_deduction (current_function_auto_return_pattern
,
8771 if (type
== error_mark_node
)
8773 else if (functype
== current_function_auto_return_pattern
)
8774 apply_deduced_return_type (current_function_decl
, type
);
8775 else if (!same_type_p (type
, functype
))
8777 if (LAMBDA_FUNCTION_P (current_function_decl
))
8778 error ("inconsistent types %qT and %qT deduced for "
8779 "lambda return type", functype
, type
);
8781 error ("inconsistent deduction for auto return type: "
8782 "%qT and then %qT", functype
, type
);
8787 result
= DECL_RESULT (current_function_decl
);
8788 valtype
= TREE_TYPE (result
);
8789 gcc_assert (valtype
!= NULL_TREE
);
8790 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
8792 /* Check for a return statement with no return value in a function
8793 that's supposed to return a value. */
8794 if (!retval
&& fn_returns_value_p
)
8796 if (functype
!= error_mark_node
)
8797 permerror (input_location
, "return-statement with no value, in "
8798 "function returning %qT", valtype
);
8799 /* Remember that this function did return. */
8800 current_function_returns_value
= 1;
8801 /* And signal caller that TREE_NO_WARNING should be set on the
8802 RETURN_EXPR to avoid control reaches end of non-void function
8803 warnings in tree-cfg.c. */
8806 /* Check for a return statement with a value in a function that
8807 isn't supposed to return a value. */
8808 else if (retval
&& !fn_returns_value_p
)
8810 if (VOID_TYPE_P (TREE_TYPE (retval
)))
8811 /* You can return a `void' value from a function of `void'
8812 type. In that case, we have to evaluate the expression for
8813 its side-effects. */
8814 finish_expr_stmt (retval
);
8816 permerror (input_location
, "return-statement with a value, in function "
8817 "returning 'void'");
8818 current_function_returns_null
= 1;
8820 /* There's really no value to return, after all. */
8824 /* Remember that this function can sometimes return without a
8826 current_function_returns_null
= 1;
8828 /* Remember that this function did return a value. */
8829 current_function_returns_value
= 1;
8831 /* Check for erroneous operands -- but after giving ourselves a
8832 chance to provide an error about returning a value from a void
8834 if (error_operand_p (retval
))
8836 current_function_return_value
= error_mark_node
;
8837 return error_mark_node
;
8840 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8841 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
8842 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
8843 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
8845 && retval
&& null_ptr_cst_p (retval
))
8846 warning (0, "%<operator new%> must not return NULL unless it is "
8847 "declared %<throw()%> (or -fcheck-new is in effect)");
8849 /* Effective C++ rule 15. See also start_function. */
8851 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
))
8855 /* The function return type must be a reference to the current
8857 if (TREE_CODE (valtype
) == REFERENCE_TYPE
8858 && same_type_ignoring_top_level_qualifiers_p
8859 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
8861 /* Returning '*this' is obviously OK. */
8862 if (retval
== current_class_ref
)
8864 /* If we are calling a function whose return type is the same of
8865 the current class reference, it is ok. */
8866 else if (INDIRECT_REF_P (retval
)
8867 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
8872 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
8875 if (processing_template_decl
)
8877 /* We should not have changed the return value. */
8878 gcc_assert (retval
== saved_retval
);
8882 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8884 [...] For a function with a class return type, if the expression
8885 in the return statement is the name of a local object, and the cv-
8886 unqualified type of the local object is the same as the function
8887 return type, an implementation is permitted to omit creating the tem-
8888 porary object to hold the function return value [...]
8890 So, if this is a value-returning function that always returns the same
8891 local variable, remember it.
8893 It might be nice to be more flexible, and choose the first suitable
8894 variable even if the function sometimes returns something else, but
8895 then we run the risk of clobbering the variable we chose if the other
8896 returned expression uses the chosen variable somehow. And people expect
8897 this restriction, anyway. (jason 2000-11-19)
8899 See finish_function and finalize_nrv for the rest of this optimization. */
8901 named_return_value_okay_p
=
8902 (retval
!= NULL_TREE
8903 /* Must be a local, automatic variable. */
8905 && DECL_CONTEXT (retval
) == current_function_decl
8906 && ! TREE_STATIC (retval
)
8907 /* And not a lambda or anonymous union proxy. */
8908 && !DECL_HAS_VALUE_EXPR_P (retval
)
8909 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
8910 /* The cv-unqualified type of the returned value must be the
8911 same as the cv-unqualified return type of the
8913 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8914 (TYPE_MAIN_VARIANT (functype
)))
8915 /* And the returned value must be non-volatile. */
8916 && ! TYPE_VOLATILE (TREE_TYPE (retval
)));
8918 if (fn_returns_value_p
&& flag_elide_constructors
)
8920 if (named_return_value_okay_p
8921 && (current_function_return_value
== NULL_TREE
8922 || current_function_return_value
== retval
))
8923 current_function_return_value
= retval
;
8925 current_function_return_value
= error_mark_node
;
8928 /* We don't need to do any conversions when there's nothing being
8933 /* Do any required conversions. */
8934 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
8935 /* No conversions are required. */
8939 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
8941 /* The functype's return type will have been set to void, if it
8942 was an incomplete type. Just treat this as 'return;' */
8943 if (VOID_TYPE_P (functype
))
8944 return error_mark_node
;
8946 /* If we had an id-expression obfuscated by force_paren_expr, we need
8947 to undo it so we can try to treat it as an rvalue below. */
8948 retval
= maybe_undo_parenthesized_ref (retval
);
8950 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8951 treated as an rvalue for the purposes of overload resolution to
8952 favor move constructors over copy constructors.
8954 Note that these conditions are similar to, but not as strict as,
8955 the conditions for the named return value optimization. */
8956 if ((cxx_dialect
!= cxx98
)
8957 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
8958 || TREE_CODE (retval
) == PARM_DECL
)
8959 && DECL_CONTEXT (retval
) == current_function_decl
8960 && !TREE_STATIC (retval
)
8961 /* This is only interesting for class type. */
8962 && CLASS_TYPE_P (functype
))
8963 flags
= flags
| LOOKUP_PREFER_RVALUE
;
8965 /* First convert the value to the function's return type, then
8966 to the type of return value's location to handle the
8967 case that functype is smaller than the valtype. */
8968 retval
= convert_for_initialization
8969 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
8970 tf_warning_or_error
);
8971 retval
= convert (valtype
, retval
);
8973 /* If the conversion failed, treat this just like `return;'. */
8974 if (retval
== error_mark_node
)
8976 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8977 else if (! cfun
->returns_struct
8978 && TREE_CODE (retval
) == TARGET_EXPR
8979 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
8980 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
8981 TREE_OPERAND (retval
, 0));
8982 else if (maybe_warn_about_returning_address_of_local (retval
))
8983 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
8984 build_zero_cst (TREE_TYPE (retval
)));
8987 /* Actually copy the value returned into the appropriate location. */
8988 if (retval
&& retval
!= result
)
8989 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
8995 /* Returns nonzero if the pointer-type FROM can be converted to the
8996 pointer-type TO via a qualification conversion. If CONSTP is -1,
8997 then we return nonzero if the pointers are similar, and the
8998 cv-qualification signature of FROM is a proper subset of that of TO.
9000 If CONSTP is positive, then all outer pointers have been
9004 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
9006 bool to_more_cv_qualified
= false;
9007 bool is_opaque_pointer
= false;
9009 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9011 if (TREE_CODE (to
) != TREE_CODE (from
))
9014 if (TREE_CODE (from
) == OFFSET_TYPE
9015 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
9016 TYPE_OFFSET_BASETYPE (to
)))
9019 /* Const and volatile mean something different for function types,
9020 so the usual checks are not appropriate. */
9021 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
9023 if (!at_least_as_qualified_p (to
, from
))
9026 if (!at_least_as_qualified_p (from
, to
))
9030 to_more_cv_qualified
= true;
9034 constp
&= TYPE_READONLY (to
);
9037 if (VECTOR_TYPE_P (to
))
9038 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9040 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
9041 return ((constp
>= 0 || to_more_cv_qualified
)
9042 && (is_opaque_pointer
9043 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
9047 /* When comparing, say, char ** to char const **, this function takes
9048 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9049 types to this function. */
9052 comp_ptr_ttypes (tree to
, tree from
)
9054 return comp_ptr_ttypes_real (to
, from
, 1);
9057 /* Returns true iff FNTYPE is a non-class type that involves
9058 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9059 if a parameter type is ill-formed. */
9062 error_type_p (const_tree type
)
9066 switch (TREE_CODE (type
))
9072 case REFERENCE_TYPE
:
9074 return error_type_p (TREE_TYPE (type
));
9078 if (error_type_p (TREE_TYPE (type
)))
9080 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
9081 if (error_type_p (TREE_VALUE (t
)))
9086 if (TYPE_PTRMEMFUNC_P (type
))
9087 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
9095 /* Returns true if to and from are (possibly multi-level) pointers to the same
9096 type or inheritance-related types, regardless of cv-quals. */
9099 ptr_reasonably_similar (const_tree to
, const_tree from
)
9101 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9103 /* Any target type is similar enough to void. */
9104 if (VOID_TYPE_P (to
))
9105 return !error_type_p (from
);
9106 if (VOID_TYPE_P (from
))
9107 return !error_type_p (to
);
9109 if (TREE_CODE (to
) != TREE_CODE (from
))
9112 if (TREE_CODE (from
) == OFFSET_TYPE
9113 && comptypes (TYPE_OFFSET_BASETYPE (to
),
9114 TYPE_OFFSET_BASETYPE (from
),
9115 COMPARE_BASE
| COMPARE_DERIVED
))
9118 if (VECTOR_TYPE_P (to
)
9119 && vector_types_convertible_p (to
, from
, false))
9122 if (TREE_CODE (to
) == INTEGER_TYPE
9123 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
9126 if (TREE_CODE (to
) == FUNCTION_TYPE
)
9127 return !error_type_p (to
) && !error_type_p (from
);
9129 if (!TYPE_PTR_P (to
))
9131 /* When either type is incomplete avoid DERIVED_FROM_P,
9132 which may call complete_type (c++/57942). */
9133 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
9135 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
9136 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
9141 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9142 pointer-to-member types) are the same, ignoring cv-qualification at
9146 comp_ptr_ttypes_const (tree to
, tree from
)
9148 bool is_opaque_pointer
= false;
9150 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9152 if (TREE_CODE (to
) != TREE_CODE (from
))
9155 if (TREE_CODE (from
) == OFFSET_TYPE
9156 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
9157 TYPE_OFFSET_BASETYPE (to
)))
9160 if (VECTOR_TYPE_P (to
))
9161 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9163 if (!TYPE_PTR_P (to
))
9164 return (is_opaque_pointer
9165 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
9169 /* Returns the type qualifiers for this type, including the qualifiers on the
9170 elements for an array type. */
9173 cp_type_quals (const_tree type
)
9176 /* This CONST_CAST is okay because strip_array_types returns its
9177 argument unmodified and we assign it to a const_tree. */
9178 type
= strip_array_types (CONST_CAST_TREE (type
));
9179 if (type
== error_mark_node
9180 /* Quals on a FUNCTION_TYPE are memfn quals. */
9181 || TREE_CODE (type
) == FUNCTION_TYPE
)
9182 return TYPE_UNQUALIFIED
;
9183 quals
= TYPE_QUALS (type
);
9184 /* METHOD and REFERENCE_TYPEs should never have quals. */
9185 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
9186 && TREE_CODE (type
) != REFERENCE_TYPE
)
9187 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
9188 == TYPE_UNQUALIFIED
));
9192 /* Returns the function-ref-qualifier for TYPE */
9195 type_memfn_rqual (const_tree type
)
9197 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
9198 || TREE_CODE (type
) == METHOD_TYPE
);
9200 if (!FUNCTION_REF_QUALIFIED (type
))
9201 return REF_QUAL_NONE
;
9202 else if (FUNCTION_RVALUE_QUALIFIED (type
))
9203 return REF_QUAL_RVALUE
;
9205 return REF_QUAL_LVALUE
;
9208 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9212 type_memfn_quals (const_tree type
)
9214 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9215 return TYPE_QUALS (type
);
9216 else if (TREE_CODE (type
) == METHOD_TYPE
)
9217 return cp_type_quals (class_of_this_parm (type
));
9222 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9223 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9226 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
9228 /* Could handle METHOD_TYPE here if necessary. */
9229 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
9230 if (TYPE_QUALS (type
) == memfn_quals
9231 && type_memfn_rqual (type
) == rqual
)
9234 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9236 tree result
= build_qualified_type (type
, memfn_quals
);
9237 return build_ref_qualified_type (result
, rqual
);
9240 /* Returns nonzero if TYPE is const or volatile. */
9243 cv_qualified_p (const_tree type
)
9245 int quals
= cp_type_quals (type
);
9246 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
9249 /* Returns nonzero if the TYPE contains a mutable member. */
9252 cp_has_mutable_p (const_tree type
)
9254 /* This CONST_CAST is okay because strip_array_types returns its
9255 argument unmodified and we assign it to a const_tree. */
9256 type
= strip_array_types (CONST_CAST_TREE(type
));
9258 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
9261 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9262 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9263 approximation. In particular, consider:
9266 struct S { int i; };
9267 const S s = { f(); }
9269 Here, we will make "s" as TREE_READONLY (because it is declared
9270 "const") -- only to reverse ourselves upon seeing that the
9271 initializer is non-constant. */
9274 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
9276 tree type
= TREE_TYPE (decl
);
9278 if (type
== error_mark_node
)
9281 if (TREE_CODE (decl
) == TYPE_DECL
)
9284 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
9285 && type_quals
!= TYPE_UNQUALIFIED
));
9287 /* Avoid setting TREE_READONLY incorrectly. */
9288 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9289 constructor can produce constant init, so rely on cp_finish_decl to
9290 clear TREE_READONLY if the variable has non-constant init. */
9292 /* If the type has (or might have) a mutable component, that component
9293 might be modified. */
9294 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
9295 type_quals
&= ~TYPE_QUAL_CONST
;
9297 c_apply_type_quals_to_decl (type_quals
, decl
);
9300 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9301 exemplar types such that casting T1 to T2 is casting away constness
9302 if and only if there is no implicit conversion from T1 to T2. */
9305 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
9310 /* [expr.const.cast]
9312 For multi-level pointer to members and multi-level mixed pointers
9313 and pointers to members (conv.qual), the "member" aspect of a
9314 pointer to member level is ignored when determining if a const
9315 cv-qualifier has been cast away. */
9316 /* [expr.const.cast]
9318 For two pointer types:
9320 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9321 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9324 casting from X1 to X2 casts away constness if, for a non-pointer
9325 type T there does not exist an implicit conversion (clause
9328 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9332 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9333 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
9334 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
9336 *t1
= cp_build_qualified_type (void_type_node
,
9337 cp_type_quals (*t1
));
9338 *t2
= cp_build_qualified_type (void_type_node
,
9339 cp_type_quals (*t2
));
9343 quals1
= cp_type_quals (*t1
);
9344 quals2
= cp_type_quals (*t2
);
9346 if (TYPE_PTRDATAMEM_P (*t1
))
9347 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
9349 *t1
= TREE_TYPE (*t1
);
9350 if (TYPE_PTRDATAMEM_P (*t2
))
9351 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
9353 *t2
= TREE_TYPE (*t2
);
9355 casts_away_constness_r (t1
, t2
, complain
);
9356 *t1
= build_pointer_type (*t1
);
9357 *t2
= build_pointer_type (*t2
);
9358 *t1
= cp_build_qualified_type (*t1
, quals1
);
9359 *t2
= cp_build_qualified_type (*t2
, quals2
);
9362 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9365 ??? This function returns non-zero if casting away qualifiers not
9366 just const. We would like to return to the caller exactly which
9367 qualifiers are casted away to give more accurate diagnostics.
9371 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
9373 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
9375 /* [expr.const.cast]
9377 Casting from an lvalue of type T1 to an lvalue of type T2
9378 using a reference cast casts away constness if a cast from an
9379 rvalue of type "pointer to T1" to the type "pointer to T2"
9380 casts away constness. */
9381 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
? TREE_TYPE (t1
) : t1
);
9382 return casts_away_constness (build_pointer_type (t1
),
9383 build_pointer_type (TREE_TYPE (t2
)),
9387 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
9388 /* [expr.const.cast]
9390 Casting from an rvalue of type "pointer to data member of X
9391 of type T1" to the type "pointer to data member of Y of type
9392 T2" casts away constness if a cast from an rvalue of type
9393 "pointer to T1" to the type "pointer to T2" casts away
9395 return casts_away_constness
9396 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
9397 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
9400 /* Casting away constness is only something that makes sense for
9401 pointer or reference types. */
9402 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
9405 /* Top-level qualifiers don't matter. */
9406 t1
= TYPE_MAIN_VARIANT (t1
);
9407 t2
= TYPE_MAIN_VARIANT (t2
);
9408 casts_away_constness_r (&t1
, &t2
, complain
);
9409 if (!can_convert (t2
, t1
, complain
))
9415 /* If T is a REFERENCE_TYPE return the type to which T refers.
9416 Otherwise, return T itself. */
9419 non_reference (tree t
)
9421 if (t
&& TREE_CODE (t
) == REFERENCE_TYPE
)
9427 /* Return nonzero if REF is an lvalue valid for this language;
9428 otherwise, print an error message and return zero. USE says
9429 how the lvalue is being used and so selects the error message. */
9432 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
9434 cp_lvalue_kind kind
= lvalue_kind (ref
);
9436 if (kind
== clk_none
)
9438 if (complain
& tf_error
)
9439 lvalue_error (input_location
, use
);
9442 else if (kind
& (clk_rvalueref
|clk_class
))
9444 if (!(complain
& tf_error
))
9446 if (kind
& clk_class
)
9447 /* Make this a permerror because we used to accept it. */
9448 permerror (input_location
, "using temporary as lvalue");
9450 error ("using xvalue (rvalue reference) as lvalue");
9455 /* Return true if a user-defined literal operator is a raw operator. */
9458 check_raw_literal_operator (const_tree decl
)
9460 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9463 bool maybe_raw_p
= false;
9465 /* Count the number and type of arguments and check for ellipsis. */
9466 for (argtype
= argtypes
, arity
= 0;
9467 argtype
&& argtype
!= void_list_node
;
9468 ++arity
, argtype
= TREE_CHAIN (argtype
))
9470 tree t
= TREE_VALUE (argtype
);
9472 if (same_type_p (t
, const_string_type_node
))
9476 return false; /* Found ellipsis. */
9478 if (!maybe_raw_p
|| arity
!= 1)
9485 /* Return true if a user-defined literal operator has one of the allowed
9489 check_literal_operator_args (const_tree decl
,
9490 bool *long_long_unsigned_p
, bool *long_double_p
)
9492 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9494 *long_long_unsigned_p
= false;
9495 *long_double_p
= false;
9496 if (processing_template_decl
|| processing_specialization
)
9497 return argtypes
== void_list_node
;
9504 /* Count the number and type of arguments and check for ellipsis. */
9505 for (argtype
= argtypes
, arity
= 0;
9506 argtype
&& argtype
!= void_list_node
;
9507 argtype
= TREE_CHAIN (argtype
))
9509 tree t
= TREE_VALUE (argtype
);
9514 bool maybe_raw_p
= false;
9516 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
9518 t
= TYPE_MAIN_VARIANT (t
);
9519 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
9520 || same_type_p (t
, wchar_type_node
)
9521 || same_type_p (t
, char16_type_node
)
9522 || same_type_p (t
, char32_type_node
))
9524 argtype
= TREE_CHAIN (argtype
);
9527 t
= TREE_VALUE (argtype
);
9528 if (maybe_raw_p
&& argtype
== void_list_node
)
9530 else if (same_type_p (t
, size_type_node
))
9539 else if (same_type_p (t
, long_long_unsigned_type_node
))
9542 *long_long_unsigned_p
= true;
9544 else if (same_type_p (t
, long_double_type_node
))
9547 *long_double_p
= true;
9549 else if (same_type_p (t
, char_type_node
))
9551 else if (same_type_p (t
, wchar_type_node
))
9553 else if (same_type_p (t
, char16_type_node
))
9555 else if (same_type_p (t
, char32_type_node
))
9561 return false; /* Found ellipsis. */
9563 if (arity
!= max_arity
)
9570 /* Always returns false since unlike C90, C++ has no concept of implicit
9571 function declarations. */
9574 c_decl_implicit (const_tree
)