1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
5 Free Software Foundation, Inc.
6 Hacked by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* This file is part of the C++ front end.
26 It contains routines to build C++ expressions given their operands,
27 including computing the types of the result, C and C++ specific error
28 checks, and some optimization. */
32 #include "coretypes.h"
38 #include "diagnostic.h"
42 #include "c-family/c-common.h"
43 #include "c-family/c-objc.h"
46 static tree pfn_from_ptrmemfunc (tree);
47 static tree delta_from_ptrmemfunc (tree);
48 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
50 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
51 static tree rationalize_conditional_expr (enum tree_code, tree,
53 static int comp_ptr_ttypes_real (tree, tree, int);
54 static bool comp_except_types (tree, tree, bool);
55 static bool comp_array_types (const_tree, const_tree, bool);
56 static tree pointer_diff (tree, tree, tree);
57 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
58 static void casts_away_constness_r (tree *, tree *);
59 static bool casts_away_constness (tree, tree);
60 static void maybe_warn_about_returning_address_of_local (tree);
61 static tree lookup_destructor (tree, tree, tree);
62 static void warn_args_num (location_t, tree, bool);
63 static int convert_arguments (tree, VEC(tree,gc) **, tree, int,
66 /* Do `exp = require_complete_type (exp);' to make sure exp
67 does not have an incomplete type. (That includes void types.)
68 Returns error_mark_node if the VALUE does not have
69 complete type when this function returns. */
72 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
76 if (processing_template_decl || value == error_mark_node)
79 if (TREE_CODE (value) == OVERLOAD)
80 type = unknown_type_node;
82 type = TREE_TYPE (value);
84 if (type == error_mark_node)
85 return error_mark_node;
87 /* First, detect a valid value with a complete type. */
88 if (COMPLETE_TYPE_P (type))
91 if (complete_type_or_maybe_complain (type, value, complain))
94 return error_mark_node;
98 require_complete_type (tree value)
100 return require_complete_type_sfinae (value, tf_warning_or_error);
103 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
104 a template instantiation, do the instantiation. Returns TYPE,
105 whether or not it could be completed, unless something goes
106 horribly wrong, in which case the error_mark_node is returned. */
109 complete_type (tree type)
111 if (type == NULL_TREE)
112 /* Rather than crash, we return something sure to cause an error
114 return error_mark_node;
116 if (type == error_mark_node || COMPLETE_TYPE_P (type))
118 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
120 tree t = complete_type (TREE_TYPE (type));
121 unsigned int needs_constructing, has_nontrivial_dtor;
122 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
125 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
127 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
128 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
130 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
131 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
134 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
135 instantiate_class_template (TYPE_MAIN_VARIANT (type));
140 /* Like complete_type, but issue an error if the TYPE cannot be completed.
141 VALUE is used for informative diagnostics.
142 Returns NULL_TREE if the type cannot be made complete. */
145 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
147 type = complete_type (type);
148 if (type == error_mark_node)
149 /* We already issued an error. */
151 else if (!COMPLETE_TYPE_P (type))
153 if (complain & tf_error)
154 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
162 complete_type_or_else (tree type, tree value)
164 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
167 /* Return truthvalue of whether type of EXP is instantiated. */
170 type_unknown_p (const_tree exp)
172 return (TREE_CODE (exp) == TREE_LIST
173 || TREE_TYPE (exp) == unknown_type_node);
177 /* Return the common type of two parameter lists.
178 We assume that comptypes has already been done and returned 1;
179 if that isn't so, this may crash.
181 As an optimization, free the space we allocate if the parameter
182 lists are already common. */
185 commonparms (tree p1, tree p2)
187 tree oldargs = p1, newargs, n;
191 len = list_length (p1);
192 newargs = tree_last (p1);
194 if (newargs == void_list_node)
203 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
208 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
210 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
212 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
215 else if (! TREE_PURPOSE (p1))
217 if (TREE_PURPOSE (p2))
219 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
225 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
227 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
229 if (TREE_VALUE (p1) != TREE_VALUE (p2))
232 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
235 TREE_VALUE (n) = TREE_VALUE (p1);
243 /* Given a type, perhaps copied for a typedef,
244 find the "original" version of it. */
246 original_type (tree t)
248 int quals = cp_type_quals (t);
249 while (t != error_mark_node
250 && TYPE_NAME (t) != NULL_TREE)
252 tree x = TYPE_NAME (t);
253 if (TREE_CODE (x) != TYPE_DECL)
255 x = DECL_ORIGINAL_TYPE (x);
260 return cp_build_qualified_type (t, quals);
263 /* Return the common type for two arithmetic types T1 and T2 under the
264 usual arithmetic conversions. The default conversions have already
265 been applied, and enumerated types converted to their compatible
269 cp_common_type (tree t1, tree t2)
271 enum tree_code code1 = TREE_CODE (t1);
272 enum tree_code code2 = TREE_CODE (t2);
276 /* In what follows, we slightly generalize the rules given in [expr] so
277 as to deal with `long long' and `complex'. First, merge the
279 attributes = (*targetm.merge_type_attributes) (t1, t2);
281 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
283 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
284 return build_type_attribute_variant (t1, attributes);
289 /* FIXME: Attributes. */
290 gcc_assert (ARITHMETIC_TYPE_P (t1)
291 || TREE_CODE (t1) == VECTOR_TYPE
292 || UNSCOPED_ENUM_P (t1));
293 gcc_assert (ARITHMETIC_TYPE_P (t2)
294 || TREE_CODE (t2) == VECTOR_TYPE
295 || UNSCOPED_ENUM_P (t2));
297 /* If one type is complex, form the common type of the non-complex
298 components, then make that complex. Use T1 or T2 if it is the
300 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
302 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
303 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
305 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
307 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
308 return build_type_attribute_variant (t1, attributes);
309 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
310 return build_type_attribute_variant (t2, attributes);
312 return build_type_attribute_variant (build_complex_type (subtype),
316 if (code1 == VECTOR_TYPE)
318 /* When we get here we should have two vectors of the same size.
319 Just prefer the unsigned one if present. */
320 if (TYPE_UNSIGNED (t1))
321 return build_type_attribute_variant (t1, attributes);
323 return build_type_attribute_variant (t2, attributes);
326 /* If only one is real, use it as the result. */
327 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
328 return build_type_attribute_variant (t1, attributes);
329 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
330 return build_type_attribute_variant (t2, attributes);
332 /* Both real or both integers; use the one with greater precision. */
333 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
334 return build_type_attribute_variant (t1, attributes);
335 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
336 return build_type_attribute_variant (t2, attributes);
338 /* The types are the same; no need to do anything fancy. */
339 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
340 return build_type_attribute_variant (t1, attributes);
342 if (code1 != REAL_TYPE)
344 /* If one is unsigned long long, then convert the other to unsigned
346 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
347 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
348 return build_type_attribute_variant (long_long_unsigned_type_node,
350 /* If one is a long long, and the other is an unsigned long, and
351 long long can represent all the values of an unsigned long, then
352 convert to a long long. Otherwise, convert to an unsigned long
353 long. Otherwise, if either operand is long long, convert the
356 Since we're here, we know the TYPE_PRECISION is the same;
357 therefore converting to long long cannot represent all the values
358 of an unsigned long, so we choose unsigned long long in that
360 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
361 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
363 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
364 ? long_long_unsigned_type_node
365 : long_long_integer_type_node);
366 return build_type_attribute_variant (t, attributes);
368 if (int128_integer_type_node != NULL_TREE
369 && (same_type_p (TYPE_MAIN_VARIANT (t1),
370 int128_integer_type_node)
371 || same_type_p (TYPE_MAIN_VARIANT (t2),
372 int128_integer_type_node)))
374 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
375 ? int128_unsigned_type_node
376 : int128_integer_type_node);
377 return build_type_attribute_variant (t, attributes);
380 /* Go through the same procedure, but for longs. */
381 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
382 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
383 return build_type_attribute_variant (long_unsigned_type_node,
385 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
386 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
388 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
389 ? long_unsigned_type_node : long_integer_type_node);
390 return build_type_attribute_variant (t, attributes);
392 /* Otherwise prefer the unsigned one. */
393 if (TYPE_UNSIGNED (t1))
394 return build_type_attribute_variant (t1, attributes);
396 return build_type_attribute_variant (t2, attributes);
400 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
401 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
402 return build_type_attribute_variant (long_double_type_node,
404 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
406 return build_type_attribute_variant (double_type_node,
408 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
409 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
410 return build_type_attribute_variant (float_type_node,
413 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
414 the standard C++ floating-point types. Logic earlier in this
415 function has already eliminated the possibility that
416 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
417 compelling reason to choose one or the other. */
418 return build_type_attribute_variant (t1, attributes);
422 /* T1 and T2 are arithmetic or enumeration types. Return the type
423 that will result from the "usual arithmetic conversions" on T1 and
424 T2 as described in [expr]. */
427 type_after_usual_arithmetic_conversions (tree t1, tree t2)
429 gcc_assert (ARITHMETIC_TYPE_P (t1)
430 || TREE_CODE (t1) == VECTOR_TYPE
431 || UNSCOPED_ENUM_P (t1));
432 gcc_assert (ARITHMETIC_TYPE_P (t2)
433 || TREE_CODE (t2) == VECTOR_TYPE
434 || UNSCOPED_ENUM_P (t2));
436 /* Perform the integral promotions. We do not promote real types here. */
437 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
438 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
440 t1 = type_promotes_to (t1);
441 t2 = type_promotes_to (t2);
444 return cp_common_type (t1, t2);
448 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
449 composite_pointer_operation operation)
454 emit_diagnostic (kind, input_location, 0,
455 "comparison between "
456 "distinct pointer types %qT and %qT lacks a cast",
460 emit_diagnostic (kind, input_location, 0,
461 "conversion between "
462 "distinct pointer types %qT and %qT lacks a cast",
465 case CPO_CONDITIONAL_EXPR:
466 emit_diagnostic (kind, input_location, 0,
467 "conditional expression between "
468 "distinct pointer types %qT and %qT lacks a cast",
476 /* Subroutine of composite_pointer_type to implement the recursive
477 case. See that function for documentation of the parameters. */
480 composite_pointer_type_r (tree t1, tree t2,
481 composite_pointer_operation operation,
482 tsubst_flags_t complain)
489 /* Determine the types pointed to by T1 and T2. */
490 if (TREE_CODE (t1) == POINTER_TYPE)
492 pointee1 = TREE_TYPE (t1);
493 pointee2 = TREE_TYPE (t2);
497 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
498 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
503 Otherwise, the composite pointer type is a pointer type
504 similar (_conv.qual_) to the type of one of the operands,
505 with a cv-qualification signature (_conv.qual_) that is the
506 union of the cv-qualification signatures of the operand
508 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
509 result_type = pointee1;
510 else if ((TREE_CODE (pointee1) == POINTER_TYPE
511 && TREE_CODE (pointee2) == POINTER_TYPE)
512 || (TYPE_PTR_TO_MEMBER_P (pointee1)
513 && TYPE_PTR_TO_MEMBER_P (pointee2)))
515 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
517 if (result_type == error_mark_node)
518 return error_mark_node;
522 if (complain & tf_error)
523 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
525 return error_mark_node;
526 result_type = void_type_node;
528 result_type = cp_build_qualified_type (result_type,
529 (cp_type_quals (pointee1)
530 | cp_type_quals (pointee2)));
531 /* If the original types were pointers to members, so is the
533 if (TYPE_PTR_TO_MEMBER_P (t1))
535 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
536 TYPE_PTRMEM_CLASS_TYPE (t2)))
538 if (complain & tf_error)
539 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
541 return error_mark_node;
543 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
547 result_type = build_pointer_type (result_type);
549 /* Merge the attributes. */
550 attributes = (*targetm.merge_type_attributes) (t1, t2);
551 return build_type_attribute_variant (result_type, attributes);
554 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
555 ARG1 and ARG2 are the values with those types. The OPERATION is to
556 describe the operation between the pointer types,
557 in case an error occurs.
559 This routine also implements the computation of a common type for
560 pointers-to-members as per [expr.eq]. */
563 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
564 composite_pointer_operation operation,
565 tsubst_flags_t complain)
572 If one operand is a null pointer constant, the composite pointer
573 type is the type of the other operand. */
574 if (null_ptr_cst_p (arg1))
576 if (null_ptr_cst_p (arg2))
583 If one of the operands has type "pointer to cv1 void*", then
584 the other has type "pointer to cv2T", and the composite pointer
585 type is "pointer to cv12 void", where cv12 is the union of cv1
588 If either type is a pointer to void, make sure it is T1. */
589 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
597 /* Now, if T1 is a pointer to void, merge the qualifiers. */
598 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
603 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
608 pedwarn (input_location, OPT_pedantic,
609 "ISO C++ forbids comparison between "
610 "pointer of type %<void *%> and pointer-to-function");
613 pedwarn (input_location, OPT_pedantic,
614 "ISO C++ forbids conversion between "
615 "pointer of type %<void *%> and pointer-to-function");
617 case CPO_CONDITIONAL_EXPR:
618 pedwarn (input_location, OPT_pedantic,
619 "ISO C++ forbids conditional expression between "
620 "pointer of type %<void *%> and pointer-to-function");
627 = cp_build_qualified_type (void_type_node,
628 (cp_type_quals (TREE_TYPE (t1))
629 | cp_type_quals (TREE_TYPE (t2))));
630 result_type = build_pointer_type (result_type);
631 /* Merge the attributes. */
632 attributes = (*targetm.merge_type_attributes) (t1, t2);
633 return build_type_attribute_variant (result_type, attributes);
636 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
637 && TREE_CODE (t2) == POINTER_TYPE)
639 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
640 return objc_common_type (t1, t2);
643 /* [expr.eq] permits the application of a pointer conversion to
644 bring the pointers to a common type. */
645 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
646 && CLASS_TYPE_P (TREE_TYPE (t1))
647 && CLASS_TYPE_P (TREE_TYPE (t2))
648 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
651 class1 = TREE_TYPE (t1);
652 class2 = TREE_TYPE (t2);
654 if (DERIVED_FROM_P (class1, class2))
655 t2 = (build_pointer_type
656 (cp_build_qualified_type (class1, cp_type_quals (class2))));
657 else if (DERIVED_FROM_P (class2, class1))
658 t1 = (build_pointer_type
659 (cp_build_qualified_type (class2, cp_type_quals (class1))));
662 if (complain & tf_error)
663 composite_pointer_error (DK_ERROR, t1, t2, operation);
664 return error_mark_node;
667 /* [expr.eq] permits the application of a pointer-to-member
668 conversion to change the class type of one of the types. */
669 else if (TYPE_PTR_TO_MEMBER_P (t1)
670 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
671 TYPE_PTRMEM_CLASS_TYPE (t2)))
673 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
674 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
676 if (DERIVED_FROM_P (class1, class2))
677 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
678 else if (DERIVED_FROM_P (class2, class1))
679 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
682 if (complain & tf_error)
686 error ("comparison between distinct "
687 "pointer-to-member types %qT and %qT lacks a cast",
691 error ("conversion between distinct "
692 "pointer-to-member types %qT and %qT lacks a cast",
695 case CPO_CONDITIONAL_EXPR:
696 error ("conditional expression between distinct "
697 "pointer-to-member types %qT and %qT lacks a cast",
703 return error_mark_node;
707 return composite_pointer_type_r (t1, t2, operation, complain);
710 /* Return the merged type of two types.
711 We assume that comptypes has already been done and returned 1;
712 if that isn't so, this may crash.
714 This just combines attributes and default arguments; any other
715 differences would cause the two types to compare unalike. */
718 merge_types (tree t1, tree t2)
720 enum tree_code code1;
721 enum tree_code code2;
724 /* Save time if the two types are the same. */
727 if (original_type (t1) == original_type (t2))
730 /* If one type is nonsense, use the other. */
731 if (t1 == error_mark_node)
733 if (t2 == error_mark_node)
736 /* Merge the attributes. */
737 attributes = (*targetm.merge_type_attributes) (t1, t2);
739 if (TYPE_PTRMEMFUNC_P (t1))
740 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
741 if (TYPE_PTRMEMFUNC_P (t2))
742 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
744 code1 = TREE_CODE (t1);
745 code2 = TREE_CODE (t2);
748 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
749 if (code1 == TYPENAME_TYPE)
751 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
752 code1 = TREE_CODE (t1);
756 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
757 code2 = TREE_CODE (t2);
765 /* For two pointers, do this recursively on the target type. */
767 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
768 int quals = cp_type_quals (t1);
770 if (code1 == POINTER_TYPE)
771 t1 = build_pointer_type (target);
773 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
774 t1 = build_type_attribute_variant (t1, attributes);
775 t1 = cp_build_qualified_type (t1, quals);
777 if (TREE_CODE (target) == METHOD_TYPE)
778 t1 = build_ptrmemfunc_type (t1);
787 quals = cp_type_quals (t1);
788 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
789 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
790 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
792 t1 = cp_build_qualified_type (t1, quals);
798 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
799 /* Save space: see if the result is identical to one of the args. */
800 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
801 return build_type_attribute_variant (t1, attributes);
802 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
803 return build_type_attribute_variant (t2, attributes);
804 /* Merge the element types, and have a size if either arg has one. */
805 t1 = build_cplus_array_type
806 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
811 /* Function types: prefer the one that specified arg types.
812 If both do, merge the arg types. Also merge the return types. */
814 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
815 tree p1 = TYPE_ARG_TYPES (t1);
816 tree p2 = TYPE_ARG_TYPES (t2);
820 /* Save space: see if the result is identical to one of the args. */
821 if (valtype == TREE_TYPE (t1) && ! p2)
822 return cp_build_type_attribute_variant (t1, attributes);
823 if (valtype == TREE_TYPE (t2) && ! p1)
824 return cp_build_type_attribute_variant (t2, attributes);
826 /* Simple way if one arg fails to specify argument types. */
827 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
829 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
832 parms = commonparms (p1, p2);
834 rval = build_function_type (valtype, parms);
835 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
836 rval = apply_memfn_quals (rval, type_memfn_quals (t1));
837 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
838 TYPE_RAISES_EXCEPTIONS (t2),
840 t1 = build_exception_variant (rval, raises);
846 /* Get this value the long way, since TYPE_METHOD_BASETYPE
847 is just the main variant of this. */
848 tree basetype = class_of_this_parm (t2);
849 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
850 TYPE_RAISES_EXCEPTIONS (t2),
854 /* If this was a member function type, get back to the
855 original type of type member function (i.e., without
856 the class instance variable up front. */
857 t1 = build_function_type (TREE_TYPE (t1),
858 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
859 t2 = build_function_type (TREE_TYPE (t2),
860 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
861 t3 = merge_types (t1, t2);
862 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
863 TYPE_ARG_TYPES (t3));
864 t1 = build_exception_variant (t3, raises);
869 /* There is no need to merge attributes into a TYPENAME_TYPE.
870 When the type is instantiated it will have whatever
871 attributes result from the instantiation. */
877 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
879 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
882 return cp_build_type_attribute_variant (t1, attributes);
885 /* Return the ARRAY_TYPE type without its domain. */
888 strip_array_domain (tree type)
891 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
892 if (TYPE_DOMAIN (type) == NULL_TREE)
894 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
895 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
898 /* Wrapper around cp_common_type that is used by c-common.c and other
899 front end optimizations that remove promotions.
901 Return the common type for two arithmetic types T1 and T2 under the
902 usual arithmetic conversions. The default conversions have already
903 been applied, and enumerated types converted to their compatible
907 common_type (tree t1, tree t2)
909 /* If one type is nonsense, use the other */
910 if (t1 == error_mark_node)
912 if (t2 == error_mark_node)
915 return cp_common_type (t1, t2);
918 /* Return the common type of two pointer types T1 and T2. This is the
919 type for the result of most arithmetic operations if the operands
920 have the given two types.
922 We assume that comp_target_types has already been done and returned
923 nonzero; if that isn't so, this may crash. */
926 common_pointer_type (tree t1, tree t2)
928 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
929 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
930 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
932 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
933 CPO_CONVERSION, tf_warning_or_error);
936 /* Compare two exception specifier types for exactness or subsetness, if
937 allowed. Returns false for mismatch, true for match (same, or
940 [except.spec] "If a class X ... objects of class X or any class publicly
941 and unambiguously derived from X. Similarly, if a pointer type Y * ...
942 exceptions of type Y * or that are pointers to any type publicly and
943 unambiguously derived from Y. Otherwise a function only allows exceptions
944 that have the same type ..."
945 This does not mention cv qualifiers and is different to what throw
946 [except.throw] and catch [except.catch] will do. They will ignore the
947 top level cv qualifiers, and allow qualifiers in the pointer to class
950 We implement the letter of the standard. */
953 comp_except_types (tree a, tree b, bool exact)
955 if (same_type_p (a, b))
959 if (cp_type_quals (a) || cp_type_quals (b))
962 if (TREE_CODE (a) == POINTER_TYPE
963 && TREE_CODE (b) == POINTER_TYPE)
967 if (cp_type_quals (a) || cp_type_quals (b))
971 if (TREE_CODE (a) != RECORD_TYPE
972 || TREE_CODE (b) != RECORD_TYPE)
975 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
981 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
982 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
983 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
984 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
985 are unordered, but we've already filtered out duplicates. Most lists will
986 be in order, we should try to make use of that. */
989 comp_except_specs (const_tree t1, const_tree t2, int exact)
998 /* First handle noexcept. */
999 if (exact < ce_exact)
1001 /* noexcept(false) is compatible with no exception-specification,
1002 and stricter than any spec. */
1003 if (t1 == noexcept_false_spec)
1004 return t2 == NULL_TREE || exact == ce_derived;
1005 /* Even a derived noexcept(false) is compatible with no
1006 exception-specification. */
1007 if (t2 == noexcept_false_spec)
1008 return t1 == NULL_TREE;
1010 /* Otherwise, if we aren't looking for an exact match, noexcept is
1011 equivalent to throw(). */
1012 if (t1 == noexcept_true_spec)
1013 t1 = empty_except_spec;
1014 if (t2 == noexcept_true_spec)
1015 t2 = empty_except_spec;
1018 /* If any noexcept is left, it is only comparable to itself;
1019 either we're looking for an exact match or we're redeclaring a
1020 template with dependent noexcept. */
1021 if ((t1 && TREE_PURPOSE (t1))
1022 || (t2 && TREE_PURPOSE (t2)))
1024 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1026 if (t1 == NULL_TREE) /* T1 is ... */
1027 return t2 == NULL_TREE || exact == ce_derived;
1028 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1029 return t2 != NULL_TREE && !TREE_VALUE (t2);
1030 if (t2 == NULL_TREE) /* T2 is ... */
1032 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1033 return exact == ce_derived;
1035 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1036 Count how many we find, to determine exactness. For exact matching and
1037 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1039 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1041 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1043 tree a = TREE_VALUE (probe);
1044 tree b = TREE_VALUE (t2);
1046 if (comp_except_types (a, b, exact))
1048 if (probe == base && exact > ce_derived)
1049 base = TREE_CHAIN (probe);
1054 if (probe == NULL_TREE)
1057 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1060 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1061 [] can match [size]. */
1064 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1073 /* The type of the array elements must be the same. */
1074 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1077 d1 = TYPE_DOMAIN (t1);
1078 d2 = TYPE_DOMAIN (t2);
1083 /* If one of the arrays is dimensionless, and the other has a
1084 dimension, they are of different types. However, it is valid to
1092 declarations for an array object can specify
1093 array types that differ by the presence or absence of a major
1094 array bound (_dcl.array_). */
1096 return allow_redeclaration;
1098 /* Check that the dimensions are the same. */
1100 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1102 max1 = TYPE_MAX_VALUE (d1);
1103 max2 = TYPE_MAX_VALUE (d2);
1104 if (processing_template_decl && !abi_version_at_least (2)
1105 && !value_dependent_expression_p (max1)
1106 && !value_dependent_expression_p (max2))
1108 /* With abi-1 we do not fold non-dependent array bounds, (and
1109 consequently mangle them incorrectly). We must therefore
1110 fold them here, to verify the domains have the same
1116 if (!cp_tree_equal (max1, max2))
1122 /* Compare the relative position of T1 and T2 into their respective
1123 template parameter list.
1124 T1 and T2 must be template parameter types.
1125 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1128 comp_template_parms_position (tree t1, tree t2)
1130 tree index1, index2;
1131 gcc_assert (t1 && t2
1132 && TREE_CODE (t1) == TREE_CODE (t2)
1133 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1134 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1135 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1137 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1138 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1140 /* Then compare their relative position. */
1141 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1142 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1143 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1144 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1150 /* Subroutine in comptypes. */
1153 structural_comptypes (tree t1, tree t2, int strict)
1158 /* Suppress errors caused by previously reported errors. */
1159 if (t1 == error_mark_node || t2 == error_mark_node)
1162 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1164 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1165 current instantiation. */
1166 if (TREE_CODE (t1) == TYPENAME_TYPE)
1167 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1169 if (TREE_CODE (t2) == TYPENAME_TYPE)
1170 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1172 if (TYPE_PTRMEMFUNC_P (t1))
1173 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1174 if (TYPE_PTRMEMFUNC_P (t2))
1175 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1177 /* Different classes of types can't be compatible. */
1178 if (TREE_CODE (t1) != TREE_CODE (t2))
1181 /* Qualifiers must match. For array types, we will check when we
1182 recur on the array element types. */
1183 if (TREE_CODE (t1) != ARRAY_TYPE
1184 && cp_type_quals (t1) != cp_type_quals (t2))
1186 if (TREE_CODE (t1) == FUNCTION_TYPE
1187 && type_memfn_quals (t1) != type_memfn_quals (t2))
1189 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1192 /* Allow for two different type nodes which have essentially the same
1193 definition. Note that we already checked for equality of the type
1194 qualifiers (just above). */
1196 if (TREE_CODE (t1) != ARRAY_TYPE
1197 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1201 /* Compare the types. Break out if they could be the same. */
1202 switch (TREE_CODE (t1))
1206 /* All void and bool types are the same. */
1210 case FIXED_POINT_TYPE:
1212 /* With these nodes, we can't determine type equivalence by
1213 looking at what is stored in the nodes themselves, because
1214 two nodes might have different TYPE_MAIN_VARIANTs but still
1215 represent the same type. For example, wchar_t and int could
1216 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1217 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1218 and are distinct types. On the other hand, int and the
1221 typedef int INT __attribute((may_alias));
1223 have identical properties, different TYPE_MAIN_VARIANTs, but
1224 represent the same type. The canonical type system keeps
1225 track of equivalence in this case, so we fall back on it. */
1226 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1228 case TEMPLATE_TEMPLATE_PARM:
1229 case BOUND_TEMPLATE_TEMPLATE_PARM:
1230 if (!comp_template_parms_position (t1, t2))
1232 if (!comp_template_parms
1233 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1234 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1236 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1238 /* Don't check inheritance. */
1239 strict = COMPARE_STRICT;
1244 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1245 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1246 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1247 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1250 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1252 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1258 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1259 strict & ~COMPARE_REDECLARATION))
1261 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1265 case REFERENCE_TYPE:
1266 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1268 /* fall through to checks for pointer types */
1271 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1272 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1273 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1279 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1281 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1286 /* Target types must match incl. qualifiers. */
1287 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1291 case TEMPLATE_TYPE_PARM:
1292 /* If T1 and T2 don't have the same relative position in their
1293 template parameters set, they can't be equal. */
1294 if (!comp_template_parms_position (t1, t2))
1299 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1300 TYPENAME_TYPE_FULLNAME (t2)))
1302 /* Qualifiers don't matter on scopes. */
1303 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1308 case UNBOUND_CLASS_TEMPLATE:
1309 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1311 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1316 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1321 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1322 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1326 case TYPE_PACK_EXPANSION:
1327 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1328 PACK_EXPANSION_PATTERN (t2))
1329 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1330 PACK_EXPANSION_EXTRA_ARGS (t2)));
1333 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1334 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1335 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1336 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1337 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1338 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1339 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1340 DECLTYPE_TYPE_EXPR (t2)))
1344 case UNDERLYING_TYPE:
1345 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1346 UNDERLYING_TYPE_TYPE (t2));
1352 /* If we get here, we know that from a target independent POV the
1353 types are the same. Make sure the target attributes are also
1355 return comp_type_attributes (t1, t2);
1358 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1359 is a bitwise-or of the COMPARE_* flags. */
1362 comptypes (tree t1, tree t2, int strict)
1364 if (strict == COMPARE_STRICT)
1369 if (t1 == error_mark_node || t2 == error_mark_node)
1372 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1373 /* At least one of the types requires structural equality, so
1374 perform a deep check. */
1375 return structural_comptypes (t1, t2, strict);
1377 #ifdef ENABLE_CHECKING
1378 if (USE_CANONICAL_TYPES)
1380 bool result = structural_comptypes (t1, t2, strict);
1382 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1383 /* The two types are structurally equivalent, but their
1384 canonical types were different. This is a failure of the
1385 canonical type propagation code.*/
1387 ("canonical types differ for identical types %T and %T",
1389 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1390 /* Two types are structurally different, but the canonical
1391 types are the same. This means we were over-eager in
1392 assigning canonical types. */
1394 ("same canonical type node for different types %T and %T",
1400 if (USE_CANONICAL_TYPES)
1401 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1404 return structural_comptypes (t1, t2, strict);
1406 else if (strict == COMPARE_STRUCTURAL)
1407 return structural_comptypes (t1, t2, COMPARE_STRICT);
1409 return structural_comptypes (t1, t2, strict);
1412 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1413 top-level qualifiers. */
1416 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1418 if (type1 == error_mark_node || type2 == error_mark_node)
1421 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1424 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1427 at_least_as_qualified_p (const_tree type1, const_tree type2)
1429 int q1 = cp_type_quals (type1);
1430 int q2 = cp_type_quals (type2);
1432 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1433 return (q1 & q2) == q2;
1436 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1437 more cv-qualified that TYPE1, and 0 otherwise. */
1440 comp_cv_qualification (const_tree type1, const_tree type2)
1442 int q1 = cp_type_quals (type1);
1443 int q2 = cp_type_quals (type2);
1448 if ((q1 & q2) == q2)
1450 else if ((q1 & q2) == q1)
1456 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1457 subset of the cv-qualification signature of TYPE2, and the types
1458 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1461 comp_cv_qual_signature (tree type1, tree type2)
1463 if (comp_ptr_ttypes_real (type2, type1, -1))
1465 else if (comp_ptr_ttypes_real (type1, type2, -1))
1471 /* Subroutines of `comptypes'. */
1473 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1474 equivalent in the sense that functions with those parameter types
1475 can have equivalent types. The two lists must be equivalent,
1476 element by element. */
1479 compparms (const_tree parms1, const_tree parms2)
1483 /* An unspecified parmlist matches any specified parmlist
1484 whose argument types don't need default promotions. */
1486 for (t1 = parms1, t2 = parms2;
1488 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1490 /* If one parmlist is shorter than the other,
1491 they fail to match. */
1494 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1501 /* Process a sizeof or alignof expression where the operand is a
1505 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1510 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1511 if (type == error_mark_node)
1512 return error_mark_node;
1514 type = non_reference (type);
1515 if (TREE_CODE (type) == METHOD_TYPE)
1518 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
1519 "invalid application of %qs to a member function",
1520 operator_name_info[(int) op].name);
1521 value = size_one_node;
1524 dependent_p = dependent_type_p (type);
1526 complete_type (type);
1528 /* VLA types will have a non-constant size. In the body of an
1529 uninstantiated template, we don't need to try to compute the
1530 value, because the sizeof expression is not an integral
1531 constant expression in that case. And, if we do try to
1532 compute the value, we'll likely end up with SAVE_EXPRs, which
1533 the template substitution machinery does not expect to see. */
1534 || (processing_template_decl
1535 && COMPLETE_TYPE_P (type)
1536 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1538 value = build_min (op, size_type_node, type);
1539 TREE_READONLY (value) = 1;
1543 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1548 /* Return the size of the type, without producing any warnings for
1549 types whose size cannot be taken. This routine should be used only
1550 in some other routine that has already produced a diagnostic about
1551 using the size of such a type. */
1553 cxx_sizeof_nowarn (tree type)
1555 if (TREE_CODE (type) == FUNCTION_TYPE
1556 || TREE_CODE (type) == VOID_TYPE
1557 || TREE_CODE (type) == ERROR_MARK)
1558 return size_one_node;
1559 else if (!COMPLETE_TYPE_P (type))
1560 return size_zero_node;
1562 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1565 /* Process a sizeof expression where the operand is an expression. */
1568 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1570 if (e == error_mark_node)
1571 return error_mark_node;
1573 if (processing_template_decl)
1575 e = build_min (SIZEOF_EXPR, size_type_node, e);
1576 TREE_SIDE_EFFECTS (e) = 0;
1577 TREE_READONLY (e) = 1;
1582 /* To get the size of a static data member declared as an array of
1583 unknown bound, we need to instantiate it. */
1584 if (TREE_CODE (e) == VAR_DECL
1585 && VAR_HAD_UNKNOWN_BOUND (e)
1586 && DECL_TEMPLATE_INSTANTIATION (e))
1587 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1589 e = mark_type_use (e);
1591 if (TREE_CODE (e) == COMPONENT_REF
1592 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1593 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1595 if (complain & tf_error)
1596 error ("invalid application of %<sizeof%> to a bit-field");
1598 return error_mark_node;
1601 else if (is_overloaded_fn (e))
1603 if (complain & tf_error)
1604 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1607 return error_mark_node;
1610 else if (type_unknown_p (e))
1612 if (complain & tf_error)
1613 cxx_incomplete_type_error (e, TREE_TYPE (e));
1615 return error_mark_node;
1621 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1624 /* Implement the __alignof keyword: Return the minimum required
1625 alignment of E, measured in bytes. For VAR_DECL's and
1626 FIELD_DECL's return DECL_ALIGN (which can be set from an
1627 "aligned" __attribute__ specification). */
1630 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1634 if (e == error_mark_node)
1635 return error_mark_node;
1637 if (processing_template_decl)
1639 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1640 TREE_SIDE_EFFECTS (e) = 0;
1641 TREE_READONLY (e) = 1;
1646 e = mark_type_use (e);
1648 if (TREE_CODE (e) == VAR_DECL)
1649 t = size_int (DECL_ALIGN_UNIT (e));
1650 else if (TREE_CODE (e) == COMPONENT_REF
1651 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1652 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1654 if (complain & tf_error)
1655 error ("invalid application of %<__alignof%> to a bit-field");
1657 return error_mark_node;
1660 else if (TREE_CODE (e) == COMPONENT_REF
1661 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1662 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1663 else if (is_overloaded_fn (e))
1665 if (complain & tf_error)
1666 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1669 return error_mark_node;
1670 if (TREE_CODE (e) == FUNCTION_DECL)
1671 t = size_int (DECL_ALIGN_UNIT (e));
1675 else if (type_unknown_p (e))
1677 if (complain & tf_error)
1678 cxx_incomplete_type_error (e, TREE_TYPE (e));
1680 return error_mark_node;
1684 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1685 complain & tf_error);
1687 return fold_convert (size_type_node, t);
1690 /* Process a sizeof or alignof expression E with code OP where the operand
1691 is an expression. */
1694 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1696 if (op == SIZEOF_EXPR)
1697 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1699 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1702 /* EXPR is being used in a context that is not a function call.
1707 The expression can be used only as the left-hand operand of a
1708 member function call.
1710 [expr.mptr.operator]
1712 If the result of .* or ->* is a function, then that result can be
1713 used only as the operand for the function call operator ().
1715 by issuing an error message if appropriate. Returns true iff EXPR
1716 violates these rules. */
1719 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1721 if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1723 if (complain & tf_error)
1724 error ("invalid use of non-static member function");
1730 /* If EXP is a reference to a bitfield, and the type of EXP does not
1731 match the declared type of the bitfield, return the declared type
1732 of the bitfield. Otherwise, return NULL_TREE. */
1735 is_bitfield_expr_with_lowered_type (const_tree exp)
1737 switch (TREE_CODE (exp))
1740 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1741 ? TREE_OPERAND (exp, 1)
1742 : TREE_OPERAND (exp, 0)))
1744 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1747 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1751 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1757 field = TREE_OPERAND (exp, 1);
1758 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1760 if (same_type_ignoring_top_level_qualifiers_p
1761 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1763 return DECL_BIT_FIELD_TYPE (field);
1767 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1768 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1769 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1777 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1778 bitfield with a lowered type, the type of EXP is returned, rather
1782 unlowered_expr_type (const_tree exp)
1785 tree etype = TREE_TYPE (exp);
1787 type = is_bitfield_expr_with_lowered_type (exp);
1789 type = cp_build_qualified_type (type, cp_type_quals (etype));
1796 /* Perform the conversions in [expr] that apply when an lvalue appears
1797 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1798 function-to-pointer conversions. In addition, manifest constants
1799 are replaced by their values, and bitfield references are converted
1800 to their declared types. Note that this function does not perform the
1801 lvalue-to-rvalue conversion for class types. If you need that conversion
1802 to for class types, then you probably need to use force_rvalue.
1804 Although the returned value is being used as an rvalue, this
1805 function does not wrap the returned expression in a
1806 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1807 that the return value is no longer an lvalue. */
1810 decay_conversion (tree exp)
1813 enum tree_code code;
1815 type = TREE_TYPE (exp);
1816 if (type == error_mark_node)
1817 return error_mark_node;
1819 exp = mark_rvalue_use (exp);
1821 exp = resolve_nondeduced_context (exp);
1822 if (type_unknown_p (exp))
1824 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1825 return error_mark_node;
1828 /* FIXME remove? at least need to remember that this isn't really a
1829 constant expression if EXP isn't decl_constant_var_p, like with
1830 C_MAYBE_CONST_EXPR. */
1831 exp = decl_constant_value_safe (exp);
1832 if (error_operand_p (exp))
1833 return error_mark_node;
1835 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1836 return nullptr_node;
1838 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1839 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1840 code = TREE_CODE (type);
1841 if (code == VOID_TYPE)
1843 error ("void value not ignored as it ought to be");
1844 return error_mark_node;
1846 if (invalid_nonstatic_memfn_p (exp, tf_warning_or_error))
1847 return error_mark_node;
1848 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1849 return cp_build_addr_expr (exp, tf_warning_or_error);
1850 if (code == ARRAY_TYPE)
1855 if (TREE_CODE (exp) == INDIRECT_REF)
1856 return build_nop (build_pointer_type (TREE_TYPE (type)),
1857 TREE_OPERAND (exp, 0));
1859 if (TREE_CODE (exp) == COMPOUND_EXPR)
1861 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1862 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1863 TREE_OPERAND (exp, 0), op1);
1867 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1869 error ("invalid use of non-lvalue array");
1870 return error_mark_node;
1873 /* Don't let an array compound literal decay to a pointer. It can
1874 still be used to initialize an array or bind to a reference. */
1875 if (TREE_CODE (exp) == TARGET_EXPR)
1877 error ("taking address of temporary array");
1878 return error_mark_node;
1881 ptrtype = build_pointer_type (TREE_TYPE (type));
1883 if (TREE_CODE (exp) == VAR_DECL)
1885 if (!cxx_mark_addressable (exp))
1886 return error_mark_node;
1887 adr = build_nop (ptrtype, build_address (exp));
1890 /* This way is better for a COMPONENT_REF since it can
1891 simplify the offset for a component. */
1892 adr = cp_build_addr_expr (exp, tf_warning_or_error);
1893 return cp_convert (ptrtype, adr);
1896 /* If a bitfield is used in a context where integral promotion
1897 applies, then the caller is expected to have used
1898 default_conversion. That function promotes bitfields correctly
1899 before calling this function. At this point, if we have a
1900 bitfield referenced, we may assume that is not subject to
1901 promotion, and that, therefore, the type of the resulting rvalue
1902 is the declared type of the bitfield. */
1903 exp = convert_bitfield_to_declared_type (exp);
1905 /* We do not call rvalue() here because we do not want to wrap EXP
1906 in a NON_LVALUE_EXPR. */
1910 Non-class rvalues always have cv-unqualified types. */
1911 type = TREE_TYPE (exp);
1912 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1913 exp = build_nop (cv_unqualified (type), exp);
1918 /* Perform preparatory conversions, as part of the "usual arithmetic
1919 conversions". In particular, as per [expr]:
1921 Whenever an lvalue expression appears as an operand of an
1922 operator that expects the rvalue for that operand, the
1923 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1924 standard conversions are applied to convert the expression to an
1927 In addition, we perform integral promotions here, as those are
1928 applied to both operands to a binary operator before determining
1929 what additional conversions should apply. */
1932 default_conversion (tree exp)
1934 /* Check for target-specific promotions. */
1935 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1937 exp = cp_convert (promoted_type, exp);
1938 /* Perform the integral promotions first so that bitfield
1939 expressions (which may promote to "int", even if the bitfield is
1940 declared "unsigned") are promoted correctly. */
1941 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1942 exp = perform_integral_promotions (exp);
1943 /* Perform the other conversions. */
1944 exp = decay_conversion (exp);
1949 /* EXPR is an expression with an integral or enumeration type.
1950 Perform the integral promotions in [conv.prom], and return the
1954 perform_integral_promotions (tree expr)
1959 expr = mark_rvalue_use (expr);
1963 If the bitfield has an enumerated type, it is treated as any
1964 other value of that type for promotion purposes. */
1965 type = is_bitfield_expr_with_lowered_type (expr);
1966 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1967 type = TREE_TYPE (expr);
1968 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1969 /* Scoped enums don't promote. */
1970 if (SCOPED_ENUM_P (type))
1972 promoted_type = type_promotes_to (type);
1973 if (type != promoted_type)
1974 expr = cp_convert (promoted_type, expr);
1978 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1979 decay_conversion to one. */
1982 string_conv_p (const_tree totype, const_tree exp, int warn)
1986 if (TREE_CODE (totype) != POINTER_TYPE)
1989 t = TREE_TYPE (totype);
1990 if (!same_type_p (t, char_type_node)
1991 && !same_type_p (t, char16_type_node)
1992 && !same_type_p (t, char32_type_node)
1993 && !same_type_p (t, wchar_type_node))
1996 if (TREE_CODE (exp) == STRING_CST)
1998 /* Make sure that we don't try to convert between char and wide chars. */
1999 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2004 /* Is this a string constant which has decayed to 'const char *'? */
2005 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2006 if (!same_type_p (TREE_TYPE (exp), t))
2009 if (TREE_CODE (exp) != ADDR_EXPR
2010 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2014 /* This warning is not very useful, as it complains about printf. */
2016 warning (OPT_Wwrite_strings,
2017 "deprecated conversion from string constant to %qT",
2023 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2024 can, for example, use as an lvalue. This code used to be in
2025 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2026 expressions, where we're dealing with aggregates. But now it's again only
2027 called from unary_complex_lvalue. The case (in particular) that led to
2028 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2032 rationalize_conditional_expr (enum tree_code code, tree t,
2033 tsubst_flags_t complain)
2035 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2036 the first operand is always the one to be used if both operands
2037 are equal, so we know what conditional expression this used to be. */
2038 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2040 tree op0 = TREE_OPERAND (t, 0);
2041 tree op1 = TREE_OPERAND (t, 1);
2043 /* The following code is incorrect if either operand side-effects. */
2044 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2045 && !TREE_SIDE_EFFECTS (op1));
2047 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
2048 ? LE_EXPR : GE_EXPR),
2049 op0, TREE_CODE (op0),
2050 op1, TREE_CODE (op1),
2053 cp_build_unary_op (code, op0, 0, complain),
2054 cp_build_unary_op (code, op1, 0, complain),
2059 build_conditional_expr (TREE_OPERAND (t, 0),
2060 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2062 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2067 /* Given the TYPE of an anonymous union field inside T, return the
2068 FIELD_DECL for the field. If not found return NULL_TREE. Because
2069 anonymous unions can nest, we must also search all anonymous unions
2070 that are directly reachable. */
2073 lookup_anon_field (tree t, tree type)
2077 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2079 if (TREE_STATIC (field))
2081 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2084 /* If we find it directly, return the field. */
2085 if (DECL_NAME (field) == NULL_TREE
2086 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2091 /* Otherwise, it could be nested, search harder. */
2092 if (DECL_NAME (field) == NULL_TREE
2093 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2095 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2103 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2104 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2105 non-NULL, it indicates the path to the base used to name MEMBER.
2106 If PRESERVE_REFERENCE is true, the expression returned will have
2107 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2108 returned will have the type referred to by the reference.
2110 This function does not perform access control; that is either done
2111 earlier by the parser when the name of MEMBER is resolved to MEMBER
2112 itself, or later when overload resolution selects one of the
2113 functions indicated by MEMBER. */
2116 build_class_member_access_expr (tree object, tree member,
2117 tree access_path, bool preserve_reference,
2118 tsubst_flags_t complain)
2122 tree result = NULL_TREE;
2123 tree using_decl = NULL_TREE;
2125 if (error_operand_p (object) || error_operand_p (member))
2126 return error_mark_node;
2128 gcc_assert (DECL_P (member) || BASELINK_P (member));
2132 The type of the first expression shall be "class object" (of a
2134 object_type = TREE_TYPE (object);
2135 if (!currently_open_class (object_type)
2136 && !complete_type_or_maybe_complain (object_type, object, complain))
2137 return error_mark_node;
2138 if (!CLASS_TYPE_P (object_type))
2140 if (complain & tf_error)
2142 if (POINTER_TYPE_P (object_type)
2143 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2144 error ("request for member %qD in %qE, which is of pointer "
2145 "type %qT (maybe you meant to use %<->%> ?)",
2146 member, object, object_type);
2148 error ("request for member %qD in %qE, which is of non-class "
2149 "type %qT", member, object, object_type);
2151 return error_mark_node;
2154 /* The standard does not seem to actually say that MEMBER must be a
2155 member of OBJECT_TYPE. However, that is clearly what is
2157 if (DECL_P (member))
2159 member_scope = DECL_CLASS_CONTEXT (member);
2161 if (TREE_DEPRECATED (member))
2162 warn_deprecated_use (member, NULL_TREE);
2165 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2166 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2167 presently be the anonymous union. Go outwards until we find a
2168 type related to OBJECT_TYPE. */
2169 while (ANON_AGGR_TYPE_P (member_scope)
2170 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2172 member_scope = TYPE_CONTEXT (member_scope);
2173 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2175 if (complain & tf_error)
2177 if (TREE_CODE (member) == FIELD_DECL)
2178 error ("invalid use of nonstatic data member %qE", member);
2180 error ("%qD is not a member of %qT", member, object_type);
2182 return error_mark_node;
2185 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2186 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2187 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2189 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2191 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2194 /* In [expr.ref], there is an explicit list of the valid choices for
2195 MEMBER. We check for each of those cases here. */
2196 if (TREE_CODE (member) == VAR_DECL)
2198 /* A static data member. */
2200 mark_exp_read (object);
2201 /* If OBJECT has side-effects, they are supposed to occur. */
2202 if (TREE_SIDE_EFFECTS (object))
2203 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2205 else if (TREE_CODE (member) == FIELD_DECL)
2207 /* A non-static data member. */
2212 null_object_p = (TREE_CODE (object) == INDIRECT_REF
2213 && integer_zerop (TREE_OPERAND (object, 0)));
2215 /* Convert OBJECT to the type of MEMBER. */
2216 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2217 TYPE_MAIN_VARIANT (member_scope)))
2222 binfo = lookup_base (access_path ? access_path : object_type,
2223 member_scope, ba_unique, &kind);
2224 if (binfo == error_mark_node)
2225 return error_mark_node;
2227 /* It is invalid to try to get to a virtual base of a
2228 NULL object. The most common cause is invalid use of
2230 if (null_object_p && kind == bk_via_virtual)
2232 if (complain & tf_error)
2234 error ("invalid access to non-static data member %qD of "
2237 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2239 return error_mark_node;
2242 /* Convert to the base. */
2243 object = build_base_path (PLUS_EXPR, object, binfo,
2244 /*nonnull=*/1, complain);
2245 /* If we found the base successfully then we should be able
2246 to convert to it successfully. */
2247 gcc_assert (object != error_mark_node);
2250 /* Complain about other invalid uses of offsetof, even though they will
2251 give the right answer. Note that we complain whether or not they
2252 actually used the offsetof macro, since there's no way to know at this
2253 point. So we just give a warning, instead of a pedwarn. */
2254 /* Do not produce this warning for base class field references, because
2255 we know for a fact that didn't come from offsetof. This does occur
2256 in various testsuite cases where a null object is passed where a
2257 vtable access is required. */
2258 if (null_object_p && warn_invalid_offsetof
2259 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2260 && !DECL_FIELD_IS_BASE (member)
2261 && cp_unevaluated_operand == 0
2262 && (complain & tf_warning))
2264 warning (OPT_Winvalid_offsetof,
2265 "invalid access to non-static data member %qD "
2266 " of NULL object", member);
2267 warning (OPT_Winvalid_offsetof,
2268 "(perhaps the %<offsetof%> macro was used incorrectly)");
2271 /* If MEMBER is from an anonymous aggregate, we have converted
2272 OBJECT so that it refers to the class containing the
2273 anonymous union. Generate a reference to the anonymous union
2274 itself, and recur to find MEMBER. */
2275 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2276 /* When this code is called from build_field_call, the
2277 object already has the type of the anonymous union.
2278 That is because the COMPONENT_REF was already
2279 constructed, and was then disassembled before calling
2280 build_field_call. After the function-call code is
2281 cleaned up, this waste can be eliminated. */
2282 && (!same_type_ignoring_top_level_qualifiers_p
2283 (TREE_TYPE (object), DECL_CONTEXT (member))))
2285 tree anonymous_union;
2287 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2288 DECL_CONTEXT (member));
2289 object = build_class_member_access_expr (object,
2291 /*access_path=*/NULL_TREE,
2296 /* Compute the type of the field, as described in [expr.ref]. */
2297 type_quals = TYPE_UNQUALIFIED;
2298 member_type = TREE_TYPE (member);
2299 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2301 type_quals = (cp_type_quals (member_type)
2302 | cp_type_quals (object_type));
2304 /* A field is const (volatile) if the enclosing object, or the
2305 field itself, is const (volatile). But, a mutable field is
2306 not const, even within a const object. */
2307 if (DECL_MUTABLE_P (member))
2308 type_quals &= ~TYPE_QUAL_CONST;
2309 member_type = cp_build_qualified_type (member_type, type_quals);
2312 result = build3 (COMPONENT_REF, member_type, object, member,
2314 result = fold_if_not_in_template (result);
2316 /* Mark the expression const or volatile, as appropriate. Even
2317 though we've dealt with the type above, we still have to mark the
2318 expression itself. */
2319 if (type_quals & TYPE_QUAL_CONST)
2320 TREE_READONLY (result) = 1;
2321 if (type_quals & TYPE_QUAL_VOLATILE)
2322 TREE_THIS_VOLATILE (result) = 1;
2324 else if (BASELINK_P (member))
2326 /* The member is a (possibly overloaded) member function. */
2330 /* If the MEMBER is exactly one static member function, then we
2331 know the type of the expression. Otherwise, we must wait
2332 until overload resolution has been performed. */
2333 functions = BASELINK_FUNCTIONS (member);
2334 if (TREE_CODE (functions) == FUNCTION_DECL
2335 && DECL_STATIC_FUNCTION_P (functions))
2336 type = TREE_TYPE (functions);
2338 type = unknown_type_node;
2339 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2340 base. That will happen when the function is called. */
2341 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2343 else if (TREE_CODE (member) == CONST_DECL)
2345 /* The member is an enumerator. */
2347 /* If OBJECT has side-effects, they are supposed to occur. */
2348 if (TREE_SIDE_EFFECTS (object))
2349 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2352 else if ((using_decl = strip_using_decl (member)) != member)
2353 result = build_class_member_access_expr (object,
2355 access_path, preserve_reference,
2359 if (complain & tf_error)
2360 error ("invalid use of %qD", member);
2361 return error_mark_node;
2364 if (!preserve_reference)
2367 If E2 is declared to have type "reference to T", then ... the
2368 type of E1.E2 is T. */
2369 result = convert_from_reference (result);
2374 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2375 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2378 lookup_destructor (tree object, tree scope, tree dtor_name)
2380 tree object_type = TREE_TYPE (object);
2381 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2384 if (scope && !check_dtor_name (scope, dtor_type))
2386 error ("qualified type %qT does not match destructor name ~%qT",
2388 return error_mark_node;
2390 if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2392 /* In a template, names we can't find a match for are still accepted
2393 destructor names, and we check them here. */
2394 if (check_dtor_name (object_type, dtor_type))
2395 dtor_type = object_type;
2398 error ("object type %qT does not match destructor name ~%qT",
2399 object_type, dtor_type);
2400 return error_mark_node;
2404 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2406 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2407 TYPE_MAIN_VARIANT (object_type), dtor_type);
2408 return error_mark_node;
2410 expr = lookup_member (dtor_type, complete_dtor_identifier,
2411 /*protect=*/1, /*want_type=*/false,
2412 tf_warning_or_error);
2413 expr = (adjust_result_of_qualified_name_lookup
2414 (expr, dtor_type, object_type));
2415 if (scope == NULL_TREE)
2416 /* We need to call adjust_result_of_qualified_name_lookup in case the
2417 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2418 that we still get virtual function binding. */
2419 BASELINK_QUALIFIED_P (expr) = false;
2423 /* An expression of the form "A::template B" has been resolved to
2424 DECL. Issue a diagnostic if B is not a template or template
2428 check_template_keyword (tree decl)
2430 /* The standard says:
2434 If a name prefixed by the keyword template is not a member
2435 template, the program is ill-formed.
2437 DR 228 removed the restriction that the template be a member
2440 DR 96, if accepted would add the further restriction that explicit
2441 template arguments must be provided if the template keyword is
2442 used, but, as of 2005-10-16, that DR is still in "drafting". If
2443 this DR is accepted, then the semantic checks here can be
2444 simplified, as the entity named must in fact be a template
2445 specialization, rather than, as at present, a set of overloaded
2446 functions containing at least one template function. */
2447 if (TREE_CODE (decl) != TEMPLATE_DECL
2448 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2450 if (!is_overloaded_fn (decl))
2451 permerror (input_location, "%qD is not a template", decl);
2456 if (BASELINK_P (fns))
2457 fns = BASELINK_FUNCTIONS (fns);
2460 tree fn = OVL_CURRENT (fns);
2461 if (TREE_CODE (fn) == TEMPLATE_DECL
2462 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2464 if (TREE_CODE (fn) == FUNCTION_DECL
2465 && DECL_USE_TEMPLATE (fn)
2466 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2468 fns = OVL_NEXT (fns);
2471 permerror (input_location, "%qD is not a template", decl);
2476 /* This function is called by the parser to process a class member
2477 access expression of the form OBJECT.NAME. NAME is a node used by
2478 the parser to represent a name; it is not yet a DECL. It may,
2479 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2480 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2481 there is no reason to do the lookup twice, so the parser keeps the
2482 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2483 be a template via the use of the "A::template B" syntax. */
2486 finish_class_member_access_expr (tree object, tree name, bool template_p,
2487 tsubst_flags_t complain)
2492 tree access_path = NULL_TREE;
2493 tree orig_object = object;
2494 tree orig_name = name;
2496 if (object == error_mark_node || name == error_mark_node)
2497 return error_mark_node;
2499 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2500 if (!objc_is_public (object, name))
2501 return error_mark_node;
2503 object_type = TREE_TYPE (object);
2505 if (processing_template_decl)
2507 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2508 dependent_type_p (object_type)
2509 /* If NAME is just an IDENTIFIER_NODE, then the expression
2511 || TREE_CODE (object) == IDENTIFIER_NODE
2512 /* If NAME is "f<args>", where either 'f' or 'args' is
2513 dependent, then the expression is dependent. */
2514 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2515 && dependent_template_id_p (TREE_OPERAND (name, 0),
2516 TREE_OPERAND (name, 1)))
2517 /* If NAME is "T::X" where "T" is dependent, then the
2518 expression is dependent. */
2519 || (TREE_CODE (name) == SCOPE_REF
2520 && TYPE_P (TREE_OPERAND (name, 0))
2521 && dependent_type_p (TREE_OPERAND (name, 0))))
2522 return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2523 object = build_non_dependent_expr (object);
2525 else if (c_dialect_objc ()
2526 && TREE_CODE (name) == IDENTIFIER_NODE
2527 && (expr = objc_maybe_build_component_ref (object, name)))
2532 The type of the first expression shall be "class object" (of a
2534 if (!currently_open_class (object_type)
2535 && !complete_type_or_maybe_complain (object_type, object, complain))
2536 return error_mark_node;
2537 if (!CLASS_TYPE_P (object_type))
2539 if (complain & tf_error)
2541 if (POINTER_TYPE_P (object_type)
2542 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2543 error ("request for member %qD in %qE, which is of pointer "
2544 "type %qT (maybe you meant to use %<->%> ?)",
2545 name, object, object_type);
2547 error ("request for member %qD in %qE, which is of non-class "
2548 "type %qT", name, object, object_type);
2550 return error_mark_node;
2553 if (BASELINK_P (name))
2554 /* A member function that has already been looked up. */
2558 bool is_template_id = false;
2559 tree template_args = NULL_TREE;
2562 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2564 is_template_id = true;
2565 template_args = TREE_OPERAND (name, 1);
2566 name = TREE_OPERAND (name, 0);
2568 if (TREE_CODE (name) == OVERLOAD)
2569 name = DECL_NAME (get_first_fn (name));
2570 else if (DECL_P (name))
2571 name = DECL_NAME (name);
2574 if (TREE_CODE (name) == SCOPE_REF)
2576 /* A qualified name. The qualifying class or namespace `S'
2577 has already been looked up; it is either a TYPE or a
2579 scope = TREE_OPERAND (name, 0);
2580 name = TREE_OPERAND (name, 1);
2582 /* If SCOPE is a namespace, then the qualified name does not
2583 name a member of OBJECT_TYPE. */
2584 if (TREE_CODE (scope) == NAMESPACE_DECL)
2586 if (complain & tf_error)
2587 error ("%<%D::%D%> is not a member of %qT",
2588 scope, name, object_type);
2589 return error_mark_node;
2592 gcc_assert (CLASS_TYPE_P (scope));
2593 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2594 || TREE_CODE (name) == BIT_NOT_EXPR);
2596 if (constructor_name_p (name, scope))
2598 if (complain & tf_error)
2599 error ("cannot call constructor %<%T::%D%> directly",
2601 return error_mark_node;
2604 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2605 access_path = lookup_base (object_type, scope, ba_check, NULL);
2606 if (access_path == error_mark_node)
2607 return error_mark_node;
2610 if (complain & tf_error)
2611 error ("%qT is not a base of %qT", scope, object_type);
2612 return error_mark_node;
2618 access_path = object_type;
2621 if (TREE_CODE (name) == BIT_NOT_EXPR)
2622 member = lookup_destructor (object, scope, name);
2625 /* Look up the member. */
2626 member = lookup_member (access_path, name, /*protect=*/1,
2627 /*want_type=*/false, complain);
2628 if (member == NULL_TREE)
2630 if (complain & tf_error)
2631 error ("%qD has no member named %qE",
2632 TREE_CODE (access_path) == TREE_BINFO
2633 ? TREE_TYPE (access_path) : object_type, name);
2634 return error_mark_node;
2636 if (member == error_mark_node)
2637 return error_mark_node;
2642 tree templ = member;
2644 if (BASELINK_P (templ))
2645 templ = lookup_template_function (templ, template_args);
2648 if (complain & tf_error)
2649 error ("%qD is not a member template function", name);
2650 return error_mark_node;
2655 if (TREE_DEPRECATED (member))
2656 warn_deprecated_use (member, NULL_TREE);
2659 check_template_keyword (member);
2661 expr = build_class_member_access_expr (object, member, access_path,
2662 /*preserve_reference=*/false,
2664 if (processing_template_decl && expr != error_mark_node)
2666 if (BASELINK_P (member))
2668 if (TREE_CODE (orig_name) == SCOPE_REF)
2669 BASELINK_QUALIFIED_P (member) = 1;
2672 return build_min_non_dep (COMPONENT_REF, expr,
2673 orig_object, orig_name,
2680 /* Return an expression for the MEMBER_NAME field in the internal
2681 representation of PTRMEM, a pointer-to-member function. (Each
2682 pointer-to-member function type gets its own RECORD_TYPE so it is
2683 more convenient to access the fields by name than by FIELD_DECL.)
2684 This routine converts the NAME to a FIELD_DECL and then creates the
2685 node for the complete expression. */
2688 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2694 /* This code is a stripped down version of
2695 build_class_member_access_expr. It does not work to use that
2696 routine directly because it expects the object to be of class
2698 ptrmem_type = TREE_TYPE (ptrmem);
2699 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2700 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2701 /*want_type=*/false, tf_warning_or_error);
2702 member_type = cp_build_qualified_type (TREE_TYPE (member),
2703 cp_type_quals (ptrmem_type));
2704 return fold_build3_loc (input_location,
2705 COMPONENT_REF, member_type,
2706 ptrmem, member, NULL_TREE);
2709 /* Given an expression PTR for a pointer, return an expression
2710 for the value pointed to.
2711 ERRORSTRING is the name of the operator to appear in error messages.
2713 This function may need to overload OPERATOR_FNNAME.
2714 Must also handle REFERENCE_TYPEs for C++. */
2717 build_x_indirect_ref (tree expr, ref_operator errorstring,
2718 tsubst_flags_t complain)
2720 tree orig_expr = expr;
2723 if (processing_template_decl)
2725 /* Retain the type if we know the operand is a pointer. */
2726 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2727 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2728 if (type_dependent_expression_p (expr))
2729 return build_min_nt (INDIRECT_REF, expr);
2730 expr = build_non_dependent_expr (expr);
2733 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2734 NULL_TREE, /*overload=*/NULL, complain);
2736 rval = cp_build_indirect_ref (expr, errorstring, complain);
2738 if (processing_template_decl && rval != error_mark_node)
2739 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2744 /* Helper function called from c-common. */
2746 build_indirect_ref (location_t loc ATTRIBUTE_UNUSED,
2747 tree ptr, ref_operator errorstring)
2749 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2753 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2754 tsubst_flags_t complain)
2758 if (ptr == error_mark_node)
2759 return error_mark_node;
2761 if (ptr == current_class_ptr)
2762 return current_class_ref;
2764 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2765 ? ptr : decay_conversion (ptr));
2766 type = TREE_TYPE (pointer);
2768 if (POINTER_TYPE_P (type))
2772 If the type of the expression is "pointer to T," the type
2773 of the result is "T." */
2774 tree t = TREE_TYPE (type);
2776 if (CONVERT_EXPR_P (ptr)
2777 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2779 /* If a warning is issued, mark it to avoid duplicates from
2780 the backend. This only needs to be done at
2781 warn_strict_aliasing > 2. */
2782 if (warn_strict_aliasing > 2)
2783 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2784 type, TREE_OPERAND (ptr, 0)))
2785 TREE_NO_WARNING (ptr) = 1;
2788 if (VOID_TYPE_P (t))
2790 /* A pointer to incomplete type (other than cv void) can be
2791 dereferenced [expr.unary.op]/1 */
2792 if (complain & tf_error)
2793 error ("%qT is not a pointer-to-object type", type);
2794 return error_mark_node;
2796 else if (TREE_CODE (pointer) == ADDR_EXPR
2797 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2798 /* The POINTER was something like `&x'. We simplify `*&x' to
2800 return TREE_OPERAND (pointer, 0);
2803 tree ref = build1 (INDIRECT_REF, t, pointer);
2805 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2806 so that we get the proper error message if the result is used
2807 to assign to. Also, &* is supposed to be a no-op. */
2808 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2809 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2810 TREE_SIDE_EFFECTS (ref)
2811 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2815 else if (!(complain & tf_error))
2816 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2818 /* `pointer' won't be an error_mark_node if we were given a
2819 pointer to member, so it's cool to check for this here. */
2820 else if (TYPE_PTR_TO_MEMBER_P (type))
2821 switch (errorstring)
2823 case RO_ARRAY_INDEXING:
2824 error ("invalid use of array indexing on pointer to member");
2827 error ("invalid use of unary %<*%> on pointer to member");
2829 case RO_IMPLICIT_CONVERSION:
2830 error ("invalid use of implicit conversion on pointer to member");
2835 else if (pointer != error_mark_node)
2836 invalid_indirection_error (input_location, type, errorstring);
2838 return error_mark_node;
2841 /* This handles expressions of the form "a[i]", which denotes
2844 This is logically equivalent in C to *(a+i), but we may do it differently.
2845 If A is a variable or a member, we generate a primitive ARRAY_REF.
2846 This avoids forcing the array out of registers, and can work on
2847 arrays that are not lvalues (for example, members of structures returned
2850 If INDEX is of some user-defined type, it must be converted to
2851 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2852 will inherit the type of the array, which will be some pointer type.
2854 LOC is the location to use in building the array reference. */
2857 cp_build_array_ref (location_t loc, tree array, tree idx,
2858 tsubst_flags_t complain)
2864 if (complain & tf_error)
2865 error_at (loc, "subscript missing in array reference");
2866 return error_mark_node;
2869 if (TREE_TYPE (array) == error_mark_node
2870 || TREE_TYPE (idx) == error_mark_node)
2871 return error_mark_node;
2873 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2875 switch (TREE_CODE (array))
2879 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2881 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2882 TREE_OPERAND (array, 0), value);
2883 SET_EXPR_LOCATION (ret, loc);
2888 ret = build_conditional_expr
2889 (TREE_OPERAND (array, 0),
2890 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2892 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2894 tf_warning_or_error);
2895 protected_set_expr_location (ret, loc);
2902 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2906 warn_array_subscript_with_type_char (idx);
2908 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2910 if (complain & tf_error)
2911 error_at (loc, "array subscript is not an integer");
2912 return error_mark_node;
2915 /* Apply integral promotions *after* noticing character types.
2916 (It is unclear why we do these promotions -- the standard
2917 does not say that we should. In fact, the natural thing would
2918 seem to be to convert IDX to ptrdiff_t; we're performing
2919 pointer arithmetic.) */
2920 idx = perform_integral_promotions (idx);
2922 /* An array that is indexed by a non-constant
2923 cannot be stored in a register; we must be able to do
2924 address arithmetic on its address.
2925 Likewise an array of elements of variable size. */
2926 if (TREE_CODE (idx) != INTEGER_CST
2927 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2928 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2931 if (!cxx_mark_addressable (array))
2932 return error_mark_node;
2935 /* An array that is indexed by a constant value which is not within
2936 the array bounds cannot be stored in a register either; because we
2937 would get a crash in store_bit_field/extract_bit_field when trying
2938 to access a non-existent part of the register. */
2939 if (TREE_CODE (idx) == INTEGER_CST
2940 && TYPE_DOMAIN (TREE_TYPE (array))
2941 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2943 if (!cxx_mark_addressable (array))
2944 return error_mark_node;
2947 if (!lvalue_p (array) && (complain & tf_error))
2948 pedwarn (loc, OPT_pedantic,
2949 "ISO C++ forbids subscripting non-lvalue array");
2951 /* Note in C++ it is valid to subscript a `register' array, since
2952 it is valid to take the address of something with that
2953 storage specification. */
2957 while (TREE_CODE (foo) == COMPONENT_REF)
2958 foo = TREE_OPERAND (foo, 0);
2959 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
2960 && (complain & tf_warning))
2961 warning_at (loc, OPT_Wextra,
2962 "subscripting array declared %<register%>");
2965 type = TREE_TYPE (TREE_TYPE (array));
2966 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2967 /* Array ref is const/volatile if the array elements are
2968 or if the array is.. */
2969 TREE_READONLY (rval)
2970 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2971 TREE_SIDE_EFFECTS (rval)
2972 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2973 TREE_THIS_VOLATILE (rval)
2974 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2975 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
2977 protected_set_expr_location (ret, loc);
2982 tree ar = default_conversion (array);
2983 tree ind = default_conversion (idx);
2985 /* Put the integer in IND to simplify error checking. */
2986 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2993 if (ar == error_mark_node)
2996 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2998 if (complain & tf_error)
2999 error_at (loc, "subscripted value is neither array nor pointer");
3000 return error_mark_node;
3002 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3004 if (complain & tf_error)
3005 error_at (loc, "array subscript is not an integer");
3006 return error_mark_node;
3009 warn_array_subscript_with_type_char (idx);
3011 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3016 protected_set_expr_location (ret, loc);
3021 /* Entry point for Obj-C++. */
3024 build_array_ref (location_t loc, tree array, tree idx)
3026 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3029 /* Resolve a pointer to member function. INSTANCE is the object
3030 instance to use, if the member points to a virtual member.
3032 This used to avoid checking for virtual functions if basetype
3033 has no virtual functions, according to an earlier ANSI draft.
3034 With the final ISO C++ rules, such an optimization is
3035 incorrect: A pointer to a derived member can be static_cast
3036 to pointer-to-base-member, as long as the dynamic object
3037 later has the right member. */
3040 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
3042 if (TREE_CODE (function) == OFFSET_REF)
3043 function = TREE_OPERAND (function, 1);
3045 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3047 tree idx, delta, e1, e2, e3, vtbl, basetype;
3048 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3050 tree instance_ptr = *instance_ptrptr;
3051 tree instance_save_expr = 0;
3052 if (instance_ptr == error_mark_node)
3054 if (TREE_CODE (function) == PTRMEM_CST)
3056 /* Extracting the function address from a pmf is only
3057 allowed with -Wno-pmf-conversions. It only works for
3059 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
3060 e1 = convert (fntype, e1);
3065 error ("object missing in use of %qE", function);
3066 return error_mark_node;
3070 if (TREE_SIDE_EFFECTS (instance_ptr))
3071 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3073 if (TREE_SIDE_EFFECTS (function))
3074 function = save_expr (function);
3076 /* Start by extracting all the information from the PMF itself. */
3077 e3 = pfn_from_ptrmemfunc (function);
3078 delta = delta_from_ptrmemfunc (function);
3079 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3080 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3082 case ptrmemfunc_vbit_in_pfn:
3083 e1 = cp_build_binary_op (input_location,
3084 BIT_AND_EXPR, idx, integer_one_node,
3085 tf_warning_or_error);
3086 idx = cp_build_binary_op (input_location,
3087 MINUS_EXPR, idx, integer_one_node,
3088 tf_warning_or_error);
3091 case ptrmemfunc_vbit_in_delta:
3092 e1 = cp_build_binary_op (input_location,
3093 BIT_AND_EXPR, delta, integer_one_node,
3094 tf_warning_or_error);
3095 delta = cp_build_binary_op (input_location,
3096 RSHIFT_EXPR, delta, integer_one_node,
3097 tf_warning_or_error);
3104 /* Convert down to the right base before using the instance. A
3105 special case is that in a pointer to member of class C, C may
3106 be incomplete. In that case, the function will of course be
3107 a member of C, and no conversion is required. In fact,
3108 lookup_base will fail in that case, because incomplete
3109 classes do not have BINFOs. */
3110 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3111 if (!same_type_ignoring_top_level_qualifiers_p
3112 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3114 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3115 basetype, ba_check, NULL);
3116 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3117 1, tf_warning_or_error);
3118 if (instance_ptr == error_mark_node)
3119 return error_mark_node;
3121 /* ...and then the delta in the PMF. */
3122 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3124 /* Hand back the adjusted 'this' argument to our caller. */
3125 *instance_ptrptr = instance_ptr;
3127 /* Next extract the vtable pointer from the object. */
3128 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3130 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, tf_warning_or_error);
3131 /* If the object is not dynamic the access invokes undefined
3132 behavior. As it is not executed in this case silence the
3133 spurious warnings it may provoke. */
3134 TREE_NO_WARNING (vtbl) = 1;
3136 /* Finally, extract the function pointer from the vtable. */
3137 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3138 e2 = cp_build_indirect_ref (e2, RO_NULL, tf_warning_or_error);
3139 TREE_CONSTANT (e2) = 1;
3141 /* When using function descriptors, the address of the
3142 vtable entry is treated as a function pointer. */
3143 if (TARGET_VTABLE_USES_DESCRIPTORS)
3144 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3145 cp_build_addr_expr (e2, tf_warning_or_error));
3147 e2 = fold_convert (TREE_TYPE (e3), e2);
3148 e1 = build_conditional_expr (e1, e2, e3, tf_warning_or_error);
3150 /* Make sure this doesn't get evaluated first inside one of the
3151 branches of the COND_EXPR. */
3152 if (instance_save_expr)
3153 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3154 instance_save_expr, e1);
3161 /* Used by the C-common bits. */
3163 build_function_call (location_t loc ATTRIBUTE_UNUSED,
3164 tree function, tree params)
3166 return cp_build_function_call (function, params, tf_warning_or_error);
3169 /* Used by the C-common bits. */
3171 build_function_call_vec (location_t loc ATTRIBUTE_UNUSED,
3172 tree function, VEC(tree,gc) *params,
3173 VEC(tree,gc) *origtypes ATTRIBUTE_UNUSED)
3175 VEC(tree,gc) *orig_params = params;
3176 tree ret = cp_build_function_call_vec (function, ¶ms,
3177 tf_warning_or_error);
3179 /* cp_build_function_call_vec can reallocate PARAMS by adding
3180 default arguments. That should never happen here. Verify
3182 gcc_assert (params == orig_params);
3187 /* Build a function call using a tree list of arguments. */
3190 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3195 vec = make_tree_vector ();
3196 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3197 VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3198 ret = cp_build_function_call_vec (function, &vec, complain);
3199 release_tree_vector (vec);
3203 /* Build a function call using varargs. */
3206 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3212 vec = make_tree_vector ();
3213 va_start (args, complain);
3214 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3215 VEC_safe_push (tree, gc, vec, t);
3217 ret = cp_build_function_call_vec (function, &vec, complain);
3218 release_tree_vector (vec);
3222 /* Build a function call using a vector of arguments. PARAMS may be
3223 NULL if there are no parameters. This changes the contents of
3227 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3228 tsubst_flags_t complain)
3230 tree fntype, fndecl;
3232 tree original = function;
3236 VEC(tree,gc) *allocated = NULL;
3239 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3240 expressions, like those used for ObjC messenger dispatches. */
3241 if (params != NULL && !VEC_empty (tree, *params))
3242 function = objc_rewrite_function_call (function,
3243 VEC_index (tree, *params, 0));
3245 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3246 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3247 if (TREE_CODE (function) == NOP_EXPR
3248 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3249 function = TREE_OPERAND (function, 0);
3251 if (TREE_CODE (function) == FUNCTION_DECL)
3253 mark_used (function);
3256 /* Convert anything with function type to a pointer-to-function. */
3257 if (DECL_MAIN_P (function) && (complain & tf_error))
3258 pedwarn (input_location, OPT_pedantic,
3259 "ISO C++ forbids calling %<::main%> from within program");
3261 function = build_addr_func (function);
3267 function = build_addr_func (function);
3270 if (function == error_mark_node)
3271 return error_mark_node;
3273 fntype = TREE_TYPE (function);
3275 if (TYPE_PTRMEMFUNC_P (fntype))
3277 if (complain & tf_error)
3278 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3279 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3280 original, original);
3281 return error_mark_node;
3284 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3285 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3287 if (!((TREE_CODE (fntype) == POINTER_TYPE
3288 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3290 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3292 if (complain & tf_error)
3293 error ("%qE cannot be used as a function", original);
3294 return error_mark_node;
3297 /* fntype now gets the type of function pointed to. */
3298 fntype = TREE_TYPE (fntype);
3299 parm_types = TYPE_ARG_TYPES (fntype);
3303 allocated = make_tree_vector ();
3304 params = &allocated;
3307 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3310 return error_mark_node;
3312 argarray = VEC_address (tree, *params);
3314 /* Check for errors in format strings and inappropriately
3316 check_function_arguments (fntype, nargs, argarray);
3318 ret = build_cxx_call (function, nargs, argarray);
3320 if (allocated != NULL)
3321 release_tree_vector (allocated);
3326 /* Subroutine of convert_arguments.
3327 Warn about wrong number of args are genereted. */
3330 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3334 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3336 if (DECL_NAME (fndecl) == NULL_TREE
3337 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3340 ? G_("too many arguments to constructor %q#D")
3341 : G_("too few arguments to constructor %q#D"),
3346 ? G_("too many arguments to member function %q#D")
3347 : G_("too few arguments to member function %q#D"),
3353 ? G_("too many arguments to function %q#D")
3354 : G_("too few arguments to function %q#D"),
3356 inform (DECL_SOURCE_LOCATION (fndecl),
3361 if (c_dialect_objc () && objc_message_selector ())
3364 ? G_("too many arguments to method %q#D")
3365 : G_("too few arguments to method %q#D"),
3366 objc_message_selector ());
3368 error_at (loc, too_many_p ? G_("too many arguments to function")
3369 : G_("too few arguments to function"));
3373 /* Convert the actual parameter expressions in the list VALUES to the
3374 types in the list TYPELIST. The converted expressions are stored
3375 back in the VALUES vector.
3376 If parmdecls is exhausted, or when an element has NULL as its type,
3377 perform the default conversions.
3379 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3381 This is also where warnings about wrong number of args are generated.
3383 Returns the actual number of arguments processed (which might be less
3384 than the length of the vector), or -1 on error.
3386 In C++, unspecified trailing parameters can be filled in with their
3387 default arguments, if such were specified. Do so here. */
3390 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3391 int flags, tsubst_flags_t complain)
3396 /* Argument passing is always copy-initialization. */
3397 flags |= LOOKUP_ONLYCONVERTING;
3399 for (i = 0, typetail = typelist;
3400 i < VEC_length (tree, *values);
3403 tree type = typetail ? TREE_VALUE (typetail) : 0;
3404 tree val = VEC_index (tree, *values, i);
3406 if (val == error_mark_node || type == error_mark_node)
3409 if (type == void_type_node)
3411 if (complain & tf_error)
3413 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3420 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3421 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3422 if (TREE_CODE (val) == NOP_EXPR
3423 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3424 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3425 val = TREE_OPERAND (val, 0);
3427 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3429 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3430 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3431 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3432 val = decay_conversion (val);
3435 if (val == error_mark_node)
3440 /* Formal parm type is specified by a function prototype. */
3443 if (!COMPLETE_TYPE_P (complete_type (type)))
3445 if (complain & tf_error)
3448 error ("parameter %P of %qD has incomplete type %qT",
3451 error ("parameter %P has incomplete type %qT", i, type);
3453 parmval = error_mark_node;
3457 parmval = convert_for_initialization
3458 (NULL_TREE, type, val, flags,
3459 ICR_ARGPASS, fndecl, i, complain);
3460 parmval = convert_for_arg_passing (type, parmval);
3463 if (parmval == error_mark_node)
3466 VEC_replace (tree, *values, i, parmval);
3470 if (fndecl && DECL_BUILT_IN (fndecl)
3471 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3472 /* Don't do ellipsis conversion for __built_in_constant_p
3473 as this will result in spurious errors for non-trivial
3475 val = require_complete_type_sfinae (val, complain);
3477 val = convert_arg_to_ellipsis (val);
3479 VEC_replace (tree, *values, i, val);
3483 typetail = TREE_CHAIN (typetail);
3486 if (typetail != 0 && typetail != void_list_node)
3488 /* See if there are default arguments that can be used. Because
3489 we hold default arguments in the FUNCTION_TYPE (which is so
3490 wrong), we can see default parameters here from deduced
3491 contexts (and via typeof) for indirect function calls.
3492 Fortunately we know whether we have a function decl to
3493 provide default arguments in a language conformant
3495 if (fndecl && TREE_PURPOSE (typetail)
3496 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3498 for (; typetail != void_list_node; ++i)
3501 = convert_default_arg (TREE_VALUE (typetail),
3502 TREE_PURPOSE (typetail),
3505 if (parmval == error_mark_node)
3508 VEC_safe_push (tree, gc, *values, parmval);
3509 typetail = TREE_CHAIN (typetail);
3510 /* ends with `...'. */
3511 if (typetail == NULL_TREE)
3517 if (complain & tf_error)
3518 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3526 /* Build a binary-operation expression, after performing default
3527 conversions on the operands. CODE is the kind of expression to
3528 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3529 are the tree codes which correspond to ARG1 and ARG2 when issuing
3530 warnings about possibly misplaced parentheses. They may differ
3531 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3532 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3533 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3534 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3535 ARG2_CODE as ERROR_MARK. */
3538 build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
3539 tree arg2, enum tree_code arg2_code, tree *overload,
3540 tsubst_flags_t complain)
3549 if (processing_template_decl)
3551 if (type_dependent_expression_p (arg1)
3552 || type_dependent_expression_p (arg2))
3553 return build_min_nt (code, arg1, arg2);
3554 arg1 = build_non_dependent_expr (arg1);
3555 arg2 = build_non_dependent_expr (arg2);
3558 if (code == DOTSTAR_EXPR)
3559 expr = build_m_component_ref (arg1, arg2);
3561 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3562 overload, complain);
3564 /* Check for cases such as x+y<<z which users are likely to
3565 misinterpret. But don't warn about obj << x + y, since that is a
3566 common idiom for I/O. */
3567 if (warn_parentheses
3568 && (complain & tf_warning)
3569 && !processing_template_decl
3570 && !error_operand_p (arg1)
3571 && !error_operand_p (arg2)
3572 && (code != LSHIFT_EXPR
3573 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3574 warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
3576 if (processing_template_decl && expr != error_mark_node)
3577 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3582 /* Build and return an ARRAY_REF expression. */
3585 build_x_array_ref (tree arg1, tree arg2, tsubst_flags_t complain)
3587 tree orig_arg1 = arg1;
3588 tree orig_arg2 = arg2;
3591 if (processing_template_decl)
3593 if (type_dependent_expression_p (arg1)
3594 || type_dependent_expression_p (arg2))
3595 return build_min_nt (ARRAY_REF, arg1, arg2,
3596 NULL_TREE, NULL_TREE);
3597 arg1 = build_non_dependent_expr (arg1);
3598 arg2 = build_non_dependent_expr (arg2);
3601 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3602 /*overload=*/NULL, complain);
3604 if (processing_template_decl && expr != error_mark_node)
3605 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3606 NULL_TREE, NULL_TREE);
3610 /* Return whether OP is an expression of enum type cast to integer
3611 type. In C++ even unsigned enum types are cast to signed integer
3612 types. We do not want to issue warnings about comparisons between
3613 signed and unsigned types when one of the types is an enum type.
3614 Those warnings are always false positives in practice. */
3617 enum_cast_to_int (tree op)
3619 if (TREE_CODE (op) == NOP_EXPR
3620 && TREE_TYPE (op) == integer_type_node
3621 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3622 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3625 /* The cast may have been pushed into a COND_EXPR. */
3626 if (TREE_CODE (op) == COND_EXPR)
3627 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3628 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3633 /* For the c-common bits. */
3635 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3636 int convert_p ATTRIBUTE_UNUSED)
3638 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3642 /* Build a binary-operation expression without default conversions.
3643 CODE is the kind of expression to build.
3644 LOCATION is the location_t of the operator in the source code.
3645 This function differs from `build' in several ways:
3646 the data type of the result is computed and recorded in it,
3647 warnings are generated if arg data types are invalid,
3648 special handling for addition and subtraction of pointers is known,
3649 and some optimization is done (operations on narrow ints
3650 are done in the narrower type when that gives the same result).
3651 Constant folding is also done before the result is returned.
3653 Note that the operands will never have enumeral types
3654 because either they have just had the default conversions performed
3655 or they have both just been converted to some other type in which
3656 the arithmetic is to be done.
3658 C++: must do special pointer arithmetic when implementing
3659 multiple inheritance, and deal with pointer to member functions. */
3662 cp_build_binary_op (location_t location,
3663 enum tree_code code, tree orig_op0, tree orig_op1,
3664 tsubst_flags_t complain)
3667 enum tree_code code0, code1;
3669 const char *invalid_op_diag;
3671 /* Expression code to give to the expression when it is built.
3672 Normally this is CODE, which is what the caller asked for,
3673 but in some special cases we change it. */
3674 enum tree_code resultcode = code;
3676 /* Data type in which the computation is to be performed.
3677 In the simplest cases this is the common type of the arguments. */
3678 tree result_type = NULL;
3680 /* Nonzero means operands have already been type-converted
3681 in whatever way is necessary.
3682 Zero means they need to be converted to RESULT_TYPE. */
3685 /* Nonzero means create the expression with this type, rather than
3687 tree build_type = 0;
3689 /* Nonzero means after finally constructing the expression
3690 convert it to this type. */
3691 tree final_type = 0;
3695 /* Nonzero if this is an operation like MIN or MAX which can
3696 safely be computed in short if both args are promoted shorts.
3697 Also implies COMMON.
3698 -1 indicates a bitwise operation; this makes a difference
3699 in the exact conditions for when it is safe to do the operation
3700 in a narrower mode. */
3703 /* Nonzero if this is a comparison operation;
3704 if both args are promoted shorts, compare the original shorts.
3705 Also implies COMMON. */
3706 int short_compare = 0;
3708 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3711 /* True if both operands have arithmetic type. */
3712 bool arithmetic_types_p;
3714 /* Apply default conversions. */
3718 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3719 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3720 || code == TRUTH_XOR_EXPR)
3722 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3723 op0 = decay_conversion (op0);
3724 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3725 op1 = decay_conversion (op1);
3729 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3730 op0 = default_conversion (op0);
3731 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3732 op1 = default_conversion (op1);
3735 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3736 STRIP_TYPE_NOPS (op0);
3737 STRIP_TYPE_NOPS (op1);
3739 /* DTRT if one side is an overloaded function, but complain about it. */
3740 if (type_unknown_p (op0))
3742 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3743 if (t != error_mark_node)
3745 if (complain & tf_error)
3746 permerror (input_location, "assuming cast to type %qT from overloaded function",
3751 if (type_unknown_p (op1))
3753 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3754 if (t != error_mark_node)
3756 if (complain & tf_error)
3757 permerror (input_location, "assuming cast to type %qT from overloaded function",
3763 type0 = TREE_TYPE (op0);
3764 type1 = TREE_TYPE (op1);
3766 /* The expression codes of the data types of the arguments tell us
3767 whether the arguments are integers, floating, pointers, etc. */
3768 code0 = TREE_CODE (type0);
3769 code1 = TREE_CODE (type1);
3771 /* If an error was already reported for one of the arguments,
3772 avoid reporting another error. */
3773 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3774 return error_mark_node;
3776 if ((invalid_op_diag
3777 = targetm.invalid_binary_op (code, type0, type1)))
3779 error (invalid_op_diag);
3780 return error_mark_node;
3783 /* Issue warnings about peculiar, but valid, uses of NULL. */
3784 if ((orig_op0 == null_node || orig_op1 == null_node)
3785 /* It's reasonable to use pointer values as operands of &&
3786 and ||, so NULL is no exception. */
3787 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3788 && ( /* Both are NULL (or 0) and the operation was not a
3789 comparison or a pointer subtraction. */
3790 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3791 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3792 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3793 || (!null_ptr_cst_p (orig_op0)
3794 && !TYPE_PTR_P (type0) && !TYPE_PTR_TO_MEMBER_P (type0))
3795 || (!null_ptr_cst_p (orig_op1)
3796 && !TYPE_PTR_P (type1) && !TYPE_PTR_TO_MEMBER_P (type1)))
3797 && (complain & tf_warning))
3798 /* Some sort of arithmetic operation involving NULL was
3800 warning (OPT_Wpointer_arith, "NULL used in arithmetic");
3805 /* Subtraction of two similar pointers.
3806 We must subtract them as integers, then divide by object size. */
3807 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3808 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3810 return pointer_diff (op0, op1, common_pointer_type (type0, type1));
3811 /* In all other cases except pointer - int, the usual arithmetic
3813 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3818 /* The pointer - int case is just like pointer + int; fall
3821 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3822 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3826 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3827 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3828 if (processing_template_decl)
3830 result_type = TREE_TYPE (ptr_operand);
3833 return cp_pointer_int_sum (code,
3844 case TRUNC_DIV_EXPR:
3846 case FLOOR_DIV_EXPR:
3847 case ROUND_DIV_EXPR:
3848 case EXACT_DIV_EXPR:
3849 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3850 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3851 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3852 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3854 enum tree_code tcode0 = code0, tcode1 = code1;
3856 warn_for_div_by_zero (location, op1);
3858 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3859 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3860 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3861 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3863 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
3864 resultcode = RDIV_EXPR;
3866 /* When dividing two signed integers, we have to promote to int.
3867 unless we divide by a constant != -1. Note that default
3868 conversion will have been performed on the operands at this
3869 point, so we have to dig out the original type to find out if
3871 shorten = ((TREE_CODE (op0) == NOP_EXPR
3872 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3873 || (TREE_CODE (op1) == INTEGER_CST
3874 && ! integer_all_onesp (op1)));
3883 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3884 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3885 && !VECTOR_FLOAT_TYPE_P (type0)
3886 && !VECTOR_FLOAT_TYPE_P (type1)))
3890 case TRUNC_MOD_EXPR:
3891 case FLOOR_MOD_EXPR:
3892 warn_for_div_by_zero (location, op1);
3894 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3895 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
3896 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
3898 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3900 /* Although it would be tempting to shorten always here, that loses
3901 on some targets, since the modulo instruction is undefined if the
3902 quotient can't be represented in the computation mode. We shorten
3903 only if unsigned or if dividing by something we know != -1. */
3904 shorten = ((TREE_CODE (op0) == NOP_EXPR
3905 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3906 || (TREE_CODE (op1) == INTEGER_CST
3907 && ! integer_all_onesp (op1)));
3912 case TRUTH_ANDIF_EXPR:
3913 case TRUTH_ORIF_EXPR:
3914 case TRUTH_AND_EXPR:
3916 result_type = boolean_type_node;
3919 /* Shift operations: result has same type as first operand;
3920 always convert second operand to int.
3921 Also set SHORT_SHIFT if shifting rightward. */
3924 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3926 result_type = type0;
3927 if (TREE_CODE (op1) == INTEGER_CST)
3929 if (tree_int_cst_lt (op1, integer_zero_node))
3931 if ((complain & tf_warning)
3932 && c_inhibit_evaluation_warnings == 0)
3933 warning (0, "right shift count is negative");
3937 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
3938 && (complain & tf_warning)
3939 && c_inhibit_evaluation_warnings == 0)
3940 warning (0, "right shift count >= width of type");
3943 /* Convert the shift-count to an integer, regardless of
3944 size of value being shifted. */
3945 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3946 op1 = cp_convert (integer_type_node, op1);
3947 /* Avoid converting op1 to result_type later. */
3953 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3955 result_type = type0;
3956 if (TREE_CODE (op1) == INTEGER_CST)
3958 if (tree_int_cst_lt (op1, integer_zero_node))
3960 if ((complain & tf_warning)
3961 && c_inhibit_evaluation_warnings == 0)
3962 warning (0, "left shift count is negative");
3964 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3966 if ((complain & tf_warning)
3967 && c_inhibit_evaluation_warnings == 0)
3968 warning (0, "left shift count >= width of type");
3971 /* Convert the shift-count to an integer, regardless of
3972 size of value being shifted. */
3973 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3974 op1 = cp_convert (integer_type_node, op1);
3975 /* Avoid converting op1 to result_type later. */
3982 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3984 result_type = type0;
3985 if (TREE_CODE (op1) == INTEGER_CST)
3987 if (tree_int_cst_lt (op1, integer_zero_node))
3989 if (complain & tf_warning)
3990 warning (0, (code == LROTATE_EXPR)
3991 ? G_("left rotate count is negative")
3992 : G_("right rotate count is negative"));
3994 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3996 if (complain & tf_warning)
3997 warning (0, (code == LROTATE_EXPR)
3998 ? G_("left rotate count >= width of type")
3999 : G_("right rotate count >= width of type"));
4002 /* Convert the shift-count to an integer, regardless of
4003 size of value being shifted. */
4004 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4005 op1 = cp_convert (integer_type_node, op1);
4011 if ((complain & tf_warning)
4012 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4013 warning (OPT_Wfloat_equal,
4014 "comparing floating point with == or != is unsafe");
4015 if ((complain & tf_warning)
4016 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4017 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4018 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4020 build_type = boolean_type_node;
4021 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4022 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4023 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4024 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4026 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4027 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
4028 result_type = composite_pointer_type (type0, type1, op0, op1,
4029 CPO_COMPARISON, complain);
4030 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
4031 && null_ptr_cst_p (op1))
4033 if (TREE_CODE (op0) == ADDR_EXPR
4034 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4036 if (complain & tf_warning)
4037 warning (OPT_Waddress, "the address of %qD will never be NULL",
4038 TREE_OPERAND (op0, 0));
4040 result_type = type0;
4042 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
4043 && null_ptr_cst_p (op0))
4045 if (TREE_CODE (op1) == ADDR_EXPR
4046 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4048 if (complain & tf_warning)
4049 warning (OPT_Waddress, "the address of %qD will never be NULL",
4050 TREE_OPERAND (op1, 0));
4052 result_type = type1;
4054 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4055 /* One of the operands must be of nullptr_t type. */
4056 result_type = TREE_TYPE (nullptr_node);
4057 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4059 result_type = type0;
4060 if (complain & tf_error)
4061 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4063 return error_mark_node;
4065 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4067 result_type = type1;
4068 if (complain & tf_error)
4069 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4071 return error_mark_node;
4073 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4075 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4076 == ptrmemfunc_vbit_in_delta)
4078 tree pfn0 = pfn_from_ptrmemfunc (op0);
4079 tree delta0 = delta_from_ptrmemfunc (op0);
4080 tree e1 = cp_build_binary_op (location,
4083 build_zero_cst (TREE_TYPE (pfn0)),
4085 tree e2 = cp_build_binary_op (location,
4091 if ((complain & tf_warning)
4092 && c_inhibit_evaluation_warnings == 0
4093 && !NULLPTR_TYPE_P (TREE_TYPE (op1)))
4094 warning (OPT_Wzero_as_null_pointer_constant,
4095 "zero as null pointer constant");
4097 e2 = cp_build_binary_op (location,
4098 EQ_EXPR, e2, integer_zero_node,
4100 op0 = cp_build_binary_op (location,
4101 TRUTH_ANDIF_EXPR, e1, e2,
4103 op1 = cp_convert (TREE_TYPE (op0), integer_one_node);
4107 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4108 op1 = cp_convert (TREE_TYPE (op0), op1);
4110 result_type = TREE_TYPE (op0);
4112 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4113 return cp_build_binary_op (location, code, op1, op0, complain);
4114 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4117 /* E will be the final comparison. */
4119 /* E1 and E2 are for scratch. */
4127 type = composite_pointer_type (type0, type1, op0, op1,
4128 CPO_COMPARISON, complain);
4130 if (!same_type_p (TREE_TYPE (op0), type))
4131 op0 = cp_convert_and_check (type, op0);
4132 if (!same_type_p (TREE_TYPE (op1), type))
4133 op1 = cp_convert_and_check (type, op1);
4135 if (op0 == error_mark_node || op1 == error_mark_node)
4136 return error_mark_node;
4138 if (TREE_SIDE_EFFECTS (op0))
4139 op0 = save_expr (op0);
4140 if (TREE_SIDE_EFFECTS (op1))
4141 op1 = save_expr (op1);
4143 pfn0 = pfn_from_ptrmemfunc (op0);
4144 pfn1 = pfn_from_ptrmemfunc (op1);
4145 delta0 = delta_from_ptrmemfunc (op0);
4146 delta1 = delta_from_ptrmemfunc (op1);
4147 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4148 == ptrmemfunc_vbit_in_delta)
4153 && ((op0.delta == op1.delta)
4154 || (!op0.pfn && op0.delta & 1 == 0
4155 && op1.delta & 1 == 0))
4157 The reason for the `!op0.pfn' bit is that a NULL
4158 pointer-to-member is any member with a zero PFN and
4159 LSB of the DELTA field is 0. */
4161 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4165 e1 = cp_build_binary_op (location,
4166 EQ_EXPR, e1, integer_zero_node,
4168 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4172 e2 = cp_build_binary_op (location,
4173 EQ_EXPR, e2, integer_zero_node,
4175 e1 = cp_build_binary_op (location,
4176 TRUTH_ANDIF_EXPR, e2, e1,
4178 e2 = cp_build_binary_op (location, EQ_EXPR,
4180 build_zero_cst (TREE_TYPE (pfn0)),
4182 e2 = cp_build_binary_op (location,
4183 TRUTH_ANDIF_EXPR, e2, e1, complain);
4184 e1 = cp_build_binary_op (location,
4185 EQ_EXPR, delta0, delta1, complain);
4186 e1 = cp_build_binary_op (location,
4187 TRUTH_ORIF_EXPR, e1, e2, complain);
4194 && (!op0.pfn || op0.delta == op1.delta))
4196 The reason for the `!op0.pfn' bit is that a NULL
4197 pointer-to-member is any member with a zero PFN; the
4198 DELTA field is unspecified. */
4200 e1 = cp_build_binary_op (location,
4201 EQ_EXPR, delta0, delta1, complain);
4202 e2 = cp_build_binary_op (location,
4205 build_zero_cst (TREE_TYPE (pfn0)),
4207 e1 = cp_build_binary_op (location,
4208 TRUTH_ORIF_EXPR, e1, e2, complain);
4210 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4211 e = cp_build_binary_op (location,
4212 TRUTH_ANDIF_EXPR, e2, e1, complain);
4213 if (code == EQ_EXPR)
4215 return cp_build_binary_op (location,
4216 EQ_EXPR, e, integer_zero_node, complain);
4220 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4221 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4223 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4224 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4232 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4233 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4235 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4236 result_type = composite_pointer_type (type0, type1, op0, op1,
4237 CPO_COMPARISON, complain);
4244 if (TREE_CODE (orig_op0) == STRING_CST
4245 || TREE_CODE (orig_op1) == STRING_CST)
4247 if (complain & tf_warning)
4248 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4251 build_type = boolean_type_node;
4252 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4253 || code0 == ENUMERAL_TYPE)
4254 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4255 || code1 == ENUMERAL_TYPE))
4257 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4258 result_type = composite_pointer_type (type0, type1, op0, op1,
4259 CPO_COMPARISON, complain);
4260 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4262 result_type = type0;
4263 if (extra_warnings && (complain & tf_warning))
4264 warning (OPT_Wextra,
4265 "ordered comparison of pointer with integer zero");
4267 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4269 result_type = type1;
4270 if (extra_warnings && (complain & tf_warning))
4271 warning (OPT_Wextra,
4272 "ordered comparison of pointer with integer zero");
4274 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4275 /* One of the operands must be of nullptr_t type. */
4276 result_type = TREE_TYPE (nullptr_node);
4277 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4279 result_type = type0;
4280 if (complain & tf_error)
4281 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4283 return error_mark_node;
4285 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4287 result_type = type1;
4288 if (complain & tf_error)
4289 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4291 return error_mark_node;
4295 case UNORDERED_EXPR:
4302 build_type = integer_type_node;
4303 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4305 if (complain & tf_error)
4306 error ("unordered comparison on non-floating point argument");
4307 return error_mark_node;
4316 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4317 || code0 == ENUMERAL_TYPE)
4318 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4319 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4320 arithmetic_types_p = 1;
4323 arithmetic_types_p = 0;
4324 /* Vector arithmetic is only allowed when both sides are vectors. */
4325 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4327 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4328 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4331 binary_op_error (location, code, type0, type1);
4332 return error_mark_node;
4334 arithmetic_types_p = 1;
4337 /* Determine the RESULT_TYPE, if it is not already known. */
4339 && arithmetic_types_p
4340 && (shorten || common || short_compare))
4342 result_type = cp_common_type (type0, type1);
4343 do_warn_double_promotion (result_type, type0, type1,
4344 "implicit conversion from %qT to %qT "
4345 "to match other operand of binary "
4352 if (complain & tf_error)
4353 error ("invalid operands of types %qT and %qT to binary %qO",
4354 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4355 return error_mark_node;
4358 /* If we're in a template, the only thing we need to know is the
4360 if (processing_template_decl)
4362 /* Since the middle-end checks the type when doing a build2, we
4363 need to build the tree in pieces. This built tree will never
4364 get out of the front-end as we replace it when instantiating
4366 tree tmp = build2 (resultcode,
4367 build_type ? build_type : result_type,
4369 TREE_OPERAND (tmp, 0) = op0;
4373 if (arithmetic_types_p)
4375 bool first_complex = (code0 == COMPLEX_TYPE);
4376 bool second_complex = (code1 == COMPLEX_TYPE);
4377 int none_complex = (!first_complex && !second_complex);
4379 /* Adapted from patch for c/24581. */
4380 if (first_complex != second_complex
4381 && (code == PLUS_EXPR
4382 || code == MINUS_EXPR
4383 || code == MULT_EXPR
4384 || (code == TRUNC_DIV_EXPR && first_complex))
4385 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4386 && flag_signed_zeros)
4388 /* An operation on mixed real/complex operands must be
4389 handled specially, but the language-independent code can
4390 more easily optimize the plain complex arithmetic if
4391 -fno-signed-zeros. */
4392 tree real_type = TREE_TYPE (result_type);
4396 if (TREE_TYPE (op0) != result_type)
4397 op0 = cp_convert_and_check (result_type, op0);
4398 if (TREE_TYPE (op1) != real_type)
4399 op1 = cp_convert_and_check (real_type, op1);
4403 if (TREE_TYPE (op0) != real_type)
4404 op0 = cp_convert_and_check (real_type, op0);
4405 if (TREE_TYPE (op1) != result_type)
4406 op1 = cp_convert_and_check (result_type, op1);
4408 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4409 return error_mark_node;
4412 op0 = save_expr (op0);
4413 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4414 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4418 case TRUNC_DIV_EXPR:
4419 op1 = save_expr (op1);
4420 imag = build2 (resultcode, real_type, imag, op1);
4424 real = build2 (resultcode, real_type, real, op1);
4432 op1 = save_expr (op1);
4433 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4434 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4438 op0 = save_expr (op0);
4439 imag = build2 (resultcode, real_type, op0, imag);
4442 real = build2 (resultcode, real_type, op0, real);
4445 real = build2 (resultcode, real_type, op0, real);
4446 imag = build1 (NEGATE_EXPR, real_type, imag);
4452 real = fold_if_not_in_template (real);
4453 imag = fold_if_not_in_template (imag);
4454 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4455 result = fold_if_not_in_template (result);
4459 /* For certain operations (which identify themselves by shorten != 0)
4460 if both args were extended from the same smaller type,
4461 do the arithmetic in that type and then extend.
4463 shorten !=0 and !=1 indicates a bitwise operation.
4464 For them, this optimization is safe only if
4465 both args are zero-extended or both are sign-extended.
4466 Otherwise, we might change the result.
4467 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4468 but calculated in (unsigned short) it would be (unsigned short)-1. */
4470 if (shorten && none_complex)
4472 final_type = result_type;
4473 result_type = shorten_binary_op (result_type, op0, op1,
4477 /* Comparison operations are shortened too but differently.
4478 They identify themselves by setting short_compare = 1. */
4482 /* Don't write &op0, etc., because that would prevent op0
4483 from being kept in a register.
4484 Instead, make copies of the our local variables and
4485 pass the copies by reference, then copy them back afterward. */
4486 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4487 enum tree_code xresultcode = resultcode;
4489 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4491 return cp_convert (boolean_type_node, val);
4492 op0 = xop0, op1 = xop1;
4494 resultcode = xresultcode;
4497 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4498 && warn_sign_compare
4499 /* Do not warn until the template is instantiated; we cannot
4500 bound the ranges of the arguments until that point. */
4501 && !processing_template_decl
4502 && (complain & tf_warning)
4503 && c_inhibit_evaluation_warnings == 0
4504 /* Even unsigned enum types promote to signed int. We don't
4505 want to issue -Wsign-compare warnings for this case. */
4506 && !enum_cast_to_int (orig_op0)
4507 && !enum_cast_to_int (orig_op1))
4509 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
4510 result_type, resultcode);
4514 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4515 Then the expression will be built.
4516 It will be given type FINAL_TYPE if that is nonzero;
4517 otherwise, it will be given type RESULT_TYPE. */
4520 if (TREE_TYPE (op0) != result_type)
4521 op0 = cp_convert_and_check (result_type, op0);
4522 if (TREE_TYPE (op1) != result_type)
4523 op1 = cp_convert_and_check (result_type, op1);
4525 if (op0 == error_mark_node || op1 == error_mark_node)
4526 return error_mark_node;
4529 if (build_type == NULL_TREE)
4530 build_type = result_type;
4532 result = build2 (resultcode, build_type, op0, op1);
4533 result = fold_if_not_in_template (result);
4534 if (final_type != 0)
4535 result = cp_convert (final_type, result);
4537 if (TREE_OVERFLOW_P (result)
4538 && !TREE_OVERFLOW_P (op0)
4539 && !TREE_OVERFLOW_P (op1))
4540 overflow_warning (location, result);
4545 /* Return a tree for the sum or difference (RESULTCODE says which)
4546 of pointer PTROP and integer INTOP. */
4549 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4551 tree res_type = TREE_TYPE (ptrop);
4553 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4554 in certain circumstance (when it's valid to do so). So we need
4555 to make sure it's complete. We don't need to check here, if we
4556 can actually complete it at all, as those checks will be done in
4557 pointer_int_sum() anyway. */
4558 complete_type (TREE_TYPE (res_type));
4560 return pointer_int_sum (input_location, resultcode, ptrop,
4561 fold_if_not_in_template (intop));
4564 /* Return a tree for the difference of pointers OP0 and OP1.
4565 The resulting tree has type int. */
4568 pointer_diff (tree op0, tree op1, tree ptrtype)
4571 tree restype = ptrdiff_type_node;
4572 tree target_type = TREE_TYPE (ptrtype);
4574 if (!complete_type_or_else (target_type, NULL_TREE))
4575 return error_mark_node;
4577 if (TREE_CODE (target_type) == VOID_TYPE)
4578 permerror (input_location, "ISO C++ forbids using pointer of type %<void *%> in subtraction");
4579 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4580 permerror (input_location, "ISO C++ forbids using pointer to a function in subtraction");
4581 if (TREE_CODE (target_type) == METHOD_TYPE)
4582 permerror (input_location, "ISO C++ forbids using pointer to a method in subtraction");
4584 /* First do the subtraction as integers;
4585 then drop through to build the divide operator. */
4587 op0 = cp_build_binary_op (input_location,
4589 cp_convert (restype, op0),
4590 cp_convert (restype, op1),
4591 tf_warning_or_error);
4593 /* This generates an error if op1 is a pointer to an incomplete type. */
4594 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4595 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4597 op1 = (TYPE_PTROB_P (ptrtype)
4598 ? size_in_bytes (target_type)
4599 : integer_one_node);
4601 /* Do the division. */
4603 result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4604 return fold_if_not_in_template (result);
4607 /* Construct and perhaps optimize a tree representation
4608 for a unary operation. CODE, a tree_code, specifies the operation
4609 and XARG is the operand. */
4612 build_x_unary_op (enum tree_code code, tree xarg, tsubst_flags_t complain)
4614 tree orig_expr = xarg;
4618 if (processing_template_decl)
4620 if (type_dependent_expression_p (xarg))
4621 return build_min_nt (code, xarg, NULL_TREE);
4623 xarg = build_non_dependent_expr (xarg);
4628 /* [expr.unary.op] says:
4630 The address of an object of incomplete type can be taken.
4632 (And is just the ordinary address operator, not an overloaded
4633 "operator &".) However, if the type is a template
4634 specialization, we must complete the type at this point so that
4635 an overloaded "operator &" will be available if required. */
4636 if (code == ADDR_EXPR
4637 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4638 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4639 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4640 || (TREE_CODE (xarg) == OFFSET_REF)))
4641 /* Don't look for a function. */;
4643 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
4644 /*overload=*/NULL, complain);
4645 if (!exp && code == ADDR_EXPR)
4647 if (is_overloaded_fn (xarg))
4649 tree fn = get_first_fn (xarg);
4650 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4652 error (DECL_CONSTRUCTOR_P (fn)
4653 ? G_("taking address of constructor %qE")
4654 : G_("taking address of destructor %qE"),
4656 return error_mark_node;
4660 /* A pointer to member-function can be formed only by saying
4662 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4663 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4665 if (TREE_CODE (xarg) != OFFSET_REF
4666 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4668 error ("invalid use of %qE to form a pointer-to-member-function",
4670 if (TREE_CODE (xarg) != OFFSET_REF)
4671 inform (input_location, " a qualified-id is required");
4672 return error_mark_node;
4676 error ("parentheses around %qE cannot be used to form a"
4677 " pointer-to-member-function",
4679 PTRMEM_OK_P (xarg) = 1;
4683 if (TREE_CODE (xarg) == OFFSET_REF)
4685 ptrmem = PTRMEM_OK_P (xarg);
4687 if (!ptrmem && !flag_ms_extensions
4688 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4690 /* A single non-static member, make sure we don't allow a
4691 pointer-to-member. */
4692 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4693 TREE_OPERAND (xarg, 0),
4694 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4695 PTRMEM_OK_P (xarg) = ptrmem;
4699 exp = cp_build_addr_expr_strict (xarg, complain);
4702 if (processing_template_decl && exp != error_mark_node)
4703 exp = build_min_non_dep (code, exp, orig_expr,
4704 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4705 if (TREE_CODE (exp) == ADDR_EXPR)
4706 PTRMEM_OK_P (exp) = ptrmem;
4710 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4711 constants, where a null value is represented by an INTEGER_CST of
4715 cp_truthvalue_conversion (tree expr)
4717 tree type = TREE_TYPE (expr);
4718 if (TYPE_PTRMEM_P (type))
4719 return build_binary_op (EXPR_LOCATION (expr),
4720 NE_EXPR, expr, nullptr_node, 1);
4721 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
4723 /* With -Wzero-as-null-pointer-constant do not warn for an
4724 'if (p)' or a 'while (!p)', where p is a pointer. */
4726 ++c_inhibit_evaluation_warnings;
4727 ret = c_common_truthvalue_conversion (input_location, expr);
4728 --c_inhibit_evaluation_warnings;
4732 return c_common_truthvalue_conversion (input_location, expr);
4735 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4738 condition_conversion (tree expr)
4741 if (processing_template_decl)
4743 t = perform_implicit_conversion_flags (boolean_type_node, expr,
4744 tf_warning_or_error, LOOKUP_NORMAL);
4745 t = fold_build_cleanup_point_expr (boolean_type_node, t);
4749 /* Returns the address of T. This function will fold away
4750 ADDR_EXPR of INDIRECT_REF. */
4753 build_address (tree t)
4755 if (error_operand_p (t) || !cxx_mark_addressable (t))
4756 return error_mark_node;
4757 t = build_fold_addr_expr (t);
4758 if (TREE_CODE (t) != ADDR_EXPR)
4763 /* Returns the address of T with type TYPE. */
4766 build_typed_address (tree t, tree type)
4768 if (error_operand_p (t) || !cxx_mark_addressable (t))
4769 return error_mark_node;
4770 t = build_fold_addr_expr_with_type (t, type);
4771 if (TREE_CODE (t) != ADDR_EXPR)
4776 /* Return a NOP_EXPR converting EXPR to TYPE. */
4779 build_nop (tree type, tree expr)
4781 if (type == error_mark_node || error_operand_p (expr))
4783 return build1 (NOP_EXPR, type, expr);
4786 /* Take the address of ARG, whatever that means under C++ semantics.
4787 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
4788 and class rvalues as well.
4790 Nothing should call this function directly; instead, callers should use
4791 cp_build_addr_expr or cp_build_addr_expr_strict. */
4794 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
4799 if (!arg || error_operand_p (arg))
4800 return error_mark_node;
4802 arg = mark_lvalue_use (arg);
4803 argtype = lvalue_type (arg);
4805 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4806 || !IDENTIFIER_OPNAME_P (arg));
4808 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4809 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4811 /* They're trying to take the address of a unique non-static
4812 member function. This is ill-formed (except in MS-land),
4813 but let's try to DTRT.
4814 Note: We only handle unique functions here because we don't
4815 want to complain if there's a static overload; non-unique
4816 cases will be handled by instantiate_type. But we need to
4817 handle this case here to allow casts on the resulting PMF.
4818 We could defer this in non-MS mode, but it's easier to give
4819 a useful error here. */
4821 /* Inside constant member functions, the `this' pointer
4822 contains an extra const qualifier. TYPE_MAIN_VARIANT
4823 is used here to remove this const from the diagnostics
4824 and the created OFFSET_REF. */
4825 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4826 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4829 if (! flag_ms_extensions)
4831 tree name = DECL_NAME (fn);
4832 if (!(complain & tf_error))
4833 return error_mark_node;
4834 else if (current_class_type
4835 && TREE_OPERAND (arg, 0) == current_class_ref)
4836 /* An expression like &memfn. */
4837 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
4838 " or parenthesized non-static member function to form"
4839 " a pointer to member function. Say %<&%T::%D%>",
4842 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
4843 " function to form a pointer to member function."
4847 arg = build_offset_ref (base, fn, /*address_p=*/true);
4850 /* Uninstantiated types are all functions. Taking the
4851 address of a function is a no-op, so just return the
4853 if (type_unknown_p (arg))
4854 return build1 (ADDR_EXPR, unknown_type_node, arg);
4856 if (TREE_CODE (arg) == OFFSET_REF)
4857 /* We want a pointer to member; bypass all the code for actually taking
4858 the address of something. */
4861 /* Anything not already handled and not a true memory reference
4863 if (TREE_CODE (argtype) != FUNCTION_TYPE
4864 && TREE_CODE (argtype) != METHOD_TYPE)
4866 cp_lvalue_kind kind = lvalue_kind (arg);
4867 if (kind == clk_none)
4869 if (complain & tf_error)
4870 lvalue_error (input_location, lv_addressof);
4871 return error_mark_node;
4873 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
4875 if (!(complain & tf_error))
4876 return error_mark_node;
4877 if (kind & clk_class)
4878 /* Make this a permerror because we used to accept it. */
4879 permerror (input_location, "taking address of temporary");
4881 error ("taking address of xvalue (rvalue reference)");
4885 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4887 tree type = build_pointer_type (TREE_TYPE (argtype));
4888 arg = build1 (CONVERT_EXPR, type, arg);
4891 else if (pedantic && DECL_MAIN_P (arg))
4894 /* Apparently a lot of autoconf scripts for C++ packages do this,
4895 so only complain if -pedantic. */
4896 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
4897 pedwarn (input_location, OPT_pedantic,
4898 "ISO C++ forbids taking address of function %<::main%>");
4899 else if (flag_pedantic_errors)
4900 return error_mark_node;
4903 /* Let &* cancel out to simplify resulting code. */
4904 if (TREE_CODE (arg) == INDIRECT_REF)
4906 /* We don't need to have `current_class_ptr' wrapped in a
4907 NON_LVALUE_EXPR node. */
4908 if (arg == current_class_ref)
4909 return current_class_ptr;
4911 arg = TREE_OPERAND (arg, 0);
4912 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4914 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
4915 arg = build1 (CONVERT_EXPR, type, arg);
4918 /* Don't let this be an lvalue. */
4923 /* ??? Cope with user tricks that amount to offsetof. */
4924 if (TREE_CODE (argtype) != FUNCTION_TYPE
4925 && TREE_CODE (argtype) != METHOD_TYPE
4926 && argtype != unknown_type_node
4927 && (val = get_base_address (arg))
4928 && COMPLETE_TYPE_P (TREE_TYPE (val))
4929 && TREE_CODE (val) == INDIRECT_REF
4930 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4932 tree type = build_pointer_type (argtype);
4933 return fold_convert (type, fold_offsetof_1 (arg));
4936 /* Handle complex lvalues (when permitted)
4937 by reduction to simpler cases. */
4938 val = unary_complex_lvalue (ADDR_EXPR, arg);
4942 switch (TREE_CODE (arg))
4946 case FIX_TRUNC_EXPR:
4947 /* Even if we're not being pedantic, we cannot allow this
4948 extension when we're instantiating in a SFINAE
4950 if (! lvalue_p (arg) && complain == tf_none)
4952 if (complain & tf_error)
4953 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4955 return error_mark_node;
4960 arg = BASELINK_FUNCTIONS (arg);
4964 arg = OVL_CURRENT (arg);
4969 /* Turn a reference to a non-static data member into a
4970 pointer-to-member. */
4975 gcc_assert (PTRMEM_OK_P (arg));
4977 t = TREE_OPERAND (arg, 1);
4978 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4980 if (complain & tf_error)
4981 error ("cannot create pointer to reference member %qD", t);
4982 return error_mark_node;
4985 type = build_ptrmem_type (context_for_name_lookup (t),
4987 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4995 if (argtype != error_mark_node)
4996 argtype = build_pointer_type (argtype);
4998 /* In a template, we are processing a non-dependent expression
4999 so we can just form an ADDR_EXPR with the correct type. */
5000 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5002 val = build_address (arg);
5003 if (TREE_CODE (arg) == OFFSET_REF)
5004 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5006 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5008 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5010 /* We can only get here with a single static member
5012 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5013 && DECL_STATIC_FUNCTION_P (fn));
5015 val = build_address (fn);
5016 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5017 /* Do not lose object's side effects. */
5018 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5019 TREE_OPERAND (arg, 0), val);
5021 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5023 if (complain & tf_error)
5024 error ("attempt to take address of bit-field structure member %qD",
5025 TREE_OPERAND (arg, 1));
5026 return error_mark_node;
5030 tree object = TREE_OPERAND (arg, 0);
5031 tree field = TREE_OPERAND (arg, 1);
5032 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5033 (TREE_TYPE (object), decl_type_context (field)));
5034 val = build_address (arg);
5037 if (TREE_CODE (argtype) == POINTER_TYPE
5038 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5040 build_ptrmemfunc_type (argtype);
5041 val = build_ptrmemfunc (argtype, val, 0,
5043 tf_warning_or_error);
5049 /* Take the address of ARG if it has one, even if it's an rvalue. */
5052 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5054 return cp_build_addr_expr_1 (arg, 0, complain);
5057 /* Take the address of ARG, but only if it's an lvalue. */
5060 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5062 return cp_build_addr_expr_1 (arg, 1, complain);
5065 /* C++: Must handle pointers to members.
5067 Perhaps type instantiation should be extended to handle conversion
5068 from aggregates to types we don't yet know we want? (Or are those
5069 cases typically errors which should be reported?)
5071 NOCONVERT nonzero suppresses the default promotions
5072 (such as from short to int). */
5075 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5076 tsubst_flags_t complain)
5078 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5081 const char *errstring = NULL;
5083 const char *invalid_op_diag;
5085 if (!arg || error_operand_p (arg))
5086 return error_mark_node;
5088 if ((invalid_op_diag
5089 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5094 error (invalid_op_diag);
5095 return error_mark_node;
5100 case UNARY_PLUS_EXPR:
5103 int flags = WANT_ARITH | WANT_ENUM;
5104 /* Unary plus (but not unary minus) is allowed on pointers. */
5105 if (code == UNARY_PLUS_EXPR)
5106 flags |= WANT_POINTER;
5107 arg = build_expr_type_conversion (flags, arg, true);
5109 errstring = (code == NEGATE_EXPR
5110 ? _("wrong type argument to unary minus")
5111 : _("wrong type argument to unary plus"));
5114 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5115 arg = perform_integral_promotions (arg);
5117 /* Make sure the result is not an lvalue: a unary plus or minus
5118 expression is always a rvalue. */
5125 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5129 arg = default_conversion (arg);
5131 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5132 | WANT_VECTOR_OR_COMPLEX,
5134 errstring = _("wrong type argument to bit-complement");
5135 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5136 arg = perform_integral_promotions (arg);
5140 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5141 errstring = _("wrong type argument to abs");
5142 else if (!noconvert)
5143 arg = default_conversion (arg);
5147 /* Conjugating a real value is a no-op, but allow it anyway. */
5148 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5149 errstring = _("wrong type argument to conjugation");
5150 else if (!noconvert)
5151 arg = default_conversion (arg);
5154 case TRUTH_NOT_EXPR:
5155 arg = perform_implicit_conversion (boolean_type_node, arg,
5157 val = invert_truthvalue_loc (input_location, arg);
5158 if (arg != error_mark_node)
5160 errstring = _("in argument to unary !");
5168 arg = build_real_imag_expr (input_location, code, arg);
5169 if (arg == error_mark_node)
5172 return fold_if_not_in_template (arg);
5174 case PREINCREMENT_EXPR:
5175 case POSTINCREMENT_EXPR:
5176 case PREDECREMENT_EXPR:
5177 case POSTDECREMENT_EXPR:
5178 /* Handle complex lvalues (when permitted)
5179 by reduction to simpler cases. */
5181 val = unary_complex_lvalue (code, arg);
5185 arg = mark_lvalue_use (arg);
5187 /* Increment or decrement the real part of the value,
5188 and don't change the imaginary part. */
5189 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5193 arg = stabilize_reference (arg);
5194 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5195 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5196 real = cp_build_unary_op (code, real, 1, complain);
5197 if (real == error_mark_node || imag == error_mark_node)
5198 return error_mark_node;
5199 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5203 /* Report invalid types. */
5205 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5208 if (code == PREINCREMENT_EXPR)
5209 errstring = _("no pre-increment operator for type");
5210 else if (code == POSTINCREMENT_EXPR)
5211 errstring = _("no post-increment operator for type");
5212 else if (code == PREDECREMENT_EXPR)
5213 errstring = _("no pre-decrement operator for type");
5215 errstring = _("no post-decrement operator for type");
5218 else if (arg == error_mark_node)
5219 return error_mark_node;
5221 /* Report something read-only. */
5223 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5224 || TREE_READONLY (arg))
5226 if (complain & tf_error)
5227 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5228 || code == POSTINCREMENT_EXPR)
5229 ? lv_increment : lv_decrement));
5231 return error_mark_node;
5236 tree declared_type = unlowered_expr_type (arg);
5238 argtype = TREE_TYPE (arg);
5240 /* ARM $5.2.5 last annotation says this should be forbidden. */
5241 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5243 if (complain & tf_error)
5244 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5245 ? G_("ISO C++ forbids incrementing an enum")
5246 : G_("ISO C++ forbids decrementing an enum"));
5248 return error_mark_node;
5251 /* Compute the increment. */
5253 if (TREE_CODE (argtype) == POINTER_TYPE)
5255 tree type = complete_type (TREE_TYPE (argtype));
5257 if (!COMPLETE_OR_VOID_TYPE_P (type))
5259 if (complain & tf_error)
5260 error (((code == PREINCREMENT_EXPR
5261 || code == POSTINCREMENT_EXPR))
5262 ? G_("cannot increment a pointer to incomplete type %qT")
5263 : G_("cannot decrement a pointer to incomplete type %qT"),
5264 TREE_TYPE (argtype));
5266 return error_mark_node;
5268 else if ((pedantic || warn_pointer_arith)
5269 && !TYPE_PTROB_P (argtype))
5271 if (complain & tf_error)
5272 permerror (input_location, (code == PREINCREMENT_EXPR
5273 || code == POSTINCREMENT_EXPR)
5274 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5275 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5278 return error_mark_node;
5281 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5284 inc = integer_one_node;
5286 inc = cp_convert (argtype, inc);
5288 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5289 need to ask Objective-C to build the increment or decrement
5290 expression for it. */
5291 if (objc_is_property_ref (arg))
5292 return objc_build_incr_expr_for_property_ref (input_location, code,
5295 /* Complain about anything else that is not a true lvalue. */
5296 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5297 || code == POSTINCREMENT_EXPR)
5298 ? lv_increment : lv_decrement),
5300 return error_mark_node;
5302 /* Forbid using -- on `bool'. */
5303 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5305 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5307 if (complain & tf_error)
5308 error ("invalid use of Boolean expression as operand "
5309 "to %<operator--%>");
5310 return error_mark_node;
5312 val = boolean_increment (code, arg);
5314 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5315 /* An rvalue has no cv-qualifiers. */
5316 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5318 val = build2 (code, TREE_TYPE (arg), arg, inc);
5320 TREE_SIDE_EFFECTS (val) = 1;
5325 /* Note that this operation never does default_conversion
5326 regardless of NOCONVERT. */
5327 return cp_build_addr_expr (arg, complain);
5336 argtype = TREE_TYPE (arg);
5337 return fold_if_not_in_template (build1 (code, argtype, arg));
5340 if (complain & tf_error)
5341 error ("%s", errstring);
5342 return error_mark_node;
5345 /* Hook for the c-common bits that build a unary op. */
5347 build_unary_op (location_t location ATTRIBUTE_UNUSED,
5348 enum tree_code code, tree xarg, int noconvert)
5350 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5353 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5354 for certain kinds of expressions which are not really lvalues
5355 but which we can accept as lvalues.
5357 If ARG is not a kind of expression we can handle, return
5361 unary_complex_lvalue (enum tree_code code, tree arg)
5363 /* Inside a template, making these kinds of adjustments is
5364 pointless; we are only concerned with the type of the
5366 if (processing_template_decl)
5369 /* Handle (a, b) used as an "lvalue". */
5370 if (TREE_CODE (arg) == COMPOUND_EXPR)
5372 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5373 tf_warning_or_error);
5374 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5375 TREE_OPERAND (arg, 0), real_result);
5378 /* Handle (a ? b : c) used as an "lvalue". */
5379 if (TREE_CODE (arg) == COND_EXPR
5380 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5381 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5383 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5384 if (TREE_CODE (arg) == MODIFY_EXPR
5385 || TREE_CODE (arg) == PREINCREMENT_EXPR
5386 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5388 tree lvalue = TREE_OPERAND (arg, 0);
5389 if (TREE_SIDE_EFFECTS (lvalue))
5391 lvalue = stabilize_reference (lvalue);
5392 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5393 lvalue, TREE_OPERAND (arg, 1));
5395 return unary_complex_lvalue
5396 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5399 if (code != ADDR_EXPR)
5402 /* Handle (a = b) used as an "lvalue" for `&'. */
5403 if (TREE_CODE (arg) == MODIFY_EXPR
5404 || TREE_CODE (arg) == INIT_EXPR)
5406 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5407 tf_warning_or_error);
5408 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5410 TREE_NO_WARNING (arg) = 1;
5414 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5415 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5416 || TREE_CODE (arg) == OFFSET_REF)
5419 /* We permit compiler to make function calls returning
5420 objects of aggregate type look like lvalues. */
5424 if (TREE_CODE (targ) == SAVE_EXPR)
5425 targ = TREE_OPERAND (targ, 0);
5427 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5429 if (TREE_CODE (arg) == SAVE_EXPR)
5432 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5433 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5436 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5437 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5438 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5441 /* Don't let anything else be handled specially. */
5445 /* Mark EXP saying that we need to be able to take the
5446 address of it; it should not be allocated in a register.
5447 Value is true if successful.
5449 C++: we do not allow `current_class_ptr' to be addressable. */
5452 cxx_mark_addressable (tree exp)
5457 switch (TREE_CODE (x))
5464 x = TREE_OPERAND (x, 0);
5468 if (x == current_class_ptr)
5470 error ("cannot take the address of %<this%>, which is an rvalue expression");
5471 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5477 /* Caller should not be trying to mark initialized
5478 constant fields addressable. */
5479 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5480 || DECL_IN_AGGR_P (x) == 0
5482 || DECL_EXTERNAL (x));
5486 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5487 && !DECL_ARTIFICIAL (x))
5489 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5492 ("address of explicit register variable %qD requested", x);
5495 else if (extra_warnings)
5497 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5499 TREE_ADDRESSABLE (x) = 1;
5504 TREE_ADDRESSABLE (x) = 1;
5508 TREE_ADDRESSABLE (x) = 1;
5512 TREE_ADDRESSABLE (x) = 1;
5513 cxx_mark_addressable (TREE_OPERAND (x, 0));
5521 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5524 build_x_conditional_expr (tree ifexp, tree op1, tree op2,
5525 tsubst_flags_t complain)
5527 tree orig_ifexp = ifexp;
5528 tree orig_op1 = op1;
5529 tree orig_op2 = op2;
5532 if (processing_template_decl)
5534 /* The standard says that the expression is type-dependent if
5535 IFEXP is type-dependent, even though the eventual type of the
5536 expression doesn't dependent on IFEXP. */
5537 if (type_dependent_expression_p (ifexp)
5538 /* As a GNU extension, the middle operand may be omitted. */
5539 || (op1 && type_dependent_expression_p (op1))
5540 || type_dependent_expression_p (op2))
5541 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5542 ifexp = build_non_dependent_expr (ifexp);
5544 op1 = build_non_dependent_expr (op1);
5545 op2 = build_non_dependent_expr (op2);
5548 expr = build_conditional_expr (ifexp, op1, op2, complain);
5549 if (processing_template_decl && expr != error_mark_node)
5551 tree min = build_min_non_dep (COND_EXPR, expr,
5552 orig_ifexp, orig_op1, orig_op2);
5553 /* In C++11, remember that the result is an lvalue or xvalue.
5554 In C++98, lvalue_kind can just assume lvalue in a template. */
5555 if (cxx_dialect >= cxx0x
5556 && lvalue_or_rvalue_with_address_p (expr)
5557 && !lvalue_or_rvalue_with_address_p (min))
5558 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5559 !real_lvalue_p (expr));
5560 expr = convert_from_reference (min);
5565 /* Given a list of expressions, return a compound expression
5566 that performs them all and returns the value of the last of them. */
5569 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5570 tsubst_flags_t complain)
5572 tree expr = TREE_VALUE (list);
5574 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5575 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5577 if (complain & tf_error)
5578 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5579 "non-class type must not be parenthesized");
5581 return error_mark_node;
5584 if (TREE_CHAIN (list))
5586 if (complain & tf_error)
5590 permerror (input_location, "expression list treated as compound "
5591 "expression in initializer");
5594 permerror (input_location, "expression list treated as compound "
5595 "expression in mem-initializer");
5598 permerror (input_location, "expression list treated as compound "
5599 "expression in functional cast");
5605 return error_mark_node;
5607 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5608 expr = build_x_compound_expr (expr, TREE_VALUE (list),
5615 /* Like build_x_compound_expr_from_list, but using a VEC. */
5618 build_x_compound_expr_from_vec (VEC(tree,gc) *vec, const char *msg)
5620 if (VEC_empty (tree, vec))
5622 else if (VEC_length (tree, vec) == 1)
5623 return VEC_index (tree, vec, 0);
5631 permerror (input_location,
5632 "%s expression list treated as compound expression",
5635 expr = VEC_index (tree, vec, 0);
5636 for (ix = 1; VEC_iterate (tree, vec, ix, t); ++ix)
5637 expr = build_x_compound_expr (expr, t, tf_warning_or_error);
5643 /* Handle overloading of the ',' operator when needed. */
5646 build_x_compound_expr (tree op1, tree op2, tsubst_flags_t complain)
5649 tree orig_op1 = op1;
5650 tree orig_op2 = op2;
5652 if (processing_template_decl)
5654 if (type_dependent_expression_p (op1)
5655 || type_dependent_expression_p (op2))
5656 return build_min_nt (COMPOUND_EXPR, op1, op2);
5657 op1 = build_non_dependent_expr (op1);
5658 op2 = build_non_dependent_expr (op2);
5661 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
5662 /*overload=*/NULL, complain);
5664 result = cp_build_compound_expr (op1, op2, complain);
5666 if (processing_template_decl && result != error_mark_node)
5667 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5672 /* Like cp_build_compound_expr, but for the c-common bits. */
5675 build_compound_expr (location_t loc ATTRIBUTE_UNUSED, tree lhs, tree rhs)
5677 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5680 /* Build a compound expression. */
5683 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5685 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
5687 if (lhs == error_mark_node || rhs == error_mark_node)
5688 return error_mark_node;
5690 if (TREE_CODE (rhs) == TARGET_EXPR)
5692 /* If the rhs is a TARGET_EXPR, then build the compound
5693 expression inside the target_expr's initializer. This
5694 helps the compiler to eliminate unnecessary temporaries. */
5695 tree init = TREE_OPERAND (rhs, 1);
5697 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5698 TREE_OPERAND (rhs, 1) = init;
5703 if (type_unknown_p (rhs))
5705 error ("no context to resolve type of %qE", rhs);
5706 return error_mark_node;
5709 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
5712 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
5713 casts away constness. CAST gives the type of cast. Returns true
5714 if the cast is ill-formed, false if it is well-formed.
5716 ??? This function warns for casting away any qualifier not just
5717 const. We would like to specify exactly what qualifiers are casted
5722 check_for_casting_away_constness (tree src_type, tree dest_type,
5723 enum tree_code cast, tsubst_flags_t complain)
5725 /* C-style casts are allowed to cast away constness. With
5726 WARN_CAST_QUAL, we still want to issue a warning. */
5727 if (cast == CAST_EXPR && !warn_cast_qual)
5730 if (!casts_away_constness (src_type, dest_type))
5736 if (complain & tf_warning)
5737 warning (OPT_Wcast_qual,
5738 "cast from type %qT to type %qT casts away qualifiers",
5739 src_type, dest_type);
5742 case STATIC_CAST_EXPR:
5743 if (complain & tf_error)
5744 error ("static_cast from type %qT to type %qT casts away qualifiers",
5745 src_type, dest_type);
5748 case REINTERPRET_CAST_EXPR:
5749 if (complain & tf_error)
5750 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
5751 src_type, dest_type);
5759 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
5760 (another pointer-to-member type in the same hierarchy) and return
5761 the converted expression. If ALLOW_INVERSE_P is permitted, a
5762 pointer-to-derived may be converted to pointer-to-base; otherwise,
5763 only the other direction is permitted. If C_CAST_P is true, this
5764 conversion is taking place as part of a C-style cast. */
5767 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
5768 bool c_cast_p, tsubst_flags_t complain)
5770 if (TYPE_PTRMEM_P (type))
5774 if (TREE_CODE (expr) == PTRMEM_CST)
5775 expr = cplus_expand_constant (expr);
5776 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
5777 TYPE_PTRMEM_CLASS_TYPE (type),
5779 c_cast_p, complain);
5780 if (delta == error_mark_node)
5781 return error_mark_node;
5783 if (!integer_zerop (delta))
5785 tree cond, op1, op2;
5787 cond = cp_build_binary_op (input_location,
5790 build_int_cst (TREE_TYPE (expr), -1),
5791 tf_warning_or_error);
5792 op1 = build_nop (ptrdiff_type_node, expr);
5793 op2 = cp_build_binary_op (input_location,
5794 PLUS_EXPR, op1, delta,
5795 tf_warning_or_error);
5797 expr = fold_build3_loc (input_location,
5798 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
5802 return build_nop (type, expr);
5805 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
5806 allow_inverse_p, c_cast_p, complain);
5809 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
5810 this static_cast is being attempted as one of the possible casts
5811 allowed by a C-style cast. (In that case, accessibility of base
5812 classes is not considered, and it is OK to cast away
5813 constness.) Return the result of the cast. *VALID_P is set to
5814 indicate whether or not the cast was valid. */
5817 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
5818 bool *valid_p, tsubst_flags_t complain)
5824 /* Assume the cast is valid. */
5827 intype = unlowered_expr_type (expr);
5829 /* Save casted types in the function's used types hash table. */
5830 used_types_insert (type);
5832 /* [expr.static.cast]
5834 An lvalue of type "cv1 B", where B is a class type, can be cast
5835 to type "reference to cv2 D", where D is a class derived (clause
5836 _class.derived_) from B, if a valid standard conversion from
5837 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
5838 same cv-qualification as, or greater cv-qualification than, cv1,
5839 and B is not a virtual base class of D. */
5840 /* We check this case before checking the validity of "TYPE t =
5841 EXPR;" below because for this case:
5844 struct D : public B { D(const B&); };
5846 void f() { static_cast<const D&>(b); }
5848 we want to avoid constructing a new D. The standard is not
5849 completely clear about this issue, but our interpretation is
5850 consistent with other compilers. */
5851 if (TREE_CODE (type) == REFERENCE_TYPE
5852 && CLASS_TYPE_P (TREE_TYPE (type))
5853 && CLASS_TYPE_P (intype)
5854 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
5855 && DERIVED_FROM_P (intype, TREE_TYPE (type))
5856 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
5857 build_pointer_type (TYPE_MAIN_VARIANT
5858 (TREE_TYPE (type))))
5860 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5864 /* There is a standard conversion from "D*" to "B*" even if "B"
5865 is ambiguous or inaccessible. If this is really a
5866 static_cast, then we check both for inaccessibility and
5867 ambiguity. However, if this is a static_cast being performed
5868 because the user wrote a C-style cast, then accessibility is
5870 base = lookup_base (TREE_TYPE (type), intype,
5871 c_cast_p ? ba_unique : ba_check,
5874 /* Convert from "B*" to "D*". This function will check that "B"
5875 is not a virtual base of "D". */
5876 expr = build_base_path (MINUS_EXPR, build_address (expr),
5877 base, /*nonnull=*/false, complain);
5878 /* Convert the pointer to a reference -- but then remember that
5879 there are no expressions with reference type in C++.
5881 We call rvalue so that there's an actual tree code
5882 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
5883 is a variable with the same type, the conversion would get folded
5884 away, leaving just the variable and causing lvalue_kind to give
5885 the wrong answer. */
5886 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
5889 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
5890 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
5891 if (TREE_CODE (type) == REFERENCE_TYPE
5892 && TYPE_REF_IS_RVALUE (type)
5893 && (clk = real_lvalue_p (expr))
5894 && reference_related_p (TREE_TYPE (type), intype)
5895 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5897 if (clk == clk_ordinary)
5899 /* Handle the (non-bit-field) lvalue case here by casting to
5900 lvalue reference and then changing it to an rvalue reference.
5901 Casting an xvalue to rvalue reference will be handled by the
5903 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
5904 result = (perform_direct_initialization_if_possible
5905 (lref, expr, c_cast_p, complain));
5906 result = cp_fold_convert (type, result);
5907 /* Make sure we don't fold back down to a named rvalue reference,
5908 because that would be an lvalue. */
5909 if (DECL_P (result))
5910 result = build1 (NON_LVALUE_EXPR, type, result);
5911 return convert_from_reference (result);
5914 /* For a bit-field or packed field, bind to a temporary. */
5915 expr = rvalue (expr);
5918 /* Resolve overloaded address here rather than once in
5919 implicit_conversion and again in the inverse code below. */
5920 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
5922 expr = instantiate_type (type, expr, complain);
5923 intype = TREE_TYPE (expr);
5926 /* [expr.static.cast]
5928 An expression e can be explicitly converted to a type T using a
5929 static_cast of the form static_cast<T>(e) if the declaration T
5930 t(e);" is well-formed, for some invented temporary variable
5932 result = perform_direct_initialization_if_possible (type, expr,
5933 c_cast_p, complain);
5936 result = convert_from_reference (result);
5938 /* [expr.static.cast]
5940 If T is a reference type, the result is an lvalue; otherwise,
5941 the result is an rvalue. */
5942 if (TREE_CODE (type) != REFERENCE_TYPE)
5943 result = rvalue (result);
5947 /* [expr.static.cast]
5949 Any expression can be explicitly converted to type cv void. */
5950 if (TREE_CODE (type) == VOID_TYPE)
5951 return convert_to_void (expr, ICV_CAST, complain);
5953 /* [expr.static.cast]
5955 The inverse of any standard conversion sequence (clause _conv_),
5956 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
5957 (_conv.array_), function-to-pointer (_conv.func_), and boolean
5958 (_conv.bool_) conversions, can be performed explicitly using
5959 static_cast subject to the restriction that the explicit
5960 conversion does not cast away constness (_expr.const.cast_), and
5961 the following additional rules for specific cases: */
5962 /* For reference, the conversions not excluded are: integral
5963 promotions, floating point promotion, integral conversions,
5964 floating point conversions, floating-integral conversions,
5965 pointer conversions, and pointer to member conversions. */
5968 A value of integral _or enumeration_ type can be explicitly
5969 converted to an enumeration type. */
5970 /* The effect of all that is that any conversion between any two
5971 types which are integral, floating, or enumeration types can be
5973 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5974 || SCALAR_FLOAT_TYPE_P (type))
5975 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
5976 || SCALAR_FLOAT_TYPE_P (intype)))
5977 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
5979 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
5980 && CLASS_TYPE_P (TREE_TYPE (type))
5981 && CLASS_TYPE_P (TREE_TYPE (intype))
5982 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
5983 (TREE_TYPE (intype))),
5984 build_pointer_type (TYPE_MAIN_VARIANT
5985 (TREE_TYPE (type)))))
5990 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
5992 return error_mark_node;
5993 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
5994 c_cast_p ? ba_unique : ba_check,
5996 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
5998 return cp_fold_convert(type, expr);
6001 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
6002 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6009 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6010 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6012 if (TYPE_PTRMEM_P (type))
6014 t1 = (build_ptrmem_type
6016 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6017 t2 = (build_ptrmem_type
6019 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6026 if (can_convert (t1, t2) || can_convert (t2, t1))
6029 && check_for_casting_away_constness (intype, type,
6032 return error_mark_node;
6033 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6034 c_cast_p, complain);
6038 /* [expr.static.cast]
6040 An rvalue of type "pointer to cv void" can be explicitly
6041 converted to a pointer to object type. A value of type pointer
6042 to object converted to "pointer to cv void" and back to the
6043 original pointer type will have its original value. */
6044 if (TREE_CODE (intype) == POINTER_TYPE
6045 && VOID_TYPE_P (TREE_TYPE (intype))
6046 && TYPE_PTROB_P (type))
6049 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6051 return error_mark_node;
6052 return build_nop (type, expr);
6056 return error_mark_node;
6059 /* Return an expression representing static_cast<TYPE>(EXPR). */
6062 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6067 if (type == error_mark_node || expr == error_mark_node)
6068 return error_mark_node;
6070 if (processing_template_decl)
6072 expr = build_min (STATIC_CAST_EXPR, type, expr);
6073 /* We don't know if it will or will not have side effects. */
6074 TREE_SIDE_EFFECTS (expr) = 1;
6075 return convert_from_reference (expr);
6078 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6079 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6080 if (TREE_CODE (type) != REFERENCE_TYPE
6081 && TREE_CODE (expr) == NOP_EXPR
6082 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6083 expr = TREE_OPERAND (expr, 0);
6085 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6090 if (complain & tf_error)
6091 error ("invalid static_cast from type %qT to type %qT",
6092 TREE_TYPE (expr), type);
6093 return error_mark_node;
6096 /* EXPR is an expression with member function or pointer-to-member
6097 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6098 not permitted by ISO C++, but we accept it in some modes. If we
6099 are not in one of those modes, issue a diagnostic. Return the
6100 converted expression. */
6103 convert_member_func_to_ptr (tree type, tree expr)
6108 intype = TREE_TYPE (expr);
6109 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6110 || TREE_CODE (intype) == METHOD_TYPE);
6112 if (pedantic || warn_pmf2ptr)
6113 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpmf_conversions,
6114 "converting from %qT to %qT", intype, type);
6116 if (TREE_CODE (intype) == METHOD_TYPE)
6117 expr = build_addr_func (expr);
6118 else if (TREE_CODE (expr) == PTRMEM_CST)
6119 expr = build_address (PTRMEM_CST_MEMBER (expr));
6122 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6123 decl = build_address (decl);
6124 expr = get_member_function_from_ptrfunc (&decl, expr);
6127 return build_nop (type, expr);
6130 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6131 If C_CAST_P is true, this reinterpret cast is being done as part of
6132 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6133 indicate whether or not reinterpret_cast was valid. */
6136 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6137 bool *valid_p, tsubst_flags_t complain)
6141 /* Assume the cast is invalid. */
6145 if (type == error_mark_node || error_operand_p (expr))
6146 return error_mark_node;
6148 intype = TREE_TYPE (expr);
6150 /* Save casted types in the function's used types hash table. */
6151 used_types_insert (type);
6153 /* [expr.reinterpret.cast]
6154 An lvalue expression of type T1 can be cast to the type
6155 "reference to T2" if an expression of type "pointer to T1" can be
6156 explicitly converted to the type "pointer to T2" using a
6157 reinterpret_cast. */
6158 if (TREE_CODE (type) == REFERENCE_TYPE)
6160 if (! real_lvalue_p (expr))
6162 if (complain & tf_error)
6163 error ("invalid cast of an rvalue expression of type "
6166 return error_mark_node;
6169 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6170 "B" are related class types; the reinterpret_cast does not
6171 adjust the pointer. */
6172 if (TYPE_PTR_P (intype)
6173 && (complain & tf_warning)
6174 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6175 COMPARE_BASE | COMPARE_DERIVED)))
6176 warning (0, "casting %qT to %qT does not dereference pointer",
6179 expr = cp_build_addr_expr (expr, complain);
6181 if (warn_strict_aliasing > 2)
6182 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6184 if (expr != error_mark_node)
6185 expr = build_reinterpret_cast_1
6186 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6188 if (expr != error_mark_node)
6189 /* cp_build_indirect_ref isn't right for rvalue refs. */
6190 expr = convert_from_reference (fold_convert (type, expr));
6194 /* As a G++ extension, we consider conversions from member
6195 functions, and pointers to member functions to
6196 pointer-to-function and pointer-to-void types. If
6197 -Wno-pmf-conversions has not been specified,
6198 convert_member_func_to_ptr will issue an error message. */
6199 if ((TYPE_PTRMEMFUNC_P (intype)
6200 || TREE_CODE (intype) == METHOD_TYPE)
6201 && TYPE_PTR_P (type)
6202 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6203 || VOID_TYPE_P (TREE_TYPE (type))))
6204 return convert_member_func_to_ptr (type, expr);
6206 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6207 array-to-pointer, and function-to-pointer conversions are
6209 expr = decay_conversion (expr);
6211 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6212 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6213 if (TREE_CODE (expr) == NOP_EXPR
6214 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6215 expr = TREE_OPERAND (expr, 0);
6217 if (error_operand_p (expr))
6218 return error_mark_node;
6220 intype = TREE_TYPE (expr);
6222 /* [expr.reinterpret.cast]
6223 A pointer can be converted to any integral type large enough to
6224 hold it. ... A value of type std::nullptr_t can be converted to
6225 an integral type; the conversion has the same meaning and
6226 validity as a conversion of (void*)0 to the integral type. */
6227 if (CP_INTEGRAL_TYPE_P (type)
6228 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6230 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6232 if (complain & tf_error)
6233 permerror (input_location, "cast from %qT to %qT loses precision",
6236 return error_mark_node;
6238 if (NULLPTR_TYPE_P (intype))
6239 return build_int_cst (type, 0);
6241 /* [expr.reinterpret.cast]
6242 A value of integral or enumeration type can be explicitly
6243 converted to a pointer. */
6244 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6247 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6248 || TYPE_PTR_P (type) || TYPE_PTR_TO_MEMBER_P (type))
6249 && same_type_p (type, intype))
6251 return fold_if_not_in_template (build_nop (type, expr));
6252 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6253 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6254 return fold_if_not_in_template (build_nop (type, expr));
6255 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
6256 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6261 && check_for_casting_away_constness (intype, type,
6262 REINTERPRET_CAST_EXPR,
6264 return error_mark_node;
6265 /* Warn about possible alignment problems. */
6266 if (STRICT_ALIGNMENT && warn_cast_align
6267 && (complain & tf_warning)
6268 && !VOID_TYPE_P (type)
6269 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6270 && COMPLETE_TYPE_P (TREE_TYPE (type))
6271 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6272 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6273 warning (OPT_Wcast_align, "cast from %qT to %qT "
6274 "increases required alignment of target type", intype, type);
6276 /* We need to strip nops here, because the front end likes to
6277 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6279 if (warn_strict_aliasing <= 2)
6280 strict_aliasing_warning (intype, type, sexpr);
6282 return fold_if_not_in_template (build_nop (type, expr));
6284 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6285 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6287 if (pedantic && (complain & tf_warning))
6288 /* Only issue a warning, as we have always supported this
6289 where possible, and it is necessary in some cases. DR 195
6290 addresses this issue, but as of 2004/10/26 is still in
6292 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6293 return fold_if_not_in_template (build_nop (type, expr));
6295 else if (TREE_CODE (type) == VECTOR_TYPE)
6296 return fold_if_not_in_template (convert_to_vector (type, expr));
6297 else if (TREE_CODE (intype) == VECTOR_TYPE
6298 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6299 return fold_if_not_in_template (convert_to_integer (type, expr));
6304 if (complain & tf_error)
6305 error ("invalid cast from type %qT to type %qT", intype, type);
6306 return error_mark_node;
6309 return cp_convert (type, expr);
6313 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6315 if (type == error_mark_node || expr == error_mark_node)
6316 return error_mark_node;
6318 if (processing_template_decl)
6320 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6322 if (!TREE_SIDE_EFFECTS (t)
6323 && type_dependent_expression_p (expr))
6324 /* There might turn out to be side effects inside expr. */
6325 TREE_SIDE_EFFECTS (t) = 1;
6326 return convert_from_reference (t);
6329 return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6330 /*valid_p=*/NULL, complain);
6333 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6334 return an appropriate expression. Otherwise, return
6335 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6336 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6337 performing a C-style cast, its value upon return will indicate
6338 whether or not the conversion succeeded. */
6341 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6345 tree reference_type;
6347 /* Callers are responsible for handling error_mark_node as a
6348 destination type. */
6349 gcc_assert (dst_type != error_mark_node);
6350 /* In a template, callers should be building syntactic
6351 representations of casts, not using this machinery. */
6352 gcc_assert (!processing_template_decl);
6354 /* Assume the conversion is invalid. */
6358 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
6360 if (complain & tf_error)
6361 error ("invalid use of const_cast with type %qT, "
6362 "which is not a pointer, "
6363 "reference, nor a pointer-to-data-member type", dst_type);
6364 return error_mark_node;
6367 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6369 if (complain & tf_error)
6370 error ("invalid use of const_cast with type %qT, which is a pointer "
6371 "or reference to a function type", dst_type);
6372 return error_mark_node;
6375 /* Save casted types in the function's used types hash table. */
6376 used_types_insert (dst_type);
6378 src_type = TREE_TYPE (expr);
6379 /* Expressions do not really have reference types. */
6380 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6381 src_type = TREE_TYPE (src_type);
6383 /* [expr.const.cast]
6385 For two object types T1 and T2, if a pointer to T1 can be explicitly
6386 converted to the type "pointer to T2" using a const_cast, then the
6387 following conversions can also be made:
6389 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6390 type T2 using the cast const_cast<T2&>;
6392 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6393 type T2 using the cast const_cast<T2&&>; and
6395 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6396 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6398 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6400 reference_type = dst_type;
6401 if (!TYPE_REF_IS_RVALUE (dst_type)
6402 ? real_lvalue_p (expr)
6403 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6405 : lvalue_or_rvalue_with_address_p (expr)))
6409 if (complain & tf_error)
6410 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6411 src_type, dst_type);
6412 return error_mark_node;
6414 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6415 src_type = build_pointer_type (src_type);
6419 reference_type = NULL_TREE;
6420 /* If the destination type is not a reference type, the
6421 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6422 conversions are performed. */
6423 src_type = type_decays_to (src_type);
6424 if (src_type == error_mark_node)
6425 return error_mark_node;
6428 if (TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
6430 if (comp_ptr_ttypes_const (dst_type, src_type))
6435 /* This cast is actually a C-style cast. Issue a warning if
6436 the user is making a potentially unsafe cast. */
6437 check_for_casting_away_constness (src_type, dst_type,
6438 CAST_EXPR, complain);
6442 expr = cp_build_addr_expr (expr, complain);
6443 expr = build_nop (reference_type, expr);
6444 return convert_from_reference (expr);
6448 expr = decay_conversion (expr);
6449 /* build_c_cast puts on a NOP_EXPR to make the result not an
6450 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6451 non-lvalue context. */
6452 if (TREE_CODE (expr) == NOP_EXPR
6453 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6454 expr = TREE_OPERAND (expr, 0);
6455 return build_nop (dst_type, expr);
6459 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6460 TREE_TYPE (src_type)))
6461 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6465 if (complain & tf_error)
6466 error ("invalid const_cast from type %qT to type %qT",
6467 src_type, dst_type);
6468 return error_mark_node;
6472 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6474 if (type == error_mark_node || error_operand_p (expr))
6475 return error_mark_node;
6477 if (processing_template_decl)
6479 tree t = build_min (CONST_CAST_EXPR, type, expr);
6481 if (!TREE_SIDE_EFFECTS (t)
6482 && type_dependent_expression_p (expr))
6483 /* There might turn out to be side effects inside expr. */
6484 TREE_SIDE_EFFECTS (t) = 1;
6485 return convert_from_reference (t);
6488 return build_const_cast_1 (type, expr, complain,
6492 /* Like cp_build_c_cast, but for the c-common bits. */
6495 build_c_cast (location_t loc ATTRIBUTE_UNUSED, tree type, tree expr)
6497 return cp_build_c_cast (type, expr, tf_warning_or_error);
6500 /* Build an expression representing an explicit C-style cast to type
6501 TYPE of expression EXPR. */
6504 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6510 if (type == error_mark_node || error_operand_p (expr))
6511 return error_mark_node;
6513 if (processing_template_decl)
6515 tree t = build_min (CAST_EXPR, type,
6516 tree_cons (NULL_TREE, value, NULL_TREE));
6517 /* We don't know if it will or will not have side effects. */
6518 TREE_SIDE_EFFECTS (t) = 1;
6519 return convert_from_reference (t);
6522 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6523 'Class') should always be retained, because this information aids
6524 in method lookup. */
6525 if (objc_is_object_ptr (type)
6526 && objc_is_object_ptr (TREE_TYPE (expr)))
6527 return build_nop (type, expr);
6529 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6530 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6531 if (TREE_CODE (type) != REFERENCE_TYPE
6532 && TREE_CODE (value) == NOP_EXPR
6533 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6534 value = TREE_OPERAND (value, 0);
6536 if (TREE_CODE (type) == ARRAY_TYPE)
6538 /* Allow casting from T1* to T2[] because Cfront allows it.
6539 NIHCL uses it. It is not valid ISO C++ however. */
6540 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6542 if (complain & tf_error)
6543 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6545 return error_mark_node;
6546 type = build_pointer_type (TREE_TYPE (type));
6550 if (complain & tf_error)
6551 error ("ISO C++ forbids casting to an array type %qT", type);
6552 return error_mark_node;
6556 if (TREE_CODE (type) == FUNCTION_TYPE
6557 || TREE_CODE (type) == METHOD_TYPE)
6559 if (complain & tf_error)
6560 error ("invalid cast to function type %qT", type);
6561 return error_mark_node;
6564 if (TREE_CODE (type) == POINTER_TYPE
6565 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6566 /* Casting to an integer of smaller size is an error detected elsewhere. */
6567 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6568 /* Don't warn about converting any constant. */
6569 && !TREE_CONSTANT (value))
6570 warning_at (input_location, OPT_Wint_to_pointer_cast,
6571 "cast to pointer from integer of different size");
6573 /* A C-style cast can be a const_cast. */
6574 result = build_const_cast_1 (type, value, complain & tf_warning,
6579 /* Or a static cast. */
6580 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6581 &valid_p, complain);
6582 /* Or a reinterpret_cast. */
6584 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6585 &valid_p, complain);
6586 /* The static_cast or reinterpret_cast may be followed by a
6589 /* A valid cast may result in errors if, for example, a
6590 conversion to am ambiguous base class is required. */
6591 && !error_operand_p (result))
6595 /* Non-class rvalues always have cv-unqualified type. */
6596 if (!CLASS_TYPE_P (type))
6597 type = TYPE_MAIN_VARIANT (type);
6598 result_type = TREE_TYPE (result);
6599 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
6600 result_type = TYPE_MAIN_VARIANT (result_type);
6601 /* If the type of RESULT does not match TYPE, perform a
6602 const_cast to make it match. If the static_cast or
6603 reinterpret_cast succeeded, we will differ by at most
6604 cv-qualification, so the follow-on const_cast is guaranteed
6606 if (!same_type_p (non_reference (type), non_reference (result_type)))
6608 result = build_const_cast_1 (type, result, false, &valid_p);
6609 gcc_assert (valid_p);
6614 return error_mark_node;
6617 /* For use from the C common bits. */
6619 build_modify_expr (location_t location ATTRIBUTE_UNUSED,
6620 tree lhs, tree lhs_origtype ATTRIBUTE_UNUSED,
6621 enum tree_code modifycode,
6622 location_t rhs_location ATTRIBUTE_UNUSED, tree rhs,
6623 tree rhs_origtype ATTRIBUTE_UNUSED)
6625 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6628 /* Build an assignment expression of lvalue LHS from value RHS.
6629 MODIFYCODE is the code for a binary operator that we use
6630 to combine the old value of LHS with RHS to get the new value.
6631 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6633 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
6636 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6637 tsubst_flags_t complain)
6641 tree lhstype = TREE_TYPE (lhs);
6642 tree olhstype = lhstype;
6643 bool plain_assign = (modifycode == NOP_EXPR);
6645 /* Avoid duplicate error messages from operands that had errors. */
6646 if (error_operand_p (lhs) || error_operand_p (rhs))
6647 return error_mark_node;
6649 /* Handle control structure constructs used as "lvalues". */
6650 switch (TREE_CODE (lhs))
6652 /* Handle --foo = 5; as these are valid constructs in C++. */
6653 case PREDECREMENT_EXPR:
6654 case PREINCREMENT_EXPR:
6655 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6656 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6657 stabilize_reference (TREE_OPERAND (lhs, 0)),
6658 TREE_OPERAND (lhs, 1));
6659 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
6660 modifycode, rhs, complain);
6661 if (newrhs == error_mark_node)
6662 return error_mark_node;
6663 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6665 /* Handle (a, b) used as an "lvalue". */
6667 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6668 modifycode, rhs, complain);
6669 if (newrhs == error_mark_node)
6670 return error_mark_node;
6671 return build2 (COMPOUND_EXPR, lhstype,
6672 TREE_OPERAND (lhs, 0), newrhs);
6675 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6676 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6677 stabilize_reference (TREE_OPERAND (lhs, 0)),
6678 TREE_OPERAND (lhs, 1));
6679 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
6681 if (newrhs == error_mark_node)
6682 return error_mark_node;
6683 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6687 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
6688 when neither operand has side-effects. */
6689 if (!lvalue_or_else (lhs, lv_assign, complain))
6690 return error_mark_node;
6692 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
6693 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
6695 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
6696 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
6698 TREE_OPERAND (lhs, 0),
6699 TREE_OPERAND (lhs, 1)),
6700 TREE_OPERAND (lhs, 0),
6701 TREE_OPERAND (lhs, 1));
6704 /* Handle (a ? b : c) used as an "lvalue". */
6707 /* Produce (a ? (b = rhs) : (c = rhs))
6708 except that the RHS goes through a save-expr
6709 so the code to compute it is only emitted once. */
6711 tree preeval = NULL_TREE;
6713 if (VOID_TYPE_P (TREE_TYPE (rhs)))
6715 if (complain & tf_error)
6716 error ("void value not ignored as it ought to be");
6717 return error_mark_node;
6720 rhs = stabilize_expr (rhs, &preeval);
6722 /* Check this here to avoid odd errors when trying to convert
6723 a throw to the type of the COND_EXPR. */
6724 if (!lvalue_or_else (lhs, lv_assign, complain))
6725 return error_mark_node;
6727 cond = build_conditional_expr
6728 (TREE_OPERAND (lhs, 0),
6729 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6730 modifycode, rhs, complain),
6731 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
6732 modifycode, rhs, complain),
6735 if (cond == error_mark_node)
6737 /* Make sure the code to compute the rhs comes out
6738 before the split. */
6740 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
6748 if (modifycode == INIT_EXPR)
6750 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6751 /* Do the default thing. */;
6752 else if (TREE_CODE (rhs) == CONSTRUCTOR)
6754 /* Compound literal. */
6755 if (! same_type_p (TREE_TYPE (rhs), lhstype))
6756 /* Call convert to generate an error; see PR 11063. */
6757 rhs = convert (lhstype, rhs);
6758 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
6759 TREE_SIDE_EFFECTS (result) = 1;
6762 else if (! MAYBE_CLASS_TYPE_P (lhstype))
6763 /* Do the default thing. */;
6766 VEC(tree,gc) *rhs_vec = make_tree_vector_single (rhs);
6767 result = build_special_member_call (lhs, complete_ctor_identifier,
6768 &rhs_vec, lhstype, LOOKUP_NORMAL,
6770 release_tree_vector (rhs_vec);
6771 if (result == NULL_TREE)
6772 return error_mark_node;
6778 lhs = require_complete_type_sfinae (lhs, complain);
6779 if (lhs == error_mark_node)
6780 return error_mark_node;
6782 if (modifycode == NOP_EXPR)
6784 if (c_dialect_objc ())
6786 result = objc_maybe_build_modify_expr (lhs, rhs);
6791 /* `operator=' is not an inheritable operator. */
6792 if (! MAYBE_CLASS_TYPE_P (lhstype))
6793 /* Do the default thing. */;
6796 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
6797 lhs, rhs, make_node (NOP_EXPR),
6800 if (result == NULL_TREE)
6801 return error_mark_node;
6808 tree init = NULL_TREE;
6810 /* A binary op has been requested. Combine the old LHS
6811 value with the RHS producing the value we should actually
6812 store into the LHS. */
6813 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
6814 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
6815 || MAYBE_CLASS_TYPE_P (lhstype)));
6817 /* Preevaluate the RHS to make sure its evaluation is complete
6818 before the lvalue-to-rvalue conversion of the LHS:
6820 [expr.ass] With respect to an indeterminately-sequenced
6821 function call, the operation of a compound assignment is a
6822 single evaluation. [ Note: Therefore, a function call shall
6823 not intervene between the lvalue-to-rvalue conversion and the
6824 side effect associated with any single compound assignment
6825 operator. -- end note ] */
6826 lhs = stabilize_reference (lhs);
6827 if (TREE_SIDE_EFFECTS (rhs))
6828 rhs = mark_rvalue_use (rhs);
6829 rhs = stabilize_expr (rhs, &init);
6830 newrhs = cp_build_binary_op (input_location,
6831 modifycode, lhs, rhs,
6833 if (newrhs == error_mark_node)
6835 if (complain & tf_error)
6836 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
6837 TREE_TYPE (lhs), TREE_TYPE (rhs));
6838 return error_mark_node;
6842 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
6844 /* Now it looks like a plain assignment. */
6845 modifycode = NOP_EXPR;
6846 if (c_dialect_objc ())
6848 result = objc_maybe_build_modify_expr (lhs, newrhs);
6853 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
6854 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
6857 /* The left-hand side must be an lvalue. */
6858 if (!lvalue_or_else (lhs, lv_assign, complain))
6859 return error_mark_node;
6861 /* Warn about modifying something that is `const'. Don't warn if
6862 this is initialization. */
6863 if (modifycode != INIT_EXPR
6864 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
6865 /* Functions are not modifiable, even though they are
6867 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
6868 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
6869 /* If it's an aggregate and any field is const, then it is
6870 effectively const. */
6871 || (CLASS_TYPE_P (lhstype)
6872 && C_TYPE_FIELDS_READONLY (lhstype))))
6874 if (complain & tf_error)
6875 cxx_readonly_error (lhs, lv_assign);
6877 return error_mark_node;
6880 /* If storing into a structure or union member, it may have been given a
6881 lowered bitfield type. We need to convert to the declared type first,
6882 so retrieve it now. */
6884 olhstype = unlowered_expr_type (lhs);
6886 /* Convert new value to destination type. */
6888 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6892 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
6894 if (modifycode != INIT_EXPR)
6896 if (complain & tf_error)
6897 error ("assigning to an array from an initializer list");
6898 return error_mark_node;
6900 if (check_array_initializer (lhs, lhstype, newrhs))
6901 return error_mark_node;
6902 newrhs = digest_init (lhstype, newrhs, complain);
6903 if (newrhs == error_mark_node)
6904 return error_mark_node;
6907 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
6908 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
6910 if (complain & tf_error)
6911 error ("incompatible types in assignment of %qT to %qT",
6912 TREE_TYPE (rhs), lhstype);
6913 return error_mark_node;
6916 /* Allow array assignment in compiler-generated code. */
6917 else if (!current_function_decl
6918 || !DECL_DEFAULTED_FN (current_function_decl))
6920 /* This routine is used for both initialization and assignment.
6921 Make sure the diagnostic message differentiates the context. */
6922 if (complain & tf_error)
6924 if (modifycode == INIT_EXPR)
6925 error ("array used as initializer");
6927 error ("invalid array assignment");
6929 return error_mark_node;
6932 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6933 ? 1 + (modifycode != INIT_EXPR): 0;
6934 return build_vec_init (lhs, NULL_TREE, newrhs,
6935 /*explicit_value_init_p=*/false,
6936 from_array, complain);
6939 if (modifycode == INIT_EXPR)
6940 /* Calls with INIT_EXPR are all direct-initialization, so don't set
6941 LOOKUP_ONLYCONVERTING. */
6942 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
6943 ICR_INIT, NULL_TREE, 0,
6946 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
6947 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
6949 if (!same_type_p (lhstype, olhstype))
6950 newrhs = cp_convert_and_check (lhstype, newrhs);
6952 if (modifycode != INIT_EXPR)
6954 if (TREE_CODE (newrhs) == CALL_EXPR
6955 && TYPE_NEEDS_CONSTRUCTING (lhstype))
6956 newrhs = build_cplus_new (lhstype, newrhs, complain);
6958 /* Can't initialize directly from a TARGET_EXPR, since that would
6959 cause the lhs to be constructed twice, and possibly result in
6960 accidental self-initialization. So we force the TARGET_EXPR to be
6961 expanded without a target. */
6962 if (TREE_CODE (newrhs) == TARGET_EXPR)
6963 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6964 TREE_OPERAND (newrhs, 0));
6967 if (newrhs == error_mark_node)
6968 return error_mark_node;
6970 if (c_dialect_objc () && flag_objc_gc)
6972 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6978 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6979 lhstype, lhs, newrhs);
6981 TREE_SIDE_EFFECTS (result) = 1;
6983 TREE_NO_WARNING (result) = 1;
6989 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6990 tsubst_flags_t complain)
6992 if (processing_template_decl)
6993 return build_min_nt (MODOP_EXPR, lhs,
6994 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6996 if (modifycode != NOP_EXPR)
6998 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6999 make_node (modifycode),
7004 TREE_NO_WARNING (rval) = 1;
7008 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7011 /* Helper function for get_delta_difference which assumes FROM is a base
7012 class of TO. Returns a delta for the conversion of pointer-to-member
7013 of FROM to pointer-to-member of TO. If the conversion is invalid and
7014 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7015 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7016 If C_CAST_P is true, this conversion is taking place as part of a
7020 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7021 tsubst_flags_t complain)
7025 base_access access = c_cast_p ? ba_unique : ba_check;
7027 /* Note: ba_quiet does not distinguish between access control and
7029 if (!(complain & tf_error))
7032 binfo = lookup_base (to, from, access, &kind);
7034 if (kind == bk_inaccessible || kind == bk_ambig)
7036 if (!(complain & tf_error))
7037 return error_mark_node;
7039 error (" in pointer to member function conversion");
7040 return size_zero_node;
7044 if (kind != bk_via_virtual)
7045 return BINFO_OFFSET (binfo);
7047 /* FROM is a virtual base class of TO. Issue an error or warning
7048 depending on whether or not this is a reinterpret cast. */
7050 if (!(complain & tf_error))
7051 return error_mark_node;
7053 error ("pointer to member conversion via virtual base %qT",
7054 BINFO_TYPE (binfo_from_vbase (binfo)));
7056 return size_zero_node;
7063 /* Get difference in deltas for different pointer to member function
7064 types. If the conversion is invalid and tf_error is not set in
7065 COMPLAIN, returns error_mark_node, otherwise returns an integer
7066 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7067 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7068 conversions as well. If C_CAST_P is true this conversion is taking
7069 place as part of a C-style cast.
7071 Note that the naming of FROM and TO is kind of backwards; the return
7072 value is what we add to a TO in order to get a FROM. They are named
7073 this way because we call this function to find out how to convert from
7074 a pointer to member of FROM to a pointer to member of TO. */
7077 get_delta_difference (tree from, tree to,
7078 bool allow_inverse_p,
7079 bool c_cast_p, tsubst_flags_t complain)
7083 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7084 /* Pointer to member of incomplete class is permitted*/
7085 result = size_zero_node;
7087 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7089 if (result == error_mark_node)
7090 return error_mark_node;
7094 if (!allow_inverse_p)
7096 if (!(complain & tf_error))
7097 return error_mark_node;
7099 error_not_base_type (from, to);
7100 error (" in pointer to member conversion");
7101 result = size_zero_node;
7105 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7107 if (result == error_mark_node)
7108 return error_mark_node;
7111 result = size_diffop_loc (input_location,
7112 size_zero_node, result);
7115 if (!(complain & tf_error))
7116 return error_mark_node;
7118 error_not_base_type (from, to);
7119 error (" in pointer to member conversion");
7120 result = size_zero_node;
7125 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7129 /* Return a constructor for the pointer-to-member-function TYPE using
7130 the other components as specified. */
7133 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7138 VEC(constructor_elt, gc) *v;
7140 /* Pull the FIELD_DECLs out of the type. */
7141 pfn_field = TYPE_FIELDS (type);
7142 delta_field = DECL_CHAIN (pfn_field);
7144 /* Make sure DELTA has the type we want. */
7145 delta = convert_and_check (delta_type_node, delta);
7147 /* Convert to the correct target type if necessary. */
7148 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7150 /* Finish creating the initializer. */
7151 v = VEC_alloc(constructor_elt, gc, 2);
7152 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7153 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7154 u = build_constructor (type, v);
7155 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7156 TREE_STATIC (u) = (TREE_CONSTANT (u)
7157 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7159 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7164 /* Build a constructor for a pointer to member function. It can be
7165 used to initialize global variables, local variable, or used
7166 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7169 If FORCE is nonzero, then force this conversion, even if
7170 we would rather not do it. Usually set when using an explicit
7171 cast. A C-style cast is being processed iff C_CAST_P is true.
7173 Return error_mark_node, if something goes wrong. */
7176 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7177 tsubst_flags_t complain)
7183 if (error_operand_p (pfn))
7184 return error_mark_node;
7186 pfn_type = TREE_TYPE (pfn);
7187 to_type = build_ptrmemfunc_type (type);
7189 /* Handle multiple conversions of pointer to member functions. */
7190 if (TYPE_PTRMEMFUNC_P (pfn_type))
7192 tree delta = NULL_TREE;
7193 tree npfn = NULL_TREE;
7197 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, LOOKUP_NORMAL))
7198 error ("invalid conversion to type %qT from type %qT",
7201 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7202 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7204 c_cast_p, complain);
7205 if (n == error_mark_node)
7206 return error_mark_node;
7208 /* We don't have to do any conversion to convert a
7209 pointer-to-member to its own type. But, we don't want to
7210 just return a PTRMEM_CST if there's an explicit cast; that
7211 cast should make the expression an invalid template argument. */
7212 if (TREE_CODE (pfn) != PTRMEM_CST)
7214 if (same_type_p (to_type, pfn_type))
7216 else if (integer_zerop (n))
7217 return build_reinterpret_cast (to_type, pfn,
7218 tf_warning_or_error);
7221 if (TREE_SIDE_EFFECTS (pfn))
7222 pfn = save_expr (pfn);
7224 /* Obtain the function pointer and the current DELTA. */
7225 if (TREE_CODE (pfn) == PTRMEM_CST)
7226 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7229 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7230 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7233 /* Just adjust the DELTA field. */
7234 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7235 (TREE_TYPE (delta), ptrdiff_type_node));
7236 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7237 n = cp_build_binary_op (input_location,
7238 LSHIFT_EXPR, n, integer_one_node,
7239 tf_warning_or_error);
7240 delta = cp_build_binary_op (input_location,
7241 PLUS_EXPR, delta, n, tf_warning_or_error);
7242 return build_ptrmemfunc1 (to_type, delta, npfn);
7245 /* Handle null pointer to member function conversions. */
7246 if (null_ptr_cst_p (pfn))
7248 pfn = build_c_cast (input_location, type, nullptr_node);
7249 return build_ptrmemfunc1 (to_type,
7254 if (type_unknown_p (pfn))
7255 return instantiate_type (type, pfn, tf_warning_or_error);
7257 fn = TREE_OPERAND (pfn, 0);
7258 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7259 /* In a template, we will have preserved the
7261 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7262 return make_ptrmem_cst (to_type, fn);
7265 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7268 ??? There is no consistency as to the types returned for the above
7269 values. Some code acts as if it were a sizetype and some as if it were
7270 integer_type_node. */
7273 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7275 tree type = TREE_TYPE (cst);
7276 tree fn = PTRMEM_CST_MEMBER (cst);
7277 tree ptr_class, fn_class;
7279 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7281 /* The class that the function belongs to. */
7282 fn_class = DECL_CONTEXT (fn);
7284 /* The class that we're creating a pointer to member of. */
7285 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7287 /* First, calculate the adjustment to the function's class. */
7288 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7289 /*c_cast_p=*/0, tf_warning_or_error);
7291 if (!DECL_VIRTUAL_P (fn))
7292 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
7295 /* If we're dealing with a virtual function, we have to adjust 'this'
7296 again, to point to the base which provides the vtable entry for
7297 fn; the call will do the opposite adjustment. */
7298 tree orig_class = DECL_CONTEXT (fn);
7299 tree binfo = binfo_or_else (orig_class, fn_class);
7300 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7301 *delta, BINFO_OFFSET (binfo));
7302 *delta = fold_if_not_in_template (*delta);
7304 /* We set PFN to the vtable offset at which the function can be
7305 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7306 case delta is shifted left, and then incremented). */
7307 *pfn = DECL_VINDEX (fn);
7308 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7309 TYPE_SIZE_UNIT (vtable_entry_type));
7310 *pfn = fold_if_not_in_template (*pfn);
7312 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7314 case ptrmemfunc_vbit_in_pfn:
7315 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7317 *pfn = fold_if_not_in_template (*pfn);
7320 case ptrmemfunc_vbit_in_delta:
7321 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7322 *delta, integer_one_node);
7323 *delta = fold_if_not_in_template (*delta);
7324 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7325 *delta, integer_one_node);
7326 *delta = fold_if_not_in_template (*delta);
7333 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7334 *pfn = fold_if_not_in_template (*pfn);
7338 /* Return an expression for PFN from the pointer-to-member function
7342 pfn_from_ptrmemfunc (tree t)
7344 if (TREE_CODE (t) == PTRMEM_CST)
7349 expand_ptrmemfunc_cst (t, &delta, &pfn);
7354 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7357 /* Return an expression for DELTA from the pointer-to-member function
7361 delta_from_ptrmemfunc (tree t)
7363 if (TREE_CODE (t) == PTRMEM_CST)
7368 expand_ptrmemfunc_cst (t, &delta, &pfn);
7373 return build_ptrmemfunc_access_expr (t, delta_identifier);
7376 /* Convert value RHS to type TYPE as preparation for an assignment to
7377 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7378 implicit conversion is. If FNDECL is non-NULL, we are doing the
7379 conversion in order to pass the PARMNUMth argument of FNDECL.
7380 If FNDECL is NULL, we are doing the conversion in function pointer
7381 argument passing, conversion in initialization, etc. */
7384 convert_for_assignment (tree type, tree rhs,
7385 impl_conv_rhs errtype, tree fndecl, int parmnum,
7386 tsubst_flags_t complain, int flags)
7389 enum tree_code coder;
7391 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7392 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7393 rhs = TREE_OPERAND (rhs, 0);
7395 rhstype = TREE_TYPE (rhs);
7396 coder = TREE_CODE (rhstype);
7398 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7399 && vector_types_convertible_p (type, rhstype, true))
7401 rhs = mark_rvalue_use (rhs);
7402 return convert (type, rhs);
7405 if (rhs == error_mark_node || rhstype == error_mark_node)
7406 return error_mark_node;
7407 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7408 return error_mark_node;
7410 /* The RHS of an assignment cannot have void type. */
7411 if (coder == VOID_TYPE)
7413 if (complain & tf_error)
7414 error ("void value not ignored as it ought to be");
7415 return error_mark_node;
7418 /* Simplify the RHS if possible. */
7419 if (TREE_CODE (rhs) == CONST_DECL)
7420 rhs = DECL_INITIAL (rhs);
7422 if (c_dialect_objc ())
7426 tree rname = fndecl;
7437 selector = objc_message_selector ();
7439 if (selector && parmno > 1)
7446 if (objc_compare_types (type, rhstype, parmno, rname))
7448 rhs = mark_rvalue_use (rhs);
7449 return convert (type, rhs);
7455 The expression is implicitly converted (clause _conv_) to the
7456 cv-unqualified type of the left operand.
7458 We allow bad conversions here because by the time we get to this point
7459 we are committed to doing the conversion. If we end up doing a bad
7460 conversion, convert_like will complain. */
7461 if (!can_convert_arg_bad (type, rhstype, rhs, flags))
7463 /* When -Wno-pmf-conversions is use, we just silently allow
7464 conversions from pointers-to-members to plain pointers. If
7465 the conversion doesn't work, cp_convert will complain. */
7467 && TYPE_PTR_P (type)
7468 && TYPE_PTRMEMFUNC_P (rhstype))
7469 rhs = cp_convert (strip_top_quals (type), rhs);
7472 if (complain & tf_error)
7474 /* If the right-hand side has unknown type, then it is an
7475 overloaded function. Call instantiate_type to get error
7477 if (rhstype == unknown_type_node)
7478 instantiate_type (type, rhs, tf_warning_or_error);
7480 error ("cannot convert %qT to %qT for argument %qP to %qD",
7481 rhstype, type, parmnum, fndecl);
7485 case ICR_DEFAULT_ARGUMENT:
7486 error ("cannot convert %qT to %qT in default argument",
7490 error ("cannot convert %qT to %qT in argument passing",
7493 case ICR_CONVERTING:
7494 error ("cannot convert %qT to %qT",
7498 error ("cannot convert %qT to %qT in initialization",
7502 error ("cannot convert %qT to %qT in return",
7506 error ("cannot convert %qT to %qT in assignment",
7513 return error_mark_node;
7516 if (warn_missing_format_attribute)
7518 const enum tree_code codel = TREE_CODE (type);
7519 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7521 && check_missing_format_attribute (type, rhstype)
7522 && (complain & tf_warning))
7526 case ICR_DEFAULT_ARGUMENT:
7528 warning (OPT_Wmissing_format_attribute,
7529 "parameter %qP of %qD might be a candidate "
7530 "for a format attribute", parmnum, fndecl);
7532 warning (OPT_Wmissing_format_attribute,
7533 "parameter might be a candidate "
7534 "for a format attribute");
7536 case ICR_CONVERTING:
7537 warning (OPT_Wmissing_format_attribute,
7538 "target of conversion might be a candidate "
7539 "for a format attribute");
7542 warning (OPT_Wmissing_format_attribute,
7543 "target of initialization might be a candidate "
7544 "for a format attribute");
7547 warning (OPT_Wmissing_format_attribute,
7548 "return type might be a candidate "
7549 "for a format attribute");
7552 warning (OPT_Wmissing_format_attribute,
7553 "left-hand side of assignment might be a candidate "
7554 "for a format attribute");
7561 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7563 if (warn_parentheses
7564 && TREE_CODE (type) == BOOLEAN_TYPE
7565 && TREE_CODE (rhs) == MODIFY_EXPR
7566 && !TREE_NO_WARNING (rhs)
7567 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7568 && (complain & tf_warning))
7570 location_t loc = EXPR_LOC_OR_HERE (rhs);
7572 warning_at (loc, OPT_Wparentheses,
7573 "suggest parentheses around assignment used as truth value");
7574 TREE_NO_WARNING (rhs) = 1;
7577 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7581 /* Convert RHS to be of type TYPE.
7582 If EXP is nonzero, it is the target of the initialization.
7583 ERRTYPE indicates what kind of error the implicit conversion is.
7585 Two major differences between the behavior of
7586 `convert_for_assignment' and `convert_for_initialization'
7587 are that references are bashed in the former, while
7588 copied in the latter, and aggregates are assigned in
7589 the former (operator=) while initialized in the
7592 If using constructor make sure no conversion operator exists, if one does
7593 exist, an ambiguity exists.
7595 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
7598 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7599 impl_conv_rhs errtype, tree fndecl, int parmnum,
7600 tsubst_flags_t complain)
7602 enum tree_code codel = TREE_CODE (type);
7604 enum tree_code coder;
7606 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7607 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7608 if (TREE_CODE (rhs) == NOP_EXPR
7609 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7610 && codel != REFERENCE_TYPE)
7611 rhs = TREE_OPERAND (rhs, 0);
7613 if (type == error_mark_node
7614 || rhs == error_mark_node
7615 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7616 return error_mark_node;
7618 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7619 && TREE_CODE (type) != ARRAY_TYPE
7620 && (TREE_CODE (type) != REFERENCE_TYPE
7621 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7622 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7623 && (TREE_CODE (type) != REFERENCE_TYPE
7624 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7625 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7626 rhs = decay_conversion (rhs);
7628 rhstype = TREE_TYPE (rhs);
7629 coder = TREE_CODE (rhstype);
7631 if (coder == ERROR_MARK)
7632 return error_mark_node;
7634 /* We accept references to incomplete types, so we can
7635 return here before checking if RHS is of complete type. */
7637 if (codel == REFERENCE_TYPE)
7639 /* This should eventually happen in convert_arguments. */
7640 int savew = 0, savee = 0;
7643 savew = warningcount, savee = errorcount;
7644 rhs = initialize_reference (type, rhs, flags, complain);
7647 if (warningcount > savew)
7648 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7649 else if (errorcount > savee)
7650 error ("in passing argument %P of %q+D", parmnum, fndecl);
7656 exp = require_complete_type_sfinae (exp, complain);
7657 if (exp == error_mark_node)
7658 return error_mark_node;
7660 rhstype = non_reference (rhstype);
7662 type = complete_type (type);
7664 if (DIRECT_INIT_EXPR_P (type, rhs))
7665 /* Don't try to do copy-initialization if we already have
7666 direct-initialization. */
7669 if (MAYBE_CLASS_TYPE_P (type))
7670 return perform_implicit_conversion_flags (type, rhs, complain, flags);
7672 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
7676 /* If RETVAL is the address of, or a reference to, a local variable or
7677 temporary give an appropriate warning. */
7680 maybe_warn_about_returning_address_of_local (tree retval)
7682 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
7683 tree whats_returned = retval;
7687 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7688 whats_returned = TREE_OPERAND (whats_returned, 1);
7689 else if (CONVERT_EXPR_P (whats_returned)
7690 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
7691 whats_returned = TREE_OPERAND (whats_returned, 0);
7696 if (TREE_CODE (whats_returned) != ADDR_EXPR)
7698 whats_returned = TREE_OPERAND (whats_returned, 0);
7700 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7702 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7703 || TREE_CODE (whats_returned) == TARGET_EXPR)
7705 warning (0, "returning reference to temporary");
7708 if (TREE_CODE (whats_returned) == VAR_DECL
7709 && DECL_NAME (whats_returned)
7710 && TEMP_NAME_P (DECL_NAME (whats_returned)))
7712 warning (0, "reference to non-lvalue returned");
7717 while (TREE_CODE (whats_returned) == COMPONENT_REF
7718 || TREE_CODE (whats_returned) == ARRAY_REF)
7719 whats_returned = TREE_OPERAND (whats_returned, 0);
7721 if (DECL_P (whats_returned)
7722 && DECL_NAME (whats_returned)
7723 && DECL_FUNCTION_SCOPE_P (whats_returned)
7724 && !(TREE_STATIC (whats_returned)
7725 || TREE_PUBLIC (whats_returned)))
7727 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7728 warning (0, "reference to local variable %q+D returned",
7731 warning (0, "address of local variable %q+D returned",
7737 /* Check that returning RETVAL from the current function is valid.
7738 Return an expression explicitly showing all conversions required to
7739 change RETVAL into the function return type, and to assign it to
7740 the DECL_RESULT for the function. Set *NO_WARNING to true if
7741 code reaches end of non-void function warning shouldn't be issued
7742 on this RETURN_EXPR. */
7745 check_return_expr (tree retval, bool *no_warning)
7748 /* The type actually returned by the function, after any
7751 int fn_returns_value_p;
7752 bool named_return_value_okay_p;
7754 *no_warning = false;
7756 /* A `volatile' function is one that isn't supposed to return, ever.
7757 (This is a G++ extension, used to get better code for functions
7758 that call the `volatile' function.) */
7759 if (TREE_THIS_VOLATILE (current_function_decl))
7760 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7762 /* Check for various simple errors. */
7763 if (DECL_DESTRUCTOR_P (current_function_decl))
7766 error ("returning a value from a destructor");
7769 else if (DECL_CONSTRUCTOR_P (current_function_decl))
7771 if (in_function_try_handler)
7772 /* If a return statement appears in a handler of the
7773 function-try-block of a constructor, the program is ill-formed. */
7774 error ("cannot return from a handler of a function-try-block of a constructor");
7776 /* You can't return a value from a constructor. */
7777 error ("returning a value from a constructor");
7781 /* As an extension, deduce lambda return type from a return statement
7782 anywhere in the body. */
7783 if (retval && LAMBDA_FUNCTION_P (current_function_decl))
7785 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7786 if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda))
7788 tree type = lambda_return_type (retval);
7789 tree oldtype = LAMBDA_EXPR_RETURN_TYPE (lambda);
7791 if (oldtype == NULL_TREE)
7792 apply_lambda_return_type (lambda, type);
7793 /* If one of the answers is type-dependent, we can't do any
7794 better until instantiation time. */
7795 else if (oldtype == dependent_lambda_return_type_node)
7797 else if (type == dependent_lambda_return_type_node)
7798 apply_lambda_return_type (lambda, type);
7799 else if (!same_type_p (type, oldtype))
7800 error ("inconsistent types %qT and %qT deduced for "
7801 "lambda return type", type, oldtype);
7805 if (processing_template_decl)
7807 current_function_returns_value = 1;
7808 if (check_for_bare_parameter_packs (retval))
7809 retval = error_mark_node;
7813 /* When no explicit return-value is given in a function with a named
7814 return value, the named return value is used. */
7815 result = DECL_RESULT (current_function_decl);
7816 valtype = TREE_TYPE (result);
7817 gcc_assert (valtype != NULL_TREE);
7818 fn_returns_value_p = !VOID_TYPE_P (valtype);
7819 if (!retval && DECL_NAME (result) && fn_returns_value_p)
7822 /* Check for a return statement with no return value in a function
7823 that's supposed to return a value. */
7824 if (!retval && fn_returns_value_p)
7826 permerror (input_location, "return-statement with no value, in function returning %qT",
7828 /* Clear this, so finish_function won't say that we reach the
7829 end of a non-void function (which we don't, we gave a
7831 current_function_returns_null = 0;
7832 /* And signal caller that TREE_NO_WARNING should be set on the
7833 RETURN_EXPR to avoid control reaches end of non-void function
7834 warnings in tree-cfg.c. */
7837 /* Check for a return statement with a value in a function that
7838 isn't supposed to return a value. */
7839 else if (retval && !fn_returns_value_p)
7841 if (VOID_TYPE_P (TREE_TYPE (retval)))
7842 /* You can return a `void' value from a function of `void'
7843 type. In that case, we have to evaluate the expression for
7844 its side-effects. */
7845 finish_expr_stmt (retval);
7847 permerror (input_location, "return-statement with a value, in function "
7848 "returning 'void'");
7849 current_function_returns_null = 1;
7851 /* There's really no value to return, after all. */
7855 /* Remember that this function can sometimes return without a
7857 current_function_returns_null = 1;
7859 /* Remember that this function did return a value. */
7860 current_function_returns_value = 1;
7862 /* Check for erroneous operands -- but after giving ourselves a
7863 chance to provide an error about returning a value from a void
7865 if (error_operand_p (retval))
7867 current_function_return_value = error_mark_node;
7868 return error_mark_node;
7871 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
7872 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
7873 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
7874 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
7876 && retval && null_ptr_cst_p (retval))
7877 warning (0, "%<operator new%> must not return NULL unless it is "
7878 "declared %<throw()%> (or -fcheck-new is in effect)");
7880 /* Effective C++ rule 15. See also start_function. */
7882 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
7886 /* The function return type must be a reference to the current
7888 if (TREE_CODE (valtype) == REFERENCE_TYPE
7889 && same_type_ignoring_top_level_qualifiers_p
7890 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
7892 /* Returning '*this' is obviously OK. */
7893 if (retval == current_class_ref)
7895 /* If we are calling a function whose return type is the same of
7896 the current class reference, it is ok. */
7897 else if (TREE_CODE (retval) == INDIRECT_REF
7898 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
7903 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
7906 /* The fabled Named Return Value optimization, as per [class.copy]/15:
7908 [...] For a function with a class return type, if the expression
7909 in the return statement is the name of a local object, and the cv-
7910 unqualified type of the local object is the same as the function
7911 return type, an implementation is permitted to omit creating the tem-
7912 porary object to hold the function return value [...]
7914 So, if this is a value-returning function that always returns the same
7915 local variable, remember it.
7917 It might be nice to be more flexible, and choose the first suitable
7918 variable even if the function sometimes returns something else, but
7919 then we run the risk of clobbering the variable we chose if the other
7920 returned expression uses the chosen variable somehow. And people expect
7921 this restriction, anyway. (jason 2000-11-19)
7923 See finish_function and finalize_nrv for the rest of this optimization. */
7925 named_return_value_okay_p =
7926 (retval != NULL_TREE
7927 /* Must be a local, automatic variable. */
7928 && TREE_CODE (retval) == VAR_DECL
7929 && DECL_CONTEXT (retval) == current_function_decl
7930 && ! TREE_STATIC (retval)
7931 && ! DECL_ANON_UNION_VAR_P (retval)
7932 && (DECL_ALIGN (retval)
7933 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
7934 /* The cv-unqualified type of the returned value must be the
7935 same as the cv-unqualified return type of the
7937 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
7939 (TREE_TYPE (TREE_TYPE (current_function_decl)))))
7940 /* And the returned value must be non-volatile. */
7941 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
7943 if (fn_returns_value_p && flag_elide_constructors)
7945 if (named_return_value_okay_p
7946 && (current_function_return_value == NULL_TREE
7947 || current_function_return_value == retval))
7948 current_function_return_value = retval;
7950 current_function_return_value = error_mark_node;
7953 /* We don't need to do any conversions when there's nothing being
7958 /* Do any required conversions. */
7959 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
7960 /* No conversions are required. */
7964 /* The type the function is declared to return. */
7965 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7966 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
7968 /* The functype's return type will have been set to void, if it
7969 was an incomplete type. Just treat this as 'return;' */
7970 if (VOID_TYPE_P (functype))
7971 return error_mark_node;
7973 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
7974 treated as an rvalue for the purposes of overload resolution to
7975 favor move constructors over copy constructors.
7977 Note that these conditions are similar to, but not as strict as,
7978 the conditions for the named return value optimization. */
7979 if ((cxx_dialect != cxx98)
7980 && (TREE_CODE (retval) == VAR_DECL
7981 || TREE_CODE (retval) == PARM_DECL)
7982 && DECL_CONTEXT (retval) == current_function_decl
7983 && !TREE_STATIC (retval)
7984 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
7986 (TREE_TYPE (TREE_TYPE (current_function_decl)))))
7987 /* This is only interesting for class type. */
7988 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
7989 flags = flags | LOOKUP_PREFER_RVALUE;
7991 /* First convert the value to the function's return type, then
7992 to the type of return value's location to handle the
7993 case that functype is smaller than the valtype. */
7994 retval = convert_for_initialization
7995 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
7996 tf_warning_or_error);
7997 retval = convert (valtype, retval);
7999 /* If the conversion failed, treat this just like `return;'. */
8000 if (retval == error_mark_node)
8002 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8003 else if (! cfun->returns_struct
8004 && TREE_CODE (retval) == TARGET_EXPR
8005 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8006 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8007 TREE_OPERAND (retval, 0));
8009 maybe_warn_about_returning_address_of_local (retval);
8012 /* Actually copy the value returned into the appropriate location. */
8013 if (retval && retval != result)
8014 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8020 /* Returns nonzero if the pointer-type FROM can be converted to the
8021 pointer-type TO via a qualification conversion. If CONSTP is -1,
8022 then we return nonzero if the pointers are similar, and the
8023 cv-qualification signature of FROM is a proper subset of that of TO.
8025 If CONSTP is positive, then all outer pointers have been
8029 comp_ptr_ttypes_real (tree to, tree from, int constp)
8031 bool to_more_cv_qualified = false;
8032 bool is_opaque_pointer = false;
8034 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8036 if (TREE_CODE (to) != TREE_CODE (from))
8039 if (TREE_CODE (from) == OFFSET_TYPE
8040 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8041 TYPE_OFFSET_BASETYPE (to)))
8044 /* Const and volatile mean something different for function types,
8045 so the usual checks are not appropriate. */
8046 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8048 if (!at_least_as_qualified_p (to, from))
8051 if (!at_least_as_qualified_p (from, to))
8055 to_more_cv_qualified = true;
8059 constp &= TYPE_READONLY (to);
8062 if (TREE_CODE (to) == VECTOR_TYPE)
8063 is_opaque_pointer = vector_targets_convertible_p (to, from);
8065 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
8066 return ((constp >= 0 || to_more_cv_qualified)
8067 && (is_opaque_pointer
8068 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8072 /* When comparing, say, char ** to char const **, this function takes
8073 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8074 types to this function. */
8077 comp_ptr_ttypes (tree to, tree from)
8079 return comp_ptr_ttypes_real (to, from, 1);
8082 /* Returns true iff FNTYPE is a non-class type that involves
8083 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8084 if a parameter type is ill-formed. */
8087 error_type_p (const_tree type)
8091 switch (TREE_CODE (type))
8097 case REFERENCE_TYPE:
8099 return error_type_p (TREE_TYPE (type));
8103 if (error_type_p (TREE_TYPE (type)))
8105 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8106 if (error_type_p (TREE_VALUE (t)))
8111 if (TYPE_PTRMEMFUNC_P (type))
8112 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8120 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8121 type or inheritance-related types, regardless of cv-quals. */
8124 ptr_reasonably_similar (const_tree to, const_tree from)
8126 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8128 /* Any target type is similar enough to void. */
8129 if (TREE_CODE (to) == VOID_TYPE)
8130 return !error_type_p (from);
8131 if (TREE_CODE (from) == VOID_TYPE)
8132 return !error_type_p (to);
8134 if (TREE_CODE (to) != TREE_CODE (from))
8137 if (TREE_CODE (from) == OFFSET_TYPE
8138 && comptypes (TYPE_OFFSET_BASETYPE (to),
8139 TYPE_OFFSET_BASETYPE (from),
8140 COMPARE_BASE | COMPARE_DERIVED))
8143 if (TREE_CODE (to) == VECTOR_TYPE
8144 && vector_types_convertible_p (to, from, false))
8147 if (TREE_CODE (to) == INTEGER_TYPE
8148 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8151 if (TREE_CODE (to) == FUNCTION_TYPE)
8152 return !error_type_p (to) && !error_type_p (from);
8154 if (TREE_CODE (to) != POINTER_TYPE)
8156 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8157 COMPARE_BASE | COMPARE_DERIVED);
8161 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8162 pointer-to-member types) are the same, ignoring cv-qualification at
8166 comp_ptr_ttypes_const (tree to, tree from)
8168 bool is_opaque_pointer = false;
8170 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8172 if (TREE_CODE (to) != TREE_CODE (from))
8175 if (TREE_CODE (from) == OFFSET_TYPE
8176 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8177 TYPE_OFFSET_BASETYPE (to)))
8180 if (TREE_CODE (to) == VECTOR_TYPE)
8181 is_opaque_pointer = vector_targets_convertible_p (to, from);
8183 if (TREE_CODE (to) != POINTER_TYPE)
8184 return (is_opaque_pointer
8185 || same_type_ignoring_top_level_qualifiers_p (to, from));
8189 /* Returns the type qualifiers for this type, including the qualifiers on the
8190 elements for an array type. */
8193 cp_type_quals (const_tree type)
8196 /* This CONST_CAST is okay because strip_array_types returns its
8197 argument unmodified and we assign it to a const_tree. */
8198 type = strip_array_types (CONST_CAST_TREE (type));
8199 if (type == error_mark_node
8200 /* Quals on a FUNCTION_TYPE are memfn quals. */
8201 || TREE_CODE (type) == FUNCTION_TYPE)
8202 return TYPE_UNQUALIFIED;
8203 quals = TYPE_QUALS (type);
8204 /* METHOD and REFERENCE_TYPEs should never have quals. */
8205 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8206 && TREE_CODE (type) != REFERENCE_TYPE)
8207 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8208 == TYPE_UNQUALIFIED));
8212 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8216 type_memfn_quals (const_tree type)
8218 if (TREE_CODE (type) == FUNCTION_TYPE)
8219 return TYPE_QUALS (type);
8220 else if (TREE_CODE (type) == METHOD_TYPE)
8221 return cp_type_quals (class_of_this_parm (type));
8226 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8230 apply_memfn_quals (tree type, cp_cv_quals memfn_quals)
8232 /* Could handle METHOD_TYPE here if necessary. */
8233 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8234 if (TYPE_QUALS (type) == memfn_quals)
8236 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8238 return build_qualified_type (type, memfn_quals);
8241 /* Returns nonzero if TYPE is const or volatile. */
8244 cv_qualified_p (const_tree type)
8246 int quals = cp_type_quals (type);
8247 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8250 /* Returns nonzero if the TYPE contains a mutable member. */
8253 cp_has_mutable_p (const_tree type)
8255 /* This CONST_CAST is okay because strip_array_types returns its
8256 argument unmodified and we assign it to a const_tree. */
8257 type = strip_array_types (CONST_CAST_TREE(type));
8259 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8262 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8263 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8264 approximation. In particular, consider:
8267 struct S { int i; };
8268 const S s = { f(); }
8270 Here, we will make "s" as TREE_READONLY (because it is declared
8271 "const") -- only to reverse ourselves upon seeing that the
8272 initializer is non-constant. */
8275 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8277 tree type = TREE_TYPE (decl);
8279 if (type == error_mark_node)
8282 if (TREE_CODE (decl) == TYPE_DECL)
8285 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8286 && type_quals != TYPE_UNQUALIFIED));
8288 /* Avoid setting TREE_READONLY incorrectly. */
8289 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8290 constructor can produce constant init, so rely on cp_finish_decl to
8291 clear TREE_READONLY if the variable has non-constant init. */
8293 /* If the type has (or might have) a mutable component, that component
8294 might be modified. */
8295 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
8296 type_quals &= ~TYPE_QUAL_CONST;
8298 c_apply_type_quals_to_decl (type_quals, decl);
8301 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8302 exemplar types such that casting T1 to T2 is casting away constness
8303 if and only if there is no implicit conversion from T1 to T2. */
8306 casts_away_constness_r (tree *t1, tree *t2)
8311 /* [expr.const.cast]
8313 For multi-level pointer to members and multi-level mixed pointers
8314 and pointers to members (conv.qual), the "member" aspect of a
8315 pointer to member level is ignored when determining if a const
8316 cv-qualifier has been cast away. */
8317 /* [expr.const.cast]
8319 For two pointer types:
8321 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8322 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8325 casting from X1 to X2 casts away constness if, for a non-pointer
8326 type T there does not exist an implicit conversion (clause
8329 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8333 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8334 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1))
8335 || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2)))
8337 *t1 = cp_build_qualified_type (void_type_node,
8338 cp_type_quals (*t1));
8339 *t2 = cp_build_qualified_type (void_type_node,
8340 cp_type_quals (*t2));
8344 quals1 = cp_type_quals (*t1);
8345 quals2 = cp_type_quals (*t2);
8347 if (TYPE_PTRMEM_P (*t1))
8348 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8350 *t1 = TREE_TYPE (*t1);
8351 if (TYPE_PTRMEM_P (*t2))
8352 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8354 *t2 = TREE_TYPE (*t2);
8356 casts_away_constness_r (t1, t2);
8357 *t1 = build_pointer_type (*t1);
8358 *t2 = build_pointer_type (*t2);
8359 *t1 = cp_build_qualified_type (*t1, quals1);
8360 *t2 = cp_build_qualified_type (*t2, quals2);
8363 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8366 ??? This function returns non-zero if casting away qualifiers not
8367 just const. We would like to return to the caller exactly which
8368 qualifiers are casted away to give more accurate diagnostics.
8372 casts_away_constness (tree t1, tree t2)
8374 if (TREE_CODE (t2) == REFERENCE_TYPE)
8376 /* [expr.const.cast]
8378 Casting from an lvalue of type T1 to an lvalue of type T2
8379 using a reference cast casts away constness if a cast from an
8380 rvalue of type "pointer to T1" to the type "pointer to T2"
8381 casts away constness. */
8382 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8383 return casts_away_constness (build_pointer_type (t1),
8384 build_pointer_type (TREE_TYPE (t2)));
8387 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
8388 /* [expr.const.cast]
8390 Casting from an rvalue of type "pointer to data member of X
8391 of type T1" to the type "pointer to data member of Y of type
8392 T2" casts away constness if a cast from an rvalue of type
8393 "pointer to T1" to the type "pointer to T2" casts away
8395 return casts_away_constness
8396 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8397 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
8399 /* Casting away constness is only something that makes sense for
8400 pointer or reference types. */
8401 if (TREE_CODE (t1) != POINTER_TYPE
8402 || TREE_CODE (t2) != POINTER_TYPE)
8405 /* Top-level qualifiers don't matter. */
8406 t1 = TYPE_MAIN_VARIANT (t1);
8407 t2 = TYPE_MAIN_VARIANT (t2);
8408 casts_away_constness_r (&t1, &t2);
8409 if (!can_convert (t2, t1))
8415 /* If T is a REFERENCE_TYPE return the type to which T refers.
8416 Otherwise, return T itself. */
8419 non_reference (tree t)
8421 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8427 /* Return nonzero if REF is an lvalue valid for this language;
8428 otherwise, print an error message and return zero. USE says
8429 how the lvalue is being used and so selects the error message. */
8432 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8434 cp_lvalue_kind kind = lvalue_kind (ref);
8436 if (kind == clk_none)
8438 if (complain & tf_error)
8439 lvalue_error (input_location, use);
8442 else if (kind & (clk_rvalueref|clk_class))
8444 if (!(complain & tf_error))
8446 if (kind & clk_class)
8447 /* Make this a permerror because we used to accept it. */
8448 permerror (input_location, "using temporary as lvalue");
8450 error ("using xvalue (rvalue reference) as lvalue");
8455 /* Return true if a user-defined literal operator is a raw operator. */
8458 check_raw_literal_operator (const_tree decl)
8460 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8463 bool maybe_raw_p = false;
8465 /* Count the number and type of arguments and check for ellipsis. */
8466 for (argtype = argtypes, arity = 0;
8467 argtype && argtype != void_list_node;
8468 ++arity, argtype = TREE_CHAIN (argtype))
8470 tree t = TREE_VALUE (argtype);
8472 if (same_type_p (t, const_string_type_node))
8476 return false; /* Found ellipsis. */
8478 if (!maybe_raw_p || arity != 1)
8485 /* Return true if a user-defined literal operator has one of the allowed
8489 check_literal_operator_args (const_tree decl,
8490 bool *long_long_unsigned_p, bool *long_double_p)
8492 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8494 *long_long_unsigned_p = false;
8495 *long_double_p = false;
8496 if (processing_template_decl || processing_specialization)
8497 return argtypes == void_list_node;
8504 /* Count the number and type of arguments and check for ellipsis. */
8505 for (argtype = argtypes, arity = 0;
8506 argtype && argtype != void_list_node;
8507 argtype = TREE_CHAIN (argtype))
8509 tree t = TREE_VALUE (argtype);
8512 if (TREE_CODE (t) == POINTER_TYPE)
8514 bool maybe_raw_p = false;
8516 if (cp_type_quals (t) != TYPE_QUAL_CONST)
8518 t = TYPE_MAIN_VARIANT (t);
8519 if ((maybe_raw_p = same_type_p (t, char_type_node))
8520 || same_type_p (t, wchar_type_node)
8521 || same_type_p (t, char16_type_node)
8522 || same_type_p (t, char32_type_node))
8524 argtype = TREE_CHAIN (argtype);
8527 t = TREE_VALUE (argtype);
8528 if (maybe_raw_p && argtype == void_list_node)
8530 else if (same_type_p (t, size_type_node))
8539 else if (same_type_p (t, long_long_unsigned_type_node))
8542 *long_long_unsigned_p = true;
8544 else if (same_type_p (t, long_double_type_node))
8547 *long_double_p = true;
8549 else if (same_type_p (t, char_type_node))
8551 else if (same_type_p (t, wchar_type_node))
8553 else if (same_type_p (t, char16_type_node))
8555 else if (same_type_p (t, char32_type_node))
8561 return false; /* Found ellipsis. */
8563 if (arity != max_arity)