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, 2011
4 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* This file is part of the C++ front end.
25 It contains routines to build C++ expressions given their operands,
26 including computing the types of the result, C and C++ specific error
27 checks, and some optimization. */
31 #include "coretypes.h"
37 #include "diagnostic.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
45 static tree pfn_from_ptrmemfunc (tree);
46 static tree delta_from_ptrmemfunc (tree);
47 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 static tree cp_pointer_int_sum (enum tree_code, tree, tree);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (tree, tree, tree);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *);
58 static bool casts_away_constness (tree, tree);
59 static void maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree);
61 static void warn_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, VEC(tree,gc) **, tree, int,
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
75 if (processing_template_decl || value == error_mark_node)
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
90 if (complete_type_or_maybe_complain (type, value, complain))
93 return error_mark_node;
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp)
171 return (TREE_CODE (exp) == TREE_LIST
172 || TREE_TYPE (exp) == unknown_type_node);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
184 commonparms (tree p1, tree p2)
186 tree oldargs = p1, newargs, n;
190 len = list_length (p1);
191 newargs = tree_last (p1);
193 if (newargs == void_list_node)
202 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
207 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
209 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
214 else if (! TREE_PURPOSE (p1))
216 if (TREE_PURPOSE (p2))
218 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
226 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
228 if (TREE_VALUE (p1) != TREE_VALUE (p2))
231 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
234 TREE_VALUE (n) = TREE_VALUE (p1);
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
245 original_type (tree t)
247 int quals = cp_type_quals (t);
248 while (t != error_mark_node
249 && TYPE_NAME (t) != NULL_TREE)
251 tree x = TYPE_NAME (t);
252 if (TREE_CODE (x) != TYPE_DECL)
254 x = DECL_ORIGINAL_TYPE (x);
259 return cp_build_qualified_type (t, quals);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
268 cp_common_type (tree t1, tree t2)
270 enum tree_code code1 = TREE_CODE (t1);
271 enum tree_code code2 = TREE_CODE (t2);
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1)
290 || TREE_CODE (t1) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t1));
292 gcc_assert (ARITHMETIC_TYPE_P (t2)
293 || TREE_CODE (t2) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t2));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
311 return build_type_attribute_variant (build_complex_type (subtype),
315 if (code1 == VECTOR_TYPE)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
322 return build_type_attribute_variant (t2, attributes);
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
341 if (code1 != REAL_TYPE)
343 /* If one is unsigned long long, then convert the other to unsigned
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
367 if (int128_integer_type_node != NULL_TREE
368 && (same_type_p (TYPE_MAIN_VARIANT (t1),
369 int128_integer_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2),
371 int128_integer_type_node)))
373 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
374 ? int128_unsigned_type_node
375 : int128_integer_type_node);
376 return build_type_attribute_variant (t, attributes);
379 /* Go through the same procedure, but for longs. */
380 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
382 return build_type_attribute_variant (long_unsigned_type_node,
384 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? long_unsigned_type_node : long_integer_type_node);
389 return build_type_attribute_variant (t, attributes);
391 /* Otherwise prefer the unsigned one. */
392 if (TYPE_UNSIGNED (t1))
393 return build_type_attribute_variant (t1, attributes);
395 return build_type_attribute_variant (t2, attributes);
399 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
400 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
401 return build_type_attribute_variant (long_double_type_node,
403 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
405 return build_type_attribute_variant (double_type_node,
407 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
408 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
409 return build_type_attribute_variant (float_type_node,
412 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413 the standard C++ floating-point types. Logic earlier in this
414 function has already eliminated the possibility that
415 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416 compelling reason to choose one or the other. */
417 return build_type_attribute_variant (t1, attributes);
421 /* T1 and T2 are arithmetic or enumeration types. Return the type
422 that will result from the "usual arithmetic conversions" on T1 and
423 T2 as described in [expr]. */
426 type_after_usual_arithmetic_conversions (tree t1, tree t2)
428 gcc_assert (ARITHMETIC_TYPE_P (t1)
429 || TREE_CODE (t1) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t1));
431 gcc_assert (ARITHMETIC_TYPE_P (t2)
432 || TREE_CODE (t2) == VECTOR_TYPE
433 || UNSCOPED_ENUM_P (t2));
435 /* Perform the integral promotions. We do not promote real types here. */
436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
437 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
439 t1 = type_promotes_to (t1);
440 t2 = type_promotes_to (t2);
443 return cp_common_type (t1, t2);
447 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
448 composite_pointer_operation operation)
453 emit_diagnostic (kind, input_location, 0,
454 "comparison between "
455 "distinct pointer types %qT and %qT lacks a cast",
459 emit_diagnostic (kind, input_location, 0,
460 "conversion between "
461 "distinct pointer types %qT and %qT lacks a cast",
464 case CPO_CONDITIONAL_EXPR:
465 emit_diagnostic (kind, input_location, 0,
466 "conditional expression between "
467 "distinct pointer types %qT and %qT lacks a cast",
475 /* Subroutine of composite_pointer_type to implement the recursive
476 case. See that function for documentation of the parameters. */
479 composite_pointer_type_r (tree t1, tree t2,
480 composite_pointer_operation operation,
481 tsubst_flags_t complain)
488 /* Determine the types pointed to by T1 and T2. */
489 if (TREE_CODE (t1) == POINTER_TYPE)
491 pointee1 = TREE_TYPE (t1);
492 pointee2 = TREE_TYPE (t2);
496 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
497 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
502 Otherwise, the composite pointer type is a pointer type
503 similar (_conv.qual_) to the type of one of the operands,
504 with a cv-qualification signature (_conv.qual_) that is the
505 union of the cv-qualification signatures of the operand
507 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
508 result_type = pointee1;
509 else if ((TREE_CODE (pointee1) == POINTER_TYPE
510 && TREE_CODE (pointee2) == POINTER_TYPE)
511 || (TYPE_PTR_TO_MEMBER_P (pointee1)
512 && TYPE_PTR_TO_MEMBER_P (pointee2)))
514 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
516 if (result_type == error_mark_node)
517 return error_mark_node;
521 if (complain & tf_error)
522 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
524 return error_mark_node;
525 result_type = void_type_node;
527 result_type = cp_build_qualified_type (result_type,
528 (cp_type_quals (pointee1)
529 | cp_type_quals (pointee2)));
530 /* If the original types were pointers to members, so is the
532 if (TYPE_PTR_TO_MEMBER_P (t1))
534 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
535 TYPE_PTRMEM_CLASS_TYPE (t2)))
537 if (complain & tf_error)
538 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
540 return error_mark_node;
542 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
546 result_type = build_pointer_type (result_type);
548 /* Merge the attributes. */
549 attributes = (*targetm.merge_type_attributes) (t1, t2);
550 return build_type_attribute_variant (result_type, attributes);
553 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
554 ARG1 and ARG2 are the values with those types. The OPERATION is to
555 describe the operation between the pointer types,
556 in case an error occurs.
558 This routine also implements the computation of a common type for
559 pointers-to-members as per [expr.eq]. */
562 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
563 composite_pointer_operation operation,
564 tsubst_flags_t complain)
571 If one operand is a null pointer constant, the composite pointer
572 type is the type of the other operand. */
573 if (null_ptr_cst_p (arg1))
575 if (null_ptr_cst_p (arg2))
582 If one of the operands has type "pointer to cv1 void*", then
583 the other has type "pointer to cv2T", and the composite pointer
584 type is "pointer to cv12 void", where cv12 is the union of cv1
587 If either type is a pointer to void, make sure it is T1. */
588 if (TREE_CODE (t2) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t2)))
596 /* Now, if T1 is a pointer to void, merge the qualifiers. */
597 if (TREE_CODE (t1) == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (t1)))
602 if (TYPE_PTRFN_P (t2) && (complain & tf_error))
607 pedwarn (input_location, OPT_pedantic,
608 "ISO C++ forbids comparison between "
609 "pointer of type %<void *%> and pointer-to-function");
612 pedwarn (input_location, OPT_pedantic,
613 "ISO C++ forbids conversion between "
614 "pointer of type %<void *%> and pointer-to-function");
616 case CPO_CONDITIONAL_EXPR:
617 pedwarn (input_location, OPT_pedantic,
618 "ISO C++ forbids conditional expression between "
619 "pointer of type %<void *%> and pointer-to-function");
626 = cp_build_qualified_type (void_type_node,
627 (cp_type_quals (TREE_TYPE (t1))
628 | cp_type_quals (TREE_TYPE (t2))));
629 result_type = build_pointer_type (result_type);
630 /* Merge the attributes. */
631 attributes = (*targetm.merge_type_attributes) (t1, t2);
632 return build_type_attribute_variant (result_type, attributes);
635 if (c_dialect_objc () && TREE_CODE (t1) == POINTER_TYPE
636 && TREE_CODE (t2) == POINTER_TYPE)
638 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
639 return objc_common_type (t1, t2);
642 /* [expr.eq] permits the application of a pointer conversion to
643 bring the pointers to a common type. */
644 if (TREE_CODE (t1) == POINTER_TYPE && TREE_CODE (t2) == POINTER_TYPE
645 && CLASS_TYPE_P (TREE_TYPE (t1))
646 && CLASS_TYPE_P (TREE_TYPE (t2))
647 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
650 class1 = TREE_TYPE (t1);
651 class2 = TREE_TYPE (t2);
653 if (DERIVED_FROM_P (class1, class2))
654 t2 = (build_pointer_type
655 (cp_build_qualified_type (class1, cp_type_quals (class2))));
656 else if (DERIVED_FROM_P (class2, class1))
657 t1 = (build_pointer_type
658 (cp_build_qualified_type (class2, cp_type_quals (class1))));
661 if (complain & tf_error)
662 composite_pointer_error (DK_ERROR, t1, t2, operation);
663 return error_mark_node;
666 /* [expr.eq] permits the application of a pointer-to-member
667 conversion to change the class type of one of the types. */
668 else if (TYPE_PTR_TO_MEMBER_P (t1)
669 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
670 TYPE_PTRMEM_CLASS_TYPE (t2)))
672 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
673 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
675 if (DERIVED_FROM_P (class1, class2))
676 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
677 else if (DERIVED_FROM_P (class2, class1))
678 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
681 if (complain & tf_error)
685 error ("comparison between distinct "
686 "pointer-to-member types %qT and %qT lacks a cast",
690 error ("conversion between distinct "
691 "pointer-to-member types %qT and %qT lacks a cast",
694 case CPO_CONDITIONAL_EXPR:
695 error ("conditional expression between distinct "
696 "pointer-to-member types %qT and %qT lacks a cast",
702 return error_mark_node;
706 return composite_pointer_type_r (t1, t2, operation, complain);
709 /* Return the merged type of two types.
710 We assume that comptypes has already been done and returned 1;
711 if that isn't so, this may crash.
713 This just combines attributes and default arguments; any other
714 differences would cause the two types to compare unalike. */
717 merge_types (tree t1, tree t2)
719 enum tree_code code1;
720 enum tree_code code2;
723 /* Save time if the two types are the same. */
726 if (original_type (t1) == original_type (t2))
729 /* If one type is nonsense, use the other. */
730 if (t1 == error_mark_node)
732 if (t2 == error_mark_node)
735 /* Merge the attributes. */
736 attributes = (*targetm.merge_type_attributes) (t1, t2);
738 if (TYPE_PTRMEMFUNC_P (t1))
739 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
740 if (TYPE_PTRMEMFUNC_P (t2))
741 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
743 code1 = TREE_CODE (t1);
744 code2 = TREE_CODE (t2);
747 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
748 if (code1 == TYPENAME_TYPE)
750 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
751 code1 = TREE_CODE (t1);
755 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
756 code2 = TREE_CODE (t2);
764 /* For two pointers, do this recursively on the target type. */
766 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
767 int quals = cp_type_quals (t1);
769 if (code1 == POINTER_TYPE)
770 t1 = build_pointer_type (target);
772 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
773 t1 = build_type_attribute_variant (t1, attributes);
774 t1 = cp_build_qualified_type (t1, quals);
776 if (TREE_CODE (target) == METHOD_TYPE)
777 t1 = build_ptrmemfunc_type (t1);
786 quals = cp_type_quals (t1);
787 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
788 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
789 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
791 t1 = cp_build_qualified_type (t1, quals);
797 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
798 /* Save space: see if the result is identical to one of the args. */
799 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
800 return build_type_attribute_variant (t1, attributes);
801 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
802 return build_type_attribute_variant (t2, attributes);
803 /* Merge the element types, and have a size if either arg has one. */
804 t1 = build_cplus_array_type
805 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
810 /* Function types: prefer the one that specified arg types.
811 If both do, merge the arg types. Also merge the return types. */
813 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
814 tree p1 = TYPE_ARG_TYPES (t1);
815 tree p2 = TYPE_ARG_TYPES (t2);
819 /* Save space: see if the result is identical to one of the args. */
820 if (valtype == TREE_TYPE (t1) && ! p2)
821 return cp_build_type_attribute_variant (t1, attributes);
822 if (valtype == TREE_TYPE (t2) && ! p1)
823 return cp_build_type_attribute_variant (t2, attributes);
825 /* Simple way if one arg fails to specify argument types. */
826 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
828 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
831 parms = commonparms (p1, p2);
833 rval = build_function_type (valtype, parms);
834 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
835 rval = apply_memfn_quals (rval, type_memfn_quals (t1));
836 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
837 TYPE_RAISES_EXCEPTIONS (t2),
839 t1 = build_exception_variant (rval, raises);
845 /* Get this value the long way, since TYPE_METHOD_BASETYPE
846 is just the main variant of this. */
847 tree basetype = class_of_this_parm (t2);
848 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
849 TYPE_RAISES_EXCEPTIONS (t2),
853 /* If this was a member function type, get back to the
854 original type of type member function (i.e., without
855 the class instance variable up front. */
856 t1 = build_function_type (TREE_TYPE (t1),
857 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
858 t2 = build_function_type (TREE_TYPE (t2),
859 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
860 t3 = merge_types (t1, t2);
861 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
862 TYPE_ARG_TYPES (t3));
863 t1 = build_exception_variant (t3, raises);
868 /* There is no need to merge attributes into a TYPENAME_TYPE.
869 When the type is instantiated it will have whatever
870 attributes result from the instantiation. */
876 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
878 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
881 return cp_build_type_attribute_variant (t1, attributes);
884 /* Return the ARRAY_TYPE type without its domain. */
887 strip_array_domain (tree type)
890 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
891 if (TYPE_DOMAIN (type) == NULL_TREE)
893 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
894 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
897 /* Wrapper around cp_common_type that is used by c-common.c and other
898 front end optimizations that remove promotions.
900 Return the common type for two arithmetic types T1 and T2 under the
901 usual arithmetic conversions. The default conversions have already
902 been applied, and enumerated types converted to their compatible
906 common_type (tree t1, tree t2)
908 /* If one type is nonsense, use the other */
909 if (t1 == error_mark_node)
911 if (t2 == error_mark_node)
914 return cp_common_type (t1, t2);
917 /* Return the common type of two pointer types T1 and T2. This is the
918 type for the result of most arithmetic operations if the operands
919 have the given two types.
921 We assume that comp_target_types has already been done and returned
922 nonzero; if that isn't so, this may crash. */
925 common_pointer_type (tree t1, tree t2)
927 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
928 || (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
929 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
931 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
932 CPO_CONVERSION, tf_warning_or_error);
935 /* Compare two exception specifier types for exactness or subsetness, if
936 allowed. Returns false for mismatch, true for match (same, or
939 [except.spec] "If a class X ... objects of class X or any class publicly
940 and unambiguously derived from X. Similarly, if a pointer type Y * ...
941 exceptions of type Y * or that are pointers to any type publicly and
942 unambiguously derived from Y. Otherwise a function only allows exceptions
943 that have the same type ..."
944 This does not mention cv qualifiers and is different to what throw
945 [except.throw] and catch [except.catch] will do. They will ignore the
946 top level cv qualifiers, and allow qualifiers in the pointer to class
949 We implement the letter of the standard. */
952 comp_except_types (tree a, tree b, bool exact)
954 if (same_type_p (a, b))
958 if (cp_type_quals (a) || cp_type_quals (b))
961 if (TREE_CODE (a) == POINTER_TYPE
962 && TREE_CODE (b) == POINTER_TYPE)
966 if (cp_type_quals (a) || cp_type_quals (b))
970 if (TREE_CODE (a) != RECORD_TYPE
971 || TREE_CODE (b) != RECORD_TYPE)
974 if (PUBLICLY_UNIQUELY_DERIVED_P (a, b))
980 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
981 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
982 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
983 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
984 are unordered, but we've already filtered out duplicates. Most lists will
985 be in order, we should try to make use of that. */
988 comp_except_specs (const_tree t1, const_tree t2, int exact)
997 /* First handle noexcept. */
998 if (exact < ce_exact)
1000 /* noexcept(false) is compatible with no exception-specification,
1001 and stricter than any spec. */
1002 if (t1 == noexcept_false_spec)
1003 return t2 == NULL_TREE || exact == ce_derived;
1004 /* Even a derived noexcept(false) is compatible with no
1005 exception-specification. */
1006 if (t2 == noexcept_false_spec)
1007 return t1 == NULL_TREE;
1009 /* Otherwise, if we aren't looking for an exact match, noexcept is
1010 equivalent to throw(). */
1011 if (t1 == noexcept_true_spec)
1012 t1 = empty_except_spec;
1013 if (t2 == noexcept_true_spec)
1014 t2 = empty_except_spec;
1017 /* If any noexcept is left, it is only comparable to itself;
1018 either we're looking for an exact match or we're redeclaring a
1019 template with dependent noexcept. */
1020 if ((t1 && TREE_PURPOSE (t1))
1021 || (t2 && TREE_PURPOSE (t2)))
1023 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1025 if (t1 == NULL_TREE) /* T1 is ... */
1026 return t2 == NULL_TREE || exact == ce_derived;
1027 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1028 return t2 != NULL_TREE && !TREE_VALUE (t2);
1029 if (t2 == NULL_TREE) /* T2 is ... */
1031 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1032 return exact == ce_derived;
1034 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1035 Count how many we find, to determine exactness. For exact matching and
1036 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1038 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1040 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1042 tree a = TREE_VALUE (probe);
1043 tree b = TREE_VALUE (t2);
1045 if (comp_except_types (a, b, exact))
1047 if (probe == base && exact > ce_derived)
1048 base = TREE_CHAIN (probe);
1053 if (probe == NULL_TREE)
1056 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1059 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1060 [] can match [size]. */
1063 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1072 /* The type of the array elements must be the same. */
1073 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1076 d1 = TYPE_DOMAIN (t1);
1077 d2 = TYPE_DOMAIN (t2);
1082 /* If one of the arrays is dimensionless, and the other has a
1083 dimension, they are of different types. However, it is valid to
1091 declarations for an array object can specify
1092 array types that differ by the presence or absence of a major
1093 array bound (_dcl.array_). */
1095 return allow_redeclaration;
1097 /* Check that the dimensions are the same. */
1099 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1101 max1 = TYPE_MAX_VALUE (d1);
1102 max2 = TYPE_MAX_VALUE (d2);
1103 if (processing_template_decl && !abi_version_at_least (2)
1104 && !value_dependent_expression_p (max1)
1105 && !value_dependent_expression_p (max2))
1107 /* With abi-1 we do not fold non-dependent array bounds, (and
1108 consequently mangle them incorrectly). We must therefore
1109 fold them here, to verify the domains have the same
1115 if (!cp_tree_equal (max1, max2))
1121 /* Compare the relative position of T1 and T2 into their respective
1122 template parameter list.
1123 T1 and T2 must be template parameter types.
1124 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1127 comp_template_parms_position (tree t1, tree t2)
1129 tree index1, index2;
1130 gcc_assert (t1 && t2
1131 && TREE_CODE (t1) == TREE_CODE (t2)
1132 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1133 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1134 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1136 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1137 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1139 /* If T1 and T2 belong to template parm lists of different size,
1140 let's assume they are different. */
1141 if (TEMPLATE_PARM_NUM_SIBLINGS (index1)
1142 != TEMPLATE_PARM_NUM_SIBLINGS (index2))
1145 /* Then compare their relative position. */
1146 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1147 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1148 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1149 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1155 /* Subroutine in comptypes. */
1158 structural_comptypes (tree t1, tree t2, int strict)
1163 /* Suppress errors caused by previously reported errors. */
1164 if (t1 == error_mark_node || t2 == error_mark_node)
1167 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1169 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1170 current instantiation. */
1171 if (TREE_CODE (t1) == TYPENAME_TYPE)
1172 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1174 if (TREE_CODE (t2) == TYPENAME_TYPE)
1175 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1177 if (TYPE_PTRMEMFUNC_P (t1))
1178 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1179 if (TYPE_PTRMEMFUNC_P (t2))
1180 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1182 /* Different classes of types can't be compatible. */
1183 if (TREE_CODE (t1) != TREE_CODE (t2))
1186 /* Qualifiers must match. For array types, we will check when we
1187 recur on the array element types. */
1188 if (TREE_CODE (t1) != ARRAY_TYPE
1189 && cp_type_quals (t1) != cp_type_quals (t2))
1191 if (TREE_CODE (t1) == FUNCTION_TYPE
1192 && type_memfn_quals (t1) != type_memfn_quals (t2))
1194 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1197 /* Allow for two different type nodes which have essentially the same
1198 definition. Note that we already checked for equality of the type
1199 qualifiers (just above). */
1201 if (TREE_CODE (t1) != ARRAY_TYPE
1202 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1206 /* Compare the types. Break out if they could be the same. */
1207 switch (TREE_CODE (t1))
1211 /* All void and bool types are the same. */
1215 case FIXED_POINT_TYPE:
1217 /* With these nodes, we can't determine type equivalence by
1218 looking at what is stored in the nodes themselves, because
1219 two nodes might have different TYPE_MAIN_VARIANTs but still
1220 represent the same type. For example, wchar_t and int could
1221 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1222 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1223 and are distinct types. On the other hand, int and the
1226 typedef int INT __attribute((may_alias));
1228 have identical properties, different TYPE_MAIN_VARIANTs, but
1229 represent the same type. The canonical type system keeps
1230 track of equivalence in this case, so we fall back on it. */
1231 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1233 case TEMPLATE_TEMPLATE_PARM:
1234 case BOUND_TEMPLATE_TEMPLATE_PARM:
1235 if (!comp_template_parms_position (t1, t2))
1237 if (!comp_template_parms
1238 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1239 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1241 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1243 /* Don't check inheritance. */
1244 strict = COMPARE_STRICT;
1249 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1250 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1251 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1252 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1255 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1257 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1263 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1264 strict & ~COMPARE_REDECLARATION))
1266 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1270 case REFERENCE_TYPE:
1271 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1273 /* fall through to checks for pointer types */
1276 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1277 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1278 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1284 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1286 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1291 /* Target types must match incl. qualifiers. */
1292 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1296 case TEMPLATE_TYPE_PARM:
1297 /* If T1 and T2 don't have the same relative position in their
1298 template parameters set, they can't be equal. */
1299 if (!comp_template_parms_position (t1, t2))
1304 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1305 TYPENAME_TYPE_FULLNAME (t2)))
1307 /* Qualifiers don't matter on scopes. */
1308 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1313 case UNBOUND_CLASS_TEMPLATE:
1314 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1316 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1321 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1326 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1327 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1331 case TYPE_PACK_EXPANSION:
1332 return same_type_p (PACK_EXPANSION_PATTERN (t1),
1333 PACK_EXPANSION_PATTERN (t2));
1336 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1337 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1338 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1339 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1340 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1341 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1342 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1343 DECLTYPE_TYPE_EXPR (t2)))
1347 case UNDERLYING_TYPE:
1348 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1349 UNDERLYING_TYPE_TYPE (t2));
1355 /* If we get here, we know that from a target independent POV the
1356 types are the same. Make sure the target attributes are also
1358 return comp_type_attributes (t1, t2);
1361 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1362 is a bitwise-or of the COMPARE_* flags. */
1365 comptypes (tree t1, tree t2, int strict)
1367 if (strict == COMPARE_STRICT)
1372 if (t1 == error_mark_node || t2 == error_mark_node)
1375 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1376 /* At least one of the types requires structural equality, so
1377 perform a deep check. */
1378 return structural_comptypes (t1, t2, strict);
1380 #ifdef ENABLE_CHECKING
1381 if (USE_CANONICAL_TYPES)
1383 bool result = structural_comptypes (t1, t2, strict);
1385 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1386 /* The two types are structurally equivalent, but their
1387 canonical types were different. This is a failure of the
1388 canonical type propagation code.*/
1390 ("canonical types differ for identical types %T and %T",
1392 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1393 /* Two types are structurally different, but the canonical
1394 types are the same. This means we were over-eager in
1395 assigning canonical types. */
1397 ("same canonical type node for different types %T and %T",
1403 if (USE_CANONICAL_TYPES)
1404 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1407 return structural_comptypes (t1, t2, strict);
1409 else if (strict == COMPARE_STRUCTURAL)
1410 return structural_comptypes (t1, t2, COMPARE_STRICT);
1412 return structural_comptypes (t1, t2, strict);
1415 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1416 top-level qualifiers. */
1419 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1421 if (type1 == error_mark_node || type2 == error_mark_node)
1424 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1427 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1430 at_least_as_qualified_p (const_tree type1, const_tree type2)
1432 int q1 = cp_type_quals (type1);
1433 int q2 = cp_type_quals (type2);
1435 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1436 return (q1 & q2) == q2;
1439 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1440 more cv-qualified that TYPE1, and 0 otherwise. */
1443 comp_cv_qualification (const_tree type1, const_tree type2)
1445 int q1 = cp_type_quals (type1);
1446 int q2 = cp_type_quals (type2);
1451 if ((q1 & q2) == q2)
1453 else if ((q1 & q2) == q1)
1459 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1460 subset of the cv-qualification signature of TYPE2, and the types
1461 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1464 comp_cv_qual_signature (tree type1, tree type2)
1466 if (comp_ptr_ttypes_real (type2, type1, -1))
1468 else if (comp_ptr_ttypes_real (type1, type2, -1))
1474 /* Subroutines of `comptypes'. */
1476 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1477 equivalent in the sense that functions with those parameter types
1478 can have equivalent types. The two lists must be equivalent,
1479 element by element. */
1482 compparms (const_tree parms1, const_tree parms2)
1486 /* An unspecified parmlist matches any specified parmlist
1487 whose argument types don't need default promotions. */
1489 for (t1 = parms1, t2 = parms2;
1491 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1493 /* If one parmlist is shorter than the other,
1494 they fail to match. */
1497 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1504 /* Process a sizeof or alignof expression where the operand is a
1508 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1513 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1514 if (type == error_mark_node)
1515 return error_mark_node;
1517 type = non_reference (type);
1518 if (TREE_CODE (type) == METHOD_TYPE)
1521 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
1522 "invalid application of %qs to a member function",
1523 operator_name_info[(int) op].name);
1524 value = size_one_node;
1527 dependent_p = dependent_type_p (type);
1529 complete_type (type);
1531 /* VLA types will have a non-constant size. In the body of an
1532 uninstantiated template, we don't need to try to compute the
1533 value, because the sizeof expression is not an integral
1534 constant expression in that case. And, if we do try to
1535 compute the value, we'll likely end up with SAVE_EXPRs, which
1536 the template substitution machinery does not expect to see. */
1537 || (processing_template_decl
1538 && COMPLETE_TYPE_P (type)
1539 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1541 value = build_min (op, size_type_node, type);
1542 TREE_READONLY (value) = 1;
1546 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1551 /* Return the size of the type, without producing any warnings for
1552 types whose size cannot be taken. This routine should be used only
1553 in some other routine that has already produced a diagnostic about
1554 using the size of such a type. */
1556 cxx_sizeof_nowarn (tree type)
1558 if (TREE_CODE (type) == FUNCTION_TYPE
1559 || TREE_CODE (type) == VOID_TYPE
1560 || TREE_CODE (type) == ERROR_MARK)
1561 return size_one_node;
1562 else if (!COMPLETE_TYPE_P (type))
1563 return size_zero_node;
1565 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1568 /* Process a sizeof expression where the operand is an expression. */
1571 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1573 if (e == error_mark_node)
1574 return error_mark_node;
1576 if (processing_template_decl)
1578 e = build_min (SIZEOF_EXPR, size_type_node, e);
1579 TREE_SIDE_EFFECTS (e) = 0;
1580 TREE_READONLY (e) = 1;
1585 /* To get the size of a static data member declared as an array of
1586 unknown bound, we need to instantiate it. */
1587 if (TREE_CODE (e) == VAR_DECL
1588 && VAR_HAD_UNKNOWN_BOUND (e)
1589 && DECL_TEMPLATE_INSTANTIATION (e))
1590 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1592 e = mark_type_use (e);
1594 if (TREE_CODE (e) == COMPONENT_REF
1595 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1596 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1598 if (complain & tf_error)
1599 error ("invalid application of %<sizeof%> to a bit-field");
1601 return error_mark_node;
1604 else if (is_overloaded_fn (e))
1606 if (complain & tf_error)
1607 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1610 return error_mark_node;
1613 else if (type_unknown_p (e))
1615 if (complain & tf_error)
1616 cxx_incomplete_type_error (e, TREE_TYPE (e));
1618 return error_mark_node;
1624 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1627 /* Implement the __alignof keyword: Return the minimum required
1628 alignment of E, measured in bytes. For VAR_DECL's and
1629 FIELD_DECL's return DECL_ALIGN (which can be set from an
1630 "aligned" __attribute__ specification). */
1633 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1637 if (e == error_mark_node)
1638 return error_mark_node;
1640 if (processing_template_decl)
1642 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1643 TREE_SIDE_EFFECTS (e) = 0;
1644 TREE_READONLY (e) = 1;
1649 e = mark_type_use (e);
1651 if (TREE_CODE (e) == VAR_DECL)
1652 t = size_int (DECL_ALIGN_UNIT (e));
1653 else if (TREE_CODE (e) == COMPONENT_REF
1654 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1655 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1657 if (complain & tf_error)
1658 error ("invalid application of %<__alignof%> to a bit-field");
1660 return error_mark_node;
1663 else if (TREE_CODE (e) == COMPONENT_REF
1664 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1665 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1666 else if (is_overloaded_fn (e))
1668 if (complain & tf_error)
1669 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1672 return error_mark_node;
1673 if (TREE_CODE (e) == FUNCTION_DECL)
1674 t = size_int (DECL_ALIGN_UNIT (e));
1678 else if (type_unknown_p (e))
1680 if (complain & tf_error)
1681 cxx_incomplete_type_error (e, TREE_TYPE (e));
1683 return error_mark_node;
1687 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1688 complain & tf_error);
1690 return fold_convert (size_type_node, t);
1693 /* Process a sizeof or alignof expression E with code OP where the operand
1694 is an expression. */
1697 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1699 if (op == SIZEOF_EXPR)
1700 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1702 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1705 /* EXPR is being used in a context that is not a function call.
1710 The expression can be used only as the left-hand operand of a
1711 member function call.
1713 [expr.mptr.operator]
1715 If the result of .* or ->* is a function, then that result can be
1716 used only as the operand for the function call operator ().
1718 by issuing an error message if appropriate. Returns true iff EXPR
1719 violates these rules. */
1722 invalid_nonstatic_memfn_p (const_tree expr, tsubst_flags_t complain)
1724 if (expr && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1726 if (complain & tf_error)
1727 error ("invalid use of non-static member function");
1733 /* If EXP is a reference to a bitfield, and the type of EXP does not
1734 match the declared type of the bitfield, return the declared type
1735 of the bitfield. Otherwise, return NULL_TREE. */
1738 is_bitfield_expr_with_lowered_type (const_tree exp)
1740 switch (TREE_CODE (exp))
1743 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1744 ? TREE_OPERAND (exp, 1)
1745 : TREE_OPERAND (exp, 0)))
1747 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1750 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1754 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1760 field = TREE_OPERAND (exp, 1);
1761 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1763 if (same_type_ignoring_top_level_qualifiers_p
1764 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1766 return DECL_BIT_FIELD_TYPE (field);
1770 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1771 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1772 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1780 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1781 bitfield with a lowered type, the type of EXP is returned, rather
1785 unlowered_expr_type (const_tree exp)
1788 tree etype = TREE_TYPE (exp);
1790 type = is_bitfield_expr_with_lowered_type (exp);
1792 type = cp_build_qualified_type (type, cp_type_quals (etype));
1799 /* Perform the conversions in [expr] that apply when an lvalue appears
1800 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1801 function-to-pointer conversions. In addition, manifest constants
1802 are replaced by their values, and bitfield references are converted
1803 to their declared types. Note that this function does not perform the
1804 lvalue-to-rvalue conversion for class types. If you need that conversion
1805 to for class types, then you probably need to use force_rvalue.
1807 Although the returned value is being used as an rvalue, this
1808 function does not wrap the returned expression in a
1809 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1810 that the return value is no longer an lvalue. */
1813 decay_conversion (tree exp)
1816 enum tree_code code;
1818 type = TREE_TYPE (exp);
1819 if (type == error_mark_node)
1820 return error_mark_node;
1822 exp = mark_rvalue_use (exp);
1824 exp = resolve_nondeduced_context (exp);
1825 if (type_unknown_p (exp))
1827 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1828 return error_mark_node;
1831 /* FIXME remove? at least need to remember that this isn't really a
1832 constant expression if EXP isn't decl_constant_var_p, like with
1833 C_MAYBE_CONST_EXPR. */
1834 exp = decl_constant_value_safe (exp);
1835 if (error_operand_p (exp))
1836 return error_mark_node;
1838 if (NULLPTR_TYPE_P (type))
1839 return nullptr_node;
1841 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1842 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1843 code = TREE_CODE (type);
1844 if (code == VOID_TYPE)
1846 error ("void value not ignored as it ought to be");
1847 return error_mark_node;
1849 if (invalid_nonstatic_memfn_p (exp, tf_warning_or_error))
1850 return error_mark_node;
1851 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1852 return cp_build_addr_expr (exp, tf_warning_or_error);
1853 if (code == ARRAY_TYPE)
1858 if (TREE_CODE (exp) == INDIRECT_REF)
1859 return build_nop (build_pointer_type (TREE_TYPE (type)),
1860 TREE_OPERAND (exp, 0));
1862 if (TREE_CODE (exp) == COMPOUND_EXPR)
1864 tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1865 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1866 TREE_OPERAND (exp, 0), op1);
1870 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1872 error ("invalid use of non-lvalue array");
1873 return error_mark_node;
1876 ptrtype = build_pointer_type (TREE_TYPE (type));
1878 if (TREE_CODE (exp) == VAR_DECL)
1880 if (!cxx_mark_addressable (exp))
1881 return error_mark_node;
1882 adr = build_nop (ptrtype, build_address (exp));
1885 /* This way is better for a COMPONENT_REF since it can
1886 simplify the offset for a component. */
1887 adr = cp_build_addr_expr (exp, tf_warning_or_error);
1888 return cp_convert (ptrtype, adr);
1891 /* If a bitfield is used in a context where integral promotion
1892 applies, then the caller is expected to have used
1893 default_conversion. That function promotes bitfields correctly
1894 before calling this function. At this point, if we have a
1895 bitfield referenced, we may assume that is not subject to
1896 promotion, and that, therefore, the type of the resulting rvalue
1897 is the declared type of the bitfield. */
1898 exp = convert_bitfield_to_declared_type (exp);
1900 /* We do not call rvalue() here because we do not want to wrap EXP
1901 in a NON_LVALUE_EXPR. */
1905 Non-class rvalues always have cv-unqualified types. */
1906 type = TREE_TYPE (exp);
1907 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
1908 exp = build_nop (cv_unqualified (type), exp);
1913 /* Perform preparatory conversions, as part of the "usual arithmetic
1914 conversions". In particular, as per [expr]:
1916 Whenever an lvalue expression appears as an operand of an
1917 operator that expects the rvalue for that operand, the
1918 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
1919 standard conversions are applied to convert the expression to an
1922 In addition, we perform integral promotions here, as those are
1923 applied to both operands to a binary operator before determining
1924 what additional conversions should apply. */
1927 default_conversion (tree exp)
1929 /* Check for target-specific promotions. */
1930 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
1932 exp = cp_convert (promoted_type, exp);
1933 /* Perform the integral promotions first so that bitfield
1934 expressions (which may promote to "int", even if the bitfield is
1935 declared "unsigned") are promoted correctly. */
1936 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
1937 exp = perform_integral_promotions (exp);
1938 /* Perform the other conversions. */
1939 exp = decay_conversion (exp);
1944 /* EXPR is an expression with an integral or enumeration type.
1945 Perform the integral promotions in [conv.prom], and return the
1949 perform_integral_promotions (tree expr)
1954 expr = mark_rvalue_use (expr);
1958 If the bitfield has an enumerated type, it is treated as any
1959 other value of that type for promotion purposes. */
1960 type = is_bitfield_expr_with_lowered_type (expr);
1961 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
1962 type = TREE_TYPE (expr);
1963 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
1964 /* Scoped enums don't promote. */
1965 if (SCOPED_ENUM_P (type))
1967 promoted_type = type_promotes_to (type);
1968 if (type != promoted_type)
1969 expr = cp_convert (promoted_type, expr);
1973 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1974 decay_conversion to one. */
1977 string_conv_p (const_tree totype, const_tree exp, int warn)
1981 if (TREE_CODE (totype) != POINTER_TYPE)
1984 t = TREE_TYPE (totype);
1985 if (!same_type_p (t, char_type_node)
1986 && !same_type_p (t, char16_type_node)
1987 && !same_type_p (t, char32_type_node)
1988 && !same_type_p (t, wchar_type_node))
1991 if (TREE_CODE (exp) == STRING_CST)
1993 /* Make sure that we don't try to convert between char and wide chars. */
1994 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1999 /* Is this a string constant which has decayed to 'const char *'? */
2000 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2001 if (!same_type_p (TREE_TYPE (exp), t))
2004 if (TREE_CODE (exp) != ADDR_EXPR
2005 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2009 /* This warning is not very useful, as it complains about printf. */
2011 warning (OPT_Wwrite_strings,
2012 "deprecated conversion from string constant to %qT",
2018 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2019 can, for example, use as an lvalue. This code used to be in
2020 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2021 expressions, where we're dealing with aggregates. But now it's again only
2022 called from unary_complex_lvalue. The case (in particular) that led to
2023 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2027 rationalize_conditional_expr (enum tree_code code, tree t,
2028 tsubst_flags_t complain)
2030 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2031 the first operand is always the one to be used if both operands
2032 are equal, so we know what conditional expression this used to be. */
2033 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2035 tree op0 = TREE_OPERAND (t, 0);
2036 tree op1 = TREE_OPERAND (t, 1);
2038 /* The following code is incorrect if either operand side-effects. */
2039 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2040 && !TREE_SIDE_EFFECTS (op1));
2042 build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
2043 ? LE_EXPR : GE_EXPR),
2044 op0, TREE_CODE (op0),
2045 op1, TREE_CODE (op1),
2048 cp_build_unary_op (code, op0, 0, complain),
2049 cp_build_unary_op (code, op1, 0, complain),
2054 build_conditional_expr (TREE_OPERAND (t, 0),
2055 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2057 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2062 /* Given the TYPE of an anonymous union field inside T, return the
2063 FIELD_DECL for the field. If not found return NULL_TREE. Because
2064 anonymous unions can nest, we must also search all anonymous unions
2065 that are directly reachable. */
2068 lookup_anon_field (tree t, tree type)
2072 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2074 if (TREE_STATIC (field))
2076 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2079 /* If we find it directly, return the field. */
2080 if (DECL_NAME (field) == NULL_TREE
2081 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2086 /* Otherwise, it could be nested, search harder. */
2087 if (DECL_NAME (field) == NULL_TREE
2088 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2090 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2098 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2099 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2100 non-NULL, it indicates the path to the base used to name MEMBER.
2101 If PRESERVE_REFERENCE is true, the expression returned will have
2102 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2103 returned will have the type referred to by the reference.
2105 This function does not perform access control; that is either done
2106 earlier by the parser when the name of MEMBER is resolved to MEMBER
2107 itself, or later when overload resolution selects one of the
2108 functions indicated by MEMBER. */
2111 build_class_member_access_expr (tree object, tree member,
2112 tree access_path, bool preserve_reference,
2113 tsubst_flags_t complain)
2117 tree result = NULL_TREE;
2119 if (error_operand_p (object) || error_operand_p (member))
2120 return error_mark_node;
2122 gcc_assert (DECL_P (member) || BASELINK_P (member));
2126 The type of the first expression shall be "class object" (of a
2128 object_type = TREE_TYPE (object);
2129 if (!currently_open_class (object_type)
2130 && !complete_type_or_maybe_complain (object_type, object, complain))
2131 return error_mark_node;
2132 if (!CLASS_TYPE_P (object_type))
2134 if (complain & tf_error)
2136 if (POINTER_TYPE_P (object_type)
2137 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2138 error ("request for member %qD in %qE, which is of pointer "
2139 "type %qT (maybe you meant to use %<->%> ?)",
2140 member, object, object_type);
2142 error ("request for member %qD in %qE, which is of non-class "
2143 "type %qT", member, object, object_type);
2145 return error_mark_node;
2148 /* The standard does not seem to actually say that MEMBER must be a
2149 member of OBJECT_TYPE. However, that is clearly what is
2151 if (DECL_P (member))
2153 member_scope = DECL_CLASS_CONTEXT (member);
2155 if (TREE_DEPRECATED (member))
2156 warn_deprecated_use (member, NULL_TREE);
2159 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2160 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2161 presently be the anonymous union. Go outwards until we find a
2162 type related to OBJECT_TYPE. */
2163 while (ANON_AGGR_TYPE_P (member_scope)
2164 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2166 member_scope = TYPE_CONTEXT (member_scope);
2167 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2169 if (complain & tf_error)
2171 if (TREE_CODE (member) == FIELD_DECL)
2172 error ("invalid use of nonstatic data member %qE", member);
2174 error ("%qD is not a member of %qT", member, object_type);
2176 return error_mark_node;
2179 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2180 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2181 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2183 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2185 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2188 /* In [expr.ref], there is an explicit list of the valid choices for
2189 MEMBER. We check for each of those cases here. */
2190 if (TREE_CODE (member) == VAR_DECL)
2192 /* A static data member. */
2194 mark_exp_read (object);
2195 /* If OBJECT has side-effects, they are supposed to occur. */
2196 if (TREE_SIDE_EFFECTS (object))
2197 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2199 else if (TREE_CODE (member) == FIELD_DECL)
2201 /* A non-static data member. */
2206 null_object_p = (TREE_CODE (object) == INDIRECT_REF
2207 && integer_zerop (TREE_OPERAND (object, 0)));
2209 /* Convert OBJECT to the type of MEMBER. */
2210 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2211 TYPE_MAIN_VARIANT (member_scope)))
2216 binfo = lookup_base (access_path ? access_path : object_type,
2217 member_scope, ba_unique, &kind);
2218 if (binfo == error_mark_node)
2219 return error_mark_node;
2221 /* It is invalid to try to get to a virtual base of a
2222 NULL object. The most common cause is invalid use of
2224 if (null_object_p && kind == bk_via_virtual)
2226 if (complain & tf_error)
2228 error ("invalid access to non-static data member %qD of "
2231 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2233 return error_mark_node;
2236 /* Convert to the base. */
2237 object = build_base_path (PLUS_EXPR, object, binfo,
2238 /*nonnull=*/1, complain);
2239 /* If we found the base successfully then we should be able
2240 to convert to it successfully. */
2241 gcc_assert (object != error_mark_node);
2244 /* Complain about other invalid uses of offsetof, even though they will
2245 give the right answer. Note that we complain whether or not they
2246 actually used the offsetof macro, since there's no way to know at this
2247 point. So we just give a warning, instead of a pedwarn. */
2248 /* Do not produce this warning for base class field references, because
2249 we know for a fact that didn't come from offsetof. This does occur
2250 in various testsuite cases where a null object is passed where a
2251 vtable access is required. */
2252 if (null_object_p && warn_invalid_offsetof
2253 && CLASSTYPE_NON_STD_LAYOUT (object_type)
2254 && !DECL_FIELD_IS_BASE (member)
2255 && cp_unevaluated_operand == 0
2256 && (complain & tf_warning))
2258 warning (OPT_Winvalid_offsetof,
2259 "invalid access to non-static data member %qD "
2260 " of NULL object", member);
2261 warning (OPT_Winvalid_offsetof,
2262 "(perhaps the %<offsetof%> macro was used incorrectly)");
2265 /* If MEMBER is from an anonymous aggregate, we have converted
2266 OBJECT so that it refers to the class containing the
2267 anonymous union. Generate a reference to the anonymous union
2268 itself, and recur to find MEMBER. */
2269 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2270 /* When this code is called from build_field_call, the
2271 object already has the type of the anonymous union.
2272 That is because the COMPONENT_REF was already
2273 constructed, and was then disassembled before calling
2274 build_field_call. After the function-call code is
2275 cleaned up, this waste can be eliminated. */
2276 && (!same_type_ignoring_top_level_qualifiers_p
2277 (TREE_TYPE (object), DECL_CONTEXT (member))))
2279 tree anonymous_union;
2281 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2282 DECL_CONTEXT (member));
2283 object = build_class_member_access_expr (object,
2285 /*access_path=*/NULL_TREE,
2290 /* Compute the type of the field, as described in [expr.ref]. */
2291 type_quals = TYPE_UNQUALIFIED;
2292 member_type = TREE_TYPE (member);
2293 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2295 type_quals = (cp_type_quals (member_type)
2296 | cp_type_quals (object_type));
2298 /* A field is const (volatile) if the enclosing object, or the
2299 field itself, is const (volatile). But, a mutable field is
2300 not const, even within a const object. */
2301 if (DECL_MUTABLE_P (member))
2302 type_quals &= ~TYPE_QUAL_CONST;
2303 member_type = cp_build_qualified_type (member_type, type_quals);
2306 result = build3 (COMPONENT_REF, member_type, object, member,
2308 result = fold_if_not_in_template (result);
2310 /* Mark the expression const or volatile, as appropriate. Even
2311 though we've dealt with the type above, we still have to mark the
2312 expression itself. */
2313 if (type_quals & TYPE_QUAL_CONST)
2314 TREE_READONLY (result) = 1;
2315 if (type_quals & TYPE_QUAL_VOLATILE)
2316 TREE_THIS_VOLATILE (result) = 1;
2318 else if (BASELINK_P (member))
2320 /* The member is a (possibly overloaded) member function. */
2324 /* If the MEMBER is exactly one static member function, then we
2325 know the type of the expression. Otherwise, we must wait
2326 until overload resolution has been performed. */
2327 functions = BASELINK_FUNCTIONS (member);
2328 if (TREE_CODE (functions) == FUNCTION_DECL
2329 && DECL_STATIC_FUNCTION_P (functions))
2330 type = TREE_TYPE (functions);
2332 type = unknown_type_node;
2333 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2334 base. That will happen when the function is called. */
2335 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2337 else if (TREE_CODE (member) == CONST_DECL)
2339 /* The member is an enumerator. */
2341 /* If OBJECT has side-effects, they are supposed to occur. */
2342 if (TREE_SIDE_EFFECTS (object))
2343 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2348 if (complain & tf_error)
2349 error ("invalid use of %qD", member);
2350 return error_mark_node;
2353 if (!preserve_reference)
2356 If E2 is declared to have type "reference to T", then ... the
2357 type of E1.E2 is T. */
2358 result = convert_from_reference (result);
2363 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2364 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2367 lookup_destructor (tree object, tree scope, tree dtor_name)
2369 tree object_type = TREE_TYPE (object);
2370 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2373 if (scope && !check_dtor_name (scope, dtor_type))
2375 error ("qualified type %qT does not match destructor name ~%qT",
2377 return error_mark_node;
2379 if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
2381 /* In a template, names we can't find a match for are still accepted
2382 destructor names, and we check them here. */
2383 if (check_dtor_name (object_type, dtor_type))
2384 dtor_type = object_type;
2387 error ("object type %qT does not match destructor name ~%qT",
2388 object_type, dtor_type);
2389 return error_mark_node;
2393 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2395 error ("the type being destroyed is %qT, but the destructor refers to %qT",
2396 TYPE_MAIN_VARIANT (object_type), dtor_type);
2397 return error_mark_node;
2399 expr = lookup_member (dtor_type, complete_dtor_identifier,
2400 /*protect=*/1, /*want_type=*/false,
2401 tf_warning_or_error);
2402 expr = (adjust_result_of_qualified_name_lookup
2403 (expr, dtor_type, object_type));
2407 /* An expression of the form "A::template B" has been resolved to
2408 DECL. Issue a diagnostic if B is not a template or template
2412 check_template_keyword (tree decl)
2414 /* The standard says:
2418 If a name prefixed by the keyword template is not a member
2419 template, the program is ill-formed.
2421 DR 228 removed the restriction that the template be a member
2424 DR 96, if accepted would add the further restriction that explicit
2425 template arguments must be provided if the template keyword is
2426 used, but, as of 2005-10-16, that DR is still in "drafting". If
2427 this DR is accepted, then the semantic checks here can be
2428 simplified, as the entity named must in fact be a template
2429 specialization, rather than, as at present, a set of overloaded
2430 functions containing at least one template function. */
2431 if (TREE_CODE (decl) != TEMPLATE_DECL
2432 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2434 if (!is_overloaded_fn (decl))
2435 permerror (input_location, "%qD is not a template", decl);
2440 if (BASELINK_P (fns))
2441 fns = BASELINK_FUNCTIONS (fns);
2444 tree fn = OVL_CURRENT (fns);
2445 if (TREE_CODE (fn) == TEMPLATE_DECL
2446 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2448 if (TREE_CODE (fn) == FUNCTION_DECL
2449 && DECL_USE_TEMPLATE (fn)
2450 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2452 fns = OVL_NEXT (fns);
2455 permerror (input_location, "%qD is not a template", decl);
2460 /* This function is called by the parser to process a class member
2461 access expression of the form OBJECT.NAME. NAME is a node used by
2462 the parser to represent a name; it is not yet a DECL. It may,
2463 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2464 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2465 there is no reason to do the lookup twice, so the parser keeps the
2466 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2467 be a template via the use of the "A::template B" syntax. */
2470 finish_class_member_access_expr (tree object, tree name, bool template_p,
2471 tsubst_flags_t complain)
2476 tree access_path = NULL_TREE;
2477 tree orig_object = object;
2478 tree orig_name = name;
2480 if (object == error_mark_node || name == error_mark_node)
2481 return error_mark_node;
2483 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2484 if (!objc_is_public (object, name))
2485 return error_mark_node;
2487 object_type = TREE_TYPE (object);
2489 if (processing_template_decl)
2491 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2492 dependent_type_p (object_type)
2493 /* If NAME is just an IDENTIFIER_NODE, then the expression
2495 || TREE_CODE (object) == IDENTIFIER_NODE
2496 /* If NAME is "f<args>", where either 'f' or 'args' is
2497 dependent, then the expression is dependent. */
2498 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2499 && dependent_template_id_p (TREE_OPERAND (name, 0),
2500 TREE_OPERAND (name, 1)))
2501 /* If NAME is "T::X" where "T" is dependent, then the
2502 expression is dependent. */
2503 || (TREE_CODE (name) == SCOPE_REF
2504 && TYPE_P (TREE_OPERAND (name, 0))
2505 && dependent_type_p (TREE_OPERAND (name, 0))))
2506 return build_min_nt (COMPONENT_REF, object, name, NULL_TREE);
2507 object = build_non_dependent_expr (object);
2509 else if (c_dialect_objc ()
2510 && TREE_CODE (name) == IDENTIFIER_NODE
2511 && (expr = objc_maybe_build_component_ref (object, name)))
2516 The type of the first expression shall be "class object" (of a
2518 if (!currently_open_class (object_type)
2519 && !complete_type_or_maybe_complain (object_type, object, complain))
2520 return error_mark_node;
2521 if (!CLASS_TYPE_P (object_type))
2523 if (complain & tf_error)
2525 if (POINTER_TYPE_P (object_type)
2526 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2527 error ("request for member %qD in %qE, which is of pointer "
2528 "type %qT (maybe you meant to use %<->%> ?)",
2529 name, object, object_type);
2531 error ("request for member %qD in %qE, which is of non-class "
2532 "type %qT", name, object, object_type);
2534 return error_mark_node;
2537 if (BASELINK_P (name))
2538 /* A member function that has already been looked up. */
2542 bool is_template_id = false;
2543 tree template_args = NULL_TREE;
2546 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2548 is_template_id = true;
2549 template_args = TREE_OPERAND (name, 1);
2550 name = TREE_OPERAND (name, 0);
2552 if (TREE_CODE (name) == OVERLOAD)
2553 name = DECL_NAME (get_first_fn (name));
2554 else if (DECL_P (name))
2555 name = DECL_NAME (name);
2558 if (TREE_CODE (name) == SCOPE_REF)
2560 /* A qualified name. The qualifying class or namespace `S'
2561 has already been looked up; it is either a TYPE or a
2563 scope = TREE_OPERAND (name, 0);
2564 name = TREE_OPERAND (name, 1);
2566 /* If SCOPE is a namespace, then the qualified name does not
2567 name a member of OBJECT_TYPE. */
2568 if (TREE_CODE (scope) == NAMESPACE_DECL)
2570 if (complain & tf_error)
2571 error ("%<%D::%D%> is not a member of %qT",
2572 scope, name, object_type);
2573 return error_mark_node;
2576 gcc_assert (CLASS_TYPE_P (scope));
2577 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE
2578 || TREE_CODE (name) == BIT_NOT_EXPR);
2580 if (constructor_name_p (name, scope))
2582 if (complain & tf_error)
2583 error ("cannot call constructor %<%T::%D%> directly",
2585 return error_mark_node;
2588 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2589 access_path = lookup_base (object_type, scope, ba_check, NULL);
2590 if (access_path == error_mark_node)
2591 return error_mark_node;
2594 if (complain & tf_error)
2595 error ("%qT is not a base of %qT", scope, object_type);
2596 return error_mark_node;
2602 access_path = object_type;
2605 if (TREE_CODE (name) == BIT_NOT_EXPR)
2606 member = lookup_destructor (object, scope, name);
2609 /* Look up the member. */
2610 member = lookup_member (access_path, name, /*protect=*/1,
2611 /*want_type=*/false, complain);
2612 if (member == NULL_TREE)
2614 if (complain & tf_error)
2615 error ("%qD has no member named %qE",
2616 TREE_CODE (access_path) == TREE_BINFO
2617 ? TREE_TYPE (access_path) : object_type, name);
2618 return error_mark_node;
2620 if (member == error_mark_node)
2621 return error_mark_node;
2626 tree templ = member;
2628 if (BASELINK_P (templ))
2629 templ = lookup_template_function (templ, template_args);
2632 if (complain & tf_error)
2633 error ("%qD is not a member template function", name);
2634 return error_mark_node;
2639 if (TREE_DEPRECATED (member))
2640 warn_deprecated_use (member, NULL_TREE);
2643 check_template_keyword (member);
2645 expr = build_class_member_access_expr (object, member, access_path,
2646 /*preserve_reference=*/false,
2648 if (processing_template_decl && expr != error_mark_node)
2650 if (BASELINK_P (member))
2652 if (TREE_CODE (orig_name) == SCOPE_REF)
2653 BASELINK_QUALIFIED_P (member) = 1;
2656 return build_min_non_dep (COMPONENT_REF, expr,
2657 orig_object, orig_name,
2664 /* Return an expression for the MEMBER_NAME field in the internal
2665 representation of PTRMEM, a pointer-to-member function. (Each
2666 pointer-to-member function type gets its own RECORD_TYPE so it is
2667 more convenient to access the fields by name than by FIELD_DECL.)
2668 This routine converts the NAME to a FIELD_DECL and then creates the
2669 node for the complete expression. */
2672 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2678 /* This code is a stripped down version of
2679 build_class_member_access_expr. It does not work to use that
2680 routine directly because it expects the object to be of class
2682 ptrmem_type = TREE_TYPE (ptrmem);
2683 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2684 member = lookup_member (ptrmem_type, member_name, /*protect=*/0,
2685 /*want_type=*/false, tf_warning_or_error);
2686 member_type = cp_build_qualified_type (TREE_TYPE (member),
2687 cp_type_quals (ptrmem_type));
2688 return fold_build3_loc (input_location,
2689 COMPONENT_REF, member_type,
2690 ptrmem, member, NULL_TREE);
2693 /* Given an expression PTR for a pointer, return an expression
2694 for the value pointed to.
2695 ERRORSTRING is the name of the operator to appear in error messages.
2697 This function may need to overload OPERATOR_FNNAME.
2698 Must also handle REFERENCE_TYPEs for C++. */
2701 build_x_indirect_ref (tree expr, ref_operator errorstring,
2702 tsubst_flags_t complain)
2704 tree orig_expr = expr;
2707 if (processing_template_decl)
2709 /* Retain the type if we know the operand is a pointer. */
2710 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2711 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2712 if (type_dependent_expression_p (expr))
2713 return build_min_nt (INDIRECT_REF, expr);
2714 expr = build_non_dependent_expr (expr);
2717 rval = build_new_op (INDIRECT_REF, LOOKUP_NORMAL, expr, NULL_TREE,
2718 NULL_TREE, /*overload=*/NULL, complain);
2720 rval = cp_build_indirect_ref (expr, errorstring, complain);
2722 if (processing_template_decl && rval != error_mark_node)
2723 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2728 /* Helper function called from c-common. */
2730 build_indirect_ref (location_t loc ATTRIBUTE_UNUSED,
2731 tree ptr, ref_operator errorstring)
2733 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2737 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2738 tsubst_flags_t complain)
2742 if (ptr == error_mark_node)
2743 return error_mark_node;
2745 if (ptr == current_class_ptr)
2746 return current_class_ref;
2748 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2749 ? ptr : decay_conversion (ptr));
2750 type = TREE_TYPE (pointer);
2752 if (POINTER_TYPE_P (type))
2756 If the type of the expression is "pointer to T," the type
2757 of the result is "T." */
2758 tree t = TREE_TYPE (type);
2760 if (CONVERT_EXPR_P (ptr)
2761 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2763 /* If a warning is issued, mark it to avoid duplicates from
2764 the backend. This only needs to be done at
2765 warn_strict_aliasing > 2. */
2766 if (warn_strict_aliasing > 2)
2767 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2768 type, TREE_OPERAND (ptr, 0)))
2769 TREE_NO_WARNING (ptr) = 1;
2772 if (VOID_TYPE_P (t))
2774 /* A pointer to incomplete type (other than cv void) can be
2775 dereferenced [expr.unary.op]/1 */
2776 if (complain & tf_error)
2777 error ("%qT is not a pointer-to-object type", type);
2778 return error_mark_node;
2780 else if (TREE_CODE (pointer) == ADDR_EXPR
2781 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2782 /* The POINTER was something like `&x'. We simplify `*&x' to
2784 return TREE_OPERAND (pointer, 0);
2787 tree ref = build1 (INDIRECT_REF, t, pointer);
2789 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2790 so that we get the proper error message if the result is used
2791 to assign to. Also, &* is supposed to be a no-op. */
2792 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2793 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2794 TREE_SIDE_EFFECTS (ref)
2795 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2799 else if (!(complain & tf_error))
2800 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2802 /* `pointer' won't be an error_mark_node if we were given a
2803 pointer to member, so it's cool to check for this here. */
2804 else if (TYPE_PTR_TO_MEMBER_P (type))
2805 switch (errorstring)
2807 case RO_ARRAY_INDEXING:
2808 error ("invalid use of array indexing on pointer to member");
2811 error ("invalid use of unary %<*%> on pointer to member");
2813 case RO_IMPLICIT_CONVERSION:
2814 error ("invalid use of implicit conversion on pointer to member");
2819 else if (pointer != error_mark_node)
2820 invalid_indirection_error (input_location, type, errorstring);
2822 return error_mark_node;
2825 /* This handles expressions of the form "a[i]", which denotes
2828 This is logically equivalent in C to *(a+i), but we may do it differently.
2829 If A is a variable or a member, we generate a primitive ARRAY_REF.
2830 This avoids forcing the array out of registers, and can work on
2831 arrays that are not lvalues (for example, members of structures returned
2834 If INDEX is of some user-defined type, it must be converted to
2835 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2836 will inherit the type of the array, which will be some pointer type.
2838 LOC is the location to use in building the array reference. */
2841 cp_build_array_ref (location_t loc, tree array, tree idx,
2842 tsubst_flags_t complain)
2848 if (complain & tf_error)
2849 error_at (loc, "subscript missing in array reference");
2850 return error_mark_node;
2853 if (TREE_TYPE (array) == error_mark_node
2854 || TREE_TYPE (idx) == error_mark_node)
2855 return error_mark_node;
2857 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
2859 switch (TREE_CODE (array))
2863 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2865 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
2866 TREE_OPERAND (array, 0), value);
2867 SET_EXPR_LOCATION (ret, loc);
2872 ret = build_conditional_expr
2873 (TREE_OPERAND (array, 0),
2874 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
2876 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
2878 tf_warning_or_error);
2879 protected_set_expr_location (ret, loc);
2886 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2890 warn_array_subscript_with_type_char (idx);
2892 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
2894 if (complain & tf_error)
2895 error_at (loc, "array subscript is not an integer");
2896 return error_mark_node;
2899 /* Apply integral promotions *after* noticing character types.
2900 (It is unclear why we do these promotions -- the standard
2901 does not say that we should. In fact, the natural thing would
2902 seem to be to convert IDX to ptrdiff_t; we're performing
2903 pointer arithmetic.) */
2904 idx = perform_integral_promotions (idx);
2906 /* An array that is indexed by a non-constant
2907 cannot be stored in a register; we must be able to do
2908 address arithmetic on its address.
2909 Likewise an array of elements of variable size. */
2910 if (TREE_CODE (idx) != INTEGER_CST
2911 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2912 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2915 if (!cxx_mark_addressable (array))
2916 return error_mark_node;
2919 /* An array that is indexed by a constant value which is not within
2920 the array bounds cannot be stored in a register either; because we
2921 would get a crash in store_bit_field/extract_bit_field when trying
2922 to access a non-existent part of the register. */
2923 if (TREE_CODE (idx) == INTEGER_CST
2924 && TYPE_DOMAIN (TREE_TYPE (array))
2925 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
2927 if (!cxx_mark_addressable (array))
2928 return error_mark_node;
2931 if (!lvalue_p (array) && (complain & tf_error))
2932 pedwarn (loc, OPT_pedantic,
2933 "ISO C++ forbids subscripting non-lvalue array");
2935 /* Note in C++ it is valid to subscript a `register' array, since
2936 it is valid to take the address of something with that
2937 storage specification. */
2941 while (TREE_CODE (foo) == COMPONENT_REF)
2942 foo = TREE_OPERAND (foo, 0);
2943 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo)
2944 && (complain & tf_warning))
2945 warning_at (loc, OPT_Wextra,
2946 "subscripting array declared %<register%>");
2949 type = TREE_TYPE (TREE_TYPE (array));
2950 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
2951 /* Array ref is const/volatile if the array elements are
2952 or if the array is.. */
2953 TREE_READONLY (rval)
2954 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2955 TREE_SIDE_EFFECTS (rval)
2956 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2957 TREE_THIS_VOLATILE (rval)
2958 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2959 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
2961 protected_set_expr_location (ret, loc);
2966 tree ar = default_conversion (array);
2967 tree ind = default_conversion (idx);
2969 /* Put the integer in IND to simplify error checking. */
2970 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2977 if (ar == error_mark_node)
2980 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2982 if (complain & tf_error)
2983 error_at (loc, "subscripted value is neither array nor pointer");
2984 return error_mark_node;
2986 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2988 if (complain & tf_error)
2989 error_at (loc, "array subscript is not an integer");
2990 return error_mark_node;
2993 warn_array_subscript_with_type_char (idx);
2995 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3000 protected_set_expr_location (ret, loc);
3005 /* Entry point for Obj-C++. */
3008 build_array_ref (location_t loc, tree array, tree idx)
3010 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3013 /* Resolve a pointer to member function. INSTANCE is the object
3014 instance to use, if the member points to a virtual member.
3016 This used to avoid checking for virtual functions if basetype
3017 has no virtual functions, according to an earlier ANSI draft.
3018 With the final ISO C++ rules, such an optimization is
3019 incorrect: A pointer to a derived member can be static_cast
3020 to pointer-to-base-member, as long as the dynamic object
3021 later has the right member. */
3024 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function)
3026 if (TREE_CODE (function) == OFFSET_REF)
3027 function = TREE_OPERAND (function, 1);
3029 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3031 tree idx, delta, e1, e2, e3, vtbl, basetype;
3032 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3034 tree instance_ptr = *instance_ptrptr;
3035 tree instance_save_expr = 0;
3036 if (instance_ptr == error_mark_node)
3038 if (TREE_CODE (function) == PTRMEM_CST)
3040 /* Extracting the function address from a pmf is only
3041 allowed with -Wno-pmf-conversions. It only works for
3043 e1 = build_addr_func (PTRMEM_CST_MEMBER (function));
3044 e1 = convert (fntype, e1);
3049 error ("object missing in use of %qE", function);
3050 return error_mark_node;
3054 if (TREE_SIDE_EFFECTS (instance_ptr))
3055 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3057 if (TREE_SIDE_EFFECTS (function))
3058 function = save_expr (function);
3060 /* Start by extracting all the information from the PMF itself. */
3061 e3 = pfn_from_ptrmemfunc (function);
3062 delta = delta_from_ptrmemfunc (function);
3063 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3064 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3066 case ptrmemfunc_vbit_in_pfn:
3067 e1 = cp_build_binary_op (input_location,
3068 BIT_AND_EXPR, idx, integer_one_node,
3069 tf_warning_or_error);
3070 idx = cp_build_binary_op (input_location,
3071 MINUS_EXPR, idx, integer_one_node,
3072 tf_warning_or_error);
3075 case ptrmemfunc_vbit_in_delta:
3076 e1 = cp_build_binary_op (input_location,
3077 BIT_AND_EXPR, delta, integer_one_node,
3078 tf_warning_or_error);
3079 delta = cp_build_binary_op (input_location,
3080 RSHIFT_EXPR, delta, integer_one_node,
3081 tf_warning_or_error);
3088 /* Convert down to the right base before using the instance. A
3089 special case is that in a pointer to member of class C, C may
3090 be incomplete. In that case, the function will of course be
3091 a member of C, and no conversion is required. In fact,
3092 lookup_base will fail in that case, because incomplete
3093 classes do not have BINFOs. */
3094 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3095 if (!same_type_ignoring_top_level_qualifiers_p
3096 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3098 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3099 basetype, ba_check, NULL);
3100 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3101 1, tf_warning_or_error);
3102 if (instance_ptr == error_mark_node)
3103 return error_mark_node;
3105 /* ...and then the delta in the PMF. */
3106 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3108 /* Hand back the adjusted 'this' argument to our caller. */
3109 *instance_ptrptr = instance_ptr;
3111 /* Next extract the vtable pointer from the object. */
3112 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3114 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, tf_warning_or_error);
3115 /* If the object is not dynamic the access invokes undefined
3116 behavior. As it is not executed in this case silence the
3117 spurious warnings it may provoke. */
3118 TREE_NO_WARNING (vtbl) = 1;
3120 /* Finally, extract the function pointer from the vtable. */
3121 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3122 e2 = cp_build_indirect_ref (e2, RO_NULL, tf_warning_or_error);
3123 TREE_CONSTANT (e2) = 1;
3125 /* When using function descriptors, the address of the
3126 vtable entry is treated as a function pointer. */
3127 if (TARGET_VTABLE_USES_DESCRIPTORS)
3128 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3129 cp_build_addr_expr (e2, tf_warning_or_error));
3131 e2 = fold_convert (TREE_TYPE (e3), e2);
3132 e1 = build_conditional_expr (e1, e2, e3, tf_warning_or_error);
3134 /* Make sure this doesn't get evaluated first inside one of the
3135 branches of the COND_EXPR. */
3136 if (instance_save_expr)
3137 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3138 instance_save_expr, e1);
3145 /* Used by the C-common bits. */
3147 build_function_call (location_t loc ATTRIBUTE_UNUSED,
3148 tree function, tree params)
3150 return cp_build_function_call (function, params, tf_warning_or_error);
3153 /* Used by the C-common bits. */
3155 build_function_call_vec (location_t loc ATTRIBUTE_UNUSED,
3156 tree function, VEC(tree,gc) *params,
3157 VEC(tree,gc) *origtypes ATTRIBUTE_UNUSED)
3159 VEC(tree,gc) *orig_params = params;
3160 tree ret = cp_build_function_call_vec (function, ¶ms,
3161 tf_warning_or_error);
3163 /* cp_build_function_call_vec can reallocate PARAMS by adding
3164 default arguments. That should never happen here. Verify
3166 gcc_assert (params == orig_params);
3171 /* Build a function call using a tree list of arguments. */
3174 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3179 vec = make_tree_vector ();
3180 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3181 VEC_safe_push (tree, gc, vec, TREE_VALUE (params));
3182 ret = cp_build_function_call_vec (function, &vec, complain);
3183 release_tree_vector (vec);
3187 /* Build a function call using varargs. */
3190 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3196 vec = make_tree_vector ();
3197 va_start (args, complain);
3198 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3199 VEC_safe_push (tree, gc, vec, t);
3201 ret = cp_build_function_call_vec (function, &vec, complain);
3202 release_tree_vector (vec);
3206 /* Build a function call using a vector of arguments. PARAMS may be
3207 NULL if there are no parameters. This changes the contents of
3211 cp_build_function_call_vec (tree function, VEC(tree,gc) **params,
3212 tsubst_flags_t complain)
3214 tree fntype, fndecl;
3216 tree original = function;
3220 VEC(tree,gc) *allocated = NULL;
3223 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3224 expressions, like those used for ObjC messenger dispatches. */
3225 if (params != NULL && !VEC_empty (tree, *params))
3226 function = objc_rewrite_function_call (function,
3227 VEC_index (tree, *params, 0));
3229 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3230 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3231 if (TREE_CODE (function) == NOP_EXPR
3232 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3233 function = TREE_OPERAND (function, 0);
3235 if (TREE_CODE (function) == FUNCTION_DECL)
3237 mark_used (function);
3240 /* Convert anything with function type to a pointer-to-function. */
3241 if (DECL_MAIN_P (function) && (complain & tf_error))
3242 pedwarn (input_location, OPT_pedantic,
3243 "ISO C++ forbids calling %<::main%> from within program");
3245 function = build_addr_func (function);
3251 function = build_addr_func (function);
3254 if (function == error_mark_node)
3255 return error_mark_node;
3257 fntype = TREE_TYPE (function);
3259 if (TYPE_PTRMEMFUNC_P (fntype))
3261 if (complain & tf_error)
3262 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3263 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3264 original, original);
3265 return error_mark_node;
3268 is_method = (TREE_CODE (fntype) == POINTER_TYPE
3269 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3271 if (!((TREE_CODE (fntype) == POINTER_TYPE
3272 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3274 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3276 if (complain & tf_error)
3277 error ("%qE cannot be used as a function", original);
3278 return error_mark_node;
3281 /* fntype now gets the type of function pointed to. */
3282 fntype = TREE_TYPE (fntype);
3283 parm_types = TYPE_ARG_TYPES (fntype);
3287 allocated = make_tree_vector ();
3288 params = &allocated;
3291 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3294 return error_mark_node;
3296 argarray = VEC_address (tree, *params);
3298 /* Check for errors in format strings and inappropriately
3300 check_function_arguments (fntype, nargs, argarray);
3302 ret = build_cxx_call (function, nargs, argarray);
3304 if (allocated != NULL)
3305 release_tree_vector (allocated);
3310 /* Subroutine of convert_arguments.
3311 Warn about wrong number of args are genereted. */
3314 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3318 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3320 if (DECL_NAME (fndecl) == NULL_TREE
3321 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3324 ? G_("too many arguments to constructor %q#D")
3325 : G_("too few arguments to constructor %q#D"),
3330 ? G_("too many arguments to member function %q#D")
3331 : G_("too few arguments to member function %q#D"),
3337 ? G_("too many arguments to function %q#D")
3338 : G_("too few arguments to function %q#D"),
3340 inform (DECL_SOURCE_LOCATION (fndecl),
3345 if (c_dialect_objc () && objc_message_selector ())
3348 ? G_("too many arguments to method %q#D")
3349 : G_("too few arguments to method %q#D"),
3350 objc_message_selector ());
3352 error_at (loc, too_many_p ? G_("too many arguments to function")
3353 : G_("too few arguments to function"));
3357 /* Convert the actual parameter expressions in the list VALUES to the
3358 types in the list TYPELIST. The converted expressions are stored
3359 back in the VALUES vector.
3360 If parmdecls is exhausted, or when an element has NULL as its type,
3361 perform the default conversions.
3363 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3365 This is also where warnings about wrong number of args are generated.
3367 Returns the actual number of arguments processed (which might be less
3368 than the length of the vector), or -1 on error.
3370 In C++, unspecified trailing parameters can be filled in with their
3371 default arguments, if such were specified. Do so here. */
3374 convert_arguments (tree typelist, VEC(tree,gc) **values, tree fndecl,
3375 int flags, tsubst_flags_t complain)
3380 /* Argument passing is always copy-initialization. */
3381 flags |= LOOKUP_ONLYCONVERTING;
3383 for (i = 0, typetail = typelist;
3384 i < VEC_length (tree, *values);
3387 tree type = typetail ? TREE_VALUE (typetail) : 0;
3388 tree val = VEC_index (tree, *values, i);
3390 if (val == error_mark_node || type == error_mark_node)
3393 if (type == void_type_node)
3395 if (complain & tf_error)
3397 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3404 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3405 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3406 if (TREE_CODE (val) == NOP_EXPR
3407 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3408 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3409 val = TREE_OPERAND (val, 0);
3411 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3413 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3414 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3415 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3416 val = decay_conversion (val);
3419 if (val == error_mark_node)
3424 /* Formal parm type is specified by a function prototype. */
3427 if (!COMPLETE_TYPE_P (complete_type (type)))
3429 if (complain & tf_error)
3432 error ("parameter %P of %qD has incomplete type %qT",
3435 error ("parameter %P has incomplete type %qT", i, type);
3437 parmval = error_mark_node;
3441 parmval = convert_for_initialization
3442 (NULL_TREE, type, val, flags,
3443 ICR_ARGPASS, fndecl, i, complain);
3444 parmval = convert_for_arg_passing (type, parmval);
3447 if (parmval == error_mark_node)
3450 VEC_replace (tree, *values, i, parmval);
3454 if (fndecl && DECL_BUILT_IN (fndecl)
3455 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
3456 /* Don't do ellipsis conversion for __built_in_constant_p
3457 as this will result in spurious errors for non-trivial
3459 val = require_complete_type_sfinae (val, complain);
3461 val = convert_arg_to_ellipsis (val);
3463 VEC_replace (tree, *values, i, val);
3467 typetail = TREE_CHAIN (typetail);
3470 if (typetail != 0 && typetail != void_list_node)
3472 /* See if there are default arguments that can be used. Because
3473 we hold default arguments in the FUNCTION_TYPE (which is so
3474 wrong), we can see default parameters here from deduced
3475 contexts (and via typeof) for indirect function calls.
3476 Fortunately we know whether we have a function decl to
3477 provide default arguments in a language conformant
3479 if (fndecl && TREE_PURPOSE (typetail)
3480 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3482 for (; typetail != void_list_node; ++i)
3485 = convert_default_arg (TREE_VALUE (typetail),
3486 TREE_PURPOSE (typetail),
3489 if (parmval == error_mark_node)
3492 VEC_safe_push (tree, gc, *values, parmval);
3493 typetail = TREE_CHAIN (typetail);
3494 /* ends with `...'. */
3495 if (typetail == NULL_TREE)
3501 if (complain & tf_error)
3502 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3510 /* Build a binary-operation expression, after performing default
3511 conversions on the operands. CODE is the kind of expression to
3512 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3513 are the tree codes which correspond to ARG1 and ARG2 when issuing
3514 warnings about possibly misplaced parentheses. They may differ
3515 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3516 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3517 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3518 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3519 ARG2_CODE as ERROR_MARK. */
3522 build_x_binary_op (enum tree_code code, tree arg1, enum tree_code arg1_code,
3523 tree arg2, enum tree_code arg2_code, tree *overload,
3524 tsubst_flags_t complain)
3533 if (processing_template_decl)
3535 if (type_dependent_expression_p (arg1)
3536 || type_dependent_expression_p (arg2))
3537 return build_min_nt (code, arg1, arg2);
3538 arg1 = build_non_dependent_expr (arg1);
3539 arg2 = build_non_dependent_expr (arg2);
3542 if (code == DOTSTAR_EXPR)
3543 expr = build_m_component_ref (arg1, arg2);
3545 expr = build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3546 overload, complain);
3548 /* Check for cases such as x+y<<z which users are likely to
3549 misinterpret. But don't warn about obj << x + y, since that is a
3550 common idiom for I/O. */
3551 if (warn_parentheses
3552 && (complain & tf_warning)
3553 && !processing_template_decl
3554 && !error_operand_p (arg1)
3555 && !error_operand_p (arg2)
3556 && (code != LSHIFT_EXPR
3557 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3558 warn_about_parentheses (code, arg1_code, orig_arg1, arg2_code, orig_arg2);
3560 if (processing_template_decl && expr != error_mark_node)
3561 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3566 /* Build and return an ARRAY_REF expression. */
3569 build_x_array_ref (tree arg1, tree arg2, tsubst_flags_t complain)
3571 tree orig_arg1 = arg1;
3572 tree orig_arg2 = arg2;
3575 if (processing_template_decl)
3577 if (type_dependent_expression_p (arg1)
3578 || type_dependent_expression_p (arg2))
3579 return build_min_nt (ARRAY_REF, arg1, arg2,
3580 NULL_TREE, NULL_TREE);
3581 arg1 = build_non_dependent_expr (arg1);
3582 arg2 = build_non_dependent_expr (arg2);
3585 expr = build_new_op (ARRAY_REF, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3586 /*overload=*/NULL, complain);
3588 if (processing_template_decl && expr != error_mark_node)
3589 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3590 NULL_TREE, NULL_TREE);
3594 /* For the c-common bits. */
3596 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3597 int convert_p ATTRIBUTE_UNUSED)
3599 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3603 /* Build a binary-operation expression without default conversions.
3604 CODE is the kind of expression to build.
3605 LOCATION is the location_t of the operator in the source code.
3606 This function differs from `build' in several ways:
3607 the data type of the result is computed and recorded in it,
3608 warnings are generated if arg data types are invalid,
3609 special handling for addition and subtraction of pointers is known,
3610 and some optimization is done (operations on narrow ints
3611 are done in the narrower type when that gives the same result).
3612 Constant folding is also done before the result is returned.
3614 Note that the operands will never have enumeral types
3615 because either they have just had the default conversions performed
3616 or they have both just been converted to some other type in which
3617 the arithmetic is to be done.
3619 C++: must do special pointer arithmetic when implementing
3620 multiple inheritance, and deal with pointer to member functions. */
3623 cp_build_binary_op (location_t location,
3624 enum tree_code code, tree orig_op0, tree orig_op1,
3625 tsubst_flags_t complain)
3628 enum tree_code code0, code1;
3630 const char *invalid_op_diag;
3632 /* Expression code to give to the expression when it is built.
3633 Normally this is CODE, which is what the caller asked for,
3634 but in some special cases we change it. */
3635 enum tree_code resultcode = code;
3637 /* Data type in which the computation is to be performed.
3638 In the simplest cases this is the common type of the arguments. */
3639 tree result_type = NULL;
3641 /* Nonzero means operands have already been type-converted
3642 in whatever way is necessary.
3643 Zero means they need to be converted to RESULT_TYPE. */
3646 /* Nonzero means create the expression with this type, rather than
3648 tree build_type = 0;
3650 /* Nonzero means after finally constructing the expression
3651 convert it to this type. */
3652 tree final_type = 0;
3656 /* Nonzero if this is an operation like MIN or MAX which can
3657 safely be computed in short if both args are promoted shorts.
3658 Also implies COMMON.
3659 -1 indicates a bitwise operation; this makes a difference
3660 in the exact conditions for when it is safe to do the operation
3661 in a narrower mode. */
3664 /* Nonzero if this is a comparison operation;
3665 if both args are promoted shorts, compare the original shorts.
3666 Also implies COMMON. */
3667 int short_compare = 0;
3669 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3672 /* True if both operands have arithmetic type. */
3673 bool arithmetic_types_p;
3675 /* Apply default conversions. */
3679 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3680 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3681 || code == TRUTH_XOR_EXPR)
3683 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3684 op0 = decay_conversion (op0);
3685 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3686 op1 = decay_conversion (op1);
3690 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3691 op0 = default_conversion (op0);
3692 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3693 op1 = default_conversion (op1);
3696 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3697 STRIP_TYPE_NOPS (op0);
3698 STRIP_TYPE_NOPS (op1);
3700 /* DTRT if one side is an overloaded function, but complain about it. */
3701 if (type_unknown_p (op0))
3703 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3704 if (t != error_mark_node)
3706 if (complain & tf_error)
3707 permerror (input_location, "assuming cast to type %qT from overloaded function",
3712 if (type_unknown_p (op1))
3714 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3715 if (t != error_mark_node)
3717 if (complain & tf_error)
3718 permerror (input_location, "assuming cast to type %qT from overloaded function",
3724 type0 = TREE_TYPE (op0);
3725 type1 = TREE_TYPE (op1);
3727 /* The expression codes of the data types of the arguments tell us
3728 whether the arguments are integers, floating, pointers, etc. */
3729 code0 = TREE_CODE (type0);
3730 code1 = TREE_CODE (type1);
3732 /* If an error was already reported for one of the arguments,
3733 avoid reporting another error. */
3734 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3735 return error_mark_node;
3737 if ((invalid_op_diag
3738 = targetm.invalid_binary_op (code, type0, type1)))
3740 error (invalid_op_diag);
3741 return error_mark_node;
3744 /* Issue warnings about peculiar, but valid, uses of NULL. */
3745 if ((orig_op0 == null_node || orig_op1 == null_node)
3746 /* It's reasonable to use pointer values as operands of &&
3747 and ||, so NULL is no exception. */
3748 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
3749 && ( /* Both are NULL (or 0) and the operation was not a
3750 comparison or a pointer subtraction. */
3751 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
3752 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
3753 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3754 || (!null_ptr_cst_p (orig_op0)
3755 && !TYPE_PTR_P (type0) && !TYPE_PTR_TO_MEMBER_P (type0))
3756 || (!null_ptr_cst_p (orig_op1)
3757 && !TYPE_PTR_P (type1) && !TYPE_PTR_TO_MEMBER_P (type1)))
3758 && (complain & tf_warning))
3759 /* Some sort of arithmetic operation involving NULL was
3761 warning (OPT_Wpointer_arith, "NULL used in arithmetic");
3766 /* Subtraction of two similar pointers.
3767 We must subtract them as integers, then divide by object size. */
3768 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3769 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
3771 return pointer_diff (op0, op1, common_pointer_type (type0, type1));
3772 /* In all other cases except pointer - int, the usual arithmetic
3774 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
3779 /* The pointer - int case is just like pointer + int; fall
3782 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
3783 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
3787 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
3788 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
3789 if (processing_template_decl)
3791 result_type = TREE_TYPE (ptr_operand);
3794 return cp_pointer_int_sum (code,
3805 case TRUNC_DIV_EXPR:
3807 case FLOOR_DIV_EXPR:
3808 case ROUND_DIV_EXPR:
3809 case EXACT_DIV_EXPR:
3810 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3811 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
3812 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3813 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
3815 enum tree_code tcode0 = code0, tcode1 = code1;
3817 warn_for_div_by_zero (location, op1);
3819 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
3820 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3821 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
3822 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3824 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
3825 resultcode = RDIV_EXPR;
3827 /* When dividing two signed integers, we have to promote to int.
3828 unless we divide by a constant != -1. Note that default
3829 conversion will have been performed on the operands at this
3830 point, so we have to dig out the original type to find out if
3832 shorten = ((TREE_CODE (op0) == NOP_EXPR
3833 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3834 || (TREE_CODE (op1) == INTEGER_CST
3835 && ! integer_all_onesp (op1)));
3844 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3845 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3846 && !VECTOR_FLOAT_TYPE_P (type0)
3847 && !VECTOR_FLOAT_TYPE_P (type1)))
3851 case TRUNC_MOD_EXPR:
3852 case FLOOR_MOD_EXPR:
3853 warn_for_div_by_zero (location, op1);
3855 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
3856 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
3857 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
3859 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3861 /* Although it would be tempting to shorten always here, that loses
3862 on some targets, since the modulo instruction is undefined if the
3863 quotient can't be represented in the computation mode. We shorten
3864 only if unsigned or if dividing by something we know != -1. */
3865 shorten = ((TREE_CODE (op0) == NOP_EXPR
3866 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3867 || (TREE_CODE (op1) == INTEGER_CST
3868 && ! integer_all_onesp (op1)));
3873 case TRUTH_ANDIF_EXPR:
3874 case TRUTH_ORIF_EXPR:
3875 case TRUTH_AND_EXPR:
3877 result_type = boolean_type_node;
3880 /* Shift operations: result has same type as first operand;
3881 always convert second operand to int.
3882 Also set SHORT_SHIFT if shifting rightward. */
3885 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3887 result_type = type0;
3888 if (TREE_CODE (op1) == INTEGER_CST)
3890 if (tree_int_cst_lt (op1, integer_zero_node))
3892 if ((complain & tf_warning)
3893 && c_inhibit_evaluation_warnings == 0)
3894 warning (0, "right shift count is negative");
3898 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0
3899 && (complain & tf_warning)
3900 && c_inhibit_evaluation_warnings == 0)
3901 warning (0, "right shift count >= width of type");
3904 /* Convert the shift-count to an integer, regardless of
3905 size of value being shifted. */
3906 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3907 op1 = cp_convert (integer_type_node, op1);
3908 /* Avoid converting op1 to result_type later. */
3914 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3916 result_type = type0;
3917 if (TREE_CODE (op1) == INTEGER_CST)
3919 if (tree_int_cst_lt (op1, integer_zero_node))
3921 if ((complain & tf_warning)
3922 && c_inhibit_evaluation_warnings == 0)
3923 warning (0, "left shift count is negative");
3925 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3927 if ((complain & tf_warning)
3928 && c_inhibit_evaluation_warnings == 0)
3929 warning (0, "left shift count >= width of type");
3932 /* Convert the shift-count to an integer, regardless of
3933 size of value being shifted. */
3934 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3935 op1 = cp_convert (integer_type_node, op1);
3936 /* Avoid converting op1 to result_type later. */
3943 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3945 result_type = type0;
3946 if (TREE_CODE (op1) == INTEGER_CST)
3948 if (tree_int_cst_lt (op1, integer_zero_node))
3950 if (complain & tf_warning)
3951 warning (0, (code == LROTATE_EXPR)
3952 ? G_("left rotate count is negative")
3953 : G_("right rotate count is negative"));
3955 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
3957 if (complain & tf_warning)
3958 warning (0, (code == LROTATE_EXPR)
3959 ? G_("left rotate count >= width of type")
3960 : G_("right rotate count >= width of type"));
3963 /* Convert the shift-count to an integer, regardless of
3964 size of value being shifted. */
3965 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3966 op1 = cp_convert (integer_type_node, op1);
3972 if ((complain & tf_warning)
3973 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
3974 warning (OPT_Wfloat_equal,
3975 "comparing floating point with == or != is unsafe");
3976 if ((complain & tf_warning)
3977 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
3978 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
3979 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
3981 build_type = boolean_type_node;
3982 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3983 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
3984 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3985 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
3987 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3988 || (TYPE_PTRMEM_P (type0) && TYPE_PTRMEM_P (type1)))
3989 result_type = composite_pointer_type (type0, type1, op0, op1,
3990 CPO_COMPARISON, complain);
3991 else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
3992 && null_ptr_cst_p (op1))
3994 if (TREE_CODE (op0) == ADDR_EXPR
3995 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
3997 if (complain & tf_warning)
3998 warning (OPT_Waddress, "the address of %qD will never be NULL",
3999 TREE_OPERAND (op0, 0));
4001 result_type = type0;
4003 else if ((code1 == POINTER_TYPE || TYPE_PTRMEM_P (type1))
4004 && null_ptr_cst_p (op0))
4006 if (TREE_CODE (op1) == ADDR_EXPR
4007 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4009 if (complain & tf_warning)
4010 warning (OPT_Waddress, "the address of %qD will never be NULL",
4011 TREE_OPERAND (op1, 0));
4013 result_type = type1;
4015 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4016 /* One of the operands must be of nullptr_t type. */
4017 result_type = TREE_TYPE (nullptr_node);
4018 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4020 result_type = type0;
4021 if (complain & tf_error)
4022 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4024 return error_mark_node;
4026 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4028 result_type = type1;
4029 if (complain & tf_error)
4030 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4032 return error_mark_node;
4034 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4036 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4037 == ptrmemfunc_vbit_in_delta)
4039 tree pfn0 = pfn_from_ptrmemfunc (op0);
4040 tree delta0 = delta_from_ptrmemfunc (op0);
4041 tree e1 = cp_build_binary_op (location,
4044 build_zero_cst (TREE_TYPE (pfn0)),
4046 tree e2 = cp_build_binary_op (location,
4051 e2 = cp_build_binary_op (location,
4052 EQ_EXPR, e2, integer_zero_node,
4054 op0 = cp_build_binary_op (location,
4055 TRUTH_ANDIF_EXPR, e1, e2,
4057 op1 = cp_convert (TREE_TYPE (op0), integer_one_node);
4061 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4062 op1 = cp_convert (TREE_TYPE (op0), op1);
4064 result_type = TREE_TYPE (op0);
4066 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4067 return cp_build_binary_op (location, code, op1, op0, complain);
4068 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4071 /* E will be the final comparison. */
4073 /* E1 and E2 are for scratch. */
4081 type = composite_pointer_type (type0, type1, op0, op1,
4082 CPO_COMPARISON, complain);
4084 if (!same_type_p (TREE_TYPE (op0), type))
4085 op0 = cp_convert_and_check (type, op0);
4086 if (!same_type_p (TREE_TYPE (op1), type))
4087 op1 = cp_convert_and_check (type, op1);
4089 if (op0 == error_mark_node || op1 == error_mark_node)
4090 return error_mark_node;
4092 if (TREE_SIDE_EFFECTS (op0))
4093 op0 = save_expr (op0);
4094 if (TREE_SIDE_EFFECTS (op1))
4095 op1 = save_expr (op1);
4097 pfn0 = pfn_from_ptrmemfunc (op0);
4098 pfn1 = pfn_from_ptrmemfunc (op1);
4099 delta0 = delta_from_ptrmemfunc (op0);
4100 delta1 = delta_from_ptrmemfunc (op1);
4101 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4102 == ptrmemfunc_vbit_in_delta)
4107 && ((op0.delta == op1.delta)
4108 || (!op0.pfn && op0.delta & 1 == 0
4109 && op1.delta & 1 == 0))
4111 The reason for the `!op0.pfn' bit is that a NULL
4112 pointer-to-member is any member with a zero PFN and
4113 LSB of the DELTA field is 0. */
4115 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4119 e1 = cp_build_binary_op (location,
4120 EQ_EXPR, e1, integer_zero_node,
4122 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4126 e2 = cp_build_binary_op (location,
4127 EQ_EXPR, e2, integer_zero_node,
4129 e1 = cp_build_binary_op (location,
4130 TRUTH_ANDIF_EXPR, e2, e1,
4132 e2 = cp_build_binary_op (location, EQ_EXPR,
4134 build_zero_cst (TREE_TYPE (pfn0)),
4136 e2 = cp_build_binary_op (location,
4137 TRUTH_ANDIF_EXPR, e2, e1, complain);
4138 e1 = cp_build_binary_op (location,
4139 EQ_EXPR, delta0, delta1, complain);
4140 e1 = cp_build_binary_op (location,
4141 TRUTH_ORIF_EXPR, e1, e2, complain);
4148 && (!op0.pfn || op0.delta == op1.delta))
4150 The reason for the `!op0.pfn' bit is that a NULL
4151 pointer-to-member is any member with a zero PFN; the
4152 DELTA field is unspecified. */
4154 e1 = cp_build_binary_op (location,
4155 EQ_EXPR, delta0, delta1, complain);
4156 e2 = cp_build_binary_op (location,
4159 build_zero_cst (TREE_TYPE (pfn0)),
4161 e1 = cp_build_binary_op (location,
4162 TRUTH_ORIF_EXPR, e1, e2, complain);
4164 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4165 e = cp_build_binary_op (location,
4166 TRUTH_ANDIF_EXPR, e2, e1, complain);
4167 if (code == EQ_EXPR)
4169 return cp_build_binary_op (location,
4170 EQ_EXPR, e, integer_zero_node, complain);
4174 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4175 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4177 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4178 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4186 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4187 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4189 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4190 result_type = composite_pointer_type (type0, type1, op0, op1,
4191 CPO_COMPARISON, complain);
4198 if (TREE_CODE (orig_op0) == STRING_CST
4199 || TREE_CODE (orig_op1) == STRING_CST)
4201 if (complain & tf_warning)
4202 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4205 build_type = boolean_type_node;
4206 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4207 || code0 == ENUMERAL_TYPE)
4208 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4209 || code1 == ENUMERAL_TYPE))
4211 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4212 result_type = composite_pointer_type (type0, type1, op0, op1,
4213 CPO_COMPARISON, complain);
4214 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4216 result_type = type0;
4217 if (extra_warnings && (complain & tf_warning))
4218 warning (OPT_Wextra,
4219 "ordered comparison of pointer with integer zero");
4221 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4223 result_type = type1;
4224 if (extra_warnings && (complain & tf_warning))
4225 warning (OPT_Wextra,
4226 "ordered comparison of pointer with integer zero");
4228 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4229 /* One of the operands must be of nullptr_t type. */
4230 result_type = TREE_TYPE (nullptr_node);
4231 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4233 result_type = type0;
4234 if (complain & tf_error)
4235 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4237 return error_mark_node;
4239 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4241 result_type = type1;
4242 if (complain & tf_error)
4243 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4245 return error_mark_node;
4249 case UNORDERED_EXPR:
4256 build_type = integer_type_node;
4257 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4259 if (complain & tf_error)
4260 error ("unordered comparison on non-floating point argument");
4261 return error_mark_node;
4270 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4271 || code0 == ENUMERAL_TYPE)
4272 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4273 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4274 arithmetic_types_p = 1;
4277 arithmetic_types_p = 0;
4278 /* Vector arithmetic is only allowed when both sides are vectors. */
4279 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4281 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4282 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
4285 binary_op_error (location, code, type0, type1);
4286 return error_mark_node;
4288 arithmetic_types_p = 1;
4291 /* Determine the RESULT_TYPE, if it is not already known. */
4293 && arithmetic_types_p
4294 && (shorten || common || short_compare))
4296 result_type = cp_common_type (type0, type1);
4297 do_warn_double_promotion (result_type, type0, type1,
4298 "implicit conversion from %qT to %qT "
4299 "to match other operand of binary "
4306 if (complain & tf_error)
4307 error ("invalid operands of types %qT and %qT to binary %qO",
4308 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4309 return error_mark_node;
4312 /* If we're in a template, the only thing we need to know is the
4314 if (processing_template_decl)
4316 /* Since the middle-end checks the type when doing a build2, we
4317 need to build the tree in pieces. This built tree will never
4318 get out of the front-end as we replace it when instantiating
4320 tree tmp = build2 (resultcode,
4321 build_type ? build_type : result_type,
4323 TREE_OPERAND (tmp, 0) = op0;
4327 if (arithmetic_types_p)
4329 bool first_complex = (code0 == COMPLEX_TYPE);
4330 bool second_complex = (code1 == COMPLEX_TYPE);
4331 int none_complex = (!first_complex && !second_complex);
4333 /* Adapted from patch for c/24581. */
4334 if (first_complex != second_complex
4335 && (code == PLUS_EXPR
4336 || code == MINUS_EXPR
4337 || code == MULT_EXPR
4338 || (code == TRUNC_DIV_EXPR && first_complex))
4339 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4340 && flag_signed_zeros)
4342 /* An operation on mixed real/complex operands must be
4343 handled specially, but the language-independent code can
4344 more easily optimize the plain complex arithmetic if
4345 -fno-signed-zeros. */
4346 tree real_type = TREE_TYPE (result_type);
4350 if (TREE_TYPE (op0) != result_type)
4351 op0 = cp_convert_and_check (result_type, op0);
4352 if (TREE_TYPE (op1) != real_type)
4353 op1 = cp_convert_and_check (real_type, op1);
4357 if (TREE_TYPE (op0) != real_type)
4358 op0 = cp_convert_and_check (real_type, op0);
4359 if (TREE_TYPE (op1) != result_type)
4360 op1 = cp_convert_and_check (result_type, op1);
4362 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4363 return error_mark_node;
4366 op0 = save_expr (op0);
4367 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4368 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4372 case TRUNC_DIV_EXPR:
4373 op1 = save_expr (op1);
4374 imag = build2 (resultcode, real_type, imag, op1);
4378 real = build2 (resultcode, real_type, real, op1);
4386 op1 = save_expr (op1);
4387 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4388 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4392 op0 = save_expr (op0);
4393 imag = build2 (resultcode, real_type, op0, imag);
4396 real = build2 (resultcode, real_type, op0, real);
4399 real = build2 (resultcode, real_type, op0, real);
4400 imag = build1 (NEGATE_EXPR, real_type, imag);
4406 real = fold_if_not_in_template (real);
4407 imag = fold_if_not_in_template (imag);
4408 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4409 result = fold_if_not_in_template (result);
4413 /* For certain operations (which identify themselves by shorten != 0)
4414 if both args were extended from the same smaller type,
4415 do the arithmetic in that type and then extend.
4417 shorten !=0 and !=1 indicates a bitwise operation.
4418 For them, this optimization is safe only if
4419 both args are zero-extended or both are sign-extended.
4420 Otherwise, we might change the result.
4421 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4422 but calculated in (unsigned short) it would be (unsigned short)-1. */
4424 if (shorten && none_complex)
4426 final_type = result_type;
4427 result_type = shorten_binary_op (result_type, op0, op1,
4431 /* Comparison operations are shortened too but differently.
4432 They identify themselves by setting short_compare = 1. */
4436 /* Don't write &op0, etc., because that would prevent op0
4437 from being kept in a register.
4438 Instead, make copies of the our local variables and
4439 pass the copies by reference, then copy them back afterward. */
4440 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4441 enum tree_code xresultcode = resultcode;
4443 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
4445 return cp_convert (boolean_type_node, val);
4446 op0 = xop0, op1 = xop1;
4448 resultcode = xresultcode;
4451 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4452 && warn_sign_compare
4453 && !TREE_NO_WARNING (orig_op0)
4454 && !TREE_NO_WARNING (orig_op1)
4455 /* Do not warn until the template is instantiated; we cannot
4456 bound the ranges of the arguments until that point. */
4457 && !processing_template_decl
4458 && (complain & tf_warning)
4459 && c_inhibit_evaluation_warnings == 0)
4461 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
4462 result_type, resultcode);
4466 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4467 Then the expression will be built.
4468 It will be given type FINAL_TYPE if that is nonzero;
4469 otherwise, it will be given type RESULT_TYPE. */
4472 if (TREE_TYPE (op0) != result_type)
4473 op0 = cp_convert_and_check (result_type, op0);
4474 if (TREE_TYPE (op1) != result_type)
4475 op1 = cp_convert_and_check (result_type, op1);
4477 if (op0 == error_mark_node || op1 == error_mark_node)
4478 return error_mark_node;
4481 if (build_type == NULL_TREE)
4482 build_type = result_type;
4484 result = build2 (resultcode, build_type, op0, op1);
4485 result = fold_if_not_in_template (result);
4486 if (final_type != 0)
4487 result = cp_convert (final_type, result);
4489 if (TREE_OVERFLOW_P (result)
4490 && !TREE_OVERFLOW_P (op0)
4491 && !TREE_OVERFLOW_P (op1))
4492 overflow_warning (location, result);
4497 /* Return a tree for the sum or difference (RESULTCODE says which)
4498 of pointer PTROP and integer INTOP. */
4501 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
4503 tree res_type = TREE_TYPE (ptrop);
4505 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4506 in certain circumstance (when it's valid to do so). So we need
4507 to make sure it's complete. We don't need to check here, if we
4508 can actually complete it at all, as those checks will be done in
4509 pointer_int_sum() anyway. */
4510 complete_type (TREE_TYPE (res_type));
4512 return pointer_int_sum (input_location, resultcode, ptrop,
4513 fold_if_not_in_template (intop));
4516 /* Return a tree for the difference of pointers OP0 and OP1.
4517 The resulting tree has type int. */
4520 pointer_diff (tree op0, tree op1, tree ptrtype)
4523 tree restype = ptrdiff_type_node;
4524 tree target_type = TREE_TYPE (ptrtype);
4526 if (!complete_type_or_else (target_type, NULL_TREE))
4527 return error_mark_node;
4529 if (TREE_CODE (target_type) == VOID_TYPE)
4530 permerror (input_location, "ISO C++ forbids using pointer of type %<void *%> in subtraction");
4531 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4532 permerror (input_location, "ISO C++ forbids using pointer to a function in subtraction");
4533 if (TREE_CODE (target_type) == METHOD_TYPE)
4534 permerror (input_location, "ISO C++ forbids using pointer to a method in subtraction");
4536 /* First do the subtraction as integers;
4537 then drop through to build the divide operator. */
4539 op0 = cp_build_binary_op (input_location,
4541 cp_convert (restype, op0),
4542 cp_convert (restype, op1),
4543 tf_warning_or_error);
4545 /* This generates an error if op1 is a pointer to an incomplete type. */
4546 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4547 error ("invalid use of a pointer to an incomplete type in pointer arithmetic");
4549 op1 = (TYPE_PTROB_P (ptrtype)
4550 ? size_in_bytes (target_type)
4551 : integer_one_node);
4553 /* Do the division. */
4555 result = build2 (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4556 return fold_if_not_in_template (result);
4559 /* Construct and perhaps optimize a tree representation
4560 for a unary operation. CODE, a tree_code, specifies the operation
4561 and XARG is the operand. */
4564 build_x_unary_op (enum tree_code code, tree xarg, tsubst_flags_t complain)
4566 tree orig_expr = xarg;
4570 if (processing_template_decl)
4572 if (type_dependent_expression_p (xarg))
4573 return build_min_nt (code, xarg, NULL_TREE);
4575 xarg = build_non_dependent_expr (xarg);
4580 /* [expr.unary.op] says:
4582 The address of an object of incomplete type can be taken.
4584 (And is just the ordinary address operator, not an overloaded
4585 "operator &".) However, if the type is a template
4586 specialization, we must complete the type at this point so that
4587 an overloaded "operator &" will be available if required. */
4588 if (code == ADDR_EXPR
4589 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4590 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
4591 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
4592 || (TREE_CODE (xarg) == OFFSET_REF)))
4593 /* Don't look for a function. */;
4595 exp = build_new_op (code, LOOKUP_NORMAL, xarg, NULL_TREE, NULL_TREE,
4596 /*overload=*/NULL, complain);
4597 if (!exp && code == ADDR_EXPR)
4599 if (is_overloaded_fn (xarg))
4601 tree fn = get_first_fn (xarg);
4602 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
4604 error (DECL_CONSTRUCTOR_P (fn)
4605 ? G_("taking address of constructor %qE")
4606 : G_("taking address of destructor %qE"),
4608 return error_mark_node;
4612 /* A pointer to member-function can be formed only by saying
4614 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
4615 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
4617 if (TREE_CODE (xarg) != OFFSET_REF
4618 || !TYPE_P (TREE_OPERAND (xarg, 0)))
4620 error ("invalid use of %qE to form a pointer-to-member-function",
4622 if (TREE_CODE (xarg) != OFFSET_REF)
4623 inform (input_location, " a qualified-id is required");
4624 return error_mark_node;
4628 error ("parentheses around %qE cannot be used to form a"
4629 " pointer-to-member-function",
4631 PTRMEM_OK_P (xarg) = 1;
4635 if (TREE_CODE (xarg) == OFFSET_REF)
4637 ptrmem = PTRMEM_OK_P (xarg);
4639 if (!ptrmem && !flag_ms_extensions
4640 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4642 /* A single non-static member, make sure we don't allow a
4643 pointer-to-member. */
4644 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
4645 TREE_OPERAND (xarg, 0),
4646 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4647 PTRMEM_OK_P (xarg) = ptrmem;
4651 exp = cp_build_addr_expr_strict (xarg, complain);
4654 if (processing_template_decl && exp != error_mark_node)
4655 exp = build_min_non_dep (code, exp, orig_expr,
4656 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
4657 if (TREE_CODE (exp) == ADDR_EXPR)
4658 PTRMEM_OK_P (exp) = ptrmem;
4662 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
4663 constants, where a null value is represented by an INTEGER_CST of
4667 cp_truthvalue_conversion (tree expr)
4669 tree type = TREE_TYPE (expr);
4670 if (TYPE_PTRMEM_P (type))
4671 return build_binary_op (EXPR_LOCATION (expr),
4672 NE_EXPR, expr, nullptr_node, 1);
4673 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
4675 /* With -Wzero-as-null-pointer-constant do not warn for an
4676 'if (p)' or a 'while (!p)', where p is a pointer. */
4678 ++c_inhibit_evaluation_warnings;
4679 ret = c_common_truthvalue_conversion (input_location, expr);
4680 --c_inhibit_evaluation_warnings;
4684 return c_common_truthvalue_conversion (input_location, expr);
4687 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
4690 condition_conversion (tree expr)
4693 if (processing_template_decl)
4695 t = perform_implicit_conversion_flags (boolean_type_node, expr,
4696 tf_warning_or_error, LOOKUP_NORMAL);
4697 t = fold_build_cleanup_point_expr (boolean_type_node, t);
4701 /* Returns the address of T. This function will fold away
4702 ADDR_EXPR of INDIRECT_REF. */
4705 build_address (tree t)
4707 if (error_operand_p (t) || !cxx_mark_addressable (t))
4708 return error_mark_node;
4709 t = build_fold_addr_expr (t);
4710 if (TREE_CODE (t) != ADDR_EXPR)
4715 /* Returns the address of T with type TYPE. */
4718 build_typed_address (tree t, tree type)
4720 if (error_operand_p (t) || !cxx_mark_addressable (t))
4721 return error_mark_node;
4722 t = build_fold_addr_expr_with_type (t, type);
4723 if (TREE_CODE (t) != ADDR_EXPR)
4728 /* Return a NOP_EXPR converting EXPR to TYPE. */
4731 build_nop (tree type, tree expr)
4733 if (type == error_mark_node || error_operand_p (expr))
4735 return build1 (NOP_EXPR, type, expr);
4738 /* Take the address of ARG, whatever that means under C++ semantics.
4739 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
4740 and class rvalues as well.
4742 Nothing should call this function directly; instead, callers should use
4743 cp_build_addr_expr or cp_build_addr_expr_strict. */
4746 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
4751 if (!arg || error_operand_p (arg))
4752 return error_mark_node;
4754 arg = mark_lvalue_use (arg);
4755 argtype = lvalue_type (arg);
4757 gcc_assert (TREE_CODE (arg) != IDENTIFIER_NODE
4758 || !IDENTIFIER_OPNAME_P (arg));
4760 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4761 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
4763 /* They're trying to take the address of a unique non-static
4764 member function. This is ill-formed (except in MS-land),
4765 but let's try to DTRT.
4766 Note: We only handle unique functions here because we don't
4767 want to complain if there's a static overload; non-unique
4768 cases will be handled by instantiate_type. But we need to
4769 handle this case here to allow casts on the resulting PMF.
4770 We could defer this in non-MS mode, but it's easier to give
4771 a useful error here. */
4773 /* Inside constant member functions, the `this' pointer
4774 contains an extra const qualifier. TYPE_MAIN_VARIANT
4775 is used here to remove this const from the diagnostics
4776 and the created OFFSET_REF. */
4777 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
4778 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
4781 if (! flag_ms_extensions)
4783 tree name = DECL_NAME (fn);
4784 if (!(complain & tf_error))
4785 return error_mark_node;
4786 else if (current_class_type
4787 && TREE_OPERAND (arg, 0) == current_class_ref)
4788 /* An expression like &memfn. */
4789 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
4790 " or parenthesized non-static member function to form"
4791 " a pointer to member function. Say %<&%T::%D%>",
4794 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
4795 " function to form a pointer to member function."
4799 arg = build_offset_ref (base, fn, /*address_p=*/true);
4802 /* Uninstantiated types are all functions. Taking the
4803 address of a function is a no-op, so just return the
4805 if (type_unknown_p (arg))
4806 return build1 (ADDR_EXPR, unknown_type_node, arg);
4808 if (TREE_CODE (arg) == OFFSET_REF)
4809 /* We want a pointer to member; bypass all the code for actually taking
4810 the address of something. */
4813 /* Anything not already handled and not a true memory reference
4815 if (TREE_CODE (argtype) != FUNCTION_TYPE
4816 && TREE_CODE (argtype) != METHOD_TYPE)
4818 cp_lvalue_kind kind = lvalue_kind (arg);
4819 if (kind == clk_none)
4821 if (complain & tf_error)
4822 lvalue_error (input_location, lv_addressof);
4823 return error_mark_node;
4825 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
4827 if (!(complain & tf_error))
4828 return error_mark_node;
4829 if (kind & clk_class)
4830 /* Make this a permerror because we used to accept it. */
4831 permerror (input_location, "taking address of temporary");
4833 error ("taking address of xvalue (rvalue reference)");
4837 if (TREE_CODE (argtype) == REFERENCE_TYPE)
4839 tree type = build_pointer_type (TREE_TYPE (argtype));
4840 arg = build1 (CONVERT_EXPR, type, arg);
4843 else if (pedantic && DECL_MAIN_P (arg))
4846 /* Apparently a lot of autoconf scripts for C++ packages do this,
4847 so only complain if -pedantic. */
4848 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
4849 pedwarn (input_location, OPT_pedantic,
4850 "ISO C++ forbids taking address of function %<::main%>");
4851 else if (flag_pedantic_errors)
4852 return error_mark_node;
4855 /* Let &* cancel out to simplify resulting code. */
4856 if (TREE_CODE (arg) == INDIRECT_REF)
4858 /* We don't need to have `current_class_ptr' wrapped in a
4859 NON_LVALUE_EXPR node. */
4860 if (arg == current_class_ref)
4861 return current_class_ptr;
4863 arg = TREE_OPERAND (arg, 0);
4864 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4866 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
4867 arg = build1 (CONVERT_EXPR, type, arg);
4870 /* Don't let this be an lvalue. */
4875 /* ??? Cope with user tricks that amount to offsetof. */
4876 if (TREE_CODE (argtype) != FUNCTION_TYPE
4877 && TREE_CODE (argtype) != METHOD_TYPE
4878 && argtype != unknown_type_node
4879 && (val = get_base_address (arg))
4880 && COMPLETE_TYPE_P (TREE_TYPE (val))
4881 && TREE_CODE (val) == INDIRECT_REF
4882 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4884 tree type = build_pointer_type (argtype);
4885 return fold_convert (type, fold_offsetof_1 (arg));
4888 /* Handle complex lvalues (when permitted)
4889 by reduction to simpler cases. */
4890 val = unary_complex_lvalue (ADDR_EXPR, arg);
4894 switch (TREE_CODE (arg))
4898 case FIX_TRUNC_EXPR:
4899 /* Even if we're not being pedantic, we cannot allow this
4900 extension when we're instantiating in a SFINAE
4902 if (! lvalue_p (arg) && complain == tf_none)
4904 if (complain & tf_error)
4905 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
4907 return error_mark_node;
4912 arg = BASELINK_FUNCTIONS (arg);
4916 arg = OVL_CURRENT (arg);
4921 /* Turn a reference to a non-static data member into a
4922 pointer-to-member. */
4927 gcc_assert (PTRMEM_OK_P (arg));
4929 t = TREE_OPERAND (arg, 1);
4930 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
4932 if (complain & tf_error)
4933 error ("cannot create pointer to reference member %qD", t);
4934 return error_mark_node;
4937 type = build_ptrmem_type (context_for_name_lookup (t),
4939 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4947 if (argtype != error_mark_node)
4948 argtype = build_pointer_type (argtype);
4950 /* In a template, we are processing a non-dependent expression
4951 so we can just form an ADDR_EXPR with the correct type. */
4952 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
4954 val = build_address (arg);
4955 if (TREE_CODE (arg) == OFFSET_REF)
4956 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
4958 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
4960 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
4962 /* We can only get here with a single static member
4964 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
4965 && DECL_STATIC_FUNCTION_P (fn));
4967 val = build_address (fn);
4968 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
4969 /* Do not lose object's side effects. */
4970 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
4971 TREE_OPERAND (arg, 0), val);
4973 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4975 if (complain & tf_error)
4976 error ("attempt to take address of bit-field structure member %qD",
4977 TREE_OPERAND (arg, 1));
4978 return error_mark_node;
4982 tree object = TREE_OPERAND (arg, 0);
4983 tree field = TREE_OPERAND (arg, 1);
4984 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4985 (TREE_TYPE (object), decl_type_context (field)));
4986 val = build_address (arg);
4989 if (TREE_CODE (argtype) == POINTER_TYPE
4990 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4992 build_ptrmemfunc_type (argtype);
4993 val = build_ptrmemfunc (argtype, val, 0,
4995 tf_warning_or_error);
5001 /* Take the address of ARG if it has one, even if it's an rvalue. */
5004 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5006 return cp_build_addr_expr_1 (arg, 0, complain);
5009 /* Take the address of ARG, but only if it's an lvalue. */
5012 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5014 return cp_build_addr_expr_1 (arg, 1, complain);
5017 /* C++: Must handle pointers to members.
5019 Perhaps type instantiation should be extended to handle conversion
5020 from aggregates to types we don't yet know we want? (Or are those
5021 cases typically errors which should be reported?)
5023 NOCONVERT nonzero suppresses the default promotions
5024 (such as from short to int). */
5027 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5028 tsubst_flags_t complain)
5030 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5033 const char *errstring = NULL;
5035 const char *invalid_op_diag;
5037 if (!arg || error_operand_p (arg))
5038 return error_mark_node;
5040 if ((invalid_op_diag
5041 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5046 error (invalid_op_diag);
5047 return error_mark_node;
5052 case UNARY_PLUS_EXPR:
5055 int flags = WANT_ARITH | WANT_ENUM;
5056 /* Unary plus (but not unary minus) is allowed on pointers. */
5057 if (code == UNARY_PLUS_EXPR)
5058 flags |= WANT_POINTER;
5059 arg = build_expr_type_conversion (flags, arg, true);
5061 errstring = (code == NEGATE_EXPR
5062 ? _("wrong type argument to unary minus")
5063 : _("wrong type argument to unary plus"));
5066 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5067 arg = perform_integral_promotions (arg);
5069 /* Make sure the result is not an lvalue: a unary plus or minus
5070 expression is always a rvalue. */
5077 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5081 arg = default_conversion (arg);
5083 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5084 | WANT_VECTOR_OR_COMPLEX,
5086 errstring = _("wrong type argument to bit-complement");
5087 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5088 arg = perform_integral_promotions (arg);
5092 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5093 errstring = _("wrong type argument to abs");
5094 else if (!noconvert)
5095 arg = default_conversion (arg);
5099 /* Conjugating a real value is a no-op, but allow it anyway. */
5100 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5101 errstring = _("wrong type argument to conjugation");
5102 else if (!noconvert)
5103 arg = default_conversion (arg);
5106 case TRUTH_NOT_EXPR:
5107 arg = perform_implicit_conversion (boolean_type_node, arg,
5109 val = invert_truthvalue_loc (input_location, arg);
5110 if (arg != error_mark_node)
5112 errstring = _("in argument to unary !");
5120 arg = build_real_imag_expr (input_location, code, arg);
5121 if (arg == error_mark_node)
5124 return fold_if_not_in_template (arg);
5126 case PREINCREMENT_EXPR:
5127 case POSTINCREMENT_EXPR:
5128 case PREDECREMENT_EXPR:
5129 case POSTDECREMENT_EXPR:
5130 /* Handle complex lvalues (when permitted)
5131 by reduction to simpler cases. */
5133 val = unary_complex_lvalue (code, arg);
5137 arg = mark_lvalue_use (arg);
5139 /* Increment or decrement the real part of the value,
5140 and don't change the imaginary part. */
5141 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5145 arg = stabilize_reference (arg);
5146 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5147 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5148 real = cp_build_unary_op (code, real, 1, complain);
5149 if (real == error_mark_node || imag == error_mark_node)
5150 return error_mark_node;
5151 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5155 /* Report invalid types. */
5157 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5160 if (code == PREINCREMENT_EXPR)
5161 errstring = _("no pre-increment operator for type");
5162 else if (code == POSTINCREMENT_EXPR)
5163 errstring = _("no post-increment operator for type");
5164 else if (code == PREDECREMENT_EXPR)
5165 errstring = _("no pre-decrement operator for type");
5167 errstring = _("no post-decrement operator for type");
5170 else if (arg == error_mark_node)
5171 return error_mark_node;
5173 /* Report something read-only. */
5175 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5176 || TREE_READONLY (arg))
5178 if (complain & tf_error)
5179 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5180 || code == POSTINCREMENT_EXPR)
5181 ? lv_increment : lv_decrement));
5183 return error_mark_node;
5188 tree declared_type = unlowered_expr_type (arg);
5190 argtype = TREE_TYPE (arg);
5192 /* ARM $5.2.5 last annotation says this should be forbidden. */
5193 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5195 if (complain & tf_error)
5196 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5197 ? G_("ISO C++ forbids incrementing an enum")
5198 : G_("ISO C++ forbids decrementing an enum"));
5200 return error_mark_node;
5203 /* Compute the increment. */
5205 if (TREE_CODE (argtype) == POINTER_TYPE)
5207 tree type = complete_type (TREE_TYPE (argtype));
5209 if (!COMPLETE_OR_VOID_TYPE_P (type))
5211 if (complain & tf_error)
5212 error (((code == PREINCREMENT_EXPR
5213 || code == POSTINCREMENT_EXPR))
5214 ? G_("cannot increment a pointer to incomplete type %qT")
5215 : G_("cannot decrement a pointer to incomplete type %qT"),
5216 TREE_TYPE (argtype));
5218 return error_mark_node;
5220 else if ((pedantic || warn_pointer_arith)
5221 && !TYPE_PTROB_P (argtype))
5223 if (complain & tf_error)
5224 permerror (input_location, (code == PREINCREMENT_EXPR
5225 || code == POSTINCREMENT_EXPR)
5226 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5227 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5230 return error_mark_node;
5233 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5236 inc = integer_one_node;
5238 inc = cp_convert (argtype, inc);
5240 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5241 need to ask Objective-C to build the increment or decrement
5242 expression for it. */
5243 if (objc_is_property_ref (arg))
5244 return objc_build_incr_expr_for_property_ref (input_location, code,
5247 /* Complain about anything else that is not a true lvalue. */
5248 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5249 || code == POSTINCREMENT_EXPR)
5250 ? lv_increment : lv_decrement),
5252 return error_mark_node;
5254 /* Forbid using -- on `bool'. */
5255 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5257 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5259 if (complain & tf_error)
5260 error ("invalid use of Boolean expression as operand "
5261 "to %<operator--%>");
5262 return error_mark_node;
5264 val = boolean_increment (code, arg);
5266 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5267 /* An rvalue has no cv-qualifiers. */
5268 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5270 val = build2 (code, TREE_TYPE (arg), arg, inc);
5272 TREE_SIDE_EFFECTS (val) = 1;
5277 /* Note that this operation never does default_conversion
5278 regardless of NOCONVERT. */
5279 return cp_build_addr_expr (arg, complain);
5288 argtype = TREE_TYPE (arg);
5289 return fold_if_not_in_template (build1 (code, argtype, arg));
5292 if (complain & tf_error)
5293 error ("%s", errstring);
5294 return error_mark_node;
5297 /* Hook for the c-common bits that build a unary op. */
5299 build_unary_op (location_t location ATTRIBUTE_UNUSED,
5300 enum tree_code code, tree xarg, int noconvert)
5302 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5305 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5306 for certain kinds of expressions which are not really lvalues
5307 but which we can accept as lvalues.
5309 If ARG is not a kind of expression we can handle, return
5313 unary_complex_lvalue (enum tree_code code, tree arg)
5315 /* Inside a template, making these kinds of adjustments is
5316 pointless; we are only concerned with the type of the
5318 if (processing_template_decl)
5321 /* Handle (a, b) used as an "lvalue". */
5322 if (TREE_CODE (arg) == COMPOUND_EXPR)
5324 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5325 tf_warning_or_error);
5326 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5327 TREE_OPERAND (arg, 0), real_result);
5330 /* Handle (a ? b : c) used as an "lvalue". */
5331 if (TREE_CODE (arg) == COND_EXPR
5332 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5333 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5335 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5336 if (TREE_CODE (arg) == MODIFY_EXPR
5337 || TREE_CODE (arg) == PREINCREMENT_EXPR
5338 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5340 tree lvalue = TREE_OPERAND (arg, 0);
5341 if (TREE_SIDE_EFFECTS (lvalue))
5343 lvalue = stabilize_reference (lvalue);
5344 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5345 lvalue, TREE_OPERAND (arg, 1));
5347 return unary_complex_lvalue
5348 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5351 if (code != ADDR_EXPR)
5354 /* Handle (a = b) used as an "lvalue" for `&'. */
5355 if (TREE_CODE (arg) == MODIFY_EXPR
5356 || TREE_CODE (arg) == INIT_EXPR)
5358 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5359 tf_warning_or_error);
5360 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5362 TREE_NO_WARNING (arg) = 1;
5366 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5367 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5368 || TREE_CODE (arg) == OFFSET_REF)
5371 /* We permit compiler to make function calls returning
5372 objects of aggregate type look like lvalues. */
5376 if (TREE_CODE (targ) == SAVE_EXPR)
5377 targ = TREE_OPERAND (targ, 0);
5379 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5381 if (TREE_CODE (arg) == SAVE_EXPR)
5384 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5385 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5388 if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
5389 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5390 TREE_OPERAND (targ, 0), current_function_decl, NULL);
5393 /* Don't let anything else be handled specially. */
5397 /* Mark EXP saying that we need to be able to take the
5398 address of it; it should not be allocated in a register.
5399 Value is true if successful.
5401 C++: we do not allow `current_class_ptr' to be addressable. */
5404 cxx_mark_addressable (tree exp)
5409 switch (TREE_CODE (x))
5416 x = TREE_OPERAND (x, 0);
5420 if (x == current_class_ptr)
5422 error ("cannot take the address of %<this%>, which is an rvalue expression");
5423 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
5429 /* Caller should not be trying to mark initialized
5430 constant fields addressable. */
5431 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
5432 || DECL_IN_AGGR_P (x) == 0
5434 || DECL_EXTERNAL (x));
5438 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5439 && !DECL_ARTIFICIAL (x))
5441 if (TREE_CODE (x) == VAR_DECL && DECL_HARD_REGISTER (x))
5444 ("address of explicit register variable %qD requested", x);
5447 else if (extra_warnings)
5449 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
5451 TREE_ADDRESSABLE (x) = 1;
5456 TREE_ADDRESSABLE (x) = 1;
5460 TREE_ADDRESSABLE (x) = 1;
5464 TREE_ADDRESSABLE (x) = 1;
5465 cxx_mark_addressable (TREE_OPERAND (x, 0));
5473 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5476 build_x_conditional_expr (tree ifexp, tree op1, tree op2,
5477 tsubst_flags_t complain)
5479 tree orig_ifexp = ifexp;
5480 tree orig_op1 = op1;
5481 tree orig_op2 = op2;
5484 if (processing_template_decl)
5486 /* The standard says that the expression is type-dependent if
5487 IFEXP is type-dependent, even though the eventual type of the
5488 expression doesn't dependent on IFEXP. */
5489 if (type_dependent_expression_p (ifexp)
5490 /* As a GNU extension, the middle operand may be omitted. */
5491 || (op1 && type_dependent_expression_p (op1))
5492 || type_dependent_expression_p (op2))
5493 return build_min_nt (COND_EXPR, ifexp, op1, op2);
5494 ifexp = build_non_dependent_expr (ifexp);
5496 op1 = build_non_dependent_expr (op1);
5497 op2 = build_non_dependent_expr (op2);
5500 expr = build_conditional_expr (ifexp, op1, op2, complain);
5501 if (processing_template_decl && expr != error_mark_node)
5503 tree min = build_min_non_dep (COND_EXPR, expr,
5504 orig_ifexp, orig_op1, orig_op2);
5505 /* Remember that the result is an lvalue or xvalue. */
5506 if (lvalue_or_rvalue_with_address_p (expr)
5507 && !lvalue_or_rvalue_with_address_p (min))
5508 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
5509 !real_lvalue_p (expr));
5510 expr = convert_from_reference (min);
5515 /* Given a list of expressions, return a compound expression
5516 that performs them all and returns the value of the last of them. */
5519 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
5520 tsubst_flags_t complain)
5522 tree expr = TREE_VALUE (list);
5524 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
5525 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
5527 if (complain & tf_error)
5528 pedwarn (EXPR_LOC_OR_HERE (expr), 0, "list-initializer for "
5529 "non-class type must not be parenthesized");
5531 return error_mark_node;
5534 if (TREE_CHAIN (list))
5536 if (complain & tf_error)
5540 permerror (input_location, "expression list treated as compound "
5541 "expression in initializer");
5544 permerror (input_location, "expression list treated as compound "
5545 "expression in mem-initializer");
5548 permerror (input_location, "expression list treated as compound "
5549 "expression in functional cast");
5555 return error_mark_node;
5557 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
5558 expr = build_x_compound_expr (expr, TREE_VALUE (list),
5565 /* Like build_x_compound_expr_from_list, but using a VEC. */
5568 build_x_compound_expr_from_vec (VEC(tree,gc) *vec, const char *msg)
5570 if (VEC_empty (tree, vec))
5572 else if (VEC_length (tree, vec) == 1)
5573 return VEC_index (tree, vec, 0);
5581 permerror (input_location,
5582 "%s expression list treated as compound expression",
5585 expr = VEC_index (tree, vec, 0);
5586 for (ix = 1; VEC_iterate (tree, vec, ix, t); ++ix)
5587 expr = build_x_compound_expr (expr, t, tf_warning_or_error);
5593 /* Handle overloading of the ',' operator when needed. */
5596 build_x_compound_expr (tree op1, tree op2, tsubst_flags_t complain)
5599 tree orig_op1 = op1;
5600 tree orig_op2 = op2;
5602 if (processing_template_decl)
5604 if (type_dependent_expression_p (op1)
5605 || type_dependent_expression_p (op2))
5606 return build_min_nt (COMPOUND_EXPR, op1, op2);
5607 op1 = build_non_dependent_expr (op1);
5608 op2 = build_non_dependent_expr (op2);
5611 result = build_new_op (COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2, NULL_TREE,
5612 /*overload=*/NULL, complain);
5614 result = cp_build_compound_expr (op1, op2, complain);
5616 if (processing_template_decl && result != error_mark_node)
5617 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
5622 /* Like cp_build_compound_expr, but for the c-common bits. */
5625 build_compound_expr (location_t loc ATTRIBUTE_UNUSED, tree lhs, tree rhs)
5627 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
5630 /* Build a compound expression. */
5633 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
5635 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
5637 if (lhs == error_mark_node || rhs == error_mark_node)
5638 return error_mark_node;
5640 if (TREE_CODE (rhs) == TARGET_EXPR)
5642 /* If the rhs is a TARGET_EXPR, then build the compound
5643 expression inside the target_expr's initializer. This
5644 helps the compiler to eliminate unnecessary temporaries. */
5645 tree init = TREE_OPERAND (rhs, 1);
5647 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
5648 TREE_OPERAND (rhs, 1) = init;
5653 if (type_unknown_p (rhs))
5655 error ("no context to resolve type of %qE", rhs);
5656 return error_mark_node;
5659 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
5662 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
5663 casts away constness. CAST gives the type of cast. Returns true
5664 if the cast is ill-formed, false if it is well-formed.
5666 ??? This function warns for casting away any qualifier not just
5667 const. We would like to specify exactly what qualifiers are casted
5672 check_for_casting_away_constness (tree src_type, tree dest_type,
5673 enum tree_code cast, tsubst_flags_t complain)
5675 /* C-style casts are allowed to cast away constness. With
5676 WARN_CAST_QUAL, we still want to issue a warning. */
5677 if (cast == CAST_EXPR && !warn_cast_qual)
5680 if (!casts_away_constness (src_type, dest_type))
5686 if (complain & tf_warning)
5687 warning (OPT_Wcast_qual,
5688 "cast from type %qT to type %qT casts away qualifiers",
5689 src_type, dest_type);
5692 case STATIC_CAST_EXPR:
5693 if (complain & tf_error)
5694 error ("static_cast from type %qT to type %qT casts away qualifiers",
5695 src_type, dest_type);
5698 case REINTERPRET_CAST_EXPR:
5699 if (complain & tf_error)
5700 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
5701 src_type, dest_type);
5709 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
5710 (another pointer-to-member type in the same hierarchy) and return
5711 the converted expression. If ALLOW_INVERSE_P is permitted, a
5712 pointer-to-derived may be converted to pointer-to-base; otherwise,
5713 only the other direction is permitted. If C_CAST_P is true, this
5714 conversion is taking place as part of a C-style cast. */
5717 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
5718 bool c_cast_p, tsubst_flags_t complain)
5720 if (TYPE_PTRMEM_P (type))
5724 if (TREE_CODE (expr) == PTRMEM_CST)
5725 expr = cplus_expand_constant (expr);
5726 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
5727 TYPE_PTRMEM_CLASS_TYPE (type),
5729 c_cast_p, complain);
5730 if (delta == error_mark_node)
5731 return error_mark_node;
5733 if (!integer_zerop (delta))
5735 tree cond, op1, op2;
5737 cond = cp_build_binary_op (input_location,
5740 build_int_cst (TREE_TYPE (expr), -1),
5741 tf_warning_or_error);
5742 op1 = build_nop (ptrdiff_type_node, expr);
5743 op2 = cp_build_binary_op (input_location,
5744 PLUS_EXPR, op1, delta,
5745 tf_warning_or_error);
5747 expr = fold_build3_loc (input_location,
5748 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
5752 return build_nop (type, expr);
5755 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
5756 allow_inverse_p, c_cast_p, complain);
5759 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
5760 this static_cast is being attempted as one of the possible casts
5761 allowed by a C-style cast. (In that case, accessibility of base
5762 classes is not considered, and it is OK to cast away
5763 constness.) Return the result of the cast. *VALID_P is set to
5764 indicate whether or not the cast was valid. */
5767 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
5768 bool *valid_p, tsubst_flags_t complain)
5773 /* Assume the cast is valid. */
5776 intype = TREE_TYPE (expr);
5778 /* Save casted types in the function's used types hash table. */
5779 used_types_insert (type);
5781 /* [expr.static.cast]
5783 An lvalue of type "cv1 B", where B is a class type, can be cast
5784 to type "reference to cv2 D", where D is a class derived (clause
5785 _class.derived_) from B, if a valid standard conversion from
5786 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
5787 same cv-qualification as, or greater cv-qualification than, cv1,
5788 and B is not a virtual base class of D. */
5789 /* We check this case before checking the validity of "TYPE t =
5790 EXPR;" below because for this case:
5793 struct D : public B { D(const B&); };
5795 void f() { static_cast<const D&>(b); }
5797 we want to avoid constructing a new D. The standard is not
5798 completely clear about this issue, but our interpretation is
5799 consistent with other compilers. */
5800 if (TREE_CODE (type) == REFERENCE_TYPE
5801 && CLASS_TYPE_P (TREE_TYPE (type))
5802 && CLASS_TYPE_P (intype)
5803 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
5804 && DERIVED_FROM_P (intype, TREE_TYPE (type))
5805 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
5806 build_pointer_type (TYPE_MAIN_VARIANT
5807 (TREE_TYPE (type))))
5809 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5813 /* There is a standard conversion from "D*" to "B*" even if "B"
5814 is ambiguous or inaccessible. If this is really a
5815 static_cast, then we check both for inaccessibility and
5816 ambiguity. However, if this is a static_cast being performed
5817 because the user wrote a C-style cast, then accessibility is
5819 base = lookup_base (TREE_TYPE (type), intype,
5820 c_cast_p ? ba_unique : ba_check,
5823 /* Convert from "B*" to "D*". This function will check that "B"
5824 is not a virtual base of "D". */
5825 expr = build_base_path (MINUS_EXPR, build_address (expr),
5826 base, /*nonnull=*/false, complain);
5827 /* Convert the pointer to a reference -- but then remember that
5828 there are no expressions with reference type in C++.
5830 We call rvalue so that there's an actual tree code
5831 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
5832 is a variable with the same type, the conversion would get folded
5833 away, leaving just the variable and causing lvalue_kind to give
5834 the wrong answer. */
5835 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
5838 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
5839 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
5840 if (TREE_CODE (type) == REFERENCE_TYPE
5841 && TYPE_REF_IS_RVALUE (type)
5842 && lvalue_or_rvalue_with_address_p (expr)
5843 && reference_related_p (TREE_TYPE (type), intype)
5844 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
5846 expr = build_typed_address (expr, type);
5847 return convert_from_reference (expr);
5850 /* Resolve overloaded address here rather than once in
5851 implicit_conversion and again in the inverse code below. */
5852 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
5854 expr = instantiate_type (type, expr, complain);
5855 intype = TREE_TYPE (expr);
5858 /* [expr.static.cast]
5860 An expression e can be explicitly converted to a type T using a
5861 static_cast of the form static_cast<T>(e) if the declaration T
5862 t(e);" is well-formed, for some invented temporary variable
5864 result = perform_direct_initialization_if_possible (type, expr,
5865 c_cast_p, complain);
5868 result = convert_from_reference (result);
5870 /* [expr.static.cast]
5872 If T is a reference type, the result is an lvalue; otherwise,
5873 the result is an rvalue. */
5874 if (TREE_CODE (type) != REFERENCE_TYPE)
5875 result = rvalue (result);
5879 /* [expr.static.cast]
5881 Any expression can be explicitly converted to type cv void. */
5882 if (TREE_CODE (type) == VOID_TYPE)
5883 return convert_to_void (expr, ICV_CAST, complain);
5885 /* [expr.static.cast]
5887 The inverse of any standard conversion sequence (clause _conv_),
5888 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
5889 (_conv.array_), function-to-pointer (_conv.func_), and boolean
5890 (_conv.bool_) conversions, can be performed explicitly using
5891 static_cast subject to the restriction that the explicit
5892 conversion does not cast away constness (_expr.const.cast_), and
5893 the following additional rules for specific cases: */
5894 /* For reference, the conversions not excluded are: integral
5895 promotions, floating point promotion, integral conversions,
5896 floating point conversions, floating-integral conversions,
5897 pointer conversions, and pointer to member conversions. */
5900 A value of integral _or enumeration_ type can be explicitly
5901 converted to an enumeration type. */
5902 /* The effect of all that is that any conversion between any two
5903 types which are integral, floating, or enumeration types can be
5905 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5906 || SCALAR_FLOAT_TYPE_P (type))
5907 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
5908 || SCALAR_FLOAT_TYPE_P (intype)))
5909 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
5911 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
5912 && CLASS_TYPE_P (TREE_TYPE (type))
5913 && CLASS_TYPE_P (TREE_TYPE (intype))
5914 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
5915 (TREE_TYPE (intype))),
5916 build_pointer_type (TYPE_MAIN_VARIANT
5917 (TREE_TYPE (type)))))
5922 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
5924 return error_mark_node;
5925 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
5926 c_cast_p ? ba_unique : ba_check,
5928 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
5930 return cp_fold_convert(type, expr);
5933 if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5934 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5941 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
5942 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
5944 if (TYPE_PTRMEM_P (type))
5946 t1 = (build_ptrmem_type
5948 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
5949 t2 = (build_ptrmem_type
5951 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
5958 if (can_convert (t1, t2) || can_convert (t2, t1))
5961 && check_for_casting_away_constness (intype, type,
5964 return error_mark_node;
5965 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
5966 c_cast_p, complain);
5970 /* [expr.static.cast]
5972 An rvalue of type "pointer to cv void" can be explicitly
5973 converted to a pointer to object type. A value of type pointer
5974 to object converted to "pointer to cv void" and back to the
5975 original pointer type will have its original value. */
5976 if (TREE_CODE (intype) == POINTER_TYPE
5977 && VOID_TYPE_P (TREE_TYPE (intype))
5978 && TYPE_PTROB_P (type))
5981 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
5983 return error_mark_node;
5984 return build_nop (type, expr);
5988 return error_mark_node;
5991 /* Return an expression representing static_cast<TYPE>(EXPR). */
5994 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
5999 if (type == error_mark_node || expr == error_mark_node)
6000 return error_mark_node;
6002 if (processing_template_decl)
6004 expr = build_min (STATIC_CAST_EXPR, type, expr);
6005 /* We don't know if it will or will not have side effects. */
6006 TREE_SIDE_EFFECTS (expr) = 1;
6007 return convert_from_reference (expr);
6010 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6011 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6012 if (TREE_CODE (type) != REFERENCE_TYPE
6013 && TREE_CODE (expr) == NOP_EXPR
6014 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6015 expr = TREE_OPERAND (expr, 0);
6017 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6022 if (complain & tf_error)
6023 error ("invalid static_cast from type %qT to type %qT",
6024 TREE_TYPE (expr), type);
6025 return error_mark_node;
6028 /* EXPR is an expression with member function or pointer-to-member
6029 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6030 not permitted by ISO C++, but we accept it in some modes. If we
6031 are not in one of those modes, issue a diagnostic. Return the
6032 converted expression. */
6035 convert_member_func_to_ptr (tree type, tree expr)
6040 intype = TREE_TYPE (expr);
6041 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6042 || TREE_CODE (intype) == METHOD_TYPE);
6044 if (pedantic || warn_pmf2ptr)
6045 pedwarn (input_location, pedantic ? OPT_pedantic : OPT_Wpmf_conversions,
6046 "converting from %qT to %qT", intype, type);
6048 if (TREE_CODE (intype) == METHOD_TYPE)
6049 expr = build_addr_func (expr);
6050 else if (TREE_CODE (expr) == PTRMEM_CST)
6051 expr = build_address (PTRMEM_CST_MEMBER (expr));
6054 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6055 decl = build_address (decl);
6056 expr = get_member_function_from_ptrfunc (&decl, expr);
6059 return build_nop (type, expr);
6062 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6063 If C_CAST_P is true, this reinterpret cast is being done as part of
6064 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6065 indicate whether or not reinterpret_cast was valid. */
6068 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6069 bool *valid_p, tsubst_flags_t complain)
6073 /* Assume the cast is invalid. */
6077 if (type == error_mark_node || error_operand_p (expr))
6078 return error_mark_node;
6080 intype = TREE_TYPE (expr);
6082 /* Save casted types in the function's used types hash table. */
6083 used_types_insert (type);
6085 /* [expr.reinterpret.cast]
6086 An lvalue expression of type T1 can be cast to the type
6087 "reference to T2" if an expression of type "pointer to T1" can be
6088 explicitly converted to the type "pointer to T2" using a
6089 reinterpret_cast. */
6090 if (TREE_CODE (type) == REFERENCE_TYPE)
6092 if (! real_lvalue_p (expr))
6094 if (complain & tf_error)
6095 error ("invalid cast of an rvalue expression of type "
6098 return error_mark_node;
6101 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6102 "B" are related class types; the reinterpret_cast does not
6103 adjust the pointer. */
6104 if (TYPE_PTR_P (intype)
6105 && (complain & tf_warning)
6106 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6107 COMPARE_BASE | COMPARE_DERIVED)))
6108 warning (0, "casting %qT to %qT does not dereference pointer",
6111 expr = cp_build_addr_expr (expr, complain);
6113 if (warn_strict_aliasing > 2)
6114 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6116 if (expr != error_mark_node)
6117 expr = build_reinterpret_cast_1
6118 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6120 if (expr != error_mark_node)
6121 /* cp_build_indirect_ref isn't right for rvalue refs. */
6122 expr = convert_from_reference (fold_convert (type, expr));
6126 /* As a G++ extension, we consider conversions from member
6127 functions, and pointers to member functions to
6128 pointer-to-function and pointer-to-void types. If
6129 -Wno-pmf-conversions has not been specified,
6130 convert_member_func_to_ptr will issue an error message. */
6131 if ((TYPE_PTRMEMFUNC_P (intype)
6132 || TREE_CODE (intype) == METHOD_TYPE)
6133 && TYPE_PTR_P (type)
6134 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6135 || VOID_TYPE_P (TREE_TYPE (type))))
6136 return convert_member_func_to_ptr (type, expr);
6138 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6139 array-to-pointer, and function-to-pointer conversions are
6141 expr = decay_conversion (expr);
6143 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6144 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6145 if (TREE_CODE (expr) == NOP_EXPR
6146 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6147 expr = TREE_OPERAND (expr, 0);
6149 if (error_operand_p (expr))
6150 return error_mark_node;
6152 intype = TREE_TYPE (expr);
6154 /* [expr.reinterpret.cast]
6155 A pointer can be converted to any integral type large enough to
6156 hold it. ... A value of type std::nullptr_t can be converted to
6157 an integral type; the conversion has the same meaning and
6158 validity as a conversion of (void*)0 to the integral type. */
6159 if (CP_INTEGRAL_TYPE_P (type)
6160 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6162 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6164 if (complain & tf_error)
6165 permerror (input_location, "cast from %qT to %qT loses precision",
6168 return error_mark_node;
6170 if (NULLPTR_TYPE_P (intype))
6171 return build_int_cst (type, 0);
6173 /* [expr.reinterpret.cast]
6174 A value of integral or enumeration type can be explicitly
6175 converted to a pointer. */
6176 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6179 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6180 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6181 return fold_if_not_in_template (build_nop (type, expr));
6182 else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
6183 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6188 && check_for_casting_away_constness (intype, type,
6189 REINTERPRET_CAST_EXPR,
6191 return error_mark_node;
6192 /* Warn about possible alignment problems. */
6193 if (STRICT_ALIGNMENT && warn_cast_align
6194 && (complain & tf_warning)
6195 && !VOID_TYPE_P (type)
6196 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6197 && COMPLETE_TYPE_P (TREE_TYPE (type))
6198 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6199 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6200 warning (OPT_Wcast_align, "cast from %qT to %qT "
6201 "increases required alignment of target type", intype, type);
6203 /* We need to strip nops here, because the front end likes to
6204 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6206 if (warn_strict_aliasing <= 2)
6207 strict_aliasing_warning (intype, type, sexpr);
6209 return fold_if_not_in_template (build_nop (type, expr));
6211 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6212 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6214 if (pedantic && (complain & tf_warning))
6215 /* Only issue a warning, as we have always supported this
6216 where possible, and it is necessary in some cases. DR 195
6217 addresses this issue, but as of 2004/10/26 is still in
6219 warning (0, "ISO C++ forbids casting between pointer-to-function and pointer-to-object");
6220 return fold_if_not_in_template (build_nop (type, expr));
6222 else if (TREE_CODE (type) == VECTOR_TYPE)
6223 return fold_if_not_in_template (convert_to_vector (type, expr));
6224 else if (TREE_CODE (intype) == VECTOR_TYPE
6225 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6226 return fold_if_not_in_template (convert_to_integer (type, expr));
6231 if (complain & tf_error)
6232 error ("invalid cast from type %qT to type %qT", intype, type);
6233 return error_mark_node;
6236 return cp_convert (type, expr);
6240 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6242 if (type == error_mark_node || expr == error_mark_node)
6243 return error_mark_node;
6245 if (processing_template_decl)
6247 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6249 if (!TREE_SIDE_EFFECTS (t)
6250 && type_dependent_expression_p (expr))
6251 /* There might turn out to be side effects inside expr. */
6252 TREE_SIDE_EFFECTS (t) = 1;
6253 return convert_from_reference (t);
6256 return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6257 /*valid_p=*/NULL, complain);
6260 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6261 return an appropriate expression. Otherwise, return
6262 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6263 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6264 performing a C-style cast, its value upon return will indicate
6265 whether or not the conversion succeeded. */
6268 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6272 tree reference_type;
6274 /* Callers are responsible for handling error_mark_node as a
6275 destination type. */
6276 gcc_assert (dst_type != error_mark_node);
6277 /* In a template, callers should be building syntactic
6278 representations of casts, not using this machinery. */
6279 gcc_assert (!processing_template_decl);
6281 /* Assume the conversion is invalid. */
6285 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRMEM_P (dst_type))
6287 if (complain & tf_error)
6288 error ("invalid use of const_cast with type %qT, "
6289 "which is not a pointer, "
6290 "reference, nor a pointer-to-data-member type", dst_type);
6291 return error_mark_node;
6294 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
6296 if (complain & tf_error)
6297 error ("invalid use of const_cast with type %qT, which is a pointer "
6298 "or reference to a function type", dst_type);
6299 return error_mark_node;
6302 /* Save casted types in the function's used types hash table. */
6303 used_types_insert (dst_type);
6305 src_type = TREE_TYPE (expr);
6306 /* Expressions do not really have reference types. */
6307 if (TREE_CODE (src_type) == REFERENCE_TYPE)
6308 src_type = TREE_TYPE (src_type);
6310 /* [expr.const.cast]
6312 For two object types T1 and T2, if a pointer to T1 can be explicitly
6313 converted to the type "pointer to T2" using a const_cast, then the
6314 following conversions can also be made:
6316 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6317 type T2 using the cast const_cast<T2&>;
6319 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6320 type T2 using the cast const_cast<T2&&>; and
6322 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6323 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6325 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
6327 reference_type = dst_type;
6328 if (!TYPE_REF_IS_RVALUE (dst_type)
6329 ? real_lvalue_p (expr)
6330 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
6332 : lvalue_or_rvalue_with_address_p (expr)))
6336 if (complain & tf_error)
6337 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6338 src_type, dst_type);
6339 return error_mark_node;
6341 dst_type = build_pointer_type (TREE_TYPE (dst_type));
6342 src_type = build_pointer_type (src_type);
6346 reference_type = NULL_TREE;
6347 /* If the destination type is not a reference type, the
6348 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6349 conversions are performed. */
6350 src_type = type_decays_to (src_type);
6351 if (src_type == error_mark_node)
6352 return error_mark_node;
6355 if (TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
6357 if (comp_ptr_ttypes_const (dst_type, src_type))
6362 /* This cast is actually a C-style cast. Issue a warning if
6363 the user is making a potentially unsafe cast. */
6364 check_for_casting_away_constness (src_type, dst_type,
6365 CAST_EXPR, complain);
6369 expr = cp_build_addr_expr (expr, complain);
6370 expr = build_nop (reference_type, expr);
6371 return convert_from_reference (expr);
6375 expr = decay_conversion (expr);
6376 /* build_c_cast puts on a NOP_EXPR to make the result not an
6377 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6378 non-lvalue context. */
6379 if (TREE_CODE (expr) == NOP_EXPR
6380 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6381 expr = TREE_OPERAND (expr, 0);
6382 return build_nop (dst_type, expr);
6386 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
6387 TREE_TYPE (src_type)))
6388 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
6392 if (complain & tf_error)
6393 error ("invalid const_cast from type %qT to type %qT",
6394 src_type, dst_type);
6395 return error_mark_node;
6399 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
6401 if (type == error_mark_node || error_operand_p (expr))
6402 return error_mark_node;
6404 if (processing_template_decl)
6406 tree t = build_min (CONST_CAST_EXPR, type, expr);
6408 if (!TREE_SIDE_EFFECTS (t)
6409 && type_dependent_expression_p (expr))
6410 /* There might turn out to be side effects inside expr. */
6411 TREE_SIDE_EFFECTS (t) = 1;
6412 return convert_from_reference (t);
6415 return build_const_cast_1 (type, expr, complain,
6419 /* Like cp_build_c_cast, but for the c-common bits. */
6422 build_c_cast (location_t loc ATTRIBUTE_UNUSED, tree type, tree expr)
6424 return cp_build_c_cast (type, expr, tf_warning_or_error);
6427 /* Build an expression representing an explicit C-style cast to type
6428 TYPE of expression EXPR. */
6431 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
6437 if (type == error_mark_node || error_operand_p (expr))
6438 return error_mark_node;
6440 if (processing_template_decl)
6442 tree t = build_min (CAST_EXPR, type,
6443 tree_cons (NULL_TREE, value, NULL_TREE));
6444 /* We don't know if it will or will not have side effects. */
6445 TREE_SIDE_EFFECTS (t) = 1;
6446 return convert_from_reference (t);
6449 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
6450 'Class') should always be retained, because this information aids
6451 in method lookup. */
6452 if (objc_is_object_ptr (type)
6453 && objc_is_object_ptr (TREE_TYPE (expr)))
6454 return build_nop (type, expr);
6456 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6457 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6458 if (TREE_CODE (type) != REFERENCE_TYPE
6459 && TREE_CODE (value) == NOP_EXPR
6460 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
6461 value = TREE_OPERAND (value, 0);
6463 if (TREE_CODE (type) == ARRAY_TYPE)
6465 /* Allow casting from T1* to T2[] because Cfront allows it.
6466 NIHCL uses it. It is not valid ISO C++ however. */
6467 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6469 if (complain & tf_error)
6470 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
6472 return error_mark_node;
6473 type = build_pointer_type (TREE_TYPE (type));
6477 if (complain & tf_error)
6478 error ("ISO C++ forbids casting to an array type %qT", type);
6479 return error_mark_node;
6483 if (TREE_CODE (type) == FUNCTION_TYPE
6484 || TREE_CODE (type) == METHOD_TYPE)
6486 if (complain & tf_error)
6487 error ("invalid cast to function type %qT", type);
6488 return error_mark_node;
6491 if (TREE_CODE (type) == POINTER_TYPE
6492 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
6493 /* Casting to an integer of smaller size is an error detected elsewhere. */
6494 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
6495 /* Don't warn about converting any constant. */
6496 && !TREE_CONSTANT (value))
6497 warning_at (input_location, OPT_Wint_to_pointer_cast,
6498 "cast to pointer from integer of different size");
6500 /* A C-style cast can be a const_cast. */
6501 result = build_const_cast_1 (type, value, complain & tf_warning,
6506 /* Or a static cast. */
6507 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
6508 &valid_p, complain);
6509 /* Or a reinterpret_cast. */
6511 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
6512 &valid_p, complain);
6513 /* The static_cast or reinterpret_cast may be followed by a
6516 /* A valid cast may result in errors if, for example, a
6517 conversion to am ambiguous base class is required. */
6518 && !error_operand_p (result))
6522 /* Non-class rvalues always have cv-unqualified type. */
6523 if (!CLASS_TYPE_P (type))
6524 type = TYPE_MAIN_VARIANT (type);
6525 result_type = TREE_TYPE (result);
6526 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
6527 result_type = TYPE_MAIN_VARIANT (result_type);
6528 /* If the type of RESULT does not match TYPE, perform a
6529 const_cast to make it match. If the static_cast or
6530 reinterpret_cast succeeded, we will differ by at most
6531 cv-qualification, so the follow-on const_cast is guaranteed
6533 if (!same_type_p (non_reference (type), non_reference (result_type)))
6535 result = build_const_cast_1 (type, result, false, &valid_p);
6536 gcc_assert (valid_p);
6541 return error_mark_node;
6544 /* For use from the C common bits. */
6546 build_modify_expr (location_t location ATTRIBUTE_UNUSED,
6547 tree lhs, tree lhs_origtype ATTRIBUTE_UNUSED,
6548 enum tree_code modifycode,
6549 location_t rhs_location ATTRIBUTE_UNUSED, tree rhs,
6550 tree rhs_origtype ATTRIBUTE_UNUSED)
6552 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
6555 /* Build an assignment expression of lvalue LHS from value RHS.
6556 MODIFYCODE is the code for a binary operator that we use
6557 to combine the old value of LHS with RHS to get the new value.
6558 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6560 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
6563 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6564 tsubst_flags_t complain)
6568 tree lhstype = TREE_TYPE (lhs);
6569 tree olhstype = lhstype;
6570 bool plain_assign = (modifycode == NOP_EXPR);
6572 /* Avoid duplicate error messages from operands that had errors. */
6573 if (error_operand_p (lhs) || error_operand_p (rhs))
6574 return error_mark_node;
6576 /* Handle control structure constructs used as "lvalues". */
6577 switch (TREE_CODE (lhs))
6579 /* Handle --foo = 5; as these are valid constructs in C++. */
6580 case PREDECREMENT_EXPR:
6581 case PREINCREMENT_EXPR:
6582 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6583 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6584 stabilize_reference (TREE_OPERAND (lhs, 0)),
6585 TREE_OPERAND (lhs, 1));
6586 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
6587 modifycode, rhs, complain);
6588 if (newrhs == error_mark_node)
6589 return error_mark_node;
6590 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6592 /* Handle (a, b) used as an "lvalue". */
6594 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6595 modifycode, rhs, complain);
6596 if (newrhs == error_mark_node)
6597 return error_mark_node;
6598 return build2 (COMPOUND_EXPR, lhstype,
6599 TREE_OPERAND (lhs, 0), newrhs);
6602 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
6603 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
6604 stabilize_reference (TREE_OPERAND (lhs, 0)),
6605 TREE_OPERAND (lhs, 1));
6606 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
6608 if (newrhs == error_mark_node)
6609 return error_mark_node;
6610 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
6614 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
6615 when neither operand has side-effects. */
6616 if (!lvalue_or_else (lhs, lv_assign, complain))
6617 return error_mark_node;
6619 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
6620 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
6622 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
6623 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
6625 TREE_OPERAND (lhs, 0),
6626 TREE_OPERAND (lhs, 1)),
6627 TREE_OPERAND (lhs, 0),
6628 TREE_OPERAND (lhs, 1));
6631 /* Handle (a ? b : c) used as an "lvalue". */
6634 /* Produce (a ? (b = rhs) : (c = rhs))
6635 except that the RHS goes through a save-expr
6636 so the code to compute it is only emitted once. */
6638 tree preeval = NULL_TREE;
6640 if (VOID_TYPE_P (TREE_TYPE (rhs)))
6642 if (complain & tf_error)
6643 error ("void value not ignored as it ought to be");
6644 return error_mark_node;
6647 rhs = stabilize_expr (rhs, &preeval);
6649 /* Check this here to avoid odd errors when trying to convert
6650 a throw to the type of the COND_EXPR. */
6651 if (!lvalue_or_else (lhs, lv_assign, complain))
6652 return error_mark_node;
6654 cond = build_conditional_expr
6655 (TREE_OPERAND (lhs, 0),
6656 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
6657 modifycode, rhs, complain),
6658 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
6659 modifycode, rhs, complain),
6662 if (cond == error_mark_node)
6664 /* Make sure the code to compute the rhs comes out
6665 before the split. */
6667 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
6675 if (modifycode == INIT_EXPR)
6677 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6678 /* Do the default thing. */;
6679 else if (TREE_CODE (rhs) == CONSTRUCTOR)
6681 /* Compound literal. */
6682 if (! same_type_p (TREE_TYPE (rhs), lhstype))
6683 /* Call convert to generate an error; see PR 11063. */
6684 rhs = convert (lhstype, rhs);
6685 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
6686 TREE_SIDE_EFFECTS (result) = 1;
6689 else if (! MAYBE_CLASS_TYPE_P (lhstype))
6690 /* Do the default thing. */;
6693 VEC(tree,gc) *rhs_vec = make_tree_vector_single (rhs);
6694 result = build_special_member_call (lhs, complete_ctor_identifier,
6695 &rhs_vec, lhstype, LOOKUP_NORMAL,
6697 release_tree_vector (rhs_vec);
6698 if (result == NULL_TREE)
6699 return error_mark_node;
6705 lhs = require_complete_type_sfinae (lhs, complain);
6706 if (lhs == error_mark_node)
6707 return error_mark_node;
6709 if (modifycode == NOP_EXPR)
6711 if (c_dialect_objc ())
6713 result = objc_maybe_build_modify_expr (lhs, rhs);
6718 /* `operator=' is not an inheritable operator. */
6719 if (! MAYBE_CLASS_TYPE_P (lhstype))
6720 /* Do the default thing. */;
6723 result = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL,
6724 lhs, rhs, make_node (NOP_EXPR),
6727 if (result == NULL_TREE)
6728 return error_mark_node;
6735 tree init = NULL_TREE;
6737 /* A binary op has been requested. Combine the old LHS
6738 value with the RHS producing the value we should actually
6739 store into the LHS. */
6740 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
6741 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
6742 || MAYBE_CLASS_TYPE_P (lhstype)));
6744 /* Preevaluate the RHS to make sure its evaluation is complete
6745 before the lvalue-to-rvalue conversion of the LHS:
6747 [expr.ass] With respect to an indeterminately-sequenced
6748 function call, the operation of a compound assignment is a
6749 single evaluation. [ Note: Therefore, a function call shall
6750 not intervene between the lvalue-to-rvalue conversion and the
6751 side effect associated with any single compound assignment
6752 operator. -- end note ] */
6753 lhs = stabilize_reference (lhs);
6754 if (TREE_SIDE_EFFECTS (rhs))
6755 rhs = mark_rvalue_use (rhs);
6756 rhs = stabilize_expr (rhs, &init);
6757 newrhs = cp_build_binary_op (input_location,
6758 modifycode, lhs, rhs,
6760 if (newrhs == error_mark_node)
6762 if (complain & tf_error)
6763 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
6764 TREE_TYPE (lhs), TREE_TYPE (rhs));
6765 return error_mark_node;
6769 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
6771 /* Now it looks like a plain assignment. */
6772 modifycode = NOP_EXPR;
6773 if (c_dialect_objc ())
6775 result = objc_maybe_build_modify_expr (lhs, newrhs);
6780 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
6781 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
6784 /* The left-hand side must be an lvalue. */
6785 if (!lvalue_or_else (lhs, lv_assign, complain))
6786 return error_mark_node;
6788 /* Warn about modifying something that is `const'. Don't warn if
6789 this is initialization. */
6790 if (modifycode != INIT_EXPR
6791 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
6792 /* Functions are not modifiable, even though they are
6794 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
6795 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
6796 /* If it's an aggregate and any field is const, then it is
6797 effectively const. */
6798 || (CLASS_TYPE_P (lhstype)
6799 && C_TYPE_FIELDS_READONLY (lhstype))))
6801 if (complain & tf_error)
6802 cxx_readonly_error (lhs, lv_assign);
6804 return error_mark_node;
6807 /* If storing into a structure or union member, it may have been given a
6808 lowered bitfield type. We need to convert to the declared type first,
6809 so retrieve it now. */
6811 olhstype = unlowered_expr_type (lhs);
6813 /* Convert new value to destination type. */
6815 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6819 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
6821 if (modifycode != INIT_EXPR)
6823 if (complain & tf_error)
6824 error ("assigning to an array from an initializer list");
6825 return error_mark_node;
6827 if (check_array_initializer (lhs, lhstype, newrhs))
6828 return error_mark_node;
6829 newrhs = digest_init (lhstype, newrhs, complain);
6830 if (newrhs == error_mark_node)
6831 return error_mark_node;
6834 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
6835 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
6837 if (complain & tf_error)
6838 error ("incompatible types in assignment of %qT to %qT",
6839 TREE_TYPE (rhs), lhstype);
6840 return error_mark_node;
6843 /* Allow array assignment in compiler-generated code. */
6844 else if (!current_function_decl
6845 || !DECL_DEFAULTED_FN (current_function_decl))
6847 /* This routine is used for both initialization and assignment.
6848 Make sure the diagnostic message differentiates the context. */
6849 if (complain & tf_error)
6851 if (modifycode == INIT_EXPR)
6852 error ("array used as initializer");
6854 error ("invalid array assignment");
6856 return error_mark_node;
6859 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6860 ? 1 + (modifycode != INIT_EXPR): 0;
6861 return build_vec_init (lhs, NULL_TREE, newrhs,
6862 /*explicit_value_init_p=*/false,
6863 from_array, complain);
6866 if (modifycode == INIT_EXPR)
6867 /* Calls with INIT_EXPR are all direct-initialization, so don't set
6868 LOOKUP_ONLYCONVERTING. */
6869 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
6870 ICR_INIT, NULL_TREE, 0,
6873 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
6874 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
6876 if (!same_type_p (lhstype, olhstype))
6877 newrhs = cp_convert_and_check (lhstype, newrhs);
6879 if (modifycode != INIT_EXPR)
6881 if (TREE_CODE (newrhs) == CALL_EXPR
6882 && TYPE_NEEDS_CONSTRUCTING (lhstype))
6883 newrhs = build_cplus_new (lhstype, newrhs, complain);
6885 /* Can't initialize directly from a TARGET_EXPR, since that would
6886 cause the lhs to be constructed twice, and possibly result in
6887 accidental self-initialization. So we force the TARGET_EXPR to be
6888 expanded without a target. */
6889 if (TREE_CODE (newrhs) == TARGET_EXPR)
6890 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6891 TREE_OPERAND (newrhs, 0));
6894 if (newrhs == error_mark_node)
6895 return error_mark_node;
6897 if (c_dialect_objc () && flag_objc_gc)
6899 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6905 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6906 lhstype, lhs, newrhs);
6908 TREE_SIDE_EFFECTS (result) = 1;
6910 TREE_NO_WARNING (result) = 1;
6916 build_x_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
6917 tsubst_flags_t complain)
6919 if (processing_template_decl)
6920 return build_min_nt (MODOP_EXPR, lhs,
6921 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6923 if (modifycode != NOP_EXPR)
6925 tree rval = build_new_op (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6926 make_node (modifycode),
6931 TREE_NO_WARNING (rval) = 1;
6935 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
6938 /* Helper function for get_delta_difference which assumes FROM is a base
6939 class of TO. Returns a delta for the conversion of pointer-to-member
6940 of FROM to pointer-to-member of TO. If the conversion is invalid and
6941 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
6942 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
6943 If C_CAST_P is true, this conversion is taking place as part of a
6947 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
6948 tsubst_flags_t complain)
6952 base_access access = c_cast_p ? ba_unique : ba_check;
6954 /* Note: ba_quiet does not distinguish between access control and
6956 if (!(complain & tf_error))
6959 binfo = lookup_base (to, from, access, &kind);
6961 if (kind == bk_inaccessible || kind == bk_ambig)
6963 if (!(complain & tf_error))
6964 return error_mark_node;
6966 error (" in pointer to member function conversion");
6967 return size_zero_node;
6971 if (kind != bk_via_virtual)
6972 return BINFO_OFFSET (binfo);
6974 /* FROM is a virtual base class of TO. Issue an error or warning
6975 depending on whether or not this is a reinterpret cast. */
6977 if (!(complain & tf_error))
6978 return error_mark_node;
6980 error ("pointer to member conversion via virtual base %qT",
6981 BINFO_TYPE (binfo_from_vbase (binfo)));
6983 return size_zero_node;
6990 /* Get difference in deltas for different pointer to member function
6991 types. If the conversion is invalid and tf_error is not set in
6992 COMPLAIN, returns error_mark_node, otherwise returns an integer
6993 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
6994 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
6995 conversions as well. If C_CAST_P is true this conversion is taking
6996 place as part of a C-style cast.
6998 Note that the naming of FROM and TO is kind of backwards; the return
6999 value is what we add to a TO in order to get a FROM. They are named
7000 this way because we call this function to find out how to convert from
7001 a pointer to member of FROM to a pointer to member of TO. */
7004 get_delta_difference (tree from, tree to,
7005 bool allow_inverse_p,
7006 bool c_cast_p, tsubst_flags_t complain)
7010 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7011 /* Pointer to member of incomplete class is permitted*/
7012 result = size_zero_node;
7014 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7016 if (result == error_mark_node)
7017 return error_mark_node;
7021 if (!allow_inverse_p)
7023 if (!(complain & tf_error))
7024 return error_mark_node;
7026 error_not_base_type (from, to);
7027 error (" in pointer to member conversion");
7028 result = size_zero_node;
7032 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7034 if (result == error_mark_node)
7035 return error_mark_node;
7038 result = size_diffop_loc (input_location,
7039 size_zero_node, result);
7042 if (!(complain & tf_error))
7043 return error_mark_node;
7045 error_not_base_type (from, to);
7046 error (" in pointer to member conversion");
7047 result = size_zero_node;
7052 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7056 /* Return a constructor for the pointer-to-member-function TYPE using
7057 the other components as specified. */
7060 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7065 VEC(constructor_elt, gc) *v;
7067 /* Pull the FIELD_DECLs out of the type. */
7068 pfn_field = TYPE_FIELDS (type);
7069 delta_field = DECL_CHAIN (pfn_field);
7071 /* Make sure DELTA has the type we want. */
7072 delta = convert_and_check (delta_type_node, delta);
7074 /* Convert to the correct target type if necessary. */
7075 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7077 /* Finish creating the initializer. */
7078 v = VEC_alloc(constructor_elt, gc, 2);
7079 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7080 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7081 u = build_constructor (type, v);
7082 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7083 TREE_STATIC (u) = (TREE_CONSTANT (u)
7084 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7086 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7091 /* Build a constructor for a pointer to member function. It can be
7092 used to initialize global variables, local variable, or used
7093 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7096 If FORCE is nonzero, then force this conversion, even if
7097 we would rather not do it. Usually set when using an explicit
7098 cast. A C-style cast is being processed iff C_CAST_P is true.
7100 Return error_mark_node, if something goes wrong. */
7103 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7104 tsubst_flags_t complain)
7110 if (error_operand_p (pfn))
7111 return error_mark_node;
7113 pfn_type = TREE_TYPE (pfn);
7114 to_type = build_ptrmemfunc_type (type);
7116 /* Handle multiple conversions of pointer to member functions. */
7117 if (TYPE_PTRMEMFUNC_P (pfn_type))
7119 tree delta = NULL_TREE;
7120 tree npfn = NULL_TREE;
7124 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn, LOOKUP_NORMAL))
7125 error ("invalid conversion to type %qT from type %qT",
7128 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7129 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7131 c_cast_p, complain);
7132 if (n == error_mark_node)
7133 return error_mark_node;
7135 /* We don't have to do any conversion to convert a
7136 pointer-to-member to its own type. But, we don't want to
7137 just return a PTRMEM_CST if there's an explicit cast; that
7138 cast should make the expression an invalid template argument. */
7139 if (TREE_CODE (pfn) != PTRMEM_CST)
7141 if (same_type_p (to_type, pfn_type))
7143 else if (integer_zerop (n))
7144 return build_reinterpret_cast (to_type, pfn,
7145 tf_warning_or_error);
7148 if (TREE_SIDE_EFFECTS (pfn))
7149 pfn = save_expr (pfn);
7151 /* Obtain the function pointer and the current DELTA. */
7152 if (TREE_CODE (pfn) == PTRMEM_CST)
7153 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7156 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7157 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7160 /* Just adjust the DELTA field. */
7161 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7162 (TREE_TYPE (delta), ptrdiff_type_node));
7163 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7164 n = cp_build_binary_op (input_location,
7165 LSHIFT_EXPR, n, integer_one_node,
7166 tf_warning_or_error);
7167 delta = cp_build_binary_op (input_location,
7168 PLUS_EXPR, delta, n, tf_warning_or_error);
7169 return build_ptrmemfunc1 (to_type, delta, npfn);
7172 /* Handle null pointer to member function conversions. */
7173 if (null_ptr_cst_p (pfn))
7175 pfn = build_c_cast (input_location, type, nullptr_node);
7176 return build_ptrmemfunc1 (to_type,
7181 if (type_unknown_p (pfn))
7182 return instantiate_type (type, pfn, tf_warning_or_error);
7184 fn = TREE_OPERAND (pfn, 0);
7185 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7186 /* In a template, we will have preserved the
7188 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7189 return make_ptrmem_cst (to_type, fn);
7192 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7195 ??? There is no consistency as to the types returned for the above
7196 values. Some code acts as if it were a sizetype and some as if it were
7197 integer_type_node. */
7200 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7202 tree type = TREE_TYPE (cst);
7203 tree fn = PTRMEM_CST_MEMBER (cst);
7204 tree ptr_class, fn_class;
7206 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7208 /* The class that the function belongs to. */
7209 fn_class = DECL_CONTEXT (fn);
7211 /* The class that we're creating a pointer to member of. */
7212 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7214 /* First, calculate the adjustment to the function's class. */
7215 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7216 /*c_cast_p=*/0, tf_warning_or_error);
7218 if (!DECL_VIRTUAL_P (fn))
7219 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
7222 /* If we're dealing with a virtual function, we have to adjust 'this'
7223 again, to point to the base which provides the vtable entry for
7224 fn; the call will do the opposite adjustment. */
7225 tree orig_class = DECL_CONTEXT (fn);
7226 tree binfo = binfo_or_else (orig_class, fn_class);
7227 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7228 *delta, BINFO_OFFSET (binfo));
7229 *delta = fold_if_not_in_template (*delta);
7231 /* We set PFN to the vtable offset at which the function can be
7232 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7233 case delta is shifted left, and then incremented). */
7234 *pfn = DECL_VINDEX (fn);
7235 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7236 TYPE_SIZE_UNIT (vtable_entry_type));
7237 *pfn = fold_if_not_in_template (*pfn);
7239 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7241 case ptrmemfunc_vbit_in_pfn:
7242 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7244 *pfn = fold_if_not_in_template (*pfn);
7247 case ptrmemfunc_vbit_in_delta:
7248 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7249 *delta, integer_one_node);
7250 *delta = fold_if_not_in_template (*delta);
7251 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7252 *delta, integer_one_node);
7253 *delta = fold_if_not_in_template (*delta);
7260 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
7261 *pfn = fold_if_not_in_template (*pfn);
7265 /* Return an expression for PFN from the pointer-to-member function
7269 pfn_from_ptrmemfunc (tree t)
7271 if (TREE_CODE (t) == PTRMEM_CST)
7276 expand_ptrmemfunc_cst (t, &delta, &pfn);
7281 return build_ptrmemfunc_access_expr (t, pfn_identifier);
7284 /* Return an expression for DELTA from the pointer-to-member function
7288 delta_from_ptrmemfunc (tree t)
7290 if (TREE_CODE (t) == PTRMEM_CST)
7295 expand_ptrmemfunc_cst (t, &delta, &pfn);
7300 return build_ptrmemfunc_access_expr (t, delta_identifier);
7303 /* Convert value RHS to type TYPE as preparation for an assignment to
7304 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7305 implicit conversion is. If FNDECL is non-NULL, we are doing the
7306 conversion in order to pass the PARMNUMth argument of FNDECL.
7307 If FNDECL is NULL, we are doing the conversion in function pointer
7308 argument passing, conversion in initialization, etc. */
7311 convert_for_assignment (tree type, tree rhs,
7312 impl_conv_rhs errtype, tree fndecl, int parmnum,
7313 tsubst_flags_t complain, int flags)
7316 enum tree_code coder;
7318 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7319 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
7320 rhs = TREE_OPERAND (rhs, 0);
7322 rhstype = TREE_TYPE (rhs);
7323 coder = TREE_CODE (rhstype);
7325 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
7326 && vector_types_convertible_p (type, rhstype, true))
7328 rhs = mark_rvalue_use (rhs);
7329 return convert (type, rhs);
7332 if (rhs == error_mark_node || rhstype == error_mark_node)
7333 return error_mark_node;
7334 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
7335 return error_mark_node;
7337 /* The RHS of an assignment cannot have void type. */
7338 if (coder == VOID_TYPE)
7340 if (complain & tf_error)
7341 error ("void value not ignored as it ought to be");
7342 return error_mark_node;
7345 /* Simplify the RHS if possible. */
7346 if (TREE_CODE (rhs) == CONST_DECL)
7347 rhs = DECL_INITIAL (rhs);
7349 if (c_dialect_objc ())
7353 tree rname = fndecl;
7364 selector = objc_message_selector ();
7366 if (selector && parmno > 1)
7373 if (objc_compare_types (type, rhstype, parmno, rname))
7375 rhs = mark_rvalue_use (rhs);
7376 return convert (type, rhs);
7382 The expression is implicitly converted (clause _conv_) to the
7383 cv-unqualified type of the left operand.
7385 We allow bad conversions here because by the time we get to this point
7386 we are committed to doing the conversion. If we end up doing a bad
7387 conversion, convert_like will complain. */
7388 if (!can_convert_arg_bad (type, rhstype, rhs, flags))
7390 /* When -Wno-pmf-conversions is use, we just silently allow
7391 conversions from pointers-to-members to plain pointers. If
7392 the conversion doesn't work, cp_convert will complain. */
7394 && TYPE_PTR_P (type)
7395 && TYPE_PTRMEMFUNC_P (rhstype))
7396 rhs = cp_convert (strip_top_quals (type), rhs);
7399 if (complain & tf_error)
7401 /* If the right-hand side has unknown type, then it is an
7402 overloaded function. Call instantiate_type to get error
7404 if (rhstype == unknown_type_node)
7405 instantiate_type (type, rhs, tf_warning_or_error);
7407 error ("cannot convert %qT to %qT for argument %qP to %qD",
7408 rhstype, type, parmnum, fndecl);
7412 case ICR_DEFAULT_ARGUMENT:
7413 error ("cannot convert %qT to %qT in default argument",
7417 error ("cannot convert %qT to %qT in argument passing",
7420 case ICR_CONVERTING:
7421 error ("cannot convert %qT to %qT",
7425 error ("cannot convert %qT to %qT in initialization",
7429 error ("cannot convert %qT to %qT in return",
7433 error ("cannot convert %qT to %qT in assignment",
7440 return error_mark_node;
7443 if (warn_missing_format_attribute)
7445 const enum tree_code codel = TREE_CODE (type);
7446 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7448 && check_missing_format_attribute (type, rhstype)
7449 && (complain & tf_warning))
7453 case ICR_DEFAULT_ARGUMENT:
7455 warning (OPT_Wmissing_format_attribute,
7456 "parameter %qP of %qD might be a candidate "
7457 "for a format attribute", parmnum, fndecl);
7459 warning (OPT_Wmissing_format_attribute,
7460 "parameter might be a candidate "
7461 "for a format attribute");
7463 case ICR_CONVERTING:
7464 warning (OPT_Wmissing_format_attribute,
7465 "target of conversion might be a candidate "
7466 "for a format attribute");
7469 warning (OPT_Wmissing_format_attribute,
7470 "target of initialization might be a candidate "
7471 "for a format attribute");
7474 warning (OPT_Wmissing_format_attribute,
7475 "return type might be a candidate "
7476 "for a format attribute");
7479 warning (OPT_Wmissing_format_attribute,
7480 "left-hand side of assignment might be a candidate "
7481 "for a format attribute");
7488 /* If -Wparentheses, warn about a = b = c when a has type bool and b
7490 if (warn_parentheses
7491 && TREE_CODE (type) == BOOLEAN_TYPE
7492 && TREE_CODE (rhs) == MODIFY_EXPR
7493 && !TREE_NO_WARNING (rhs)
7494 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
7495 && (complain & tf_warning))
7497 location_t loc = EXPR_LOC_OR_HERE (rhs);
7499 warning_at (loc, OPT_Wparentheses,
7500 "suggest parentheses around assignment used as truth value");
7501 TREE_NO_WARNING (rhs) = 1;
7504 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
7508 /* Convert RHS to be of type TYPE.
7509 If EXP is nonzero, it is the target of the initialization.
7510 ERRTYPE indicates what kind of error the implicit conversion is.
7512 Two major differences between the behavior of
7513 `convert_for_assignment' and `convert_for_initialization'
7514 are that references are bashed in the former, while
7515 copied in the latter, and aggregates are assigned in
7516 the former (operator=) while initialized in the
7519 If using constructor make sure no conversion operator exists, if one does
7520 exist, an ambiguity exists.
7522 If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything. */
7525 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
7526 impl_conv_rhs errtype, tree fndecl, int parmnum,
7527 tsubst_flags_t complain)
7529 enum tree_code codel = TREE_CODE (type);
7531 enum tree_code coder;
7533 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7534 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
7535 if (TREE_CODE (rhs) == NOP_EXPR
7536 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7537 && codel != REFERENCE_TYPE)
7538 rhs = TREE_OPERAND (rhs, 0);
7540 if (type == error_mark_node
7541 || rhs == error_mark_node
7542 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7543 return error_mark_node;
7545 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7546 && TREE_CODE (type) != ARRAY_TYPE
7547 && (TREE_CODE (type) != REFERENCE_TYPE
7548 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7549 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7550 && (TREE_CODE (type) != REFERENCE_TYPE
7551 || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
7552 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7553 rhs = decay_conversion (rhs);
7555 rhstype = TREE_TYPE (rhs);
7556 coder = TREE_CODE (rhstype);
7558 if (coder == ERROR_MARK)
7559 return error_mark_node;
7561 /* We accept references to incomplete types, so we can
7562 return here before checking if RHS is of complete type. */
7564 if (codel == REFERENCE_TYPE)
7566 /* This should eventually happen in convert_arguments. */
7567 int savew = 0, savee = 0;
7570 savew = warningcount, savee = errorcount;
7571 rhs = initialize_reference (type, rhs, flags, complain);
7574 if (warningcount > savew)
7575 warning (0, "in passing argument %P of %q+D", parmnum, fndecl);
7576 else if (errorcount > savee)
7577 error ("in passing argument %P of %q+D", parmnum, fndecl);
7583 exp = require_complete_type_sfinae (exp, complain);
7584 if (exp == error_mark_node)
7585 return error_mark_node;
7587 rhstype = non_reference (rhstype);
7589 type = complete_type (type);
7591 if (DIRECT_INIT_EXPR_P (type, rhs))
7592 /* Don't try to do copy-initialization if we already have
7593 direct-initialization. */
7596 if (MAYBE_CLASS_TYPE_P (type))
7597 return perform_implicit_conversion_flags (type, rhs, complain, flags);
7599 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
7603 /* If RETVAL is the address of, or a reference to, a local variable or
7604 temporary give an appropriate warning. */
7607 maybe_warn_about_returning_address_of_local (tree retval)
7609 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
7610 tree whats_returned = retval;
7614 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7615 whats_returned = TREE_OPERAND (whats_returned, 1);
7616 else if (CONVERT_EXPR_P (whats_returned)
7617 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
7618 whats_returned = TREE_OPERAND (whats_returned, 0);
7623 if (TREE_CODE (whats_returned) != ADDR_EXPR)
7625 whats_returned = TREE_OPERAND (whats_returned, 0);
7627 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7629 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7630 || TREE_CODE (whats_returned) == TARGET_EXPR)
7632 warning (0, "returning reference to temporary");
7635 if (TREE_CODE (whats_returned) == VAR_DECL
7636 && DECL_NAME (whats_returned)
7637 && TEMP_NAME_P (DECL_NAME (whats_returned)))
7639 warning (0, "reference to non-lvalue returned");
7644 while (TREE_CODE (whats_returned) == COMPONENT_REF
7645 || TREE_CODE (whats_returned) == ARRAY_REF)
7646 whats_returned = TREE_OPERAND (whats_returned, 0);
7648 if (DECL_P (whats_returned)
7649 && DECL_NAME (whats_returned)
7650 && DECL_FUNCTION_SCOPE_P (whats_returned)
7651 && !(TREE_STATIC (whats_returned)
7652 || TREE_PUBLIC (whats_returned)))
7654 if (TREE_CODE (valtype) == REFERENCE_TYPE)
7655 warning (0, "reference to local variable %q+D returned",
7658 warning (0, "address of local variable %q+D returned",
7664 /* Check that returning RETVAL from the current function is valid.
7665 Return an expression explicitly showing all conversions required to
7666 change RETVAL into the function return type, and to assign it to
7667 the DECL_RESULT for the function. Set *NO_WARNING to true if
7668 code reaches end of non-void function warning shouldn't be issued
7669 on this RETURN_EXPR. */
7672 check_return_expr (tree retval, bool *no_warning)
7675 /* The type actually returned by the function, after any
7678 int fn_returns_value_p;
7679 bool named_return_value_okay_p;
7681 *no_warning = false;
7683 /* A `volatile' function is one that isn't supposed to return, ever.
7684 (This is a G++ extension, used to get better code for functions
7685 that call the `volatile' function.) */
7686 if (TREE_THIS_VOLATILE (current_function_decl))
7687 warning (0, "function declared %<noreturn%> has a %<return%> statement");
7689 /* Check for various simple errors. */
7690 if (DECL_DESTRUCTOR_P (current_function_decl))
7693 error ("returning a value from a destructor");
7696 else if (DECL_CONSTRUCTOR_P (current_function_decl))
7698 if (in_function_try_handler)
7699 /* If a return statement appears in a handler of the
7700 function-try-block of a constructor, the program is ill-formed. */
7701 error ("cannot return from a handler of a function-try-block of a constructor");
7703 /* You can't return a value from a constructor. */
7704 error ("returning a value from a constructor");
7708 /* As an extension, deduce lambda return type from a return statement
7709 anywhere in the body. */
7710 if (retval && LAMBDA_FUNCTION_P (current_function_decl))
7712 tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7713 if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda))
7715 tree type = lambda_return_type (retval);
7716 tree oldtype = LAMBDA_EXPR_RETURN_TYPE (lambda);
7718 if (oldtype == NULL_TREE)
7719 apply_lambda_return_type (lambda, type);
7720 /* If one of the answers is type-dependent, we can't do any
7721 better until instantiation time. */
7722 else if (oldtype == dependent_lambda_return_type_node)
7724 else if (type == dependent_lambda_return_type_node)
7725 apply_lambda_return_type (lambda, type);
7726 else if (!same_type_p (type, oldtype))
7727 error ("inconsistent types %qT and %qT deduced for "
7728 "lambda return type", type, oldtype);
7732 if (processing_template_decl)
7734 current_function_returns_value = 1;
7735 if (check_for_bare_parameter_packs (retval))
7736 retval = error_mark_node;
7740 /* When no explicit return-value is given in a function with a named
7741 return value, the named return value is used. */
7742 result = DECL_RESULT (current_function_decl);
7743 valtype = TREE_TYPE (result);
7744 gcc_assert (valtype != NULL_TREE);
7745 fn_returns_value_p = !VOID_TYPE_P (valtype);
7746 if (!retval && DECL_NAME (result) && fn_returns_value_p)
7749 /* Check for a return statement with no return value in a function
7750 that's supposed to return a value. */
7751 if (!retval && fn_returns_value_p)
7753 permerror (input_location, "return-statement with no value, in function returning %qT",
7755 /* Clear this, so finish_function won't say that we reach the
7756 end of a non-void function (which we don't, we gave a
7758 current_function_returns_null = 0;
7759 /* And signal caller that TREE_NO_WARNING should be set on the
7760 RETURN_EXPR to avoid control reaches end of non-void function
7761 warnings in tree-cfg.c. */
7764 /* Check for a return statement with a value in a function that
7765 isn't supposed to return a value. */
7766 else if (retval && !fn_returns_value_p)
7768 if (VOID_TYPE_P (TREE_TYPE (retval)))
7769 /* You can return a `void' value from a function of `void'
7770 type. In that case, we have to evaluate the expression for
7771 its side-effects. */
7772 finish_expr_stmt (retval);
7774 permerror (input_location, "return-statement with a value, in function "
7775 "returning 'void'");
7776 current_function_returns_null = 1;
7778 /* There's really no value to return, after all. */
7782 /* Remember that this function can sometimes return without a
7784 current_function_returns_null = 1;
7786 /* Remember that this function did return a value. */
7787 current_function_returns_value = 1;
7789 /* Check for erroneous operands -- but after giving ourselves a
7790 chance to provide an error about returning a value from a void
7792 if (error_operand_p (retval))
7794 current_function_return_value = error_mark_node;
7795 return error_mark_node;
7798 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
7799 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
7800 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
7801 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
7803 && retval && null_ptr_cst_p (retval))
7804 warning (0, "%<operator new%> must not return NULL unless it is "
7805 "declared %<throw()%> (or -fcheck-new is in effect)");
7807 /* Effective C++ rule 15. See also start_function. */
7809 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
7813 /* The function return type must be a reference to the current
7815 if (TREE_CODE (valtype) == REFERENCE_TYPE
7816 && same_type_ignoring_top_level_qualifiers_p
7817 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
7819 /* Returning '*this' is obviously OK. */
7820 if (retval == current_class_ref)
7822 /* If we are calling a function whose return type is the same of
7823 the current class reference, it is ok. */
7824 else if (TREE_CODE (retval) == INDIRECT_REF
7825 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
7830 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
7833 /* The fabled Named Return Value optimization, as per [class.copy]/15:
7835 [...] For a function with a class return type, if the expression
7836 in the return statement is the name of a local object, and the cv-
7837 unqualified type of the local object is the same as the function
7838 return type, an implementation is permitted to omit creating the tem-
7839 porary object to hold the function return value [...]
7841 So, if this is a value-returning function that always returns the same
7842 local variable, remember it.
7844 It might be nice to be more flexible, and choose the first suitable
7845 variable even if the function sometimes returns something else, but
7846 then we run the risk of clobbering the variable we chose if the other
7847 returned expression uses the chosen variable somehow. And people expect
7848 this restriction, anyway. (jason 2000-11-19)
7850 See finish_function and finalize_nrv for the rest of this optimization. */
7852 named_return_value_okay_p =
7853 (retval != NULL_TREE
7854 /* Must be a local, automatic variable. */
7855 && TREE_CODE (retval) == VAR_DECL
7856 && DECL_CONTEXT (retval) == current_function_decl
7857 && ! TREE_STATIC (retval)
7858 && ! DECL_ANON_UNION_VAR_P (retval)
7859 && (DECL_ALIGN (retval)
7860 >= DECL_ALIGN (DECL_RESULT (current_function_decl)))
7861 /* The cv-unqualified type of the returned value must be the
7862 same as the cv-unqualified return type of the
7864 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
7866 (TREE_TYPE (TREE_TYPE (current_function_decl)))))
7867 /* And the returned value must be non-volatile. */
7868 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
7870 if (fn_returns_value_p && flag_elide_constructors)
7872 if (named_return_value_okay_p
7873 && (current_function_return_value == NULL_TREE
7874 || current_function_return_value == retval))
7875 current_function_return_value = retval;
7877 current_function_return_value = error_mark_node;
7880 /* We don't need to do any conversions when there's nothing being
7885 /* Do any required conversions. */
7886 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
7887 /* No conversions are required. */
7891 /* The type the function is declared to return. */
7892 tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7893 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
7895 /* The functype's return type will have been set to void, if it
7896 was an incomplete type. Just treat this as 'return;' */
7897 if (VOID_TYPE_P (functype))
7898 return error_mark_node;
7900 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
7901 treated as an rvalue for the purposes of overload resolution to
7902 favor move constructors over copy constructors.
7904 Note that these conditions are similar to, but not as strict as,
7905 the conditions for the named return value optimization. */
7906 if ((cxx_dialect != cxx98)
7907 && (TREE_CODE (retval) == VAR_DECL
7908 || TREE_CODE (retval) == PARM_DECL)
7909 && DECL_CONTEXT (retval) == current_function_decl
7910 && !TREE_STATIC (retval)
7911 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
7913 (TREE_TYPE (TREE_TYPE (current_function_decl)))))
7914 /* This is only interesting for class type. */
7915 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
7916 flags = flags | LOOKUP_PREFER_RVALUE;
7918 /* First convert the value to the function's return type, then
7919 to the type of return value's location to handle the
7920 case that functype is smaller than the valtype. */
7921 retval = convert_for_initialization
7922 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
7923 tf_warning_or_error);
7924 retval = convert (valtype, retval);
7926 /* If the conversion failed, treat this just like `return;'. */
7927 if (retval == error_mark_node)
7929 /* We can't initialize a register from a AGGR_INIT_EXPR. */
7930 else if (! cfun->returns_struct
7931 && TREE_CODE (retval) == TARGET_EXPR
7932 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
7933 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
7934 TREE_OPERAND (retval, 0));
7936 maybe_warn_about_returning_address_of_local (retval);
7939 /* Actually copy the value returned into the appropriate location. */
7940 if (retval && retval != result)
7941 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
7947 /* Returns nonzero if the pointer-type FROM can be converted to the
7948 pointer-type TO via a qualification conversion. If CONSTP is -1,
7949 then we return nonzero if the pointers are similar, and the
7950 cv-qualification signature of FROM is a proper subset of that of TO.
7952 If CONSTP is positive, then all outer pointers have been
7956 comp_ptr_ttypes_real (tree to, tree from, int constp)
7958 bool to_more_cv_qualified = false;
7959 bool is_opaque_pointer = false;
7961 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7963 if (TREE_CODE (to) != TREE_CODE (from))
7966 if (TREE_CODE (from) == OFFSET_TYPE
7967 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
7968 TYPE_OFFSET_BASETYPE (to)))
7971 /* Const and volatile mean something different for function types,
7972 so the usual checks are not appropriate. */
7973 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7975 if (!at_least_as_qualified_p (to, from))
7978 if (!at_least_as_qualified_p (from, to))
7982 to_more_cv_qualified = true;
7986 constp &= TYPE_READONLY (to);
7989 if (TREE_CODE (to) == VECTOR_TYPE)
7990 is_opaque_pointer = vector_targets_convertible_p (to, from);
7992 if (TREE_CODE (to) != POINTER_TYPE && !TYPE_PTRMEM_P (to))
7993 return ((constp >= 0 || to_more_cv_qualified)
7994 && (is_opaque_pointer
7995 || same_type_ignoring_top_level_qualifiers_p (to, from)));
7999 /* When comparing, say, char ** to char const **, this function takes
8000 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8001 types to this function. */
8004 comp_ptr_ttypes (tree to, tree from)
8006 return comp_ptr_ttypes_real (to, from, 1);
8009 /* Returns true iff FNTYPE is a non-class type that involves
8010 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8011 if a parameter type is ill-formed. */
8014 error_type_p (const_tree type)
8018 switch (TREE_CODE (type))
8024 case REFERENCE_TYPE:
8026 return error_type_p (TREE_TYPE (type));
8030 if (error_type_p (TREE_TYPE (type)))
8032 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8033 if (error_type_p (TREE_VALUE (t)))
8038 if (TYPE_PTRMEMFUNC_P (type))
8039 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8047 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
8048 type or inheritance-related types, regardless of cv-quals. */
8051 ptr_reasonably_similar (const_tree to, const_tree from)
8053 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8055 /* Any target type is similar enough to void. */
8056 if (TREE_CODE (to) == VOID_TYPE)
8057 return !error_type_p (from);
8058 if (TREE_CODE (from) == VOID_TYPE)
8059 return !error_type_p (to);
8061 if (TREE_CODE (to) != TREE_CODE (from))
8064 if (TREE_CODE (from) == OFFSET_TYPE
8065 && comptypes (TYPE_OFFSET_BASETYPE (to),
8066 TYPE_OFFSET_BASETYPE (from),
8067 COMPARE_BASE | COMPARE_DERIVED))
8070 if (TREE_CODE (to) == VECTOR_TYPE
8071 && vector_types_convertible_p (to, from, false))
8074 if (TREE_CODE (to) == INTEGER_TYPE
8075 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8078 if (TREE_CODE (to) == FUNCTION_TYPE)
8079 return !error_type_p (to) && !error_type_p (from);
8081 if (TREE_CODE (to) != POINTER_TYPE)
8083 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8084 COMPARE_BASE | COMPARE_DERIVED);
8088 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8089 pointer-to-member types) are the same, ignoring cv-qualification at
8093 comp_ptr_ttypes_const (tree to, tree from)
8095 bool is_opaque_pointer = false;
8097 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8099 if (TREE_CODE (to) != TREE_CODE (from))
8102 if (TREE_CODE (from) == OFFSET_TYPE
8103 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8104 TYPE_OFFSET_BASETYPE (to)))
8107 if (TREE_CODE (to) == VECTOR_TYPE)
8108 is_opaque_pointer = vector_targets_convertible_p (to, from);
8110 if (TREE_CODE (to) != POINTER_TYPE)
8111 return (is_opaque_pointer
8112 || same_type_ignoring_top_level_qualifiers_p (to, from));
8116 /* Returns the type qualifiers for this type, including the qualifiers on the
8117 elements for an array type. */
8120 cp_type_quals (const_tree type)
8123 /* This CONST_CAST is okay because strip_array_types returns its
8124 argument unmodified and we assign it to a const_tree. */
8125 type = strip_array_types (CONST_CAST_TREE (type));
8126 if (type == error_mark_node
8127 /* Quals on a FUNCTION_TYPE are memfn quals. */
8128 || TREE_CODE (type) == FUNCTION_TYPE)
8129 return TYPE_UNQUALIFIED;
8130 quals = TYPE_QUALS (type);
8131 /* METHOD and REFERENCE_TYPEs should never have quals. */
8132 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8133 && TREE_CODE (type) != REFERENCE_TYPE)
8134 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8135 == TYPE_UNQUALIFIED));
8139 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8143 type_memfn_quals (const_tree type)
8145 if (TREE_CODE (type) == FUNCTION_TYPE)
8146 return TYPE_QUALS (type);
8147 else if (TREE_CODE (type) == METHOD_TYPE)
8148 return cp_type_quals (class_of_this_parm (type));
8153 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8157 apply_memfn_quals (tree type, cp_cv_quals memfn_quals)
8159 /* Could handle METHOD_TYPE here if necessary. */
8160 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8161 if (TYPE_QUALS (type) == memfn_quals)
8163 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8165 return build_qualified_type (type, memfn_quals);
8168 /* Returns nonzero if TYPE is const or volatile. */
8171 cv_qualified_p (const_tree type)
8173 int quals = cp_type_quals (type);
8174 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8177 /* Returns nonzero if the TYPE contains a mutable member. */
8180 cp_has_mutable_p (const_tree type)
8182 /* This CONST_CAST is okay because strip_array_types returns its
8183 argument unmodified and we assign it to a const_tree. */
8184 type = strip_array_types (CONST_CAST_TREE(type));
8186 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
8189 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8190 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8191 approximation. In particular, consider:
8194 struct S { int i; };
8195 const S s = { f(); }
8197 Here, we will make "s" as TREE_READONLY (because it is declared
8198 "const") -- only to reverse ourselves upon seeing that the
8199 initializer is non-constant. */
8202 cp_apply_type_quals_to_decl (int type_quals, tree decl)
8204 tree type = TREE_TYPE (decl);
8206 if (type == error_mark_node)
8209 if (TREE_CODE (decl) == TYPE_DECL)
8212 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
8213 && type_quals != TYPE_UNQUALIFIED));
8215 /* Avoid setting TREE_READONLY incorrectly. */
8216 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8217 constructor can produce constant init, so rely on cp_finish_decl to
8218 clear TREE_READONLY if the variable has non-constant init. */
8220 /* If the type has a mutable component, that component might be
8222 if (TYPE_HAS_MUTABLE_P (type))
8223 type_quals &= ~TYPE_QUAL_CONST;
8225 c_apply_type_quals_to_decl (type_quals, decl);
8228 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8229 exemplar types such that casting T1 to T2 is casting away constness
8230 if and only if there is no implicit conversion from T1 to T2. */
8233 casts_away_constness_r (tree *t1, tree *t2)
8238 /* [expr.const.cast]
8240 For multi-level pointer to members and multi-level mixed pointers
8241 and pointers to members (conv.qual), the "member" aspect of a
8242 pointer to member level is ignored when determining if a const
8243 cv-qualifier has been cast away. */
8244 /* [expr.const.cast]
8246 For two pointer types:
8248 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8249 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8252 casting from X1 to X2 casts away constness if, for a non-pointer
8253 type T there does not exist an implicit conversion (clause
8256 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8260 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8261 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRMEM_P (*t1))
8262 || (!TYPE_PTR_P (*t2) && !TYPE_PTRMEM_P (*t2)))
8264 *t1 = cp_build_qualified_type (void_type_node,
8265 cp_type_quals (*t1));
8266 *t2 = cp_build_qualified_type (void_type_node,
8267 cp_type_quals (*t2));
8271 quals1 = cp_type_quals (*t1);
8272 quals2 = cp_type_quals (*t2);
8274 if (TYPE_PTRMEM_P (*t1))
8275 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
8277 *t1 = TREE_TYPE (*t1);
8278 if (TYPE_PTRMEM_P (*t2))
8279 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
8281 *t2 = TREE_TYPE (*t2);
8283 casts_away_constness_r (t1, t2);
8284 *t1 = build_pointer_type (*t1);
8285 *t2 = build_pointer_type (*t2);
8286 *t1 = cp_build_qualified_type (*t1, quals1);
8287 *t2 = cp_build_qualified_type (*t2, quals2);
8290 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8293 ??? This function returns non-zero if casting away qualifiers not
8294 just const. We would like to return to the caller exactly which
8295 qualifiers are casted away to give more accurate diagnostics.
8299 casts_away_constness (tree t1, tree t2)
8301 if (TREE_CODE (t2) == REFERENCE_TYPE)
8303 /* [expr.const.cast]
8305 Casting from an lvalue of type T1 to an lvalue of type T2
8306 using a reference cast casts away constness if a cast from an
8307 rvalue of type "pointer to T1" to the type "pointer to T2"
8308 casts away constness. */
8309 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
8310 return casts_away_constness (build_pointer_type (t1),
8311 build_pointer_type (TREE_TYPE (t2)));
8314 if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
8315 /* [expr.const.cast]
8317 Casting from an rvalue of type "pointer to data member of X
8318 of type T1" to the type "pointer to data member of Y of type
8319 T2" casts away constness if a cast from an rvalue of type
8320 "pointer to T1" to the type "pointer to T2" casts away
8322 return casts_away_constness
8323 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
8324 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)));
8326 /* Casting away constness is only something that makes sense for
8327 pointer or reference types. */
8328 if (TREE_CODE (t1) != POINTER_TYPE
8329 || TREE_CODE (t2) != POINTER_TYPE)
8332 /* Top-level qualifiers don't matter. */
8333 t1 = TYPE_MAIN_VARIANT (t1);
8334 t2 = TYPE_MAIN_VARIANT (t2);
8335 casts_away_constness_r (&t1, &t2);
8336 if (!can_convert (t2, t1))
8342 /* If T is a REFERENCE_TYPE return the type to which T refers.
8343 Otherwise, return T itself. */
8346 non_reference (tree t)
8348 if (t && TREE_CODE (t) == REFERENCE_TYPE)
8354 /* Return nonzero if REF is an lvalue valid for this language;
8355 otherwise, print an error message and return zero. USE says
8356 how the lvalue is being used and so selects the error message. */
8359 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
8361 cp_lvalue_kind kind = lvalue_kind (ref);
8363 if (kind == clk_none)
8365 if (complain & tf_error)
8366 lvalue_error (input_location, use);
8369 else if (kind & (clk_rvalueref|clk_class))
8371 if (!(complain & tf_error))
8373 if (kind & clk_class)
8374 /* Make this a permerror because we used to accept it. */
8375 permerror (input_location, "using temporary as lvalue");
8377 error ("using xvalue (rvalue reference) as lvalue");
8382 /* Return true if a user-defined literal operator is a raw operator. */
8385 check_raw_literal_operator (const_tree decl)
8387 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8390 bool maybe_raw_p = false;
8392 /* Count the number and type of arguments and check for ellipsis. */
8393 for (argtype = argtypes, arity = 0;
8394 argtype && argtype != void_list_node;
8395 ++arity, argtype = TREE_CHAIN (argtype))
8397 tree t = TREE_VALUE (argtype);
8399 if (same_type_p (t, const_string_type_node))
8403 return false; /* Found ellipsis. */
8405 if (!maybe_raw_p || arity != 1)
8412 /* Return true if a user-defined literal operator has one of the allowed
8416 check_literal_operator_args (const_tree decl,
8417 bool *long_long_unsigned_p, bool *long_double_p)
8419 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
8420 if (processing_template_decl)
8421 return (argtypes == NULL_TREE
8422 || same_type_p (TREE_VALUE (argtypes), void_type_node));
8428 bool found_string_p = false;
8429 bool maybe_raw_p = false;
8430 bool found_size_p = false;
8432 *long_long_unsigned_p = false;
8433 *long_double_p = false;
8435 /* Count the number and type of arguments and check for ellipsis. */
8436 for (argtype = argtypes, arity = 0;
8437 argtype && argtype != void_list_node;
8438 argtype = TREE_CHAIN (argtype))
8440 tree t = TREE_VALUE (argtype);
8443 if (TREE_CODE (t) == POINTER_TYPE)
8446 if (cp_type_quals (t) != TYPE_QUAL_CONST)
8448 t = TYPE_MAIN_VARIANT (t);
8449 if (same_type_p (t, char_type_node))
8451 found_string_p = true;
8454 else if (same_type_p (t, wchar_type_node))
8455 found_string_p = true;
8456 else if (same_type_p (t, char16_type_node))
8457 found_string_p = true;
8458 else if (same_type_p (t, char32_type_node))
8459 found_string_p = true;
8463 else if (same_type_p (t, size_type_node))
8465 if (!found_string_p)
8467 found_size_p = true;
8469 else if (same_type_p (t, long_long_unsigned_type_node))
8472 *long_long_unsigned_p = true;
8474 else if (same_type_p (t, long_double_type_node))
8477 *long_double_p = true;
8479 else if (same_type_p (t, char_type_node))
8481 else if (same_type_p (t, wchar_type_node))
8483 else if (same_type_p (t, char16_type_node))
8485 else if (same_type_p (t, char32_type_node))
8491 return false; /* Found ellipsis. */
8493 if (arity > max_arity)
8496 if (found_string_p && !maybe_raw_p && !found_size_p)