re PR c++/87080 (ice in cp_get_fndecl_from_callee, at cp/cvt.c:965)
[official-gcc.git] / gcc / cp / typeck.c
blob24647e29a5547bb4ea8b299dd25ed7604113c51e
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "target.h"
31 #include "cp-tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "intl.h"
35 #include "convert.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
38 #include "params.h"
39 #include "gcc-rich-location.h"
40 #include "stringpool.h"
41 #include "attribs.h"
42 #include "asan.h"
44 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46 static tree pfn_from_ptrmemfunc (tree);
47 static tree delta_from_ptrmemfunc (tree);
48 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 tsubst_flags_t, int);
50 static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 tsubst_flags_t);
52 static tree rationalize_conditional_expr (enum tree_code, tree,
53 tsubst_flags_t);
54 static int comp_ptr_ttypes_real (tree, tree, int);
55 static bool comp_except_types (tree, tree, bool);
56 static bool comp_array_types (const_tree, const_tree, bool);
57 static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 static bool maybe_warn_about_returning_address_of_local (tree);
62 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
63 static void error_args_num (location_t, tree, bool);
64 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
65 tsubst_flags_t);
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.)
69 Returns error_mark_node if the VALUE does not have
70 complete type when this function returns. */
72 tree
73 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
75 tree type;
77 if (processing_template_decl || value == error_mark_node)
78 return value;
80 if (TREE_CODE (value) == OVERLOAD)
81 type = unknown_type_node;
82 else
83 type = TREE_TYPE (value);
85 if (type == error_mark_node)
86 return error_mark_node;
88 /* First, detect a valid value with a complete type. */
89 if (COMPLETE_TYPE_P (type))
90 return value;
92 if (complete_type_or_maybe_complain (type, value, complain))
93 return value;
94 else
95 return error_mark_node;
98 tree
99 require_complete_type (tree value)
101 return require_complete_type_sfinae (value, tf_warning_or_error);
104 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
105 a template instantiation, do the instantiation. Returns TYPE,
106 whether or not it could be completed, unless something goes
107 horribly wrong, in which case the error_mark_node is returned. */
109 tree
110 complete_type (tree type)
112 if (type == NULL_TREE)
113 /* Rather than crash, we return something sure to cause an error
114 at some point. */
115 return error_mark_node;
117 if (type == error_mark_node || COMPLETE_TYPE_P (type))
119 else if (TREE_CODE (type) == ARRAY_TYPE)
121 tree t = complete_type (TREE_TYPE (type));
122 unsigned int needs_constructing, has_nontrivial_dtor;
123 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
124 layout_type (type);
125 needs_constructing
126 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
127 has_nontrivial_dtor
128 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
129 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
131 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
132 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
135 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
136 instantiate_class_template (TYPE_MAIN_VARIANT (type));
138 return type;
141 /* Like complete_type, but issue an error if the TYPE cannot be completed.
142 VALUE is used for informative diagnostics.
143 Returns NULL_TREE if the type cannot be made complete. */
145 tree
146 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
148 type = complete_type (type);
149 if (type == error_mark_node)
150 /* We already issued an error. */
151 return NULL_TREE;
152 else if (!COMPLETE_TYPE_P (type))
154 if (complain & tf_error)
155 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
156 return NULL_TREE;
158 else
159 return type;
162 tree
163 complete_type_or_else (tree type, tree value)
165 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
169 /* Return the common type of two parameter lists.
170 We assume that comptypes has already been done and returned 1;
171 if that isn't so, this may crash.
173 As an optimization, free the space we allocate if the parameter
174 lists are already common. */
176 static tree
177 commonparms (tree p1, tree p2)
179 tree oldargs = p1, newargs, n;
180 int i, len;
181 int any_change = 0;
183 len = list_length (p1);
184 newargs = tree_last (p1);
186 if (newargs == void_list_node)
187 i = 1;
188 else
190 i = 0;
191 newargs = 0;
194 for (; i < len; i++)
195 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
197 n = newargs;
199 for (i = 0; p1;
200 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
202 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
204 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
205 any_change = 1;
207 else if (! TREE_PURPOSE (p1))
209 if (TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
212 any_change = 1;
215 else
217 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
218 any_change = 1;
219 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 if (TREE_VALUE (p1) != TREE_VALUE (p2))
223 any_change = 1;
224 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
226 else
227 TREE_VALUE (n) = TREE_VALUE (p1);
229 if (! any_change)
230 return oldargs;
232 return newargs;
235 /* Given a type, perhaps copied for a typedef,
236 find the "original" version of it. */
237 static tree
238 original_type (tree t)
240 int quals = cp_type_quals (t);
241 while (t != error_mark_node
242 && TYPE_NAME (t) != NULL_TREE)
244 tree x = TYPE_NAME (t);
245 if (TREE_CODE (x) != TYPE_DECL)
246 break;
247 x = DECL_ORIGINAL_TYPE (x);
248 if (x == NULL_TREE)
249 break;
250 t = x;
252 return cp_build_qualified_type (t, quals);
255 /* Return the common type for two arithmetic types T1 and T2 under the
256 usual arithmetic conversions. The default conversions have already
257 been applied, and enumerated types converted to their compatible
258 integer types. */
260 static tree
261 cp_common_type (tree t1, tree t2)
263 enum tree_code code1 = TREE_CODE (t1);
264 enum tree_code code2 = TREE_CODE (t2);
265 tree attributes;
266 int i;
269 /* In what follows, we slightly generalize the rules given in [expr] so
270 as to deal with `long long' and `complex'. First, merge the
271 attributes. */
272 attributes = (*targetm.merge_type_attributes) (t1, t2);
274 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
276 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
277 return build_type_attribute_variant (t1, attributes);
278 else
279 return NULL_TREE;
282 /* FIXME: Attributes. */
283 gcc_assert (ARITHMETIC_TYPE_P (t1)
284 || VECTOR_TYPE_P (t1)
285 || UNSCOPED_ENUM_P (t1));
286 gcc_assert (ARITHMETIC_TYPE_P (t2)
287 || VECTOR_TYPE_P (t2)
288 || UNSCOPED_ENUM_P (t2));
290 /* If one type is complex, form the common type of the non-complex
291 components, then make that complex. Use T1 or T2 if it is the
292 required type. */
293 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
295 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
296 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
297 tree subtype
298 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
300 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
301 return build_type_attribute_variant (t1, attributes);
302 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
303 return build_type_attribute_variant (t2, attributes);
304 else
305 return build_type_attribute_variant (build_complex_type (subtype),
306 attributes);
309 if (code1 == VECTOR_TYPE)
311 /* When we get here we should have two vectors of the same size.
312 Just prefer the unsigned one if present. */
313 if (TYPE_UNSIGNED (t1))
314 return build_type_attribute_variant (t1, attributes);
315 else
316 return build_type_attribute_variant (t2, attributes);
319 /* If only one is real, use it as the result. */
320 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
321 return build_type_attribute_variant (t1, attributes);
322 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
323 return build_type_attribute_variant (t2, attributes);
325 /* Both real or both integers; use the one with greater precision. */
326 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
327 return build_type_attribute_variant (t1, attributes);
328 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
329 return build_type_attribute_variant (t2, attributes);
331 /* The types are the same; no need to do anything fancy. */
332 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
333 return build_type_attribute_variant (t1, attributes);
335 if (code1 != REAL_TYPE)
337 /* If one is unsigned long long, then convert the other to unsigned
338 long long. */
339 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
340 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
341 return build_type_attribute_variant (long_long_unsigned_type_node,
342 attributes);
343 /* If one is a long long, and the other is an unsigned long, and
344 long long can represent all the values of an unsigned long, then
345 convert to a long long. Otherwise, convert to an unsigned long
346 long. Otherwise, if either operand is long long, convert the
347 other to long long.
349 Since we're here, we know the TYPE_PRECISION is the same;
350 therefore converting to long long cannot represent all the values
351 of an unsigned long, so we choose unsigned long long in that
352 case. */
353 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
354 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
356 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
357 ? long_long_unsigned_type_node
358 : long_long_integer_type_node);
359 return build_type_attribute_variant (t, attributes);
362 /* Go through the same procedure, but for longs. */
363 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
364 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
365 return build_type_attribute_variant (long_unsigned_type_node,
366 attributes);
367 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
368 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
370 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
371 ? long_unsigned_type_node : long_integer_type_node);
372 return build_type_attribute_variant (t, attributes);
375 /* For __intN types, either the type is __int128 (and is lower
376 priority than the types checked above, but higher than other
377 128-bit types) or it's known to not be the same size as other
378 types (enforced in toplev.c). Prefer the unsigned type. */
379 for (i = 0; i < NUM_INT_N_ENTS; i ++)
381 if (int_n_enabled_p [i]
382 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
383 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
384 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? int_n_trees[i].unsigned_type
389 : int_n_trees[i].signed_type);
390 return build_type_attribute_variant (t, attributes);
394 /* Otherwise prefer the unsigned one. */
395 if (TYPE_UNSIGNED (t1))
396 return build_type_attribute_variant (t1, attributes);
397 else
398 return build_type_attribute_variant (t2, attributes);
400 else
402 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
403 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
404 return build_type_attribute_variant (long_double_type_node,
405 attributes);
406 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
407 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
408 return build_type_attribute_variant (double_type_node,
409 attributes);
410 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
411 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
412 return build_type_attribute_variant (float_type_node,
413 attributes);
415 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
416 the standard C++ floating-point types. Logic earlier in this
417 function has already eliminated the possibility that
418 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
419 compelling reason to choose one or the other. */
420 return build_type_attribute_variant (t1, attributes);
424 /* T1 and T2 are arithmetic or enumeration types. Return the type
425 that will result from the "usual arithmetic conversions" on T1 and
426 T2 as described in [expr]. */
428 tree
429 type_after_usual_arithmetic_conversions (tree t1, tree t2)
431 gcc_assert (ARITHMETIC_TYPE_P (t1)
432 || VECTOR_TYPE_P (t1)
433 || UNSCOPED_ENUM_P (t1));
434 gcc_assert (ARITHMETIC_TYPE_P (t2)
435 || VECTOR_TYPE_P (t2)
436 || UNSCOPED_ENUM_P (t2));
438 /* Perform the integral promotions. We do not promote real types here. */
439 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
440 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
442 t1 = type_promotes_to (t1);
443 t2 = type_promotes_to (t2);
446 return cp_common_type (t1, t2);
449 static void
450 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
451 composite_pointer_operation operation)
453 switch (operation)
455 case CPO_COMPARISON:
456 emit_diagnostic (kind, input_location, 0,
457 "comparison between "
458 "distinct pointer types %qT and %qT lacks a cast",
459 t1, t2);
460 break;
461 case CPO_CONVERSION:
462 emit_diagnostic (kind, input_location, 0,
463 "conversion between "
464 "distinct pointer types %qT and %qT lacks a cast",
465 t1, t2);
466 break;
467 case CPO_CONDITIONAL_EXPR:
468 emit_diagnostic (kind, input_location, 0,
469 "conditional expression between "
470 "distinct pointer types %qT and %qT lacks a cast",
471 t1, t2);
472 break;
473 default:
474 gcc_unreachable ();
478 /* Subroutine of composite_pointer_type to implement the recursive
479 case. See that function for documentation of the parameters. */
481 static tree
482 composite_pointer_type_r (tree t1, tree t2,
483 composite_pointer_operation operation,
484 tsubst_flags_t complain)
486 tree pointee1;
487 tree pointee2;
488 tree result_type;
489 tree attributes;
491 /* Determine the types pointed to by T1 and T2. */
492 if (TYPE_PTR_P (t1))
494 pointee1 = TREE_TYPE (t1);
495 pointee2 = TREE_TYPE (t2);
497 else
499 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
500 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
503 /* [expr.rel]
505 Otherwise, the composite pointer type is a pointer type
506 similar (_conv.qual_) to the type of one of the operands,
507 with a cv-qualification signature (_conv.qual_) that is the
508 union of the cv-qualification signatures of the operand
509 types. */
510 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
511 result_type = pointee1;
512 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
513 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
515 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
516 complain);
517 if (result_type == error_mark_node)
518 return error_mark_node;
520 else
522 if (complain & tf_error)
523 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
524 else
525 return error_mark_node;
526 result_type = void_type_node;
528 result_type = cp_build_qualified_type (result_type,
529 (cp_type_quals (pointee1)
530 | cp_type_quals (pointee2)));
531 /* If the original types were pointers to members, so is the
532 result. */
533 if (TYPE_PTRMEM_P (t1))
535 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
536 TYPE_PTRMEM_CLASS_TYPE (t2)))
538 if (complain & tf_error)
539 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
540 else
541 return error_mark_node;
543 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
544 result_type);
546 else
547 result_type = build_pointer_type (result_type);
549 /* Merge the attributes. */
550 attributes = (*targetm.merge_type_attributes) (t1, t2);
551 return build_type_attribute_variant (result_type, attributes);
554 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
555 ARG1 and ARG2 are the values with those types. The OPERATION is to
556 describe the operation between the pointer types,
557 in case an error occurs.
559 This routine also implements the computation of a common type for
560 pointers-to-members as per [expr.eq]. */
562 tree
563 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
564 composite_pointer_operation operation,
565 tsubst_flags_t complain)
567 tree class1;
568 tree class2;
570 /* [expr.rel]
572 If one operand is a null pointer constant, the composite pointer
573 type is the type of the other operand. */
574 if (null_ptr_cst_p (arg1))
575 return t2;
576 if (null_ptr_cst_p (arg2))
577 return t1;
579 /* We have:
581 [expr.rel]
583 If one of the operands has type "pointer to cv1 void*", then
584 the other has type "pointer to cv2T", and the composite pointer
585 type is "pointer to cv12 void", where cv12 is the union of cv1
586 and cv2.
588 If either type is a pointer to void, make sure it is T1. */
589 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
590 std::swap (t1, t2);
592 /* Now, if T1 is a pointer to void, merge the qualifiers. */
593 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
595 tree attributes;
596 tree result_type;
598 if (TYPE_PTRFN_P (t2))
600 if (complain & tf_error)
602 switch (operation)
604 case CPO_COMPARISON:
605 pedwarn (input_location, OPT_Wpedantic,
606 "ISO C++ forbids comparison between pointer "
607 "of type %<void *%> and pointer-to-function");
608 break;
609 case CPO_CONVERSION:
610 pedwarn (input_location, OPT_Wpedantic,
611 "ISO C++ forbids conversion between pointer "
612 "of type %<void *%> and pointer-to-function");
613 break;
614 case CPO_CONDITIONAL_EXPR:
615 pedwarn (input_location, OPT_Wpedantic,
616 "ISO C++ forbids conditional expression between "
617 "pointer of type %<void *%> and "
618 "pointer-to-function");
619 break;
620 default:
621 gcc_unreachable ();
624 else
625 return error_mark_node;
627 result_type
628 = cp_build_qualified_type (void_type_node,
629 (cp_type_quals (TREE_TYPE (t1))
630 | cp_type_quals (TREE_TYPE (t2))));
631 result_type = build_pointer_type (result_type);
632 /* Merge the attributes. */
633 attributes = (*targetm.merge_type_attributes) (t1, t2);
634 return build_type_attribute_variant (result_type, attributes);
637 if (c_dialect_objc () && TYPE_PTR_P (t1)
638 && TYPE_PTR_P (t2))
640 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
641 return objc_common_type (t1, t2);
644 /* if T1 or T2 is "pointer to noexcept function" and the other type is
645 "pointer to function", where the function types are otherwise the same,
646 "pointer to function" */
647 if (fnptr_conv_p (t1, t2))
648 return t1;
649 if (fnptr_conv_p (t2, t1))
650 return t2;
652 /* [expr.eq] permits the application of a pointer conversion to
653 bring the pointers to a common type. */
654 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
655 && CLASS_TYPE_P (TREE_TYPE (t1))
656 && CLASS_TYPE_P (TREE_TYPE (t2))
657 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
658 TREE_TYPE (t2)))
660 class1 = TREE_TYPE (t1);
661 class2 = TREE_TYPE (t2);
663 if (DERIVED_FROM_P (class1, class2))
664 t2 = (build_pointer_type
665 (cp_build_qualified_type (class1, cp_type_quals (class2))));
666 else if (DERIVED_FROM_P (class2, class1))
667 t1 = (build_pointer_type
668 (cp_build_qualified_type (class2, cp_type_quals (class1))));
669 else
671 if (complain & tf_error)
672 composite_pointer_error (DK_ERROR, t1, t2, operation);
673 return error_mark_node;
676 /* [expr.eq] permits the application of a pointer-to-member
677 conversion to change the class type of one of the types. */
678 else if (TYPE_PTRMEM_P (t1)
679 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
680 TYPE_PTRMEM_CLASS_TYPE (t2)))
682 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
683 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
685 if (DERIVED_FROM_P (class1, class2))
686 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
687 else if (DERIVED_FROM_P (class2, class1))
688 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
689 else
691 if (complain & tf_error)
692 switch (operation)
694 case CPO_COMPARISON:
695 error ("comparison between distinct "
696 "pointer-to-member types %qT and %qT lacks a cast",
697 t1, t2);
698 break;
699 case CPO_CONVERSION:
700 error ("conversion between distinct "
701 "pointer-to-member types %qT and %qT lacks a cast",
702 t1, t2);
703 break;
704 case CPO_CONDITIONAL_EXPR:
705 error ("conditional expression between distinct "
706 "pointer-to-member types %qT and %qT lacks a cast",
707 t1, t2);
708 break;
709 default:
710 gcc_unreachable ();
712 return error_mark_node;
716 return composite_pointer_type_r (t1, t2, operation, complain);
719 /* Return the merged type of two types.
720 We assume that comptypes has already been done and returned 1;
721 if that isn't so, this may crash.
723 This just combines attributes and default arguments; any other
724 differences would cause the two types to compare unalike. */
726 tree
727 merge_types (tree t1, tree t2)
729 enum tree_code code1;
730 enum tree_code code2;
731 tree attributes;
733 /* Save time if the two types are the same. */
734 if (t1 == t2)
735 return t1;
736 if (original_type (t1) == original_type (t2))
737 return t1;
739 /* If one type is nonsense, use the other. */
740 if (t1 == error_mark_node)
741 return t2;
742 if (t2 == error_mark_node)
743 return t1;
745 /* Handle merging an auto redeclaration with a previous deduced
746 return type. */
747 if (is_auto (t1))
748 return t2;
750 /* Merge the attributes. */
751 attributes = (*targetm.merge_type_attributes) (t1, t2);
753 if (TYPE_PTRMEMFUNC_P (t1))
754 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
755 if (TYPE_PTRMEMFUNC_P (t2))
756 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
758 code1 = TREE_CODE (t1);
759 code2 = TREE_CODE (t2);
760 if (code1 != code2)
762 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
763 if (code1 == TYPENAME_TYPE)
765 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
766 code1 = TREE_CODE (t1);
768 else
770 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
771 code2 = TREE_CODE (t2);
775 switch (code1)
777 case POINTER_TYPE:
778 case REFERENCE_TYPE:
779 /* For two pointers, do this recursively on the target type. */
781 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
782 int quals = cp_type_quals (t1);
784 if (code1 == POINTER_TYPE)
786 t1 = build_pointer_type (target);
787 if (TREE_CODE (target) == METHOD_TYPE)
788 t1 = build_ptrmemfunc_type (t1);
790 else
791 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
792 t1 = build_type_attribute_variant (t1, attributes);
793 t1 = cp_build_qualified_type (t1, quals);
795 return t1;
798 case OFFSET_TYPE:
800 int quals;
801 tree pointee;
802 quals = cp_type_quals (t1);
803 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
804 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
805 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
806 pointee);
807 t1 = cp_build_qualified_type (t1, quals);
808 break;
811 case ARRAY_TYPE:
813 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
814 /* Save space: see if the result is identical to one of the args. */
815 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
816 return build_type_attribute_variant (t1, attributes);
817 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
818 return build_type_attribute_variant (t2, attributes);
819 /* Merge the element types, and have a size if either arg has one. */
820 t1 = build_cplus_array_type
821 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
822 break;
825 case FUNCTION_TYPE:
826 /* Function types: prefer the one that specified arg types.
827 If both do, merge the arg types. Also merge the return types. */
829 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
830 tree p1 = TYPE_ARG_TYPES (t1);
831 tree p2 = TYPE_ARG_TYPES (t2);
832 tree parms;
834 /* Save space: see if the result is identical to one of the args. */
835 if (valtype == TREE_TYPE (t1) && ! p2)
836 return cp_build_type_attribute_variant (t1, attributes);
837 if (valtype == TREE_TYPE (t2) && ! p1)
838 return cp_build_type_attribute_variant (t2, attributes);
840 /* Simple way if one arg fails to specify argument types. */
841 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
842 parms = p2;
843 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
844 parms = p1;
845 else
846 parms = commonparms (p1, p2);
848 cp_cv_quals quals = type_memfn_quals (t1);
849 cp_ref_qualifier rqual = type_memfn_rqual (t1);
850 gcc_assert (quals == type_memfn_quals (t2));
851 gcc_assert (rqual == type_memfn_rqual (t2));
853 tree rval = build_function_type (valtype, parms);
854 rval = apply_memfn_quals (rval, quals);
855 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
856 TYPE_RAISES_EXCEPTIONS (t2));
857 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
858 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
859 break;
862 case METHOD_TYPE:
864 /* Get this value the long way, since TYPE_METHOD_BASETYPE
865 is just the main variant of this. */
866 tree basetype = class_of_this_parm (t2);
867 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
868 TYPE_RAISES_EXCEPTIONS (t2));
869 cp_ref_qualifier rqual = type_memfn_rqual (t1);
870 tree t3;
871 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
873 /* If this was a member function type, get back to the
874 original type of type member function (i.e., without
875 the class instance variable up front. */
876 t1 = build_function_type (TREE_TYPE (t1),
877 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
878 t2 = build_function_type (TREE_TYPE (t2),
879 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
880 t3 = merge_types (t1, t2);
881 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
882 TYPE_ARG_TYPES (t3));
883 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
884 break;
887 case TYPENAME_TYPE:
888 /* There is no need to merge attributes into a TYPENAME_TYPE.
889 When the type is instantiated it will have whatever
890 attributes result from the instantiation. */
891 return t1;
893 default:;
894 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
895 return t1;
896 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
897 return t2;
898 break;
901 return cp_build_type_attribute_variant (t1, attributes);
904 /* Return the ARRAY_TYPE type without its domain. */
906 tree
907 strip_array_domain (tree type)
909 tree t2;
910 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
911 if (TYPE_DOMAIN (type) == NULL_TREE)
912 return type;
913 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
914 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
917 /* Wrapper around cp_common_type that is used by c-common.c and other
918 front end optimizations that remove promotions.
920 Return the common type for two arithmetic types T1 and T2 under the
921 usual arithmetic conversions. The default conversions have already
922 been applied, and enumerated types converted to their compatible
923 integer types. */
925 tree
926 common_type (tree t1, tree t2)
928 /* If one type is nonsense, use the other */
929 if (t1 == error_mark_node)
930 return t2;
931 if (t2 == error_mark_node)
932 return t1;
934 return cp_common_type (t1, t2);
937 /* Return the common type of two pointer types T1 and T2. This is the
938 type for the result of most arithmetic operations if the operands
939 have the given two types.
941 We assume that comp_target_types has already been done and returned
942 nonzero; if that isn't so, this may crash. */
944 tree
945 common_pointer_type (tree t1, tree t2)
947 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
948 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
949 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
951 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
952 CPO_CONVERSION, tf_warning_or_error);
955 /* Compare two exception specifier types for exactness or subsetness, if
956 allowed. Returns false for mismatch, true for match (same, or
957 derived and !exact).
959 [except.spec] "If a class X ... objects of class X or any class publicly
960 and unambiguously derived from X. Similarly, if a pointer type Y * ...
961 exceptions of type Y * or that are pointers to any type publicly and
962 unambiguously derived from Y. Otherwise a function only allows exceptions
963 that have the same type ..."
964 This does not mention cv qualifiers and is different to what throw
965 [except.throw] and catch [except.catch] will do. They will ignore the
966 top level cv qualifiers, and allow qualifiers in the pointer to class
967 example.
969 We implement the letter of the standard. */
971 static bool
972 comp_except_types (tree a, tree b, bool exact)
974 if (same_type_p (a, b))
975 return true;
976 else if (!exact)
978 if (cp_type_quals (a) || cp_type_quals (b))
979 return false;
981 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
983 a = TREE_TYPE (a);
984 b = TREE_TYPE (b);
985 if (cp_type_quals (a) || cp_type_quals (b))
986 return false;
989 if (TREE_CODE (a) != RECORD_TYPE
990 || TREE_CODE (b) != RECORD_TYPE)
991 return false;
993 if (publicly_uniquely_derived_p (a, b))
994 return true;
996 return false;
999 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1000 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1001 If EXACT is ce_type, the C++17 type compatibility rules apply.
1002 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1003 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1004 are unordered, but we've already filtered out duplicates. Most lists will
1005 be in order, we should try to make use of that. */
1007 bool
1008 comp_except_specs (const_tree t1, const_tree t2, int exact)
1010 const_tree probe;
1011 const_tree base;
1012 int length = 0;
1014 if (t1 == t2)
1015 return true;
1017 /* First handle noexcept. */
1018 if (exact < ce_exact)
1020 if (exact == ce_type
1021 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1022 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1023 return true;
1025 /* noexcept(false) is compatible with no exception-specification,
1026 and less strict than any spec. */
1027 if (t1 == noexcept_false_spec)
1028 return t2 == NULL_TREE || exact == ce_derived;
1029 /* Even a derived noexcept(false) is compatible with no
1030 exception-specification. */
1031 if (t2 == noexcept_false_spec)
1032 return t1 == NULL_TREE;
1034 /* Otherwise, if we aren't looking for an exact match, noexcept is
1035 equivalent to throw(). */
1036 if (t1 == noexcept_true_spec)
1037 t1 = empty_except_spec;
1038 if (t2 == noexcept_true_spec)
1039 t2 = empty_except_spec;
1042 /* If any noexcept is left, it is only comparable to itself;
1043 either we're looking for an exact match or we're redeclaring a
1044 template with dependent noexcept. */
1045 if ((t1 && TREE_PURPOSE (t1))
1046 || (t2 && TREE_PURPOSE (t2)))
1047 return (t1 && t2
1048 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1050 if (t1 == NULL_TREE) /* T1 is ... */
1051 return t2 == NULL_TREE || exact == ce_derived;
1052 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1053 return t2 != NULL_TREE && !TREE_VALUE (t2);
1054 if (t2 == NULL_TREE) /* T2 is ... */
1055 return false;
1056 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1057 return exact == ce_derived;
1059 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1060 Count how many we find, to determine exactness. For exact matching and
1061 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1062 O(nm). */
1063 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1065 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1067 tree a = TREE_VALUE (probe);
1068 tree b = TREE_VALUE (t2);
1070 if (comp_except_types (a, b, exact))
1072 if (probe == base && exact > ce_derived)
1073 base = TREE_CHAIN (probe);
1074 length++;
1075 break;
1078 if (probe == NULL_TREE)
1079 return false;
1081 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1084 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1085 [] can match [size]. */
1087 static bool
1088 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1090 tree d1;
1091 tree d2;
1092 tree max1, max2;
1094 if (t1 == t2)
1095 return true;
1097 /* The type of the array elements must be the same. */
1098 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1099 return false;
1101 d1 = TYPE_DOMAIN (t1);
1102 d2 = TYPE_DOMAIN (t2);
1104 if (d1 == d2)
1105 return true;
1107 /* If one of the arrays is dimensionless, and the other has a
1108 dimension, they are of different types. However, it is valid to
1109 write:
1111 extern int a[];
1112 int a[3];
1114 by [basic.link]:
1116 declarations for an array object can specify
1117 array types that differ by the presence or absence of a major
1118 array bound (_dcl.array_). */
1119 if (!d1 || !d2)
1120 return allow_redeclaration;
1122 /* Check that the dimensions are the same. */
1124 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1125 return false;
1126 max1 = TYPE_MAX_VALUE (d1);
1127 max2 = TYPE_MAX_VALUE (d2);
1129 if (!cp_tree_equal (max1, max2))
1130 return false;
1132 return true;
1135 /* Compare the relative position of T1 and T2 into their respective
1136 template parameter list.
1137 T1 and T2 must be template parameter types.
1138 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1140 static bool
1141 comp_template_parms_position (tree t1, tree t2)
1143 tree index1, index2;
1144 gcc_assert (t1 && t2
1145 && TREE_CODE (t1) == TREE_CODE (t2)
1146 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1147 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1148 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1150 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1151 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1153 /* Then compare their relative position. */
1154 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1155 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1156 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1157 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1158 return false;
1160 /* In C++14 we can end up comparing 'auto' to a normal template
1161 parameter. Don't confuse them. */
1162 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1163 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1165 return true;
1168 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1170 static bool
1171 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1173 t1 = TYPE_MAIN_VARIANT (t1);
1174 t2 = TYPE_MAIN_VARIANT (t2);
1176 if (TYPE_PTR_P (t1)
1177 && TYPE_PTR_P (t2))
1178 return true;
1180 /* The signedness of the parameter matters only when an integral
1181 type smaller than int is promoted to int, otherwise only the
1182 precision of the parameter matters.
1183 This check should make sure that the callee does not see
1184 undefined values in argument registers. */
1185 if (INTEGRAL_TYPE_P (t1)
1186 && INTEGRAL_TYPE_P (t2)
1187 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1188 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1189 || !targetm.calls.promote_prototypes (NULL_TREE)
1190 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1191 return true;
1193 return same_type_p (t1, t2);
1196 /* Check if a type cast between two function types can be considered safe. */
1198 static bool
1199 cxx_safe_function_type_cast_p (tree t1, tree t2)
1201 if (TREE_TYPE (t1) == void_type_node &&
1202 TYPE_ARG_TYPES (t1) == void_list_node)
1203 return true;
1205 if (TREE_TYPE (t2) == void_type_node &&
1206 TYPE_ARG_TYPES (t2) == void_list_node)
1207 return true;
1209 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1210 return false;
1212 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1213 t1 && t2;
1214 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1215 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1216 return false;
1218 return true;
1221 /* Subroutine in comptypes. */
1223 static bool
1224 structural_comptypes (tree t1, tree t2, int strict)
1226 if (t1 == t2)
1227 return true;
1229 /* Suppress errors caused by previously reported errors. */
1230 if (t1 == error_mark_node || t2 == error_mark_node)
1231 return false;
1233 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1235 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1236 current instantiation. */
1237 if (TREE_CODE (t1) == TYPENAME_TYPE)
1238 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1240 if (TREE_CODE (t2) == TYPENAME_TYPE)
1241 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1243 if (TYPE_PTRMEMFUNC_P (t1))
1244 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1245 if (TYPE_PTRMEMFUNC_P (t2))
1246 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1248 /* Different classes of types can't be compatible. */
1249 if (TREE_CODE (t1) != TREE_CODE (t2))
1250 return false;
1252 /* Qualifiers must match. For array types, we will check when we
1253 recur on the array element types. */
1254 if (TREE_CODE (t1) != ARRAY_TYPE
1255 && cp_type_quals (t1) != cp_type_quals (t2))
1256 return false;
1257 if (TREE_CODE (t1) == FUNCTION_TYPE
1258 && type_memfn_quals (t1) != type_memfn_quals (t2))
1259 return false;
1260 /* Need to check this before TYPE_MAIN_VARIANT.
1261 FIXME function qualifiers should really change the main variant. */
1262 if (TREE_CODE (t1) == FUNCTION_TYPE
1263 || TREE_CODE (t1) == METHOD_TYPE)
1265 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1266 return false;
1267 if (flag_noexcept_type
1268 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1269 TYPE_RAISES_EXCEPTIONS (t2),
1270 ce_type))
1271 return false;
1274 /* Allow for two different type nodes which have essentially the same
1275 definition. Note that we already checked for equality of the type
1276 qualifiers (just above). */
1278 if (TREE_CODE (t1) != ARRAY_TYPE
1279 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1280 return true;
1283 /* Compare the types. Break out if they could be the same. */
1284 switch (TREE_CODE (t1))
1286 case VOID_TYPE:
1287 case BOOLEAN_TYPE:
1288 /* All void and bool types are the same. */
1289 break;
1291 case INTEGER_TYPE:
1292 case FIXED_POINT_TYPE:
1293 case REAL_TYPE:
1294 /* With these nodes, we can't determine type equivalence by
1295 looking at what is stored in the nodes themselves, because
1296 two nodes might have different TYPE_MAIN_VARIANTs but still
1297 represent the same type. For example, wchar_t and int could
1298 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1299 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1300 and are distinct types. On the other hand, int and the
1301 following typedef
1303 typedef int INT __attribute((may_alias));
1305 have identical properties, different TYPE_MAIN_VARIANTs, but
1306 represent the same type. The canonical type system keeps
1307 track of equivalence in this case, so we fall back on it. */
1308 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1310 case TEMPLATE_TEMPLATE_PARM:
1311 case BOUND_TEMPLATE_TEMPLATE_PARM:
1312 if (!comp_template_parms_position (t1, t2))
1313 return false;
1314 if (!comp_template_parms
1315 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1316 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1317 return false;
1318 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1319 break;
1320 /* Don't check inheritance. */
1321 strict = COMPARE_STRICT;
1322 /* Fall through. */
1324 case RECORD_TYPE:
1325 case UNION_TYPE:
1326 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1327 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1328 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1329 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1330 break;
1332 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1333 break;
1334 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1335 break;
1337 return false;
1339 case OFFSET_TYPE:
1340 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1341 strict & ~COMPARE_REDECLARATION))
1342 return false;
1343 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1344 return false;
1345 break;
1347 case REFERENCE_TYPE:
1348 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1349 return false;
1350 /* fall through to checks for pointer types */
1351 gcc_fallthrough ();
1353 case POINTER_TYPE:
1354 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1355 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1356 return false;
1357 break;
1359 case METHOD_TYPE:
1360 case FUNCTION_TYPE:
1361 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1362 return false;
1363 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1364 return false;
1365 break;
1367 case ARRAY_TYPE:
1368 /* Target types must match incl. qualifiers. */
1369 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1370 return false;
1371 break;
1373 case TEMPLATE_TYPE_PARM:
1374 /* If T1 and T2 don't have the same relative position in their
1375 template parameters set, they can't be equal. */
1376 if (!comp_template_parms_position (t1, t2))
1377 return false;
1378 /* If T1 and T2 don't represent the same class template deduction,
1379 they aren't equal. */
1380 if (CLASS_PLACEHOLDER_TEMPLATE (t1)
1381 != CLASS_PLACEHOLDER_TEMPLATE (t2))
1382 return false;
1383 /* Constrained 'auto's are distinct from parms that don't have the same
1384 constraints. */
1385 if (!equivalent_placeholder_constraints (t1, t2))
1386 return false;
1387 break;
1389 case TYPENAME_TYPE:
1390 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1391 TYPENAME_TYPE_FULLNAME (t2)))
1392 return false;
1393 /* Qualifiers don't matter on scopes. */
1394 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1395 TYPE_CONTEXT (t2)))
1396 return false;
1397 break;
1399 case UNBOUND_CLASS_TEMPLATE:
1400 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1401 return false;
1402 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1403 return false;
1404 break;
1406 case COMPLEX_TYPE:
1407 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1408 return false;
1409 break;
1411 case VECTOR_TYPE:
1412 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1413 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1414 return false;
1415 break;
1417 case TYPE_PACK_EXPANSION:
1418 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1419 PACK_EXPANSION_PATTERN (t2))
1420 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1421 PACK_EXPANSION_EXTRA_ARGS (t2)));
1423 case DECLTYPE_TYPE:
1424 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1425 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1426 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1427 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1428 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1429 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1430 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1431 DECLTYPE_TYPE_EXPR (t2)))
1432 return false;
1433 break;
1435 case UNDERLYING_TYPE:
1436 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1437 UNDERLYING_TYPE_TYPE (t2));
1439 default:
1440 return false;
1443 /* If we get here, we know that from a target independent POV the
1444 types are the same. Make sure the target attributes are also
1445 the same. */
1446 return comp_type_attributes (t1, t2);
1449 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1450 is a bitwise-or of the COMPARE_* flags. */
1452 bool
1453 comptypes (tree t1, tree t2, int strict)
1455 if (strict == COMPARE_STRICT)
1457 if (t1 == t2)
1458 return true;
1460 if (t1 == error_mark_node || t2 == error_mark_node)
1461 return false;
1463 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1464 /* At least one of the types requires structural equality, so
1465 perform a deep check. */
1466 return structural_comptypes (t1, t2, strict);
1468 if (flag_checking && USE_CANONICAL_TYPES)
1470 bool result = structural_comptypes (t1, t2, strict);
1472 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1473 /* The two types are structurally equivalent, but their
1474 canonical types were different. This is a failure of the
1475 canonical type propagation code.*/
1476 internal_error
1477 ("canonical types differ for identical types %qT and %qT",
1478 t1, t2);
1479 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1480 /* Two types are structurally different, but the canonical
1481 types are the same. This means we were over-eager in
1482 assigning canonical types. */
1483 internal_error
1484 ("same canonical type node for different types %qT and %qT",
1485 t1, t2);
1487 return result;
1489 if (!flag_checking && USE_CANONICAL_TYPES)
1490 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1491 else
1492 return structural_comptypes (t1, t2, strict);
1494 else if (strict == COMPARE_STRUCTURAL)
1495 return structural_comptypes (t1, t2, COMPARE_STRICT);
1496 else
1497 return structural_comptypes (t1, t2, strict);
1500 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1501 top-level qualifiers. */
1503 bool
1504 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1506 if (type1 == error_mark_node || type2 == error_mark_node)
1507 return false;
1509 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1510 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1511 return same_type_p (type1, type2);
1514 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1516 bool
1517 at_least_as_qualified_p (const_tree type1, const_tree type2)
1519 int q1 = cp_type_quals (type1);
1520 int q2 = cp_type_quals (type2);
1522 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1523 return (q1 & q2) == q2;
1526 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1527 more cv-qualified that TYPE1, and 0 otherwise. */
1530 comp_cv_qualification (int q1, int q2)
1532 if (q1 == q2)
1533 return 0;
1535 if ((q1 & q2) == q2)
1536 return 1;
1537 else if ((q1 & q2) == q1)
1538 return -1;
1540 return 0;
1544 comp_cv_qualification (const_tree type1, const_tree type2)
1546 int q1 = cp_type_quals (type1);
1547 int q2 = cp_type_quals (type2);
1548 return comp_cv_qualification (q1, q2);
1551 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1552 subset of the cv-qualification signature of TYPE2, and the types
1553 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1556 comp_cv_qual_signature (tree type1, tree type2)
1558 if (comp_ptr_ttypes_real (type2, type1, -1))
1559 return 1;
1560 else if (comp_ptr_ttypes_real (type1, type2, -1))
1561 return -1;
1562 else
1563 return 0;
1566 /* Subroutines of `comptypes'. */
1568 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1569 equivalent in the sense that functions with those parameter types
1570 can have equivalent types. The two lists must be equivalent,
1571 element by element. */
1573 bool
1574 compparms (const_tree parms1, const_tree parms2)
1576 const_tree t1, t2;
1578 /* An unspecified parmlist matches any specified parmlist
1579 whose argument types don't need default promotions. */
1581 for (t1 = parms1, t2 = parms2;
1582 t1 || t2;
1583 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1585 /* If one parmlist is shorter than the other,
1586 they fail to match. */
1587 if (!t1 || !t2)
1588 return false;
1589 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1590 return false;
1592 return true;
1596 /* Process a sizeof or alignof expression where the operand is a
1597 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1598 or GNU (preferred alignment) semantics; it is ignored if op is
1599 SIZEOF_EXPR. */
1601 tree
1602 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool std_alignof,
1603 bool complain)
1605 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1606 if (type == error_mark_node)
1607 return error_mark_node;
1609 type = non_reference (type);
1610 if (TREE_CODE (type) == METHOD_TYPE)
1612 if (complain)
1614 pedwarn (input_location, OPT_Wpointer_arith,
1615 "invalid application of %qs to a member function",
1616 OVL_OP_INFO (false, op)->name);
1617 return size_one_node;
1619 else
1620 return error_mark_node;
1623 bool dependent_p = dependent_type_p (type);
1624 if (!dependent_p)
1625 complete_type (type);
1626 if (dependent_p
1627 /* VLA types will have a non-constant size. In the body of an
1628 uninstantiated template, we don't need to try to compute the
1629 value, because the sizeof expression is not an integral
1630 constant expression in that case. And, if we do try to
1631 compute the value, we'll likely end up with SAVE_EXPRs, which
1632 the template substitution machinery does not expect to see. */
1633 || (processing_template_decl
1634 && COMPLETE_TYPE_P (type)
1635 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1637 tree value = build_min (op, size_type_node, type);
1638 TREE_READONLY (value) = 1;
1639 if (op == ALIGNOF_EXPR && std_alignof)
1640 ALIGNOF_EXPR_STD_P (value) = true;
1641 return value;
1644 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1645 op == SIZEOF_EXPR, std_alignof,
1646 complain);
1649 /* Return the size of the type, without producing any warnings for
1650 types whose size cannot be taken. This routine should be used only
1651 in some other routine that has already produced a diagnostic about
1652 using the size of such a type. */
1653 tree
1654 cxx_sizeof_nowarn (tree type)
1656 if (TREE_CODE (type) == FUNCTION_TYPE
1657 || VOID_TYPE_P (type)
1658 || TREE_CODE (type) == ERROR_MARK)
1659 return size_one_node;
1660 else if (!COMPLETE_TYPE_P (type))
1661 return size_zero_node;
1662 else
1663 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false, false);
1666 /* Process a sizeof expression where the operand is an expression. */
1668 static tree
1669 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1671 if (e == error_mark_node)
1672 return error_mark_node;
1674 if (processing_template_decl)
1676 e = build_min (SIZEOF_EXPR, size_type_node, e);
1677 TREE_SIDE_EFFECTS (e) = 0;
1678 TREE_READONLY (e) = 1;
1680 return e;
1683 /* To get the size of a static data member declared as an array of
1684 unknown bound, we need to instantiate it. */
1685 if (VAR_P (e)
1686 && VAR_HAD_UNKNOWN_BOUND (e)
1687 && DECL_TEMPLATE_INSTANTIATION (e))
1688 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1690 if (TREE_CODE (e) == PARM_DECL
1691 && DECL_ARRAY_PARAMETER_P (e)
1692 && (complain & tf_warning))
1694 auto_diagnostic_group d;
1695 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1696 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1697 inform (DECL_SOURCE_LOCATION (e), "declared here");
1700 e = mark_type_use (e);
1702 if (bitfield_p (e))
1704 if (complain & tf_error)
1705 error ("invalid application of %<sizeof%> to a bit-field");
1706 else
1707 return error_mark_node;
1708 e = char_type_node;
1710 else if (is_overloaded_fn (e))
1712 if (complain & tf_error)
1713 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1714 "function type");
1715 else
1716 return error_mark_node;
1717 e = char_type_node;
1719 else if (type_unknown_p (e))
1721 if (complain & tf_error)
1722 cxx_incomplete_type_error (e, TREE_TYPE (e));
1723 else
1724 return error_mark_node;
1725 e = char_type_node;
1727 else
1728 e = TREE_TYPE (e);
1730 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, false, complain & tf_error);
1733 /* Implement the __alignof keyword: Return the minimum required
1734 alignment of E, measured in bytes. For VAR_DECL's and
1735 FIELD_DECL's return DECL_ALIGN (which can be set from an
1736 "aligned" __attribute__ specification). */
1738 static tree
1739 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1741 tree t;
1743 if (e == error_mark_node)
1744 return error_mark_node;
1746 if (processing_template_decl)
1748 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1749 TREE_SIDE_EFFECTS (e) = 0;
1750 TREE_READONLY (e) = 1;
1752 return e;
1755 e = mark_type_use (e);
1757 if (VAR_P (e))
1758 t = size_int (DECL_ALIGN_UNIT (e));
1759 else if (bitfield_p (e))
1761 if (complain & tf_error)
1762 error ("invalid application of %<__alignof%> to a bit-field");
1763 else
1764 return error_mark_node;
1765 t = size_one_node;
1767 else if (TREE_CODE (e) == COMPONENT_REF
1768 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1769 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1770 else if (is_overloaded_fn (e))
1772 if (complain & tf_error)
1773 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1774 "function type");
1775 else
1776 return error_mark_node;
1777 if (TREE_CODE (e) == FUNCTION_DECL)
1778 t = size_int (DECL_ALIGN_UNIT (e));
1779 else
1780 t = size_one_node;
1782 else if (type_unknown_p (e))
1784 if (complain & tf_error)
1785 cxx_incomplete_type_error (e, TREE_TYPE (e));
1786 else
1787 return error_mark_node;
1788 t = size_one_node;
1790 else
1791 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, false,
1792 complain & tf_error);
1794 return fold_convert (size_type_node, t);
1797 /* Process a sizeof or alignof expression E with code OP where the operand
1798 is an expression. */
1800 tree
1801 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1803 if (op == SIZEOF_EXPR)
1804 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1805 else
1806 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1809 /* Build a representation of an expression 'alignas(E).' Return the
1810 folded integer value of E if it is an integral constant expression
1811 that resolves to a valid alignment. If E depends on a template
1812 parameter, return a syntactic representation tree of kind
1813 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1814 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1816 tree
1817 cxx_alignas_expr (tree e)
1819 if (e == NULL_TREE || e == error_mark_node
1820 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1821 return e;
1823 if (TYPE_P (e))
1824 /* [dcl.align]/3:
1826 When the alignment-specifier is of the form
1827 alignas(type-id ), it shall have the same effect as
1828 alignas(alignof(type-id )). */
1830 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, true, false);
1832 /* If we reach this point, it means the alignas expression if of
1833 the form "alignas(assignment-expression)", so we should follow
1834 what is stated by [dcl.align]/2. */
1836 if (value_dependent_expression_p (e))
1837 /* Leave value-dependent expression alone for now. */
1838 return e;
1840 e = instantiate_non_dependent_expr (e);
1841 e = mark_rvalue_use (e);
1843 /* [dcl.align]/2 says:
1845 the assignment-expression shall be an integral constant
1846 expression. */
1848 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1850 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1851 return error_mark_node;
1854 return cxx_constant_value (e);
1858 /* EXPR is being used in a context that is not a function call.
1859 Enforce:
1861 [expr.ref]
1863 The expression can be used only as the left-hand operand of a
1864 member function call.
1866 [expr.mptr.operator]
1868 If the result of .* or ->* is a function, then that result can be
1869 used only as the operand for the function call operator ().
1871 by issuing an error message if appropriate. Returns true iff EXPR
1872 violates these rules. */
1874 bool
1875 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1877 if (expr == NULL_TREE)
1878 return false;
1879 /* Don't enforce this in MS mode. */
1880 if (flag_ms_extensions)
1881 return false;
1882 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1883 expr = get_first_fn (expr);
1884 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1886 if (complain & tf_error)
1888 if (DECL_P (expr))
1890 error_at (loc, "invalid use of non-static member function %qD",
1891 expr);
1892 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1894 else
1895 error_at (loc, "invalid use of non-static member function of "
1896 "type %qT", TREE_TYPE (expr));
1898 return true;
1900 return false;
1903 /* If EXP is a reference to a bitfield, and the type of EXP does not
1904 match the declared type of the bitfield, return the declared type
1905 of the bitfield. Otherwise, return NULL_TREE. */
1907 tree
1908 is_bitfield_expr_with_lowered_type (const_tree exp)
1910 switch (TREE_CODE (exp))
1912 case COND_EXPR:
1913 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1914 ? TREE_OPERAND (exp, 1)
1915 : TREE_OPERAND (exp, 0)))
1916 return NULL_TREE;
1917 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1919 case COMPOUND_EXPR:
1920 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1922 case MODIFY_EXPR:
1923 case SAVE_EXPR:
1924 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1926 case COMPONENT_REF:
1928 tree field;
1930 field = TREE_OPERAND (exp, 1);
1931 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1932 return NULL_TREE;
1933 if (same_type_ignoring_top_level_qualifiers_p
1934 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1935 return NULL_TREE;
1936 return DECL_BIT_FIELD_TYPE (field);
1939 case VAR_DECL:
1940 if (DECL_HAS_VALUE_EXPR_P (exp))
1941 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1942 (CONST_CAST_TREE (exp)));
1943 return NULL_TREE;
1945 CASE_CONVERT:
1946 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1947 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1948 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1949 /* Fallthrough. */
1951 default:
1952 return NULL_TREE;
1956 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1957 bitfield with a lowered type, the type of EXP is returned, rather
1958 than NULL_TREE. */
1960 tree
1961 unlowered_expr_type (const_tree exp)
1963 tree type;
1964 tree etype = TREE_TYPE (exp);
1966 type = is_bitfield_expr_with_lowered_type (exp);
1967 if (type)
1968 type = cp_build_qualified_type (type, cp_type_quals (etype));
1969 else
1970 type = etype;
1972 return type;
1975 /* Perform the conversions in [expr] that apply when an lvalue appears
1976 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1977 function-to-pointer conversions. In addition, bitfield references are
1978 converted to their declared types. Note that this function does not perform
1979 the lvalue-to-rvalue conversion for class types. If you need that conversion
1980 for class types, then you probably need to use force_rvalue.
1982 Although the returned value is being used as an rvalue, this
1983 function does not wrap the returned expression in a
1984 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1985 that the return value is no longer an lvalue. */
1987 tree
1988 decay_conversion (tree exp,
1989 tsubst_flags_t complain,
1990 bool reject_builtin /* = true */)
1992 tree type;
1993 enum tree_code code;
1994 location_t loc = cp_expr_loc_or_loc (exp, input_location);
1996 type = TREE_TYPE (exp);
1997 if (type == error_mark_node)
1998 return error_mark_node;
2000 exp = resolve_nondeduced_context (exp, complain);
2001 if (type_unknown_p (exp))
2003 if (complain & tf_error)
2004 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
2005 return error_mark_node;
2008 code = TREE_CODE (type);
2010 if (error_operand_p (exp))
2011 return error_mark_node;
2013 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2015 mark_rvalue_use (exp, loc, reject_builtin);
2016 return nullptr_node;
2019 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2020 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2021 if (code == VOID_TYPE)
2023 if (complain & tf_error)
2024 error_at (loc, "void value not ignored as it ought to be");
2025 return error_mark_node;
2027 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2028 return error_mark_node;
2029 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2031 exp = mark_lvalue_use (exp);
2032 if (reject_builtin && reject_gcc_builtin (exp, loc))
2033 return error_mark_node;
2034 return cp_build_addr_expr (exp, complain);
2036 if (code == ARRAY_TYPE)
2038 tree adr;
2039 tree ptrtype;
2041 exp = mark_lvalue_use (exp);
2043 if (INDIRECT_REF_P (exp))
2044 return build_nop (build_pointer_type (TREE_TYPE (type)),
2045 TREE_OPERAND (exp, 0));
2047 if (TREE_CODE (exp) == COMPOUND_EXPR)
2049 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2050 if (op1 == error_mark_node)
2051 return error_mark_node;
2052 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2053 TREE_OPERAND (exp, 0), op1);
2056 if (!obvalue_p (exp)
2057 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2059 if (complain & tf_error)
2060 error_at (loc, "invalid use of non-lvalue array");
2061 return error_mark_node;
2064 /* Don't let an array compound literal decay to a pointer. It can
2065 still be used to initialize an array or bind to a reference. */
2066 if (TREE_CODE (exp) == TARGET_EXPR)
2068 if (complain & tf_error)
2069 error_at (loc, "taking address of temporary array");
2070 return error_mark_node;
2073 ptrtype = build_pointer_type (TREE_TYPE (type));
2075 if (VAR_P (exp))
2077 if (!cxx_mark_addressable (exp))
2078 return error_mark_node;
2079 adr = build_nop (ptrtype, build_address (exp));
2080 return adr;
2082 /* This way is better for a COMPONENT_REF since it can
2083 simplify the offset for a component. */
2084 adr = cp_build_addr_expr (exp, complain);
2085 return cp_convert (ptrtype, adr, complain);
2088 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2089 exp = mark_rvalue_use (exp, loc, reject_builtin);
2091 /* If a bitfield is used in a context where integral promotion
2092 applies, then the caller is expected to have used
2093 default_conversion. That function promotes bitfields correctly
2094 before calling this function. At this point, if we have a
2095 bitfield referenced, we may assume that is not subject to
2096 promotion, and that, therefore, the type of the resulting rvalue
2097 is the declared type of the bitfield. */
2098 exp = convert_bitfield_to_declared_type (exp);
2100 /* We do not call rvalue() here because we do not want to wrap EXP
2101 in a NON_LVALUE_EXPR. */
2103 /* [basic.lval]
2105 Non-class rvalues always have cv-unqualified types. */
2106 type = TREE_TYPE (exp);
2107 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2108 exp = build_nop (cv_unqualified (type), exp);
2110 if (!complete_type_or_maybe_complain (type, exp, complain))
2111 return error_mark_node;
2113 return exp;
2116 /* Perform preparatory conversions, as part of the "usual arithmetic
2117 conversions". In particular, as per [expr]:
2119 Whenever an lvalue expression appears as an operand of an
2120 operator that expects the rvalue for that operand, the
2121 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2122 standard conversions are applied to convert the expression to an
2123 rvalue.
2125 In addition, we perform integral promotions here, as those are
2126 applied to both operands to a binary operator before determining
2127 what additional conversions should apply. */
2129 static tree
2130 cp_default_conversion (tree exp, tsubst_flags_t complain)
2132 /* Check for target-specific promotions. */
2133 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2134 if (promoted_type)
2135 exp = cp_convert (promoted_type, exp, complain);
2136 /* Perform the integral promotions first so that bitfield
2137 expressions (which may promote to "int", even if the bitfield is
2138 declared "unsigned") are promoted correctly. */
2139 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2140 exp = cp_perform_integral_promotions (exp, complain);
2141 /* Perform the other conversions. */
2142 exp = decay_conversion (exp, complain);
2144 return exp;
2147 /* C version. */
2149 tree
2150 default_conversion (tree exp)
2152 return cp_default_conversion (exp, tf_warning_or_error);
2155 /* EXPR is an expression with an integral or enumeration type.
2156 Perform the integral promotions in [conv.prom], and return the
2157 converted value. */
2159 tree
2160 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2162 tree type;
2163 tree promoted_type;
2165 expr = mark_rvalue_use (expr);
2166 if (error_operand_p (expr))
2167 return error_mark_node;
2169 /* [conv.prom]
2171 If the bitfield has an enumerated type, it is treated as any
2172 other value of that type for promotion purposes. */
2173 type = is_bitfield_expr_with_lowered_type (expr);
2174 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2175 type = TREE_TYPE (expr);
2176 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2177 /* Scoped enums don't promote. */
2178 if (SCOPED_ENUM_P (type))
2179 return expr;
2180 promoted_type = type_promotes_to (type);
2181 if (type != promoted_type)
2182 expr = cp_convert (promoted_type, expr, complain);
2183 return expr;
2186 /* C version. */
2188 tree
2189 perform_integral_promotions (tree expr)
2191 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2194 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2195 decay_conversion to one. */
2198 string_conv_p (const_tree totype, const_tree exp, int warn)
2200 tree t;
2202 if (!TYPE_PTR_P (totype))
2203 return 0;
2205 t = TREE_TYPE (totype);
2206 if (!same_type_p (t, char_type_node)
2207 && !same_type_p (t, char16_type_node)
2208 && !same_type_p (t, char32_type_node)
2209 && !same_type_p (t, wchar_type_node))
2210 return 0;
2212 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2214 STRIP_ANY_LOCATION_WRAPPER (exp);
2216 if (TREE_CODE (exp) == STRING_CST)
2218 /* Make sure that we don't try to convert between char and wide chars. */
2219 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2220 return 0;
2222 else
2224 /* Is this a string constant which has decayed to 'const char *'? */
2225 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2226 if (!same_type_p (TREE_TYPE (exp), t))
2227 return 0;
2228 STRIP_NOPS (exp);
2229 if (TREE_CODE (exp) != ADDR_EXPR
2230 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2231 return 0;
2233 if (warn)
2235 if (cxx_dialect >= cxx11)
2236 pedwarn (loc, OPT_Wwrite_strings,
2237 "ISO C++ forbids converting a string constant to %qT",
2238 totype);
2239 else
2240 warning_at (loc, OPT_Wwrite_strings,
2241 "deprecated conversion from string constant to %qT",
2242 totype);
2245 return 1;
2248 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2249 can, for example, use as an lvalue. This code used to be in
2250 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2251 expressions, where we're dealing with aggregates. But now it's again only
2252 called from unary_complex_lvalue. The case (in particular) that led to
2253 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2254 get it there. */
2256 static tree
2257 rationalize_conditional_expr (enum tree_code code, tree t,
2258 tsubst_flags_t complain)
2260 location_t loc = cp_expr_loc_or_loc (t, input_location);
2262 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2263 the first operand is always the one to be used if both operands
2264 are equal, so we know what conditional expression this used to be. */
2265 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2267 tree op0 = TREE_OPERAND (t, 0);
2268 tree op1 = TREE_OPERAND (t, 1);
2270 /* The following code is incorrect if either operand side-effects. */
2271 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2272 && !TREE_SIDE_EFFECTS (op1));
2273 return
2274 build_conditional_expr (loc,
2275 build_x_binary_op (loc,
2276 (TREE_CODE (t) == MIN_EXPR
2277 ? LE_EXPR : GE_EXPR),
2278 op0, TREE_CODE (op0),
2279 op1, TREE_CODE (op1),
2280 /*overload=*/NULL,
2281 complain),
2282 cp_build_unary_op (code, op0, false, complain),
2283 cp_build_unary_op (code, op1, false, complain),
2284 complain);
2287 return
2288 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2289 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2290 complain),
2291 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2292 complain),
2293 complain);
2296 /* Given the TYPE of an anonymous union field inside T, return the
2297 FIELD_DECL for the field. If not found return NULL_TREE. Because
2298 anonymous unions can nest, we must also search all anonymous unions
2299 that are directly reachable. */
2301 tree
2302 lookup_anon_field (tree t, tree type)
2304 tree field;
2306 t = TYPE_MAIN_VARIANT (t);
2308 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2310 if (TREE_STATIC (field))
2311 continue;
2312 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2313 continue;
2315 /* If we find it directly, return the field. */
2316 if (DECL_NAME (field) == NULL_TREE
2317 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2319 return field;
2322 /* Otherwise, it could be nested, search harder. */
2323 if (DECL_NAME (field) == NULL_TREE
2324 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2326 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2327 if (subfield)
2328 return subfield;
2331 return NULL_TREE;
2334 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2335 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2336 non-NULL, it indicates the path to the base used to name MEMBER.
2337 If PRESERVE_REFERENCE is true, the expression returned will have
2338 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2339 returned will have the type referred to by the reference.
2341 This function does not perform access control; that is either done
2342 earlier by the parser when the name of MEMBER is resolved to MEMBER
2343 itself, or later when overload resolution selects one of the
2344 functions indicated by MEMBER. */
2346 tree
2347 build_class_member_access_expr (cp_expr object, tree member,
2348 tree access_path, bool preserve_reference,
2349 tsubst_flags_t complain)
2351 tree object_type;
2352 tree member_scope;
2353 tree result = NULL_TREE;
2354 tree using_decl = NULL_TREE;
2356 if (error_operand_p (object) || error_operand_p (member))
2357 return error_mark_node;
2359 gcc_assert (DECL_P (member) || BASELINK_P (member));
2361 /* [expr.ref]
2363 The type of the first expression shall be "class object" (of a
2364 complete type). */
2365 object_type = TREE_TYPE (object);
2366 if (!currently_open_class (object_type)
2367 && !complete_type_or_maybe_complain (object_type, object, complain))
2368 return error_mark_node;
2369 if (!CLASS_TYPE_P (object_type))
2371 if (complain & tf_error)
2373 if (INDIRECT_TYPE_P (object_type)
2374 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2375 error ("request for member %qD in %qE, which is of pointer "
2376 "type %qT (maybe you meant to use %<->%> ?)",
2377 member, object.get_value (), object_type);
2378 else
2379 error ("request for member %qD in %qE, which is of non-class "
2380 "type %qT", member, object.get_value (), object_type);
2382 return error_mark_node;
2385 /* The standard does not seem to actually say that MEMBER must be a
2386 member of OBJECT_TYPE. However, that is clearly what is
2387 intended. */
2388 if (DECL_P (member))
2390 member_scope = DECL_CLASS_CONTEXT (member);
2391 if (!mark_used (member, complain) && !(complain & tf_error))
2392 return error_mark_node;
2393 if (TREE_DEPRECATED (member))
2394 warn_deprecated_use (member, NULL_TREE);
2396 else
2397 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2398 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2399 presently be the anonymous union. Go outwards until we find a
2400 type related to OBJECT_TYPE. */
2401 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2402 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2403 object_type))
2404 member_scope = TYPE_CONTEXT (member_scope);
2405 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2407 if (complain & tf_error)
2409 if (TREE_CODE (member) == FIELD_DECL)
2410 error ("invalid use of nonstatic data member %qE", member);
2411 else
2412 error ("%qD is not a member of %qT", member, object_type);
2414 return error_mark_node;
2417 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2418 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2419 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2421 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2422 if (temp)
2423 object = cp_build_fold_indirect_ref (temp);
2426 /* In [expr.ref], there is an explicit list of the valid choices for
2427 MEMBER. We check for each of those cases here. */
2428 if (VAR_P (member))
2430 /* A static data member. */
2431 result = member;
2432 mark_exp_read (object);
2433 /* If OBJECT has side-effects, they are supposed to occur. */
2434 if (TREE_SIDE_EFFECTS (object))
2435 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2437 else if (TREE_CODE (member) == FIELD_DECL)
2439 /* A non-static data member. */
2440 bool null_object_p;
2441 int type_quals;
2442 tree member_type;
2444 if (INDIRECT_REF_P (object))
2445 null_object_p =
2446 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2447 else
2448 null_object_p = false;
2450 /* Convert OBJECT to the type of MEMBER. */
2451 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2452 TYPE_MAIN_VARIANT (member_scope)))
2454 tree binfo;
2455 base_kind kind;
2457 binfo = lookup_base (access_path ? access_path : object_type,
2458 member_scope, ba_unique, &kind, complain);
2459 if (binfo == error_mark_node)
2460 return error_mark_node;
2462 /* It is invalid to try to get to a virtual base of a
2463 NULL object. The most common cause is invalid use of
2464 offsetof macro. */
2465 if (null_object_p && kind == bk_via_virtual)
2467 if (complain & tf_error)
2469 error ("invalid access to non-static data member %qD in "
2470 "virtual base of NULL object", member);
2472 return error_mark_node;
2475 /* Convert to the base. */
2476 object = build_base_path (PLUS_EXPR, object, binfo,
2477 /*nonnull=*/1, complain);
2478 /* If we found the base successfully then we should be able
2479 to convert to it successfully. */
2480 gcc_assert (object != error_mark_node);
2483 /* If MEMBER is from an anonymous aggregate, we have converted
2484 OBJECT so that it refers to the class containing the
2485 anonymous union. Generate a reference to the anonymous union
2486 itself, and recur to find MEMBER. */
2487 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2488 /* When this code is called from build_field_call, the
2489 object already has the type of the anonymous union.
2490 That is because the COMPONENT_REF was already
2491 constructed, and was then disassembled before calling
2492 build_field_call. After the function-call code is
2493 cleaned up, this waste can be eliminated. */
2494 && (!same_type_ignoring_top_level_qualifiers_p
2495 (TREE_TYPE (object), DECL_CONTEXT (member))))
2497 tree anonymous_union;
2499 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2500 DECL_CONTEXT (member));
2501 object = build_class_member_access_expr (object,
2502 anonymous_union,
2503 /*access_path=*/NULL_TREE,
2504 preserve_reference,
2505 complain);
2508 /* Compute the type of the field, as described in [expr.ref]. */
2509 type_quals = TYPE_UNQUALIFIED;
2510 member_type = TREE_TYPE (member);
2511 if (!TYPE_REF_P (member_type))
2513 type_quals = (cp_type_quals (member_type)
2514 | cp_type_quals (object_type));
2516 /* A field is const (volatile) if the enclosing object, or the
2517 field itself, is const (volatile). But, a mutable field is
2518 not const, even within a const object. */
2519 if (DECL_MUTABLE_P (member))
2520 type_quals &= ~TYPE_QUAL_CONST;
2521 member_type = cp_build_qualified_type (member_type, type_quals);
2524 result = build3_loc (input_location, COMPONENT_REF, member_type,
2525 object, member, NULL_TREE);
2527 /* Mark the expression const or volatile, as appropriate. Even
2528 though we've dealt with the type above, we still have to mark the
2529 expression itself. */
2530 if (type_quals & TYPE_QUAL_CONST)
2531 TREE_READONLY (result) = 1;
2532 if (type_quals & TYPE_QUAL_VOLATILE)
2533 TREE_THIS_VOLATILE (result) = 1;
2535 else if (BASELINK_P (member))
2537 /* The member is a (possibly overloaded) member function. */
2538 tree functions;
2539 tree type;
2541 /* If the MEMBER is exactly one static member function, then we
2542 know the type of the expression. Otherwise, we must wait
2543 until overload resolution has been performed. */
2544 functions = BASELINK_FUNCTIONS (member);
2545 if (TREE_CODE (functions) == FUNCTION_DECL
2546 && DECL_STATIC_FUNCTION_P (functions))
2547 type = TREE_TYPE (functions);
2548 else
2549 type = unknown_type_node;
2550 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2551 base. That will happen when the function is called. */
2552 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2554 else if (TREE_CODE (member) == CONST_DECL)
2556 /* The member is an enumerator. */
2557 result = member;
2558 /* If OBJECT has side-effects, they are supposed to occur. */
2559 if (TREE_SIDE_EFFECTS (object))
2560 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2561 object, result);
2563 else if ((using_decl = strip_using_decl (member)) != member)
2564 result = build_class_member_access_expr (object,
2565 using_decl,
2566 access_path, preserve_reference,
2567 complain);
2568 else
2570 if (complain & tf_error)
2571 error ("invalid use of %qD", member);
2572 return error_mark_node;
2575 if (!preserve_reference)
2576 /* [expr.ref]
2578 If E2 is declared to have type "reference to T", then ... the
2579 type of E1.E2 is T. */
2580 result = convert_from_reference (result);
2582 return result;
2585 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2586 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2588 static tree
2589 lookup_destructor (tree object, tree scope, tree dtor_name,
2590 tsubst_flags_t complain)
2592 tree object_type = TREE_TYPE (object);
2593 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2594 tree expr;
2596 /* We've already complained about this destructor. */
2597 if (dtor_type == error_mark_node)
2598 return error_mark_node;
2600 if (scope && !check_dtor_name (scope, dtor_type))
2602 if (complain & tf_error)
2603 error ("qualified type %qT does not match destructor name ~%qT",
2604 scope, dtor_type);
2605 return error_mark_node;
2607 if (is_auto (dtor_type))
2608 dtor_type = object_type;
2609 else if (identifier_p (dtor_type))
2611 /* In a template, names we can't find a match for are still accepted
2612 destructor names, and we check them here. */
2613 if (check_dtor_name (object_type, dtor_type))
2614 dtor_type = object_type;
2615 else
2617 if (complain & tf_error)
2618 error ("object type %qT does not match destructor name ~%qT",
2619 object_type, dtor_type);
2620 return error_mark_node;
2624 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2626 if (complain & tf_error)
2627 error ("the type being destroyed is %qT, but the destructor "
2628 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2629 return error_mark_node;
2631 expr = lookup_member (dtor_type, complete_dtor_identifier,
2632 /*protect=*/1, /*want_type=*/false,
2633 tf_warning_or_error);
2634 if (!expr)
2636 if (complain & tf_error)
2637 cxx_incomplete_type_error (dtor_name, dtor_type);
2638 return error_mark_node;
2640 expr = (adjust_result_of_qualified_name_lookup
2641 (expr, dtor_type, object_type));
2642 if (scope == NULL_TREE)
2643 /* We need to call adjust_result_of_qualified_name_lookup in case the
2644 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2645 that we still get virtual function binding. */
2646 BASELINK_QUALIFIED_P (expr) = false;
2647 return expr;
2650 /* An expression of the form "A::template B" has been resolved to
2651 DECL. Issue a diagnostic if B is not a template or template
2652 specialization. */
2654 void
2655 check_template_keyword (tree decl)
2657 /* The standard says:
2659 [temp.names]
2661 If a name prefixed by the keyword template is not a member
2662 template, the program is ill-formed.
2664 DR 228 removed the restriction that the template be a member
2665 template.
2667 DR 96, if accepted would add the further restriction that explicit
2668 template arguments must be provided if the template keyword is
2669 used, but, as of 2005-10-16, that DR is still in "drafting". If
2670 this DR is accepted, then the semantic checks here can be
2671 simplified, as the entity named must in fact be a template
2672 specialization, rather than, as at present, a set of overloaded
2673 functions containing at least one template function. */
2674 if (TREE_CODE (decl) != TEMPLATE_DECL
2675 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2677 if (VAR_P (decl))
2679 if (DECL_USE_TEMPLATE (decl)
2680 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2682 else
2683 permerror (input_location, "%qD is not a template", decl);
2685 else if (!is_overloaded_fn (decl))
2686 permerror (input_location, "%qD is not a template", decl);
2687 else
2689 bool found = false;
2691 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2692 !found && iter; ++iter)
2694 tree fn = *iter;
2695 if (TREE_CODE (fn) == TEMPLATE_DECL
2696 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2697 || (TREE_CODE (fn) == FUNCTION_DECL
2698 && DECL_USE_TEMPLATE (fn)
2699 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2700 found = true;
2702 if (!found)
2703 permerror (input_location, "%qD is not a template", decl);
2708 /* Record that an access failure occurred on BASETYPE_PATH attempting
2709 to access FIELD_DECL. */
2711 void
2712 access_failure_info::record_access_failure (tree basetype_path,
2713 tree field_decl)
2715 m_was_inaccessible = true;
2716 m_basetype_path = basetype_path;
2717 m_field_decl = field_decl;
2720 /* If an access failure was recorded, then attempt to locate an
2721 accessor function for the pertinent field, and if one is
2722 available, add a note and fix-it hint suggesting using it. */
2724 void
2725 access_failure_info::maybe_suggest_accessor (bool const_p) const
2727 if (!m_was_inaccessible)
2728 return;
2730 tree accessor
2731 = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2732 if (!accessor)
2733 return;
2735 /* The accessor must itself be accessible for it to be a reasonable
2736 suggestion. */
2737 if (!accessible_p (m_basetype_path, accessor, true))
2738 return;
2740 rich_location richloc (line_table, input_location);
2741 pretty_printer pp;
2742 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2743 richloc.add_fixit_replace (pp_formatted_text (&pp));
2744 inform (&richloc, "field %q#D can be accessed via %q#D",
2745 m_field_decl, accessor);
2748 /* This function is called by the parser to process a class member
2749 access expression of the form OBJECT.NAME. NAME is a node used by
2750 the parser to represent a name; it is not yet a DECL. It may,
2751 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2752 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2753 there is no reason to do the lookup twice, so the parser keeps the
2754 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2755 be a template via the use of the "A::template B" syntax. */
2757 tree
2758 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2759 tsubst_flags_t complain)
2761 tree expr;
2762 tree object_type;
2763 tree member;
2764 tree access_path = NULL_TREE;
2765 tree orig_object = object;
2766 tree orig_name = name;
2768 if (object == error_mark_node || name == error_mark_node)
2769 return error_mark_node;
2771 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2772 if (!objc_is_public (object, name))
2773 return error_mark_node;
2775 object_type = TREE_TYPE (object);
2777 if (processing_template_decl)
2779 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2780 type_dependent_object_expression_p (object)
2781 /* If NAME is "f<args>", where either 'f' or 'args' is
2782 dependent, then the expression is dependent. */
2783 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2784 && dependent_template_id_p (TREE_OPERAND (name, 0),
2785 TREE_OPERAND (name, 1)))
2786 /* If NAME is "T::X" where "T" is dependent, then the
2787 expression is dependent. */
2788 || (TREE_CODE (name) == SCOPE_REF
2789 && TYPE_P (TREE_OPERAND (name, 0))
2790 && dependent_scope_p (TREE_OPERAND (name, 0))))
2792 dependent:
2793 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2794 orig_object, orig_name, NULL_TREE);
2796 object = build_non_dependent_expr (object);
2798 else if (c_dialect_objc ()
2799 && identifier_p (name)
2800 && (expr = objc_maybe_build_component_ref (object, name)))
2801 return expr;
2803 /* [expr.ref]
2805 The type of the first expression shall be "class object" (of a
2806 complete type). */
2807 if (!currently_open_class (object_type)
2808 && !complete_type_or_maybe_complain (object_type, object, complain))
2809 return error_mark_node;
2810 if (!CLASS_TYPE_P (object_type))
2812 if (complain & tf_error)
2814 if (INDIRECT_TYPE_P (object_type)
2815 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2816 error ("request for member %qD in %qE, which is of pointer "
2817 "type %qT (maybe you meant to use %<->%> ?)",
2818 name, object.get_value (), object_type);
2819 else
2820 error ("request for member %qD in %qE, which is of non-class "
2821 "type %qT", name, object.get_value (), object_type);
2823 return error_mark_node;
2826 if (BASELINK_P (name))
2827 /* A member function that has already been looked up. */
2828 member = name;
2829 else
2831 bool is_template_id = false;
2832 tree template_args = NULL_TREE;
2833 tree scope = NULL_TREE;
2835 access_path = object_type;
2837 if (TREE_CODE (name) == SCOPE_REF)
2839 /* A qualified name. The qualifying class or namespace `S'
2840 has already been looked up; it is either a TYPE or a
2841 NAMESPACE_DECL. */
2842 scope = TREE_OPERAND (name, 0);
2843 name = TREE_OPERAND (name, 1);
2845 /* If SCOPE is a namespace, then the qualified name does not
2846 name a member of OBJECT_TYPE. */
2847 if (TREE_CODE (scope) == NAMESPACE_DECL)
2849 if (complain & tf_error)
2850 error ("%<%D::%D%> is not a member of %qT",
2851 scope, name, object_type);
2852 return error_mark_node;
2856 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2858 is_template_id = true;
2859 template_args = TREE_OPERAND (name, 1);
2860 name = TREE_OPERAND (name, 0);
2862 if (!identifier_p (name))
2863 name = OVL_NAME (name);
2866 if (scope)
2868 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2870 gcc_assert (!is_template_id);
2871 /* Looking up a member enumerator (c++/56793). */
2872 if (!TYPE_CLASS_SCOPE_P (scope)
2873 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2875 if (complain & tf_error)
2876 error ("%<%D::%D%> is not a member of %qT",
2877 scope, name, object_type);
2878 return error_mark_node;
2880 tree val = lookup_enumerator (scope, name);
2881 if (!val)
2883 if (complain & tf_error)
2884 error ("%qD is not a member of %qD",
2885 name, scope);
2886 return error_mark_node;
2889 if (TREE_SIDE_EFFECTS (object))
2890 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2891 return val;
2894 gcc_assert (CLASS_TYPE_P (scope));
2895 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2897 if (constructor_name_p (name, scope))
2899 if (complain & tf_error)
2900 error ("cannot call constructor %<%T::%D%> directly",
2901 scope, name);
2902 return error_mark_node;
2905 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2906 access_path = lookup_base (object_type, scope, ba_check,
2907 NULL, complain);
2908 if (access_path == error_mark_node)
2909 return error_mark_node;
2910 if (!access_path)
2912 if (any_dependent_bases_p (object_type))
2913 goto dependent;
2914 if (complain & tf_error)
2915 error ("%qT is not a base of %qT", scope, object_type);
2916 return error_mark_node;
2920 if (TREE_CODE (name) == BIT_NOT_EXPR)
2922 if (dependent_type_p (object_type))
2923 /* The destructor isn't declared yet. */
2924 goto dependent;
2925 member = lookup_destructor (object, scope, name, complain);
2927 else
2929 /* Look up the member. */
2930 access_failure_info afi;
2931 member = lookup_member (access_path, name, /*protect=*/1,
2932 /*want_type=*/false, complain,
2933 &afi);
2934 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2935 if (member == NULL_TREE)
2937 if (dependent_type_p (object_type))
2938 /* Try again at instantiation time. */
2939 goto dependent;
2940 if (complain & tf_error)
2942 tree guessed_id = lookup_member_fuzzy (access_path, name,
2943 /*want_type=*/false);
2944 if (guessed_id)
2946 location_t bogus_component_loc = input_location;
2947 gcc_rich_location rich_loc (bogus_component_loc);
2948 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2949 guessed_id);
2950 error_at (&rich_loc,
2951 "%q#T has no member named %qE;"
2952 " did you mean %qE?",
2953 TREE_CODE (access_path) == TREE_BINFO
2954 ? TREE_TYPE (access_path) : object_type,
2955 name, guessed_id);
2957 else
2958 error ("%q#T has no member named %qE",
2959 TREE_CODE (access_path) == TREE_BINFO
2960 ? TREE_TYPE (access_path) : object_type, name);
2962 return error_mark_node;
2964 if (member == error_mark_node)
2965 return error_mark_node;
2966 if (DECL_P (member)
2967 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2968 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2969 wrong, so don't use it. */
2970 goto dependent;
2971 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2972 goto dependent;
2975 if (is_template_id)
2977 tree templ = member;
2979 if (BASELINK_P (templ))
2980 member = lookup_template_function (templ, template_args);
2981 else if (variable_template_p (templ))
2982 member = (lookup_and_finish_template_variable
2983 (templ, template_args, complain));
2984 else
2986 if (complain & tf_error)
2987 error ("%qD is not a member template function", name);
2988 return error_mark_node;
2993 if (TREE_DEPRECATED (member))
2994 warn_deprecated_use (member, NULL_TREE);
2996 if (template_p)
2997 check_template_keyword (member);
2999 expr = build_class_member_access_expr (object, member, access_path,
3000 /*preserve_reference=*/false,
3001 complain);
3002 if (processing_template_decl && expr != error_mark_node)
3004 if (BASELINK_P (member))
3006 if (TREE_CODE (orig_name) == SCOPE_REF)
3007 BASELINK_QUALIFIED_P (member) = 1;
3008 orig_name = member;
3010 return build_min_non_dep (COMPONENT_REF, expr,
3011 orig_object, orig_name,
3012 NULL_TREE);
3015 return expr;
3018 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3019 type. */
3021 tree
3022 build_simple_component_ref (tree object, tree member)
3024 tree type = cp_build_qualified_type (TREE_TYPE (member),
3025 cp_type_quals (TREE_TYPE (object)));
3026 return build3_loc (input_location,
3027 COMPONENT_REF, type,
3028 object, member, NULL_TREE);
3031 /* Return an expression for the MEMBER_NAME field in the internal
3032 representation of PTRMEM, a pointer-to-member function. (Each
3033 pointer-to-member function type gets its own RECORD_TYPE so it is
3034 more convenient to access the fields by name than by FIELD_DECL.)
3035 This routine converts the NAME to a FIELD_DECL and then creates the
3036 node for the complete expression. */
3038 tree
3039 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3041 tree ptrmem_type;
3042 tree member;
3044 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3046 unsigned int ix;
3047 tree index, value;
3048 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem),
3049 ix, index, value)
3050 if (index && DECL_P (index) && DECL_NAME (index) == member_name)
3051 return value;
3052 gcc_unreachable ();
3055 /* This code is a stripped down version of
3056 build_class_member_access_expr. It does not work to use that
3057 routine directly because it expects the object to be of class
3058 type. */
3059 ptrmem_type = TREE_TYPE (ptrmem);
3060 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3061 for (member = TYPE_FIELDS (ptrmem_type); member;
3062 member = DECL_CHAIN (member))
3063 if (DECL_NAME (member) == member_name)
3064 break;
3065 tree res = build_simple_component_ref (ptrmem, member);
3067 TREE_NO_WARNING (res) = 1;
3068 return res;
3071 /* Given an expression PTR for a pointer, return an expression
3072 for the value pointed to.
3073 ERRORSTRING is the name of the operator to appear in error messages.
3075 This function may need to overload OPERATOR_FNNAME.
3076 Must also handle REFERENCE_TYPEs for C++. */
3078 tree
3079 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3080 tsubst_flags_t complain)
3082 tree orig_expr = expr;
3083 tree rval;
3084 tree overload = NULL_TREE;
3086 if (processing_template_decl)
3088 /* Retain the type if we know the operand is a pointer. */
3089 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3090 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3091 if (type_dependent_expression_p (expr))
3092 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3093 expr = build_non_dependent_expr (expr);
3096 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3097 NULL_TREE, NULL_TREE, &overload, complain);
3098 if (!rval)
3099 rval = cp_build_indirect_ref (expr, errorstring, complain);
3101 if (processing_template_decl && rval != error_mark_node)
3103 if (overload != NULL_TREE)
3104 return (build_min_non_dep_op_overload
3105 (INDIRECT_REF, rval, overload, orig_expr));
3107 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3109 else
3110 return rval;
3113 /* The implementation of the above, and of indirection implied by other
3114 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3116 static tree
3117 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3118 tsubst_flags_t complain, bool do_fold)
3120 tree pointer, type;
3122 /* RO_NULL should only be used with the folding entry points below, not
3123 cp_build_indirect_ref. */
3124 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3126 if (ptr == current_class_ptr
3127 || (TREE_CODE (ptr) == NOP_EXPR
3128 && TREE_OPERAND (ptr, 0) == current_class_ptr
3129 && (same_type_ignoring_top_level_qualifiers_p
3130 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3131 return current_class_ref;
3133 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3134 ? ptr : decay_conversion (ptr, complain));
3135 if (pointer == error_mark_node)
3136 return error_mark_node;
3138 type = TREE_TYPE (pointer);
3140 if (INDIRECT_TYPE_P (type))
3142 /* [expr.unary.op]
3144 If the type of the expression is "pointer to T," the type
3145 of the result is "T." */
3146 tree t = TREE_TYPE (type);
3148 if ((CONVERT_EXPR_P (ptr)
3149 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3150 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3152 /* If a warning is issued, mark it to avoid duplicates from
3153 the backend. This only needs to be done at
3154 warn_strict_aliasing > 2. */
3155 if (warn_strict_aliasing > 2)
3156 if (strict_aliasing_warning (EXPR_LOCATION (ptr),
3157 type, TREE_OPERAND (ptr, 0)))
3158 TREE_NO_WARNING (ptr) = 1;
3161 if (VOID_TYPE_P (t))
3163 /* A pointer to incomplete type (other than cv void) can be
3164 dereferenced [expr.unary.op]/1 */
3165 if (complain & tf_error)
3166 error ("%qT is not a pointer-to-object type", type);
3167 return error_mark_node;
3169 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3170 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3171 /* The POINTER was something like `&x'. We simplify `*&x' to
3172 `x'. */
3173 return TREE_OPERAND (pointer, 0);
3174 else
3176 tree ref = build1 (INDIRECT_REF, t, pointer);
3178 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3179 so that we get the proper error message if the result is used
3180 to assign to. Also, &* is supposed to be a no-op. */
3181 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3182 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3183 TREE_SIDE_EFFECTS (ref)
3184 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3185 return ref;
3188 else if (!(complain & tf_error))
3189 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3191 /* `pointer' won't be an error_mark_node if we were given a
3192 pointer to member, so it's cool to check for this here. */
3193 else if (TYPE_PTRMEM_P (type))
3194 switch (errorstring)
3196 case RO_ARRAY_INDEXING:
3197 error ("invalid use of array indexing on pointer to member");
3198 break;
3199 case RO_UNARY_STAR:
3200 error ("invalid use of unary %<*%> on pointer to member");
3201 break;
3202 case RO_IMPLICIT_CONVERSION:
3203 error ("invalid use of implicit conversion on pointer to member");
3204 break;
3205 case RO_ARROW_STAR:
3206 error ("left hand operand of %<->*%> must be a pointer to class, "
3207 "but is a pointer to member of type %qT", type);
3208 break;
3209 default:
3210 gcc_unreachable ();
3212 else if (pointer != error_mark_node)
3213 invalid_indirection_error (input_location, type, errorstring);
3215 return error_mark_node;
3218 /* Entry point used by c-common, which expects folding. */
3220 tree
3221 build_indirect_ref (location_t /*loc*/,
3222 tree ptr, ref_operator errorstring)
3224 return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3227 /* Entry point used by internal indirection needs that don't correspond to any
3228 syntactic construct. */
3230 tree
3231 cp_build_fold_indirect_ref (tree pointer)
3233 return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3236 /* Entry point used by indirection needs that correspond to some syntactic
3237 construct. */
3239 tree
3240 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3241 tsubst_flags_t complain)
3243 return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3246 /* This handles expressions of the form "a[i]", which denotes
3247 an array reference.
3249 This is logically equivalent in C to *(a+i), but we may do it differently.
3250 If A is a variable or a member, we generate a primitive ARRAY_REF.
3251 This avoids forcing the array out of registers, and can work on
3252 arrays that are not lvalues (for example, members of structures returned
3253 by functions).
3255 If INDEX is of some user-defined type, it must be converted to
3256 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3257 will inherit the type of the array, which will be some pointer type.
3259 LOC is the location to use in building the array reference. */
3261 tree
3262 cp_build_array_ref (location_t loc, tree array, tree idx,
3263 tsubst_flags_t complain)
3265 tree ret;
3267 if (idx == 0)
3269 if (complain & tf_error)
3270 error_at (loc, "subscript missing in array reference");
3271 return error_mark_node;
3274 if (TREE_TYPE (array) == error_mark_node
3275 || TREE_TYPE (idx) == error_mark_node)
3276 return error_mark_node;
3278 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3279 inside it. */
3280 switch (TREE_CODE (array))
3282 case COMPOUND_EXPR:
3284 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3285 complain);
3286 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3287 TREE_OPERAND (array, 0), value);
3288 SET_EXPR_LOCATION (ret, loc);
3289 return ret;
3292 case COND_EXPR:
3293 ret = build_conditional_expr
3294 (loc, TREE_OPERAND (array, 0),
3295 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3296 complain),
3297 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3298 complain),
3299 complain);
3300 protected_set_expr_location (ret, loc);
3301 return ret;
3303 default:
3304 break;
3307 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3309 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3311 tree rval, type;
3313 warn_array_subscript_with_type_char (loc, idx);
3315 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3317 if (complain & tf_error)
3318 error_at (loc, "array subscript is not an integer");
3319 return error_mark_node;
3322 /* Apply integral promotions *after* noticing character types.
3323 (It is unclear why we do these promotions -- the standard
3324 does not say that we should. In fact, the natural thing would
3325 seem to be to convert IDX to ptrdiff_t; we're performing
3326 pointer arithmetic.) */
3327 idx = cp_perform_integral_promotions (idx, complain);
3329 /* An array that is indexed by a non-constant
3330 cannot be stored in a register; we must be able to do
3331 address arithmetic on its address.
3332 Likewise an array of elements of variable size. */
3333 if (TREE_CODE (idx) != INTEGER_CST
3334 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3335 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3336 != INTEGER_CST)))
3338 if (!cxx_mark_addressable (array, true))
3339 return error_mark_node;
3342 /* An array that is indexed by a constant value which is not within
3343 the array bounds cannot be stored in a register either; because we
3344 would get a crash in store_bit_field/extract_bit_field when trying
3345 to access a non-existent part of the register. */
3346 if (TREE_CODE (idx) == INTEGER_CST
3347 && TYPE_DOMAIN (TREE_TYPE (array))
3348 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3350 if (!cxx_mark_addressable (array))
3351 return error_mark_node;
3354 /* Note in C++ it is valid to subscript a `register' array, since
3355 it is valid to take the address of something with that
3356 storage specification. */
3357 if (extra_warnings)
3359 tree foo = array;
3360 while (TREE_CODE (foo) == COMPONENT_REF)
3361 foo = TREE_OPERAND (foo, 0);
3362 if (VAR_P (foo) && DECL_REGISTER (foo)
3363 && (complain & tf_warning))
3364 warning_at (loc, OPT_Wextra,
3365 "subscripting array declared %<register%>");
3368 type = TREE_TYPE (TREE_TYPE (array));
3369 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3370 /* Array ref is const/volatile if the array elements are
3371 or if the array is.. */
3372 TREE_READONLY (rval)
3373 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3374 TREE_SIDE_EFFECTS (rval)
3375 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3376 TREE_THIS_VOLATILE (rval)
3377 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3378 ret = require_complete_type_sfinae (rval, complain);
3379 protected_set_expr_location (ret, loc);
3380 if (non_lvalue)
3381 ret = non_lvalue_loc (loc, ret);
3382 return ret;
3386 tree ar = cp_default_conversion (array, complain);
3387 tree ind = cp_default_conversion (idx, complain);
3389 /* Put the integer in IND to simplify error checking. */
3390 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3391 std::swap (ar, ind);
3393 if (ar == error_mark_node || ind == error_mark_node)
3394 return error_mark_node;
3396 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3398 if (complain & tf_error)
3399 error_at (loc, "subscripted value is neither array nor pointer");
3400 return error_mark_node;
3402 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3404 if (complain & tf_error)
3405 error_at (loc, "array subscript is not an integer");
3406 return error_mark_node;
3409 warn_array_subscript_with_type_char (loc, idx);
3411 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3412 PLUS_EXPR, ar, ind,
3413 complain),
3414 RO_ARRAY_INDEXING,
3415 complain);
3416 protected_set_expr_location (ret, loc);
3417 if (non_lvalue)
3418 ret = non_lvalue_loc (loc, ret);
3419 return ret;
3423 /* Entry point for Obj-C++. */
3425 tree
3426 build_array_ref (location_t loc, tree array, tree idx)
3428 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3431 /* Resolve a pointer to member function. INSTANCE is the object
3432 instance to use, if the member points to a virtual member.
3434 This used to avoid checking for virtual functions if basetype
3435 has no virtual functions, according to an earlier ANSI draft.
3436 With the final ISO C++ rules, such an optimization is
3437 incorrect: A pointer to a derived member can be static_cast
3438 to pointer-to-base-member, as long as the dynamic object
3439 later has the right member. So now we only do this optimization
3440 when we know the dynamic type of the object. */
3442 tree
3443 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3444 tsubst_flags_t complain)
3446 if (TREE_CODE (function) == OFFSET_REF)
3447 function = TREE_OPERAND (function, 1);
3449 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3451 tree idx, delta, e1, e2, e3, vtbl;
3452 bool nonvirtual;
3453 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3454 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3456 tree instance_ptr = *instance_ptrptr;
3457 tree instance_save_expr = 0;
3458 if (instance_ptr == error_mark_node)
3460 if (TREE_CODE (function) == PTRMEM_CST)
3462 /* Extracting the function address from a pmf is only
3463 allowed with -Wno-pmf-conversions. It only works for
3464 pmf constants. */
3465 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3466 e1 = convert (fntype, e1);
3467 return e1;
3469 else
3471 if (complain & tf_error)
3472 error ("object missing in use of %qE", function);
3473 return error_mark_node;
3477 /* True if we know that the dynamic type of the object doesn't have
3478 virtual functions, so we can assume the PFN field is a pointer. */
3479 nonvirtual = (COMPLETE_TYPE_P (basetype)
3480 && !TYPE_POLYMORPHIC_P (basetype)
3481 && resolves_to_fixed_type_p (instance_ptr, 0));
3483 /* If we don't really have an object (i.e. in an ill-formed
3484 conversion from PMF to pointer), we can't resolve virtual
3485 functions anyway. */
3486 if (!nonvirtual && is_dummy_object (instance_ptr))
3487 nonvirtual = true;
3489 if (TREE_SIDE_EFFECTS (instance_ptr))
3490 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3492 if (TREE_SIDE_EFFECTS (function))
3493 function = save_expr (function);
3495 /* Start by extracting all the information from the PMF itself. */
3496 e3 = pfn_from_ptrmemfunc (function);
3497 delta = delta_from_ptrmemfunc (function);
3498 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3499 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3501 int flag_sanitize_save;
3502 case ptrmemfunc_vbit_in_pfn:
3503 e1 = cp_build_binary_op (input_location,
3504 BIT_AND_EXPR, idx, integer_one_node,
3505 complain);
3506 idx = cp_build_binary_op (input_location,
3507 MINUS_EXPR, idx, integer_one_node,
3508 complain);
3509 if (idx == error_mark_node)
3510 return error_mark_node;
3511 break;
3513 case ptrmemfunc_vbit_in_delta:
3514 e1 = cp_build_binary_op (input_location,
3515 BIT_AND_EXPR, delta, integer_one_node,
3516 complain);
3517 /* Don't instrument the RSHIFT_EXPR we're about to create because
3518 we're going to use DELTA number of times, and that wouldn't play
3519 well with SAVE_EXPRs therein. */
3520 flag_sanitize_save = flag_sanitize;
3521 flag_sanitize = 0;
3522 delta = cp_build_binary_op (input_location,
3523 RSHIFT_EXPR, delta, integer_one_node,
3524 complain);
3525 flag_sanitize = flag_sanitize_save;
3526 if (delta == error_mark_node)
3527 return error_mark_node;
3528 break;
3530 default:
3531 gcc_unreachable ();
3534 if (e1 == error_mark_node)
3535 return error_mark_node;
3537 /* Convert down to the right base before using the instance. A
3538 special case is that in a pointer to member of class C, C may
3539 be incomplete. In that case, the function will of course be
3540 a member of C, and no conversion is required. In fact,
3541 lookup_base will fail in that case, because incomplete
3542 classes do not have BINFOs. */
3543 if (!same_type_ignoring_top_level_qualifiers_p
3544 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3546 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3547 basetype, ba_check, NULL, complain);
3548 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3549 1, complain);
3550 if (instance_ptr == error_mark_node)
3551 return error_mark_node;
3553 /* ...and then the delta in the PMF. */
3554 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3556 /* Hand back the adjusted 'this' argument to our caller. */
3557 *instance_ptrptr = instance_ptr;
3559 if (nonvirtual)
3560 /* Now just return the pointer. */
3561 return e3;
3563 /* Next extract the vtable pointer from the object. */
3564 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3565 instance_ptr);
3566 vtbl = cp_build_fold_indirect_ref (vtbl);
3567 if (vtbl == error_mark_node)
3568 return error_mark_node;
3570 /* Finally, extract the function pointer from the vtable. */
3571 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3572 e2 = cp_build_fold_indirect_ref (e2);
3573 if (e2 == error_mark_node)
3574 return error_mark_node;
3575 TREE_CONSTANT (e2) = 1;
3577 /* When using function descriptors, the address of the
3578 vtable entry is treated as a function pointer. */
3579 if (TARGET_VTABLE_USES_DESCRIPTORS)
3580 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3581 cp_build_addr_expr (e2, complain));
3583 e2 = fold_convert (TREE_TYPE (e3), e2);
3584 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3585 if (e1 == error_mark_node)
3586 return error_mark_node;
3588 /* Make sure this doesn't get evaluated first inside one of the
3589 branches of the COND_EXPR. */
3590 if (instance_save_expr)
3591 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3592 instance_save_expr, e1);
3594 function = e1;
3596 return function;
3599 /* Used by the C-common bits. */
3600 tree
3601 build_function_call (location_t /*loc*/,
3602 tree function, tree params)
3604 return cp_build_function_call (function, params, tf_warning_or_error);
3607 /* Used by the C-common bits. */
3608 tree
3609 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3610 tree function, vec<tree, va_gc> *params,
3611 vec<tree, va_gc> * /*origtypes*/)
3613 vec<tree, va_gc> *orig_params = params;
3614 tree ret = cp_build_function_call_vec (function, &params,
3615 tf_warning_or_error);
3617 /* cp_build_function_call_vec can reallocate PARAMS by adding
3618 default arguments. That should never happen here. Verify
3619 that. */
3620 gcc_assert (params == orig_params);
3622 return ret;
3625 /* Build a function call using a tree list of arguments. */
3627 static tree
3628 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3630 vec<tree, va_gc> *vec;
3631 tree ret;
3633 vec = make_tree_vector ();
3634 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3635 vec_safe_push (vec, TREE_VALUE (params));
3636 ret = cp_build_function_call_vec (function, &vec, complain);
3637 release_tree_vector (vec);
3638 return ret;
3641 /* Build a function call using varargs. */
3643 tree
3644 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3646 vec<tree, va_gc> *vec;
3647 va_list args;
3648 tree ret, t;
3650 vec = make_tree_vector ();
3651 va_start (args, complain);
3652 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3653 vec_safe_push (vec, t);
3654 va_end (args);
3655 ret = cp_build_function_call_vec (function, &vec, complain);
3656 release_tree_vector (vec);
3657 return ret;
3660 /* Build a function call using a vector of arguments. PARAMS may be
3661 NULL if there are no parameters. This changes the contents of
3662 PARAMS. */
3664 tree
3665 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3666 tsubst_flags_t complain)
3668 tree fntype, fndecl;
3669 int is_method;
3670 tree original = function;
3671 int nargs;
3672 tree *argarray;
3673 tree parm_types;
3674 vec<tree, va_gc> *allocated = NULL;
3675 tree ret;
3677 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3678 expressions, like those used for ObjC messenger dispatches. */
3679 if (params != NULL && !vec_safe_is_empty (*params))
3680 function = objc_rewrite_function_call (function, (**params)[0]);
3682 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3683 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3684 if (TREE_CODE (function) == NOP_EXPR
3685 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3686 function = TREE_OPERAND (function, 0);
3688 if (TREE_CODE (function) == FUNCTION_DECL)
3690 /* If the function is a non-template member function
3691 or a non-template friend, then we need to check the
3692 constraints.
3694 Note that if overload resolution failed with a single
3695 candidate this function will be used to explicitly diagnose
3696 the failure for the single call expression. The check is
3697 technically redundant since we also would have failed in
3698 add_function_candidate. */
3699 if (flag_concepts
3700 && (complain & tf_error)
3701 && !constraints_satisfied_p (function))
3703 auto_diagnostic_group d;
3704 error ("cannot call function %qD", function);
3705 location_t loc = DECL_SOURCE_LOCATION (function);
3706 diagnose_constraints (loc, function, NULL_TREE);
3707 return error_mark_node;
3710 if (!mark_used (function, complain) && !(complain & tf_error))
3711 return error_mark_node;
3712 fndecl = function;
3714 /* Convert anything with function type to a pointer-to-function. */
3715 if (DECL_MAIN_P (function))
3717 if (complain & tf_error)
3718 pedwarn (input_location, OPT_Wpedantic,
3719 "ISO C++ forbids calling %<::main%> from within program");
3720 else
3721 return error_mark_node;
3723 function = build_addr_func (function, complain);
3725 else
3727 fndecl = NULL_TREE;
3729 function = build_addr_func (function, complain);
3732 if (function == error_mark_node)
3733 return error_mark_node;
3735 fntype = TREE_TYPE (function);
3737 if (TYPE_PTRMEMFUNC_P (fntype))
3739 if (complain & tf_error)
3740 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3741 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3742 original, original);
3743 return error_mark_node;
3746 is_method = (TYPE_PTR_P (fntype)
3747 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3749 if (!(TYPE_PTRFN_P (fntype)
3750 || is_method
3751 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3753 if (complain & tf_error)
3755 if (!flag_diagnostics_show_caret)
3756 error_at (input_location,
3757 "%qE cannot be used as a function", original);
3758 else if (DECL_P (original))
3759 error_at (input_location,
3760 "%qD cannot be used as a function", original);
3761 else
3762 error_at (input_location,
3763 "expression cannot be used as a function");
3766 return error_mark_node;
3769 /* fntype now gets the type of function pointed to. */
3770 fntype = TREE_TYPE (fntype);
3771 parm_types = TYPE_ARG_TYPES (fntype);
3773 if (params == NULL)
3775 allocated = make_tree_vector ();
3776 params = &allocated;
3779 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3780 complain);
3781 if (nargs < 0)
3782 return error_mark_node;
3784 argarray = (*params)->address ();
3786 /* Check for errors in format strings and inappropriately
3787 null parameters. */
3788 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3789 nargs, argarray, NULL);
3791 ret = build_cxx_call (function, nargs, argarray, complain);
3793 if (warned_p)
3795 tree c = extract_call_expr (ret);
3796 if (TREE_CODE (c) == CALL_EXPR)
3797 TREE_NO_WARNING (c) = 1;
3800 if (allocated != NULL)
3801 release_tree_vector (allocated);
3803 return ret;
3806 /* Subroutine of convert_arguments.
3807 Print an error message about a wrong number of arguments. */
3809 static void
3810 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3812 if (fndecl)
3814 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3816 if (DECL_NAME (fndecl) == NULL_TREE
3817 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3818 error_at (loc,
3819 too_many_p
3820 ? G_("too many arguments to constructor %q#D")
3821 : G_("too few arguments to constructor %q#D"),
3822 fndecl);
3823 else
3824 error_at (loc,
3825 too_many_p
3826 ? G_("too many arguments to member function %q#D")
3827 : G_("too few arguments to member function %q#D"),
3828 fndecl);
3830 else
3831 error_at (loc,
3832 too_many_p
3833 ? G_("too many arguments to function %q#D")
3834 : G_("too few arguments to function %q#D"),
3835 fndecl);
3836 if (!DECL_IS_BUILTIN (fndecl))
3837 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3839 else
3841 if (c_dialect_objc () && objc_message_selector ())
3842 error_at (loc,
3843 too_many_p
3844 ? G_("too many arguments to method %q#D")
3845 : G_("too few arguments to method %q#D"),
3846 objc_message_selector ());
3847 else
3848 error_at (loc, too_many_p ? G_("too many arguments to function")
3849 : G_("too few arguments to function"));
3853 /* Convert the actual parameter expressions in the list VALUES to the
3854 types in the list TYPELIST. The converted expressions are stored
3855 back in the VALUES vector.
3856 If parmdecls is exhausted, or when an element has NULL as its type,
3857 perform the default conversions.
3859 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3861 This is also where warnings about wrong number of args are generated.
3863 Returns the actual number of arguments processed (which might be less
3864 than the length of the vector), or -1 on error.
3866 In C++, unspecified trailing parameters can be filled in with their
3867 default arguments, if such were specified. Do so here. */
3869 static int
3870 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3871 int flags, tsubst_flags_t complain)
3873 tree typetail;
3874 unsigned int i;
3876 /* Argument passing is always copy-initialization. */
3877 flags |= LOOKUP_ONLYCONVERTING;
3879 for (i = 0, typetail = typelist;
3880 i < vec_safe_length (*values);
3881 i++)
3883 tree type = typetail ? TREE_VALUE (typetail) : 0;
3884 tree val = (**values)[i];
3886 if (val == error_mark_node || type == error_mark_node)
3887 return -1;
3889 if (type == void_type_node)
3891 if (complain & tf_error)
3893 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3894 return i;
3896 else
3897 return -1;
3900 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3901 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3902 if (TREE_CODE (val) == NOP_EXPR
3903 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3904 && (type == 0 || !TYPE_REF_P (type)))
3905 val = TREE_OPERAND (val, 0);
3907 if (type == 0 || !TYPE_REF_P (type))
3909 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3910 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3911 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3912 val = decay_conversion (val, complain);
3915 if (val == error_mark_node)
3916 return -1;
3918 if (type != 0)
3920 /* Formal parm type is specified by a function prototype. */
3921 tree parmval;
3923 if (!COMPLETE_TYPE_P (complete_type (type)))
3925 if (complain & tf_error)
3927 if (fndecl)
3928 error ("parameter %P of %qD has incomplete type %qT",
3929 i, fndecl, type);
3930 else
3931 error ("parameter %P has incomplete type %qT", i, type);
3933 parmval = error_mark_node;
3935 else
3937 parmval = convert_for_initialization
3938 (NULL_TREE, type, val, flags,
3939 ICR_ARGPASS, fndecl, i, complain);
3940 parmval = convert_for_arg_passing (type, parmval, complain);
3943 if (parmval == error_mark_node)
3944 return -1;
3946 (**values)[i] = parmval;
3948 else
3950 if (fndecl && magic_varargs_p (fndecl))
3951 /* Don't do ellipsis conversion for __built_in_constant_p
3952 as this will result in spurious errors for non-trivial
3953 types. */
3954 val = require_complete_type_sfinae (val, complain);
3955 else
3956 val = convert_arg_to_ellipsis (val, complain);
3958 (**values)[i] = val;
3961 if (typetail)
3962 typetail = TREE_CHAIN (typetail);
3965 if (typetail != 0 && typetail != void_list_node)
3967 /* See if there are default arguments that can be used. Because
3968 we hold default arguments in the FUNCTION_TYPE (which is so
3969 wrong), we can see default parameters here from deduced
3970 contexts (and via typeof) for indirect function calls.
3971 Fortunately we know whether we have a function decl to
3972 provide default arguments in a language conformant
3973 manner. */
3974 if (fndecl && TREE_PURPOSE (typetail)
3975 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3977 for (; typetail != void_list_node; ++i)
3979 /* After DR777, with explicit template args we can end up with a
3980 default argument followed by no default argument. */
3981 if (!TREE_PURPOSE (typetail))
3982 break;
3983 tree parmval
3984 = convert_default_arg (TREE_VALUE (typetail),
3985 TREE_PURPOSE (typetail),
3986 fndecl, i, complain);
3988 if (parmval == error_mark_node)
3989 return -1;
3991 vec_safe_push (*values, parmval);
3992 typetail = TREE_CHAIN (typetail);
3993 /* ends with `...'. */
3994 if (typetail == NULL_TREE)
3995 break;
3999 if (typetail && typetail != void_list_node)
4001 if (complain & tf_error)
4002 error_args_num (input_location, fndecl, /*too_many_p=*/false);
4003 return -1;
4007 return (int) i;
4010 /* Build a binary-operation expression, after performing default
4011 conversions on the operands. CODE is the kind of expression to
4012 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4013 are the tree codes which correspond to ARG1 and ARG2 when issuing
4014 warnings about possibly misplaced parentheses. They may differ
4015 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4016 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4017 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4018 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4019 ARG2_CODE as ERROR_MARK. */
4021 tree
4022 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4023 enum tree_code arg1_code, tree arg2,
4024 enum tree_code arg2_code, tree *overload_p,
4025 tsubst_flags_t complain)
4027 tree orig_arg1;
4028 tree orig_arg2;
4029 tree expr;
4030 tree overload = NULL_TREE;
4032 orig_arg1 = arg1;
4033 orig_arg2 = arg2;
4035 if (processing_template_decl)
4037 if (type_dependent_expression_p (arg1)
4038 || type_dependent_expression_p (arg2))
4039 return build_min_nt_loc (loc, code, arg1, arg2);
4040 arg1 = build_non_dependent_expr (arg1);
4041 arg2 = build_non_dependent_expr (arg2);
4044 if (code == DOTSTAR_EXPR)
4045 expr = build_m_component_ref (arg1, arg2, complain);
4046 else
4047 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4048 &overload, complain);
4050 if (overload_p != NULL)
4051 *overload_p = overload;
4053 /* Check for cases such as x+y<<z which users are likely to
4054 misinterpret. But don't warn about obj << x + y, since that is a
4055 common idiom for I/O. */
4056 if (warn_parentheses
4057 && (complain & tf_warning)
4058 && !processing_template_decl
4059 && !error_operand_p (arg1)
4060 && !error_operand_p (arg2)
4061 && (code != LSHIFT_EXPR
4062 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4063 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4064 arg2_code, orig_arg2);
4066 if (processing_template_decl && expr != error_mark_node)
4068 if (overload != NULL_TREE)
4069 return (build_min_non_dep_op_overload
4070 (code, expr, overload, orig_arg1, orig_arg2));
4072 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4075 return expr;
4078 /* Build and return an ARRAY_REF expression. */
4080 tree
4081 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4082 tsubst_flags_t complain)
4084 tree orig_arg1 = arg1;
4085 tree orig_arg2 = arg2;
4086 tree expr;
4087 tree overload = NULL_TREE;
4089 if (processing_template_decl)
4091 if (type_dependent_expression_p (arg1)
4092 || type_dependent_expression_p (arg2))
4093 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4094 NULL_TREE, NULL_TREE);
4095 arg1 = build_non_dependent_expr (arg1);
4096 arg2 = build_non_dependent_expr (arg2);
4099 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4100 NULL_TREE, &overload, complain);
4102 if (processing_template_decl && expr != error_mark_node)
4104 if (overload != NULL_TREE)
4105 return (build_min_non_dep_op_overload
4106 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4108 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4109 NULL_TREE, NULL_TREE);
4111 return expr;
4114 /* Return whether OP is an expression of enum type cast to integer
4115 type. In C++ even unsigned enum types are cast to signed integer
4116 types. We do not want to issue warnings about comparisons between
4117 signed and unsigned types when one of the types is an enum type.
4118 Those warnings are always false positives in practice. */
4120 static bool
4121 enum_cast_to_int (tree op)
4123 if (CONVERT_EXPR_P (op)
4124 && TREE_TYPE (op) == integer_type_node
4125 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4126 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4127 return true;
4129 /* The cast may have been pushed into a COND_EXPR. */
4130 if (TREE_CODE (op) == COND_EXPR)
4131 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4132 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4134 return false;
4137 /* For the c-common bits. */
4138 tree
4139 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4140 bool /*convert_p*/)
4142 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4145 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4146 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4148 static tree
4149 build_vec_cmp (tree_code code, tree type,
4150 tree arg0, tree arg1)
4152 tree zero_vec = build_zero_cst (type);
4153 tree minus_one_vec = build_minus_one_cst (type);
4154 tree cmp_type = build_same_sized_truth_vector_type(type);
4155 tree cmp = build2 (code, cmp_type, arg0, arg1);
4156 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4159 /* Possibly warn about an address never being NULL. */
4161 static void
4162 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4164 if (!warn_address
4165 || (complain & tf_warning) == 0
4166 || c_inhibit_evaluation_warnings != 0
4167 || TREE_NO_WARNING (op))
4168 return;
4170 tree cop = fold_non_dependent_expr (op, complain);
4172 if (TREE_CODE (cop) == ADDR_EXPR
4173 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4174 && !TREE_NO_WARNING (cop))
4175 warning_at (location, OPT_Waddress, "the address of %qD will never "
4176 "be NULL", TREE_OPERAND (cop, 0));
4178 if (CONVERT_EXPR_P (op)
4179 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4181 tree inner_op = op;
4182 STRIP_NOPS (inner_op);
4184 if (DECL_P (inner_op))
4185 warning_at (location, OPT_Waddress,
4186 "the compiler can assume that the address of "
4187 "%qD will never be NULL", inner_op);
4191 /* Build a binary-operation expression without default conversions.
4192 CODE is the kind of expression to build.
4193 LOCATION is the location_t of the operator in the source code.
4194 This function differs from `build' in several ways:
4195 the data type of the result is computed and recorded in it,
4196 warnings are generated if arg data types are invalid,
4197 special handling for addition and subtraction of pointers is known,
4198 and some optimization is done (operations on narrow ints
4199 are done in the narrower type when that gives the same result).
4200 Constant folding is also done before the result is returned.
4202 Note that the operands will never have enumeral types
4203 because either they have just had the default conversions performed
4204 or they have both just been converted to some other type in which
4205 the arithmetic is to be done.
4207 C++: must do special pointer arithmetic when implementing
4208 multiple inheritance, and deal with pointer to member functions. */
4210 tree
4211 cp_build_binary_op (location_t location,
4212 enum tree_code code, tree orig_op0, tree orig_op1,
4213 tsubst_flags_t complain)
4215 tree op0, op1;
4216 enum tree_code code0, code1;
4217 tree type0, type1;
4218 const char *invalid_op_diag;
4220 /* Expression code to give to the expression when it is built.
4221 Normally this is CODE, which is what the caller asked for,
4222 but in some special cases we change it. */
4223 enum tree_code resultcode = code;
4225 /* Data type in which the computation is to be performed.
4226 In the simplest cases this is the common type of the arguments. */
4227 tree result_type = NULL_TREE;
4229 /* Nonzero means operands have already been type-converted
4230 in whatever way is necessary.
4231 Zero means they need to be converted to RESULT_TYPE. */
4232 int converted = 0;
4234 /* Nonzero means create the expression with this type, rather than
4235 RESULT_TYPE. */
4236 tree build_type = 0;
4238 /* Nonzero means after finally constructing the expression
4239 convert it to this type. */
4240 tree final_type = 0;
4242 tree result, result_ovl;
4244 /* Nonzero if this is an operation like MIN or MAX which can
4245 safely be computed in short if both args are promoted shorts.
4246 Also implies COMMON.
4247 -1 indicates a bitwise operation; this makes a difference
4248 in the exact conditions for when it is safe to do the operation
4249 in a narrower mode. */
4250 int shorten = 0;
4252 /* Nonzero if this is a comparison operation;
4253 if both args are promoted shorts, compare the original shorts.
4254 Also implies COMMON. */
4255 int short_compare = 0;
4257 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4258 int common = 0;
4260 /* True if both operands have arithmetic type. */
4261 bool arithmetic_types_p;
4263 /* Apply default conversions. */
4264 op0 = orig_op0;
4265 op1 = orig_op1;
4267 /* Remember whether we're doing / or %. */
4268 bool doing_div_or_mod = false;
4270 /* Remember whether we're doing << or >>. */
4271 bool doing_shift = false;
4273 /* Tree holding instrumentation expression. */
4274 tree instrument_expr = NULL_TREE;
4276 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4277 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4278 || code == TRUTH_XOR_EXPR)
4280 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4281 op0 = decay_conversion (op0, complain);
4282 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4283 op1 = decay_conversion (op1, complain);
4285 else
4287 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4288 op0 = cp_default_conversion (op0, complain);
4289 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4290 op1 = cp_default_conversion (op1, complain);
4293 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4294 STRIP_TYPE_NOPS (op0);
4295 STRIP_TYPE_NOPS (op1);
4297 /* DTRT if one side is an overloaded function, but complain about it. */
4298 if (type_unknown_p (op0))
4300 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4301 if (t != error_mark_node)
4303 if (complain & tf_error)
4304 permerror (input_location, "assuming cast to type %qT from overloaded function",
4305 TREE_TYPE (t));
4306 op0 = t;
4309 if (type_unknown_p (op1))
4311 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4312 if (t != error_mark_node)
4314 if (complain & tf_error)
4315 permerror (input_location, "assuming cast to type %qT from overloaded function",
4316 TREE_TYPE (t));
4317 op1 = t;
4321 type0 = TREE_TYPE (op0);
4322 type1 = TREE_TYPE (op1);
4324 /* The expression codes of the data types of the arguments tell us
4325 whether the arguments are integers, floating, pointers, etc. */
4326 code0 = TREE_CODE (type0);
4327 code1 = TREE_CODE (type1);
4329 /* If an error was already reported for one of the arguments,
4330 avoid reporting another error. */
4331 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4332 return error_mark_node;
4334 if ((invalid_op_diag
4335 = targetm.invalid_binary_op (code, type0, type1)))
4337 if (complain & tf_error)
4338 error (invalid_op_diag);
4339 return error_mark_node;
4342 /* Issue warnings about peculiar, but valid, uses of NULL. */
4343 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4344 /* It's reasonable to use pointer values as operands of &&
4345 and ||, so NULL is no exception. */
4346 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4347 && ( /* Both are NULL (or 0) and the operation was not a
4348 comparison or a pointer subtraction. */
4349 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4350 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4351 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4352 || (!null_ptr_cst_p (orig_op0)
4353 && !TYPE_PTR_OR_PTRMEM_P (type0))
4354 || (!null_ptr_cst_p (orig_op1)
4355 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4356 && (complain & tf_warning))
4358 source_location loc =
4359 expansion_point_location_if_in_system_header (input_location);
4361 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4364 /* In case when one of the operands of the binary operation is
4365 a vector and another is a scalar -- convert scalar to vector. */
4366 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4368 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4369 complain & tf_error);
4371 switch (convert_flag)
4373 case stv_error:
4374 return error_mark_node;
4375 case stv_firstarg:
4377 op0 = convert (TREE_TYPE (type1), op0);
4378 op0 = save_expr (op0);
4379 op0 = build_vector_from_val (type1, op0);
4380 type0 = TREE_TYPE (op0);
4381 code0 = TREE_CODE (type0);
4382 converted = 1;
4383 break;
4385 case stv_secondarg:
4387 op1 = convert (TREE_TYPE (type0), op1);
4388 op1 = save_expr (op1);
4389 op1 = build_vector_from_val (type0, op1);
4390 type1 = TREE_TYPE (op1);
4391 code1 = TREE_CODE (type1);
4392 converted = 1;
4393 break;
4395 default:
4396 break;
4400 switch (code)
4402 case MINUS_EXPR:
4403 /* Subtraction of two similar pointers.
4404 We must subtract them as integers, then divide by object size. */
4405 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4406 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4407 TREE_TYPE (type1)))
4409 result = pointer_diff (location, op0, op1,
4410 common_pointer_type (type0, type1), complain,
4411 &instrument_expr);
4412 if (instrument_expr != NULL)
4413 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4414 instrument_expr, result);
4416 return result;
4418 /* In all other cases except pointer - int, the usual arithmetic
4419 rules apply. */
4420 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4422 common = 1;
4423 break;
4425 /* The pointer - int case is just like pointer + int; fall
4426 through. */
4427 gcc_fallthrough ();
4428 case PLUS_EXPR:
4429 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4430 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4432 tree ptr_operand;
4433 tree int_operand;
4434 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4435 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4436 if (processing_template_decl)
4438 result_type = TREE_TYPE (ptr_operand);
4439 break;
4441 return cp_pointer_int_sum (location, code,
4442 ptr_operand,
4443 int_operand,
4444 complain);
4446 common = 1;
4447 break;
4449 case MULT_EXPR:
4450 common = 1;
4451 break;
4453 case TRUNC_DIV_EXPR:
4454 case CEIL_DIV_EXPR:
4455 case FLOOR_DIV_EXPR:
4456 case ROUND_DIV_EXPR:
4457 case EXACT_DIV_EXPR:
4458 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4460 tree type0 = TREE_OPERAND (op0, 0);
4461 tree type1 = TREE_OPERAND (op1, 0);
4462 tree first_arg = type0;
4463 if (!TYPE_P (type0))
4464 type0 = TREE_TYPE (type0);
4465 if (!TYPE_P (type1))
4466 type1 = TREE_TYPE (type1);
4467 if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4468 && !(TREE_CODE (first_arg) == PARM_DECL
4469 && DECL_ARRAY_PARAMETER_P (first_arg)
4470 && warn_sizeof_array_argument)
4471 && (complain & tf_warning))
4473 auto_diagnostic_group d;
4474 if (warning_at (location, OPT_Wsizeof_pointer_div,
4475 "division %<sizeof (%T) / sizeof (%T)%> does "
4476 "not compute the number of array elements",
4477 type0, type1))
4478 if (DECL_P (first_arg))
4479 inform (DECL_SOURCE_LOCATION (first_arg),
4480 "first %<sizeof%> operand was declared here");
4484 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4485 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4486 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4487 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4489 enum tree_code tcode0 = code0, tcode1 = code1;
4490 tree cop1 = fold_non_dependent_expr (op1, complain);
4491 doing_div_or_mod = true;
4492 warn_for_div_by_zero (location, cop1);
4494 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4495 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4496 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4497 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4499 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4500 resultcode = RDIV_EXPR;
4501 else
4502 /* When dividing two signed integers, we have to promote to int.
4503 unless we divide by a constant != -1. Note that default
4504 conversion will have been performed on the operands at this
4505 point, so we have to dig out the original type to find out if
4506 it was unsigned. */
4507 shorten = ((TREE_CODE (op0) == NOP_EXPR
4508 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4509 || (TREE_CODE (op1) == INTEGER_CST
4510 && ! integer_all_onesp (op1)));
4512 common = 1;
4514 break;
4516 case BIT_AND_EXPR:
4517 case BIT_IOR_EXPR:
4518 case BIT_XOR_EXPR:
4519 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4520 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4521 && !VECTOR_FLOAT_TYPE_P (type0)
4522 && !VECTOR_FLOAT_TYPE_P (type1)))
4523 shorten = -1;
4524 break;
4526 case TRUNC_MOD_EXPR:
4527 case FLOOR_MOD_EXPR:
4529 tree cop1 = fold_non_dependent_expr (op1, complain);
4530 doing_div_or_mod = true;
4531 warn_for_div_by_zero (location, cop1);
4534 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4535 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4536 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4537 common = 1;
4538 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4540 /* Although it would be tempting to shorten always here, that loses
4541 on some targets, since the modulo instruction is undefined if the
4542 quotient can't be represented in the computation mode. We shorten
4543 only if unsigned or if dividing by something we know != -1. */
4544 shorten = ((TREE_CODE (op0) == NOP_EXPR
4545 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4546 || (TREE_CODE (op1) == INTEGER_CST
4547 && ! integer_all_onesp (op1)));
4548 common = 1;
4550 break;
4552 case TRUTH_ANDIF_EXPR:
4553 case TRUTH_ORIF_EXPR:
4554 case TRUTH_AND_EXPR:
4555 case TRUTH_OR_EXPR:
4556 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4558 if (!COMPARISON_CLASS_P (op1))
4559 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4560 build_zero_cst (type1), complain);
4561 if (code == TRUTH_ANDIF_EXPR)
4563 tree z = build_zero_cst (TREE_TYPE (op1));
4564 return build_conditional_expr (location, op0, op1, z, complain);
4566 else if (code == TRUTH_ORIF_EXPR)
4568 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4569 return build_conditional_expr (location, op0, m1, op1, complain);
4571 else
4572 gcc_unreachable ();
4574 if (VECTOR_TYPE_P (type0))
4576 if (!COMPARISON_CLASS_P (op0))
4577 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4578 build_zero_cst (type0), complain);
4579 if (!VECTOR_TYPE_P (type1))
4581 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4582 tree z = build_zero_cst (TREE_TYPE (op0));
4583 op1 = build_conditional_expr (location, op1, m1, z, complain);
4585 else if (!COMPARISON_CLASS_P (op1))
4586 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4587 build_zero_cst (type1), complain);
4589 if (code == TRUTH_ANDIF_EXPR)
4590 code = BIT_AND_EXPR;
4591 else if (code == TRUTH_ORIF_EXPR)
4592 code = BIT_IOR_EXPR;
4593 else
4594 gcc_unreachable ();
4596 return cp_build_binary_op (location, code, op0, op1, complain);
4599 result_type = boolean_type_node;
4600 break;
4602 /* Shift operations: result has same type as first operand;
4603 always convert second operand to int.
4604 Also set SHORT_SHIFT if shifting rightward. */
4606 case RSHIFT_EXPR:
4607 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4608 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4610 result_type = type0;
4611 converted = 1;
4613 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4614 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4615 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4616 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4617 TYPE_VECTOR_SUBPARTS (type1)))
4619 result_type = type0;
4620 converted = 1;
4622 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4624 tree const_op1 = fold_non_dependent_expr (op1, complain);
4625 if (TREE_CODE (const_op1) != INTEGER_CST)
4626 const_op1 = op1;
4627 result_type = type0;
4628 doing_shift = true;
4629 if (TREE_CODE (const_op1) == INTEGER_CST)
4631 if (tree_int_cst_lt (const_op1, integer_zero_node))
4633 if ((complain & tf_warning)
4634 && c_inhibit_evaluation_warnings == 0)
4635 warning (OPT_Wshift_count_negative,
4636 "right shift count is negative");
4638 else
4640 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4641 && (complain & tf_warning)
4642 && c_inhibit_evaluation_warnings == 0)
4643 warning (OPT_Wshift_count_overflow,
4644 "right shift count >= width of type");
4647 /* Avoid converting op1 to result_type later. */
4648 converted = 1;
4650 break;
4652 case LSHIFT_EXPR:
4653 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4654 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4656 result_type = type0;
4657 converted = 1;
4659 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4660 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4661 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4662 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4663 TYPE_VECTOR_SUBPARTS (type1)))
4665 result_type = type0;
4666 converted = 1;
4668 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4670 tree const_op0 = fold_non_dependent_expr (op0, complain);
4671 if (TREE_CODE (const_op0) != INTEGER_CST)
4672 const_op0 = op0;
4673 tree const_op1 = fold_non_dependent_expr (op1, complain);
4674 if (TREE_CODE (const_op1) != INTEGER_CST)
4675 const_op1 = op1;
4676 result_type = type0;
4677 doing_shift = true;
4678 if (TREE_CODE (const_op0) == INTEGER_CST
4679 && tree_int_cst_sgn (const_op0) < 0
4680 && (complain & tf_warning)
4681 && c_inhibit_evaluation_warnings == 0)
4682 warning (OPT_Wshift_negative_value,
4683 "left shift of negative value");
4684 if (TREE_CODE (const_op1) == INTEGER_CST)
4686 if (tree_int_cst_lt (const_op1, integer_zero_node))
4688 if ((complain & tf_warning)
4689 && c_inhibit_evaluation_warnings == 0)
4690 warning (OPT_Wshift_count_negative,
4691 "left shift count is negative");
4693 else if (compare_tree_int (const_op1,
4694 TYPE_PRECISION (type0)) >= 0)
4696 if ((complain & tf_warning)
4697 && c_inhibit_evaluation_warnings == 0)
4698 warning (OPT_Wshift_count_overflow,
4699 "left shift count >= width of type");
4701 else if (TREE_CODE (const_op0) == INTEGER_CST
4702 && (complain & tf_warning))
4703 maybe_warn_shift_overflow (location, const_op0, const_op1);
4705 /* Avoid converting op1 to result_type later. */
4706 converted = 1;
4708 break;
4710 case RROTATE_EXPR:
4711 case LROTATE_EXPR:
4712 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4714 result_type = type0;
4715 if (TREE_CODE (op1) == INTEGER_CST)
4717 if (tree_int_cst_lt (op1, integer_zero_node))
4719 if (complain & tf_warning)
4720 warning (0, (code == LROTATE_EXPR)
4721 ? G_("left rotate count is negative")
4722 : G_("right rotate count is negative"));
4724 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4726 if (complain & tf_warning)
4727 warning (0, (code == LROTATE_EXPR)
4728 ? G_("left rotate count >= width of type")
4729 : G_("right rotate count >= width of type"));
4732 /* Convert the shift-count to an integer, regardless of
4733 size of value being shifted. */
4734 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4735 op1 = cp_convert (integer_type_node, op1, complain);
4737 break;
4739 case EQ_EXPR:
4740 case NE_EXPR:
4741 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4742 goto vector_compare;
4743 if ((complain & tf_warning)
4744 && c_inhibit_evaluation_warnings == 0
4745 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4746 warning (OPT_Wfloat_equal,
4747 "comparing floating point with == or != is unsafe");
4748 if ((complain & tf_warning)
4749 && ((TREE_CODE (orig_op0) == STRING_CST
4750 && !integer_zerop (cp_fully_fold (op1)))
4751 || (TREE_CODE (orig_op1) == STRING_CST
4752 && !integer_zerop (cp_fully_fold (op0)))))
4753 warning (OPT_Waddress, "comparison with string literal results "
4754 "in unspecified behavior");
4756 build_type = boolean_type_node;
4757 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4758 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4759 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4760 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4761 short_compare = 1;
4762 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4763 && null_ptr_cst_p (orig_op1))
4764 /* Handle, eg, (void*)0 (c++/43906), and more. */
4765 || (code0 == POINTER_TYPE
4766 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4768 if (TYPE_PTR_P (type1))
4769 result_type = composite_pointer_type (type0, type1, op0, op1,
4770 CPO_COMPARISON, complain);
4771 else
4772 result_type = type0;
4774 if (char_type_p (TREE_TYPE (orig_op1)))
4776 auto_diagnostic_group d;
4777 if (warning (OPT_Wpointer_compare,
4778 "comparison between pointer and zero character "
4779 "constant"))
4780 inform (input_location,
4781 "did you mean to dereference the pointer?");
4783 warn_for_null_address (location, op0, complain);
4785 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4786 && null_ptr_cst_p (orig_op0))
4787 /* Handle, eg, (void*)0 (c++/43906), and more. */
4788 || (code1 == POINTER_TYPE
4789 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4791 if (TYPE_PTR_P (type0))
4792 result_type = composite_pointer_type (type0, type1, op0, op1,
4793 CPO_COMPARISON, complain);
4794 else
4795 result_type = type1;
4797 if (char_type_p (TREE_TYPE (orig_op0)))
4799 auto_diagnostic_group d;
4800 if (warning (OPT_Wpointer_compare,
4801 "comparison between pointer and zero character "
4802 "constant"))
4803 inform (input_location,
4804 "did you mean to dereference the pointer?");
4806 warn_for_null_address (location, op1, complain);
4808 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4809 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4810 result_type = composite_pointer_type (type0, type1, op0, op1,
4811 CPO_COMPARISON, complain);
4812 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4813 /* One of the operands must be of nullptr_t type. */
4814 result_type = TREE_TYPE (nullptr_node);
4815 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4817 result_type = type0;
4818 if (complain & tf_error)
4819 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4820 else
4821 return error_mark_node;
4823 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4825 result_type = type1;
4826 if (complain & tf_error)
4827 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4828 else
4829 return error_mark_node;
4831 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4833 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4834 == ptrmemfunc_vbit_in_delta)
4836 tree pfn0, delta0, e1, e2;
4838 if (TREE_SIDE_EFFECTS (op0))
4839 op0 = save_expr (op0);
4841 pfn0 = pfn_from_ptrmemfunc (op0);
4842 delta0 = delta_from_ptrmemfunc (op0);
4843 e1 = cp_build_binary_op (location,
4844 EQ_EXPR,
4845 pfn0,
4846 build_zero_cst (TREE_TYPE (pfn0)),
4847 complain);
4848 e2 = cp_build_binary_op (location,
4849 BIT_AND_EXPR,
4850 delta0,
4851 integer_one_node,
4852 complain);
4854 if (complain & tf_warning)
4855 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4857 e2 = cp_build_binary_op (location,
4858 EQ_EXPR, e2, integer_zero_node,
4859 complain);
4860 op0 = cp_build_binary_op (location,
4861 TRUTH_ANDIF_EXPR, e1, e2,
4862 complain);
4863 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4865 else
4867 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4868 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4870 result_type = TREE_TYPE (op0);
4872 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4873 return cp_build_binary_op (location, code, op1, op0, complain);
4874 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4876 tree type;
4877 /* E will be the final comparison. */
4878 tree e;
4879 /* E1 and E2 are for scratch. */
4880 tree e1;
4881 tree e2;
4882 tree pfn0;
4883 tree pfn1;
4884 tree delta0;
4885 tree delta1;
4887 type = composite_pointer_type (type0, type1, op0, op1,
4888 CPO_COMPARISON, complain);
4890 if (!same_type_p (TREE_TYPE (op0), type))
4891 op0 = cp_convert_and_check (type, op0, complain);
4892 if (!same_type_p (TREE_TYPE (op1), type))
4893 op1 = cp_convert_and_check (type, op1, complain);
4895 if (op0 == error_mark_node || op1 == error_mark_node)
4896 return error_mark_node;
4898 if (TREE_SIDE_EFFECTS (op0))
4899 op0 = save_expr (op0);
4900 if (TREE_SIDE_EFFECTS (op1))
4901 op1 = save_expr (op1);
4903 pfn0 = pfn_from_ptrmemfunc (op0);
4904 pfn0 = cp_fully_fold (pfn0);
4905 /* Avoid -Waddress warnings (c++/64877). */
4906 if (TREE_CODE (pfn0) == ADDR_EXPR)
4907 TREE_NO_WARNING (pfn0) = 1;
4908 pfn1 = pfn_from_ptrmemfunc (op1);
4909 pfn1 = cp_fully_fold (pfn1);
4910 delta0 = delta_from_ptrmemfunc (op0);
4911 delta1 = delta_from_ptrmemfunc (op1);
4912 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4913 == ptrmemfunc_vbit_in_delta)
4915 /* We generate:
4917 (op0.pfn == op1.pfn
4918 && ((op0.delta == op1.delta)
4919 || (!op0.pfn && op0.delta & 1 == 0
4920 && op1.delta & 1 == 0))
4922 The reason for the `!op0.pfn' bit is that a NULL
4923 pointer-to-member is any member with a zero PFN and
4924 LSB of the DELTA field is 0. */
4926 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4927 delta0,
4928 integer_one_node,
4929 complain);
4930 e1 = cp_build_binary_op (location,
4931 EQ_EXPR, e1, integer_zero_node,
4932 complain);
4933 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4934 delta1,
4935 integer_one_node,
4936 complain);
4937 e2 = cp_build_binary_op (location,
4938 EQ_EXPR, e2, integer_zero_node,
4939 complain);
4940 e1 = cp_build_binary_op (location,
4941 TRUTH_ANDIF_EXPR, e2, e1,
4942 complain);
4943 e2 = cp_build_binary_op (location, EQ_EXPR,
4944 pfn0,
4945 build_zero_cst (TREE_TYPE (pfn0)),
4946 complain);
4947 e2 = cp_build_binary_op (location,
4948 TRUTH_ANDIF_EXPR, e2, e1, complain);
4949 e1 = cp_build_binary_op (location,
4950 EQ_EXPR, delta0, delta1, complain);
4951 e1 = cp_build_binary_op (location,
4952 TRUTH_ORIF_EXPR, e1, e2, complain);
4954 else
4956 /* We generate:
4958 (op0.pfn == op1.pfn
4959 && (!op0.pfn || op0.delta == op1.delta))
4961 The reason for the `!op0.pfn' bit is that a NULL
4962 pointer-to-member is any member with a zero PFN; the
4963 DELTA field is unspecified. */
4965 e1 = cp_build_binary_op (location,
4966 EQ_EXPR, delta0, delta1, complain);
4967 e2 = cp_build_binary_op (location,
4968 EQ_EXPR,
4969 pfn0,
4970 build_zero_cst (TREE_TYPE (pfn0)),
4971 complain);
4972 e1 = cp_build_binary_op (location,
4973 TRUTH_ORIF_EXPR, e1, e2, complain);
4975 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4976 e = cp_build_binary_op (location,
4977 TRUTH_ANDIF_EXPR, e2, e1, complain);
4978 if (code == EQ_EXPR)
4979 return e;
4980 return cp_build_binary_op (location,
4981 EQ_EXPR, e, integer_zero_node, complain);
4983 else
4985 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4986 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4987 type1));
4988 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4989 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4990 type0));
4993 break;
4995 case MAX_EXPR:
4996 case MIN_EXPR:
4997 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4998 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4999 shorten = 1;
5000 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5001 result_type = composite_pointer_type (type0, type1, op0, op1,
5002 CPO_COMPARISON, complain);
5003 break;
5005 case LE_EXPR:
5006 case GE_EXPR:
5007 case LT_EXPR:
5008 case GT_EXPR:
5009 if (TREE_CODE (orig_op0) == STRING_CST
5010 || TREE_CODE (orig_op1) == STRING_CST)
5012 if (complain & tf_warning)
5013 warning (OPT_Waddress, "comparison with string literal results "
5014 "in unspecified behavior");
5017 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5019 vector_compare:
5020 tree intt;
5021 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5022 TREE_TYPE (type1))
5023 && !vector_types_compatible_elements_p (type0, type1))
5025 if (complain & tf_error)
5027 error_at (location, "comparing vectors with different "
5028 "element types");
5029 inform (location, "operand types are %qT and %qT",
5030 type0, type1);
5032 return error_mark_node;
5035 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5036 TYPE_VECTOR_SUBPARTS (type1)))
5038 if (complain & tf_error)
5040 error_at (location, "comparing vectors with different "
5041 "number of elements");
5042 inform (location, "operand types are %qT and %qT",
5043 type0, type1);
5045 return error_mark_node;
5048 /* It's not precisely specified how the usual arithmetic
5049 conversions apply to the vector types. Here, we use
5050 the unsigned type if one of the operands is signed and
5051 the other one is unsigned. */
5052 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5054 if (!TYPE_UNSIGNED (type0))
5055 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5056 else
5057 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5058 warning_at (location, OPT_Wsign_compare, "comparison between "
5059 "types %qT and %qT", type0, type1);
5062 /* Always construct signed integer vector type. */
5063 intt = c_common_type_for_size
5064 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5065 if (!intt)
5067 if (complain & tf_error)
5068 error_at (location, "could not find an integer type "
5069 "of the same size as %qT", TREE_TYPE (type0));
5070 return error_mark_node;
5072 result_type = build_opaque_vector_type (intt,
5073 TYPE_VECTOR_SUBPARTS (type0));
5074 converted = 1;
5075 return build_vec_cmp (resultcode, result_type, op0, op1);
5077 build_type = boolean_type_node;
5078 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5079 || code0 == ENUMERAL_TYPE)
5080 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5081 || code1 == ENUMERAL_TYPE))
5082 short_compare = 1;
5083 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5084 result_type = composite_pointer_type (type0, type1, op0, op1,
5085 CPO_COMPARISON, complain);
5086 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5088 result_type = type0;
5089 if (extra_warnings && (complain & tf_warning))
5090 warning (OPT_Wextra,
5091 "ordered comparison of pointer with integer zero");
5093 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5095 result_type = type1;
5096 if (extra_warnings && (complain & tf_warning))
5097 warning (OPT_Wextra,
5098 "ordered comparison of pointer with integer zero");
5100 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5101 /* One of the operands must be of nullptr_t type. */
5102 result_type = TREE_TYPE (nullptr_node);
5103 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5105 result_type = type0;
5106 if (complain & tf_error)
5107 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5108 else
5109 return error_mark_node;
5111 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5113 result_type = type1;
5114 if (complain & tf_error)
5115 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5116 else
5117 return error_mark_node;
5120 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5121 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5123 op0 = save_expr (op0);
5124 op1 = save_expr (op1);
5126 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5127 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5130 break;
5132 case UNORDERED_EXPR:
5133 case ORDERED_EXPR:
5134 case UNLT_EXPR:
5135 case UNLE_EXPR:
5136 case UNGT_EXPR:
5137 case UNGE_EXPR:
5138 case UNEQ_EXPR:
5139 build_type = integer_type_node;
5140 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5142 if (complain & tf_error)
5143 error ("unordered comparison on non-floating point argument");
5144 return error_mark_node;
5146 common = 1;
5147 break;
5149 default:
5150 break;
5153 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5154 || code0 == ENUMERAL_TYPE)
5155 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5156 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5157 arithmetic_types_p = 1;
5158 else
5160 arithmetic_types_p = 0;
5161 /* Vector arithmetic is only allowed when both sides are vectors. */
5162 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5164 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5165 || !vector_types_compatible_elements_p (type0, type1))
5167 if (complain & tf_error)
5169 /* "location" already embeds the locations of the
5170 operands, so we don't need to add them separately
5171 to richloc. */
5172 rich_location richloc (line_table, location);
5173 binary_op_error (&richloc, code, type0, type1);
5175 return error_mark_node;
5177 arithmetic_types_p = 1;
5180 /* Determine the RESULT_TYPE, if it is not already known. */
5181 if (!result_type
5182 && arithmetic_types_p
5183 && (shorten || common || short_compare))
5185 result_type = cp_common_type (type0, type1);
5186 if (complain & tf_warning)
5187 do_warn_double_promotion (result_type, type0, type1,
5188 "implicit conversion from %qH to %qI "
5189 "to match other operand of binary "
5190 "expression",
5191 location);
5194 if (!result_type)
5196 if (complain & tf_error)
5197 error_at (location,
5198 "invalid operands of types %qT and %qT to binary %qO",
5199 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5200 return error_mark_node;
5203 /* If we're in a template, the only thing we need to know is the
5204 RESULT_TYPE. */
5205 if (processing_template_decl)
5207 /* Since the middle-end checks the type when doing a build2, we
5208 need to build the tree in pieces. This built tree will never
5209 get out of the front-end as we replace it when instantiating
5210 the template. */
5211 tree tmp = build2 (resultcode,
5212 build_type ? build_type : result_type,
5213 NULL_TREE, op1);
5214 TREE_OPERAND (tmp, 0) = op0;
5215 return tmp;
5218 /* Remember the original type; RESULT_TYPE might be changed later on
5219 by shorten_binary_op. */
5220 tree orig_type = result_type;
5222 if (arithmetic_types_p)
5224 bool first_complex = (code0 == COMPLEX_TYPE);
5225 bool second_complex = (code1 == COMPLEX_TYPE);
5226 int none_complex = (!first_complex && !second_complex);
5228 /* Adapted from patch for c/24581. */
5229 if (first_complex != second_complex
5230 && (code == PLUS_EXPR
5231 || code == MINUS_EXPR
5232 || code == MULT_EXPR
5233 || (code == TRUNC_DIV_EXPR && first_complex))
5234 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5235 && flag_signed_zeros)
5237 /* An operation on mixed real/complex operands must be
5238 handled specially, but the language-independent code can
5239 more easily optimize the plain complex arithmetic if
5240 -fno-signed-zeros. */
5241 tree real_type = TREE_TYPE (result_type);
5242 tree real, imag;
5243 if (first_complex)
5245 if (TREE_TYPE (op0) != result_type)
5246 op0 = cp_convert_and_check (result_type, op0, complain);
5247 if (TREE_TYPE (op1) != real_type)
5248 op1 = cp_convert_and_check (real_type, op1, complain);
5250 else
5252 if (TREE_TYPE (op0) != real_type)
5253 op0 = cp_convert_and_check (real_type, op0, complain);
5254 if (TREE_TYPE (op1) != result_type)
5255 op1 = cp_convert_and_check (result_type, op1, complain);
5257 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5258 return error_mark_node;
5259 if (first_complex)
5261 op0 = save_expr (op0);
5262 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5263 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5264 switch (code)
5266 case MULT_EXPR:
5267 case TRUNC_DIV_EXPR:
5268 op1 = save_expr (op1);
5269 imag = build2 (resultcode, real_type, imag, op1);
5270 /* Fall through. */
5271 case PLUS_EXPR:
5272 case MINUS_EXPR:
5273 real = build2 (resultcode, real_type, real, op1);
5274 break;
5275 default:
5276 gcc_unreachable();
5279 else
5281 op1 = save_expr (op1);
5282 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5283 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5284 switch (code)
5286 case MULT_EXPR:
5287 op0 = save_expr (op0);
5288 imag = build2 (resultcode, real_type, op0, imag);
5289 /* Fall through. */
5290 case PLUS_EXPR:
5291 real = build2 (resultcode, real_type, op0, real);
5292 break;
5293 case MINUS_EXPR:
5294 real = build2 (resultcode, real_type, op0, real);
5295 imag = build1 (NEGATE_EXPR, real_type, imag);
5296 break;
5297 default:
5298 gcc_unreachable();
5301 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5302 return result;
5305 /* For certain operations (which identify themselves by shorten != 0)
5306 if both args were extended from the same smaller type,
5307 do the arithmetic in that type and then extend.
5309 shorten !=0 and !=1 indicates a bitwise operation.
5310 For them, this optimization is safe only if
5311 both args are zero-extended or both are sign-extended.
5312 Otherwise, we might change the result.
5313 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5314 but calculated in (unsigned short) it would be (unsigned short)-1. */
5316 if (shorten && none_complex)
5318 final_type = result_type;
5319 result_type = shorten_binary_op (result_type, op0, op1,
5320 shorten == -1);
5323 /* Comparison operations are shortened too but differently.
5324 They identify themselves by setting short_compare = 1. */
5326 if (short_compare)
5328 /* We call shorten_compare only for diagnostics. */
5329 tree xop0 = fold_simple (op0);
5330 tree xop1 = fold_simple (op1);
5331 tree xresult_type = result_type;
5332 enum tree_code xresultcode = resultcode;
5333 shorten_compare (location, &xop0, &xop1, &xresult_type,
5334 &xresultcode);
5337 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5338 && warn_sign_compare
5339 /* Do not warn until the template is instantiated; we cannot
5340 bound the ranges of the arguments until that point. */
5341 && !processing_template_decl
5342 && (complain & tf_warning)
5343 && c_inhibit_evaluation_warnings == 0
5344 /* Even unsigned enum types promote to signed int. We don't
5345 want to issue -Wsign-compare warnings for this case. */
5346 && !enum_cast_to_int (orig_op0)
5347 && !enum_cast_to_int (orig_op1))
5349 tree oop0 = maybe_constant_value (orig_op0);
5350 tree oop1 = maybe_constant_value (orig_op1);
5352 if (TREE_CODE (oop0) != INTEGER_CST)
5353 oop0 = cp_fully_fold (orig_op0);
5354 if (TREE_CODE (oop1) != INTEGER_CST)
5355 oop1 = cp_fully_fold (orig_op1);
5356 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5357 result_type, resultcode);
5361 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5362 Then the expression will be built.
5363 It will be given type FINAL_TYPE if that is nonzero;
5364 otherwise, it will be given type RESULT_TYPE. */
5365 if (! converted)
5367 warning_sentinel w (warn_sign_conversion, short_compare);
5368 if (TREE_TYPE (op0) != result_type)
5369 op0 = cp_convert_and_check (result_type, op0, complain);
5370 if (TREE_TYPE (op1) != result_type)
5371 op1 = cp_convert_and_check (result_type, op1, complain);
5373 if (op0 == error_mark_node || op1 == error_mark_node)
5374 return error_mark_node;
5377 if (build_type == NULL_TREE)
5378 build_type = result_type;
5380 if (sanitize_flags_p ((SANITIZE_SHIFT
5381 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5382 && current_function_decl != NULL_TREE
5383 && !processing_template_decl
5384 && (doing_div_or_mod || doing_shift))
5386 /* OP0 and/or OP1 might have side-effects. */
5387 op0 = cp_save_expr (op0);
5388 op1 = cp_save_expr (op1);
5389 op0 = fold_non_dependent_expr (op0, complain);
5390 op1 = fold_non_dependent_expr (op1, complain);
5391 if (doing_div_or_mod
5392 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5394 /* For diagnostics we want to use the promoted types without
5395 shorten_binary_op. So convert the arguments to the
5396 original result_type. */
5397 tree cop0 = op0;
5398 tree cop1 = op1;
5399 if (TREE_TYPE (cop0) != orig_type)
5400 cop0 = cp_convert (orig_type, op0, complain);
5401 if (TREE_TYPE (cop1) != orig_type)
5402 cop1 = cp_convert (orig_type, op1, complain);
5403 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5405 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5406 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5409 result = build2_loc (location, resultcode, build_type, op0, op1);
5410 if (final_type != 0)
5411 result = cp_convert (final_type, result, complain);
5413 if (instrument_expr != NULL)
5414 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5415 instrument_expr, result);
5417 if (!processing_template_decl)
5419 op0 = cp_fully_fold (op0);
5420 /* Only consider the second argument if the first isn't overflowed. */
5421 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5422 return result;
5423 op1 = cp_fully_fold (op1);
5424 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5425 return result;
5427 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5428 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5429 return result;
5431 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5432 if (TREE_OVERFLOW_P (result_ovl))
5433 overflow_warning (location, result_ovl);
5435 return result;
5438 /* Build a VEC_PERM_EXPR.
5439 This is a simple wrapper for c_build_vec_perm_expr. */
5440 tree
5441 build_x_vec_perm_expr (location_t loc,
5442 tree arg0, tree arg1, tree arg2,
5443 tsubst_flags_t complain)
5445 tree orig_arg0 = arg0;
5446 tree orig_arg1 = arg1;
5447 tree orig_arg2 = arg2;
5448 if (processing_template_decl)
5450 if (type_dependent_expression_p (arg0)
5451 || type_dependent_expression_p (arg1)
5452 || type_dependent_expression_p (arg2))
5453 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5454 arg0 = build_non_dependent_expr (arg0);
5455 if (arg1)
5456 arg1 = build_non_dependent_expr (arg1);
5457 arg2 = build_non_dependent_expr (arg2);
5459 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5460 if (processing_template_decl && exp != error_mark_node)
5461 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5462 orig_arg1, orig_arg2);
5463 return exp;
5466 /* Return a tree for the sum or difference (RESULTCODE says which)
5467 of pointer PTROP and integer INTOP. */
5469 static tree
5470 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5471 tree intop, tsubst_flags_t complain)
5473 tree res_type = TREE_TYPE (ptrop);
5475 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5476 in certain circumstance (when it's valid to do so). So we need
5477 to make sure it's complete. We don't need to check here, if we
5478 can actually complete it at all, as those checks will be done in
5479 pointer_int_sum() anyway. */
5480 complete_type (TREE_TYPE (res_type));
5482 return pointer_int_sum (loc, resultcode, ptrop,
5483 intop, complain & tf_warning_or_error);
5486 /* Return a tree for the difference of pointers OP0 and OP1.
5487 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5488 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5490 static tree
5491 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5492 tsubst_flags_t complain, tree *instrument_expr)
5494 tree result, inttype;
5495 tree restype = ptrdiff_type_node;
5496 tree target_type = TREE_TYPE (ptrtype);
5498 if (!complete_type_or_else (target_type, NULL_TREE))
5499 return error_mark_node;
5501 if (VOID_TYPE_P (target_type))
5503 if (complain & tf_error)
5504 permerror (loc, "ISO C++ forbids using pointer of "
5505 "type %<void *%> in subtraction");
5506 else
5507 return error_mark_node;
5509 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5511 if (complain & tf_error)
5512 permerror (loc, "ISO C++ forbids using pointer to "
5513 "a function in subtraction");
5514 else
5515 return error_mark_node;
5517 if (TREE_CODE (target_type) == METHOD_TYPE)
5519 if (complain & tf_error)
5520 permerror (loc, "ISO C++ forbids using pointer to "
5521 "a method in subtraction");
5522 else
5523 return error_mark_node;
5526 /* Determine integer type result of the subtraction. This will usually
5527 be the same as the result type (ptrdiff_t), but may need to be a wider
5528 type if pointers for the address space are wider than ptrdiff_t. */
5529 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5530 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5531 else
5532 inttype = restype;
5534 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5536 op0 = save_expr (op0);
5537 op1 = save_expr (op1);
5539 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5540 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5543 /* First do the subtraction, then build the divide operator
5544 and only convert at the very end.
5545 Do not do default conversions in case restype is a short type. */
5547 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5548 pointers. If some platform cannot provide that, or has a larger
5549 ptrdiff_type to support differences larger than half the address
5550 space, cast the pointers to some larger integer type and do the
5551 computations in that type. */
5552 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5553 op0 = cp_build_binary_op (loc,
5554 MINUS_EXPR,
5555 cp_convert (inttype, op0, complain),
5556 cp_convert (inttype, op1, complain),
5557 complain);
5558 else
5559 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5561 /* This generates an error if op1 is a pointer to an incomplete type. */
5562 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5564 if (complain & tf_error)
5565 error_at (loc, "invalid use of a pointer to an incomplete type in "
5566 "pointer arithmetic");
5567 else
5568 return error_mark_node;
5571 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5573 if (complain & tf_error)
5574 error_at (loc, "arithmetic on pointer to an empty aggregate");
5575 else
5576 return error_mark_node;
5579 op1 = (TYPE_PTROB_P (ptrtype)
5580 ? size_in_bytes_loc (loc, target_type)
5581 : integer_one_node);
5583 /* Do the division. */
5585 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5586 cp_convert (inttype, op1, complain));
5587 return cp_convert (restype, result, complain);
5590 /* Construct and perhaps optimize a tree representation
5591 for a unary operation. CODE, a tree_code, specifies the operation
5592 and XARG is the operand. */
5594 tree
5595 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5596 tsubst_flags_t complain)
5598 tree orig_expr = xarg;
5599 tree exp;
5600 int ptrmem = 0;
5601 tree overload = NULL_TREE;
5603 if (processing_template_decl)
5605 if (type_dependent_expression_p (xarg))
5606 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5608 xarg = build_non_dependent_expr (xarg);
5611 exp = NULL_TREE;
5613 /* [expr.unary.op] says:
5615 The address of an object of incomplete type can be taken.
5617 (And is just the ordinary address operator, not an overloaded
5618 "operator &".) However, if the type is a template
5619 specialization, we must complete the type at this point so that
5620 an overloaded "operator &" will be available if required. */
5621 if (code == ADDR_EXPR
5622 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5623 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5624 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5625 || (TREE_CODE (xarg) == OFFSET_REF)))
5626 /* Don't look for a function. */;
5627 else
5628 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5629 NULL_TREE, &overload, complain);
5631 if (!exp && code == ADDR_EXPR)
5633 if (is_overloaded_fn (xarg))
5635 tree fn = get_first_fn (xarg);
5636 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5638 if (complain & tf_error)
5639 error (DECL_CONSTRUCTOR_P (fn)
5640 ? G_("taking address of constructor %qD")
5641 : G_("taking address of destructor %qD"),
5642 fn);
5643 return error_mark_node;
5647 /* A pointer to member-function can be formed only by saying
5648 &X::mf. */
5649 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5650 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5652 if (TREE_CODE (xarg) != OFFSET_REF
5653 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5655 if (complain & tf_error)
5657 error ("invalid use of %qE to form a "
5658 "pointer-to-member-function", xarg.get_value ());
5659 if (TREE_CODE (xarg) != OFFSET_REF)
5660 inform (input_location, " a qualified-id is required");
5662 return error_mark_node;
5664 else
5666 if (complain & tf_error)
5667 error ("parentheses around %qE cannot be used to form a"
5668 " pointer-to-member-function",
5669 xarg.get_value ());
5670 else
5671 return error_mark_node;
5672 PTRMEM_OK_P (xarg) = 1;
5676 if (TREE_CODE (xarg) == OFFSET_REF)
5678 ptrmem = PTRMEM_OK_P (xarg);
5680 if (!ptrmem && !flag_ms_extensions
5681 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5683 /* A single non-static member, make sure we don't allow a
5684 pointer-to-member. */
5685 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5686 TREE_OPERAND (xarg, 0),
5687 ovl_make (TREE_OPERAND (xarg, 1)));
5688 PTRMEM_OK_P (xarg) = ptrmem;
5692 exp = cp_build_addr_expr_strict (xarg, complain);
5695 if (processing_template_decl && exp != error_mark_node)
5697 if (overload != NULL_TREE)
5698 return (build_min_non_dep_op_overload
5699 (code, exp, overload, orig_expr, integer_zero_node));
5701 exp = build_min_non_dep (code, exp, orig_expr,
5702 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5704 if (TREE_CODE (exp) == ADDR_EXPR)
5705 PTRMEM_OK_P (exp) = ptrmem;
5706 return exp;
5709 /* Construct and perhaps optimize a tree representation
5710 for __builtin_addressof operation. ARG specifies the operand. */
5712 tree
5713 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5715 tree orig_expr = arg;
5717 if (processing_template_decl)
5719 if (type_dependent_expression_p (arg))
5720 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5722 arg = build_non_dependent_expr (arg);
5725 tree exp = cp_build_addr_expr_strict (arg, complain);
5727 if (processing_template_decl && exp != error_mark_node)
5728 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5729 return exp;
5732 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5733 constants, where a null value is represented by an INTEGER_CST of
5734 -1. */
5736 tree
5737 cp_truthvalue_conversion (tree expr)
5739 tree type = TREE_TYPE (expr);
5740 if (TYPE_PTR_OR_PTRMEM_P (type)
5741 /* Avoid ICE on invalid use of non-static member function. */
5742 || TREE_CODE (expr) == FUNCTION_DECL)
5743 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5744 else
5745 return c_common_truthvalue_conversion (input_location, expr);
5748 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5750 tree
5751 condition_conversion (tree expr)
5753 tree t;
5754 /* Anything that might happen in a template should go through
5755 maybe_convert_cond. */
5756 gcc_assert (!processing_template_decl);
5757 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5758 tf_warning_or_error, LOOKUP_NORMAL);
5759 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5760 return t;
5763 /* Returns the address of T. This function will fold away
5764 ADDR_EXPR of INDIRECT_REF. */
5766 tree
5767 build_address (tree t)
5769 if (error_operand_p (t) || !cxx_mark_addressable (t))
5770 return error_mark_node;
5771 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5772 || processing_template_decl);
5773 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5774 if (TREE_CODE (t) != ADDR_EXPR)
5775 t = rvalue (t);
5776 return t;
5779 /* Return a NOP_EXPR converting EXPR to TYPE. */
5781 tree
5782 build_nop (tree type, tree expr)
5784 if (type == error_mark_node || error_operand_p (expr))
5785 return expr;
5786 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5789 /* Take the address of ARG, whatever that means under C++ semantics.
5790 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5791 and class rvalues as well.
5793 Nothing should call this function directly; instead, callers should use
5794 cp_build_addr_expr or cp_build_addr_expr_strict. */
5796 static tree
5797 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5799 tree argtype;
5800 tree val;
5802 if (!arg || error_operand_p (arg))
5803 return error_mark_node;
5805 arg = mark_lvalue_use (arg);
5806 if (error_operand_p (arg))
5807 return error_mark_node;
5809 argtype = lvalue_type (arg);
5811 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5813 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5814 && !really_overloaded_fn (arg))
5816 /* They're trying to take the address of a unique non-static
5817 member function. This is ill-formed (except in MS-land),
5818 but let's try to DTRT.
5819 Note: We only handle unique functions here because we don't
5820 want to complain if there's a static overload; non-unique
5821 cases will be handled by instantiate_type. But we need to
5822 handle this case here to allow casts on the resulting PMF.
5823 We could defer this in non-MS mode, but it's easier to give
5824 a useful error here. */
5826 /* Inside constant member functions, the `this' pointer
5827 contains an extra const qualifier. TYPE_MAIN_VARIANT
5828 is used here to remove this const from the diagnostics
5829 and the created OFFSET_REF. */
5830 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5831 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5832 if (!mark_used (fn, complain) && !(complain & tf_error))
5833 return error_mark_node;
5835 if (! flag_ms_extensions)
5837 tree name = DECL_NAME (fn);
5838 if (!(complain & tf_error))
5839 return error_mark_node;
5840 else if (current_class_type
5841 && TREE_OPERAND (arg, 0) == current_class_ref)
5842 /* An expression like &memfn. */
5843 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5844 " or parenthesized non-static member function to form"
5845 " a pointer to member function. Say %<&%T::%D%>",
5846 base, name);
5847 else
5848 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5849 " function to form a pointer to member function."
5850 " Say %<&%T::%D%>",
5851 base, name);
5853 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5856 /* Uninstantiated types are all functions. Taking the
5857 address of a function is a no-op, so just return the
5858 argument. */
5859 if (type_unknown_p (arg))
5860 return build1 (ADDR_EXPR, unknown_type_node, arg);
5862 if (TREE_CODE (arg) == OFFSET_REF)
5863 /* We want a pointer to member; bypass all the code for actually taking
5864 the address of something. */
5865 goto offset_ref;
5867 /* Anything not already handled and not a true memory reference
5868 is an error. */
5869 if (TREE_CODE (argtype) != FUNCTION_TYPE
5870 && TREE_CODE (argtype) != METHOD_TYPE)
5872 cp_lvalue_kind kind = lvalue_kind (arg);
5873 if (kind == clk_none)
5875 if (complain & tf_error)
5876 lvalue_error (input_location, lv_addressof);
5877 return error_mark_node;
5879 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5881 if (!(complain & tf_error))
5882 return error_mark_node;
5883 /* Make this a permerror because we used to accept it. */
5884 permerror (input_location, "taking address of rvalue");
5888 if (TYPE_REF_P (argtype))
5890 tree type = build_pointer_type (TREE_TYPE (argtype));
5891 arg = build1 (CONVERT_EXPR, type, arg);
5892 return arg;
5894 else if (pedantic && DECL_MAIN_P (arg))
5896 /* ARM $3.4 */
5897 /* Apparently a lot of autoconf scripts for C++ packages do this,
5898 so only complain if -Wpedantic. */
5899 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5900 pedwarn (input_location, OPT_Wpedantic,
5901 "ISO C++ forbids taking address of function %<::main%>");
5902 else if (flag_pedantic_errors)
5903 return error_mark_node;
5906 /* Let &* cancel out to simplify resulting code. */
5907 if (INDIRECT_REF_P (arg))
5909 arg = TREE_OPERAND (arg, 0);
5910 if (TYPE_REF_P (TREE_TYPE (arg)))
5912 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5913 arg = build1 (CONVERT_EXPR, type, arg);
5915 else
5916 /* Don't let this be an lvalue. */
5917 arg = rvalue (arg);
5918 return arg;
5921 /* Handle complex lvalues (when permitted)
5922 by reduction to simpler cases. */
5923 val = unary_complex_lvalue (ADDR_EXPR, arg);
5924 if (val != 0)
5925 return val;
5927 switch (TREE_CODE (arg))
5929 CASE_CONVERT:
5930 case FLOAT_EXPR:
5931 case FIX_TRUNC_EXPR:
5932 /* We should have handled this above in the lvalue_kind check. */
5933 gcc_unreachable ();
5934 break;
5936 case BASELINK:
5937 arg = BASELINK_FUNCTIONS (arg);
5938 /* Fall through. */
5940 case OVERLOAD:
5941 arg = OVL_FIRST (arg);
5942 break;
5944 case OFFSET_REF:
5945 offset_ref:
5946 /* Turn a reference to a non-static data member into a
5947 pointer-to-member. */
5949 tree type;
5950 tree t;
5952 gcc_assert (PTRMEM_OK_P (arg));
5954 t = TREE_OPERAND (arg, 1);
5955 if (TYPE_REF_P (TREE_TYPE (t)))
5957 if (complain & tf_error)
5958 error ("cannot create pointer to reference member %qD", t);
5959 return error_mark_node;
5962 type = build_ptrmem_type (context_for_name_lookup (t),
5963 TREE_TYPE (t));
5964 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5965 return t;
5968 default:
5969 break;
5972 if (argtype != error_mark_node)
5973 argtype = build_pointer_type (argtype);
5975 if (bitfield_p (arg))
5977 if (complain & tf_error)
5978 error ("attempt to take address of bit-field");
5979 return error_mark_node;
5982 /* In a template, we are processing a non-dependent expression
5983 so we can just form an ADDR_EXPR with the correct type. */
5984 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5986 if (TREE_CODE (arg) == FUNCTION_DECL
5987 && !mark_used (arg, complain) && !(complain & tf_error))
5988 return error_mark_node;
5989 val = build_address (arg);
5990 if (TREE_CODE (arg) == OFFSET_REF)
5991 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5993 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5995 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5997 /* We can only get here with a single static member
5998 function. */
5999 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6000 && DECL_STATIC_FUNCTION_P (fn));
6001 if (!mark_used (fn, complain) && !(complain & tf_error))
6002 return error_mark_node;
6003 val = build_address (fn);
6004 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
6005 /* Do not lose object's side effects. */
6006 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
6007 TREE_OPERAND (arg, 0), val);
6009 else
6011 tree object = TREE_OPERAND (arg, 0);
6012 tree field = TREE_OPERAND (arg, 1);
6013 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6014 (TREE_TYPE (object), decl_type_context (field)));
6015 val = build_address (arg);
6018 if (TYPE_PTR_P (argtype)
6019 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6021 build_ptrmemfunc_type (argtype);
6022 val = build_ptrmemfunc (argtype, val, 0,
6023 /*c_cast_p=*/false,
6024 complain);
6027 return val;
6030 /* Take the address of ARG if it has one, even if it's an rvalue. */
6032 tree
6033 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6035 return cp_build_addr_expr_1 (arg, 0, complain);
6038 /* Take the address of ARG, but only if it's an lvalue. */
6040 static tree
6041 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6043 return cp_build_addr_expr_1 (arg, 1, complain);
6046 /* C++: Must handle pointers to members.
6048 Perhaps type instantiation should be extended to handle conversion
6049 from aggregates to types we don't yet know we want? (Or are those
6050 cases typically errors which should be reported?)
6052 NOCONVERT suppresses the default promotions (such as from short to int). */
6054 tree
6055 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6056 tsubst_flags_t complain)
6058 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6059 tree arg = xarg;
6060 location_t location = cp_expr_loc_or_loc (arg, input_location);
6061 tree argtype = 0;
6062 const char *errstring = NULL;
6063 tree val;
6064 const char *invalid_op_diag;
6066 if (!arg || error_operand_p (arg))
6067 return error_mark_node;
6069 if ((invalid_op_diag
6070 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6071 ? CONVERT_EXPR
6072 : code),
6073 TREE_TYPE (xarg))))
6075 if (complain & tf_error)
6076 error (invalid_op_diag);
6077 return error_mark_node;
6080 switch (code)
6082 case UNARY_PLUS_EXPR:
6083 case NEGATE_EXPR:
6085 int flags = WANT_ARITH | WANT_ENUM;
6086 /* Unary plus (but not unary minus) is allowed on pointers. */
6087 if (code == UNARY_PLUS_EXPR)
6088 flags |= WANT_POINTER;
6089 arg = build_expr_type_conversion (flags, arg, true);
6090 if (!arg)
6091 errstring = (code == NEGATE_EXPR
6092 ? _("wrong type argument to unary minus")
6093 : _("wrong type argument to unary plus"));
6094 else
6096 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6097 arg = cp_perform_integral_promotions (arg, complain);
6099 /* Make sure the result is not an lvalue: a unary plus or minus
6100 expression is always a rvalue. */
6101 arg = rvalue (arg);
6104 break;
6106 case BIT_NOT_EXPR:
6107 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6109 code = CONJ_EXPR;
6110 if (!noconvert)
6112 arg = cp_default_conversion (arg, complain);
6113 if (arg == error_mark_node)
6114 return error_mark_node;
6117 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6118 | WANT_VECTOR_OR_COMPLEX,
6119 arg, true)))
6120 errstring = _("wrong type argument to bit-complement");
6121 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6123 /* Warn if the expression has boolean value. */
6124 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6125 && (complain & tf_warning)
6126 && warning_at (location, OPT_Wbool_operation,
6127 "%<~%> on an expression of type bool"))
6128 inform (location, "did you mean to use logical not (%<!%>)?");
6129 arg = cp_perform_integral_promotions (arg, complain);
6131 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6132 arg = mark_rvalue_use (arg);
6133 break;
6135 case ABS_EXPR:
6136 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6137 errstring = _("wrong type argument to abs");
6138 else if (!noconvert)
6140 arg = cp_default_conversion (arg, complain);
6141 if (arg == error_mark_node)
6142 return error_mark_node;
6144 break;
6146 case CONJ_EXPR:
6147 /* Conjugating a real value is a no-op, but allow it anyway. */
6148 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6149 errstring = _("wrong type argument to conjugation");
6150 else if (!noconvert)
6152 arg = cp_default_conversion (arg, complain);
6153 if (arg == error_mark_node)
6154 return error_mark_node;
6156 break;
6158 case TRUTH_NOT_EXPR:
6159 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6160 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6161 build_zero_cst (TREE_TYPE (arg)), complain);
6162 arg = perform_implicit_conversion (boolean_type_node, arg,
6163 complain);
6164 val = invert_truthvalue_loc (input_location, arg);
6165 if (arg != error_mark_node)
6166 return val;
6167 errstring = _("in argument to unary !");
6168 break;
6170 case NOP_EXPR:
6171 break;
6173 case REALPART_EXPR:
6174 case IMAGPART_EXPR:
6175 arg = build_real_imag_expr (input_location, code, arg);
6176 return arg;
6178 case PREINCREMENT_EXPR:
6179 case POSTINCREMENT_EXPR:
6180 case PREDECREMENT_EXPR:
6181 case POSTDECREMENT_EXPR:
6182 /* Handle complex lvalues (when permitted)
6183 by reduction to simpler cases. */
6185 val = unary_complex_lvalue (code, arg);
6186 if (val != 0)
6187 return val;
6189 arg = mark_lvalue_use (arg);
6191 /* Increment or decrement the real part of the value,
6192 and don't change the imaginary part. */
6193 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6195 tree real, imag;
6197 arg = cp_stabilize_reference (arg);
6198 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6199 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6200 real = cp_build_unary_op (code, real, true, complain);
6201 if (real == error_mark_node || imag == error_mark_node)
6202 return error_mark_node;
6203 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6204 real, imag);
6207 /* Report invalid types. */
6209 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6210 arg, true)))
6212 if (code == PREINCREMENT_EXPR)
6213 errstring = _("no pre-increment operator for type");
6214 else if (code == POSTINCREMENT_EXPR)
6215 errstring = _("no post-increment operator for type");
6216 else if (code == PREDECREMENT_EXPR)
6217 errstring = _("no pre-decrement operator for type");
6218 else
6219 errstring = _("no post-decrement operator for type");
6220 break;
6222 else if (arg == error_mark_node)
6223 return error_mark_node;
6225 /* Report something read-only. */
6227 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6228 || TREE_READONLY (arg))
6230 if (complain & tf_error)
6231 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6232 || code == POSTINCREMENT_EXPR)
6233 ? lv_increment : lv_decrement));
6234 else
6235 return error_mark_node;
6239 tree inc;
6240 tree declared_type = unlowered_expr_type (arg);
6242 argtype = TREE_TYPE (arg);
6244 /* ARM $5.2.5 last annotation says this should be forbidden. */
6245 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6247 if (complain & tf_error)
6248 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6249 ? G_("ISO C++ forbids incrementing an enum")
6250 : G_("ISO C++ forbids decrementing an enum"));
6251 else
6252 return error_mark_node;
6255 /* Compute the increment. */
6257 if (TYPE_PTR_P (argtype))
6259 tree type = complete_type (TREE_TYPE (argtype));
6261 if (!COMPLETE_OR_VOID_TYPE_P (type))
6263 if (complain & tf_error)
6264 error (((code == PREINCREMENT_EXPR
6265 || code == POSTINCREMENT_EXPR))
6266 ? G_("cannot increment a pointer to incomplete type %qT")
6267 : G_("cannot decrement a pointer to incomplete type %qT"),
6268 TREE_TYPE (argtype));
6269 else
6270 return error_mark_node;
6272 else if (!TYPE_PTROB_P (argtype))
6274 if (complain & tf_error)
6275 pedwarn (input_location, OPT_Wpointer_arith,
6276 (code == PREINCREMENT_EXPR
6277 || code == POSTINCREMENT_EXPR)
6278 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6279 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6280 argtype);
6281 else
6282 return error_mark_node;
6285 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6287 else
6288 inc = VECTOR_TYPE_P (argtype)
6289 ? build_one_cst (argtype)
6290 : integer_one_node;
6292 inc = cp_convert (argtype, inc, complain);
6294 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6295 need to ask Objective-C to build the increment or decrement
6296 expression for it. */
6297 if (objc_is_property_ref (arg))
6298 return objc_build_incr_expr_for_property_ref (input_location, code,
6299 arg, inc);
6301 /* Complain about anything else that is not a true lvalue. */
6302 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6303 || code == POSTINCREMENT_EXPR)
6304 ? lv_increment : lv_decrement),
6305 complain))
6306 return error_mark_node;
6308 /* Forbid using -- or ++ in C++17 on `bool'. */
6309 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6311 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6313 if (complain & tf_error)
6314 error ("use of an operand of type %qT in %<operator--%> "
6315 "is forbidden", boolean_type_node);
6316 return error_mark_node;
6318 else
6320 if (cxx_dialect >= cxx17)
6322 if (complain & tf_error)
6323 error ("use of an operand of type %qT in "
6324 "%<operator++%> is forbidden in C++17",
6325 boolean_type_node);
6326 return error_mark_node;
6328 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6329 else if (!in_system_header_at (input_location))
6330 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6331 "in %<operator++%> is deprecated",
6332 boolean_type_node);
6334 val = boolean_increment (code, arg);
6336 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6337 /* An rvalue has no cv-qualifiers. */
6338 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6339 else
6340 val = build2 (code, TREE_TYPE (arg), arg, inc);
6342 TREE_SIDE_EFFECTS (val) = 1;
6343 return val;
6346 case ADDR_EXPR:
6347 /* Note that this operation never does default_conversion
6348 regardless of NOCONVERT. */
6349 return cp_build_addr_expr (arg, complain);
6351 default:
6352 break;
6355 if (!errstring)
6357 if (argtype == 0)
6358 argtype = TREE_TYPE (arg);
6359 return build1 (code, argtype, arg);
6362 if (complain & tf_error)
6363 error ("%s", errstring);
6364 return error_mark_node;
6367 /* Hook for the c-common bits that build a unary op. */
6368 tree
6369 build_unary_op (location_t /*location*/,
6370 enum tree_code code, tree xarg, bool noconvert)
6372 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6375 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6376 so that it is a valid lvalue even for GENERIC by replacing
6377 (lhs = rhs) with ((lhs = rhs), lhs)
6378 (--lhs) with ((--lhs), lhs)
6379 (++lhs) with ((++lhs), lhs)
6380 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6381 that it can be evaluated multiple times. */
6383 tree
6384 genericize_compound_lvalue (tree lvalue)
6386 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6387 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6388 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6389 TREE_OPERAND (lvalue, 1));
6390 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6391 lvalue, TREE_OPERAND (lvalue, 0));
6394 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6395 for certain kinds of expressions which are not really lvalues
6396 but which we can accept as lvalues.
6398 If ARG is not a kind of expression we can handle, return
6399 NULL_TREE. */
6401 tree
6402 unary_complex_lvalue (enum tree_code code, tree arg)
6404 /* Inside a template, making these kinds of adjustments is
6405 pointless; we are only concerned with the type of the
6406 expression. */
6407 if (processing_template_decl)
6408 return NULL_TREE;
6410 /* Handle (a, b) used as an "lvalue". */
6411 if (TREE_CODE (arg) == COMPOUND_EXPR)
6413 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6414 tf_warning_or_error);
6415 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6416 TREE_OPERAND (arg, 0), real_result);
6419 /* Handle (a ? b : c) used as an "lvalue". */
6420 if (TREE_CODE (arg) == COND_EXPR
6421 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6422 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6424 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6425 if (TREE_CODE (arg) == MODIFY_EXPR
6426 || TREE_CODE (arg) == PREINCREMENT_EXPR
6427 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6428 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6430 if (code != ADDR_EXPR)
6431 return NULL_TREE;
6433 /* Handle (a = b) used as an "lvalue" for `&'. */
6434 if (TREE_CODE (arg) == MODIFY_EXPR
6435 || TREE_CODE (arg) == INIT_EXPR)
6437 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6438 tf_warning_or_error);
6439 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6440 arg, real_result);
6441 TREE_NO_WARNING (arg) = 1;
6442 return arg;
6445 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6446 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6447 || TREE_CODE (arg) == OFFSET_REF)
6448 return NULL_TREE;
6450 /* We permit compiler to make function calls returning
6451 objects of aggregate type look like lvalues. */
6453 tree targ = arg;
6455 if (TREE_CODE (targ) == SAVE_EXPR)
6456 targ = TREE_OPERAND (targ, 0);
6458 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6460 if (TREE_CODE (arg) == SAVE_EXPR)
6461 targ = arg;
6462 else
6463 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6464 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6467 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6468 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6469 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6472 /* Don't let anything else be handled specially. */
6473 return NULL_TREE;
6476 /* Mark EXP saying that we need to be able to take the
6477 address of it; it should not be allocated in a register.
6478 Value is true if successful. ARRAY_REF_P is true if this
6479 is for ARRAY_REF construction - in that case we don't want
6480 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6481 it is fine to use ARRAY_REFs for vector subscripts on vector
6482 register variables.
6484 C++: we do not allow `current_class_ptr' to be addressable. */
6486 bool
6487 cxx_mark_addressable (tree exp, bool array_ref_p)
6489 tree x = exp;
6491 while (1)
6492 switch (TREE_CODE (x))
6494 case VIEW_CONVERT_EXPR:
6495 if (array_ref_p
6496 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6497 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6498 return true;
6499 /* FALLTHRU */
6500 case ADDR_EXPR:
6501 case COMPONENT_REF:
6502 case ARRAY_REF:
6503 case REALPART_EXPR:
6504 case IMAGPART_EXPR:
6505 x = TREE_OPERAND (x, 0);
6506 break;
6508 case PARM_DECL:
6509 if (x == current_class_ptr)
6511 error ("cannot take the address of %<this%>, which is an rvalue expression");
6512 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6513 return true;
6515 /* Fall through. */
6517 case VAR_DECL:
6518 /* Caller should not be trying to mark initialized
6519 constant fields addressable. */
6520 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6521 || DECL_IN_AGGR_P (x) == 0
6522 || TREE_STATIC (x)
6523 || DECL_EXTERNAL (x));
6524 /* Fall through. */
6526 case RESULT_DECL:
6527 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6528 && !DECL_ARTIFICIAL (x))
6530 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6532 error
6533 ("address of explicit register variable %qD requested", x);
6534 return false;
6536 else if (extra_warnings)
6537 warning
6538 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6540 TREE_ADDRESSABLE (x) = 1;
6541 return true;
6543 case CONST_DECL:
6544 case FUNCTION_DECL:
6545 TREE_ADDRESSABLE (x) = 1;
6546 return true;
6548 case CONSTRUCTOR:
6549 TREE_ADDRESSABLE (x) = 1;
6550 return true;
6552 case TARGET_EXPR:
6553 TREE_ADDRESSABLE (x) = 1;
6554 cxx_mark_addressable (TREE_OPERAND (x, 0));
6555 return true;
6557 default:
6558 return true;
6562 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6564 tree
6565 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6566 tsubst_flags_t complain)
6568 tree orig_ifexp = ifexp;
6569 tree orig_op1 = op1;
6570 tree orig_op2 = op2;
6571 tree expr;
6573 if (processing_template_decl)
6575 /* The standard says that the expression is type-dependent if
6576 IFEXP is type-dependent, even though the eventual type of the
6577 expression doesn't dependent on IFEXP. */
6578 if (type_dependent_expression_p (ifexp)
6579 /* As a GNU extension, the middle operand may be omitted. */
6580 || (op1 && type_dependent_expression_p (op1))
6581 || type_dependent_expression_p (op2))
6582 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6583 ifexp = build_non_dependent_expr (ifexp);
6584 if (op1)
6585 op1 = build_non_dependent_expr (op1);
6586 op2 = build_non_dependent_expr (op2);
6589 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6590 if (processing_template_decl && expr != error_mark_node)
6592 tree min = build_min_non_dep (COND_EXPR, expr,
6593 orig_ifexp, orig_op1, orig_op2);
6594 expr = convert_from_reference (min);
6596 return expr;
6599 /* Given a list of expressions, return a compound expression
6600 that performs them all and returns the value of the last of them. */
6602 tree
6603 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6604 tsubst_flags_t complain)
6606 tree expr = TREE_VALUE (list);
6608 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6609 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6611 if (complain & tf_error)
6612 pedwarn (cp_expr_loc_or_loc (expr, input_location), 0,
6613 "list-initializer for non-class type must not "
6614 "be parenthesized");
6615 else
6616 return error_mark_node;
6619 if (TREE_CHAIN (list))
6621 if (complain & tf_error)
6622 switch (exp)
6624 case ELK_INIT:
6625 permerror (input_location, "expression list treated as compound "
6626 "expression in initializer");
6627 break;
6628 case ELK_MEM_INIT:
6629 permerror (input_location, "expression list treated as compound "
6630 "expression in mem-initializer");
6631 break;
6632 case ELK_FUNC_CAST:
6633 permerror (input_location, "expression list treated as compound "
6634 "expression in functional cast");
6635 break;
6636 default:
6637 gcc_unreachable ();
6639 else
6640 return error_mark_node;
6642 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6643 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6644 expr, TREE_VALUE (list), complain);
6647 return expr;
6650 /* Like build_x_compound_expr_from_list, but using a VEC. */
6652 tree
6653 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6654 tsubst_flags_t complain)
6656 if (vec_safe_is_empty (vec))
6657 return NULL_TREE;
6658 else if (vec->length () == 1)
6659 return (*vec)[0];
6660 else
6662 tree expr;
6663 unsigned int ix;
6664 tree t;
6666 if (msg != NULL)
6668 if (complain & tf_error)
6669 permerror (input_location,
6670 "%s expression list treated as compound expression",
6671 msg);
6672 else
6673 return error_mark_node;
6676 expr = (*vec)[0];
6677 for (ix = 1; vec->iterate (ix, &t); ++ix)
6678 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6679 t, complain);
6681 return expr;
6685 /* Handle overloading of the ',' operator when needed. */
6687 tree
6688 build_x_compound_expr (location_t loc, tree op1, tree op2,
6689 tsubst_flags_t complain)
6691 tree result;
6692 tree orig_op1 = op1;
6693 tree orig_op2 = op2;
6694 tree overload = NULL_TREE;
6696 if (processing_template_decl)
6698 if (type_dependent_expression_p (op1)
6699 || type_dependent_expression_p (op2))
6700 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6701 op1 = build_non_dependent_expr (op1);
6702 op2 = build_non_dependent_expr (op2);
6705 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6706 NULL_TREE, &overload, complain);
6707 if (!result)
6708 result = cp_build_compound_expr (op1, op2, complain);
6710 if (processing_template_decl && result != error_mark_node)
6712 if (overload != NULL_TREE)
6713 return (build_min_non_dep_op_overload
6714 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6716 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6719 return result;
6722 /* Like cp_build_compound_expr, but for the c-common bits. */
6724 tree
6725 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6727 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6730 /* Build a compound expression. */
6732 tree
6733 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6735 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6737 if (lhs == error_mark_node || rhs == error_mark_node)
6738 return error_mark_node;
6740 if (TREE_CODE (rhs) == TARGET_EXPR)
6742 /* If the rhs is a TARGET_EXPR, then build the compound
6743 expression inside the target_expr's initializer. This
6744 helps the compiler to eliminate unnecessary temporaries. */
6745 tree init = TREE_OPERAND (rhs, 1);
6747 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6748 TREE_OPERAND (rhs, 1) = init;
6750 return rhs;
6753 if (type_unknown_p (rhs))
6755 if (complain & tf_error)
6756 error ("no context to resolve type of %qE", rhs);
6757 return error_mark_node;
6760 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6763 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6764 casts away constness. CAST gives the type of cast. Returns true
6765 if the cast is ill-formed, false if it is well-formed.
6767 ??? This function warns for casting away any qualifier not just
6768 const. We would like to specify exactly what qualifiers are casted
6769 away.
6772 static bool
6773 check_for_casting_away_constness (tree src_type, tree dest_type,
6774 enum tree_code cast, tsubst_flags_t complain)
6776 /* C-style casts are allowed to cast away constness. With
6777 WARN_CAST_QUAL, we still want to issue a warning. */
6778 if (cast == CAST_EXPR && !warn_cast_qual)
6779 return false;
6781 if (!casts_away_constness (src_type, dest_type, complain))
6782 return false;
6784 switch (cast)
6786 case CAST_EXPR:
6787 if (complain & tf_warning)
6788 warning (OPT_Wcast_qual,
6789 "cast from type %qT to type %qT casts away qualifiers",
6790 src_type, dest_type);
6791 return false;
6793 case STATIC_CAST_EXPR:
6794 if (complain & tf_error)
6795 error ("static_cast from type %qT to type %qT casts away qualifiers",
6796 src_type, dest_type);
6797 return true;
6799 case REINTERPRET_CAST_EXPR:
6800 if (complain & tf_error)
6801 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6802 src_type, dest_type);
6803 return true;
6805 default:
6806 gcc_unreachable();
6810 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6811 void
6812 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6814 if (warn_useless_cast
6815 && complain & tf_warning)
6817 if ((TYPE_REF_P (type)
6818 && (TYPE_REF_IS_RVALUE (type)
6819 ? xvalue_p (expr) : lvalue_p (expr))
6820 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6821 || same_type_p (TREE_TYPE (expr), type))
6822 warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6826 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6827 void
6828 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6830 if (warn_ignored_qualifiers
6831 && complain & tf_warning
6832 && !CLASS_TYPE_P (type)
6833 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6835 warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6836 "result type");
6840 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6841 (another pointer-to-member type in the same hierarchy) and return
6842 the converted expression. If ALLOW_INVERSE_P is permitted, a
6843 pointer-to-derived may be converted to pointer-to-base; otherwise,
6844 only the other direction is permitted. If C_CAST_P is true, this
6845 conversion is taking place as part of a C-style cast. */
6847 tree
6848 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6849 bool c_cast_p, tsubst_flags_t complain)
6851 if (same_type_p (type, TREE_TYPE (expr)))
6852 return expr;
6854 if (TYPE_PTRDATAMEM_P (type))
6856 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
6857 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
6858 tree delta = (get_delta_difference
6859 (obase, nbase,
6860 allow_inverse_p, c_cast_p, complain));
6862 if (delta == error_mark_node)
6863 return error_mark_node;
6865 if (!same_type_p (obase, nbase))
6867 if (TREE_CODE (expr) == PTRMEM_CST)
6868 expr = cplus_expand_constant (expr);
6870 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
6871 build_int_cst (TREE_TYPE (expr), -1),
6872 complain);
6873 tree op1 = build_nop (ptrdiff_type_node, expr);
6874 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
6875 complain);
6877 expr = fold_build3_loc (input_location,
6878 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6881 return build_nop (type, expr);
6883 else
6884 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6885 allow_inverse_p, c_cast_p, complain);
6888 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6889 this static_cast is being attempted as one of the possible casts
6890 allowed by a C-style cast. (In that case, accessibility of base
6891 classes is not considered, and it is OK to cast away
6892 constness.) Return the result of the cast. *VALID_P is set to
6893 indicate whether or not the cast was valid. */
6895 static tree
6896 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6897 bool *valid_p, tsubst_flags_t complain)
6899 tree intype;
6900 tree result;
6901 cp_lvalue_kind clk;
6903 /* Assume the cast is valid. */
6904 *valid_p = true;
6906 intype = unlowered_expr_type (expr);
6908 /* Save casted types in the function's used types hash table. */
6909 used_types_insert (type);
6911 /* A prvalue of non-class type is cv-unqualified. */
6912 if (!CLASS_TYPE_P (type))
6913 type = cv_unqualified (type);
6915 /* [expr.static.cast]
6917 An lvalue of type "cv1 B", where B is a class type, can be cast
6918 to type "reference to cv2 D", where D is a class derived (clause
6919 _class.derived_) from B, if a valid standard conversion from
6920 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6921 same cv-qualification as, or greater cv-qualification than, cv1,
6922 and B is not a virtual base class of D. */
6923 /* We check this case before checking the validity of "TYPE t =
6924 EXPR;" below because for this case:
6926 struct B {};
6927 struct D : public B { D(const B&); };
6928 extern B& b;
6929 void f() { static_cast<const D&>(b); }
6931 we want to avoid constructing a new D. The standard is not
6932 completely clear about this issue, but our interpretation is
6933 consistent with other compilers. */
6934 if (TYPE_REF_P (type)
6935 && CLASS_TYPE_P (TREE_TYPE (type))
6936 && CLASS_TYPE_P (intype)
6937 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6938 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6939 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6940 build_pointer_type (TYPE_MAIN_VARIANT
6941 (TREE_TYPE (type))),
6942 complain)
6943 && (c_cast_p
6944 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6946 tree base;
6948 if (processing_template_decl)
6949 return expr;
6951 /* There is a standard conversion from "D*" to "B*" even if "B"
6952 is ambiguous or inaccessible. If this is really a
6953 static_cast, then we check both for inaccessibility and
6954 ambiguity. However, if this is a static_cast being performed
6955 because the user wrote a C-style cast, then accessibility is
6956 not considered. */
6957 base = lookup_base (TREE_TYPE (type), intype,
6958 c_cast_p ? ba_unique : ba_check,
6959 NULL, complain);
6960 expr = build_address (expr);
6962 if (sanitize_flags_p (SANITIZE_VPTR))
6964 tree ubsan_check
6965 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6966 intype, expr);
6967 if (ubsan_check)
6968 expr = ubsan_check;
6971 /* Convert from "B*" to "D*". This function will check that "B"
6972 is not a virtual base of "D". Even if we don't have a guarantee
6973 that expr is NULL, if the static_cast is to a reference type,
6974 it is UB if it would be NULL, so omit the non-NULL check. */
6975 expr = build_base_path (MINUS_EXPR, expr, base,
6976 /*nonnull=*/flag_delete_null_pointer_checks,
6977 complain);
6979 /* Convert the pointer to a reference -- but then remember that
6980 there are no expressions with reference type in C++.
6982 We call rvalue so that there's an actual tree code
6983 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6984 is a variable with the same type, the conversion would get folded
6985 away, leaving just the variable and causing lvalue_kind to give
6986 the wrong answer. */
6987 expr = cp_fold_convert (type, expr);
6989 /* When -fsanitize=null, make sure to diagnose reference binding to
6990 NULL even when the reference is converted to pointer later on. */
6991 if (sanitize_flags_p (SANITIZE_NULL)
6992 && TREE_CODE (expr) == COND_EXPR
6993 && TREE_OPERAND (expr, 2)
6994 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
6995 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
6996 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
6998 return convert_from_reference (rvalue (expr));
7001 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7002 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7003 if (TYPE_REF_P (type)
7004 && TYPE_REF_IS_RVALUE (type)
7005 && (clk = real_lvalue_p (expr))
7006 && reference_related_p (TREE_TYPE (type), intype)
7007 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7009 if (processing_template_decl)
7010 return expr;
7011 if (clk == clk_ordinary)
7013 /* Handle the (non-bit-field) lvalue case here by casting to
7014 lvalue reference and then changing it to an rvalue reference.
7015 Casting an xvalue to rvalue reference will be handled by the
7016 main code path. */
7017 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7018 result = (perform_direct_initialization_if_possible
7019 (lref, expr, c_cast_p, complain));
7020 result = build1 (NON_LVALUE_EXPR, type, result);
7021 return convert_from_reference (result);
7023 else
7024 /* For a bit-field or packed field, bind to a temporary. */
7025 expr = rvalue (expr);
7028 /* Resolve overloaded address here rather than once in
7029 implicit_conversion and again in the inverse code below. */
7030 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7032 expr = instantiate_type (type, expr, complain);
7033 intype = TREE_TYPE (expr);
7036 /* [expr.static.cast]
7038 Any expression can be explicitly converted to type cv void. */
7039 if (VOID_TYPE_P (type))
7040 return convert_to_void (expr, ICV_CAST, complain);
7042 /* [class.abstract]
7043 An abstract class shall not be used ... as the type of an explicit
7044 conversion. */
7045 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7046 return error_mark_node;
7048 /* [expr.static.cast]
7050 An expression e can be explicitly converted to a type T using a
7051 static_cast of the form static_cast<T>(e) if the declaration T
7052 t(e);" is well-formed, for some invented temporary variable
7053 t. */
7054 result = perform_direct_initialization_if_possible (type, expr,
7055 c_cast_p, complain);
7056 if (result)
7058 if (processing_template_decl)
7059 return expr;
7061 result = convert_from_reference (result);
7063 /* [expr.static.cast]
7065 If T is a reference type, the result is an lvalue; otherwise,
7066 the result is an rvalue. */
7067 if (!TYPE_REF_P (type))
7069 result = rvalue (result);
7071 if (result == expr && SCALAR_TYPE_P (type))
7072 /* Leave some record of the cast. */
7073 result = build_nop (type, expr);
7075 return result;
7078 /* [expr.static.cast]
7080 The inverse of any standard conversion sequence (clause _conv_),
7081 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7082 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7083 (_conv.bool_) conversions, can be performed explicitly using
7084 static_cast subject to the restriction that the explicit
7085 conversion does not cast away constness (_expr.const.cast_), and
7086 the following additional rules for specific cases: */
7087 /* For reference, the conversions not excluded are: integral
7088 promotions, floating point promotion, integral conversions,
7089 floating point conversions, floating-integral conversions,
7090 pointer conversions, and pointer to member conversions. */
7091 /* DR 128
7093 A value of integral _or enumeration_ type can be explicitly
7094 converted to an enumeration type. */
7095 /* The effect of all that is that any conversion between any two
7096 types which are integral, floating, or enumeration types can be
7097 performed. */
7098 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7099 || SCALAR_FLOAT_TYPE_P (type))
7100 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7101 || SCALAR_FLOAT_TYPE_P (intype)))
7103 if (processing_template_decl)
7104 return expr;
7105 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7108 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7109 && CLASS_TYPE_P (TREE_TYPE (type))
7110 && CLASS_TYPE_P (TREE_TYPE (intype))
7111 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7112 (TREE_TYPE (intype))),
7113 build_pointer_type (TYPE_MAIN_VARIANT
7114 (TREE_TYPE (type))),
7115 complain))
7117 tree base;
7119 if (processing_template_decl)
7120 return expr;
7122 if (!c_cast_p
7123 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7124 complain))
7125 return error_mark_node;
7126 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7127 c_cast_p ? ba_unique : ba_check,
7128 NULL, complain);
7129 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7130 complain);
7132 if (sanitize_flags_p (SANITIZE_VPTR))
7134 tree ubsan_check
7135 = cp_ubsan_maybe_instrument_downcast (input_location, type,
7136 intype, expr);
7137 if (ubsan_check)
7138 expr = ubsan_check;
7141 return cp_fold_convert (type, expr);
7144 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7145 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7147 tree c1;
7148 tree c2;
7149 tree t1;
7150 tree t2;
7152 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7153 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7155 if (TYPE_PTRDATAMEM_P (type))
7157 t1 = (build_ptrmem_type
7158 (c1,
7159 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7160 t2 = (build_ptrmem_type
7161 (c2,
7162 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7164 else
7166 t1 = intype;
7167 t2 = type;
7169 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7171 if (!c_cast_p
7172 && check_for_casting_away_constness (intype, type,
7173 STATIC_CAST_EXPR,
7174 complain))
7175 return error_mark_node;
7176 if (processing_template_decl)
7177 return expr;
7178 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7179 c_cast_p, complain);
7183 /* [expr.static.cast]
7185 An rvalue of type "pointer to cv void" can be explicitly
7186 converted to a pointer to object type. A value of type pointer
7187 to object converted to "pointer to cv void" and back to the
7188 original pointer type will have its original value. */
7189 if (TYPE_PTR_P (intype)
7190 && VOID_TYPE_P (TREE_TYPE (intype))
7191 && TYPE_PTROB_P (type))
7193 if (!c_cast_p
7194 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7195 complain))
7196 return error_mark_node;
7197 if (processing_template_decl)
7198 return expr;
7199 return build_nop (type, expr);
7202 *valid_p = false;
7203 return error_mark_node;
7206 /* Return an expression representing static_cast<TYPE>(EXPR). */
7208 tree
7209 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7211 tree expr = oexpr;
7212 tree result;
7213 bool valid_p;
7215 if (type == error_mark_node || expr == error_mark_node)
7216 return error_mark_node;
7218 bool dependent = (dependent_type_p (type)
7219 || type_dependent_expression_p (expr));
7220 if (dependent)
7222 tmpl:
7223 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7224 /* We don't know if it will or will not have side effects. */
7225 TREE_SIDE_EFFECTS (expr) = 1;
7226 return convert_from_reference (expr);
7228 else if (processing_template_decl)
7229 expr = build_non_dependent_expr (expr);
7231 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7232 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7233 if (!TYPE_REF_P (type)
7234 && TREE_CODE (expr) == NOP_EXPR
7235 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7236 expr = TREE_OPERAND (expr, 0);
7238 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7239 complain);
7240 if (valid_p)
7242 if (result != error_mark_node)
7244 maybe_warn_about_useless_cast (type, expr, complain);
7245 maybe_warn_about_cast_ignoring_quals (type, complain);
7247 if (processing_template_decl)
7248 goto tmpl;
7249 return result;
7252 if (complain & tf_error)
7253 error ("invalid static_cast from type %qT to type %qT",
7254 TREE_TYPE (expr), type);
7255 return error_mark_node;
7258 /* EXPR is an expression with member function or pointer-to-member
7259 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7260 not permitted by ISO C++, but we accept it in some modes. If we
7261 are not in one of those modes, issue a diagnostic. Return the
7262 converted expression. */
7264 tree
7265 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7267 tree intype;
7268 tree decl;
7270 intype = TREE_TYPE (expr);
7271 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7272 || TREE_CODE (intype) == METHOD_TYPE);
7274 if (!(complain & tf_warning_or_error))
7275 return error_mark_node;
7277 if (pedantic || warn_pmf2ptr)
7278 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7279 "converting from %qH to %qI", intype, type);
7281 if (TREE_CODE (intype) == METHOD_TYPE)
7282 expr = build_addr_func (expr, complain);
7283 else if (TREE_CODE (expr) == PTRMEM_CST)
7284 expr = build_address (PTRMEM_CST_MEMBER (expr));
7285 else
7287 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7288 decl = build_address (decl);
7289 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7292 if (expr == error_mark_node)
7293 return error_mark_node;
7295 return build_nop (type, expr);
7298 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7299 constexpr evaluation knows to reject it. */
7301 static tree
7302 build_nop_reinterpret (tree type, tree expr)
7304 tree ret = build_nop (type, expr);
7305 if (ret != expr)
7306 REINTERPRET_CAST_P (ret) = true;
7307 return ret;
7310 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7311 If C_CAST_P is true, this reinterpret cast is being done as part of
7312 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7313 indicate whether or not reinterpret_cast was valid. */
7315 static tree
7316 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7317 bool *valid_p, tsubst_flags_t complain)
7319 tree intype;
7321 /* Assume the cast is invalid. */
7322 if (valid_p)
7323 *valid_p = true;
7325 if (type == error_mark_node || error_operand_p (expr))
7326 return error_mark_node;
7328 intype = TREE_TYPE (expr);
7330 /* Save casted types in the function's used types hash table. */
7331 used_types_insert (type);
7333 /* A prvalue of non-class type is cv-unqualified. */
7334 if (!CLASS_TYPE_P (type))
7335 type = cv_unqualified (type);
7337 /* [expr.reinterpret.cast]
7338 A glvalue expression of type T1 can be cast to the type
7339 "reference to T2" if an expression of type "pointer to T1" can be
7340 explicitly converted to the type "pointer to T2" using a
7341 reinterpret_cast. */
7342 if (TYPE_REF_P (type))
7344 if (TYPE_REF_IS_RVALUE (type))
7346 if (!obvalue_p (expr))
7347 /* Perform the temporary materialization conversion. */
7348 expr = get_target_expr_sfinae (expr, complain);
7350 else if (!lvalue_p (expr))
7352 if (complain & tf_error)
7353 error ("invalid cast of an rvalue expression of type "
7354 "%qT to type %qT",
7355 intype, type);
7356 return error_mark_node;
7359 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7360 "B" are related class types; the reinterpret_cast does not
7361 adjust the pointer. */
7362 if (TYPE_PTR_P (intype)
7363 && (complain & tf_warning)
7364 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7365 COMPARE_BASE | COMPARE_DERIVED)))
7366 warning (0, "casting %qT to %qT does not dereference pointer",
7367 intype, type);
7369 expr = cp_build_addr_expr (expr, complain);
7371 if (warn_strict_aliasing > 2)
7372 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7374 if (expr != error_mark_node)
7375 expr = build_reinterpret_cast_1
7376 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7377 valid_p, complain);
7378 if (expr != error_mark_node)
7379 /* cp_build_indirect_ref isn't right for rvalue refs. */
7380 expr = convert_from_reference (fold_convert (type, expr));
7381 return expr;
7384 /* As a G++ extension, we consider conversions from member
7385 functions, and pointers to member functions to
7386 pointer-to-function and pointer-to-void types. If
7387 -Wno-pmf-conversions has not been specified,
7388 convert_member_func_to_ptr will issue an error message. */
7389 if ((TYPE_PTRMEMFUNC_P (intype)
7390 || TREE_CODE (intype) == METHOD_TYPE)
7391 && TYPE_PTR_P (type)
7392 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7393 || VOID_TYPE_P (TREE_TYPE (type))))
7394 return convert_member_func_to_ptr (type, expr, complain);
7396 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7397 array-to-pointer, and function-to-pointer conversions are
7398 performed. */
7399 expr = decay_conversion (expr, complain);
7401 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7402 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7403 if (TREE_CODE (expr) == NOP_EXPR
7404 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7405 expr = TREE_OPERAND (expr, 0);
7407 if (error_operand_p (expr))
7408 return error_mark_node;
7410 intype = TREE_TYPE (expr);
7412 /* [expr.reinterpret.cast]
7413 A pointer can be converted to any integral type large enough to
7414 hold it. ... A value of type std::nullptr_t can be converted to
7415 an integral type; the conversion has the same meaning and
7416 validity as a conversion of (void*)0 to the integral type. */
7417 if (CP_INTEGRAL_TYPE_P (type)
7418 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7420 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7422 if (complain & tf_error)
7423 permerror (input_location, "cast from %qH to %qI loses precision",
7424 intype, type);
7425 else
7426 return error_mark_node;
7428 if (NULLPTR_TYPE_P (intype))
7429 return build_int_cst (type, 0);
7431 /* [expr.reinterpret.cast]
7432 A value of integral or enumeration type can be explicitly
7433 converted to a pointer. */
7434 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7435 /* OK */
7437 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7438 || TYPE_PTR_OR_PTRMEM_P (type))
7439 && same_type_p (type, intype))
7440 /* DR 799 */
7441 return rvalue (expr);
7442 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7444 if ((complain & tf_warning)
7445 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7446 TREE_TYPE (intype)))
7447 warning (OPT_Wcast_function_type,
7448 "cast between incompatible function types"
7449 " from %qH to %qI", intype, type);
7450 return build_nop_reinterpret (type, expr);
7452 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7454 if ((complain & tf_warning)
7455 && !cxx_safe_function_type_cast_p
7456 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7457 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7458 warning (OPT_Wcast_function_type,
7459 "cast between incompatible pointer to member types"
7460 " from %qH to %qI", intype, type);
7461 return build_nop_reinterpret (type, expr);
7463 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7464 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7466 if (!c_cast_p
7467 && check_for_casting_away_constness (intype, type,
7468 REINTERPRET_CAST_EXPR,
7469 complain))
7470 return error_mark_node;
7471 /* Warn about possible alignment problems. */
7472 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7473 && (complain & tf_warning)
7474 && !VOID_TYPE_P (type)
7475 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7476 && COMPLETE_TYPE_P (TREE_TYPE (type))
7477 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7478 && min_align_of_type (TREE_TYPE (type))
7479 > min_align_of_type (TREE_TYPE (intype)))
7480 warning (OPT_Wcast_align, "cast from %qH to %qI "
7481 "increases required alignment of target type", intype, type);
7483 if (warn_strict_aliasing <= 2)
7484 /* strict_aliasing_warning STRIP_NOPs its expr. */
7485 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7487 return build_nop_reinterpret (type, expr);
7489 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7490 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7492 if (complain & tf_warning)
7493 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7494 object pointer type or vice versa is conditionally-supported." */
7495 warning (OPT_Wconditionally_supported,
7496 "casting between pointer-to-function and pointer-to-object "
7497 "is conditionally-supported");
7498 return build_nop_reinterpret (type, expr);
7500 else if (VECTOR_TYPE_P (type))
7501 return convert_to_vector (type, expr);
7502 else if (VECTOR_TYPE_P (intype)
7503 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7504 return convert_to_integer_nofold (type, expr);
7505 else
7507 if (valid_p)
7508 *valid_p = false;
7509 if (complain & tf_error)
7510 error ("invalid cast from type %qT to type %qT", intype, type);
7511 return error_mark_node;
7514 expr = cp_convert (type, expr, complain);
7515 if (TREE_CODE (expr) == NOP_EXPR)
7516 /* Mark any nop_expr that created as a reintepret_cast. */
7517 REINTERPRET_CAST_P (expr) = true;
7518 return expr;
7521 tree
7522 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7524 tree r;
7526 if (type == error_mark_node || expr == error_mark_node)
7527 return error_mark_node;
7529 if (processing_template_decl)
7531 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7533 if (!TREE_SIDE_EFFECTS (t)
7534 && type_dependent_expression_p (expr))
7535 /* There might turn out to be side effects inside expr. */
7536 TREE_SIDE_EFFECTS (t) = 1;
7537 return convert_from_reference (t);
7540 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7541 /*valid_p=*/NULL, complain);
7542 if (r != error_mark_node)
7544 maybe_warn_about_useless_cast (type, expr, complain);
7545 maybe_warn_about_cast_ignoring_quals (type, complain);
7547 return r;
7550 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7551 return an appropriate expression. Otherwise, return
7552 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7553 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7554 performing a C-style cast, its value upon return will indicate
7555 whether or not the conversion succeeded. */
7557 static tree
7558 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7559 bool *valid_p)
7561 tree src_type;
7562 tree reference_type;
7564 /* Callers are responsible for handling error_mark_node as a
7565 destination type. */
7566 gcc_assert (dst_type != error_mark_node);
7567 /* In a template, callers should be building syntactic
7568 representations of casts, not using this machinery. */
7569 gcc_assert (!processing_template_decl);
7571 /* Assume the conversion is invalid. */
7572 if (valid_p)
7573 *valid_p = false;
7575 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7577 if (complain & tf_error)
7578 error ("invalid use of const_cast with type %qT, "
7579 "which is not a pointer, "
7580 "reference, nor a pointer-to-data-member type", dst_type);
7581 return error_mark_node;
7584 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7586 if (complain & tf_error)
7587 error ("invalid use of const_cast with type %qT, which is a pointer "
7588 "or reference to a function type", dst_type);
7589 return error_mark_node;
7592 /* A prvalue of non-class type is cv-unqualified. */
7593 dst_type = cv_unqualified (dst_type);
7595 /* Save casted types in the function's used types hash table. */
7596 used_types_insert (dst_type);
7598 src_type = TREE_TYPE (expr);
7599 /* Expressions do not really have reference types. */
7600 if (TYPE_REF_P (src_type))
7601 src_type = TREE_TYPE (src_type);
7603 /* [expr.const.cast]
7605 For two object types T1 and T2, if a pointer to T1 can be explicitly
7606 converted to the type "pointer to T2" using a const_cast, then the
7607 following conversions can also be made:
7609 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7610 type T2 using the cast const_cast<T2&>;
7612 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7613 type T2 using the cast const_cast<T2&&>; and
7615 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7616 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7618 if (TYPE_REF_P (dst_type))
7620 reference_type = dst_type;
7621 if (!TYPE_REF_IS_RVALUE (dst_type)
7622 ? lvalue_p (expr)
7623 : obvalue_p (expr))
7624 /* OK. */;
7625 else
7627 if (complain & tf_error)
7628 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7629 src_type, dst_type);
7630 return error_mark_node;
7632 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7633 src_type = build_pointer_type (src_type);
7635 else
7637 reference_type = NULL_TREE;
7638 /* If the destination type is not a reference type, the
7639 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7640 conversions are performed. */
7641 src_type = type_decays_to (src_type);
7642 if (src_type == error_mark_node)
7643 return error_mark_node;
7646 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7648 if (comp_ptr_ttypes_const (dst_type, src_type))
7650 if (valid_p)
7652 *valid_p = true;
7653 /* This cast is actually a C-style cast. Issue a warning if
7654 the user is making a potentially unsafe cast. */
7655 check_for_casting_away_constness (src_type, dst_type,
7656 CAST_EXPR, complain);
7657 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7658 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7659 && (complain & tf_warning)
7660 && min_align_of_type (TREE_TYPE (dst_type))
7661 > min_align_of_type (TREE_TYPE (src_type)))
7662 warning (OPT_Wcast_align, "cast from %qH to %qI "
7663 "increases required alignment of target type",
7664 src_type, dst_type);
7666 if (reference_type)
7668 expr = cp_build_addr_expr (expr, complain);
7669 if (expr == error_mark_node)
7670 return error_mark_node;
7671 expr = build_nop (reference_type, expr);
7672 return convert_from_reference (expr);
7674 else
7676 expr = decay_conversion (expr, complain);
7677 if (expr == error_mark_node)
7678 return error_mark_node;
7680 /* build_c_cast puts on a NOP_EXPR to make the result not an
7681 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7682 non-lvalue context. */
7683 if (TREE_CODE (expr) == NOP_EXPR
7684 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7685 expr = TREE_OPERAND (expr, 0);
7686 return build_nop (dst_type, expr);
7689 else if (valid_p
7690 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7691 TREE_TYPE (src_type)))
7692 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7693 complain);
7696 if (complain & tf_error)
7697 error ("invalid const_cast from type %qT to type %qT",
7698 src_type, dst_type);
7699 return error_mark_node;
7702 tree
7703 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7705 tree r;
7707 if (type == error_mark_node || error_operand_p (expr))
7708 return error_mark_node;
7710 if (processing_template_decl)
7712 tree t = build_min (CONST_CAST_EXPR, type, expr);
7714 if (!TREE_SIDE_EFFECTS (t)
7715 && type_dependent_expression_p (expr))
7716 /* There might turn out to be side effects inside expr. */
7717 TREE_SIDE_EFFECTS (t) = 1;
7718 return convert_from_reference (t);
7721 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7722 if (r != error_mark_node)
7724 maybe_warn_about_useless_cast (type, expr, complain);
7725 maybe_warn_about_cast_ignoring_quals (type, complain);
7727 return r;
7730 /* Like cp_build_c_cast, but for the c-common bits. */
7732 tree
7733 build_c_cast (location_t /*loc*/, tree type, tree expr)
7735 return cp_build_c_cast (type, expr, tf_warning_or_error);
7738 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7739 preserve location information even for tree nodes that don't
7740 support it. */
7742 cp_expr
7743 build_c_cast (location_t loc, tree type, cp_expr expr)
7745 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7746 result.set_location (loc);
7747 return result;
7750 /* Build an expression representing an explicit C-style cast to type
7751 TYPE of expression EXPR. */
7753 tree
7754 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7756 tree value = expr;
7757 tree result;
7758 bool valid_p;
7760 if (type == error_mark_node || error_operand_p (expr))
7761 return error_mark_node;
7763 if (processing_template_decl)
7765 tree t = build_min (CAST_EXPR, type,
7766 tree_cons (NULL_TREE, value, NULL_TREE));
7767 /* We don't know if it will or will not have side effects. */
7768 TREE_SIDE_EFFECTS (t) = 1;
7769 return convert_from_reference (t);
7772 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7773 'Class') should always be retained, because this information aids
7774 in method lookup. */
7775 if (objc_is_object_ptr (type)
7776 && objc_is_object_ptr (TREE_TYPE (expr)))
7777 return build_nop (type, expr);
7779 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7780 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7781 if (!TYPE_REF_P (type)
7782 && TREE_CODE (value) == NOP_EXPR
7783 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7784 value = TREE_OPERAND (value, 0);
7786 if (TREE_CODE (type) == ARRAY_TYPE)
7788 /* Allow casting from T1* to T2[] because Cfront allows it.
7789 NIHCL uses it. It is not valid ISO C++ however. */
7790 if (TYPE_PTR_P (TREE_TYPE (expr)))
7792 if (complain & tf_error)
7793 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7794 else
7795 return error_mark_node;
7796 type = build_pointer_type (TREE_TYPE (type));
7798 else
7800 if (complain & tf_error)
7801 error ("ISO C++ forbids casting to an array type %qT", type);
7802 return error_mark_node;
7806 if (TREE_CODE (type) == FUNCTION_TYPE
7807 || TREE_CODE (type) == METHOD_TYPE)
7809 if (complain & tf_error)
7810 error ("invalid cast to function type %qT", type);
7811 return error_mark_node;
7814 if (TYPE_PTR_P (type)
7815 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7816 /* Casting to an integer of smaller size is an error detected elsewhere. */
7817 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7818 /* Don't warn about converting any constant. */
7819 && !TREE_CONSTANT (value))
7820 warning_at (input_location, OPT_Wint_to_pointer_cast,
7821 "cast to pointer from integer of different size");
7823 /* A C-style cast can be a const_cast. */
7824 result = build_const_cast_1 (type, value, complain & tf_warning,
7825 &valid_p);
7826 if (valid_p)
7828 if (result != error_mark_node)
7830 maybe_warn_about_useless_cast (type, value, complain);
7831 maybe_warn_about_cast_ignoring_quals (type, complain);
7833 return result;
7836 /* Or a static cast. */
7837 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7838 &valid_p, complain);
7839 /* Or a reinterpret_cast. */
7840 if (!valid_p)
7841 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7842 &valid_p, complain);
7843 /* The static_cast or reinterpret_cast may be followed by a
7844 const_cast. */
7845 if (valid_p
7846 /* A valid cast may result in errors if, for example, a
7847 conversion to an ambiguous base class is required. */
7848 && !error_operand_p (result))
7850 tree result_type;
7852 maybe_warn_about_useless_cast (type, value, complain);
7853 maybe_warn_about_cast_ignoring_quals (type, complain);
7855 /* Non-class rvalues always have cv-unqualified type. */
7856 if (!CLASS_TYPE_P (type))
7857 type = TYPE_MAIN_VARIANT (type);
7858 result_type = TREE_TYPE (result);
7859 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
7860 result_type = TYPE_MAIN_VARIANT (result_type);
7861 /* If the type of RESULT does not match TYPE, perform a
7862 const_cast to make it match. If the static_cast or
7863 reinterpret_cast succeeded, we will differ by at most
7864 cv-qualification, so the follow-on const_cast is guaranteed
7865 to succeed. */
7866 if (!same_type_p (non_reference (type), non_reference (result_type)))
7868 result = build_const_cast_1 (type, result, false, &valid_p);
7869 gcc_assert (valid_p);
7871 return result;
7874 return error_mark_node;
7877 /* For use from the C common bits. */
7878 tree
7879 build_modify_expr (location_t location,
7880 tree lhs, tree /*lhs_origtype*/,
7881 enum tree_code modifycode,
7882 location_t /*rhs_location*/, tree rhs,
7883 tree /*rhs_origtype*/)
7885 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7886 tf_warning_or_error);
7889 /* Build an assignment expression of lvalue LHS from value RHS.
7890 MODIFYCODE is the code for a binary operator that we use
7891 to combine the old value of LHS with RHS to get the new value.
7892 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7894 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7896 tree
7897 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7898 tree rhs, tsubst_flags_t complain)
7900 lhs = mark_lvalue_use_nonread (lhs);
7902 tree result = NULL_TREE;
7903 tree newrhs = rhs;
7904 tree lhstype = TREE_TYPE (lhs);
7905 tree olhs = lhs;
7906 tree olhstype = lhstype;
7907 bool plain_assign = (modifycode == NOP_EXPR);
7908 bool compound_side_effects_p = false;
7909 tree preeval = NULL_TREE;
7911 /* Avoid duplicate error messages from operands that had errors. */
7912 if (error_operand_p (lhs) || error_operand_p (rhs))
7913 return error_mark_node;
7915 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7917 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7918 compound_side_effects_p = true;
7919 lhs = TREE_OPERAND (lhs, 1);
7922 /* Handle control structure constructs used as "lvalues". Note that we
7923 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7924 switch (TREE_CODE (lhs))
7926 /* Handle --foo = 5; as these are valid constructs in C++. */
7927 case PREDECREMENT_EXPR:
7928 case PREINCREMENT_EXPR:
7929 if (compound_side_effects_p)
7930 newrhs = rhs = stabilize_expr (rhs, &preeval);
7931 lhs = genericize_compound_lvalue (lhs);
7932 maybe_add_compound:
7933 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7934 and looked through the COMPOUND_EXPRs, readd them now around
7935 the resulting lhs. */
7936 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7938 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7939 tree *ptr = &TREE_OPERAND (lhs, 1);
7940 for (olhs = TREE_OPERAND (olhs, 1);
7941 TREE_CODE (olhs) == COMPOUND_EXPR;
7942 olhs = TREE_OPERAND (olhs, 1))
7944 *ptr = build2 (COMPOUND_EXPR, lhstype,
7945 TREE_OPERAND (olhs, 0), *ptr);
7946 ptr = &TREE_OPERAND (*ptr, 1);
7949 break;
7951 case MODIFY_EXPR:
7952 if (compound_side_effects_p)
7953 newrhs = rhs = stabilize_expr (rhs, &preeval);
7954 lhs = genericize_compound_lvalue (lhs);
7955 goto maybe_add_compound;
7957 case MIN_EXPR:
7958 case MAX_EXPR:
7959 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7960 when neither operand has side-effects. */
7961 if (!lvalue_or_else (lhs, lv_assign, complain))
7962 return error_mark_node;
7964 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7965 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7967 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7968 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7969 boolean_type_node,
7970 TREE_OPERAND (lhs, 0),
7971 TREE_OPERAND (lhs, 1)),
7972 TREE_OPERAND (lhs, 0),
7973 TREE_OPERAND (lhs, 1));
7974 gcc_fallthrough ();
7976 /* Handle (a ? b : c) used as an "lvalue". */
7977 case COND_EXPR:
7979 /* Produce (a ? (b = rhs) : (c = rhs))
7980 except that the RHS goes through a save-expr
7981 so the code to compute it is only emitted once. */
7982 tree cond;
7984 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7986 if (complain & tf_error)
7987 error ("void value not ignored as it ought to be");
7988 return error_mark_node;
7991 rhs = stabilize_expr (rhs, &preeval);
7993 /* Check this here to avoid odd errors when trying to convert
7994 a throw to the type of the COND_EXPR. */
7995 if (!lvalue_or_else (lhs, lv_assign, complain))
7996 return error_mark_node;
7998 cond = build_conditional_expr
7999 (input_location, TREE_OPERAND (lhs, 0),
8000 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
8001 modifycode, rhs, complain),
8002 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
8003 modifycode, rhs, complain),
8004 complain);
8006 if (cond == error_mark_node)
8007 return cond;
8008 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8009 and looked through the COMPOUND_EXPRs, readd them now around
8010 the resulting cond before adding the preevaluated rhs. */
8011 if (TREE_CODE (olhs) == COMPOUND_EXPR)
8013 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8014 TREE_OPERAND (olhs, 0), cond);
8015 tree *ptr = &TREE_OPERAND (cond, 1);
8016 for (olhs = TREE_OPERAND (olhs, 1);
8017 TREE_CODE (olhs) == COMPOUND_EXPR;
8018 olhs = TREE_OPERAND (olhs, 1))
8020 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8021 TREE_OPERAND (olhs, 0), *ptr);
8022 ptr = &TREE_OPERAND (*ptr, 1);
8025 /* Make sure the code to compute the rhs comes out
8026 before the split. */
8027 result = cond;
8028 goto ret;
8031 default:
8032 lhs = olhs;
8033 break;
8036 if (modifycode == INIT_EXPR)
8038 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8039 /* Do the default thing. */;
8040 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8042 /* Compound literal. */
8043 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8044 /* Call convert to generate an error; see PR 11063. */
8045 rhs = convert (lhstype, rhs);
8046 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8047 TREE_SIDE_EFFECTS (result) = 1;
8048 goto ret;
8050 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8051 /* Do the default thing. */;
8052 else
8054 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8055 result = build_special_member_call (lhs, complete_ctor_identifier,
8056 &rhs_vec, lhstype, LOOKUP_NORMAL,
8057 complain);
8058 release_tree_vector (rhs_vec);
8059 if (result == NULL_TREE)
8060 return error_mark_node;
8061 goto ret;
8064 else
8066 lhs = require_complete_type_sfinae (lhs, complain);
8067 if (lhs == error_mark_node)
8068 return error_mark_node;
8070 if (modifycode == NOP_EXPR)
8072 if (c_dialect_objc ())
8074 result = objc_maybe_build_modify_expr (lhs, rhs);
8075 if (result)
8076 goto ret;
8079 /* `operator=' is not an inheritable operator. */
8080 if (! MAYBE_CLASS_TYPE_P (lhstype))
8081 /* Do the default thing. */;
8082 else
8084 result = build_new_op (input_location, MODIFY_EXPR,
8085 LOOKUP_NORMAL, lhs, rhs,
8086 make_node (NOP_EXPR), /*overload=*/NULL,
8087 complain);
8088 if (result == NULL_TREE)
8089 return error_mark_node;
8090 goto ret;
8092 lhstype = olhstype;
8094 else
8096 tree init = NULL_TREE;
8098 /* A binary op has been requested. Combine the old LHS
8099 value with the RHS producing the value we should actually
8100 store into the LHS. */
8101 gcc_assert (!((TYPE_REF_P (lhstype)
8102 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8103 || MAYBE_CLASS_TYPE_P (lhstype)));
8105 /* Preevaluate the RHS to make sure its evaluation is complete
8106 before the lvalue-to-rvalue conversion of the LHS:
8108 [expr.ass] With respect to an indeterminately-sequenced
8109 function call, the operation of a compound assignment is a
8110 single evaluation. [ Note: Therefore, a function call shall
8111 not intervene between the lvalue-to-rvalue conversion and the
8112 side effect associated with any single compound assignment
8113 operator. -- end note ] */
8114 lhs = cp_stabilize_reference (lhs);
8115 rhs = decay_conversion (rhs, complain);
8116 if (rhs == error_mark_node)
8117 return error_mark_node;
8118 rhs = stabilize_expr (rhs, &init);
8119 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8120 if (newrhs == error_mark_node)
8122 if (complain & tf_error)
8123 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8124 TREE_TYPE (lhs), TREE_TYPE (rhs));
8125 return error_mark_node;
8128 if (init)
8129 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8131 /* Now it looks like a plain assignment. */
8132 modifycode = NOP_EXPR;
8133 if (c_dialect_objc ())
8135 result = objc_maybe_build_modify_expr (lhs, newrhs);
8136 if (result)
8137 goto ret;
8140 gcc_assert (!TYPE_REF_P (lhstype));
8141 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
8144 /* The left-hand side must be an lvalue. */
8145 if (!lvalue_or_else (lhs, lv_assign, complain))
8146 return error_mark_node;
8148 /* Warn about modifying something that is `const'. Don't warn if
8149 this is initialization. */
8150 if (modifycode != INIT_EXPR
8151 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8152 /* Functions are not modifiable, even though they are
8153 lvalues. */
8154 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8155 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8156 /* If it's an aggregate and any field is const, then it is
8157 effectively const. */
8158 || (CLASS_TYPE_P (lhstype)
8159 && C_TYPE_FIELDS_READONLY (lhstype))))
8161 if (complain & tf_error)
8162 cxx_readonly_error (lhs, lv_assign);
8163 return error_mark_node;
8166 /* If storing into a structure or union member, it may have been given a
8167 lowered bitfield type. We need to convert to the declared type first,
8168 so retrieve it now. */
8170 olhstype = unlowered_expr_type (lhs);
8172 /* Convert new value to destination type. */
8174 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8176 int from_array;
8178 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8180 if (modifycode != INIT_EXPR)
8182 if (complain & tf_error)
8183 error ("assigning to an array from an initializer list");
8184 return error_mark_node;
8186 if (check_array_initializer (lhs, lhstype, newrhs))
8187 return error_mark_node;
8188 newrhs = digest_init (lhstype, newrhs, complain);
8189 if (newrhs == error_mark_node)
8190 return error_mark_node;
8193 /* C++11 8.5/17: "If the destination type is an array of characters,
8194 an array of char16_t, an array of char32_t, or an array of wchar_t,
8195 and the initializer is a string literal...". */
8196 else if (TREE_CODE (newrhs) == STRING_CST
8197 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8198 && modifycode == INIT_EXPR)
8200 newrhs = digest_init (lhstype, newrhs, complain);
8201 if (newrhs == error_mark_node)
8202 return error_mark_node;
8205 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8206 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8208 if (complain & tf_error)
8209 error ("incompatible types in assignment of %qT to %qT",
8210 TREE_TYPE (rhs), lhstype);
8211 return error_mark_node;
8214 /* Allow array assignment in compiler-generated code. */
8215 else if (!current_function_decl
8216 || !DECL_DEFAULTED_FN (current_function_decl))
8218 /* This routine is used for both initialization and assignment.
8219 Make sure the diagnostic message differentiates the context. */
8220 if (complain & tf_error)
8222 if (modifycode == INIT_EXPR)
8223 error ("array used as initializer");
8224 else
8225 error ("invalid array assignment");
8227 return error_mark_node;
8230 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8231 ? 1 + (modifycode != INIT_EXPR): 0;
8232 result = build_vec_init (lhs, NULL_TREE, newrhs,
8233 /*explicit_value_init_p=*/false,
8234 from_array, complain);
8235 goto ret;
8238 if (modifycode == INIT_EXPR)
8239 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8240 LOOKUP_ONLYCONVERTING. */
8241 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8242 ICR_INIT, NULL_TREE, 0,
8243 complain);
8244 else
8245 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8246 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8248 if (!same_type_p (lhstype, olhstype))
8249 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8251 if (modifycode != INIT_EXPR)
8253 if (TREE_CODE (newrhs) == CALL_EXPR
8254 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8255 newrhs = build_cplus_new (lhstype, newrhs, complain);
8257 /* Can't initialize directly from a TARGET_EXPR, since that would
8258 cause the lhs to be constructed twice, and possibly result in
8259 accidental self-initialization. So we force the TARGET_EXPR to be
8260 expanded without a target. */
8261 if (TREE_CODE (newrhs) == TARGET_EXPR)
8262 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8263 TREE_OPERAND (newrhs, 0));
8266 if (newrhs == error_mark_node)
8267 return error_mark_node;
8269 if (c_dialect_objc () && flag_objc_gc)
8271 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8273 if (result)
8274 goto ret;
8277 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8278 lhstype, lhs, newrhs);
8280 TREE_SIDE_EFFECTS (result) = 1;
8281 if (!plain_assign)
8282 TREE_NO_WARNING (result) = 1;
8284 ret:
8285 if (preeval)
8286 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8287 return result;
8290 cp_expr
8291 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8292 tree rhs, tsubst_flags_t complain)
8294 tree orig_lhs = lhs;
8295 tree orig_rhs = rhs;
8296 tree overload = NULL_TREE;
8297 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8299 if (processing_template_decl)
8301 if (modifycode == NOP_EXPR
8302 || type_dependent_expression_p (lhs)
8303 || type_dependent_expression_p (rhs))
8304 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8305 build_min_nt_loc (loc, modifycode, NULL_TREE,
8306 NULL_TREE), rhs);
8308 lhs = build_non_dependent_expr (lhs);
8309 rhs = build_non_dependent_expr (rhs);
8312 if (modifycode != NOP_EXPR)
8314 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8315 lhs, rhs, op, &overload, complain);
8316 if (rval)
8318 if (rval == error_mark_node)
8319 return rval;
8320 TREE_NO_WARNING (rval) = 1;
8321 if (processing_template_decl)
8323 if (overload != NULL_TREE)
8324 return (build_min_non_dep_op_overload
8325 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8327 return (build_min_non_dep
8328 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8330 return rval;
8333 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8336 /* Helper function for get_delta_difference which assumes FROM is a base
8337 class of TO. Returns a delta for the conversion of pointer-to-member
8338 of FROM to pointer-to-member of TO. If the conversion is invalid and
8339 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8340 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8341 If C_CAST_P is true, this conversion is taking place as part of a
8342 C-style cast. */
8344 static tree
8345 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8346 tsubst_flags_t complain)
8348 tree binfo;
8349 base_kind kind;
8351 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8352 &kind, complain);
8354 if (binfo == error_mark_node)
8356 if (!(complain & tf_error))
8357 return error_mark_node;
8359 error (" in pointer to member function conversion");
8360 return size_zero_node;
8362 else if (binfo)
8364 if (kind != bk_via_virtual)
8365 return BINFO_OFFSET (binfo);
8366 else
8367 /* FROM is a virtual base class of TO. Issue an error or warning
8368 depending on whether or not this is a reinterpret cast. */
8370 if (!(complain & tf_error))
8371 return error_mark_node;
8373 error ("pointer to member conversion via virtual base %qT",
8374 BINFO_TYPE (binfo_from_vbase (binfo)));
8376 return size_zero_node;
8379 else
8380 return NULL_TREE;
8383 /* Get difference in deltas for different pointer to member function
8384 types. If the conversion is invalid and tf_error is not set in
8385 COMPLAIN, returns error_mark_node, otherwise returns an integer
8386 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8387 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8388 conversions as well. If C_CAST_P is true this conversion is taking
8389 place as part of a C-style cast.
8391 Note that the naming of FROM and TO is kind of backwards; the return
8392 value is what we add to a TO in order to get a FROM. They are named
8393 this way because we call this function to find out how to convert from
8394 a pointer to member of FROM to a pointer to member of TO. */
8396 static tree
8397 get_delta_difference (tree from, tree to,
8398 bool allow_inverse_p,
8399 bool c_cast_p, tsubst_flags_t complain)
8401 tree result;
8403 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8404 /* Pointer to member of incomplete class is permitted*/
8405 result = size_zero_node;
8406 else
8407 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8409 if (result == error_mark_node)
8410 return error_mark_node;
8412 if (!result)
8414 if (!allow_inverse_p)
8416 if (!(complain & tf_error))
8417 return error_mark_node;
8419 error_not_base_type (from, to);
8420 error (" in pointer to member conversion");
8421 result = size_zero_node;
8423 else
8425 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8427 if (result == error_mark_node)
8428 return error_mark_node;
8430 if (result)
8431 result = size_diffop_loc (input_location,
8432 size_zero_node, result);
8433 else
8435 if (!(complain & tf_error))
8436 return error_mark_node;
8438 error_not_base_type (from, to);
8439 error (" in pointer to member conversion");
8440 result = size_zero_node;
8445 return convert_to_integer (ptrdiff_type_node, result);
8448 /* Return a constructor for the pointer-to-member-function TYPE using
8449 the other components as specified. */
8451 tree
8452 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8454 tree u = NULL_TREE;
8455 tree delta_field;
8456 tree pfn_field;
8457 vec<constructor_elt, va_gc> *v;
8459 /* Pull the FIELD_DECLs out of the type. */
8460 pfn_field = TYPE_FIELDS (type);
8461 delta_field = DECL_CHAIN (pfn_field);
8463 /* Make sure DELTA has the type we want. */
8464 delta = convert_and_check (input_location, delta_type_node, delta);
8466 /* Convert to the correct target type if necessary. */
8467 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8469 /* Finish creating the initializer. */
8470 vec_alloc (v, 2);
8471 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8472 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8473 u = build_constructor (type, v);
8474 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8475 TREE_STATIC (u) = (TREE_CONSTANT (u)
8476 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8477 != NULL_TREE)
8478 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8479 != NULL_TREE));
8480 return u;
8483 /* Build a constructor for a pointer to member function. It can be
8484 used to initialize global variables, local variable, or used
8485 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8486 want to be.
8488 If FORCE is nonzero, then force this conversion, even if
8489 we would rather not do it. Usually set when using an explicit
8490 cast. A C-style cast is being processed iff C_CAST_P is true.
8492 Return error_mark_node, if something goes wrong. */
8494 tree
8495 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8496 tsubst_flags_t complain)
8498 tree fn;
8499 tree pfn_type;
8500 tree to_type;
8502 if (error_operand_p (pfn))
8503 return error_mark_node;
8505 pfn_type = TREE_TYPE (pfn);
8506 to_type = build_ptrmemfunc_type (type);
8508 /* Handle multiple conversions of pointer to member functions. */
8509 if (TYPE_PTRMEMFUNC_P (pfn_type))
8511 tree delta = NULL_TREE;
8512 tree npfn = NULL_TREE;
8513 tree n;
8515 if (!force
8516 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8517 LOOKUP_NORMAL, complain))
8519 if (complain & tf_error)
8520 error ("invalid conversion to type %qT from type %qT",
8521 to_type, pfn_type);
8522 else
8523 return error_mark_node;
8526 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8527 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8528 force,
8529 c_cast_p, complain);
8530 if (n == error_mark_node)
8531 return error_mark_node;
8533 /* We don't have to do any conversion to convert a
8534 pointer-to-member to its own type. But, we don't want to
8535 just return a PTRMEM_CST if there's an explicit cast; that
8536 cast should make the expression an invalid template argument. */
8537 if (TREE_CODE (pfn) != PTRMEM_CST)
8539 if (same_type_p (to_type, pfn_type))
8540 return pfn;
8541 else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
8542 return build_reinterpret_cast (to_type, pfn,
8543 complain);
8546 if (TREE_SIDE_EFFECTS (pfn))
8547 pfn = save_expr (pfn);
8549 /* Obtain the function pointer and the current DELTA. */
8550 if (TREE_CODE (pfn) == PTRMEM_CST)
8551 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8552 else
8554 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8555 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8558 /* Just adjust the DELTA field. */
8559 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8560 (TREE_TYPE (delta), ptrdiff_type_node));
8561 if (!integer_zerop (n))
8563 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8564 n = cp_build_binary_op (input_location,
8565 LSHIFT_EXPR, n, integer_one_node,
8566 complain);
8567 delta = cp_build_binary_op (input_location,
8568 PLUS_EXPR, delta, n, complain);
8570 return build_ptrmemfunc1 (to_type, delta, npfn);
8573 /* Handle null pointer to member function conversions. */
8574 if (null_ptr_cst_p (pfn))
8576 pfn = cp_build_c_cast (type, pfn, complain);
8577 return build_ptrmemfunc1 (to_type,
8578 integer_zero_node,
8579 pfn);
8582 if (type_unknown_p (pfn))
8583 return instantiate_type (type, pfn, complain);
8585 fn = TREE_OPERAND (pfn, 0);
8586 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8587 /* In a template, we will have preserved the
8588 OFFSET_REF. */
8589 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8590 return make_ptrmem_cst (to_type, fn);
8593 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8594 given by CST.
8596 ??? There is no consistency as to the types returned for the above
8597 values. Some code acts as if it were a sizetype and some as if it were
8598 integer_type_node. */
8600 void
8601 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8603 tree type = TREE_TYPE (cst);
8604 tree fn = PTRMEM_CST_MEMBER (cst);
8605 tree ptr_class, fn_class;
8607 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8609 /* The class that the function belongs to. */
8610 fn_class = DECL_CONTEXT (fn);
8612 /* The class that we're creating a pointer to member of. */
8613 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8615 /* First, calculate the adjustment to the function's class. */
8616 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8617 /*c_cast_p=*/0, tf_warning_or_error);
8619 if (!DECL_VIRTUAL_P (fn))
8620 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8621 build_addr_func (fn, tf_warning_or_error));
8622 else
8624 /* If we're dealing with a virtual function, we have to adjust 'this'
8625 again, to point to the base which provides the vtable entry for
8626 fn; the call will do the opposite adjustment. */
8627 tree orig_class = DECL_CONTEXT (fn);
8628 tree binfo = binfo_or_else (orig_class, fn_class);
8629 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8630 *delta, BINFO_OFFSET (binfo));
8632 /* We set PFN to the vtable offset at which the function can be
8633 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8634 case delta is shifted left, and then incremented). */
8635 *pfn = DECL_VINDEX (fn);
8636 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8637 TYPE_SIZE_UNIT (vtable_entry_type));
8639 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8641 case ptrmemfunc_vbit_in_pfn:
8642 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8643 integer_one_node);
8644 break;
8646 case ptrmemfunc_vbit_in_delta:
8647 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8648 *delta, integer_one_node);
8649 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8650 *delta, integer_one_node);
8651 break;
8653 default:
8654 gcc_unreachable ();
8657 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8661 /* Return an expression for PFN from the pointer-to-member function
8662 given by T. */
8664 static tree
8665 pfn_from_ptrmemfunc (tree t)
8667 if (TREE_CODE (t) == PTRMEM_CST)
8669 tree delta;
8670 tree pfn;
8672 expand_ptrmemfunc_cst (t, &delta, &pfn);
8673 if (pfn)
8674 return pfn;
8677 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8680 /* Return an expression for DELTA from the pointer-to-member function
8681 given by T. */
8683 static tree
8684 delta_from_ptrmemfunc (tree t)
8686 if (TREE_CODE (t) == PTRMEM_CST)
8688 tree delta;
8689 tree pfn;
8691 expand_ptrmemfunc_cst (t, &delta, &pfn);
8692 if (delta)
8693 return delta;
8696 return build_ptrmemfunc_access_expr (t, delta_identifier);
8699 /* Convert value RHS to type TYPE as preparation for an assignment to
8700 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8701 implicit conversion is. If FNDECL is non-NULL, we are doing the
8702 conversion in order to pass the PARMNUMth argument of FNDECL.
8703 If FNDECL is NULL, we are doing the conversion in function pointer
8704 argument passing, conversion in initialization, etc. */
8706 static tree
8707 convert_for_assignment (tree type, tree rhs,
8708 impl_conv_rhs errtype, tree fndecl, int parmnum,
8709 tsubst_flags_t complain, int flags)
8711 tree rhstype;
8712 enum tree_code coder;
8714 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8715 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8716 rhs = TREE_OPERAND (rhs, 0);
8718 /* Handle [dcl.init.list] direct-list-initialization from
8719 single element of enumeration with a fixed underlying type. */
8720 if (is_direct_enum_init (type, rhs))
8722 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8723 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8725 warning_sentinel w (warn_useless_cast);
8726 warning_sentinel w2 (warn_ignored_qualifiers);
8727 rhs = cp_build_c_cast (type, elt, complain);
8729 else
8730 rhs = error_mark_node;
8733 rhstype = TREE_TYPE (rhs);
8734 coder = TREE_CODE (rhstype);
8736 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8737 && vector_types_convertible_p (type, rhstype, true))
8739 rhs = mark_rvalue_use (rhs);
8740 return convert (type, rhs);
8743 if (rhs == error_mark_node || rhstype == error_mark_node)
8744 return error_mark_node;
8745 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8746 return error_mark_node;
8748 /* The RHS of an assignment cannot have void type. */
8749 if (coder == VOID_TYPE)
8751 if (complain & tf_error)
8752 error ("void value not ignored as it ought to be");
8753 return error_mark_node;
8756 if (c_dialect_objc ())
8758 int parmno;
8759 tree selector;
8760 tree rname = fndecl;
8762 switch (errtype)
8764 case ICR_ASSIGN:
8765 parmno = -1;
8766 break;
8767 case ICR_INIT:
8768 parmno = -2;
8769 break;
8770 default:
8771 selector = objc_message_selector ();
8772 parmno = parmnum;
8773 if (selector && parmno > 1)
8775 rname = selector;
8776 parmno -= 1;
8780 if (objc_compare_types (type, rhstype, parmno, rname))
8782 rhs = mark_rvalue_use (rhs);
8783 return convert (type, rhs);
8787 /* [expr.ass]
8789 The expression is implicitly converted (clause _conv_) to the
8790 cv-unqualified type of the left operand.
8792 We allow bad conversions here because by the time we get to this point
8793 we are committed to doing the conversion. If we end up doing a bad
8794 conversion, convert_like will complain. */
8795 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8797 /* When -Wno-pmf-conversions is use, we just silently allow
8798 conversions from pointers-to-members to plain pointers. If
8799 the conversion doesn't work, cp_convert will complain. */
8800 if (!warn_pmf2ptr
8801 && TYPE_PTR_P (type)
8802 && TYPE_PTRMEMFUNC_P (rhstype))
8803 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8804 else
8806 if (complain & tf_error)
8808 /* If the right-hand side has unknown type, then it is an
8809 overloaded function. Call instantiate_type to get error
8810 messages. */
8811 if (rhstype == unknown_type_node)
8813 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8814 /* -fpermissive might allow this; recurse. */
8815 if (!seen_error ())
8816 return convert_for_assignment (type, r, errtype, fndecl,
8817 parmnum, complain, flags);
8819 else if (fndecl)
8821 auto_diagnostic_group d;
8822 location_t loc = cp_expr_location (rhs);
8823 range_label_for_type_mismatch rhs_label (rhstype, type);
8824 range_label *label = &rhs_label;
8825 if (loc == UNKNOWN_LOCATION)
8827 loc = input_location;
8828 label = NULL;
8830 gcc_rich_location richloc (loc, label);
8831 error_at (&richloc,
8832 "cannot convert %qH to %qI",
8833 rhstype, type);
8834 inform (get_fndecl_argument_location (fndecl, parmnum),
8835 " initializing argument %P of %qD", parmnum, fndecl);
8837 else
8838 switch (errtype)
8840 case ICR_DEFAULT_ARGUMENT:
8841 error ("cannot convert %qH to %qI in default argument",
8842 rhstype, type);
8843 break;
8844 case ICR_ARGPASS:
8845 error ("cannot convert %qH to %qI in argument passing",
8846 rhstype, type);
8847 break;
8848 case ICR_CONVERTING:
8849 error ("cannot convert %qH to %qI",
8850 rhstype, type);
8851 break;
8852 case ICR_INIT:
8853 error ("cannot convert %qH to %qI in initialization",
8854 rhstype, type);
8855 break;
8856 case ICR_RETURN:
8857 error ("cannot convert %qH to %qI in return",
8858 rhstype, type);
8859 break;
8860 case ICR_ASSIGN:
8861 error ("cannot convert %qH to %qI in assignment",
8862 rhstype, type);
8863 break;
8864 default:
8865 gcc_unreachable();
8867 if (TYPE_PTR_P (rhstype)
8868 && TYPE_PTR_P (type)
8869 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8870 && CLASS_TYPE_P (TREE_TYPE (type))
8871 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8872 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8873 (TREE_TYPE (rhstype))),
8874 "class type %qT is incomplete", TREE_TYPE (rhstype));
8876 return error_mark_node;
8879 if (warn_suggest_attribute_format)
8881 const enum tree_code codel = TREE_CODE (type);
8882 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8883 && coder == codel
8884 && check_missing_format_attribute (type, rhstype)
8885 && (complain & tf_warning))
8886 switch (errtype)
8888 case ICR_ARGPASS:
8889 case ICR_DEFAULT_ARGUMENT:
8890 if (fndecl)
8891 warning (OPT_Wsuggest_attribute_format,
8892 "parameter %qP of %qD might be a candidate "
8893 "for a format attribute", parmnum, fndecl);
8894 else
8895 warning (OPT_Wsuggest_attribute_format,
8896 "parameter might be a candidate "
8897 "for a format attribute");
8898 break;
8899 case ICR_CONVERTING:
8900 warning (OPT_Wsuggest_attribute_format,
8901 "target of conversion might be a candidate "
8902 "for a format attribute");
8903 break;
8904 case ICR_INIT:
8905 warning (OPT_Wsuggest_attribute_format,
8906 "target of initialization might be a candidate "
8907 "for a format attribute");
8908 break;
8909 case ICR_RETURN:
8910 warning (OPT_Wsuggest_attribute_format,
8911 "return type might be a candidate "
8912 "for a format attribute");
8913 break;
8914 case ICR_ASSIGN:
8915 warning (OPT_Wsuggest_attribute_format,
8916 "left-hand side of assignment might be a candidate "
8917 "for a format attribute");
8918 break;
8919 default:
8920 gcc_unreachable();
8924 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8925 does not. */
8926 if (warn_parentheses
8927 && TREE_CODE (type) == BOOLEAN_TYPE
8928 && TREE_CODE (rhs) == MODIFY_EXPR
8929 && !TREE_NO_WARNING (rhs)
8930 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8931 && (complain & tf_warning))
8933 location_t loc = cp_expr_loc_or_loc (rhs, input_location);
8935 warning_at (loc, OPT_Wparentheses,
8936 "suggest parentheses around assignment used as truth value");
8937 TREE_NO_WARNING (rhs) = 1;
8940 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8941 complain, flags);
8944 /* Convert RHS to be of type TYPE.
8945 If EXP is nonzero, it is the target of the initialization.
8946 ERRTYPE indicates what kind of error the implicit conversion is.
8948 Two major differences between the behavior of
8949 `convert_for_assignment' and `convert_for_initialization'
8950 are that references are bashed in the former, while
8951 copied in the latter, and aggregates are assigned in
8952 the former (operator=) while initialized in the
8953 latter (X(X&)).
8955 If using constructor make sure no conversion operator exists, if one does
8956 exist, an ambiguity exists. */
8958 tree
8959 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8960 impl_conv_rhs errtype, tree fndecl, int parmnum,
8961 tsubst_flags_t complain)
8963 enum tree_code codel = TREE_CODE (type);
8964 tree rhstype;
8965 enum tree_code coder;
8967 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8968 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8969 if (TREE_CODE (rhs) == NOP_EXPR
8970 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8971 && codel != REFERENCE_TYPE)
8972 rhs = TREE_OPERAND (rhs, 0);
8974 if (type == error_mark_node
8975 || rhs == error_mark_node
8976 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8977 return error_mark_node;
8979 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8981 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8982 && TREE_CODE (type) != ARRAY_TYPE
8983 && (!TYPE_REF_P (type)
8984 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8985 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8986 && !TYPE_REFFN_P (type))
8987 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8988 rhs = decay_conversion (rhs, complain);
8990 rhstype = TREE_TYPE (rhs);
8991 coder = TREE_CODE (rhstype);
8993 if (coder == ERROR_MARK)
8994 return error_mark_node;
8996 /* We accept references to incomplete types, so we can
8997 return here before checking if RHS is of complete type. */
8999 if (codel == REFERENCE_TYPE)
9001 /* This should eventually happen in convert_arguments. */
9002 int savew = 0, savee = 0;
9004 if (fndecl)
9005 savew = warningcount + werrorcount, savee = errorcount;
9006 rhs = initialize_reference (type, rhs, flags, complain);
9008 if (fndecl
9009 && (warningcount + werrorcount > savew || errorcount > savee))
9010 inform (DECL_SOURCE_LOCATION (fndecl),
9011 "in passing argument %P of %qD", parmnum, fndecl);
9013 return rhs;
9016 if (exp != 0)
9017 exp = require_complete_type_sfinae (exp, complain);
9018 if (exp == error_mark_node)
9019 return error_mark_node;
9021 rhstype = non_reference (rhstype);
9023 type = complete_type (type);
9025 if (DIRECT_INIT_EXPR_P (type, rhs))
9026 /* Don't try to do copy-initialization if we already have
9027 direct-initialization. */
9028 return rhs;
9030 if (MAYBE_CLASS_TYPE_P (type))
9031 return perform_implicit_conversion_flags (type, rhs, complain, flags);
9033 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
9034 complain, flags);
9037 /* If RETVAL is the address of, or a reference to, a local variable or
9038 temporary give an appropriate warning and return true. */
9040 static bool
9041 maybe_warn_about_returning_address_of_local (tree retval)
9043 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9044 tree whats_returned = fold_for_warn (retval);
9045 location_t loc = cp_expr_loc_or_loc (retval, input_location);
9047 for (;;)
9049 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9050 whats_returned = TREE_OPERAND (whats_returned, 1);
9051 else if (CONVERT_EXPR_P (whats_returned)
9052 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9053 whats_returned = TREE_OPERAND (whats_returned, 0);
9054 else
9055 break;
9058 if (TREE_CODE (whats_returned) == TARGET_EXPR
9059 && is_std_init_list (TREE_TYPE (whats_returned)))
9061 tree init = TARGET_EXPR_INITIAL (whats_returned);
9062 if (TREE_CODE (init) == CONSTRUCTOR)
9063 /* Pull out the array address. */
9064 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
9065 else if (TREE_CODE (init) == INDIRECT_REF)
9066 /* The source of a trivial copy looks like *(T*)&var. */
9067 whats_returned = TREE_OPERAND (init, 0);
9068 else
9069 return false;
9070 STRIP_NOPS (whats_returned);
9073 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9074 return false;
9075 whats_returned = TREE_OPERAND (whats_returned, 0);
9077 while (TREE_CODE (whats_returned) == COMPONENT_REF
9078 || TREE_CODE (whats_returned) == ARRAY_REF)
9079 whats_returned = TREE_OPERAND (whats_returned, 0);
9081 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9082 || TREE_CODE (whats_returned) == TARGET_EXPR)
9084 if (TYPE_REF_P (valtype))
9085 warning_at (loc, OPT_Wreturn_local_addr,
9086 "returning reference to temporary");
9087 else if (is_std_init_list (valtype))
9088 warning_at (loc, OPT_Winit_list_lifetime,
9089 "returning temporary initializer_list does not extend "
9090 "the lifetime of the underlying array");
9091 return true;
9094 if (DECL_P (whats_returned)
9095 && DECL_NAME (whats_returned)
9096 && DECL_FUNCTION_SCOPE_P (whats_returned)
9097 && !is_capture_proxy (whats_returned)
9098 && !(TREE_STATIC (whats_returned)
9099 || TREE_PUBLIC (whats_returned)))
9101 bool w = false;
9102 auto_diagnostic_group d;
9103 if (TYPE_REF_P (valtype))
9104 w = warning_at (loc, OPT_Wreturn_local_addr,
9105 "reference to local variable %qD returned",
9106 whats_returned);
9107 else if (is_std_init_list (valtype))
9108 w = warning_at (loc, OPT_Winit_list_lifetime,
9109 "returning local initializer_list variable %qD "
9110 "does not extend the lifetime of the underlying array",
9111 whats_returned);
9112 else if (TREE_CODE (whats_returned) == LABEL_DECL)
9113 w = warning_at (loc, OPT_Wreturn_local_addr,
9114 "address of label %qD returned",
9115 whats_returned);
9116 else
9117 w = warning_at (loc, OPT_Wreturn_local_addr,
9118 "address of local variable %qD returned",
9119 whats_returned);
9120 if (w)
9121 inform (DECL_SOURCE_LOCATION (whats_returned),
9122 "declared here");
9123 return true;
9126 return false;
9129 /* Returns true if DECL is in the std namespace. */
9131 static bool
9132 decl_in_std_namespace_p (tree decl)
9134 return (decl != NULL_TREE
9135 && DECL_NAMESPACE_STD_P (decl_namespace_context (decl)));
9138 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9140 static bool
9141 is_std_move_p (tree fn)
9143 /* std::move only takes one argument. */
9144 if (call_expr_nargs (fn) != 1)
9145 return false;
9147 tree fndecl = cp_get_callee_fndecl_nofold (fn);
9148 if (!decl_in_std_namespace_p (fndecl))
9149 return false;
9151 tree name = DECL_NAME (fndecl);
9152 return name && id_equal (name, "move");
9155 /* Returns true if RETVAL is a good candidate for the NRVO as per
9156 [class.copy.elision]. FUNCTYPE is the type the function is declared
9157 to return. */
9159 static bool
9160 can_do_nrvo_p (tree retval, tree functype)
9162 tree result = DECL_RESULT (current_function_decl);
9163 return (retval != NULL_TREE
9164 && !processing_template_decl
9165 /* Must be a local, automatic variable. */
9166 && VAR_P (retval)
9167 && DECL_CONTEXT (retval) == current_function_decl
9168 && !TREE_STATIC (retval)
9169 /* And not a lambda or anonymous union proxy. */
9170 && !DECL_HAS_VALUE_EXPR_P (retval)
9171 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9172 /* The cv-unqualified type of the returned value must be the
9173 same as the cv-unqualified return type of the
9174 function. */
9175 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9176 (TYPE_MAIN_VARIANT (functype)))
9177 /* And the returned value must be non-volatile. */
9178 && !TYPE_VOLATILE (TREE_TYPE (retval)));
9181 /* Warn about wrong usage of std::move in a return statement. RETVAL
9182 is the expression we are returning; FUNCTYPE is the type the function
9183 is declared to return. */
9185 static void
9186 maybe_warn_pessimizing_move (tree retval, tree functype)
9188 if (!warn_pessimizing_move)
9189 return;
9191 /* C++98 doesn't know move. */
9192 if (cxx_dialect < cxx11)
9193 return;
9195 /* Wait until instantiation time, since we can't gauge if we should do
9196 the NRVO until then. */
9197 if (processing_template_decl)
9198 return;
9200 /* This is only interesting for class types. */
9201 if (!CLASS_TYPE_P (functype))
9202 return;
9204 /* We're looking for *std::move<T&> (&arg). */
9205 if (REFERENCE_REF_P (retval)
9206 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9208 tree fn = TREE_OPERAND (retval, 0);
9209 if (is_std_move_p (fn))
9211 tree arg = CALL_EXPR_ARG (fn, 0);
9212 STRIP_NOPS (arg);
9213 if (TREE_CODE (arg) == ADDR_EXPR)
9214 arg = TREE_OPERAND (arg, 0);
9215 /* Warn if we could do copy elision were it not for the move. */
9216 if (can_do_nrvo_p (arg, functype))
9218 auto_diagnostic_group d;
9219 if (warning_at (location_of (retval), OPT_Wpessimizing_move,
9220 "moving a local object in a return statement "
9221 "prevents copy elision"))
9222 inform (location_of (retval), "remove %<std::move%> call");
9228 /* Check that returning RETVAL from the current function is valid.
9229 Return an expression explicitly showing all conversions required to
9230 change RETVAL into the function return type, and to assign it to
9231 the DECL_RESULT for the function. Set *NO_WARNING to true if
9232 code reaches end of non-void function warning shouldn't be issued
9233 on this RETURN_EXPR. */
9235 tree
9236 check_return_expr (tree retval, bool *no_warning)
9238 tree result;
9239 /* The type actually returned by the function. */
9240 tree valtype;
9241 /* The type the function is declared to return, or void if
9242 the declared type is incomplete. */
9243 tree functype;
9244 int fn_returns_value_p;
9246 *no_warning = false;
9248 /* A `volatile' function is one that isn't supposed to return, ever.
9249 (This is a G++ extension, used to get better code for functions
9250 that call the `volatile' function.) */
9251 if (TREE_THIS_VOLATILE (current_function_decl))
9252 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9254 /* Check for various simple errors. */
9255 if (DECL_DESTRUCTOR_P (current_function_decl))
9257 if (retval)
9258 error ("returning a value from a destructor");
9259 return NULL_TREE;
9261 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9263 if (in_function_try_handler)
9264 /* If a return statement appears in a handler of the
9265 function-try-block of a constructor, the program is ill-formed. */
9266 error ("cannot return from a handler of a function-try-block of a constructor");
9267 else if (retval)
9268 /* You can't return a value from a constructor. */
9269 error ("returning a value from a constructor");
9270 return NULL_TREE;
9273 const tree saved_retval = retval;
9275 if (processing_template_decl)
9277 current_function_returns_value = 1;
9279 if (check_for_bare_parameter_packs (retval))
9280 return error_mark_node;
9282 /* If one of the types might be void, we can't tell whether we're
9283 returning a value. */
9284 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9285 && !current_function_auto_return_pattern)
9286 || (retval != NULL_TREE
9287 && (TREE_TYPE (retval) == NULL_TREE
9288 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9289 goto dependent;
9292 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9294 /* Deduce auto return type from a return statement. */
9295 if (current_function_auto_return_pattern)
9297 tree auto_node;
9298 tree type;
9300 if (!retval && !is_auto (current_function_auto_return_pattern))
9302 /* Give a helpful error message. */
9303 error ("return-statement with no value, in function returning %qT",
9304 current_function_auto_return_pattern);
9305 inform (input_location, "only plain %<auto%> return type can be "
9306 "deduced to %<void%>");
9307 type = error_mark_node;
9309 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9311 error ("returning initializer list");
9312 type = error_mark_node;
9314 else
9316 if (!retval)
9317 retval = void_node;
9318 auto_node = type_uses_auto (current_function_auto_return_pattern);
9319 type = do_auto_deduction (current_function_auto_return_pattern,
9320 retval, auto_node);
9323 if (type == error_mark_node)
9324 /* Leave it. */;
9325 else if (functype == current_function_auto_return_pattern)
9326 apply_deduced_return_type (current_function_decl, type);
9327 else if (!same_type_p (type, functype))
9329 if (LAMBDA_FUNCTION_P (current_function_decl))
9330 error ("inconsistent types %qT and %qT deduced for "
9331 "lambda return type", functype, type);
9332 else
9333 error ("inconsistent deduction for auto return type: "
9334 "%qT and then %qT", functype, type);
9336 functype = type;
9339 result = DECL_RESULT (current_function_decl);
9340 valtype = TREE_TYPE (result);
9341 gcc_assert (valtype != NULL_TREE);
9342 fn_returns_value_p = !VOID_TYPE_P (valtype);
9344 /* Check for a return statement with no return value in a function
9345 that's supposed to return a value. */
9346 if (!retval && fn_returns_value_p)
9348 if (functype != error_mark_node)
9349 permerror (input_location, "return-statement with no value, in "
9350 "function returning %qT", valtype);
9351 /* Remember that this function did return. */
9352 current_function_returns_value = 1;
9353 /* And signal caller that TREE_NO_WARNING should be set on the
9354 RETURN_EXPR to avoid control reaches end of non-void function
9355 warnings in tree-cfg.c. */
9356 *no_warning = true;
9358 /* Check for a return statement with a value in a function that
9359 isn't supposed to return a value. */
9360 else if (retval && !fn_returns_value_p)
9362 if (VOID_TYPE_P (TREE_TYPE (retval)))
9363 /* You can return a `void' value from a function of `void'
9364 type. In that case, we have to evaluate the expression for
9365 its side-effects. */
9366 finish_expr_stmt (retval);
9367 else
9368 permerror (input_location,
9369 "return-statement with a value, in function "
9370 "returning %qT", valtype);
9371 current_function_returns_null = 1;
9373 /* There's really no value to return, after all. */
9374 return NULL_TREE;
9376 else if (!retval)
9377 /* Remember that this function can sometimes return without a
9378 value. */
9379 current_function_returns_null = 1;
9380 else
9381 /* Remember that this function did return a value. */
9382 current_function_returns_value = 1;
9384 /* Check for erroneous operands -- but after giving ourselves a
9385 chance to provide an error about returning a value from a void
9386 function. */
9387 if (error_operand_p (retval))
9389 current_function_return_value = error_mark_node;
9390 return error_mark_node;
9393 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9394 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9395 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9396 && ! flag_check_new
9397 && retval && null_ptr_cst_p (retval))
9398 warning (0, "%<operator new%> must not return NULL unless it is "
9399 "declared %<throw()%> (or -fcheck-new is in effect)");
9401 /* Effective C++ rule 15. See also start_function. */
9402 if (warn_ecpp
9403 && DECL_NAME (current_function_decl) == assign_op_identifier
9404 && !type_dependent_expression_p (retval))
9406 bool warn = true;
9408 /* The function return type must be a reference to the current
9409 class. */
9410 if (TYPE_REF_P (valtype)
9411 && same_type_ignoring_top_level_qualifiers_p
9412 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9414 /* Returning '*this' is obviously OK. */
9415 if (retval == current_class_ref)
9416 warn = false;
9417 /* If we are calling a function whose return type is the same of
9418 the current class reference, it is ok. */
9419 else if (INDIRECT_REF_P (retval)
9420 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9421 warn = false;
9424 if (warn)
9425 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9428 if (dependent_type_p (functype)
9429 || type_dependent_expression_p (retval))
9431 dependent:
9432 /* We should not have changed the return value. */
9433 gcc_assert (retval == saved_retval);
9434 return retval;
9437 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9439 [...] For a function with a class return type, if the expression
9440 in the return statement is the name of a local object, and the cv-
9441 unqualified type of the local object is the same as the function
9442 return type, an implementation is permitted to omit creating the tem-
9443 porary object to hold the function return value [...]
9445 So, if this is a value-returning function that always returns the same
9446 local variable, remember it.
9448 It might be nice to be more flexible, and choose the first suitable
9449 variable even if the function sometimes returns something else, but
9450 then we run the risk of clobbering the variable we chose if the other
9451 returned expression uses the chosen variable somehow. And people expect
9452 this restriction, anyway. (jason 2000-11-19)
9454 See finish_function and finalize_nrv for the rest of this optimization. */
9456 bool named_return_value_okay_p = can_do_nrvo_p (retval, functype);
9457 if (fn_returns_value_p && flag_elide_constructors)
9459 if (named_return_value_okay_p
9460 && (current_function_return_value == NULL_TREE
9461 || current_function_return_value == retval))
9462 current_function_return_value = retval;
9463 else
9464 current_function_return_value = error_mark_node;
9467 /* We don't need to do any conversions when there's nothing being
9468 returned. */
9469 if (!retval)
9470 return NULL_TREE;
9472 if (!named_return_value_okay_p)
9473 maybe_warn_pessimizing_move (retval, functype);
9475 /* Do any required conversions. */
9476 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9477 /* No conversions are required. */
9479 else
9481 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9483 /* The functype's return type will have been set to void, if it
9484 was an incomplete type. Just treat this as 'return;' */
9485 if (VOID_TYPE_P (functype))
9486 return error_mark_node;
9488 /* If we had an id-expression obfuscated by force_paren_expr, we need
9489 to undo it so we can try to treat it as an rvalue below. */
9490 retval = maybe_undo_parenthesized_ref (retval);
9492 if (processing_template_decl)
9493 retval = build_non_dependent_expr (retval);
9495 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9496 treated as an rvalue for the purposes of overload resolution to
9497 favor move constructors over copy constructors.
9499 Note that these conditions are similar to, but not as strict as,
9500 the conditions for the named return value optimization. */
9501 bool converted = false;
9502 if ((cxx_dialect != cxx98)
9503 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9504 || TREE_CODE (retval) == PARM_DECL)
9505 && DECL_CONTEXT (retval) == current_function_decl
9506 && !TREE_STATIC (retval)
9507 /* This is only interesting for class type. */
9508 && CLASS_TYPE_P (functype))
9510 tree moved = move (retval);
9511 moved = convert_for_initialization
9512 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9513 ICR_RETURN, NULL_TREE, 0, tf_none);
9514 if (moved != error_mark_node)
9516 retval = moved;
9517 converted = true;
9521 /* First convert the value to the function's return type, then
9522 to the type of return value's location to handle the
9523 case that functype is smaller than the valtype. */
9524 if (!converted)
9525 retval = convert_for_initialization
9526 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9527 tf_warning_or_error);
9528 retval = convert (valtype, retval);
9530 /* If the conversion failed, treat this just like `return;'. */
9531 if (retval == error_mark_node)
9532 return retval;
9533 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9534 else if (! cfun->returns_struct
9535 && TREE_CODE (retval) == TARGET_EXPR
9536 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9537 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9538 TREE_OPERAND (retval, 0));
9539 else if (!processing_template_decl
9540 && maybe_warn_about_returning_address_of_local (retval)
9541 && INDIRECT_TYPE_P (valtype))
9542 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9543 build_zero_cst (TREE_TYPE (retval)));
9546 if (processing_template_decl)
9547 return saved_retval;
9549 /* Actually copy the value returned into the appropriate location. */
9550 if (retval && retval != result)
9551 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9553 return retval;
9557 /* Returns nonzero if the pointer-type FROM can be converted to the
9558 pointer-type TO via a qualification conversion. If CONSTP is -1,
9559 then we return nonzero if the pointers are similar, and the
9560 cv-qualification signature of FROM is a proper subset of that of TO.
9562 If CONSTP is positive, then all outer pointers have been
9563 const-qualified. */
9565 static int
9566 comp_ptr_ttypes_real (tree to, tree from, int constp)
9568 bool to_more_cv_qualified = false;
9569 bool is_opaque_pointer = false;
9571 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9573 if (TREE_CODE (to) != TREE_CODE (from))
9574 return 0;
9576 if (TREE_CODE (from) == OFFSET_TYPE
9577 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9578 TYPE_OFFSET_BASETYPE (to)))
9579 return 0;
9581 /* Const and volatile mean something different for function types,
9582 so the usual checks are not appropriate. */
9583 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9585 if (!at_least_as_qualified_p (to, from))
9586 return 0;
9588 if (!at_least_as_qualified_p (from, to))
9590 if (constp == 0)
9591 return 0;
9592 to_more_cv_qualified = true;
9595 if (constp > 0)
9596 constp &= TYPE_READONLY (to);
9599 if (VECTOR_TYPE_P (to))
9600 is_opaque_pointer = vector_targets_convertible_p (to, from);
9602 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9603 return ((constp >= 0 || to_more_cv_qualified)
9604 && (is_opaque_pointer
9605 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9609 /* When comparing, say, char ** to char const **, this function takes
9610 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9611 types to this function. */
9614 comp_ptr_ttypes (tree to, tree from)
9616 return comp_ptr_ttypes_real (to, from, 1);
9619 /* Returns true iff FNTYPE is a non-class type that involves
9620 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9621 if a parameter type is ill-formed. */
9623 bool
9624 error_type_p (const_tree type)
9626 tree t;
9628 switch (TREE_CODE (type))
9630 case ERROR_MARK:
9631 return true;
9633 case POINTER_TYPE:
9634 case REFERENCE_TYPE:
9635 case OFFSET_TYPE:
9636 return error_type_p (TREE_TYPE (type));
9638 case FUNCTION_TYPE:
9639 case METHOD_TYPE:
9640 if (error_type_p (TREE_TYPE (type)))
9641 return true;
9642 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9643 if (error_type_p (TREE_VALUE (t)))
9644 return true;
9645 return false;
9647 case RECORD_TYPE:
9648 if (TYPE_PTRMEMFUNC_P (type))
9649 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9650 return false;
9652 default:
9653 return false;
9657 /* Returns true if to and from are (possibly multi-level) pointers to the same
9658 type or inheritance-related types, regardless of cv-quals. */
9660 bool
9661 ptr_reasonably_similar (const_tree to, const_tree from)
9663 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9665 /* Any target type is similar enough to void. */
9666 if (VOID_TYPE_P (to))
9667 return !error_type_p (from);
9668 if (VOID_TYPE_P (from))
9669 return !error_type_p (to);
9671 if (TREE_CODE (to) != TREE_CODE (from))
9672 return false;
9674 if (TREE_CODE (from) == OFFSET_TYPE
9675 && comptypes (TYPE_OFFSET_BASETYPE (to),
9676 TYPE_OFFSET_BASETYPE (from),
9677 COMPARE_BASE | COMPARE_DERIVED))
9678 continue;
9680 if (VECTOR_TYPE_P (to)
9681 && vector_types_convertible_p (to, from, false))
9682 return true;
9684 if (TREE_CODE (to) == INTEGER_TYPE
9685 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9686 return true;
9688 if (TREE_CODE (to) == FUNCTION_TYPE)
9689 return !error_type_p (to) && !error_type_p (from);
9691 if (!TYPE_PTR_P (to))
9693 /* When either type is incomplete avoid DERIVED_FROM_P,
9694 which may call complete_type (c++/57942). */
9695 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9696 return comptypes
9697 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9698 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9703 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9704 pointer-to-member types) are the same, ignoring cv-qualification at
9705 all levels. */
9707 bool
9708 comp_ptr_ttypes_const (tree to, tree from)
9710 bool is_opaque_pointer = false;
9712 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9714 if (TREE_CODE (to) != TREE_CODE (from))
9715 return false;
9717 if (TREE_CODE (from) == OFFSET_TYPE
9718 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9719 TYPE_OFFSET_BASETYPE (to)))
9720 continue;
9722 if (VECTOR_TYPE_P (to))
9723 is_opaque_pointer = vector_targets_convertible_p (to, from);
9725 if (!TYPE_PTR_P (to))
9726 return (is_opaque_pointer
9727 || same_type_ignoring_top_level_qualifiers_p (to, from));
9731 /* Returns the type qualifiers for this type, including the qualifiers on the
9732 elements for an array type. */
9735 cp_type_quals (const_tree type)
9737 int quals;
9738 /* This CONST_CAST is okay because strip_array_types returns its
9739 argument unmodified and we assign it to a const_tree. */
9740 type = strip_array_types (CONST_CAST_TREE (type));
9741 if (type == error_mark_node
9742 /* Quals on a FUNCTION_TYPE are memfn quals. */
9743 || TREE_CODE (type) == FUNCTION_TYPE)
9744 return TYPE_UNQUALIFIED;
9745 quals = TYPE_QUALS (type);
9746 /* METHOD and REFERENCE_TYPEs should never have quals. */
9747 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9748 && !TYPE_REF_P (type))
9749 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9750 == TYPE_UNQUALIFIED));
9751 return quals;
9754 /* Returns the function-ref-qualifier for TYPE */
9756 cp_ref_qualifier
9757 type_memfn_rqual (const_tree type)
9759 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9760 || TREE_CODE (type) == METHOD_TYPE);
9762 if (!FUNCTION_REF_QUALIFIED (type))
9763 return REF_QUAL_NONE;
9764 else if (FUNCTION_RVALUE_QUALIFIED (type))
9765 return REF_QUAL_RVALUE;
9766 else
9767 return REF_QUAL_LVALUE;
9770 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9771 METHOD_TYPE. */
9774 type_memfn_quals (const_tree type)
9776 if (TREE_CODE (type) == FUNCTION_TYPE)
9777 return TYPE_QUALS (type);
9778 else if (TREE_CODE (type) == METHOD_TYPE)
9779 return cp_type_quals (class_of_this_parm (type));
9780 else
9781 gcc_unreachable ();
9784 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9785 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9787 tree
9788 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9790 /* Could handle METHOD_TYPE here if necessary. */
9791 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9792 if (TYPE_QUALS (type) == memfn_quals
9793 && type_memfn_rqual (type) == rqual)
9794 return type;
9796 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9797 complex. */
9798 tree result = build_qualified_type (type, memfn_quals);
9799 return build_ref_qualified_type (result, rqual);
9802 /* Returns nonzero if TYPE is const or volatile. */
9804 bool
9805 cv_qualified_p (const_tree type)
9807 int quals = cp_type_quals (type);
9808 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9811 /* Returns nonzero if the TYPE contains a mutable member. */
9813 bool
9814 cp_has_mutable_p (const_tree type)
9816 /* This CONST_CAST is okay because strip_array_types returns its
9817 argument unmodified and we assign it to a const_tree. */
9818 type = strip_array_types (CONST_CAST_TREE(type));
9820 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9823 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9824 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9825 approximation. In particular, consider:
9827 int f();
9828 struct S { int i; };
9829 const S s = { f(); }
9831 Here, we will make "s" as TREE_READONLY (because it is declared
9832 "const") -- only to reverse ourselves upon seeing that the
9833 initializer is non-constant. */
9835 void
9836 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9838 tree type = TREE_TYPE (decl);
9840 if (type == error_mark_node)
9841 return;
9843 if (TREE_CODE (decl) == TYPE_DECL)
9844 return;
9846 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9847 && type_quals != TYPE_UNQUALIFIED));
9849 /* Avoid setting TREE_READONLY incorrectly. */
9850 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9851 constructor can produce constant init, so rely on cp_finish_decl to
9852 clear TREE_READONLY if the variable has non-constant init. */
9854 /* If the type has (or might have) a mutable component, that component
9855 might be modified. */
9856 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9857 type_quals &= ~TYPE_QUAL_CONST;
9859 c_apply_type_quals_to_decl (type_quals, decl);
9862 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9863 exemplar types such that casting T1 to T2 is casting away constness
9864 if and only if there is no implicit conversion from T1 to T2. */
9866 static void
9867 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9869 int quals1;
9870 int quals2;
9872 /* [expr.const.cast]
9874 For multi-level pointer to members and multi-level mixed pointers
9875 and pointers to members (conv.qual), the "member" aspect of a
9876 pointer to member level is ignored when determining if a const
9877 cv-qualifier has been cast away. */
9878 /* [expr.const.cast]
9880 For two pointer types:
9882 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9883 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9884 K is min(N,M)
9886 casting from X1 to X2 casts away constness if, for a non-pointer
9887 type T there does not exist an implicit conversion (clause
9888 _conv_) from:
9890 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9894 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9895 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9896 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9898 *t1 = cp_build_qualified_type (void_type_node,
9899 cp_type_quals (*t1));
9900 *t2 = cp_build_qualified_type (void_type_node,
9901 cp_type_quals (*t2));
9902 return;
9905 quals1 = cp_type_quals (*t1);
9906 quals2 = cp_type_quals (*t2);
9908 if (TYPE_PTRDATAMEM_P (*t1))
9909 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9910 else
9911 *t1 = TREE_TYPE (*t1);
9912 if (TYPE_PTRDATAMEM_P (*t2))
9913 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9914 else
9915 *t2 = TREE_TYPE (*t2);
9917 casts_away_constness_r (t1, t2, complain);
9918 *t1 = build_pointer_type (*t1);
9919 *t2 = build_pointer_type (*t2);
9920 *t1 = cp_build_qualified_type (*t1, quals1);
9921 *t2 = cp_build_qualified_type (*t2, quals2);
9924 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9925 constness.
9927 ??? This function returns non-zero if casting away qualifiers not
9928 just const. We would like to return to the caller exactly which
9929 qualifiers are casted away to give more accurate diagnostics.
9932 static bool
9933 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9935 if (TYPE_REF_P (t2))
9937 /* [expr.const.cast]
9939 Casting from an lvalue of type T1 to an lvalue of type T2
9940 using a reference cast casts away constness if a cast from an
9941 rvalue of type "pointer to T1" to the type "pointer to T2"
9942 casts away constness. */
9943 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
9944 return casts_away_constness (build_pointer_type (t1),
9945 build_pointer_type (TREE_TYPE (t2)),
9946 complain);
9949 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9950 /* [expr.const.cast]
9952 Casting from an rvalue of type "pointer to data member of X
9953 of type T1" to the type "pointer to data member of Y of type
9954 T2" casts away constness if a cast from an rvalue of type
9955 "pointer to T1" to the type "pointer to T2" casts away
9956 constness. */
9957 return casts_away_constness
9958 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9959 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9960 complain);
9962 /* Casting away constness is only something that makes sense for
9963 pointer or reference types. */
9964 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9965 return false;
9967 /* Top-level qualifiers don't matter. */
9968 t1 = TYPE_MAIN_VARIANT (t1);
9969 t2 = TYPE_MAIN_VARIANT (t2);
9970 casts_away_constness_r (&t1, &t2, complain);
9971 if (!can_convert (t2, t1, complain))
9972 return true;
9974 return false;
9977 /* If T is a REFERENCE_TYPE return the type to which T refers.
9978 Otherwise, return T itself. */
9980 tree
9981 non_reference (tree t)
9983 if (t && TYPE_REF_P (t))
9984 t = TREE_TYPE (t);
9985 return t;
9989 /* Return nonzero if REF is an lvalue valid for this language;
9990 otherwise, print an error message and return zero. USE says
9991 how the lvalue is being used and so selects the error message. */
9994 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9996 cp_lvalue_kind kind = lvalue_kind (ref);
9998 if (kind == clk_none)
10000 if (complain & tf_error)
10001 lvalue_error (input_location, use);
10002 return 0;
10004 else if (kind & (clk_rvalueref|clk_class))
10006 if (!(complain & tf_error))
10007 return 0;
10008 /* Make this a permerror because we used to accept it. */
10009 permerror (input_location, "using rvalue as lvalue");
10011 return 1;
10014 /* Return true if a user-defined literal operator is a raw operator. */
10016 bool
10017 check_raw_literal_operator (const_tree decl)
10019 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10020 tree argtype;
10021 int arity;
10022 bool maybe_raw_p = false;
10024 /* Count the number and type of arguments and check for ellipsis. */
10025 for (argtype = argtypes, arity = 0;
10026 argtype && argtype != void_list_node;
10027 ++arity, argtype = TREE_CHAIN (argtype))
10029 tree t = TREE_VALUE (argtype);
10031 if (same_type_p (t, const_string_type_node))
10032 maybe_raw_p = true;
10034 if (!argtype)
10035 return false; /* Found ellipsis. */
10037 if (!maybe_raw_p || arity != 1)
10038 return false;
10040 return true;
10044 /* Return true if a user-defined literal operator has one of the allowed
10045 argument types. */
10047 bool
10048 check_literal_operator_args (const_tree decl,
10049 bool *long_long_unsigned_p, bool *long_double_p)
10051 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10053 *long_long_unsigned_p = false;
10054 *long_double_p = false;
10055 if (processing_template_decl || processing_specialization)
10056 return argtypes == void_list_node;
10057 else
10059 tree argtype;
10060 int arity;
10061 int max_arity = 2;
10063 /* Count the number and type of arguments and check for ellipsis. */
10064 for (argtype = argtypes, arity = 0;
10065 argtype && argtype != void_list_node;
10066 argtype = TREE_CHAIN (argtype))
10068 tree t = TREE_VALUE (argtype);
10069 ++arity;
10071 if (TYPE_PTR_P (t))
10073 bool maybe_raw_p = false;
10074 t = TREE_TYPE (t);
10075 if (cp_type_quals (t) != TYPE_QUAL_CONST)
10076 return false;
10077 t = TYPE_MAIN_VARIANT (t);
10078 if ((maybe_raw_p = same_type_p (t, char_type_node))
10079 || same_type_p (t, wchar_type_node)
10080 || same_type_p (t, char16_type_node)
10081 || same_type_p (t, char32_type_node))
10083 argtype = TREE_CHAIN (argtype);
10084 if (!argtype)
10085 return false;
10086 t = TREE_VALUE (argtype);
10087 if (maybe_raw_p && argtype == void_list_node)
10088 return true;
10089 else if (same_type_p (t, size_type_node))
10091 ++arity;
10092 continue;
10094 else
10095 return false;
10098 else if (same_type_p (t, long_long_unsigned_type_node))
10100 max_arity = 1;
10101 *long_long_unsigned_p = true;
10103 else if (same_type_p (t, long_double_type_node))
10105 max_arity = 1;
10106 *long_double_p = true;
10108 else if (same_type_p (t, char_type_node))
10109 max_arity = 1;
10110 else if (same_type_p (t, wchar_type_node))
10111 max_arity = 1;
10112 else if (same_type_p (t, char16_type_node))
10113 max_arity = 1;
10114 else if (same_type_p (t, char32_type_node))
10115 max_arity = 1;
10116 else
10117 return false;
10119 if (!argtype)
10120 return false; /* Found ellipsis. */
10122 if (arity != max_arity)
10123 return false;
10125 return true;
10129 /* Always returns false since unlike C90, C++ has no concept of implicit
10130 function declarations. */
10132 bool
10133 c_decl_implicit (const_tree)
10135 return false;