c++: ttp TEMPLATE_DECL equivalence [PR112737]
[official-gcc.git] / gcc / cp / typeck.cc
blob132c55cfc6d5693428a11a0812399303ebc64ebd
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2024 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 "gcc-rich-location.h"
39 #include "stringpool.h"
40 #include "attribs.h"
41 #include "asan.h"
42 #include "gimplify.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 bool 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, compare_bounds_t, 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, location_t = UNKNOWN_LOCATION);
62 static void error_args_num (location_t, tree, bool);
63 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
64 tsubst_flags_t);
65 static bool is_std_move_p (tree);
66 static bool is_std_forward_p (tree);
68 /* Do `exp = require_complete_type (exp);' to make sure exp
69 does not have an incomplete type. (That includes void types.)
70 Returns error_mark_node if the VALUE does not have
71 complete type when this function returns. */
73 tree
74 require_complete_type (tree value,
75 tsubst_flags_t complain /* = tf_warning_or_error */)
77 tree type;
79 if (processing_template_decl || value == error_mark_node)
80 return value;
82 if (TREE_CODE (value) == OVERLOAD)
83 type = unknown_type_node;
84 else
85 type = TREE_TYPE (value);
87 if (type == error_mark_node)
88 return error_mark_node;
90 /* First, detect a valid value with a complete type. */
91 if (COMPLETE_TYPE_P (type))
92 return value;
94 if (complete_type_or_maybe_complain (type, value, complain))
95 return value;
96 else
97 return error_mark_node;
100 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
101 a template instantiation, do the instantiation. Returns TYPE,
102 whether or not it could be completed, unless something goes
103 horribly wrong, in which case the error_mark_node is returned. */
105 tree
106 complete_type (tree type)
108 if (type == NULL_TREE)
109 /* Rather than crash, we return something sure to cause an error
110 at some point. */
111 return error_mark_node;
113 if (type == error_mark_node || COMPLETE_TYPE_P (type))
115 else if (TREE_CODE (type) == ARRAY_TYPE)
117 tree t = complete_type (TREE_TYPE (type));
118 unsigned int needs_constructing, has_nontrivial_dtor;
119 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 layout_type (type);
121 needs_constructing
122 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 has_nontrivial_dtor
124 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
127 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
131 else if (CLASS_TYPE_P (type))
133 if (modules_p ())
134 /* TYPE could be a class member we've not loaded the definition of. */
135 lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
137 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 instantiate_class_template (TYPE_MAIN_VARIANT (type));
141 return type;
144 /* Like complete_type, but issue an error if the TYPE cannot be completed.
145 VALUE is used for informative diagnostics.
146 Returns NULL_TREE if the type cannot be made complete. */
148 tree
149 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
151 type = complete_type (type);
152 if (type == error_mark_node)
153 /* We already issued an error. */
154 return NULL_TREE;
155 else if (!COMPLETE_TYPE_P (type))
157 if (complain & tf_error)
158 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
159 note_failed_type_completion_for_satisfaction (type);
160 return NULL_TREE;
162 else
163 return type;
166 tree
167 complete_type_or_else (tree type, tree value)
169 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
173 /* Return the common type of two parameter lists.
174 We assume that comptypes has already been done and returned 1;
175 if that isn't so, this may crash.
177 As an optimization, free the space we allocate if the parameter
178 lists are already common. */
180 static tree
181 commonparms (tree p1, tree p2)
183 tree oldargs = p1, newargs, n;
184 int i, len;
185 int any_change = 0;
187 len = list_length (p1);
188 newargs = tree_last (p1);
190 if (newargs == void_list_node)
191 i = 1;
192 else
194 i = 0;
195 newargs = 0;
198 for (; i < len; i++)
199 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
201 n = newargs;
203 for (i = 0; p1;
204 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
206 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
208 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 any_change = 1;
211 else if (! TREE_PURPOSE (p1))
213 if (TREE_PURPOSE (p2))
215 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 any_change = 1;
219 else
221 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 any_change = 1;
223 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
225 if (TREE_VALUE (p1) != TREE_VALUE (p2))
227 any_change = 1;
228 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
230 else
231 TREE_VALUE (n) = TREE_VALUE (p1);
233 if (! any_change)
234 return oldargs;
236 return newargs;
239 /* Given a type, perhaps copied for a typedef,
240 find the "original" version of it. */
241 static tree
242 original_type (tree t)
244 int quals = cp_type_quals (t);
245 while (t != error_mark_node
246 && TYPE_NAME (t) != NULL_TREE)
248 tree x = TYPE_NAME (t);
249 if (TREE_CODE (x) != TYPE_DECL)
250 break;
251 x = DECL_ORIGINAL_TYPE (x);
252 if (x == NULL_TREE)
253 break;
254 t = x;
256 return cp_build_qualified_type (t, quals);
259 /* Merge the attributes of type OTHER_TYPE into the attributes of type TYPE
260 and return a variant of TYPE with the merged attributes. */
262 static tree
263 merge_type_attributes_from (tree type, tree other_type)
265 tree attrs = targetm.merge_type_attributes (type, other_type);
266 attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 return cp_build_type_attribute_variant (type, attrs);
270 /* Compare floating point conversion ranks and subranks of T1 and T2
271 types. If T1 and T2 have unordered conversion ranks, return 3.
272 If T1 has greater conversion rank than T2, return 2.
273 If T2 has greater conversion rank than T1, return -2.
274 If T1 has equal conversion rank as T2, return -1, 0 or 1 depending
275 on if T1 has smaller, equal or greater conversion subrank than
276 T2. */
279 cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
281 tree mv1 = TYPE_MAIN_VARIANT (t1);
282 tree mv2 = TYPE_MAIN_VARIANT (t2);
283 int extended1 = 0;
284 int extended2 = 0;
286 if (mv1 == mv2)
287 return 0;
289 for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
291 if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 extended1 = i + 1;
293 if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 extended2 = i + 1;
296 if (mv1 == bfloat16_type_node)
297 extended1 = true;
298 if (mv2 == bfloat16_type_node)
299 extended2 = true;
300 if (extended2 && !extended1)
302 int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 return ret == 3 ? 3 : -ret;
306 const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 gcc_assert (fmt1->b == 2 && fmt2->b == 2);
309 /* For {ibm,mips}_extended_format formats, the type has variable
310 precision up to ~2150 bits when the first double is around maximum
311 representable double and second double is subnormal minimum.
312 So, e.g. for __ibm128 vs. std::float128_t, they have unordered
313 ranks. */
314 int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 ? fmt2->emax - fmt2->emin + fmt2->p - 1 : fmt2->p);
318 /* The rank of a floating point type T is greater than the rank of
319 any floating-point type whose set of values is a proper subset
320 of the set of values of T. */
321 if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 || (p1 == p2 && fmt1->emax > fmt2->emax))
323 return 2;
324 if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 || (p1 == p2 && fmt1->emax < fmt2->emax))
326 return -2;
327 if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 || (p1 < p2 && fmt1->emax > fmt2->emax))
329 return 3;
330 if (!extended1 && !extended2)
332 /* The rank of long double is greater than the rank of double, which
333 is greater than the rank of float. */
334 if (t1 == long_double_type_node)
335 return 2;
336 else if (t2 == long_double_type_node)
337 return -2;
338 if (t1 == double_type_node)
339 return 2;
340 else if (t2 == double_type_node)
341 return -2;
342 if (t1 == float_type_node)
343 return 2;
344 else if (t2 == float_type_node)
345 return -2;
346 return 0;
348 /* Two extended floating-point types with the same set of values have equal
349 ranks. */
350 if (extended1 && extended2)
352 if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES))
354 /* Prefer higher extendedN value. */
355 if (extended1 > extended2)
356 return 1;
357 else if (extended1 < extended2)
358 return -1;
359 else
360 return 0;
362 else if (extended1 <= NUM_FLOATN_TYPES)
363 /* Prefer _FloatN type over _FloatMx type. */
364 return 1;
365 else if (extended2 <= NUM_FLOATN_TYPES)
366 return -1;
367 else
368 return 0;
371 /* gcc_assert (extended1 && !extended2); */
372 tree *p;
373 int cnt = 0;
374 for (p = &float_type_node; p <= &long_double_type_node; ++p)
376 const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 gcc_assert (fmt3->b == 2);
378 int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 if (p1 == p3 && fmt1->emax == fmt3->emax)
381 ++cnt;
383 /* An extended floating-point type with the same set of values
384 as exactly one cv-unqualified standard floating-point type
385 has a rank equal to the rank of that standard floating-point
386 type.
388 An extended floating-point type with the same set of values
389 as more than one cv-unqualified standard floating-point type
390 has a rank equal to the rank of double.
392 Thus, if the latter is true and t2 is long double, t2
393 has higher rank. */
394 if (cnt > 1 && mv2 == long_double_type_node)
395 return -2;
396 /* Otherwise, they have equal rank, but extended types
397 (other than std::bfloat16_t) have higher subrank.
398 std::bfloat16_t shouldn't have equal rank to any standard
399 floating point type. */
400 return 1;
403 /* Return the common type for two arithmetic types T1 and T2 under the
404 usual arithmetic conversions. The default conversions have already
405 been applied, and enumerated types converted to their compatible
406 integer types. */
408 static tree
409 cp_common_type (tree t1, tree t2)
411 enum tree_code code1 = TREE_CODE (t1);
412 enum tree_code code2 = TREE_CODE (t2);
413 tree attributes;
414 int i;
417 /* In what follows, we slightly generalize the rules given in [expr] so
418 as to deal with `long long' and `complex'. First, merge the
419 attributes. */
420 attributes = (*targetm.merge_type_attributes) (t1, t2);
422 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
424 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
425 return build_type_attribute_variant (t1, attributes);
426 else
427 return NULL_TREE;
430 /* FIXME: Attributes. */
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 /* If one type is complex, form the common type of the non-complex
439 components, then make that complex. Use T1 or T2 if it is the
440 required type. */
441 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
443 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
444 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
445 tree subtype
446 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
448 if (subtype == error_mark_node)
449 return subtype;
450 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
451 return build_type_attribute_variant (t1, attributes);
452 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
453 return build_type_attribute_variant (t2, attributes);
454 else
455 return build_type_attribute_variant (build_complex_type (subtype),
456 attributes);
459 if (code1 == VECTOR_TYPE)
461 /* When we get here we should have two vectors of the same size.
462 Just prefer the unsigned one if present. */
463 if (TYPE_UNSIGNED (t1))
464 return merge_type_attributes_from (t1, t2);
465 else
466 return merge_type_attributes_from (t2, t1);
469 /* If only one is real, use it as the result. */
470 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
471 return build_type_attribute_variant (t1, attributes);
472 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
473 return build_type_attribute_variant (t2, attributes);
475 if (code1 == REAL_TYPE
476 && (extended_float_type_p (t1) || extended_float_type_p (t2)))
478 tree mv1 = TYPE_MAIN_VARIANT (t1);
479 tree mv2 = TYPE_MAIN_VARIANT (t2);
480 if (mv1 == mv2)
481 return build_type_attribute_variant (t1, attributes);
483 int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
484 if (cmpret == 3)
485 return error_mark_node;
486 else if (cmpret >= 0)
487 return build_type_attribute_variant (t1, attributes);
488 else
489 return build_type_attribute_variant (t2, attributes);
492 /* Both real or both integers; use the one with greater precision. */
493 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
494 return build_type_attribute_variant (t1, attributes);
495 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
496 return build_type_attribute_variant (t2, attributes);
498 /* The types are the same; no need to do anything fancy. */
499 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
500 return build_type_attribute_variant (t1, attributes);
502 if (code1 != REAL_TYPE)
504 /* If one is unsigned long long, then convert the other to unsigned
505 long long. */
506 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
507 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
508 return build_type_attribute_variant (long_long_unsigned_type_node,
509 attributes);
510 /* If one is a long long, and the other is an unsigned long, and
511 long long can represent all the values of an unsigned long, then
512 convert to a long long. Otherwise, convert to an unsigned long
513 long. Otherwise, if either operand is long long, convert the
514 other to long long.
516 Since we're here, we know the TYPE_PRECISION is the same;
517 therefore converting to long long cannot represent all the values
518 of an unsigned long, so we choose unsigned long long in that
519 case. */
520 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
521 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
523 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
524 ? long_long_unsigned_type_node
525 : long_long_integer_type_node);
526 return build_type_attribute_variant (t, attributes);
529 /* Go through the same procedure, but for longs. */
530 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
531 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
532 return build_type_attribute_variant (long_unsigned_type_node,
533 attributes);
534 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
535 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
537 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
538 ? long_unsigned_type_node : long_integer_type_node);
539 return build_type_attribute_variant (t, attributes);
542 /* For __intN types, either the type is __int128 (and is lower
543 priority than the types checked above, but higher than other
544 128-bit types) or it's known to not be the same size as other
545 types (enforced in toplev.cc). Prefer the unsigned type. */
546 for (i = 0; i < NUM_INT_N_ENTS; i ++)
548 if (int_n_enabled_p [i]
549 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
550 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
551 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
552 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
554 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
555 ? int_n_trees[i].unsigned_type
556 : int_n_trees[i].signed_type);
557 return build_type_attribute_variant (t, attributes);
561 /* Otherwise prefer the unsigned one. */
562 if (TYPE_UNSIGNED (t1))
563 return build_type_attribute_variant (t1, attributes);
564 else
565 return build_type_attribute_variant (t2, attributes);
567 else
569 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
570 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
571 return build_type_attribute_variant (long_double_type_node,
572 attributes);
573 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
574 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
575 return build_type_attribute_variant (double_type_node,
576 attributes);
577 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
578 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
579 return build_type_attribute_variant (float_type_node,
580 attributes);
582 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
583 the standard C++ floating-point types. Logic earlier in this
584 function has already eliminated the possibility that
585 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
586 compelling reason to choose one or the other. */
587 return build_type_attribute_variant (t1, attributes);
591 /* T1 and T2 are arithmetic or enumeration types. Return the type
592 that will result from the "usual arithmetic conversions" on T1 and
593 T2 as described in [expr]. */
595 tree
596 type_after_usual_arithmetic_conversions (tree t1, tree t2)
598 gcc_assert (ARITHMETIC_TYPE_P (t1)
599 || VECTOR_TYPE_P (t1)
600 || UNSCOPED_ENUM_P (t1));
601 gcc_assert (ARITHMETIC_TYPE_P (t2)
602 || VECTOR_TYPE_P (t2)
603 || UNSCOPED_ENUM_P (t2));
605 /* Perform the integral promotions. We do not promote real types here. */
606 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
607 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
609 t1 = type_promotes_to (t1);
610 t2 = type_promotes_to (t2);
613 return cp_common_type (t1, t2);
616 static void
617 composite_pointer_error (const op_location_t &location,
618 diagnostic_t kind, tree t1, tree t2,
619 composite_pointer_operation operation)
621 switch (operation)
623 case CPO_COMPARISON:
624 emit_diagnostic (kind, location, 0,
625 "comparison between "
626 "distinct pointer types %qT and %qT lacks a cast",
627 t1, t2);
628 break;
629 case CPO_CONVERSION:
630 emit_diagnostic (kind, location, 0,
631 "conversion between "
632 "distinct pointer types %qT and %qT lacks a cast",
633 t1, t2);
634 break;
635 case CPO_CONDITIONAL_EXPR:
636 emit_diagnostic (kind, location, 0,
637 "conditional expression between "
638 "distinct pointer types %qT and %qT lacks a cast",
639 t1, t2);
640 break;
641 default:
642 gcc_unreachable ();
646 /* Subroutine of composite_pointer_type to implement the recursive
647 case. See that function for documentation of the parameters. And ADD_CONST
648 is used to track adding "const" where needed. */
650 static tree
651 composite_pointer_type_r (const op_location_t &location,
652 tree t1, tree t2, bool *add_const,
653 composite_pointer_operation operation,
654 tsubst_flags_t complain)
656 tree pointee1;
657 tree pointee2;
658 tree result_type;
659 tree attributes;
661 /* Determine the types pointed to by T1 and T2. */
662 if (TYPE_PTR_P (t1))
664 pointee1 = TREE_TYPE (t1);
665 pointee2 = TREE_TYPE (t2);
667 else
669 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
670 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
673 /* [expr.type]
675 If T1 and T2 are similar types, the result is the cv-combined type of
676 T1 and T2. */
677 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
678 result_type = pointee1;
679 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
680 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
682 result_type = composite_pointer_type_r (location, pointee1, pointee2,
683 add_const, operation, complain);
684 if (result_type == error_mark_node)
685 return error_mark_node;
687 else
689 if (complain & tf_error)
690 composite_pointer_error (location, DK_PERMERROR,
691 t1, t2, operation);
692 else
693 return error_mark_node;
694 result_type = void_type_node;
696 const int q1 = cp_type_quals (pointee1);
697 const int q2 = cp_type_quals (pointee2);
698 const int quals = q1 | q2;
699 result_type = cp_build_qualified_type (result_type,
700 (quals | (*add_const
701 ? TYPE_QUAL_CONST
702 : TYPE_UNQUALIFIED)));
703 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
704 the TLQ). The reason is that both T1 and T2 can then be converted to the
705 cv-combined type of T1 and T2. */
706 if (quals != q1 || quals != q2)
707 *add_const = true;
708 /* If the original types were pointers to members, so is the
709 result. */
710 if (TYPE_PTRMEM_P (t1))
712 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
713 TYPE_PTRMEM_CLASS_TYPE (t2)))
715 if (complain & tf_error)
716 composite_pointer_error (location, DK_PERMERROR,
717 t1, t2, operation);
718 else
719 return error_mark_node;
721 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
722 result_type);
724 else
725 result_type = build_pointer_type (result_type);
727 /* Merge the attributes. */
728 attributes = (*targetm.merge_type_attributes) (t1, t2);
729 return build_type_attribute_variant (result_type, attributes);
732 /* Return the composite pointer type (see [expr.type]) for T1 and T2.
733 ARG1 and ARG2 are the values with those types. The OPERATION is to
734 describe the operation between the pointer types,
735 in case an error occurs.
737 This routine also implements the computation of a common type for
738 pointers-to-members as per [expr.eq]. */
740 tree
741 composite_pointer_type (const op_location_t &location,
742 tree t1, tree t2, tree arg1, tree arg2,
743 composite_pointer_operation operation,
744 tsubst_flags_t complain)
746 tree class1;
747 tree class2;
749 /* [expr.type]
751 If one operand is a null pointer constant, the composite pointer
752 type is the type of the other operand. */
753 if (null_ptr_cst_p (arg1))
754 return t2;
755 if (null_ptr_cst_p (arg2))
756 return t1;
758 /* We have:
760 [expr.type]
762 If one of the operands has type "pointer to cv1 void", then
763 the other has type "pointer to cv2 T", and the composite pointer
764 type is "pointer to cv12 void", where cv12 is the union of cv1
765 and cv2.
767 If either type is a pointer to void, make sure it is T1. */
768 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
769 std::swap (t1, t2);
771 /* Now, if T1 is a pointer to void, merge the qualifiers. */
772 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
774 tree attributes;
775 tree result_type;
777 if (TYPE_PTRFN_P (t2))
779 if (complain & tf_error)
781 switch (operation)
783 case CPO_COMPARISON:
784 pedwarn (location, OPT_Wpedantic,
785 "ISO C++ forbids comparison between pointer "
786 "of type %<void *%> and pointer-to-function");
787 break;
788 case CPO_CONVERSION:
789 pedwarn (location, OPT_Wpedantic,
790 "ISO C++ forbids conversion between pointer "
791 "of type %<void *%> and pointer-to-function");
792 break;
793 case CPO_CONDITIONAL_EXPR:
794 pedwarn (location, OPT_Wpedantic,
795 "ISO C++ forbids conditional expression between "
796 "pointer of type %<void *%> and "
797 "pointer-to-function");
798 break;
799 default:
800 gcc_unreachable ();
803 else
804 return error_mark_node;
806 result_type
807 = cp_build_qualified_type (void_type_node,
808 (cp_type_quals (TREE_TYPE (t1))
809 | cp_type_quals (TREE_TYPE (t2))));
810 result_type = build_pointer_type (result_type);
811 /* Merge the attributes. */
812 attributes = (*targetm.merge_type_attributes) (t1, t2);
813 return build_type_attribute_variant (result_type, attributes);
816 if (c_dialect_objc () && TYPE_PTR_P (t1)
817 && TYPE_PTR_P (t2))
819 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
820 return objc_common_type (t1, t2);
823 /* if T1 or T2 is "pointer to noexcept function" and the other type is
824 "pointer to function", where the function types are otherwise the same,
825 "pointer to function" */
826 if (fnptr_conv_p (t1, t2))
827 return t1;
828 if (fnptr_conv_p (t2, t1))
829 return t2;
831 /* [expr.eq] permits the application of a pointer conversion to
832 bring the pointers to a common type. */
833 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
834 && CLASS_TYPE_P (TREE_TYPE (t1))
835 && CLASS_TYPE_P (TREE_TYPE (t2))
836 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
837 TREE_TYPE (t2)))
839 class1 = TREE_TYPE (t1);
840 class2 = TREE_TYPE (t2);
842 if (DERIVED_FROM_P (class1, class2))
843 t2 = (build_pointer_type
844 (cp_build_qualified_type (class1, cp_type_quals (class2))));
845 else if (DERIVED_FROM_P (class2, class1))
846 t1 = (build_pointer_type
847 (cp_build_qualified_type (class2, cp_type_quals (class1))));
848 else
850 if (complain & tf_error)
851 composite_pointer_error (location, DK_ERROR, t1, t2, operation);
852 return error_mark_node;
855 /* [expr.eq] permits the application of a pointer-to-member
856 conversion to change the class type of one of the types. */
857 else if (TYPE_PTRMEM_P (t1)
858 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
859 TYPE_PTRMEM_CLASS_TYPE (t2)))
861 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
862 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
864 if (DERIVED_FROM_P (class1, class2))
865 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
866 else if (DERIVED_FROM_P (class2, class1))
867 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
868 else
870 if (complain & tf_error)
871 switch (operation)
873 case CPO_COMPARISON:
874 error_at (location, "comparison between distinct "
875 "pointer-to-member types %qT and %qT lacks a cast",
876 t1, t2);
877 break;
878 case CPO_CONVERSION:
879 error_at (location, "conversion between distinct "
880 "pointer-to-member types %qT and %qT lacks a cast",
881 t1, t2);
882 break;
883 case CPO_CONDITIONAL_EXPR:
884 error_at (location, "conditional expression between distinct "
885 "pointer-to-member types %qT and %qT lacks a cast",
886 t1, t2);
887 break;
888 default:
889 gcc_unreachable ();
891 return error_mark_node;
895 bool add_const = false;
896 return composite_pointer_type_r (location, t1, t2, &add_const, operation,
897 complain);
900 /* Return the merged type of two types.
901 We assume that comptypes has already been done and returned 1;
902 if that isn't so, this may crash.
904 This just combines attributes and default arguments; any other
905 differences would cause the two types to compare unalike. */
907 tree
908 merge_types (tree t1, tree t2)
910 enum tree_code code1;
911 enum tree_code code2;
912 tree attributes;
914 /* Save time if the two types are the same. */
915 if (t1 == t2)
916 return t1;
917 if (original_type (t1) == original_type (t2))
918 return t1;
920 /* If one type is nonsense, use the other. */
921 if (t1 == error_mark_node)
922 return t2;
923 if (t2 == error_mark_node)
924 return t1;
926 /* Handle merging an auto redeclaration with a previous deduced
927 return type. */
928 if (is_auto (t1))
929 return t2;
931 /* Merge the attributes. */
932 attributes = (*targetm.merge_type_attributes) (t1, t2);
934 if (TYPE_PTRMEMFUNC_P (t1))
935 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
936 if (TYPE_PTRMEMFUNC_P (t2))
937 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
939 code1 = TREE_CODE (t1);
940 code2 = TREE_CODE (t2);
941 if (code1 != code2)
943 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
944 if (code1 == TYPENAME_TYPE)
946 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
947 code1 = TREE_CODE (t1);
949 else
951 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
952 code2 = TREE_CODE (t2);
956 switch (code1)
958 case POINTER_TYPE:
959 case REFERENCE_TYPE:
960 /* For two pointers, do this recursively on the target type. */
962 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
963 int quals = cp_type_quals (t1);
965 if (code1 == POINTER_TYPE)
967 t1 = build_pointer_type (target);
968 if (TREE_CODE (target) == METHOD_TYPE)
969 t1 = build_ptrmemfunc_type (t1);
971 else
972 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
973 t1 = build_type_attribute_variant (t1, attributes);
974 t1 = cp_build_qualified_type (t1, quals);
976 return t1;
979 case OFFSET_TYPE:
981 int quals;
982 tree pointee;
983 quals = cp_type_quals (t1);
984 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
985 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
986 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
987 pointee);
988 t1 = cp_build_qualified_type (t1, quals);
989 break;
992 case ARRAY_TYPE:
994 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
995 /* Save space: see if the result is identical to one of the args. */
996 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
997 return build_type_attribute_variant (t1, attributes);
998 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
999 return build_type_attribute_variant (t2, attributes);
1000 /* Merge the element types, and have a size if either arg has one. */
1001 t1 = build_cplus_array_type
1002 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
1003 break;
1006 case FUNCTION_TYPE:
1007 /* Function types: prefer the one that specified arg types.
1008 If both do, merge the arg types. Also merge the return types. */
1010 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1011 tree p1 = TYPE_ARG_TYPES (t1);
1012 tree p2 = TYPE_ARG_TYPES (t2);
1013 tree parms;
1015 /* Save space: see if the result is identical to one of the args. */
1016 if (valtype == TREE_TYPE (t1) && ! p2)
1017 return cp_build_type_attribute_variant (t1, attributes);
1018 if (valtype == TREE_TYPE (t2) && ! p1)
1019 return cp_build_type_attribute_variant (t2, attributes);
1021 /* Simple way if one arg fails to specify argument types. */
1022 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1023 parms = p2;
1024 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1025 parms = p1;
1026 else
1027 parms = commonparms (p1, p2);
1029 cp_cv_quals quals = type_memfn_quals (t1);
1030 cp_ref_qualifier rqual = type_memfn_rqual (t1);
1031 gcc_assert (quals == type_memfn_quals (t2));
1032 gcc_assert (rqual == type_memfn_rqual (t2));
1034 tree rval = build_function_type (valtype, parms);
1035 rval = apply_memfn_quals (rval, quals);
1036 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1037 TYPE_RAISES_EXCEPTIONS (t2));
1038 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1039 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1040 break;
1043 case METHOD_TYPE:
1045 /* Get this value the long way, since TYPE_METHOD_BASETYPE
1046 is just the main variant of this. */
1047 tree basetype = class_of_this_parm (t2);
1048 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1049 TYPE_RAISES_EXCEPTIONS (t2));
1050 cp_ref_qualifier rqual = type_memfn_rqual (t1);
1051 tree t3;
1052 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1054 /* If this was a member function type, get back to the
1055 original type of type member function (i.e., without
1056 the class instance variable up front. */
1057 t1 = build_function_type (TREE_TYPE (t1),
1058 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1059 t2 = build_function_type (TREE_TYPE (t2),
1060 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1061 t3 = merge_types (t1, t2);
1062 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1063 TYPE_ARG_TYPES (t3));
1064 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1065 break;
1068 case TYPENAME_TYPE:
1069 /* There is no need to merge attributes into a TYPENAME_TYPE.
1070 When the type is instantiated it will have whatever
1071 attributes result from the instantiation. */
1072 return t1;
1074 default:;
1075 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1076 return t1;
1077 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1078 return t2;
1079 break;
1082 return cp_build_type_attribute_variant (t1, attributes);
1085 /* Return the ARRAY_TYPE type without its domain. */
1087 tree
1088 strip_array_domain (tree type)
1090 tree t2;
1091 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1092 if (TYPE_DOMAIN (type) == NULL_TREE)
1093 return type;
1094 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1095 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1098 /* Wrapper around cp_common_type that is used by c-common.cc and other
1099 front end optimizations that remove promotions.
1101 Return the common type for two arithmetic types T1 and T2 under the
1102 usual arithmetic conversions. The default conversions have already
1103 been applied, and enumerated types converted to their compatible
1104 integer types. */
1106 tree
1107 common_type (tree t1, tree t2)
1109 /* If one type is nonsense, use the other */
1110 if (t1 == error_mark_node)
1111 return t2;
1112 if (t2 == error_mark_node)
1113 return t1;
1115 return cp_common_type (t1, t2);
1118 /* Return the common type of two pointer types T1 and T2. This is the
1119 type for the result of most arithmetic operations if the operands
1120 have the given two types.
1122 We assume that comp_target_types has already been done and returned
1123 nonzero; if that isn't so, this may crash. */
1125 tree
1126 common_pointer_type (tree t1, tree t2)
1128 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1129 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1130 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1132 return composite_pointer_type (input_location, t1, t2,
1133 error_mark_node, error_mark_node,
1134 CPO_CONVERSION, tf_warning_or_error);
1137 /* Compare two exception specifier types for exactness or subsetness, if
1138 allowed. Returns false for mismatch, true for match (same, or
1139 derived and !exact).
1141 [except.spec] "If a class X ... objects of class X or any class publicly
1142 and unambiguously derived from X. Similarly, if a pointer type Y * ...
1143 exceptions of type Y * or that are pointers to any type publicly and
1144 unambiguously derived from Y. Otherwise a function only allows exceptions
1145 that have the same type ..."
1146 This does not mention cv qualifiers and is different to what throw
1147 [except.throw] and catch [except.catch] will do. They will ignore the
1148 top level cv qualifiers, and allow qualifiers in the pointer to class
1149 example.
1151 We implement the letter of the standard. */
1153 static bool
1154 comp_except_types (tree a, tree b, bool exact)
1156 if (same_type_p (a, b))
1157 return true;
1158 else if (!exact)
1160 if (cp_type_quals (a) || cp_type_quals (b))
1161 return false;
1163 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1165 a = TREE_TYPE (a);
1166 b = TREE_TYPE (b);
1167 if (cp_type_quals (a) || cp_type_quals (b))
1168 return false;
1171 if (TREE_CODE (a) != RECORD_TYPE
1172 || TREE_CODE (b) != RECORD_TYPE)
1173 return false;
1175 if (publicly_uniquely_derived_p (a, b))
1176 return true;
1178 return false;
1181 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1182 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1183 If EXACT is ce_type, the C++17 type compatibility rules apply.
1184 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1185 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1186 are unordered, but we've already filtered out duplicates. Most lists will
1187 be in order, we should try to make use of that. */
1189 bool
1190 comp_except_specs (const_tree t1, const_tree t2, int exact)
1192 const_tree probe;
1193 const_tree base;
1194 int length = 0;
1196 if (t1 == t2)
1197 return true;
1199 /* First handle noexcept. */
1200 if (exact < ce_exact)
1202 if (exact == ce_type
1203 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1204 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1205 return true;
1207 /* noexcept(false) is compatible with no exception-specification,
1208 and less strict than any spec. */
1209 if (t1 == noexcept_false_spec)
1210 return t2 == NULL_TREE || exact == ce_derived;
1211 /* Even a derived noexcept(false) is compatible with no
1212 exception-specification. */
1213 if (t2 == noexcept_false_spec)
1214 return t1 == NULL_TREE;
1216 /* Otherwise, if we aren't looking for an exact match, noexcept is
1217 equivalent to throw(). */
1218 if (t1 == noexcept_true_spec)
1219 t1 = empty_except_spec;
1220 if (t2 == noexcept_true_spec)
1221 t2 = empty_except_spec;
1224 /* If any noexcept is left, it is only comparable to itself;
1225 either we're looking for an exact match or we're redeclaring a
1226 template with dependent noexcept. */
1227 if ((t1 && TREE_PURPOSE (t1))
1228 || (t2 && TREE_PURPOSE (t2)))
1229 return (t1 && t2
1230 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1232 if (t1 == NULL_TREE) /* T1 is ... */
1233 return t2 == NULL_TREE || exact == ce_derived;
1234 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1235 return t2 != NULL_TREE && !TREE_VALUE (t2);
1236 if (t2 == NULL_TREE) /* T2 is ... */
1237 return false;
1238 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1239 return exact == ce_derived;
1241 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1242 Count how many we find, to determine exactness. For exact matching and
1243 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1244 O(nm). */
1245 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1247 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1249 tree a = TREE_VALUE (probe);
1250 tree b = TREE_VALUE (t2);
1252 if (comp_except_types (a, b, exact))
1254 if (probe == base && exact > ce_derived)
1255 base = TREE_CHAIN (probe);
1256 length++;
1257 break;
1260 if (probe == NULL_TREE)
1261 return false;
1263 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1266 /* Compare the array types T1 and T2. CB says how we should behave when
1267 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1268 bounds_either says than any array can be [], bounds_first means that
1269 onlt T1 can be an array with unknown bounds. STRICT is true if
1270 qualifiers must match when comparing the types of the array elements. */
1272 static bool
1273 comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1274 bool strict)
1276 tree d1;
1277 tree d2;
1278 tree max1, max2;
1280 if (t1 == t2)
1281 return true;
1283 /* The type of the array elements must be the same. */
1284 if (strict
1285 ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1286 : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1287 return false;
1289 d1 = TYPE_DOMAIN (t1);
1290 d2 = TYPE_DOMAIN (t2);
1292 if (d1 == d2)
1293 return true;
1295 /* If one of the arrays is dimensionless, and the other has a
1296 dimension, they are of different types. However, it is valid to
1297 write:
1299 extern int a[];
1300 int a[3];
1302 by [basic.link]:
1304 declarations for an array object can specify
1305 array types that differ by the presence or absence of a major
1306 array bound (_dcl.array_). */
1307 if (!d1 && d2)
1308 return cb >= bounds_either;
1309 else if (d1 && !d2)
1310 return cb == bounds_either;
1312 /* Check that the dimensions are the same. */
1314 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1315 return false;
1316 max1 = TYPE_MAX_VALUE (d1);
1317 max2 = TYPE_MAX_VALUE (d2);
1319 if (!cp_tree_equal (max1, max2))
1320 return false;
1322 return true;
1325 /* Compare the relative position of T1 and T2 into their respective
1326 template parameter list.
1327 T1 and T2 must be template parameter types.
1328 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1330 static bool
1331 comp_template_parms_position (tree t1, tree t2)
1333 tree index1, index2;
1334 gcc_assert (t1 && t2
1335 && TREE_CODE (t1) == TREE_CODE (t2)
1336 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1337 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1338 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1340 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1341 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1343 /* Then compare their relative position. */
1344 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1345 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1346 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1347 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1348 return false;
1350 /* In C++14 we can end up comparing 'auto' to a normal template
1351 parameter. Don't confuse them. */
1352 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1353 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1355 return true;
1358 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1360 static bool
1361 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1363 t1 = TYPE_MAIN_VARIANT (t1);
1364 t2 = TYPE_MAIN_VARIANT (t2);
1366 if (TYPE_PTR_P (t1)
1367 && TYPE_PTR_P (t2))
1368 return true;
1370 /* The signedness of the parameter matters only when an integral
1371 type smaller than int is promoted to int, otherwise only the
1372 precision of the parameter matters.
1373 This check should make sure that the callee does not see
1374 undefined values in argument registers. */
1375 if (INTEGRAL_TYPE_P (t1)
1376 && INTEGRAL_TYPE_P (t2)
1377 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1378 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1379 || !targetm.calls.promote_prototypes (NULL_TREE)
1380 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1381 return true;
1383 return same_type_p (t1, t2);
1386 /* Check if a type cast between two function types can be considered safe. */
1388 static bool
1389 cxx_safe_function_type_cast_p (tree t1, tree t2)
1391 if (TREE_TYPE (t1) == void_type_node &&
1392 TYPE_ARG_TYPES (t1) == void_list_node)
1393 return true;
1395 if (TREE_TYPE (t2) == void_type_node &&
1396 TYPE_ARG_TYPES (t2) == void_list_node)
1397 return true;
1399 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1400 return false;
1402 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1403 t1 && t2;
1404 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1405 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1406 return false;
1408 return true;
1411 /* Subroutine in comptypes. */
1413 static bool
1414 structural_comptypes (tree t1, tree t2, int strict)
1416 /* Both should be types that are not obviously the same. */
1417 gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1419 /* Suppress typename resolution under spec_hasher::equal in place of calling
1420 push_to_top_level there. */
1421 if (!comparing_specializations)
1423 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1424 current instantiation. */
1425 if (TREE_CODE (t1) == TYPENAME_TYPE)
1426 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1428 if (TREE_CODE (t2) == TYPENAME_TYPE)
1429 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1432 if (TYPE_PTRMEMFUNC_P (t1))
1433 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1434 if (TYPE_PTRMEMFUNC_P (t2))
1435 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1437 /* Different classes of types can't be compatible. */
1438 if (TREE_CODE (t1) != TREE_CODE (t2))
1439 return false;
1441 /* Qualifiers must match. For array types, we will check when we
1442 recur on the array element types. */
1443 if (TREE_CODE (t1) != ARRAY_TYPE
1444 && cp_type_quals (t1) != cp_type_quals (t2))
1445 return false;
1446 if (TREE_CODE (t1) == FUNCTION_TYPE
1447 && type_memfn_quals (t1) != type_memfn_quals (t2))
1448 return false;
1449 /* Need to check this before TYPE_MAIN_VARIANT.
1450 FIXME function qualifiers should really change the main variant. */
1451 if (FUNC_OR_METHOD_TYPE_P (t1))
1453 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1454 return false;
1455 if (flag_noexcept_type
1456 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1457 TYPE_RAISES_EXCEPTIONS (t2),
1458 ce_type))
1459 return false;
1462 /* Allow for two different type nodes which have essentially the same
1463 definition. Note that we already checked for equality of the type
1464 qualifiers (just above). */
1465 if (TREE_CODE (t1) != ARRAY_TYPE
1466 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1467 goto check_alias;
1469 /* Compare the types. Return false on known not-same. Break on not
1470 known. Never return true from this switch -- you'll break
1471 specialization comparison. */
1472 switch (TREE_CODE (t1))
1474 case VOID_TYPE:
1475 case BOOLEAN_TYPE:
1476 /* All void and bool types are the same. */
1477 break;
1479 case OPAQUE_TYPE:
1480 case INTEGER_TYPE:
1481 case FIXED_POINT_TYPE:
1482 case REAL_TYPE:
1483 /* With these nodes, we can't determine type equivalence by
1484 looking at what is stored in the nodes themselves, because
1485 two nodes might have different TYPE_MAIN_VARIANTs but still
1486 represent the same type. For example, wchar_t and int could
1487 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1488 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1489 and are distinct types. On the other hand, int and the
1490 following typedef
1492 typedef int INT __attribute((may_alias));
1494 have identical properties, different TYPE_MAIN_VARIANTs, but
1495 represent the same type. The canonical type system keeps
1496 track of equivalence in this case, so we fall back on it. */
1497 if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1498 return false;
1500 /* We don't need or want the attribute comparison. */
1501 goto check_alias;
1503 case TEMPLATE_TEMPLATE_PARM:
1504 case BOUND_TEMPLATE_TEMPLATE_PARM:
1505 if (!comp_template_parms_position (t1, t2))
1506 return false;
1507 if (!comp_template_parms
1508 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1509 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1510 return false;
1511 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1512 break;
1513 /* Don't check inheritance. */
1514 strict = COMPARE_STRICT;
1515 /* Fall through. */
1517 case RECORD_TYPE:
1518 case UNION_TYPE:
1519 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1520 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1521 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1522 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1523 break;
1525 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1526 break;
1527 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1528 break;
1530 return false;
1532 case OFFSET_TYPE:
1533 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1534 strict & ~COMPARE_REDECLARATION))
1535 return false;
1536 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1537 return false;
1538 break;
1540 case REFERENCE_TYPE:
1541 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1542 return false;
1543 /* fall through to checks for pointer types */
1544 gcc_fallthrough ();
1546 case POINTER_TYPE:
1547 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1548 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1549 return false;
1550 break;
1552 case METHOD_TYPE:
1553 case FUNCTION_TYPE:
1554 /* Exception specs and memfn_rquals were checked above. */
1555 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1556 return false;
1557 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1558 return false;
1559 break;
1561 case ARRAY_TYPE:
1562 /* Target types must match incl. qualifiers. */
1563 if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1564 ? bounds_either : bounds_none),
1565 /*strict=*/true))
1566 return false;
1567 break;
1569 case TEMPLATE_TYPE_PARM:
1570 /* If T1 and T2 don't have the same relative position in their
1571 template parameters set, they can't be equal. */
1572 if (!comp_template_parms_position (t1, t2))
1573 return false;
1574 /* If T1 and T2 don't represent the same class template deduction,
1575 they aren't equal. */
1576 if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1577 CLASS_PLACEHOLDER_TEMPLATE (t2)))
1578 return false;
1579 /* Constrained 'auto's are distinct from parms that don't have the same
1580 constraints. */
1581 if (!equivalent_placeholder_constraints (t1, t2))
1582 return false;
1583 break;
1585 case TYPENAME_TYPE:
1586 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1587 TYPENAME_TYPE_FULLNAME (t2)))
1588 return false;
1589 /* Qualifiers don't matter on scopes. */
1590 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1591 TYPE_CONTEXT (t2)))
1592 return false;
1593 break;
1595 case UNBOUND_CLASS_TEMPLATE:
1596 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1597 return false;
1598 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1599 return false;
1600 break;
1602 case COMPLEX_TYPE:
1603 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1604 return false;
1605 break;
1607 case VECTOR_TYPE:
1608 if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1609 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1610 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1611 return false;
1612 break;
1614 case TYPE_PACK_EXPANSION:
1615 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1616 PACK_EXPANSION_PATTERN (t2))
1617 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1618 PACK_EXPANSION_EXTRA_ARGS (t2)));
1620 case DECLTYPE_TYPE:
1621 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1622 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1623 return false;
1624 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1625 return false;
1626 if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1627 return false;
1628 if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1629 return false;
1630 break;
1632 case TRAIT_TYPE:
1633 if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1634 return false;
1635 if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1636 || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1637 return false;
1638 break;
1640 case TYPEOF_TYPE:
1641 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1642 return false;
1643 break;
1645 default:
1646 return false;
1649 /* If we get here, we know that from a target independent POV the
1650 types are the same. Make sure the target attributes are also
1651 the same. */
1652 if (!comp_type_attributes (t1, t2))
1653 return false;
1655 check_alias:
1656 if (comparing_dependent_aliases)
1658 /* Don't treat an alias template specialization with dependent
1659 arguments as equivalent to its underlying type when used as a
1660 template argument; we need them to be distinct so that we
1661 substitute into the specialization arguments at instantiation
1662 time. And aliases can't be equivalent without being ==, so
1663 we don't need to look any deeper. */
1664 ++processing_template_decl;
1665 tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1666 tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1667 --processing_template_decl;
1668 if ((dep1 || dep2) && dep1 != dep2)
1669 return false;
1672 return true;
1675 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1676 is a bitwise-or of the COMPARE_* flags. */
1678 bool
1679 comptypes (tree t1, tree t2, int strict)
1681 gcc_checking_assert (t1 && t2);
1683 /* TYPE_ARGUMENT_PACKS are not really types. */
1684 gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1685 && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1687 if (t1 == t2)
1688 return true;
1690 /* Suppress errors caused by previously reported errors. */
1691 if (t1 == error_mark_node || t2 == error_mark_node)
1692 return false;
1694 if (strict == COMPARE_STRICT)
1696 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1697 /* At least one of the types requires structural equality, so
1698 perform a deep check. */
1699 return structural_comptypes (t1, t2, strict);
1701 if (flag_checking && param_use_canonical_types)
1703 bool result = structural_comptypes (t1, t2, strict);
1705 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1706 /* The two types are structurally equivalent, but their
1707 canonical types were different. This is a failure of the
1708 canonical type propagation code.*/
1709 internal_error
1710 ("canonical types differ for identical types %qT and %qT",
1711 t1, t2);
1712 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1713 /* Two types are structurally different, but the canonical
1714 types are the same. This means we were over-eager in
1715 assigning canonical types. */
1716 internal_error
1717 ("same canonical type node for different types %qT and %qT",
1718 t1, t2);
1720 return result;
1722 if (!flag_checking && param_use_canonical_types)
1723 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1724 else
1725 return structural_comptypes (t1, t2, strict);
1727 else if (strict == COMPARE_STRUCTURAL)
1728 return structural_comptypes (t1, t2, COMPARE_STRICT);
1729 else
1730 return structural_comptypes (t1, t2, strict);
1733 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1734 top-level qualifiers. */
1736 bool
1737 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1739 if (type1 == error_mark_node || type2 == error_mark_node)
1740 return false;
1741 if (type1 == type2)
1742 return true;
1744 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1745 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1746 return same_type_p (type1, type2);
1749 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1751 bool
1752 similar_type_p (tree type1, tree type2)
1754 if (type1 == error_mark_node || type2 == error_mark_node)
1755 return false;
1757 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1758 * they are the same type; or
1759 * they are both pointers, and the pointed-to types are similar; or
1760 * they are both pointers to member of the same class, and the types of
1761 the pointed-to members are similar; or
1762 * they are both arrays of the same size or both arrays of unknown bound,
1763 and the array element types are similar. */
1765 if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1766 return true;
1768 if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1769 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1770 || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1771 return comp_ptr_ttypes_const (type1, type2, bounds_either);
1773 return false;
1776 /* Helper function for layout_compatible_type_p and
1777 is_corresponding_member_aggr. Advance to next members (NULL if
1778 no further ones) and return true if those members are still part of
1779 the common initial sequence. */
1781 bool
1782 next_common_initial_sequence (tree &memb1, tree &memb2)
1784 while (memb1)
1786 if (TREE_CODE (memb1) != FIELD_DECL
1787 || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1789 memb1 = DECL_CHAIN (memb1);
1790 continue;
1792 if (DECL_FIELD_IS_BASE (memb1))
1794 memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1795 continue;
1797 break;
1799 while (memb2)
1801 if (TREE_CODE (memb2) != FIELD_DECL
1802 || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1804 memb2 = DECL_CHAIN (memb2);
1805 continue;
1807 if (DECL_FIELD_IS_BASE (memb2))
1809 memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1810 continue;
1812 break;
1814 if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1815 return true;
1816 if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1817 return false;
1818 if (DECL_BIT_FIELD_TYPE (memb1))
1820 if (!DECL_BIT_FIELD_TYPE (memb2))
1821 return false;
1822 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1823 DECL_BIT_FIELD_TYPE (memb2)))
1824 return false;
1825 if (TYPE_PRECISION (TREE_TYPE (memb1))
1826 != TYPE_PRECISION (TREE_TYPE (memb2)))
1827 return false;
1829 else if (DECL_BIT_FIELD_TYPE (memb2))
1830 return false;
1831 else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1832 return false;
1833 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1834 != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1835 return false;
1836 if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1837 return false;
1838 if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1839 return false;
1840 return true;
1843 /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1845 bool
1846 layout_compatible_type_p (tree type1, tree type2)
1848 if (type1 == error_mark_node || type2 == error_mark_node)
1849 return false;
1850 if (type1 == type2)
1851 return true;
1852 if (TREE_CODE (type1) != TREE_CODE (type2))
1853 return false;
1855 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1856 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1858 if (TREE_CODE (type1) == ENUMERAL_TYPE)
1859 return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1860 && same_type_p (finish_underlying_type (type1),
1861 finish_underlying_type (type2)));
1863 if (CLASS_TYPE_P (type1)
1864 && std_layout_type_p (type1)
1865 && std_layout_type_p (type2)
1866 && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1868 tree field1 = TYPE_FIELDS (type1);
1869 tree field2 = TYPE_FIELDS (type2);
1870 if (TREE_CODE (type1) == RECORD_TYPE)
1872 while (1)
1874 if (!next_common_initial_sequence (field1, field2))
1875 return false;
1876 if (field1 == NULL_TREE)
1877 return true;
1878 field1 = DECL_CHAIN (field1);
1879 field2 = DECL_CHAIN (field2);
1882 /* Otherwise both types must be union types.
1883 The standard says:
1884 "Two standard-layout unions are layout-compatible if they have
1885 the same number of non-static data members and corresponding
1886 non-static data members (in any order) have layout-compatible
1887 types."
1888 but the code anticipates that bitfield vs. non-bitfield,
1889 different bitfield widths or presence/absence of
1890 [[no_unique_address]] should be checked as well. */
1891 auto_vec<tree, 16> vec;
1892 unsigned int count = 0;
1893 for (; field1; field1 = DECL_CHAIN (field1))
1894 if (TREE_CODE (field1) == FIELD_DECL)
1895 count++;
1896 for (; field2; field2 = DECL_CHAIN (field2))
1897 if (TREE_CODE (field2) == FIELD_DECL)
1898 vec.safe_push (field2);
1899 /* Discussions on core lean towards treating multiple union fields
1900 of the same type as the same field, so this might need changing
1901 in the future. */
1902 if (count != vec.length ())
1903 return false;
1904 for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1906 if (TREE_CODE (field1) != FIELD_DECL)
1907 continue;
1908 unsigned int j;
1909 tree t1 = DECL_BIT_FIELD_TYPE (field1);
1910 if (t1 == NULL_TREE)
1911 t1 = TREE_TYPE (field1);
1912 FOR_EACH_VEC_ELT (vec, j, field2)
1914 tree t2 = DECL_BIT_FIELD_TYPE (field2);
1915 if (t2 == NULL_TREE)
1916 t2 = TREE_TYPE (field2);
1917 if (DECL_BIT_FIELD_TYPE (field1))
1919 if (!DECL_BIT_FIELD_TYPE (field2))
1920 continue;
1921 if (TYPE_PRECISION (TREE_TYPE (field1))
1922 != TYPE_PRECISION (TREE_TYPE (field2)))
1923 continue;
1925 else if (DECL_BIT_FIELD_TYPE (field2))
1926 continue;
1927 if (!layout_compatible_type_p (t1, t2))
1928 continue;
1929 if ((!lookup_attribute ("no_unique_address",
1930 DECL_ATTRIBUTES (field1)))
1931 != !lookup_attribute ("no_unique_address",
1932 DECL_ATTRIBUTES (field2)))
1933 continue;
1934 break;
1936 if (j == vec.length ())
1937 return false;
1938 vec.unordered_remove (j);
1940 return true;
1943 return same_type_p (type1, type2);
1946 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1948 bool
1949 at_least_as_qualified_p (const_tree type1, const_tree type2)
1951 int q1 = cp_type_quals (type1);
1952 int q2 = cp_type_quals (type2);
1954 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1955 return (q1 & q2) == q2;
1958 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1959 more cv-qualified that TYPE1, and 0 otherwise. */
1962 comp_cv_qualification (int q1, int q2)
1964 if (q1 == q2)
1965 return 0;
1967 if ((q1 & q2) == q2)
1968 return 1;
1969 else if ((q1 & q2) == q1)
1970 return -1;
1972 return 0;
1976 comp_cv_qualification (const_tree type1, const_tree type2)
1978 int q1 = cp_type_quals (type1);
1979 int q2 = cp_type_quals (type2);
1980 return comp_cv_qualification (q1, q2);
1983 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1984 subset of the cv-qualification signature of TYPE2, and the types
1985 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1988 comp_cv_qual_signature (tree type1, tree type2)
1990 if (comp_ptr_ttypes_real (type2, type1, -1))
1991 return 1;
1992 else if (comp_ptr_ttypes_real (type1, type2, -1))
1993 return -1;
1994 else
1995 return 0;
1998 /* Subroutines of `comptypes'. */
2000 /* Return true if two parameter type lists PARMS1 and PARMS2 are
2001 equivalent in the sense that functions with those parameter types
2002 can have equivalent types. The two lists must be equivalent,
2003 element by element. */
2005 bool
2006 compparms (const_tree parms1, const_tree parms2)
2008 const_tree t1, t2;
2010 /* An unspecified parmlist matches any specified parmlist
2011 whose argument types don't need default promotions. */
2013 for (t1 = parms1, t2 = parms2;
2014 t1 || t2;
2015 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2017 /* If one parmlist is shorter than the other,
2018 they fail to match. */
2019 if (!t1 || !t2)
2020 return false;
2021 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2022 return false;
2024 return true;
2028 /* Process a sizeof or alignof expression where the operand is a type.
2029 STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2030 or GNU (preferred alignment) semantics; it is ignored if OP is
2031 SIZEOF_EXPR. */
2033 tree
2034 cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2035 bool std_alignof, bool complain)
2037 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2038 if (type == error_mark_node)
2039 return error_mark_node;
2041 type = non_reference (type);
2042 if (TREE_CODE (type) == METHOD_TYPE)
2044 if (complain)
2046 pedwarn (loc, OPT_Wpointer_arith,
2047 "invalid application of %qs to a member function",
2048 OVL_OP_INFO (false, op)->name);
2049 return size_one_node;
2051 else
2052 return error_mark_node;
2054 else if (VOID_TYPE_P (type) && std_alignof)
2056 if (complain)
2057 error_at (loc, "invalid application of %qs to a void type",
2058 OVL_OP_INFO (false, op)->name);
2059 return error_mark_node;
2062 bool dependent_p = dependent_type_p (type);
2063 if (!dependent_p)
2064 complete_type (type);
2065 if (dependent_p
2066 /* VLA types will have a non-constant size. In the body of an
2067 uninstantiated template, we don't need to try to compute the
2068 value, because the sizeof expression is not an integral
2069 constant expression in that case. And, if we do try to
2070 compute the value, we'll likely end up with SAVE_EXPRs, which
2071 the template substitution machinery does not expect to see. */
2072 || (processing_template_decl
2073 && COMPLETE_TYPE_P (type)
2074 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2076 tree value = build_min (op, size_type_node, type);
2077 TREE_READONLY (value) = 1;
2078 if (op == ALIGNOF_EXPR && std_alignof)
2079 ALIGNOF_EXPR_STD_P (value) = true;
2080 SET_EXPR_LOCATION (value, loc);
2081 return value;
2084 return c_sizeof_or_alignof_type (loc, complete_type (type),
2085 op == SIZEOF_EXPR, std_alignof,
2086 complain);
2089 /* Return the size of the type, without producing any warnings for
2090 types whose size cannot be taken. This routine should be used only
2091 in some other routine that has already produced a diagnostic about
2092 using the size of such a type. */
2093 tree
2094 cxx_sizeof_nowarn (tree type)
2096 if (TREE_CODE (type) == FUNCTION_TYPE
2097 || VOID_TYPE_P (type)
2098 || TREE_CODE (type) == ERROR_MARK)
2099 return size_one_node;
2100 else if (!COMPLETE_TYPE_P (type))
2101 return size_zero_node;
2102 else
2103 return cxx_sizeof_or_alignof_type (input_location, type,
2104 SIZEOF_EXPR, false, false);
2107 /* Process a sizeof expression where the operand is an expression. */
2109 static tree
2110 cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2112 if (e == error_mark_node)
2113 return error_mark_node;
2115 if (instantiation_dependent_uneval_expression_p (e))
2117 e = build_min (SIZEOF_EXPR, size_type_node, e);
2118 TREE_SIDE_EFFECTS (e) = 0;
2119 TREE_READONLY (e) = 1;
2120 SET_EXPR_LOCATION (e, loc);
2122 return e;
2125 location_t e_loc = cp_expr_loc_or_loc (e, loc);
2126 STRIP_ANY_LOCATION_WRAPPER (e);
2128 /* To get the size of a static data member declared as an array of
2129 unknown bound, we need to instantiate it. */
2130 if (VAR_P (e)
2131 && VAR_HAD_UNKNOWN_BOUND (e)
2132 && DECL_TEMPLATE_INSTANTIATION (e))
2133 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
2135 if (TREE_CODE (e) == PARM_DECL
2136 && DECL_ARRAY_PARAMETER_P (e)
2137 && (complain & tf_warning))
2139 auto_diagnostic_group d;
2140 if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2141 "%<sizeof%> on array function parameter %qE "
2142 "will return size of %qT", e, TREE_TYPE (e)))
2143 inform (DECL_SOURCE_LOCATION (e), "declared here");
2146 e = mark_type_use (e);
2148 if (bitfield_p (e))
2150 if (complain & tf_error)
2151 error_at (e_loc,
2152 "invalid application of %<sizeof%> to a bit-field");
2153 else
2154 return error_mark_node;
2155 e = char_type_node;
2157 else if (is_overloaded_fn (e))
2159 if (complain & tf_error)
2160 permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2161 "an expression of function type");
2162 else
2163 return error_mark_node;
2164 e = char_type_node;
2166 else if (type_unknown_p (e))
2168 if (complain & tf_error)
2169 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2170 else
2171 return error_mark_node;
2172 e = char_type_node;
2174 else
2175 e = TREE_TYPE (e);
2177 return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2178 complain & tf_error);
2181 /* Implement the __alignof keyword: Return the minimum required
2182 alignment of E, measured in bytes. For VAR_DECL's and
2183 FIELD_DECL's return DECL_ALIGN (which can be set from an
2184 "aligned" __attribute__ specification). STD_ALIGNOF acts
2185 like in cxx_sizeof_or_alignof_type. */
2187 static tree
2188 cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2189 tsubst_flags_t complain)
2191 tree t;
2193 if (e == error_mark_node)
2194 return error_mark_node;
2196 if (processing_template_decl)
2198 e = build_min (ALIGNOF_EXPR, size_type_node, e);
2199 TREE_SIDE_EFFECTS (e) = 0;
2200 TREE_READONLY (e) = 1;
2201 SET_EXPR_LOCATION (e, loc);
2202 ALIGNOF_EXPR_STD_P (e) = std_alignof;
2204 return e;
2207 location_t e_loc = cp_expr_loc_or_loc (e, loc);
2208 STRIP_ANY_LOCATION_WRAPPER (e);
2210 e = mark_type_use (e);
2212 if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2213 !(complain & tf_error)))
2215 if (!(complain & tf_error))
2216 return error_mark_node;
2217 t = size_one_node;
2219 else if (VAR_P (e))
2220 t = size_int (DECL_ALIGN_UNIT (e));
2221 else if (bitfield_p (e))
2223 if (complain & tf_error)
2224 error_at (e_loc,
2225 "invalid application of %<__alignof%> to a bit-field");
2226 else
2227 return error_mark_node;
2228 t = size_one_node;
2230 else if (TREE_CODE (e) == COMPONENT_REF
2231 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2232 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2233 else if (is_overloaded_fn (e))
2235 if (complain & tf_error)
2236 permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2237 "an expression of function type");
2238 else
2239 return error_mark_node;
2240 if (TREE_CODE (e) == FUNCTION_DECL)
2241 t = size_int (DECL_ALIGN_UNIT (e));
2242 else
2243 t = size_one_node;
2245 else if (type_unknown_p (e))
2247 if (complain & tf_error)
2248 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2249 else
2250 return error_mark_node;
2251 t = size_one_node;
2253 else
2254 return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2255 ALIGNOF_EXPR, std_alignof,
2256 complain & tf_error);
2258 return fold_convert_loc (loc, size_type_node, t);
2261 /* Process a sizeof or alignof expression E with code OP where the operand
2262 is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2264 tree
2265 cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2266 bool std_alignof, bool complain)
2268 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2269 if (op == SIZEOF_EXPR)
2270 return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2271 else
2272 return cxx_alignof_expr (loc, e, std_alignof,
2273 complain? tf_warning_or_error : tf_none);
2276 /* Build a representation of an expression 'alignas(E).' Return the
2277 folded integer value of E if it is an integral constant expression
2278 that resolves to a valid alignment. If E depends on a template
2279 parameter, return a syntactic representation tree of kind
2280 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2281 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2283 tree
2284 cxx_alignas_expr (tree e)
2286 if (e == NULL_TREE || e == error_mark_node
2287 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2288 return e;
2290 if (TYPE_P (e))
2291 /* [dcl.align]/3:
2293 When the alignment-specifier is of the form
2294 alignas(type-id), it shall have the same effect as
2295 alignas(alignof(type-id)). */
2297 return cxx_sizeof_or_alignof_type (input_location,
2298 e, ALIGNOF_EXPR,
2299 /*std_alignof=*/true,
2300 /*complain=*/true);
2302 /* If we reach this point, it means the alignas expression if of
2303 the form "alignas(assignment-expression)", so we should follow
2304 what is stated by [dcl.align]/2. */
2306 if (value_dependent_expression_p (e))
2307 /* Leave value-dependent expression alone for now. */
2308 return e;
2310 e = instantiate_non_dependent_expr (e);
2311 e = mark_rvalue_use (e);
2313 /* [dcl.align]/2 says:
2315 the assignment-expression shall be an integral constant
2316 expression. */
2318 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2320 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2321 return error_mark_node;
2324 return cxx_constant_value (e);
2328 /* EXPR is being used in a context that is not a function call.
2329 Enforce:
2331 [expr.ref]
2333 The expression can be used only as the left-hand operand of a
2334 member function call.
2336 [expr.mptr.operator]
2338 If the result of .* or ->* is a function, then that result can be
2339 used only as the operand for the function call operator ().
2341 by issuing an error message if appropriate. Returns true iff EXPR
2342 violates these rules. */
2344 bool
2345 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2347 if (expr == NULL_TREE)
2348 return false;
2349 /* Don't enforce this in MS mode. */
2350 if (flag_ms_extensions)
2351 return false;
2352 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2353 expr = get_first_fn (expr);
2354 if (TREE_TYPE (expr)
2355 && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2357 if (complain & tf_error)
2359 if (DECL_P (expr))
2361 error_at (loc, "invalid use of non-static member function %qD",
2362 expr);
2363 inform (DECL_SOURCE_LOCATION (expr), "declared here");
2365 else
2366 error_at (loc, "invalid use of non-static member function of "
2367 "type %qT", TREE_TYPE (expr));
2369 return true;
2371 return false;
2374 /* If EXP is a reference to a bit-field, and the type of EXP does not
2375 match the declared type of the bit-field, return the declared type
2376 of the bit-field. Otherwise, return NULL_TREE. */
2378 tree
2379 is_bitfield_expr_with_lowered_type (const_tree exp)
2381 switch (TREE_CODE (exp))
2383 case COND_EXPR:
2384 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2385 ? TREE_OPERAND (exp, 1)
2386 : TREE_OPERAND (exp, 0)))
2387 return NULL_TREE;
2388 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2390 case COMPOUND_EXPR:
2391 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2393 case MODIFY_EXPR:
2394 case SAVE_EXPR:
2395 case UNARY_PLUS_EXPR:
2396 case PREDECREMENT_EXPR:
2397 case PREINCREMENT_EXPR:
2398 case POSTDECREMENT_EXPR:
2399 case POSTINCREMENT_EXPR:
2400 case NEGATE_EXPR:
2401 case NON_LVALUE_EXPR:
2402 case BIT_NOT_EXPR:
2403 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2405 case COMPONENT_REF:
2407 tree field;
2409 field = TREE_OPERAND (exp, 1);
2410 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2411 return NULL_TREE;
2412 if (same_type_ignoring_top_level_qualifiers_p
2413 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2414 return NULL_TREE;
2415 return DECL_BIT_FIELD_TYPE (field);
2418 case VAR_DECL:
2419 if (DECL_HAS_VALUE_EXPR_P (exp))
2420 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2421 (CONST_CAST_TREE (exp)));
2422 return NULL_TREE;
2424 case VIEW_CONVERT_EXPR:
2425 if (location_wrapper_p (exp))
2426 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2427 else
2428 return NULL_TREE;
2430 default:
2431 return NULL_TREE;
2435 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2436 bitfield with a lowered type, the type of EXP is returned, rather
2437 than NULL_TREE. */
2439 tree
2440 unlowered_expr_type (const_tree exp)
2442 tree type;
2443 tree etype = TREE_TYPE (exp);
2445 type = is_bitfield_expr_with_lowered_type (exp);
2446 if (type)
2447 type = cp_build_qualified_type (type, cp_type_quals (etype));
2448 else
2449 type = etype;
2451 return type;
2454 /* Perform the conversions in [expr] that apply when an lvalue appears
2455 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2456 function-to-pointer conversions. In addition, bitfield references are
2457 converted to their declared types. Note that this function does not perform
2458 the lvalue-to-rvalue conversion for class types. If you need that conversion
2459 for class types, then you probably need to use force_rvalue.
2461 Although the returned value is being used as an rvalue, this
2462 function does not wrap the returned expression in a
2463 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2464 that the return value is no longer an lvalue. */
2466 tree
2467 decay_conversion (tree exp,
2468 tsubst_flags_t complain,
2469 bool reject_builtin /* = true */)
2471 tree type;
2472 enum tree_code code;
2473 location_t loc = cp_expr_loc_or_input_loc (exp);
2475 type = TREE_TYPE (exp);
2476 if (type == error_mark_node)
2477 return error_mark_node;
2479 exp = resolve_nondeduced_context_or_error (exp, complain);
2481 code = TREE_CODE (type);
2483 if (error_operand_p (exp))
2484 return error_mark_node;
2486 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2488 mark_rvalue_use (exp, loc, reject_builtin);
2489 return nullptr_node;
2492 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2493 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2494 if (code == VOID_TYPE)
2496 if (complain & tf_error)
2497 error_at (loc, "void value not ignored as it ought to be");
2498 return error_mark_node;
2500 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2501 return error_mark_node;
2502 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2504 exp = mark_lvalue_use (exp);
2505 if (reject_builtin && reject_gcc_builtin (exp, loc))
2506 return error_mark_node;
2507 return cp_build_addr_expr (exp, complain);
2509 if (code == ARRAY_TYPE)
2511 tree adr;
2512 tree ptrtype;
2514 exp = mark_lvalue_use (exp);
2516 if (INDIRECT_REF_P (exp))
2517 return build_nop (build_pointer_type (TREE_TYPE (type)),
2518 TREE_OPERAND (exp, 0));
2520 if (TREE_CODE (exp) == COMPOUND_EXPR)
2522 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2523 if (op1 == error_mark_node)
2524 return error_mark_node;
2525 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2526 TREE_OPERAND (exp, 0), op1);
2529 if (!obvalue_p (exp)
2530 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2532 if (complain & tf_error)
2533 error_at (loc, "invalid use of non-lvalue array");
2534 return error_mark_node;
2537 ptrtype = build_pointer_type (TREE_TYPE (type));
2539 if (VAR_P (exp))
2541 if (!cxx_mark_addressable (exp))
2542 return error_mark_node;
2543 adr = build_nop (ptrtype, build_address (exp));
2544 return adr;
2546 /* This way is better for a COMPONENT_REF since it can
2547 simplify the offset for a component. */
2548 adr = cp_build_addr_expr (exp, complain);
2549 return cp_convert (ptrtype, adr, complain);
2552 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2553 exp = mark_rvalue_use (exp, loc, reject_builtin);
2555 /* If a bitfield is used in a context where integral promotion
2556 applies, then the caller is expected to have used
2557 default_conversion. That function promotes bitfields correctly
2558 before calling this function. At this point, if we have a
2559 bitfield referenced, we may assume that is not subject to
2560 promotion, and that, therefore, the type of the resulting rvalue
2561 is the declared type of the bitfield. */
2562 exp = convert_bitfield_to_declared_type (exp);
2564 /* We do not call rvalue() here because we do not want to wrap EXP
2565 in a NON_LVALUE_EXPR. */
2567 /* [basic.lval]
2569 Non-class rvalues always have cv-unqualified types. */
2570 type = TREE_TYPE (exp);
2571 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2572 exp = build_nop (cv_unqualified (type), exp);
2574 if (!complete_type_or_maybe_complain (type, exp, complain))
2575 return error_mark_node;
2577 return exp;
2580 /* Perform preparatory conversions, as part of the "usual arithmetic
2581 conversions". In particular, as per [expr]:
2583 Whenever an lvalue expression appears as an operand of an
2584 operator that expects the rvalue for that operand, the
2585 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2586 standard conversions are applied to convert the expression to an
2587 rvalue.
2589 In addition, we perform integral promotions here, as those are
2590 applied to both operands to a binary operator before determining
2591 what additional conversions should apply. */
2593 static tree
2594 cp_default_conversion (tree exp, tsubst_flags_t complain)
2596 /* Check for target-specific promotions. */
2597 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2598 if (promoted_type)
2599 exp = cp_convert (promoted_type, exp, complain);
2600 /* Perform the integral promotions first so that bitfield
2601 expressions (which may promote to "int", even if the bitfield is
2602 declared "unsigned") are promoted correctly. */
2603 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2604 exp = cp_perform_integral_promotions (exp, complain);
2605 /* Perform the other conversions. */
2606 exp = decay_conversion (exp, complain);
2608 return exp;
2611 /* C version. */
2613 tree
2614 default_conversion (tree exp)
2616 return cp_default_conversion (exp, tf_warning_or_error);
2619 /* EXPR is an expression with an integral or enumeration type.
2620 Perform the integral promotions in [conv.prom], and return the
2621 converted value. */
2623 tree
2624 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2626 tree type;
2627 tree promoted_type;
2629 expr = mark_rvalue_use (expr);
2630 if (error_operand_p (expr))
2631 return error_mark_node;
2633 type = TREE_TYPE (expr);
2635 /* [conv.prom]
2637 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2638 of type int if int can represent all the values of the bit-field;
2639 otherwise, it can be converted to unsigned int if unsigned int can
2640 represent all the values of the bit-field. If the bit-field is larger yet,
2641 no integral promotion applies to it. If the bit-field has an enumerated
2642 type, it is treated as any other value of that type for promotion
2643 purposes. */
2644 tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2645 if (bitfield_type
2646 && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2647 || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2648 type = bitfield_type;
2650 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2651 /* Scoped enums don't promote. */
2652 if (SCOPED_ENUM_P (type))
2653 return expr;
2654 promoted_type = type_promotes_to (type);
2655 if (type != promoted_type)
2656 expr = cp_convert (promoted_type, expr, complain);
2657 else if (bitfield_type && bitfield_type != type)
2658 /* Prevent decay_conversion from converting to bitfield_type. */
2659 expr = build_nop (type, expr);
2660 return expr;
2663 /* C version. */
2665 tree
2666 perform_integral_promotions (tree expr)
2668 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2671 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2672 decay_conversion to one. */
2675 string_conv_p (const_tree totype, const_tree exp, int warn)
2677 tree t;
2679 if (!TYPE_PTR_P (totype))
2680 return 0;
2682 t = TREE_TYPE (totype);
2683 if (!same_type_p (t, char_type_node)
2684 && !same_type_p (t, char8_type_node)
2685 && !same_type_p (t, char16_type_node)
2686 && !same_type_p (t, char32_type_node)
2687 && !same_type_p (t, wchar_type_node))
2688 return 0;
2690 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2692 STRIP_ANY_LOCATION_WRAPPER (exp);
2694 if (TREE_CODE (exp) == STRING_CST)
2696 /* Make sure that we don't try to convert between char and wide chars. */
2697 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2698 return 0;
2700 else
2702 /* Is this a string constant which has decayed to 'const char *'? */
2703 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2704 if (!same_type_p (TREE_TYPE (exp), t))
2705 return 0;
2706 STRIP_NOPS (exp);
2707 if (TREE_CODE (exp) != ADDR_EXPR
2708 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2709 return 0;
2711 if (warn)
2713 if (cxx_dialect >= cxx11)
2714 pedwarn (loc, OPT_Wwrite_strings,
2715 "ISO C++ forbids converting a string constant to %qT",
2716 totype);
2717 else
2718 warning_at (loc, OPT_Wwrite_strings,
2719 "deprecated conversion from string constant to %qT",
2720 totype);
2723 return 1;
2726 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2727 can, for example, use as an lvalue. This code used to be in
2728 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2729 expressions, where we're dealing with aggregates. But now it's again only
2730 called from unary_complex_lvalue. The case (in particular) that led to
2731 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2732 get it there. */
2734 static tree
2735 rationalize_conditional_expr (enum tree_code code, tree t,
2736 tsubst_flags_t complain)
2738 location_t loc = cp_expr_loc_or_input_loc (t);
2740 /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2741 the first operand is always the one to be used if both operands
2742 are equal, so we know what conditional expression this used to be. */
2743 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2745 tree op0 = TREE_OPERAND (t, 0);
2746 tree op1 = TREE_OPERAND (t, 1);
2748 /* The following code is incorrect if either operand side-effects. */
2749 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2750 && !TREE_SIDE_EFFECTS (op1));
2751 return
2752 build_conditional_expr (loc,
2753 build_x_binary_op (loc,
2754 (TREE_CODE (t) == MIN_EXPR
2755 ? LE_EXPR : GE_EXPR),
2756 op0, TREE_CODE (op0),
2757 op1, TREE_CODE (op1),
2758 NULL_TREE,
2759 /*overload=*/NULL,
2760 complain),
2761 cp_build_unary_op (code, op0, false, complain),
2762 cp_build_unary_op (code, op1, false, complain),
2763 complain);
2766 tree op1 = TREE_OPERAND (t, 1);
2767 if (TREE_CODE (op1) != THROW_EXPR)
2768 op1 = cp_build_unary_op (code, op1, false, complain);
2769 tree op2 = TREE_OPERAND (t, 2);
2770 if (TREE_CODE (op2) != THROW_EXPR)
2771 op2 = cp_build_unary_op (code, op2, false, complain);
2773 return
2774 build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2777 /* Given the TYPE of an anonymous union field inside T, return the
2778 FIELD_DECL for the field. If not found return NULL_TREE. Because
2779 anonymous unions can nest, we must also search all anonymous unions
2780 that are directly reachable. */
2782 tree
2783 lookup_anon_field (tree, tree type)
2785 tree field;
2787 type = TYPE_MAIN_VARIANT (type);
2788 field = ANON_AGGR_TYPE_FIELD (type);
2789 gcc_assert (field);
2790 return field;
2793 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2794 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2795 non-NULL, it indicates the path to the base used to name MEMBER.
2796 If PRESERVE_REFERENCE is true, the expression returned will have
2797 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2798 returned will have the type referred to by the reference.
2800 This function does not perform access control; that is either done
2801 earlier by the parser when the name of MEMBER is resolved to MEMBER
2802 itself, or later when overload resolution selects one of the
2803 functions indicated by MEMBER. */
2805 tree
2806 build_class_member_access_expr (cp_expr object, tree member,
2807 tree access_path, bool preserve_reference,
2808 tsubst_flags_t complain)
2810 tree object_type;
2811 tree member_scope;
2812 tree result = NULL_TREE;
2813 tree using_decl = NULL_TREE;
2815 if (error_operand_p (object) || error_operand_p (member))
2816 return error_mark_node;
2818 gcc_assert (DECL_P (member) || BASELINK_P (member));
2820 /* [expr.ref]
2822 The type of the first expression shall be "class object" (of a
2823 complete type). */
2824 object_type = TREE_TYPE (object);
2825 if (!currently_open_class (object_type)
2826 && !complete_type_or_maybe_complain (object_type, object, complain))
2827 return error_mark_node;
2828 if (!CLASS_TYPE_P (object_type))
2830 if (complain & tf_error)
2832 if (INDIRECT_TYPE_P (object_type)
2833 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2834 error ("request for member %qD in %qE, which is of pointer "
2835 "type %qT (maybe you meant to use %<->%> ?)",
2836 member, object.get_value (), object_type);
2837 else
2838 error ("request for member %qD in %qE, which is of non-class "
2839 "type %qT", member, object.get_value (), object_type);
2841 return error_mark_node;
2844 /* The standard does not seem to actually say that MEMBER must be a
2845 member of OBJECT_TYPE. However, that is clearly what is
2846 intended. */
2847 if (DECL_P (member))
2849 member_scope = DECL_CLASS_CONTEXT (member);
2850 if (!mark_used (member, complain) && !(complain & tf_error))
2851 return error_mark_node;
2853 if (TREE_UNAVAILABLE (member))
2854 error_unavailable_use (member, NULL_TREE);
2855 else if (TREE_DEPRECATED (member))
2856 warn_deprecated_use (member, NULL_TREE);
2858 else
2859 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2860 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2861 presently be the anonymous union. Go outwards until we find a
2862 type related to OBJECT_TYPE. */
2863 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2864 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2865 object_type))
2866 member_scope = TYPE_CONTEXT (member_scope);
2867 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2869 if (complain & tf_error)
2871 if (TREE_CODE (member) == FIELD_DECL)
2872 error ("invalid use of non-static data member %qE", member);
2873 else
2874 error ("%qD is not a member of %qT", member, object_type);
2876 return error_mark_node;
2879 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2880 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2881 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2882 if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2884 temp = cp_build_fold_indirect_ref (temp);
2885 if (!lvalue_p (object) && lvalue_p (temp))
2886 /* Preserve rvalueness. */
2887 temp = move (temp);
2888 object = temp;
2891 /* In [expr.ref], there is an explicit list of the valid choices for
2892 MEMBER. We check for each of those cases here. */
2893 if (VAR_P (member))
2895 /* A static data member. */
2896 result = member;
2897 mark_exp_read (object);
2899 if (tree wrap = maybe_get_tls_wrapper_call (result))
2900 /* Replace an evaluated use of the thread_local variable with
2901 a call to its wrapper. */
2902 result = wrap;
2904 /* If OBJECT has side-effects, they are supposed to occur. */
2905 if (TREE_SIDE_EFFECTS (object))
2906 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2908 else if (TREE_CODE (member) == FIELD_DECL)
2910 /* A non-static data member. */
2911 bool null_object_p;
2912 int type_quals;
2913 tree member_type;
2915 if (INDIRECT_REF_P (object))
2916 null_object_p =
2917 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2918 else
2919 null_object_p = false;
2921 /* Convert OBJECT to the type of MEMBER. */
2922 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2923 TYPE_MAIN_VARIANT (member_scope)))
2925 tree binfo;
2926 base_kind kind;
2928 /* We didn't complain above about a currently open class, but now we
2929 must: we don't know how to refer to a base member before layout is
2930 complete. But still don't complain in a template. */
2931 if (!cp_unevaluated_operand
2932 && !dependent_type_p (object_type)
2933 && !complete_type_or_maybe_complain (object_type, object,
2934 complain))
2935 return error_mark_node;
2937 binfo = lookup_base (access_path ? access_path : object_type,
2938 member_scope, ba_unique, &kind, complain);
2939 if (binfo == error_mark_node)
2940 return error_mark_node;
2942 /* It is invalid to try to get to a virtual base of a
2943 NULL object. The most common cause is invalid use of
2944 offsetof macro. */
2945 if (null_object_p && kind == bk_via_virtual)
2947 if (complain & tf_error)
2949 error ("invalid access to non-static data member %qD in "
2950 "virtual base of NULL object", member);
2952 return error_mark_node;
2955 /* Convert to the base. */
2956 object = build_base_path (PLUS_EXPR, object, binfo,
2957 /*nonnull=*/1, complain);
2958 /* If we found the base successfully then we should be able
2959 to convert to it successfully. */
2960 gcc_assert (object != error_mark_node);
2963 /* If MEMBER is from an anonymous aggregate, we have converted
2964 OBJECT so that it refers to the class containing the
2965 anonymous union. Generate a reference to the anonymous union
2966 itself, and recur to find MEMBER. */
2967 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2968 /* When this code is called from build_field_call, the
2969 object already has the type of the anonymous union.
2970 That is because the COMPONENT_REF was already
2971 constructed, and was then disassembled before calling
2972 build_field_call. After the function-call code is
2973 cleaned up, this waste can be eliminated. */
2974 && (!same_type_ignoring_top_level_qualifiers_p
2975 (TREE_TYPE (object), DECL_CONTEXT (member))))
2977 tree anonymous_union;
2979 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2980 DECL_CONTEXT (member));
2981 object = build_class_member_access_expr (object,
2982 anonymous_union,
2983 /*access_path=*/NULL_TREE,
2984 preserve_reference,
2985 complain);
2988 /* Compute the type of the field, as described in [expr.ref]. */
2989 type_quals = TYPE_UNQUALIFIED;
2990 member_type = TREE_TYPE (member);
2991 if (!TYPE_REF_P (member_type))
2993 type_quals = (cp_type_quals (member_type)
2994 | cp_type_quals (object_type));
2996 /* A field is const (volatile) if the enclosing object, or the
2997 field itself, is const (volatile). But, a mutable field is
2998 not const, even within a const object. */
2999 if (DECL_MUTABLE_P (member))
3000 type_quals &= ~TYPE_QUAL_CONST;
3001 member_type = cp_build_qualified_type (member_type, type_quals);
3004 result = build3_loc (input_location, COMPONENT_REF, member_type,
3005 object, member, NULL_TREE);
3007 /* Mark the expression const or volatile, as appropriate. Even
3008 though we've dealt with the type above, we still have to mark the
3009 expression itself. */
3010 if (type_quals & TYPE_QUAL_CONST)
3011 TREE_READONLY (result) = 1;
3012 if (type_quals & TYPE_QUAL_VOLATILE)
3013 TREE_THIS_VOLATILE (result) = 1;
3015 else if (BASELINK_P (member))
3017 /* The member is a (possibly overloaded) member function. */
3018 tree functions;
3019 tree type;
3021 /* If the MEMBER is exactly one static member function, then we
3022 know the type of the expression. Otherwise, we must wait
3023 until overload resolution has been performed. */
3024 functions = BASELINK_FUNCTIONS (member);
3025 if (TREE_CODE (functions) == FUNCTION_DECL
3026 && DECL_STATIC_FUNCTION_P (functions))
3027 type = TREE_TYPE (functions);
3028 else
3029 type = unknown_type_node;
3030 /* Note that we do not convert OBJECT to the BASELINK_BINFO
3031 base. That will happen when the function is called. */
3032 result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3033 NULL_TREE);
3035 else if (TREE_CODE (member) == CONST_DECL)
3037 /* The member is an enumerator. */
3038 result = member;
3039 /* If OBJECT has side-effects, they are supposed to occur. */
3040 if (TREE_SIDE_EFFECTS (object))
3041 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3042 object, result);
3044 else if ((using_decl = strip_using_decl (member)) != member)
3045 result = build_class_member_access_expr (object,
3046 using_decl,
3047 access_path, preserve_reference,
3048 complain);
3049 else
3051 if (complain & tf_error)
3052 error ("invalid use of %qD", member);
3053 return error_mark_node;
3056 if (!preserve_reference)
3057 /* [expr.ref]
3059 If E2 is declared to have type "reference to T", then ... the
3060 type of E1.E2 is T. */
3061 result = convert_from_reference (result);
3063 return result;
3066 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3067 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3069 tree
3070 lookup_destructor (tree object, tree scope, tree dtor_name,
3071 tsubst_flags_t complain)
3073 tree object_type = TREE_TYPE (object);
3074 tree dtor_type = TREE_OPERAND (dtor_name, 0);
3075 tree expr;
3077 /* We've already complained about this destructor. */
3078 if (dtor_type == error_mark_node)
3079 return error_mark_node;
3081 if (scope && !check_dtor_name (scope, dtor_type))
3083 if (complain & tf_error)
3084 error ("qualified type %qT does not match destructor name ~%qT",
3085 scope, dtor_type);
3086 return error_mark_node;
3088 if (is_auto (dtor_type))
3089 dtor_type = object_type;
3090 else if (identifier_p (dtor_type))
3092 /* In a template, names we can't find a match for are still accepted
3093 destructor names, and we check them here. */
3094 if (check_dtor_name (object_type, dtor_type))
3095 dtor_type = object_type;
3096 else
3098 if (complain & tf_error)
3099 error ("object type %qT does not match destructor name ~%qT",
3100 object_type, dtor_type);
3101 return error_mark_node;
3105 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3107 if (complain & tf_error)
3108 error ("the type being destroyed is %qT, but the destructor "
3109 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3110 return error_mark_node;
3112 expr = lookup_member (dtor_type, complete_dtor_identifier,
3113 /*protect=*/1, /*want_type=*/false,
3114 tf_warning_or_error);
3115 if (!expr)
3117 if (complain & tf_error)
3118 cxx_incomplete_type_error (dtor_name, dtor_type);
3119 return error_mark_node;
3121 expr = (adjust_result_of_qualified_name_lookup
3122 (expr, dtor_type, object_type));
3123 if (scope == NULL_TREE)
3124 /* We need to call adjust_result_of_qualified_name_lookup in case the
3125 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3126 that we still get virtual function binding. */
3127 BASELINK_QUALIFIED_P (expr) = false;
3128 return expr;
3131 /* An expression of the form "A::template B" has been resolved to
3132 DECL. Issue a diagnostic if B is not a template or template
3133 specialization. */
3135 void
3136 check_template_keyword (tree decl)
3138 /* The standard says:
3140 [temp.names]
3142 If a name prefixed by the keyword template is not a member
3143 template, the program is ill-formed.
3145 DR 228 removed the restriction that the template be a member
3146 template.
3148 DR 96, if accepted would add the further restriction that explicit
3149 template arguments must be provided if the template keyword is
3150 used, but, as of 2005-10-16, that DR is still in "drafting". If
3151 this DR is accepted, then the semantic checks here can be
3152 simplified, as the entity named must in fact be a template
3153 specialization, rather than, as at present, a set of overloaded
3154 functions containing at least one template function. */
3155 if (TREE_CODE (decl) != TEMPLATE_DECL
3156 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3158 if (VAR_P (decl))
3160 if (DECL_USE_TEMPLATE (decl)
3161 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3163 else
3164 permerror (input_location, "%qD is not a template", decl);
3166 else if (!is_overloaded_fn (decl))
3167 permerror (input_location, "%qD is not a template", decl);
3168 else
3170 bool found = false;
3172 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3173 !found && iter; ++iter)
3175 tree fn = *iter;
3176 if (TREE_CODE (fn) == TEMPLATE_DECL
3177 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3178 || (TREE_CODE (fn) == FUNCTION_DECL
3179 && DECL_USE_TEMPLATE (fn)
3180 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3181 found = true;
3183 if (!found)
3184 permerror (input_location, "%qD is not a template", decl);
3189 /* Record that an access failure occurred on BASETYPE_PATH attempting
3190 to access DECL, where DIAG_DECL should be used for diagnostics. */
3192 void
3193 access_failure_info::record_access_failure (tree basetype_path,
3194 tree decl, tree diag_decl)
3196 m_was_inaccessible = true;
3197 m_basetype_path = basetype_path;
3198 m_decl = decl;
3199 m_diag_decl = diag_decl;
3202 /* If an access failure was recorded, then attempt to locate an
3203 accessor function for the pertinent field.
3204 Otherwise, return NULL_TREE. */
3206 tree
3207 access_failure_info::get_any_accessor (bool const_p) const
3209 if (!was_inaccessible_p ())
3210 return NULL_TREE;
3212 tree accessor
3213 = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3214 if (!accessor)
3215 return NULL_TREE;
3217 /* The accessor must itself be accessible for it to be a reasonable
3218 suggestion. */
3219 if (!accessible_p (m_basetype_path, accessor, true))
3220 return NULL_TREE;
3222 return accessor;
3225 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3226 replacing the primary location in RICHLOC with "accessor()". */
3228 void
3229 access_failure_info::add_fixit_hint (rich_location *richloc,
3230 tree accessor_decl)
3232 pretty_printer pp;
3233 pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3234 pp_string (&pp, "()");
3235 richloc->add_fixit_replace (pp_formatted_text (&pp));
3238 /* If an access failure was recorded, then attempt to locate an
3239 accessor function for the pertinent field, and if one is
3240 available, add a note and fix-it hint suggesting using it. */
3242 void
3243 access_failure_info::maybe_suggest_accessor (bool const_p) const
3245 tree accessor = get_any_accessor (const_p);
3246 if (accessor == NULL_TREE)
3247 return;
3248 rich_location richloc (line_table, input_location);
3249 add_fixit_hint (&richloc, accessor);
3250 inform (&richloc, "field %q#D can be accessed via %q#D",
3251 m_diag_decl, accessor);
3254 /* Subroutine of finish_class_member_access_expr.
3255 Issue an error about NAME not being a member of ACCESS_PATH (or
3256 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3257 names. */
3259 static void
3260 complain_about_unrecognized_member (tree access_path, tree name,
3261 tree object_type)
3263 /* Attempt to provide a hint about misspelled names. */
3264 tree guessed_id = lookup_member_fuzzy (access_path, name,
3265 /*want_type=*/false);
3266 if (guessed_id == NULL_TREE)
3268 /* No hint. */
3269 error ("%q#T has no member named %qE",
3270 TREE_CODE (access_path) == TREE_BINFO
3271 ? TREE_TYPE (access_path) : object_type, name);
3272 return;
3275 location_t bogus_component_loc = input_location;
3276 gcc_rich_location rich_loc (bogus_component_loc);
3278 /* Check that the guessed name is accessible along access_path. */
3279 access_failure_info afi;
3280 lookup_member (access_path, guessed_id, /*protect=*/1,
3281 /*want_type=*/false, /*complain=*/false,
3282 &afi);
3283 if (afi.was_inaccessible_p ())
3285 tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3286 if (accessor)
3288 /* The guessed name isn't directly accessible, but can be accessed
3289 via an accessor member function. */
3290 afi.add_fixit_hint (&rich_loc, accessor);
3291 error_at (&rich_loc,
3292 "%q#T has no member named %qE;"
3293 " did you mean %q#D? (accessible via %q#D)",
3294 TREE_CODE (access_path) == TREE_BINFO
3295 ? TREE_TYPE (access_path) : object_type,
3296 name, afi.get_diag_decl (), accessor);
3298 else
3300 /* The guessed name isn't directly accessible, and no accessor
3301 member function could be found. */
3302 error_at (&rich_loc,
3303 "%q#T has no member named %qE;"
3304 " did you mean %q#D? (not accessible from this context)",
3305 TREE_CODE (access_path) == TREE_BINFO
3306 ? TREE_TYPE (access_path) : object_type,
3307 name, afi.get_diag_decl ());
3308 complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3309 afi.get_diag_decl (), false, ak_none);
3312 else
3314 /* The guessed name is directly accessible; suggest it. */
3315 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3316 guessed_id);
3317 error_at (&rich_loc,
3318 "%q#T has no member named %qE;"
3319 " did you mean %qE?",
3320 TREE_CODE (access_path) == TREE_BINFO
3321 ? TREE_TYPE (access_path) : object_type,
3322 name, guessed_id);
3326 /* This function is called by the parser to process a class member
3327 access expression of the form OBJECT.NAME. NAME is a node used by
3328 the parser to represent a name; it is not yet a DECL. It may,
3329 however, be a BASELINK where the BASELINK_FUNCTIONS is a
3330 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3331 there is no reason to do the lookup twice, so the parser keeps the
3332 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3333 be a template via the use of the "A::template B" syntax. */
3335 tree
3336 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3337 tsubst_flags_t complain)
3339 tree expr;
3340 tree object_type;
3341 tree member;
3342 tree access_path = NULL_TREE;
3343 tree orig_object = object;
3344 tree orig_name = name;
3346 if (object == error_mark_node || name == error_mark_node)
3347 return error_mark_node;
3349 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3350 if (!objc_is_public (object, name))
3351 return error_mark_node;
3353 object_type = TREE_TYPE (object);
3355 if (processing_template_decl)
3357 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3358 type_dependent_object_expression_p (object)
3359 /* If NAME is "f<args>", where either 'f' or 'args' is
3360 dependent, then the expression is dependent. */
3361 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3362 && dependent_template_id_p (TREE_OPERAND (name, 0),
3363 TREE_OPERAND (name, 1)))
3364 /* If NAME is "T::X" where "T" is dependent, then the
3365 expression is dependent. */
3366 || (TREE_CODE (name) == SCOPE_REF
3367 && TYPE_P (TREE_OPERAND (name, 0))
3368 && dependent_scope_p (TREE_OPERAND (name, 0)))
3369 /* If NAME is operator T where "T" is dependent, we can't
3370 lookup until we instantiate the T. */
3371 || (TREE_CODE (name) == IDENTIFIER_NODE
3372 && IDENTIFIER_CONV_OP_P (name)
3373 && dependent_type_p (TREE_TYPE (name))))
3375 dependent:
3376 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3377 orig_object, orig_name, NULL_TREE);
3380 else if (c_dialect_objc ()
3381 && identifier_p (name)
3382 && (expr = objc_maybe_build_component_ref (object, name)))
3383 return expr;
3385 /* [expr.ref]
3387 The type of the first expression shall be "class object" (of a
3388 complete type). */
3389 if (!currently_open_class (object_type)
3390 && !complete_type_or_maybe_complain (object_type, object, complain))
3391 return error_mark_node;
3392 if (!CLASS_TYPE_P (object_type))
3394 if (complain & tf_error)
3396 if (INDIRECT_TYPE_P (object_type)
3397 && CLASS_TYPE_P (TREE_TYPE (object_type)))
3398 error ("request for member %qD in %qE, which is of pointer "
3399 "type %qT (maybe you meant to use %<->%> ?)",
3400 name, object.get_value (), object_type);
3401 else
3402 error ("request for member %qD in %qE, which is of non-class "
3403 "type %qT", name, object.get_value (), object_type);
3405 return error_mark_node;
3408 if (BASELINK_P (name))
3409 /* A member function that has already been looked up. */
3410 member = name;
3411 else
3413 bool is_template_id = false;
3414 tree template_args = NULL_TREE;
3415 tree scope = NULL_TREE;
3417 access_path = object_type;
3419 if (TREE_CODE (name) == SCOPE_REF)
3421 /* A qualified name. The qualifying class or namespace `S'
3422 has already been looked up; it is either a TYPE or a
3423 NAMESPACE_DECL. */
3424 scope = TREE_OPERAND (name, 0);
3425 name = TREE_OPERAND (name, 1);
3427 /* If SCOPE is a namespace, then the qualified name does not
3428 name a member of OBJECT_TYPE. */
3429 if (TREE_CODE (scope) == NAMESPACE_DECL)
3431 if (complain & tf_error)
3432 error ("%<%D::%D%> is not a member of %qT",
3433 scope, name, object_type);
3434 return error_mark_node;
3438 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3440 is_template_id = true;
3441 template_args = TREE_OPERAND (name, 1);
3442 name = TREE_OPERAND (name, 0);
3444 if (!identifier_p (name))
3445 name = OVL_NAME (name);
3448 if (scope)
3450 if (TREE_CODE (scope) == ENUMERAL_TYPE)
3452 gcc_assert (!is_template_id);
3453 /* Looking up a member enumerator (c++/56793). */
3454 if (!TYPE_CLASS_SCOPE_P (scope)
3455 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3457 if (complain & tf_error)
3458 error ("%<%D::%D%> is not a member of %qT",
3459 scope, name, object_type);
3460 return error_mark_node;
3462 tree val = lookup_enumerator (scope, name);
3463 if (!val)
3465 if (complain & tf_error)
3466 error ("%qD is not a member of %qD",
3467 name, scope);
3468 return error_mark_node;
3471 if (TREE_SIDE_EFFECTS (object))
3472 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3473 return val;
3476 gcc_assert (CLASS_TYPE_P (scope));
3477 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3479 if (constructor_name_p (name, scope))
3481 if (complain & tf_error)
3482 error ("cannot call constructor %<%T::%D%> directly",
3483 scope, name);
3484 return error_mark_node;
3487 /* NAME may refer to a static data member, in which case there is
3488 one copy of the data member that is shared by all the objects of
3489 the class. So NAME can be unambiguously referred to even if
3490 there are multiple indirect base classes containing NAME. */
3491 const base_access ba = [scope, name] ()
3493 if (identifier_p (name))
3495 tree m = lookup_member (scope, name, /*protect=*/0,
3496 /*want_type=*/false, tf_none);
3497 if (!m || shared_member_p (m))
3498 return ba_any;
3500 return ba_check;
3501 } ();
3503 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3504 access_path = lookup_base (object_type, scope, ba, NULL, complain);
3505 if (access_path == error_mark_node)
3506 return error_mark_node;
3507 if (!access_path)
3509 if (any_dependent_bases_p (object_type))
3510 goto dependent;
3511 if (complain & tf_error)
3512 error ("%qT is not a base of %qT", scope, object_type);
3513 return error_mark_node;
3517 if (TREE_CODE (name) == BIT_NOT_EXPR)
3519 if (dependent_type_p (object_type))
3520 /* The destructor isn't declared yet. */
3521 goto dependent;
3522 member = lookup_destructor (object, scope, name, complain);
3524 else
3526 /* Look up the member. */
3527 access_failure_info afi;
3528 if (processing_template_decl)
3529 /* Even though this class member access expression is at this
3530 point not dependent, the member itself may be dependent, and
3531 we must not potentially push a access check for a dependent
3532 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3533 ahead of time here; we're going to redo this member lookup at
3534 instantiation time anyway. */
3535 push_deferring_access_checks (dk_no_check);
3536 member = lookup_member (access_path, name, /*protect=*/1,
3537 /*want_type=*/false, complain,
3538 &afi);
3539 if (processing_template_decl)
3540 pop_deferring_access_checks ();
3541 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3542 if (member == NULL_TREE)
3544 if (dependent_type_p (object_type))
3545 /* Try again at instantiation time. */
3546 goto dependent;
3547 if (complain & tf_error)
3548 complain_about_unrecognized_member (access_path, name,
3549 object_type);
3550 return error_mark_node;
3552 if (member == error_mark_node)
3553 return error_mark_node;
3554 if (DECL_P (member)
3555 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3556 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3557 wrong, so don't use it. */
3558 goto dependent;
3559 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3560 goto dependent;
3563 if (is_template_id)
3565 tree templ = member;
3567 if (BASELINK_P (templ))
3568 member = lookup_template_function (templ, template_args);
3569 else if (variable_template_p (templ))
3570 member = (lookup_and_finish_template_variable
3571 (templ, template_args, complain));
3572 else
3574 if (complain & tf_error)
3575 error ("%qD is not a member template function", name);
3576 return error_mark_node;
3581 if (TREE_UNAVAILABLE (member))
3582 error_unavailable_use (member, NULL_TREE);
3583 else if (TREE_DEPRECATED (member))
3584 warn_deprecated_use (member, NULL_TREE);
3586 if (template_p)
3587 check_template_keyword (member);
3589 expr = build_class_member_access_expr (object, member, access_path,
3590 /*preserve_reference=*/false,
3591 complain);
3592 if (processing_template_decl && expr != error_mark_node)
3594 if (BASELINK_P (member))
3596 if (TREE_CODE (orig_name) == SCOPE_REF)
3597 BASELINK_QUALIFIED_P (member) = 1;
3598 orig_name = member;
3600 return build_min_non_dep (COMPONENT_REF, expr,
3601 orig_object, orig_name,
3602 NULL_TREE);
3605 return expr;
3608 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3609 type. */
3611 tree
3612 build_simple_component_ref (tree object, tree member)
3614 tree type = cp_build_qualified_type (TREE_TYPE (member),
3615 cp_type_quals (TREE_TYPE (object)));
3616 return build3_loc (input_location,
3617 COMPONENT_REF, type,
3618 object, member, NULL_TREE);
3621 /* Return an expression for the MEMBER_NAME field in the internal
3622 representation of PTRMEM, a pointer-to-member function. (Each
3623 pointer-to-member function type gets its own RECORD_TYPE so it is
3624 more convenient to access the fields by name than by FIELD_DECL.)
3625 This routine converts the NAME to a FIELD_DECL and then creates the
3626 node for the complete expression. */
3628 tree
3629 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3631 tree ptrmem_type;
3632 tree member;
3634 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3636 for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3637 if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3638 return e.value;
3639 gcc_unreachable ();
3642 /* This code is a stripped down version of
3643 build_class_member_access_expr. It does not work to use that
3644 routine directly because it expects the object to be of class
3645 type. */
3646 ptrmem_type = TREE_TYPE (ptrmem);
3647 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3648 for (member = TYPE_FIELDS (ptrmem_type); member;
3649 member = DECL_CHAIN (member))
3650 if (DECL_NAME (member) == member_name)
3651 break;
3652 return build_simple_component_ref (ptrmem, member);
3655 /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3656 and for any other relevant operator. */
3658 static tree
3659 op_unqualified_lookup (tree_code code, bool is_assign)
3661 tree lookups = NULL_TREE;
3663 if (cxx_dialect >= cxx20 && !is_assign)
3665 if (code == NE_EXPR)
3667 /* != can get rewritten in terms of ==. */
3668 tree fnname = ovl_op_identifier (false, EQ_EXPR);
3669 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3670 lookups = tree_cons (fnname, fns, lookups);
3672 else if (code == GT_EXPR || code == LE_EXPR
3673 || code == LT_EXPR || code == GE_EXPR)
3675 /* These can get rewritten in terms of <=>. */
3676 tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3677 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3678 lookups = tree_cons (fnname, fns, lookups);
3682 tree fnname = ovl_op_identifier (is_assign, code);
3683 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3684 lookups = tree_cons (fnname, fns, lookups);
3686 if (lookups)
3687 return lookups;
3688 else
3689 return build_tree_list (NULL_TREE, NULL_TREE);
3692 /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3693 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3694 name lookup for the given operator. */
3696 tree
3697 build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3699 if (lookups)
3700 /* We're partially instantiating a dependent operator expression, and
3701 LOOKUPS is the result of phase 1 name lookup that we performed
3702 earlier at template definition time, so just reuse the corresponding
3703 DEPENDENT_OPERATOR_TYPE. */
3704 return TREE_TYPE (lookups);
3706 /* Otherwise we're processing a dependent operator expression at template
3707 definition time, so perform phase 1 name lookup now. */
3708 lookups = op_unqualified_lookup (code, is_assign);
3710 tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3711 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3712 TREE_TYPE (lookups) = type;
3713 return type;
3716 /* Given an expression PTR for a pointer, return an expression
3717 for the value pointed to.
3718 ERRORSTRING is the name of the operator to appear in error messages.
3720 This function may need to overload OPERATOR_FNNAME.
3721 Must also handle REFERENCE_TYPEs for C++. */
3723 tree
3724 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3725 tree lookups, tsubst_flags_t complain)
3727 tree orig_expr = expr;
3728 tree rval;
3729 tree overload = NULL_TREE;
3731 if (processing_template_decl)
3733 /* Retain the type if we know the operand is a pointer. */
3734 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3736 if (expr == current_class_ptr
3737 || (TREE_CODE (expr) == NOP_EXPR
3738 && TREE_OPERAND (expr, 0) == current_class_ptr
3739 && (same_type_ignoring_top_level_qualifiers_p
3740 (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3741 return current_class_ref;
3742 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3744 if (type_dependent_expression_p (expr))
3746 expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3747 TREE_TYPE (expr)
3748 = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3749 return expr;
3753 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3754 NULL_TREE, NULL_TREE, lookups,
3755 &overload, complain);
3756 if (!rval)
3757 rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3759 if (processing_template_decl && rval != error_mark_node)
3761 if (overload != NULL_TREE)
3762 return (build_min_non_dep_op_overload
3763 (INDIRECT_REF, rval, overload, orig_expr));
3765 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3767 else
3768 return rval;
3771 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3772 types or expressions. */
3774 static bool
3775 cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3777 if (processing_template_decl)
3779 tree e = expr;
3780 STRIP_NOPS (e);
3781 if (dependent_type_p (type) || type_dependent_expression_p (e))
3782 return false;
3784 return strict_aliasing_warning (loc, type, expr);
3787 /* The implementation of the above, and of indirection implied by other
3788 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3790 static tree
3791 cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3792 tsubst_flags_t complain, bool do_fold)
3794 tree pointer, type;
3796 /* RO_NULL should only be used with the folding entry points below, not
3797 cp_build_indirect_ref. */
3798 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3800 if (ptr == current_class_ptr
3801 || (TREE_CODE (ptr) == NOP_EXPR
3802 && TREE_OPERAND (ptr, 0) == current_class_ptr
3803 && (same_type_ignoring_top_level_qualifiers_p
3804 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3805 return current_class_ref;
3807 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3808 ? ptr : decay_conversion (ptr, complain));
3809 if (pointer == error_mark_node)
3810 return error_mark_node;
3812 type = TREE_TYPE (pointer);
3814 if (INDIRECT_TYPE_P (type))
3816 /* [expr.unary.op]
3818 If the type of the expression is "pointer to T," the type
3819 of the result is "T." */
3820 tree t = TREE_TYPE (type);
3822 if ((CONVERT_EXPR_P (ptr)
3823 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3824 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3826 /* If a warning is issued, mark it to avoid duplicates from
3827 the backend. This only needs to be done at
3828 warn_strict_aliasing > 2. */
3829 if (warn_strict_aliasing > 2
3830 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3831 type, TREE_OPERAND (ptr, 0)))
3832 suppress_warning (ptr, OPT_Wstrict_aliasing);
3835 if (VOID_TYPE_P (t))
3837 /* A pointer to incomplete type (other than cv void) can be
3838 dereferenced [expr.unary.op]/1 */
3839 if (complain & tf_error)
3840 error_at (loc, "%qT is not a pointer-to-object type", type);
3841 return error_mark_node;
3843 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3844 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3845 /* The POINTER was something like `&x'. We simplify `*&x' to
3846 `x'. */
3847 return TREE_OPERAND (pointer, 0);
3848 else
3850 tree ref = build1 (INDIRECT_REF, t, pointer);
3852 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3853 so that we get the proper error message if the result is used
3854 to assign to. Also, &* is supposed to be a no-op. */
3855 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3856 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3857 TREE_SIDE_EFFECTS (ref)
3858 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3859 return ref;
3862 else if (!(complain & tf_error))
3863 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3865 /* `pointer' won't be an error_mark_node if we were given a
3866 pointer to member, so it's cool to check for this here. */
3867 else if (TYPE_PTRMEM_P (type))
3868 switch (errorstring)
3870 case RO_ARRAY_INDEXING:
3871 error_at (loc,
3872 "invalid use of array indexing on pointer to member");
3873 break;
3874 case RO_UNARY_STAR:
3875 error_at (loc, "invalid use of unary %<*%> on pointer to member");
3876 break;
3877 case RO_IMPLICIT_CONVERSION:
3878 error_at (loc, "invalid use of implicit conversion on pointer "
3879 "to member");
3880 break;
3881 case RO_ARROW_STAR:
3882 error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3883 "class, but is a pointer to member of type %qT", type);
3884 break;
3885 default:
3886 gcc_unreachable ();
3888 else if (pointer != error_mark_node)
3889 invalid_indirection_error (loc, type, errorstring);
3891 return error_mark_node;
3894 /* Entry point used by c-common, which expects folding. */
3896 tree
3897 build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3899 return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3900 tf_warning_or_error, true);
3903 /* Entry point used by internal indirection needs that don't correspond to any
3904 syntactic construct. */
3906 tree
3907 cp_build_fold_indirect_ref (tree pointer)
3909 return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3910 tf_warning_or_error, true);
3913 /* Entry point used by indirection needs that correspond to some syntactic
3914 construct. */
3916 tree
3917 cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3918 tsubst_flags_t complain)
3920 return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3923 /* This handles expressions of the form "a[i]", which denotes
3924 an array reference.
3926 This is logically equivalent in C to *(a+i), but we may do it differently.
3927 If A is a variable or a member, we generate a primitive ARRAY_REF.
3928 This avoids forcing the array out of registers, and can work on
3929 arrays that are not lvalues (for example, members of structures returned
3930 by functions).
3932 If INDEX is of some user-defined type, it must be converted to
3933 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3934 will inherit the type of the array, which will be some pointer type.
3936 LOC is the location to use in building the array reference. */
3938 tree
3939 cp_build_array_ref (location_t loc, tree array, tree idx,
3940 tsubst_flags_t complain)
3942 tree first = NULL_TREE;
3943 tree ret;
3945 if (idx == 0)
3947 if (complain & tf_error)
3948 error_at (loc, "subscript missing in array reference");
3949 return error_mark_node;
3952 if (TREE_TYPE (array) == error_mark_node
3953 || TREE_TYPE (idx) == error_mark_node)
3954 return error_mark_node;
3956 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3957 inside it. */
3958 switch (TREE_CODE (array))
3960 case COMPOUND_EXPR:
3962 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3963 complain);
3964 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3965 TREE_OPERAND (array, 0), value);
3966 SET_EXPR_LOCATION (ret, loc);
3967 return ret;
3970 case COND_EXPR:
3971 ret = build_conditional_expr
3972 (loc, TREE_OPERAND (array, 0),
3973 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3974 complain),
3975 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3976 complain),
3977 complain);
3978 protected_set_expr_location (ret, loc);
3979 return ret;
3981 default:
3982 break;
3985 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3987 /* 0[array] */
3988 if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
3990 std::swap (array, idx);
3991 if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
3992 idx = first = save_expr (idx);
3995 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3997 tree rval, type;
3999 warn_array_subscript_with_type_char (loc, idx);
4001 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4003 if (complain & tf_error)
4004 error_at (loc, "array subscript is not an integer");
4005 return error_mark_node;
4008 /* Apply integral promotions *after* noticing character types.
4009 (It is unclear why we do these promotions -- the standard
4010 does not say that we should. In fact, the natural thing would
4011 seem to be to convert IDX to ptrdiff_t; we're performing
4012 pointer arithmetic.) */
4013 idx = cp_perform_integral_promotions (idx, complain);
4015 idx = maybe_fold_non_dependent_expr (idx, complain);
4017 /* An array that is indexed by a non-constant
4018 cannot be stored in a register; we must be able to do
4019 address arithmetic on its address.
4020 Likewise an array of elements of variable size. */
4021 if (TREE_CODE (idx) != INTEGER_CST
4022 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4023 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4024 != INTEGER_CST)))
4026 if (!cxx_mark_addressable (array, true))
4027 return error_mark_node;
4030 /* An array that is indexed by a constant value which is not within
4031 the array bounds cannot be stored in a register either; because we
4032 would get a crash in store_bit_field/extract_bit_field when trying
4033 to access a non-existent part of the register. */
4034 if (TREE_CODE (idx) == INTEGER_CST
4035 && TYPE_DOMAIN (TREE_TYPE (array))
4036 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4038 if (!cxx_mark_addressable (array))
4039 return error_mark_node;
4042 /* Note in C++ it is valid to subscript a `register' array, since
4043 it is valid to take the address of something with that
4044 storage specification. */
4045 if (extra_warnings)
4047 tree foo = array;
4048 while (TREE_CODE (foo) == COMPONENT_REF)
4049 foo = TREE_OPERAND (foo, 0);
4050 if (VAR_P (foo) && DECL_REGISTER (foo)
4051 && (complain & tf_warning))
4052 warning_at (loc, OPT_Wextra,
4053 "subscripting array declared %<register%>");
4056 type = TREE_TYPE (TREE_TYPE (array));
4057 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4058 /* Array ref is const/volatile if the array elements are
4059 or if the array is.. */
4060 TREE_READONLY (rval)
4061 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4062 TREE_SIDE_EFFECTS (rval)
4063 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4064 TREE_THIS_VOLATILE (rval)
4065 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4066 ret = require_complete_type (rval, complain);
4067 protected_set_expr_location (ret, loc);
4068 if (non_lvalue)
4069 ret = non_lvalue_loc (loc, ret);
4070 if (first)
4071 ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4072 return ret;
4076 tree ar = cp_default_conversion (array, complain);
4077 tree ind = cp_default_conversion (idx, complain);
4079 if (!first && flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
4080 ar = first = save_expr (ar);
4082 /* Put the integer in IND to simplify error checking. */
4083 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4084 std::swap (ar, ind);
4086 if (ar == error_mark_node || ind == error_mark_node)
4087 return error_mark_node;
4089 if (!TYPE_PTR_P (TREE_TYPE (ar)))
4091 if (complain & tf_error)
4092 error_at (loc, "subscripted value is neither array nor pointer");
4093 return error_mark_node;
4095 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4097 if (complain & tf_error)
4098 error_at (loc, "array subscript is not an integer");
4099 return error_mark_node;
4102 warn_array_subscript_with_type_char (loc, idx);
4104 ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4105 if (first)
4106 ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4107 ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4108 protected_set_expr_location (ret, loc);
4109 if (non_lvalue)
4110 ret = non_lvalue_loc (loc, ret);
4111 return ret;
4115 /* Entry point for Obj-C++. */
4117 tree
4118 build_array_ref (location_t loc, tree array, tree idx)
4120 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4123 /* Resolve a pointer to member function. INSTANCE is the object
4124 instance to use, if the member points to a virtual member.
4126 This used to avoid checking for virtual functions if basetype
4127 has no virtual functions, according to an earlier ANSI draft.
4128 With the final ISO C++ rules, such an optimization is
4129 incorrect: A pointer to a derived member can be static_cast
4130 to pointer-to-base-member, as long as the dynamic object
4131 later has the right member. So now we only do this optimization
4132 when we know the dynamic type of the object. */
4134 tree
4135 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4136 tsubst_flags_t complain)
4138 if (TREE_CODE (function) == OFFSET_REF)
4139 function = TREE_OPERAND (function, 1);
4141 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4143 tree idx, delta, e1, e2, e3, vtbl;
4144 bool nonvirtual;
4145 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4146 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4148 tree instance_ptr = *instance_ptrptr;
4149 tree instance_save_expr = 0;
4150 if (instance_ptr == error_mark_node)
4152 if (TREE_CODE (function) == PTRMEM_CST)
4154 /* Extracting the function address from a pmf is only
4155 allowed with -Wno-pmf-conversions. It only works for
4156 pmf constants. */
4157 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4158 e1 = convert (fntype, e1);
4159 return e1;
4161 else
4163 if (complain & tf_error)
4164 error ("object missing in use of %qE", function);
4165 return error_mark_node;
4169 /* True if we know that the dynamic type of the object doesn't have
4170 virtual functions, so we can assume the PFN field is a pointer. */
4171 nonvirtual = (COMPLETE_TYPE_P (basetype)
4172 && !TYPE_POLYMORPHIC_P (basetype)
4173 && resolves_to_fixed_type_p (instance_ptr, 0));
4175 /* If we don't really have an object (i.e. in an ill-formed
4176 conversion from PMF to pointer), we can't resolve virtual
4177 functions anyway. */
4178 if (!nonvirtual && is_dummy_object (instance_ptr))
4179 nonvirtual = true;
4181 if (TREE_SIDE_EFFECTS (instance_ptr))
4182 instance_ptr = instance_save_expr = save_expr (instance_ptr);
4184 if (TREE_SIDE_EFFECTS (function))
4185 function = save_expr (function);
4187 /* Start by extracting all the information from the PMF itself. */
4188 e3 = pfn_from_ptrmemfunc (function);
4189 delta = delta_from_ptrmemfunc (function);
4190 idx = build1 (NOP_EXPR, vtable_index_type, e3);
4191 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4193 int flag_sanitize_save;
4194 case ptrmemfunc_vbit_in_pfn:
4195 e1 = cp_build_binary_op (input_location,
4196 BIT_AND_EXPR, idx, integer_one_node,
4197 complain);
4198 idx = cp_build_binary_op (input_location,
4199 MINUS_EXPR, idx, integer_one_node,
4200 complain);
4201 if (idx == error_mark_node)
4202 return error_mark_node;
4203 break;
4205 case ptrmemfunc_vbit_in_delta:
4206 e1 = cp_build_binary_op (input_location,
4207 BIT_AND_EXPR, delta, integer_one_node,
4208 complain);
4209 /* Don't instrument the RSHIFT_EXPR we're about to create because
4210 we're going to use DELTA number of times, and that wouldn't play
4211 well with SAVE_EXPRs therein. */
4212 flag_sanitize_save = flag_sanitize;
4213 flag_sanitize = 0;
4214 delta = cp_build_binary_op (input_location,
4215 RSHIFT_EXPR, delta, integer_one_node,
4216 complain);
4217 flag_sanitize = flag_sanitize_save;
4218 if (delta == error_mark_node)
4219 return error_mark_node;
4220 break;
4222 default:
4223 gcc_unreachable ();
4226 if (e1 == error_mark_node)
4227 return error_mark_node;
4229 /* Convert down to the right base before using the instance. A
4230 special case is that in a pointer to member of class C, C may
4231 be incomplete. In that case, the function will of course be
4232 a member of C, and no conversion is required. In fact,
4233 lookup_base will fail in that case, because incomplete
4234 classes do not have BINFOs. */
4235 if (!same_type_ignoring_top_level_qualifiers_p
4236 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4238 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4239 basetype, ba_check, NULL, complain);
4240 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4241 1, complain);
4242 if (instance_ptr == error_mark_node)
4243 return error_mark_node;
4245 /* ...and then the delta in the PMF. */
4246 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4248 /* Hand back the adjusted 'this' argument to our caller. */
4249 *instance_ptrptr = instance_ptr;
4251 if (nonvirtual)
4252 /* Now just return the pointer. */
4253 return e3;
4255 /* Next extract the vtable pointer from the object. */
4256 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4257 instance_ptr);
4258 vtbl = cp_build_fold_indirect_ref (vtbl);
4259 if (vtbl == error_mark_node)
4260 return error_mark_node;
4262 /* Finally, extract the function pointer from the vtable. */
4263 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4264 e2 = cp_build_fold_indirect_ref (e2);
4265 if (e2 == error_mark_node)
4266 return error_mark_node;
4267 TREE_CONSTANT (e2) = 1;
4269 /* When using function descriptors, the address of the
4270 vtable entry is treated as a function pointer. */
4271 if (TARGET_VTABLE_USES_DESCRIPTORS)
4272 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4273 cp_build_addr_expr (e2, complain));
4275 e2 = fold_convert (TREE_TYPE (e3), e2);
4276 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4277 if (e1 == error_mark_node)
4278 return error_mark_node;
4280 /* Make sure this doesn't get evaluated first inside one of the
4281 branches of the COND_EXPR. */
4282 if (instance_save_expr)
4283 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4284 instance_save_expr, e1);
4286 function = e1;
4288 return function;
4291 /* Used by the C-common bits. */
4292 tree
4293 build_function_call (location_t /*loc*/,
4294 tree function, tree params)
4296 return cp_build_function_call (function, params, tf_warning_or_error);
4299 /* Used by the C-common bits. */
4300 tree
4301 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4302 tree function, vec<tree, va_gc> *params,
4303 vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4305 vec<tree, va_gc> *orig_params = params;
4306 tree ret = cp_build_function_call_vec (function, &params,
4307 tf_warning_or_error, orig_function);
4309 /* cp_build_function_call_vec can reallocate PARAMS by adding
4310 default arguments. That should never happen here. Verify
4311 that. */
4312 gcc_assert (params == orig_params);
4314 return ret;
4317 /* Build a function call using a tree list of arguments. */
4319 static tree
4320 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4322 tree ret;
4324 releasing_vec vec;
4325 for (; params != NULL_TREE; params = TREE_CHAIN (params))
4326 vec_safe_push (vec, TREE_VALUE (params));
4327 ret = cp_build_function_call_vec (function, &vec, complain);
4328 return ret;
4331 /* Build a function call using varargs. */
4333 tree
4334 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4336 va_list args;
4337 tree ret, t;
4339 releasing_vec vec;
4340 va_start (args, complain);
4341 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4342 vec_safe_push (vec, t);
4343 va_end (args);
4344 ret = cp_build_function_call_vec (function, &vec, complain);
4345 return ret;
4348 /* Build a function call using a vector of arguments.
4349 If FUNCTION is the result of resolving an overloaded target built-in,
4350 ORIG_FNDECL is the original function decl, otherwise it is null.
4351 PARAMS may be NULL if there are no parameters. This changes the
4352 contents of PARAMS. */
4354 tree
4355 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4356 tsubst_flags_t complain, tree orig_fndecl)
4358 tree fntype, fndecl;
4359 int is_method;
4360 tree original = function;
4361 int nargs;
4362 tree *argarray;
4363 tree parm_types;
4364 vec<tree, va_gc> *allocated = NULL;
4365 tree ret;
4367 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4368 expressions, like those used for ObjC messenger dispatches. */
4369 if (params != NULL && !vec_safe_is_empty (*params))
4370 function = objc_rewrite_function_call (function, (**params)[0]);
4372 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4373 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4374 if (TREE_CODE (function) == NOP_EXPR
4375 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4376 function = TREE_OPERAND (function, 0);
4378 if (TREE_CODE (function) == FUNCTION_DECL)
4380 if (!mark_used (function, complain))
4381 return error_mark_node;
4382 fndecl = function;
4384 /* Convert anything with function type to a pointer-to-function. */
4385 if (DECL_MAIN_P (function))
4387 if (complain & tf_error)
4388 pedwarn (input_location, OPT_Wpedantic,
4389 "ISO C++ forbids calling %<::main%> from within program");
4390 else
4391 return error_mark_node;
4393 function = build_addr_func (function, complain);
4395 else
4397 fndecl = NULL_TREE;
4399 function = build_addr_func (function, complain);
4402 if (function == error_mark_node)
4403 return error_mark_node;
4405 fntype = TREE_TYPE (function);
4407 if (TYPE_PTRMEMFUNC_P (fntype))
4409 if (complain & tf_error)
4410 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4411 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4412 original, original);
4413 return error_mark_node;
4416 is_method = (TYPE_PTR_P (fntype)
4417 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4419 if (!(TYPE_PTRFN_P (fntype)
4420 || is_method
4421 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4423 if (complain & tf_error)
4425 if (!flag_diagnostics_show_caret)
4426 error_at (input_location,
4427 "%qE cannot be used as a function", original);
4428 else if (DECL_P (original))
4429 error_at (input_location,
4430 "%qD cannot be used as a function", original);
4431 else
4432 error_at (input_location,
4433 "expression cannot be used as a function");
4436 return error_mark_node;
4439 /* fntype now gets the type of function pointed to. */
4440 fntype = TREE_TYPE (fntype);
4441 parm_types = TYPE_ARG_TYPES (fntype);
4443 if (params == NULL)
4445 allocated = make_tree_vector ();
4446 params = &allocated;
4449 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4450 complain);
4451 if (nargs < 0)
4452 return error_mark_node;
4454 argarray = (*params)->address ();
4456 /* Check for errors in format strings and inappropriately
4457 null parameters. */
4458 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
4459 nargs, argarray, NULL);
4461 ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4463 if (warned_p)
4465 tree c = extract_call_expr (ret);
4466 if (TREE_CODE (c) == CALL_EXPR)
4467 suppress_warning (c, OPT_Wnonnull);
4470 if (allocated != NULL)
4471 release_tree_vector (allocated);
4473 return ret;
4476 /* Subroutine of convert_arguments.
4477 Print an error message about a wrong number of arguments. */
4479 static void
4480 error_args_num (location_t loc, tree fndecl, bool too_many_p)
4482 if (fndecl)
4484 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4486 if (DECL_NAME (fndecl) == NULL_TREE
4487 || (DECL_NAME (fndecl)
4488 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4489 error_at (loc,
4490 too_many_p
4491 ? G_("too many arguments to constructor %q#D")
4492 : G_("too few arguments to constructor %q#D"),
4493 fndecl);
4494 else
4495 error_at (loc,
4496 too_many_p
4497 ? G_("too many arguments to member function %q#D")
4498 : G_("too few arguments to member function %q#D"),
4499 fndecl);
4501 else
4502 error_at (loc,
4503 too_many_p
4504 ? G_("too many arguments to function %q#D")
4505 : G_("too few arguments to function %q#D"),
4506 fndecl);
4507 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4508 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4510 else
4512 if (c_dialect_objc () && objc_message_selector ())
4513 error_at (loc,
4514 too_many_p
4515 ? G_("too many arguments to method %q#D")
4516 : G_("too few arguments to method %q#D"),
4517 objc_message_selector ());
4518 else
4519 error_at (loc, too_many_p ? G_("too many arguments to function")
4520 : G_("too few arguments to function"));
4524 /* Convert the actual parameter expressions in the list VALUES to the
4525 types in the list TYPELIST. The converted expressions are stored
4526 back in the VALUES vector.
4527 If parmdecls is exhausted, or when an element has NULL as its type,
4528 perform the default conversions.
4530 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4532 This is also where warnings about wrong number of args are generated.
4534 Returns the actual number of arguments processed (which might be less
4535 than the length of the vector), or -1 on error.
4537 In C++, unspecified trailing parameters can be filled in with their
4538 default arguments, if such were specified. Do so here. */
4540 static int
4541 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4542 int flags, tsubst_flags_t complain)
4544 tree typetail;
4545 unsigned int i;
4547 /* Argument passing is always copy-initialization. */
4548 flags |= LOOKUP_ONLYCONVERTING;
4550 for (i = 0, typetail = typelist;
4551 i < vec_safe_length (*values);
4552 i++)
4554 tree type = typetail ? TREE_VALUE (typetail) : 0;
4555 tree val = (**values)[i];
4557 if (val == error_mark_node || type == error_mark_node)
4558 return -1;
4560 if (type == void_type_node)
4562 if (complain & tf_error)
4564 error_args_num (input_location, fndecl, /*too_many_p=*/true);
4565 return i;
4567 else
4568 return -1;
4571 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4572 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4573 if (TREE_CODE (val) == NOP_EXPR
4574 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4575 && (type == 0 || !TYPE_REF_P (type)))
4576 val = TREE_OPERAND (val, 0);
4578 if (type == 0 || !TYPE_REF_P (type))
4580 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4581 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4582 val = decay_conversion (val, complain);
4585 if (val == error_mark_node)
4586 return -1;
4588 if (type != 0)
4590 /* Formal parm type is specified by a function prototype. */
4591 tree parmval;
4593 if (!COMPLETE_TYPE_P (complete_type (type)))
4595 if (complain & tf_error)
4597 location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4598 if (fndecl)
4600 auto_diagnostic_group d;
4601 error_at (loc,
4602 "parameter %P of %qD has incomplete type %qT",
4603 i, fndecl, type);
4604 inform (get_fndecl_argument_location (fndecl, i),
4605 " declared here");
4607 else
4608 error_at (loc, "parameter %P has incomplete type %qT", i,
4609 type);
4611 parmval = error_mark_node;
4613 else
4615 parmval = convert_for_initialization
4616 (NULL_TREE, type, val, flags,
4617 ICR_ARGPASS, fndecl, i, complain);
4618 parmval = convert_for_arg_passing (type, parmval, complain);
4621 if (parmval == error_mark_node)
4622 return -1;
4624 (**values)[i] = parmval;
4626 else
4628 int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4629 if (magic)
4631 /* Don't truncate excess precision to the semantic type. */
4632 if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4633 val = TREE_OPERAND (val, 0);
4634 /* Don't do ellipsis conversion for __built_in_constant_p
4635 as this will result in spurious errors for non-trivial
4636 types. */
4637 val = require_complete_type (val, complain);
4639 else
4640 val = convert_arg_to_ellipsis (val, complain);
4642 (**values)[i] = val;
4645 if (typetail)
4646 typetail = TREE_CHAIN (typetail);
4649 if (typetail != 0 && typetail != void_list_node)
4651 /* See if there are default arguments that can be used. Because
4652 we hold default arguments in the FUNCTION_TYPE (which is so
4653 wrong), we can see default parameters here from deduced
4654 contexts (and via typeof) for indirect function calls.
4655 Fortunately we know whether we have a function decl to
4656 provide default arguments in a language conformant
4657 manner. */
4658 if (fndecl && TREE_PURPOSE (typetail)
4659 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4661 for (; typetail != void_list_node; ++i)
4663 /* After DR777, with explicit template args we can end up with a
4664 default argument followed by no default argument. */
4665 if (!TREE_PURPOSE (typetail))
4666 break;
4667 tree parmval
4668 = convert_default_arg (TREE_VALUE (typetail),
4669 TREE_PURPOSE (typetail),
4670 fndecl, i, complain);
4672 if (parmval == error_mark_node)
4673 return -1;
4675 vec_safe_push (*values, parmval);
4676 typetail = TREE_CHAIN (typetail);
4677 /* ends with `...'. */
4678 if (typetail == NULL_TREE)
4679 break;
4683 if (typetail && typetail != void_list_node)
4685 if (complain & tf_error)
4686 error_args_num (input_location, fndecl, /*too_many_p=*/false);
4687 return -1;
4691 return (int) i;
4694 /* Build a binary-operation expression, after performing default
4695 conversions on the operands. CODE is the kind of expression to
4696 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4697 are the tree codes which correspond to ARG1 and ARG2 when issuing
4698 warnings about possibly misplaced parentheses. They may differ
4699 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4700 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4701 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4702 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4703 ARG2_CODE as ERROR_MARK. */
4705 tree
4706 build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4707 enum tree_code arg1_code, tree arg2,
4708 enum tree_code arg2_code, tree lookups,
4709 tree *overload_p, tsubst_flags_t complain)
4711 tree orig_arg1;
4712 tree orig_arg2;
4713 tree expr;
4714 tree overload = NULL_TREE;
4716 orig_arg1 = arg1;
4717 orig_arg2 = arg2;
4719 if (processing_template_decl)
4721 if (type_dependent_expression_p (arg1)
4722 || type_dependent_expression_p (arg2))
4724 expr = build_min_nt_loc (loc, code, arg1, arg2);
4725 TREE_TYPE (expr)
4726 = build_dependent_operator_type (lookups, code, false);
4727 return expr;
4731 if (code == DOTSTAR_EXPR)
4732 expr = build_m_component_ref (arg1, arg2, complain);
4733 else
4734 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4735 lookups, &overload, complain);
4737 if (overload_p != NULL)
4738 *overload_p = overload;
4740 /* Check for cases such as x+y<<z which users are likely to
4741 misinterpret. But don't warn about obj << x + y, since that is a
4742 common idiom for I/O. */
4743 if (warn_parentheses
4744 && (complain & tf_warning)
4745 && !processing_template_decl
4746 && !error_operand_p (arg1)
4747 && !error_operand_p (arg2)
4748 && (code != LSHIFT_EXPR
4749 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4750 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4751 arg2_code, orig_arg2);
4753 if (processing_template_decl && expr != error_mark_node)
4755 if (overload != NULL_TREE)
4756 return (build_min_non_dep_op_overload
4757 (code, expr, overload, orig_arg1, orig_arg2));
4759 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4762 return expr;
4765 /* Build and return an ARRAY_REF expression. */
4767 tree
4768 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4769 tsubst_flags_t complain)
4771 tree orig_arg1 = arg1;
4772 tree orig_arg2 = arg2;
4773 tree expr;
4774 tree overload = NULL_TREE;
4776 if (processing_template_decl)
4778 if (type_dependent_expression_p (arg1)
4779 || type_dependent_expression_p (arg2))
4780 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4781 NULL_TREE, NULL_TREE);
4784 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4785 NULL_TREE, NULL_TREE, &overload, complain);
4787 if (processing_template_decl && expr != error_mark_node)
4789 if (overload != NULL_TREE)
4790 return (build_min_non_dep_op_overload
4791 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4793 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4794 NULL_TREE, NULL_TREE);
4796 return expr;
4799 /* Build an OpenMP array section reference, creating an exact type for the
4800 resulting expression based on the element type and bounds if possible. If
4801 we have variable bounds, create an incomplete array type for the result
4802 instead. */
4804 tree
4805 build_omp_array_section (location_t loc, tree array_expr, tree index,
4806 tree length)
4808 tree type = TREE_TYPE (array_expr);
4809 gcc_assert (type);
4810 type = non_reference (type);
4812 tree sectype, eltype = TREE_TYPE (type);
4814 /* It's not an array or pointer type. Just reuse the type of the
4815 original expression as the type of the array section (an error will be
4816 raised anyway, later). */
4817 if (eltype == NULL_TREE)
4818 sectype = TREE_TYPE (array_expr);
4819 else
4821 tree idxtype = NULL_TREE;
4823 /* If we know the integer bounds, create an index type with exact
4824 low/high (or zero/length) bounds. Otherwise, create an incomplete
4825 array type. (This mostly only affects diagnostics.) */
4826 if (index != NULL_TREE
4827 && length != NULL_TREE
4828 && TREE_CODE (index) == INTEGER_CST
4829 && TREE_CODE (length) == INTEGER_CST)
4831 tree low = fold_convert (sizetype, index);
4832 tree high = fold_convert (sizetype, length);
4833 high = size_binop (PLUS_EXPR, low, high);
4834 high = size_binop (MINUS_EXPR, high, size_one_node);
4835 idxtype = build_range_type (sizetype, low, high);
4837 else if ((index == NULL_TREE || integer_zerop (index))
4838 && length != NULL_TREE
4839 && TREE_CODE (length) == INTEGER_CST)
4840 idxtype = build_index_type (length);
4842 sectype = build_array_type (eltype, idxtype);
4845 return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
4846 length);
4849 /* Return whether OP is an expression of enum type cast to integer
4850 type. In C++ even unsigned enum types are cast to signed integer
4851 types. We do not want to issue warnings about comparisons between
4852 signed and unsigned types when one of the types is an enum type.
4853 Those warnings are always false positives in practice. */
4855 static bool
4856 enum_cast_to_int (tree op)
4858 if (CONVERT_EXPR_P (op)
4859 && TREE_TYPE (op) == integer_type_node
4860 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4861 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4862 return true;
4864 /* The cast may have been pushed into a COND_EXPR. */
4865 if (TREE_CODE (op) == COND_EXPR)
4866 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4867 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4869 return false;
4872 /* For the c-common bits. */
4873 tree
4874 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4875 bool /*convert_p*/)
4877 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4880 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4881 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4883 static tree
4884 build_vec_cmp (tree_code code, tree type,
4885 tree arg0, tree arg1)
4887 tree zero_vec = build_zero_cst (type);
4888 tree minus_one_vec = build_minus_one_cst (type);
4889 tree cmp_type = truth_type_for (TREE_TYPE (arg0));
4890 tree cmp = build2 (code, cmp_type, arg0, arg1);
4891 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4894 /* Possibly warn about an address never being NULL. */
4896 static void
4897 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4899 /* Prevent warnings issued for macro expansion. */
4900 if (!warn_address
4901 || (complain & tf_warning) == 0
4902 || c_inhibit_evaluation_warnings != 0
4903 || from_macro_expansion_at (location)
4904 || warning_suppressed_p (op, OPT_Waddress))
4905 return;
4907 tree cop = fold_for_warn (op);
4909 if (TREE_CODE (cop) == NON_LVALUE_EXPR)
4910 /* Unwrap the expression for C++ 98. */
4911 cop = TREE_OPERAND (cop, 0);
4913 if (TREE_CODE (cop) == PTRMEM_CST)
4915 /* The address of a nonstatic data member is never null. */
4916 warning_at (location, OPT_Waddress,
4917 "the address %qE will never be NULL",
4918 cop);
4919 return;
4922 if (TREE_CODE (cop) == NOP_EXPR)
4924 /* Allow casts to intptr_t to suppress the warning. */
4925 tree type = TREE_TYPE (cop);
4926 if (TREE_CODE (type) == INTEGER_TYPE)
4927 return;
4929 STRIP_NOPS (cop);
4932 bool warned = false;
4933 if (TREE_CODE (cop) == ADDR_EXPR)
4935 cop = TREE_OPERAND (cop, 0);
4937 /* Set to true in the loop below if OP dereferences its operand.
4938 In such a case the ultimate target need not be a decl for
4939 the null [in]equality test to be necessarily constant. */
4940 bool deref = false;
4942 /* Get the outermost array or object, or member. */
4943 while (handled_component_p (cop))
4945 if (TREE_CODE (cop) == COMPONENT_REF)
4947 /* Get the member (its address is never null). */
4948 cop = TREE_OPERAND (cop, 1);
4949 break;
4952 /* Get the outer array/object to refer to in the warning. */
4953 cop = TREE_OPERAND (cop, 0);
4954 deref = true;
4957 if ((!deref && !decl_with_nonnull_addr_p (cop))
4958 || from_macro_expansion_at (location)
4959 || warning_suppressed_p (cop, OPT_Waddress))
4960 return;
4962 warned = warning_at (location, OPT_Waddress,
4963 "the address of %qD will never be NULL", cop);
4964 op = cop;
4966 else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
4968 /* Adding zero to the null pointer is well-defined in C++. When
4969 the offset is unknown (i.e., not a constant) warn anyway since
4970 it's less likely that the pointer operand is null than not. */
4971 tree off = TREE_OPERAND (cop, 1);
4972 if (!integer_zerop (off)
4973 && !warning_suppressed_p (cop, OPT_Waddress))
4975 tree base = TREE_OPERAND (cop, 0);
4976 STRIP_NOPS (base);
4977 if (TYPE_REF_P (TREE_TYPE (base)))
4978 warning_at (location, OPT_Waddress, "the compiler can assume that "
4979 "the address of %qE will never be NULL", base);
4980 else
4981 warning_at (location, OPT_Waddress, "comparing the result of "
4982 "pointer addition %qE and NULL", cop);
4984 return;
4986 else if (CONVERT_EXPR_P (op)
4987 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4989 STRIP_NOPS (op);
4991 if (TREE_CODE (op) == COMPONENT_REF)
4992 op = TREE_OPERAND (op, 1);
4994 if (DECL_P (op))
4995 warned = warning_at (location, OPT_Waddress,
4996 "the compiler can assume that the address of "
4997 "%qD will never be NULL", op);
5000 if (warned && DECL_P (op))
5001 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5004 /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5005 the other operand is of a different enumeration type or a floating-point
5006 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5007 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5008 and LOC is the location for the whole binary expression.
5009 For C++26 this is ill-formed rather than deprecated.
5010 Return true for SFINAE errors.
5011 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5013 static bool
5014 do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5015 tree type1, tsubst_flags_t complain)
5017 if (TREE_CODE (type0) == ENUMERAL_TYPE
5018 && TREE_CODE (type1) == ENUMERAL_TYPE
5019 && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5021 if (cxx_dialect >= cxx26)
5023 if ((complain & tf_warning_or_error) == 0)
5024 return true;
5026 else if ((complain & tf_warning) == 0)
5027 return false;
5028 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5029 Otherwise, warn if -Wenum-conversion is on. */
5030 enum opt_code opt;
5031 if (warn_deprecated_enum_enum_conv)
5032 opt = OPT_Wdeprecated_enum_enum_conversion;
5033 else if (warn_enum_conversion)
5034 opt = OPT_Wenum_conversion;
5035 else
5036 return false;
5038 switch (code)
5040 case GT_EXPR:
5041 case LT_EXPR:
5042 case GE_EXPR:
5043 case LE_EXPR:
5044 case EQ_EXPR:
5045 case NE_EXPR:
5046 /* Comparisons are handled by -Wenum-compare. */
5047 return false;
5048 case SPACESHIP_EXPR:
5049 /* This is invalid, don't warn. */
5050 return false;
5051 case BIT_AND_EXPR:
5052 case BIT_IOR_EXPR:
5053 case BIT_XOR_EXPR:
5054 if (cxx_dialect >= cxx26)
5055 pedwarn (loc, opt, "bitwise operation between different "
5056 "enumeration types %qT and %qT", type0, type1);
5057 else
5058 warning_at (loc, opt, "bitwise operation between different "
5059 "enumeration types %qT and %qT is deprecated",
5060 type0, type1);
5061 return false;
5062 default:
5063 if (cxx_dialect >= cxx26)
5064 pedwarn (loc, opt, "arithmetic between different enumeration "
5065 "types %qT and %qT", type0, type1);
5066 else
5067 warning_at (loc, opt, "arithmetic between different enumeration "
5068 "types %qT and %qT is deprecated", type0, type1);
5069 return false;
5072 else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5073 && SCALAR_FLOAT_TYPE_P (type1))
5074 || (SCALAR_FLOAT_TYPE_P (type0)
5075 && TREE_CODE (type1) == ENUMERAL_TYPE))
5077 if (cxx_dialect >= cxx26)
5079 if ((complain & tf_warning_or_error) == 0)
5080 return true;
5082 else if ((complain & tf_warning) == 0)
5083 return false;
5084 const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5085 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5086 Otherwise, warn if -Wenum-conversion is on. */
5087 enum opt_code opt;
5088 if (warn_deprecated_enum_float_conv)
5089 opt = OPT_Wdeprecated_enum_float_conversion;
5090 else if (warn_enum_conversion)
5091 opt = OPT_Wenum_conversion;
5092 else
5093 return false;
5095 switch (code)
5097 case GT_EXPR:
5098 case LT_EXPR:
5099 case GE_EXPR:
5100 case LE_EXPR:
5101 case EQ_EXPR:
5102 case NE_EXPR:
5103 if (enum_first_p && cxx_dialect >= cxx26)
5104 pedwarn (loc, opt, "comparison of enumeration type %qT with "
5105 "floating-point type %qT", type0, type1);
5106 else if (cxx_dialect >= cxx26)
5107 pedwarn (loc, opt, "comparison of floating-point type %qT "
5108 "with enumeration type %qT", type0, type1);
5109 else if (enum_first_p)
5110 warning_at (loc, opt, "comparison of enumeration type %qT with "
5111 "floating-point type %qT is deprecated",
5112 type0, type1);
5113 else
5114 warning_at (loc, opt, "comparison of floating-point type %qT "
5115 "with enumeration type %qT is deprecated",
5116 type0, type1);
5117 return false;
5118 case SPACESHIP_EXPR:
5119 /* This is invalid, don't warn. */
5120 return false;
5121 default:
5122 if (enum_first_p && cxx_dialect >= cxx26)
5123 pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5124 "and floating-point type %qT", type0, type1);
5125 else if (cxx_dialect >= cxx26)
5126 pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5127 "and enumeration type %qT", type0, type1);
5128 else if (enum_first_p)
5129 warning_at (loc, opt, "arithmetic between enumeration type %qT "
5130 "and floating-point type %qT is deprecated",
5131 type0, type1);
5132 else
5133 warning_at (loc, opt, "arithmetic between floating-point type %qT "
5134 "and enumeration type %qT is deprecated",
5135 type0, type1);
5136 return false;
5139 return false;
5142 /* Build a binary-operation expression without default conversions.
5143 CODE is the kind of expression to build.
5144 LOCATION is the location_t of the operator in the source code.
5145 This function differs from `build' in several ways:
5146 the data type of the result is computed and recorded in it,
5147 warnings are generated if arg data types are invalid,
5148 special handling for addition and subtraction of pointers is known,
5149 and some optimization is done (operations on narrow ints
5150 are done in the narrower type when that gives the same result).
5151 Constant folding is also done before the result is returned.
5153 Note that the operands will never have enumeral types
5154 because either they have just had the default conversions performed
5155 or they have both just been converted to some other type in which
5156 the arithmetic is to be done.
5158 C++: must do special pointer arithmetic when implementing
5159 multiple inheritance, and deal with pointer to member functions. */
5161 tree
5162 cp_build_binary_op (const op_location_t &location,
5163 enum tree_code code, tree orig_op0, tree orig_op1,
5164 tsubst_flags_t complain)
5166 tree op0, op1;
5167 enum tree_code code0, code1;
5168 tree type0, type1, orig_type0, orig_type1;
5169 const char *invalid_op_diag;
5171 /* Expression code to give to the expression when it is built.
5172 Normally this is CODE, which is what the caller asked for,
5173 but in some special cases we change it. */
5174 enum tree_code resultcode = code;
5176 /* Data type in which the computation is to be performed.
5177 In the simplest cases this is the common type of the arguments. */
5178 tree result_type = NULL_TREE;
5180 /* When the computation is in excess precision, the type of the
5181 final EXCESS_PRECISION_EXPR. */
5182 tree semantic_result_type = NULL;
5184 /* Nonzero means operands have already been type-converted
5185 in whatever way is necessary.
5186 Zero means they need to be converted to RESULT_TYPE. */
5187 int converted = 0;
5189 /* Nonzero means create the expression with this type, rather than
5190 RESULT_TYPE. */
5191 tree build_type = 0;
5193 /* Nonzero means after finally constructing the expression
5194 convert it to this type. */
5195 tree final_type = 0;
5197 tree result;
5199 /* Nonzero if this is an operation like MIN or MAX which can
5200 safely be computed in short if both args are promoted shorts.
5201 Also implies COMMON.
5202 -1 indicates a bitwise operation; this makes a difference
5203 in the exact conditions for when it is safe to do the operation
5204 in a narrower mode. */
5205 int shorten = 0;
5207 /* Nonzero if this is a comparison operation;
5208 if both args are promoted shorts, compare the original shorts.
5209 Also implies COMMON. */
5210 int short_compare = 0;
5212 /* Nonzero if this is a right-shift operation, which can be computed on the
5213 original short and then promoted if the operand is a promoted short. */
5214 int short_shift = 0;
5216 /* Nonzero means set RESULT_TYPE to the common type of the args. */
5217 int common = 0;
5219 /* True if both operands have arithmetic type. */
5220 bool arithmetic_types_p;
5222 /* Remember whether we're doing / or %. */
5223 bool doing_div_or_mod = false;
5225 /* Remember whether we're doing << or >>. */
5226 bool doing_shift = false;
5228 /* Tree holding instrumentation expression. */
5229 tree instrument_expr = NULL_TREE;
5231 /* True means this is an arithmetic operation that may need excess
5232 precision. */
5233 bool may_need_excess_precision;
5235 /* Apply default conversions. */
5236 op0 = resolve_nondeduced_context (orig_op0, complain);
5237 op1 = resolve_nondeduced_context (orig_op1, complain);
5239 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5240 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5241 || code == TRUTH_XOR_EXPR)
5243 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5244 op0 = decay_conversion (op0, complain);
5245 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5246 op1 = decay_conversion (op1, complain);
5248 else
5250 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5251 op0 = cp_default_conversion (op0, complain);
5252 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5253 op1 = cp_default_conversion (op1, complain);
5256 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5257 STRIP_TYPE_NOPS (op0);
5258 STRIP_TYPE_NOPS (op1);
5260 /* DTRT if one side is an overloaded function, but complain about it. */
5261 if (type_unknown_p (op0))
5263 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5264 if (t != error_mark_node)
5266 if (complain & tf_error)
5267 permerror (location,
5268 "assuming cast to type %qT from overloaded function",
5269 TREE_TYPE (t));
5270 op0 = t;
5273 if (type_unknown_p (op1))
5275 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5276 if (t != error_mark_node)
5278 if (complain & tf_error)
5279 permerror (location,
5280 "assuming cast to type %qT from overloaded function",
5281 TREE_TYPE (t));
5282 op1 = t;
5286 orig_type0 = type0 = TREE_TYPE (op0);
5287 orig_type1 = type1 = TREE_TYPE (op1);
5288 tree non_ep_op0 = op0;
5289 tree non_ep_op1 = op1;
5291 /* The expression codes of the data types of the arguments tell us
5292 whether the arguments are integers, floating, pointers, etc. */
5293 code0 = TREE_CODE (type0);
5294 code1 = TREE_CODE (type1);
5296 /* If an error was already reported for one of the arguments,
5297 avoid reporting another error. */
5298 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5299 return error_mark_node;
5301 if ((invalid_op_diag
5302 = targetm.invalid_binary_op (code, type0, type1)))
5304 if (complain & tf_error)
5306 if (code0 == REAL_TYPE
5307 && code1 == REAL_TYPE
5308 && (extended_float_type_p (type0)
5309 || extended_float_type_p (type1))
5310 && cp_compare_floating_point_conversion_ranks (type0,
5311 type1) == 3)
5313 rich_location richloc (line_table, location);
5314 binary_op_error (&richloc, code, type0, type1);
5316 else
5317 error (invalid_op_diag);
5319 return error_mark_node;
5322 switch (code)
5324 case PLUS_EXPR:
5325 case MINUS_EXPR:
5326 case MULT_EXPR:
5327 case TRUNC_DIV_EXPR:
5328 case CEIL_DIV_EXPR:
5329 case FLOOR_DIV_EXPR:
5330 case ROUND_DIV_EXPR:
5331 case EXACT_DIV_EXPR:
5332 may_need_excess_precision = true;
5333 break;
5334 case EQ_EXPR:
5335 case NE_EXPR:
5336 case LE_EXPR:
5337 case GE_EXPR:
5338 case LT_EXPR:
5339 case GT_EXPR:
5340 case SPACESHIP_EXPR:
5341 /* Excess precision for implicit conversions of integers to
5342 floating point. */
5343 may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5344 || ANY_INTEGRAL_TYPE_P (type1));
5345 break;
5346 default:
5347 may_need_excess_precision = false;
5348 break;
5350 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5352 op0 = TREE_OPERAND (op0, 0);
5353 type0 = TREE_TYPE (op0);
5355 else if (may_need_excess_precision
5356 && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5357 if (tree eptype = excess_precision_type (type0))
5359 type0 = eptype;
5360 op0 = convert (eptype, op0);
5362 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5364 op1 = TREE_OPERAND (op1, 0);
5365 type1 = TREE_TYPE (op1);
5367 else if (may_need_excess_precision
5368 && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5369 if (tree eptype = excess_precision_type (type1))
5371 type1 = eptype;
5372 op1 = convert (eptype, op1);
5375 /* Issue warnings about peculiar, but valid, uses of NULL. */
5376 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5377 /* It's reasonable to use pointer values as operands of &&
5378 and ||, so NULL is no exception. */
5379 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5380 && ( /* Both are NULL (or 0) and the operation was not a
5381 comparison or a pointer subtraction. */
5382 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5383 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5384 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5385 || (!null_ptr_cst_p (orig_op0)
5386 && !TYPE_PTR_OR_PTRMEM_P (type0))
5387 || (!null_ptr_cst_p (orig_op1)
5388 && !TYPE_PTR_OR_PTRMEM_P (type1)))
5389 && (complain & tf_warning))
5391 location_t loc =
5392 expansion_point_location_if_in_system_header (input_location);
5394 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5397 /* In case when one of the operands of the binary operation is
5398 a vector and another is a scalar -- convert scalar to vector. */
5399 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5400 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5402 enum stv_conv convert_flag
5403 = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5404 complain & tf_error);
5406 switch (convert_flag)
5408 case stv_error:
5409 return error_mark_node;
5410 case stv_firstarg:
5412 op0 = convert (TREE_TYPE (type1), op0);
5413 op0 = save_expr (op0);
5414 op0 = build_vector_from_val (type1, op0);
5415 orig_type0 = type0 = TREE_TYPE (op0);
5416 code0 = TREE_CODE (type0);
5417 converted = 1;
5418 break;
5420 case stv_secondarg:
5422 op1 = convert (TREE_TYPE (type0), op1);
5423 op1 = save_expr (op1);
5424 op1 = build_vector_from_val (type0, op1);
5425 orig_type1 = type1 = TREE_TYPE (op1);
5426 code1 = TREE_CODE (type1);
5427 converted = 1;
5428 break;
5430 default:
5431 break;
5435 switch (code)
5437 case MINUS_EXPR:
5438 /* Subtraction of two similar pointers.
5439 We must subtract them as integers, then divide by object size. */
5440 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5441 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5442 TREE_TYPE (type1)))
5444 result = pointer_diff (location, op0, op1,
5445 common_pointer_type (type0, type1), complain,
5446 &instrument_expr);
5447 if (instrument_expr != NULL)
5448 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5449 instrument_expr, result);
5451 return result;
5453 /* In all other cases except pointer - int, the usual arithmetic
5454 rules apply. */
5455 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5457 common = 1;
5458 break;
5460 /* The pointer - int case is just like pointer + int; fall
5461 through. */
5462 gcc_fallthrough ();
5463 case PLUS_EXPR:
5464 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5465 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5467 tree ptr_operand;
5468 tree int_operand;
5469 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5470 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5471 if (processing_template_decl)
5473 result_type = TREE_TYPE (ptr_operand);
5474 break;
5476 return cp_pointer_int_sum (location, code,
5477 ptr_operand,
5478 int_operand,
5479 complain);
5481 common = 1;
5482 break;
5484 case MULT_EXPR:
5485 common = 1;
5486 break;
5488 case TRUNC_DIV_EXPR:
5489 case CEIL_DIV_EXPR:
5490 case FLOOR_DIV_EXPR:
5491 case ROUND_DIV_EXPR:
5492 case EXACT_DIV_EXPR:
5493 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5495 tree type0 = TREE_OPERAND (op0, 0);
5496 tree type1 = TREE_OPERAND (op1, 0);
5497 tree first_arg = tree_strip_any_location_wrapper (type0);
5498 if (!TYPE_P (type0))
5499 type0 = TREE_TYPE (type0);
5500 if (!TYPE_P (type1))
5501 type1 = TREE_TYPE (type1);
5502 if (type0
5503 && INDIRECT_TYPE_P (type0)
5504 && same_type_p (TREE_TYPE (type0), type1))
5506 if (!(TREE_CODE (first_arg) == PARM_DECL
5507 && DECL_ARRAY_PARAMETER_P (first_arg)
5508 && warn_sizeof_array_argument)
5509 && (complain & tf_warning))
5511 auto_diagnostic_group d;
5512 if (warning_at (location, OPT_Wsizeof_pointer_div,
5513 "division %<sizeof (%T) / sizeof (%T)%> does "
5514 "not compute the number of array elements",
5515 type0, type1))
5516 if (DECL_P (first_arg))
5517 inform (DECL_SOURCE_LOCATION (first_arg),
5518 "first %<sizeof%> operand was declared here");
5521 else if (!dependent_type_p (type0)
5522 && !dependent_type_p (type1)
5523 && TREE_CODE (type0) == ARRAY_TYPE
5524 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5525 /* Set by finish_parenthesized_expr. */
5526 && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5527 && (complain & tf_warning))
5528 maybe_warn_sizeof_array_div (location, first_arg, type0,
5529 op1, non_reference (type1));
5532 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5533 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5534 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5535 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5537 enum tree_code tcode0 = code0, tcode1 = code1;
5538 doing_div_or_mod = true;
5539 warn_for_div_by_zero (location, fold_for_warn (op1));
5541 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5542 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5543 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5544 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5546 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5547 resultcode = RDIV_EXPR;
5548 else
5550 /* When dividing two signed integers, we have to promote to int.
5551 unless we divide by a constant != -1. Note that default
5552 conversion will have been performed on the operands at this
5553 point, so we have to dig out the original type to find out if
5554 it was unsigned. */
5555 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5556 shorten = may_shorten_divmod (op0, stripped_op1);
5559 common = 1;
5561 break;
5563 case BIT_AND_EXPR:
5564 case BIT_IOR_EXPR:
5565 case BIT_XOR_EXPR:
5566 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5567 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5568 && !VECTOR_FLOAT_TYPE_P (type0)
5569 && !VECTOR_FLOAT_TYPE_P (type1)))
5570 shorten = -1;
5571 break;
5573 case TRUNC_MOD_EXPR:
5574 case FLOOR_MOD_EXPR:
5575 doing_div_or_mod = true;
5576 warn_for_div_by_zero (location, fold_for_warn (op1));
5578 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5579 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5580 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5581 common = 1;
5582 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5584 /* Although it would be tempting to shorten always here, that loses
5585 on some targets, since the modulo instruction is undefined if the
5586 quotient can't be represented in the computation mode. We shorten
5587 only if unsigned or if dividing by something we know != -1. */
5588 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5589 shorten = may_shorten_divmod (op0, stripped_op1);
5590 common = 1;
5592 break;
5594 case TRUTH_ANDIF_EXPR:
5595 case TRUTH_ORIF_EXPR:
5596 case TRUTH_AND_EXPR:
5597 case TRUTH_OR_EXPR:
5598 if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5600 if (!COMPARISON_CLASS_P (op1))
5601 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5602 build_zero_cst (type1), complain);
5603 if (code == TRUTH_ANDIF_EXPR)
5605 tree z = build_zero_cst (TREE_TYPE (op1));
5606 return build_conditional_expr (location, op0, op1, z, complain);
5608 else if (code == TRUTH_ORIF_EXPR)
5610 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5611 return build_conditional_expr (location, op0, m1, op1, complain);
5613 else
5614 gcc_unreachable ();
5616 if (gnu_vector_type_p (type0)
5617 && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5619 if (!COMPARISON_CLASS_P (op0))
5620 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5621 build_zero_cst (type0), complain);
5622 if (!VECTOR_TYPE_P (type1))
5624 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5625 tree z = build_zero_cst (TREE_TYPE (op0));
5626 op1 = build_conditional_expr (location, op1, m1, z, complain);
5628 else if (!COMPARISON_CLASS_P (op1))
5629 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5630 build_zero_cst (type1), complain);
5632 if (code == TRUTH_ANDIF_EXPR)
5633 code = BIT_AND_EXPR;
5634 else if (code == TRUTH_ORIF_EXPR)
5635 code = BIT_IOR_EXPR;
5636 else
5637 gcc_unreachable ();
5639 return cp_build_binary_op (location, code, op0, op1, complain);
5642 result_type = boolean_type_node;
5643 break;
5645 /* Shift operations: result has same type as first operand;
5646 always convert second operand to int.
5647 Also set SHORT_SHIFT if shifting rightward. */
5649 case RSHIFT_EXPR:
5650 if (gnu_vector_type_p (type0)
5651 && code1 == INTEGER_TYPE
5652 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5654 result_type = type0;
5655 converted = 1;
5657 else if (gnu_vector_type_p (type0)
5658 && gnu_vector_type_p (type1)
5659 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5660 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5661 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5662 TYPE_VECTOR_SUBPARTS (type1)))
5664 result_type = type0;
5665 converted = 1;
5667 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5669 tree const_op1 = fold_for_warn (op1);
5670 if (TREE_CODE (const_op1) != INTEGER_CST)
5671 const_op1 = op1;
5672 result_type = type0;
5673 doing_shift = true;
5674 if (TREE_CODE (const_op1) == INTEGER_CST)
5676 if (tree_int_cst_lt (const_op1, integer_zero_node))
5678 if ((complain & tf_warning)
5679 && c_inhibit_evaluation_warnings == 0)
5680 warning_at (location, OPT_Wshift_count_negative,
5681 "right shift count is negative");
5683 else
5685 if (!integer_zerop (const_op1))
5686 short_shift = 1;
5688 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5689 && (complain & tf_warning)
5690 && c_inhibit_evaluation_warnings == 0)
5691 warning_at (location, OPT_Wshift_count_overflow,
5692 "right shift count >= width of type");
5695 /* Avoid converting op1 to result_type later. */
5696 converted = 1;
5698 break;
5700 case LSHIFT_EXPR:
5701 if (gnu_vector_type_p (type0)
5702 && code1 == INTEGER_TYPE
5703 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5705 result_type = type0;
5706 converted = 1;
5708 else if (gnu_vector_type_p (type0)
5709 && gnu_vector_type_p (type1)
5710 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5711 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5712 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5713 TYPE_VECTOR_SUBPARTS (type1)))
5715 result_type = type0;
5716 converted = 1;
5718 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5720 tree const_op0 = fold_for_warn (op0);
5721 if (TREE_CODE (const_op0) != INTEGER_CST)
5722 const_op0 = op0;
5723 tree const_op1 = fold_for_warn (op1);
5724 if (TREE_CODE (const_op1) != INTEGER_CST)
5725 const_op1 = op1;
5726 result_type = type0;
5727 doing_shift = true;
5728 if (TREE_CODE (const_op0) == INTEGER_CST
5729 && tree_int_cst_sgn (const_op0) < 0
5730 && !TYPE_OVERFLOW_WRAPS (type0)
5731 && (complain & tf_warning)
5732 && c_inhibit_evaluation_warnings == 0)
5733 warning_at (location, OPT_Wshift_negative_value,
5734 "left shift of negative value");
5735 if (TREE_CODE (const_op1) == INTEGER_CST)
5737 if (tree_int_cst_lt (const_op1, integer_zero_node))
5739 if ((complain & tf_warning)
5740 && c_inhibit_evaluation_warnings == 0)
5741 warning_at (location, OPT_Wshift_count_negative,
5742 "left shift count is negative");
5744 else if (compare_tree_int (const_op1,
5745 TYPE_PRECISION (type0)) >= 0)
5747 if ((complain & tf_warning)
5748 && c_inhibit_evaluation_warnings == 0)
5749 warning_at (location, OPT_Wshift_count_overflow,
5750 "left shift count >= width of type");
5752 else if (TREE_CODE (const_op0) == INTEGER_CST
5753 && (complain & tf_warning))
5754 maybe_warn_shift_overflow (location, const_op0, const_op1);
5756 /* Avoid converting op1 to result_type later. */
5757 converted = 1;
5759 break;
5761 case EQ_EXPR:
5762 case NE_EXPR:
5763 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5764 goto vector_compare;
5765 if ((complain & tf_warning)
5766 && c_inhibit_evaluation_warnings == 0
5767 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5768 warning_at (location, OPT_Wfloat_equal,
5769 "comparing floating-point with %<==%> "
5770 "or %<!=%> is unsafe");
5771 if (complain & tf_warning)
5773 tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5774 tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5775 if ((TREE_CODE (stripped_orig_op0) == STRING_CST
5776 && !integer_zerop (cp_fully_fold (op1)))
5777 || (TREE_CODE (stripped_orig_op1) == STRING_CST
5778 && !integer_zerop (cp_fully_fold (op0))))
5779 warning_at (location, OPT_Waddress,
5780 "comparison with string literal results in "
5781 "unspecified behavior");
5782 else if (warn_array_compare
5783 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5784 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5785 do_warn_array_compare (location, code, stripped_orig_op0,
5786 stripped_orig_op1);
5789 build_type = boolean_type_node;
5790 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5791 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5792 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5793 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5794 short_compare = 1;
5795 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5796 && null_ptr_cst_p (orig_op1))
5797 /* Handle, eg, (void*)0 (c++/43906), and more. */
5798 || (code0 == POINTER_TYPE
5799 && TYPE_PTR_P (type1) && integer_zerop (op1)))
5801 if (TYPE_PTR_P (type1))
5802 result_type = composite_pointer_type (location,
5803 type0, type1, op0, op1,
5804 CPO_COMPARISON, complain);
5805 else
5806 result_type = type0;
5808 if (char_type_p (TREE_TYPE (orig_op1)))
5810 auto_diagnostic_group d;
5811 if (warning_at (location, OPT_Wpointer_compare,
5812 "comparison between pointer and zero character "
5813 "constant"))
5814 inform (location,
5815 "did you mean to dereference the pointer?");
5817 warn_for_null_address (location, op0, complain);
5819 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5820 && null_ptr_cst_p (orig_op0))
5821 /* Handle, eg, (void*)0 (c++/43906), and more. */
5822 || (code1 == POINTER_TYPE
5823 && TYPE_PTR_P (type0) && integer_zerop (op0)))
5825 if (TYPE_PTR_P (type0))
5826 result_type = composite_pointer_type (location,
5827 type0, type1, op0, op1,
5828 CPO_COMPARISON, complain);
5829 else
5830 result_type = type1;
5832 if (char_type_p (TREE_TYPE (orig_op0)))
5834 auto_diagnostic_group d;
5835 if (warning_at (location, OPT_Wpointer_compare,
5836 "comparison between pointer and zero character "
5837 "constant"))
5838 inform (location,
5839 "did you mean to dereference the pointer?");
5841 warn_for_null_address (location, op1, complain);
5843 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5844 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5845 result_type = composite_pointer_type (location,
5846 type0, type1, op0, op1,
5847 CPO_COMPARISON, complain);
5848 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5849 /* One of the operands must be of nullptr_t type. */
5850 result_type = TREE_TYPE (nullptr_node);
5851 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5853 result_type = type0;
5854 if (complain & tf_error)
5855 permerror (location, "ISO C++ forbids comparison between "
5856 "pointer and integer");
5857 else
5858 return error_mark_node;
5860 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5862 result_type = type1;
5863 if (complain & tf_error)
5864 permerror (location, "ISO C++ forbids comparison between "
5865 "pointer and integer");
5866 else
5867 return error_mark_node;
5869 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5871 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5872 == ptrmemfunc_vbit_in_delta)
5874 tree pfn0, delta0, e1, e2;
5876 if (TREE_SIDE_EFFECTS (op0))
5877 op0 = cp_save_expr (op0);
5879 pfn0 = pfn_from_ptrmemfunc (op0);
5880 delta0 = delta_from_ptrmemfunc (op0);
5882 /* If we will warn below about a null-address compare
5883 involving the orig_op0 ptrmemfunc, we'd likely also
5884 warn about the pfn0's null-address compare, and
5885 that would be redundant, so suppress it. */
5886 warning_sentinel ws (warn_address);
5887 e1 = cp_build_binary_op (location,
5888 EQ_EXPR,
5889 pfn0,
5890 build_zero_cst (TREE_TYPE (pfn0)),
5891 complain);
5893 e2 = cp_build_binary_op (location,
5894 BIT_AND_EXPR,
5895 delta0,
5896 integer_one_node,
5897 complain);
5899 if (complain & tf_warning)
5900 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5902 e2 = cp_build_binary_op (location,
5903 EQ_EXPR, e2, integer_zero_node,
5904 complain);
5905 op0 = cp_build_binary_op (location,
5906 TRUTH_ANDIF_EXPR, e1, e2,
5907 complain);
5908 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5910 else
5912 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5913 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5915 result_type = TREE_TYPE (op0);
5917 warn_for_null_address (location, orig_op0, complain);
5919 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5920 return cp_build_binary_op (location, code, op1, op0, complain);
5921 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5923 tree type;
5924 /* E will be the final comparison. */
5925 tree e;
5926 /* E1 and E2 are for scratch. */
5927 tree e1;
5928 tree e2;
5929 tree pfn0;
5930 tree pfn1;
5931 tree delta0;
5932 tree delta1;
5934 type = composite_pointer_type (location, type0, type1, op0, op1,
5935 CPO_COMPARISON, complain);
5937 if (!same_type_p (TREE_TYPE (op0), type))
5938 op0 = cp_convert_and_check (type, op0, complain);
5939 if (!same_type_p (TREE_TYPE (op1), type))
5940 op1 = cp_convert_and_check (type, op1, complain);
5942 if (op0 == error_mark_node || op1 == error_mark_node)
5943 return error_mark_node;
5945 if (TREE_SIDE_EFFECTS (op0))
5946 op0 = save_expr (op0);
5947 if (TREE_SIDE_EFFECTS (op1))
5948 op1 = save_expr (op1);
5950 pfn0 = pfn_from_ptrmemfunc (op0);
5951 pfn0 = cp_fully_fold (pfn0);
5952 /* Avoid -Waddress warnings (c++/64877). */
5953 if (TREE_CODE (pfn0) == ADDR_EXPR)
5954 suppress_warning (pfn0, OPT_Waddress);
5955 pfn1 = pfn_from_ptrmemfunc (op1);
5956 pfn1 = cp_fully_fold (pfn1);
5957 delta0 = delta_from_ptrmemfunc (op0);
5958 delta1 = delta_from_ptrmemfunc (op1);
5959 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5960 == ptrmemfunc_vbit_in_delta)
5962 /* We generate:
5964 (op0.pfn == op1.pfn
5965 && ((op0.delta == op1.delta)
5966 || (!op0.pfn && op0.delta & 1 == 0
5967 && op1.delta & 1 == 0))
5969 The reason for the `!op0.pfn' bit is that a NULL
5970 pointer-to-member is any member with a zero PFN and
5971 LSB of the DELTA field is 0. */
5973 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
5974 delta0,
5975 integer_one_node,
5976 complain);
5977 e1 = cp_build_binary_op (location,
5978 EQ_EXPR, e1, integer_zero_node,
5979 complain);
5980 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
5981 delta1,
5982 integer_one_node,
5983 complain);
5984 e2 = cp_build_binary_op (location,
5985 EQ_EXPR, e2, integer_zero_node,
5986 complain);
5987 e1 = cp_build_binary_op (location,
5988 TRUTH_ANDIF_EXPR, e2, e1,
5989 complain);
5990 e2 = cp_build_binary_op (location, EQ_EXPR,
5991 pfn0,
5992 build_zero_cst (TREE_TYPE (pfn0)),
5993 complain);
5994 e2 = cp_build_binary_op (location,
5995 TRUTH_ANDIF_EXPR, e2, e1, complain);
5996 e1 = cp_build_binary_op (location,
5997 EQ_EXPR, delta0, delta1, complain);
5998 e1 = cp_build_binary_op (location,
5999 TRUTH_ORIF_EXPR, e1, e2, complain);
6001 else
6003 /* We generate:
6005 (op0.pfn == op1.pfn
6006 && (!op0.pfn || op0.delta == op1.delta))
6008 The reason for the `!op0.pfn' bit is that a NULL
6009 pointer-to-member is any member with a zero PFN; the
6010 DELTA field is unspecified. */
6012 e1 = cp_build_binary_op (location,
6013 EQ_EXPR, delta0, delta1, complain);
6014 e2 = cp_build_binary_op (location,
6015 EQ_EXPR,
6016 pfn0,
6017 build_zero_cst (TREE_TYPE (pfn0)),
6018 complain);
6019 e1 = cp_build_binary_op (location,
6020 TRUTH_ORIF_EXPR, e1, e2, complain);
6022 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6023 e = cp_build_binary_op (location,
6024 TRUTH_ANDIF_EXPR, e2, e1, complain);
6025 if (code == EQ_EXPR)
6026 return e;
6027 return cp_build_binary_op (location,
6028 EQ_EXPR, e, integer_zero_node, complain);
6030 else
6032 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6033 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6034 type1));
6035 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6036 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6037 type0));
6040 break;
6042 case MAX_EXPR:
6043 case MIN_EXPR:
6044 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6045 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6046 shorten = 1;
6047 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6048 result_type = composite_pointer_type (location,
6049 type0, type1, op0, op1,
6050 CPO_COMPARISON, complain);
6051 break;
6053 case LE_EXPR:
6054 case GE_EXPR:
6055 case LT_EXPR:
6056 case GT_EXPR:
6057 case SPACESHIP_EXPR:
6058 if (TREE_CODE (orig_op0) == STRING_CST
6059 || TREE_CODE (orig_op1) == STRING_CST)
6061 if (complain & tf_warning)
6062 warning_at (location, OPT_Waddress,
6063 "comparison with string literal results "
6064 "in unspecified behavior");
6066 else if (warn_array_compare
6067 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6068 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6069 && code != SPACESHIP_EXPR
6070 && (complain & tf_warning))
6071 do_warn_array_compare (location, code,
6072 tree_strip_any_location_wrapper (orig_op0),
6073 tree_strip_any_location_wrapper (orig_op1));
6075 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6077 vector_compare:
6078 tree intt;
6079 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6080 TREE_TYPE (type1))
6081 && !vector_types_compatible_elements_p (type0, type1))
6083 if (complain & tf_error)
6085 error_at (location, "comparing vectors with different "
6086 "element types");
6087 inform (location, "operand types are %qT and %qT",
6088 type0, type1);
6090 return error_mark_node;
6093 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6094 TYPE_VECTOR_SUBPARTS (type1)))
6096 if (complain & tf_error)
6098 error_at (location, "comparing vectors with different "
6099 "number of elements");
6100 inform (location, "operand types are %qT and %qT",
6101 type0, type1);
6103 return error_mark_node;
6106 /* It's not precisely specified how the usual arithmetic
6107 conversions apply to the vector types. Here, we use
6108 the unsigned type if one of the operands is signed and
6109 the other one is unsigned. */
6110 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6112 if (!TYPE_UNSIGNED (type0))
6113 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6114 else
6115 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6116 warning_at (location, OPT_Wsign_compare, "comparison between "
6117 "types %qT and %qT", type0, type1);
6120 if (resultcode == SPACESHIP_EXPR)
6122 if (complain & tf_error)
6123 sorry_at (location, "three-way comparison of vectors");
6124 return error_mark_node;
6127 /* Always construct signed integer vector type. */
6128 intt = c_common_type_for_size
6129 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
6130 if (!intt)
6132 if (complain & tf_error)
6133 error_at (location, "could not find an integer type "
6134 "of the same size as %qT", TREE_TYPE (type0));
6135 return error_mark_node;
6137 result_type = build_opaque_vector_type (intt,
6138 TYPE_VECTOR_SUBPARTS (type0));
6139 return build_vec_cmp (resultcode, result_type, op0, op1);
6141 build_type = boolean_type_node;
6142 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6143 || code0 == ENUMERAL_TYPE)
6144 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6145 || code1 == ENUMERAL_TYPE))
6146 short_compare = 1;
6147 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6148 result_type = composite_pointer_type (location,
6149 type0, type1, op0, op1,
6150 CPO_COMPARISON, complain);
6151 else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6152 || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6153 || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6155 /* Core Issue 1512 made this ill-formed. */
6156 if (complain & tf_error)
6157 error_at (location, "ordered comparison of pointer with "
6158 "integer zero (%qT and %qT)", type0, type1);
6159 return error_mark_node;
6161 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6163 result_type = type0;
6164 if (complain & tf_error)
6165 permerror (location, "ISO C++ forbids comparison between "
6166 "pointer and integer");
6167 else
6168 return error_mark_node;
6170 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6172 result_type = type1;
6173 if (complain & tf_error)
6174 permerror (location, "ISO C++ forbids comparison between "
6175 "pointer and integer");
6176 else
6177 return error_mark_node;
6180 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6181 && !processing_template_decl
6182 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6184 op0 = save_expr (op0);
6185 op1 = save_expr (op1);
6187 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6188 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6191 break;
6193 case UNORDERED_EXPR:
6194 case ORDERED_EXPR:
6195 case UNLT_EXPR:
6196 case UNLE_EXPR:
6197 case UNGT_EXPR:
6198 case UNGE_EXPR:
6199 case UNEQ_EXPR:
6200 build_type = integer_type_node;
6201 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6203 if (complain & tf_error)
6204 error ("unordered comparison on non-floating-point argument");
6205 return error_mark_node;
6207 common = 1;
6208 break;
6210 default:
6211 break;
6214 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6215 || code0 == ENUMERAL_TYPE)
6216 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6217 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6218 arithmetic_types_p = 1;
6219 else
6221 arithmetic_types_p = 0;
6222 /* Vector arithmetic is only allowed when both sides are vectors. */
6223 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6225 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6226 || !vector_types_compatible_elements_p (type0, type1))
6228 if (complain & tf_error)
6230 /* "location" already embeds the locations of the
6231 operands, so we don't need to add them separately
6232 to richloc. */
6233 rich_location richloc (line_table, location);
6234 binary_op_error (&richloc, code, type0, type1);
6236 return error_mark_node;
6238 arithmetic_types_p = 1;
6241 /* Determine the RESULT_TYPE, if it is not already known. */
6242 if (!result_type
6243 && arithmetic_types_p
6244 && (shorten || common || short_compare))
6246 result_type = cp_common_type (type0, type1);
6247 if (result_type == error_mark_node)
6249 tree t1 = type0;
6250 tree t2 = type1;
6251 if (TREE_CODE (t1) == COMPLEX_TYPE)
6252 t1 = TREE_TYPE (t1);
6253 if (TREE_CODE (t2) == COMPLEX_TYPE)
6254 t2 = TREE_TYPE (t2);
6255 gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6256 && TREE_CODE (t2) == REAL_TYPE
6257 && (extended_float_type_p (t1)
6258 || extended_float_type_p (t2))
6259 && cp_compare_floating_point_conversion_ranks
6260 (t1, t2) == 3);
6261 if (complain & tf_error)
6263 rich_location richloc (line_table, location);
6264 binary_op_error (&richloc, code, type0, type1);
6266 return error_mark_node;
6268 if (complain & tf_warning)
6269 do_warn_double_promotion (result_type, type0, type1,
6270 "implicit conversion from %qH to %qI "
6271 "to match other operand of binary "
6272 "expression", location);
6273 if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6274 TREE_TYPE (orig_op1), complain))
6275 return error_mark_node;
6277 if (may_need_excess_precision
6278 && (orig_type0 != type0 || orig_type1 != type1)
6279 && build_type == NULL_TREE
6280 && result_type)
6282 gcc_assert (common);
6283 semantic_result_type = cp_common_type (orig_type0, orig_type1);
6284 if (semantic_result_type == error_mark_node)
6286 tree t1 = orig_type0;
6287 tree t2 = orig_type1;
6288 if (TREE_CODE (t1) == COMPLEX_TYPE)
6289 t1 = TREE_TYPE (t1);
6290 if (TREE_CODE (t2) == COMPLEX_TYPE)
6291 t2 = TREE_TYPE (t2);
6292 gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6293 && TREE_CODE (t2) == REAL_TYPE
6294 && (extended_float_type_p (t1)
6295 || extended_float_type_p (t2))
6296 && cp_compare_floating_point_conversion_ranks
6297 (t1, t2) == 3);
6298 if (complain & tf_error)
6300 rich_location richloc (line_table, location);
6301 binary_op_error (&richloc, code, type0, type1);
6303 return error_mark_node;
6307 if (code == SPACESHIP_EXPR)
6309 iloc_sentinel s (location);
6311 tree orig_type0 = TREE_TYPE (orig_op0);
6312 tree_code orig_code0 = TREE_CODE (orig_type0);
6313 tree orig_type1 = TREE_TYPE (orig_op1);
6314 tree_code orig_code1 = TREE_CODE (orig_type1);
6315 if (!result_type || result_type == error_mark_node)
6316 /* Nope. */
6317 result_type = NULL_TREE;
6318 else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6319 /* "If one of the operands is of type bool and the other is not, the
6320 program is ill-formed." */
6321 result_type = NULL_TREE;
6322 else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6323 && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6324 /* We only do array/function-to-pointer conversion if "at least one of
6325 the operands is of pointer type". */
6326 result_type = NULL_TREE;
6327 else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6328 /* <=> no longer supports equality relations. */
6329 result_type = NULL_TREE;
6330 else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6331 && !(same_type_ignoring_top_level_qualifiers_p
6332 (orig_type0, orig_type1)))
6333 /* "If both operands have arithmetic types, or one operand has integral
6334 type and the other operand has unscoped enumeration type, the usual
6335 arithmetic conversions are applied to the operands." So we don't do
6336 arithmetic conversions if the operands both have enumeral type. */
6337 result_type = NULL_TREE;
6338 else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6339 || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6340 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6341 [where one is of enumeration type and the other is of a different
6342 enumeration type or a floating-point type] are ill-formed. */
6343 result_type = NULL_TREE;
6345 if (result_type)
6347 build_type = spaceship_type (result_type, complain);
6348 if (build_type == error_mark_node)
6349 return error_mark_node;
6352 if (result_type && arithmetic_types_p)
6354 /* If a narrowing conversion is required, other than from an integral
6355 type to a floating point type, the program is ill-formed. */
6356 bool ok = true;
6357 if (TREE_CODE (result_type) == REAL_TYPE
6358 && CP_INTEGRAL_TYPE_P (orig_type0))
6359 /* OK */;
6360 else if (!check_narrowing (result_type, orig_op0, complain))
6361 ok = false;
6362 if (TREE_CODE (result_type) == REAL_TYPE
6363 && CP_INTEGRAL_TYPE_P (orig_type1))
6364 /* OK */;
6365 else if (!check_narrowing (result_type, orig_op1, complain))
6366 ok = false;
6367 if (!ok && !(complain & tf_error))
6368 return error_mark_node;
6372 if (!result_type)
6374 if (complain & tf_error)
6376 binary_op_rich_location richloc (location,
6377 orig_op0, orig_op1, true);
6378 error_at (&richloc,
6379 "invalid operands of types %qT and %qT to binary %qO",
6380 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6382 return error_mark_node;
6385 /* If we're in a template, the only thing we need to know is the
6386 RESULT_TYPE. */
6387 if (processing_template_decl)
6389 /* Since the middle-end checks the type when doing a build2, we
6390 need to build the tree in pieces. This built tree will never
6391 get out of the front-end as we replace it when instantiating
6392 the template. */
6393 tree tmp = build2 (resultcode,
6394 build_type ? build_type : result_type,
6395 NULL_TREE, op1);
6396 TREE_OPERAND (tmp, 0) = op0;
6397 if (semantic_result_type)
6398 tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6399 return tmp;
6402 /* Remember the original type; RESULT_TYPE might be changed later on
6403 by shorten_binary_op. */
6404 tree orig_type = result_type;
6406 if (arithmetic_types_p)
6408 bool first_complex = (code0 == COMPLEX_TYPE);
6409 bool second_complex = (code1 == COMPLEX_TYPE);
6410 int none_complex = (!first_complex && !second_complex);
6412 /* Adapted from patch for c/24581. */
6413 if (first_complex != second_complex
6414 && (code == PLUS_EXPR
6415 || code == MINUS_EXPR
6416 || code == MULT_EXPR
6417 || (code == TRUNC_DIV_EXPR && first_complex))
6418 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6419 && flag_signed_zeros)
6421 /* An operation on mixed real/complex operands must be
6422 handled specially, but the language-independent code can
6423 more easily optimize the plain complex arithmetic if
6424 -fno-signed-zeros. */
6425 tree real_type = TREE_TYPE (result_type);
6426 tree real, imag;
6427 if (first_complex)
6429 if (TREE_TYPE (op0) != result_type)
6430 op0 = cp_convert_and_check (result_type, op0, complain);
6431 if (TREE_TYPE (op1) != real_type)
6432 op1 = cp_convert_and_check (real_type, op1, complain);
6434 else
6436 if (TREE_TYPE (op0) != real_type)
6437 op0 = cp_convert_and_check (real_type, op0, complain);
6438 if (TREE_TYPE (op1) != result_type)
6439 op1 = cp_convert_and_check (result_type, op1, complain);
6441 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6442 return error_mark_node;
6443 if (first_complex)
6445 op0 = save_expr (op0);
6446 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6447 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6448 switch (code)
6450 case MULT_EXPR:
6451 case TRUNC_DIV_EXPR:
6452 op1 = save_expr (op1);
6453 imag = build2 (resultcode, real_type, imag, op1);
6454 /* Fall through. */
6455 case PLUS_EXPR:
6456 case MINUS_EXPR:
6457 real = build2 (resultcode, real_type, real, op1);
6458 break;
6459 default:
6460 gcc_unreachable();
6463 else
6465 op1 = save_expr (op1);
6466 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6467 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6468 switch (code)
6470 case MULT_EXPR:
6471 op0 = save_expr (op0);
6472 imag = build2 (resultcode, real_type, op0, imag);
6473 /* Fall through. */
6474 case PLUS_EXPR:
6475 real = build2 (resultcode, real_type, op0, real);
6476 break;
6477 case MINUS_EXPR:
6478 real = build2 (resultcode, real_type, op0, real);
6479 imag = build1 (NEGATE_EXPR, real_type, imag);
6480 break;
6481 default:
6482 gcc_unreachable();
6485 result = build2 (COMPLEX_EXPR, result_type, real, imag);
6486 if (semantic_result_type)
6487 result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6488 result);
6489 return result;
6492 /* For certain operations (which identify themselves by shorten != 0)
6493 if both args were extended from the same smaller type,
6494 do the arithmetic in that type and then extend.
6496 shorten !=0 and !=1 indicates a bitwise operation.
6497 For them, this optimization is safe only if
6498 both args are zero-extended or both are sign-extended.
6499 Otherwise, we might change the result.
6500 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6501 but calculated in (unsigned short) it would be (unsigned short)-1. */
6503 if (shorten && none_complex)
6505 final_type = result_type;
6506 result_type = shorten_binary_op (result_type, op0, op1,
6507 shorten == -1);
6510 /* Shifts can be shortened if shifting right. */
6512 if (short_shift)
6514 int unsigned_arg;
6515 tree arg0 = get_narrower (op0, &unsigned_arg);
6516 /* We're not really warning here but when we set short_shift we
6517 used fold_for_warn to fold the operand. */
6518 tree const_op1 = fold_for_warn (op1);
6520 final_type = result_type;
6522 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6523 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6525 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6526 && tree_int_cst_sgn (const_op1) > 0
6527 /* We can shorten only if the shift count is less than the
6528 number of bits in the smaller type size. */
6529 && compare_tree_int (const_op1,
6530 TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6531 /* We cannot drop an unsigned shift after sign-extension. */
6532 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6534 /* Do an unsigned shift if the operand was zero-extended. */
6535 result_type
6536 = c_common_signed_or_unsigned_type (unsigned_arg,
6537 TREE_TYPE (arg0));
6538 /* Convert value-to-be-shifted to that type. */
6539 if (TREE_TYPE (op0) != result_type)
6540 op0 = convert (result_type, op0);
6541 converted = 1;
6545 /* Comparison operations are shortened too but differently.
6546 They identify themselves by setting short_compare = 1. */
6548 if (short_compare)
6550 /* We call shorten_compare only for diagnostics. */
6551 tree xop0 = fold_simple (op0);
6552 tree xop1 = fold_simple (op1);
6553 tree xresult_type = result_type;
6554 enum tree_code xresultcode = resultcode;
6555 shorten_compare (location, &xop0, &xop1, &xresult_type,
6556 &xresultcode);
6559 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6560 && warn_sign_compare
6561 /* Do not warn until the template is instantiated; we cannot
6562 bound the ranges of the arguments until that point. */
6563 && !processing_template_decl
6564 && (complain & tf_warning)
6565 && c_inhibit_evaluation_warnings == 0
6566 /* Even unsigned enum types promote to signed int. We don't
6567 want to issue -Wsign-compare warnings for this case. */
6568 && !enum_cast_to_int (orig_op0)
6569 && !enum_cast_to_int (orig_op1))
6571 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6572 result_type, resultcode);
6576 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6577 Then the expression will be built.
6578 It will be given type FINAL_TYPE if that is nonzero;
6579 otherwise, it will be given type RESULT_TYPE. */
6580 if (! converted)
6582 warning_sentinel w (warn_sign_conversion, short_compare);
6583 if (!same_type_p (TREE_TYPE (op0), result_type))
6584 op0 = cp_convert_and_check (result_type, op0, complain);
6585 if (!same_type_p (TREE_TYPE (op1), result_type))
6586 op1 = cp_convert_and_check (result_type, op1, complain);
6588 if (op0 == error_mark_node || op1 == error_mark_node)
6589 return error_mark_node;
6592 if (build_type == NULL_TREE)
6593 build_type = result_type;
6595 if (doing_shift
6596 && flag_strong_eval_order == 2
6597 && TREE_SIDE_EFFECTS (op1)
6598 && !processing_template_decl)
6600 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6601 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6602 op0 = cp_save_expr (op0);
6603 instrument_expr = op0;
6606 if (sanitize_flags_p ((SANITIZE_SHIFT
6607 | SANITIZE_DIVIDE
6608 | SANITIZE_FLOAT_DIVIDE
6609 | SANITIZE_SI_OVERFLOW))
6610 && current_function_decl != NULL_TREE
6611 && !processing_template_decl
6612 && (doing_div_or_mod || doing_shift))
6614 /* OP0 and/or OP1 might have side-effects. */
6615 op0 = cp_save_expr (op0);
6616 op1 = cp_save_expr (op1);
6617 op0 = fold_non_dependent_expr (op0, complain);
6618 op1 = fold_non_dependent_expr (op1, complain);
6619 tree instrument_expr1 = NULL_TREE;
6620 if (doing_div_or_mod
6621 && sanitize_flags_p (SANITIZE_DIVIDE
6622 | SANITIZE_FLOAT_DIVIDE
6623 | SANITIZE_SI_OVERFLOW))
6625 /* For diagnostics we want to use the promoted types without
6626 shorten_binary_op. So convert the arguments to the
6627 original result_type. */
6628 tree cop0 = op0;
6629 tree cop1 = op1;
6630 if (TREE_TYPE (cop0) != orig_type)
6631 cop0 = cp_convert (orig_type, op0, complain);
6632 if (TREE_TYPE (cop1) != orig_type)
6633 cop1 = cp_convert (orig_type, op1, complain);
6634 instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6636 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6637 instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6638 if (instrument_expr != NULL)
6639 instrument_expr = add_stmt_to_compound (instrument_expr,
6640 instrument_expr1);
6641 else
6642 instrument_expr = instrument_expr1;
6645 result = build2_loc (location, resultcode, build_type, op0, op1);
6646 if (final_type != 0)
6647 result = cp_convert (final_type, result, complain);
6649 if (instrument_expr != NULL)
6650 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6651 instrument_expr, result);
6653 if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6654 result = get_target_expr (result, complain);
6656 if (semantic_result_type)
6657 result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6659 if (!c_inhibit_evaluation_warnings)
6661 if (!processing_template_decl)
6663 op0 = cp_fully_fold (op0);
6664 /* Only consider the second argument if the first isn't overflowed. */
6665 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6666 return result;
6667 op1 = cp_fully_fold (op1);
6668 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6669 return result;
6671 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6672 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6673 return result;
6675 tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6676 if (TREE_OVERFLOW_P (result_ovl))
6677 overflow_warning (location, result_ovl);
6680 return result;
6683 /* Build a VEC_PERM_EXPR.
6684 This is a simple wrapper for c_build_vec_perm_expr. */
6685 tree
6686 build_x_vec_perm_expr (location_t loc,
6687 tree arg0, tree arg1, tree arg2,
6688 tsubst_flags_t complain)
6690 tree orig_arg0 = arg0;
6691 tree orig_arg1 = arg1;
6692 tree orig_arg2 = arg2;
6693 if (processing_template_decl)
6695 if (type_dependent_expression_p (arg0)
6696 || type_dependent_expression_p (arg1)
6697 || type_dependent_expression_p (arg2))
6698 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6700 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6701 if (processing_template_decl && exp != error_mark_node)
6702 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6703 orig_arg1, orig_arg2);
6704 return exp;
6707 /* Build a VEC_PERM_EXPR.
6708 This is a simple wrapper for c_build_shufflevector. */
6709 tree
6710 build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6711 tsubst_flags_t complain)
6713 tree arg0 = (*args)[0];
6714 tree arg1 = (*args)[1];
6715 if (processing_template_decl)
6717 for (unsigned i = 0; i < args->length (); ++i)
6718 if (i <= 1
6719 ? type_dependent_expression_p ((*args)[i])
6720 : instantiation_dependent_expression_p ((*args)[i]))
6722 tree exp = build_min_nt_call_vec (NULL, args);
6723 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6724 return exp;
6727 auto_vec<tree, 16> mask;
6728 for (unsigned i = 2; i < args->length (); ++i)
6730 tree idx = fold_non_dependent_expr ((*args)[i], complain);
6731 mask.safe_push (idx);
6733 tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6734 if (processing_template_decl && exp != error_mark_node)
6736 exp = build_min_non_dep_call_vec (exp, NULL, args);
6737 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6739 return exp;
6742 /* Return a tree for the sum or difference (RESULTCODE says which)
6743 of pointer PTROP and integer INTOP. */
6745 static tree
6746 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6747 tree intop, tsubst_flags_t complain)
6749 tree res_type = TREE_TYPE (ptrop);
6751 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6752 in certain circumstance (when it's valid to do so). So we need
6753 to make sure it's complete. We don't need to check here, if we
6754 can actually complete it at all, as those checks will be done in
6755 pointer_int_sum() anyway. */
6756 complete_type (TREE_TYPE (res_type));
6758 return pointer_int_sum (loc, resultcode, ptrop,
6759 intop, complain & tf_warning_or_error);
6762 /* Return a tree for the difference of pointers OP0 and OP1.
6763 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6764 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6766 static tree
6767 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6768 tsubst_flags_t complain, tree *instrument_expr)
6770 tree result, inttype;
6771 tree restype = ptrdiff_type_node;
6772 tree target_type = TREE_TYPE (ptrtype);
6774 if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6775 return error_mark_node;
6777 if (VOID_TYPE_P (target_type))
6779 if (complain & tf_error)
6780 permerror (loc, "ISO C++ forbids using pointer of "
6781 "type %<void *%> in subtraction");
6782 else
6783 return error_mark_node;
6785 if (TREE_CODE (target_type) == FUNCTION_TYPE)
6787 if (complain & tf_error)
6788 permerror (loc, "ISO C++ forbids using pointer to "
6789 "a function in subtraction");
6790 else
6791 return error_mark_node;
6793 if (TREE_CODE (target_type) == METHOD_TYPE)
6795 if (complain & tf_error)
6796 permerror (loc, "ISO C++ forbids using pointer to "
6797 "a method in subtraction");
6798 else
6799 return error_mark_node;
6801 else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6802 TREE_TYPE (TREE_TYPE (op0)),
6803 !(complain & tf_error))
6804 || !verify_type_context (loc, TCTX_POINTER_ARITH,
6805 TREE_TYPE (TREE_TYPE (op1)),
6806 !(complain & tf_error)))
6807 return error_mark_node;
6809 /* Determine integer type result of the subtraction. This will usually
6810 be the same as the result type (ptrdiff_t), but may need to be a wider
6811 type if pointers for the address space are wider than ptrdiff_t. */
6812 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6813 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6814 else
6815 inttype = restype;
6817 if (!processing_template_decl
6818 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6820 op0 = save_expr (op0);
6821 op1 = save_expr (op1);
6823 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6824 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6827 /* First do the subtraction, then build the divide operator
6828 and only convert at the very end.
6829 Do not do default conversions in case restype is a short type. */
6831 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6832 pointers. If some platform cannot provide that, or has a larger
6833 ptrdiff_type to support differences larger than half the address
6834 space, cast the pointers to some larger integer type and do the
6835 computations in that type. */
6836 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6837 op0 = cp_build_binary_op (loc,
6838 MINUS_EXPR,
6839 cp_convert (inttype, op0, complain),
6840 cp_convert (inttype, op1, complain),
6841 complain);
6842 else
6843 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6845 /* This generates an error if op1 is a pointer to an incomplete type. */
6846 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6848 if (complain & tf_error)
6849 error_at (loc, "invalid use of a pointer to an incomplete type in "
6850 "pointer arithmetic");
6851 else
6852 return error_mark_node;
6855 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6857 if (complain & tf_error)
6858 error_at (loc, "arithmetic on pointer to an empty aggregate");
6859 else
6860 return error_mark_node;
6863 op1 = (TYPE_PTROB_P (ptrtype)
6864 ? size_in_bytes_loc (loc, target_type)
6865 : integer_one_node);
6867 /* Do the division. */
6869 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
6870 cp_convert (inttype, op1, complain));
6871 return cp_convert (restype, result, complain);
6874 /* Construct and perhaps optimize a tree representation
6875 for a unary operation. CODE, a tree_code, specifies the operation
6876 and XARG is the operand. */
6878 tree
6879 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
6880 tree lookups, tsubst_flags_t complain)
6882 tree orig_expr = xarg;
6883 tree exp;
6884 int ptrmem = 0;
6885 tree overload = NULL_TREE;
6887 if (processing_template_decl)
6889 if (type_dependent_expression_p (xarg))
6891 tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6892 TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
6893 return e;
6897 exp = NULL_TREE;
6899 /* [expr.unary.op] says:
6901 The address of an object of incomplete type can be taken.
6903 (And is just the ordinary address operator, not an overloaded
6904 "operator &".) However, if the type is a template
6905 specialization, we must complete the type at this point so that
6906 an overloaded "operator &" will be available if required. */
6907 if (code == ADDR_EXPR
6908 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6909 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6910 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6911 || (TREE_CODE (xarg) == OFFSET_REF)))
6912 /* Don't look for a function. */;
6913 else
6914 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6915 NULL_TREE, lookups, &overload, complain);
6917 if (!exp && code == ADDR_EXPR)
6919 if (is_overloaded_fn (xarg))
6921 tree fn = get_first_fn (xarg);
6922 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
6924 if (complain & tf_error)
6925 error_at (loc, DECL_CONSTRUCTOR_P (fn)
6926 ? G_("taking address of constructor %qD")
6927 : G_("taking address of destructor %qD"),
6928 fn);
6929 return error_mark_node;
6933 /* A pointer to member-function can be formed only by saying
6934 &X::mf. */
6935 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
6936 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
6938 if (TREE_CODE (xarg) != OFFSET_REF
6939 || !TYPE_P (TREE_OPERAND (xarg, 0)))
6941 if (complain & tf_error)
6943 error_at (loc, "invalid use of %qE to form a "
6944 "pointer-to-member-function", xarg.get_value ());
6945 if (TREE_CODE (xarg) != OFFSET_REF)
6946 inform (loc, " a qualified-id is required");
6948 return error_mark_node;
6950 else
6952 if (complain & tf_error)
6953 error_at (loc, "parentheses around %qE cannot be used to "
6954 "form a pointer-to-member-function",
6955 xarg.get_value ());
6956 else
6957 return error_mark_node;
6958 PTRMEM_OK_P (xarg) = 1;
6962 if (TREE_CODE (xarg) == OFFSET_REF)
6964 ptrmem = PTRMEM_OK_P (xarg);
6966 if (!ptrmem && !flag_ms_extensions
6967 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
6969 /* A single non-static member, make sure we don't allow a
6970 pointer-to-member. */
6971 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
6972 TREE_OPERAND (xarg, 0),
6973 ovl_make (TREE_OPERAND (xarg, 1)));
6974 PTRMEM_OK_P (xarg) = ptrmem;
6978 exp = cp_build_addr_expr_strict (xarg, complain);
6980 if (TREE_CODE (exp) == PTRMEM_CST)
6981 PTRMEM_CST_LOCATION (exp) = loc;
6982 else
6983 protected_set_expr_location (exp, loc);
6986 if (processing_template_decl && exp != error_mark_node)
6988 if (overload != NULL_TREE)
6989 return (build_min_non_dep_op_overload
6990 (code, exp, overload, orig_expr, integer_zero_node));
6992 exp = build_min_non_dep (code, exp, orig_expr,
6993 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
6995 if (TREE_CODE (exp) == ADDR_EXPR)
6996 PTRMEM_OK_P (exp) = ptrmem;
6997 return exp;
7000 /* Construct and perhaps optimize a tree representation
7001 for __builtin_addressof operation. ARG specifies the operand. */
7003 tree
7004 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7006 tree orig_expr = arg;
7008 if (processing_template_decl)
7010 if (type_dependent_expression_p (arg))
7011 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7014 tree exp = cp_build_addr_expr_strict (arg, complain);
7016 if (processing_template_decl && exp != error_mark_node)
7017 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7018 return exp;
7021 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7022 constants, where a null value is represented by an INTEGER_CST of
7023 -1. */
7025 tree
7026 cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7028 tree type = TREE_TYPE (expr);
7029 location_t loc = cp_expr_loc_or_input_loc (expr);
7030 if (TYPE_PTR_OR_PTRMEM_P (type)
7031 /* Avoid ICE on invalid use of non-static member function. */
7032 || TREE_CODE (expr) == FUNCTION_DECL)
7033 return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7034 else
7035 return c_common_truthvalue_conversion (loc, expr);
7038 /* Returns EXPR contextually converted to bool. */
7040 tree
7041 contextual_conv_bool (tree expr, tsubst_flags_t complain)
7043 return perform_implicit_conversion_flags (boolean_type_node, expr,
7044 complain, LOOKUP_NORMAL);
7047 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7048 is a low-level function; most callers should use maybe_convert_cond. */
7050 tree
7051 condition_conversion (tree expr)
7053 tree t = contextual_conv_bool (expr, tf_warning_or_error);
7054 if (!processing_template_decl)
7055 t = fold_build_cleanup_point_expr (boolean_type_node, t);
7056 return t;
7059 /* Returns the address of T. This function will fold away
7060 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7061 most places should use cp_build_addr_expr instead. */
7063 tree
7064 build_address (tree t)
7066 if (error_operand_p (t) || !cxx_mark_addressable (t))
7067 return error_mark_node;
7068 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7069 || processing_template_decl);
7070 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7071 if (TREE_CODE (t) != ADDR_EXPR)
7072 t = rvalue (t);
7073 return t;
7076 /* Return a NOP_EXPR converting EXPR to TYPE. */
7078 tree
7079 build_nop (tree type, tree expr)
7081 if (type == error_mark_node || error_operand_p (expr))
7082 return expr;
7083 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
7086 /* Take the address of ARG, whatever that means under C++ semantics.
7087 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7088 and class rvalues as well.
7090 Nothing should call this function directly; instead, callers should use
7091 cp_build_addr_expr or cp_build_addr_expr_strict. */
7093 static tree
7094 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7096 tree argtype;
7097 tree val;
7099 if (!arg || error_operand_p (arg))
7100 return error_mark_node;
7102 arg = mark_lvalue_use (arg);
7103 if (error_operand_p (arg))
7104 return error_mark_node;
7106 argtype = lvalue_type (arg);
7107 location_t loc = cp_expr_loc_or_input_loc (arg);
7109 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7111 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7112 && !really_overloaded_fn (arg))
7114 /* They're trying to take the address of a unique non-static
7115 member function. This is ill-formed (except in MS-land),
7116 but let's try to DTRT.
7117 Note: We only handle unique functions here because we don't
7118 want to complain if there's a static overload; non-unique
7119 cases will be handled by instantiate_type. But we need to
7120 handle this case here to allow casts on the resulting PMF.
7121 We could defer this in non-MS mode, but it's easier to give
7122 a useful error here. */
7124 /* Inside constant member functions, the `this' pointer
7125 contains an extra const qualifier. TYPE_MAIN_VARIANT
7126 is used here to remove this const from the diagnostics
7127 and the created OFFSET_REF. */
7128 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7129 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7130 if (!mark_used (fn, complain) && !(complain & tf_error))
7131 return error_mark_node;
7132 /* Until microsoft headers are known to incorrectly take the address of
7133 unqualified xobj member functions we should not support this
7134 extension.
7135 See comment in class.cc:resolve_address_of_overloaded_function for
7136 the extended reasoning. */
7137 if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7139 auto_diagnostic_group d;
7140 tree name = DECL_NAME (fn);
7141 if (!(complain & tf_error))
7142 return error_mark_node;
7143 else if (current_class_type
7144 && TREE_OPERAND (arg, 0) == current_class_ref)
7145 /* An expression like &memfn. */
7146 if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7147 permerror (loc,
7148 "ISO C++ forbids taking the address of an unqualified"
7149 " or parenthesized non-static member function to form"
7150 " a pointer to member function. Say %<&%T::%D%>",
7151 base, name);
7152 else
7153 error_at (loc,
7154 "ISO C++ forbids taking the address of an unqualified"
7155 " or parenthesized non-static member function to form"
7156 " a pointer to explicit object member function");
7157 else
7158 if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7159 permerror (loc,
7160 "ISO C++ forbids taking the address of a bound member"
7161 " function to form a pointer to member function."
7162 " Say %<&%T::%D%>",
7163 base, name);
7164 else
7165 error_at (loc,
7166 "ISO C++ forbids taking the address of a bound member"
7167 " function to form a pointer to explicit object member"
7168 " function");
7169 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7170 inform (loc,
7171 "a pointer to explicit object member function can only be "
7172 "formed with %<&%T::%D%>", base, name);
7174 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7177 /* Uninstantiated types are all functions. Taking the
7178 address of a function is a no-op, so just return the
7179 argument. */
7180 if (type_unknown_p (arg))
7181 return build1 (ADDR_EXPR, unknown_type_node, arg);
7183 if (TREE_CODE (arg) == OFFSET_REF)
7184 /* We want a pointer to member; bypass all the code for actually taking
7185 the address of something. */
7186 goto offset_ref;
7188 /* Anything not already handled and not a true memory reference
7189 is an error. */
7190 if (!FUNC_OR_METHOD_TYPE_P (argtype))
7192 cp_lvalue_kind kind = lvalue_kind (arg);
7193 if (kind == clk_none)
7195 if (complain & tf_error)
7196 lvalue_error (loc, lv_addressof);
7197 return error_mark_node;
7199 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7201 if (!(complain & tf_error))
7202 return error_mark_node;
7203 /* Make this a permerror because we used to accept it. */
7204 permerror (loc, "taking address of rvalue");
7208 if (TYPE_REF_P (argtype))
7210 tree type = build_pointer_type (TREE_TYPE (argtype));
7211 arg = build1 (CONVERT_EXPR, type, arg);
7212 return arg;
7214 else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7216 /* ARM $3.4 */
7217 /* Apparently a lot of autoconf scripts for C++ packages do this,
7218 so only complain if -Wpedantic. */
7219 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7220 pedwarn (loc, OPT_Wpedantic,
7221 "ISO C++ forbids taking address of function %<::main%>");
7222 else if (flag_pedantic_errors)
7223 return error_mark_node;
7226 /* Let &* cancel out to simplify resulting code. */
7227 if (INDIRECT_REF_P (arg))
7229 arg = TREE_OPERAND (arg, 0);
7230 if (TYPE_REF_P (TREE_TYPE (arg)))
7232 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7233 arg = build1 (CONVERT_EXPR, type, arg);
7235 else
7236 /* Don't let this be an lvalue. */
7237 arg = rvalue (arg);
7238 return arg;
7241 /* Handle complex lvalues (when permitted)
7242 by reduction to simpler cases. */
7243 val = unary_complex_lvalue (ADDR_EXPR, arg);
7244 if (val != 0)
7245 return val;
7247 switch (TREE_CODE (arg))
7249 CASE_CONVERT:
7250 case FLOAT_EXPR:
7251 case FIX_TRUNC_EXPR:
7252 /* We should have handled this above in the lvalue_kind check. */
7253 gcc_unreachable ();
7254 break;
7256 case BASELINK:
7257 arg = BASELINK_FUNCTIONS (arg);
7258 /* Fall through. */
7260 case OVERLOAD:
7261 arg = OVL_FIRST (arg);
7262 break;
7264 case OFFSET_REF:
7265 offset_ref:
7266 /* Turn a reference to a non-static data member into a
7267 pointer-to-member. */
7269 tree type;
7270 tree t;
7272 gcc_assert (PTRMEM_OK_P (arg));
7274 t = TREE_OPERAND (arg, 1);
7275 if (TYPE_REF_P (TREE_TYPE (t)))
7277 if (complain & tf_error)
7278 error_at (loc,
7279 "cannot create pointer to reference member %qD", t);
7280 return error_mark_node;
7283 /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7284 if (TREE_CODE (t) == FUNCTION_DECL
7285 && !DECL_PURE_VIRTUAL_P (t)
7286 && !mark_used (t, complain) && !(complain & tf_error))
7287 return error_mark_node;
7289 /* Pull out the function_decl for a single xobj member function, and
7290 let the rest of this function handle it. This is similar to how
7291 static member functions are handled in the BASELINK case above. */
7292 if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7294 arg = t;
7295 break;
7298 type = build_ptrmem_type (context_for_name_lookup (t),
7299 TREE_TYPE (t));
7300 t = make_ptrmem_cst (type, t);
7301 return t;
7304 default:
7305 break;
7308 if (argtype != error_mark_node)
7309 argtype = build_pointer_type (argtype);
7311 if (bitfield_p (arg))
7313 if (complain & tf_error)
7314 error_at (loc, "attempt to take address of bit-field");
7315 return error_mark_node;
7318 /* In a template, we are processing a non-dependent expression
7319 so we can just form an ADDR_EXPR with the correct type. */
7320 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7322 if (!mark_single_function (arg, complain))
7323 return error_mark_node;
7324 val = build_address (arg);
7325 if (TREE_CODE (arg) == OFFSET_REF)
7326 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7328 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7330 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7332 /* We can only get here with a single static member
7333 function. */
7334 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7335 && DECL_STATIC_FUNCTION_P (fn));
7336 if (!mark_used (fn, complain) && !(complain & tf_error))
7337 return error_mark_node;
7338 val = build_address (fn);
7339 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7340 /* Do not lose object's side effects. */
7341 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7342 TREE_OPERAND (arg, 0), val);
7344 else
7346 tree object = TREE_OPERAND (arg, 0);
7347 tree field = TREE_OPERAND (arg, 1);
7348 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7349 (TREE_TYPE (object), decl_type_context (field)));
7350 val = build_address (arg);
7353 if (TYPE_PTR_P (argtype)
7354 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7356 build_ptrmemfunc_type (argtype);
7357 val = build_ptrmemfunc (argtype, val, 0,
7358 /*c_cast_p=*/false,
7359 complain);
7362 /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7363 if (TREE_CODE (val) == ADDR_EXPR
7364 && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7365 SET_EXPR_LOCATION (val, input_location);
7367 return val;
7370 /* Take the address of ARG if it has one, even if it's an rvalue. */
7372 tree
7373 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7375 return cp_build_addr_expr_1 (arg, 0, complain);
7378 /* Take the address of ARG, but only if it's an lvalue. */
7380 static tree
7381 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7383 return cp_build_addr_expr_1 (arg, 1, complain);
7386 /* C++: Must handle pointers to members.
7388 Perhaps type instantiation should be extended to handle conversion
7389 from aggregates to types we don't yet know we want? (Or are those
7390 cases typically errors which should be reported?)
7392 NOCONVERT suppresses the default promotions (such as from short to int). */
7394 tree
7395 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7396 tsubst_flags_t complain)
7398 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7399 tree arg = xarg;
7400 location_t location = cp_expr_loc_or_input_loc (arg);
7401 tree argtype = 0;
7402 tree eptype = NULL_TREE;
7403 const char *errstring = NULL;
7404 tree val;
7405 const char *invalid_op_diag;
7407 if (!arg || error_operand_p (arg))
7408 return error_mark_node;
7410 arg = resolve_nondeduced_context (arg, complain);
7412 if ((invalid_op_diag
7413 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7414 ? CONVERT_EXPR
7415 : code),
7416 TREE_TYPE (arg))))
7418 if (complain & tf_error)
7419 error (invalid_op_diag);
7420 return error_mark_node;
7423 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7425 eptype = TREE_TYPE (arg);
7426 arg = TREE_OPERAND (arg, 0);
7429 switch (code)
7431 case UNARY_PLUS_EXPR:
7432 case NEGATE_EXPR:
7434 int flags = WANT_ARITH | WANT_ENUM;
7435 /* Unary plus (but not unary minus) is allowed on pointers. */
7436 if (code == UNARY_PLUS_EXPR)
7437 flags |= WANT_POINTER;
7438 arg = build_expr_type_conversion (flags, arg, true);
7439 if (!arg)
7440 errstring = (code == NEGATE_EXPR
7441 ? _("wrong type argument to unary minus")
7442 : _("wrong type argument to unary plus"));
7443 else
7445 if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7446 arg = cp_perform_integral_promotions (arg, complain);
7448 /* Make sure the result is not an lvalue: a unary plus or minus
7449 expression is always a rvalue. */
7450 arg = rvalue (arg);
7453 break;
7455 case BIT_NOT_EXPR:
7456 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7458 code = CONJ_EXPR;
7459 if (!noconvert)
7461 arg = cp_default_conversion (arg, complain);
7462 if (arg == error_mark_node)
7463 return error_mark_node;
7466 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7467 | WANT_VECTOR_OR_COMPLEX,
7468 arg, true)))
7469 errstring = _("wrong type argument to bit-complement");
7470 else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7472 /* Warn if the expression has boolean value. */
7473 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7474 && (complain & tf_warning)
7475 && warning_at (location, OPT_Wbool_operation,
7476 "%<~%> on an expression of type %<bool%>"))
7477 inform (location, "did you mean to use logical not (%<!%>)?");
7478 arg = cp_perform_integral_promotions (arg, complain);
7480 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7481 arg = mark_rvalue_use (arg);
7482 break;
7484 case ABS_EXPR:
7485 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7486 errstring = _("wrong type argument to abs");
7487 else if (!noconvert)
7489 arg = cp_default_conversion (arg, complain);
7490 if (arg == error_mark_node)
7491 return error_mark_node;
7493 break;
7495 case CONJ_EXPR:
7496 /* Conjugating a real value is a no-op, but allow it anyway. */
7497 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7498 errstring = _("wrong type argument to conjugation");
7499 else if (!noconvert)
7501 arg = cp_default_conversion (arg, complain);
7502 if (arg == error_mark_node)
7503 return error_mark_node;
7505 break;
7507 case TRUTH_NOT_EXPR:
7508 if (gnu_vector_type_p (TREE_TYPE (arg)))
7509 return cp_build_binary_op (input_location, EQ_EXPR, arg,
7510 build_zero_cst (TREE_TYPE (arg)), complain);
7511 arg = perform_implicit_conversion (boolean_type_node, arg,
7512 complain);
7513 if (arg != error_mark_node)
7515 if (processing_template_decl)
7516 return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7517 val = invert_truthvalue_loc (location, arg);
7518 if (obvalue_p (val))
7519 val = non_lvalue_loc (location, val);
7520 return val;
7522 errstring = _("in argument to unary !");
7523 break;
7525 case NOP_EXPR:
7526 break;
7528 case REALPART_EXPR:
7529 case IMAGPART_EXPR:
7530 val = build_real_imag_expr (input_location, code, arg);
7531 if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7532 val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7533 TREE_TYPE (eptype), val);
7534 return val;
7536 case PREINCREMENT_EXPR:
7537 case POSTINCREMENT_EXPR:
7538 case PREDECREMENT_EXPR:
7539 case POSTDECREMENT_EXPR:
7540 /* Handle complex lvalues (when permitted)
7541 by reduction to simpler cases. */
7543 val = unary_complex_lvalue (code, arg);
7544 if (val != 0)
7545 goto return_build_unary_op;
7547 arg = mark_lvalue_use (arg);
7549 /* Increment or decrement the real part of the value,
7550 and don't change the imaginary part. */
7551 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7553 tree real, imag;
7555 arg = cp_stabilize_reference (arg);
7556 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7557 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7558 real = cp_build_unary_op (code, real, true, complain);
7559 if (real == error_mark_node || imag == error_mark_node)
7560 return error_mark_node;
7561 val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7562 goto return_build_unary_op;
7565 /* Report invalid types. */
7567 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7568 arg, true)))
7570 if (code == PREINCREMENT_EXPR)
7571 errstring = _("no pre-increment operator for type");
7572 else if (code == POSTINCREMENT_EXPR)
7573 errstring = _("no post-increment operator for type");
7574 else if (code == PREDECREMENT_EXPR)
7575 errstring = _("no pre-decrement operator for type");
7576 else
7577 errstring = _("no post-decrement operator for type");
7578 break;
7580 else if (arg == error_mark_node)
7581 return error_mark_node;
7583 /* Report something read-only. */
7585 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7586 || TREE_READONLY (arg))
7588 if (complain & tf_error)
7589 cxx_readonly_error (location, arg,
7590 ((code == PREINCREMENT_EXPR
7591 || code == POSTINCREMENT_EXPR)
7592 ? lv_increment : lv_decrement));
7593 else
7594 return error_mark_node;
7598 tree inc;
7599 tree declared_type = unlowered_expr_type (arg);
7601 argtype = TREE_TYPE (arg);
7603 /* ARM $5.2.5 last annotation says this should be forbidden. */
7604 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7606 if (complain & tf_error)
7607 permerror (location, (code == PREINCREMENT_EXPR
7608 || code == POSTINCREMENT_EXPR)
7609 ? G_("ISO C++ forbids incrementing an enum")
7610 : G_("ISO C++ forbids decrementing an enum"));
7611 else
7612 return error_mark_node;
7615 /* Compute the increment. */
7617 if (TYPE_PTR_P (argtype))
7619 tree type = complete_type (TREE_TYPE (argtype));
7621 if (!COMPLETE_OR_VOID_TYPE_P (type))
7623 if (complain & tf_error)
7624 error_at (location, ((code == PREINCREMENT_EXPR
7625 || code == POSTINCREMENT_EXPR))
7626 ? G_("cannot increment a pointer to incomplete "
7627 "type %qT")
7628 : G_("cannot decrement a pointer to incomplete "
7629 "type %qT"),
7630 TREE_TYPE (argtype));
7631 else
7632 return error_mark_node;
7634 else if (!TYPE_PTROB_P (argtype))
7636 if (complain & tf_error)
7637 pedwarn (location, OPT_Wpointer_arith,
7638 (code == PREINCREMENT_EXPR
7639 || code == POSTINCREMENT_EXPR)
7640 ? G_("ISO C++ forbids incrementing a pointer "
7641 "of type %qT")
7642 : G_("ISO C++ forbids decrementing a pointer "
7643 "of type %qT"),
7644 argtype);
7645 else
7646 return error_mark_node;
7648 else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7649 TREE_TYPE (argtype),
7650 !(complain & tf_error)))
7651 return error_mark_node;
7653 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7655 else
7656 inc = VECTOR_TYPE_P (argtype)
7657 ? build_one_cst (argtype)
7658 : integer_one_node;
7660 inc = cp_convert (argtype, inc, complain);
7662 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7663 need to ask Objective-C to build the increment or decrement
7664 expression for it. */
7665 if (objc_is_property_ref (arg))
7666 return objc_build_incr_expr_for_property_ref (input_location, code,
7667 arg, inc);
7669 /* Complain about anything else that is not a true lvalue. */
7670 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7671 || code == POSTINCREMENT_EXPR)
7672 ? lv_increment : lv_decrement),
7673 complain))
7674 return error_mark_node;
7676 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7677 prefix ++ and -- expressions of volatile-qualified arithmetic
7678 and pointer types are deprecated." */
7679 if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7680 && (complain & tf_warning))
7681 warning_at (location, OPT_Wvolatile,
7682 "%qs expression of %<volatile%>-qualified type is "
7683 "deprecated",
7684 ((code == PREINCREMENT_EXPR
7685 || code == POSTINCREMENT_EXPR)
7686 ? "++" : "--"));
7688 /* Forbid using -- or ++ in C++17 on `bool'. */
7689 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7691 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7693 if (complain & tf_error)
7694 error_at (location,
7695 "use of an operand of type %qT in %<operator--%> "
7696 "is forbidden", boolean_type_node);
7697 return error_mark_node;
7699 else
7701 if (cxx_dialect >= cxx17)
7703 if (complain & tf_error)
7704 error_at (location,
7705 "use of an operand of type %qT in "
7706 "%<operator++%> is forbidden in C++17",
7707 boolean_type_node);
7708 return error_mark_node;
7710 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7711 else if (complain & tf_warning)
7712 warning_at (location, OPT_Wdeprecated,
7713 "use of an operand of type %qT "
7714 "in %<operator++%> is deprecated",
7715 boolean_type_node);
7717 val = boolean_increment (code, arg);
7719 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7720 /* An rvalue has no cv-qualifiers. */
7721 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7722 else
7723 val = build2 (code, TREE_TYPE (arg), arg, inc);
7725 TREE_SIDE_EFFECTS (val) = 1;
7726 goto return_build_unary_op;
7729 case ADDR_EXPR:
7730 /* Note that this operation never does default_conversion
7731 regardless of NOCONVERT. */
7732 return cp_build_addr_expr (arg, complain);
7734 default:
7735 break;
7738 if (!errstring)
7740 if (argtype == 0)
7741 argtype = TREE_TYPE (arg);
7742 val = build1 (code, argtype, arg);
7743 return_build_unary_op:
7744 if (eptype)
7745 val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7746 return val;
7749 if (complain & tf_error)
7750 error_at (location, "%s", errstring);
7751 return error_mark_node;
7754 /* Hook for the c-common bits that build a unary op. */
7755 tree
7756 build_unary_op (location_t /*location*/,
7757 enum tree_code code, tree xarg, bool noconvert)
7759 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7762 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7763 so that it is a valid lvalue even for GENERIC by replacing
7764 (lhs = rhs) with ((lhs = rhs), lhs)
7765 (--lhs) with ((--lhs), lhs)
7766 (++lhs) with ((++lhs), lhs)
7767 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7768 that it can be evaluated multiple times. */
7770 tree
7771 genericize_compound_lvalue (tree lvalue)
7773 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7774 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7775 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7776 TREE_OPERAND (lvalue, 1));
7777 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7778 lvalue, TREE_OPERAND (lvalue, 0));
7781 /* Apply unary lvalue-demanding operator CODE to the expression ARG
7782 for certain kinds of expressions which are not really lvalues
7783 but which we can accept as lvalues.
7785 If ARG is not a kind of expression we can handle, return
7786 NULL_TREE. */
7788 tree
7789 unary_complex_lvalue (enum tree_code code, tree arg)
7791 /* Inside a template, making these kinds of adjustments is
7792 pointless; we are only concerned with the type of the
7793 expression. */
7794 if (processing_template_decl)
7795 return NULL_TREE;
7797 /* Handle (a, b) used as an "lvalue". */
7798 if (TREE_CODE (arg) == COMPOUND_EXPR)
7800 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7801 tf_warning_or_error);
7802 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7803 TREE_OPERAND (arg, 0), real_result);
7806 /* Handle (a ? b : c) used as an "lvalue". */
7807 if (TREE_CODE (arg) == COND_EXPR
7808 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7809 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7811 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7812 if (TREE_CODE (arg) == MODIFY_EXPR
7813 || TREE_CODE (arg) == PREINCREMENT_EXPR
7814 || TREE_CODE (arg) == PREDECREMENT_EXPR)
7815 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7817 if (code != ADDR_EXPR)
7818 return NULL_TREE;
7820 /* Handle (a = b) used as an "lvalue" for `&'. */
7821 if (TREE_CODE (arg) == MODIFY_EXPR
7822 || TREE_CODE (arg) == INIT_EXPR)
7824 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7825 tf_warning_or_error);
7826 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7827 arg, real_result);
7828 suppress_warning (arg /* What warning? */);
7829 return arg;
7832 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7833 || TREE_CODE (arg) == OFFSET_REF)
7834 return NULL_TREE;
7836 /* We permit compiler to make function calls returning
7837 objects of aggregate type look like lvalues. */
7839 tree targ = arg;
7841 if (TREE_CODE (targ) == SAVE_EXPR)
7842 targ = TREE_OPERAND (targ, 0);
7844 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7846 if (TREE_CODE (arg) == SAVE_EXPR)
7847 targ = arg;
7848 else
7849 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7850 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7853 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
7854 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
7855 TREE_OPERAND (targ, 0), current_function_decl, NULL);
7858 /* Don't let anything else be handled specially. */
7859 return NULL_TREE;
7862 /* Mark EXP saying that we need to be able to take the
7863 address of it; it should not be allocated in a register.
7864 Value is true if successful. ARRAY_REF_P is true if this
7865 is for ARRAY_REF construction - in that case we don't want
7866 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7867 it is fine to use ARRAY_REFs for vector subscripts on vector
7868 register variables.
7870 C++: we do not allow `current_class_ptr' to be addressable. */
7872 bool
7873 cxx_mark_addressable (tree exp, bool array_ref_p)
7875 tree x = exp;
7877 while (1)
7878 switch (TREE_CODE (x))
7880 case VIEW_CONVERT_EXPR:
7881 if (array_ref_p
7882 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7883 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
7884 return true;
7885 x = TREE_OPERAND (x, 0);
7886 break;
7888 case COMPONENT_REF:
7889 if (bitfield_p (x))
7890 error ("attempt to take address of bit-field");
7891 /* FALLTHRU */
7892 case ADDR_EXPR:
7893 case ARRAY_REF:
7894 case REALPART_EXPR:
7895 case IMAGPART_EXPR:
7896 x = TREE_OPERAND (x, 0);
7897 break;
7899 case PARM_DECL:
7900 if (x == current_class_ptr)
7902 error ("cannot take the address of %<this%>, which is an rvalue expression");
7903 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
7904 return true;
7906 /* Fall through. */
7908 case VAR_DECL:
7909 /* Caller should not be trying to mark initialized
7910 constant fields addressable. */
7911 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
7912 || DECL_IN_AGGR_P (x) == 0
7913 || TREE_STATIC (x)
7914 || DECL_EXTERNAL (x));
7915 /* Fall through. */
7917 case RESULT_DECL:
7918 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
7919 && !DECL_ARTIFICIAL (x))
7921 if (VAR_P (x) && DECL_HARD_REGISTER (x))
7923 error
7924 ("address of explicit register variable %qD requested", x);
7925 return false;
7927 else if (extra_warnings)
7928 warning
7929 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
7931 TREE_ADDRESSABLE (x) = 1;
7932 return true;
7934 case CONST_DECL:
7935 case FUNCTION_DECL:
7936 TREE_ADDRESSABLE (x) = 1;
7937 return true;
7939 case CONSTRUCTOR:
7940 TREE_ADDRESSABLE (x) = 1;
7941 return true;
7943 case TARGET_EXPR:
7944 TREE_ADDRESSABLE (x) = 1;
7945 cxx_mark_addressable (TREE_OPERAND (x, 0));
7946 return true;
7948 default:
7949 return true;
7953 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7955 tree
7956 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
7957 tsubst_flags_t complain)
7959 tree orig_ifexp = ifexp;
7960 tree orig_op1 = op1;
7961 tree orig_op2 = op2;
7962 tree expr;
7964 if (processing_template_decl)
7966 /* The standard says that the expression is type-dependent if
7967 IFEXP is type-dependent, even though the eventual type of the
7968 expression doesn't dependent on IFEXP. */
7969 if (type_dependent_expression_p (ifexp)
7970 /* As a GNU extension, the middle operand may be omitted. */
7971 || (op1 && type_dependent_expression_p (op1))
7972 || type_dependent_expression_p (op2))
7973 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
7976 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
7977 if (processing_template_decl && expr != error_mark_node)
7979 tree min = build_min_non_dep (COND_EXPR, expr,
7980 orig_ifexp, orig_op1, orig_op2);
7981 expr = convert_from_reference (min);
7983 return expr;
7986 /* Given a list of expressions, return a compound expression
7987 that performs them all and returns the value of the last of them. */
7989 tree
7990 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
7991 tsubst_flags_t complain)
7993 tree expr = TREE_VALUE (list);
7995 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7996 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
7998 if (complain & tf_error)
7999 pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8000 "list-initializer for non-class type must not "
8001 "be parenthesized");
8002 else
8003 return error_mark_node;
8006 if (TREE_CHAIN (list))
8008 if (complain & tf_error)
8009 switch (exp)
8011 case ELK_INIT:
8012 permerror (input_location, "expression list treated as compound "
8013 "expression in initializer");
8014 break;
8015 case ELK_MEM_INIT:
8016 permerror (input_location, "expression list treated as compound "
8017 "expression in mem-initializer");
8018 break;
8019 case ELK_FUNC_CAST:
8020 permerror (input_location, "expression list treated as compound "
8021 "expression in functional cast");
8022 break;
8023 default:
8024 gcc_unreachable ();
8026 else
8027 return error_mark_node;
8029 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8030 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8031 expr, TREE_VALUE (list), NULL_TREE,
8032 complain);
8035 return expr;
8038 /* Like build_x_compound_expr_from_list, but using a VEC. */
8040 tree
8041 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8042 tsubst_flags_t complain)
8044 if (vec_safe_is_empty (vec))
8045 return NULL_TREE;
8046 else if (vec->length () == 1)
8047 return (*vec)[0];
8048 else
8050 tree expr;
8051 unsigned int ix;
8052 tree t;
8054 if (msg != NULL)
8056 if (complain & tf_error)
8057 permerror (input_location,
8058 "%s expression list treated as compound expression",
8059 msg);
8060 else
8061 return error_mark_node;
8064 expr = (*vec)[0];
8065 for (ix = 1; vec->iterate (ix, &t); ++ix)
8066 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8067 t, NULL_TREE, complain);
8069 return expr;
8073 /* Handle overloading of the ',' operator when needed. */
8075 tree
8076 build_x_compound_expr (location_t loc, tree op1, tree op2,
8077 tree lookups, tsubst_flags_t complain)
8079 tree result;
8080 tree orig_op1 = op1;
8081 tree orig_op2 = op2;
8082 tree overload = NULL_TREE;
8084 if (processing_template_decl)
8086 if (type_dependent_expression_p (op1)
8087 || type_dependent_expression_p (op2))
8089 result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8090 TREE_TYPE (result)
8091 = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8092 return result;
8096 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8097 NULL_TREE, lookups, &overload, complain);
8098 if (!result)
8099 result = cp_build_compound_expr (op1, op2, complain);
8101 if (processing_template_decl && result != error_mark_node)
8103 if (overload != NULL_TREE)
8104 return (build_min_non_dep_op_overload
8105 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8107 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8110 return result;
8113 /* Like cp_build_compound_expr, but for the c-common bits. */
8115 tree
8116 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8118 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8121 /* Build a compound expression. */
8123 tree
8124 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8126 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8128 if (lhs == error_mark_node || rhs == error_mark_node)
8129 return error_mark_node;
8131 if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8132 lhs = TREE_OPERAND (lhs, 0);
8133 tree eptype = NULL_TREE;
8134 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8136 eptype = TREE_TYPE (rhs);
8137 rhs = TREE_OPERAND (rhs, 0);
8140 if (TREE_CODE (rhs) == TARGET_EXPR)
8142 /* If the rhs is a TARGET_EXPR, then build the compound
8143 expression inside the target_expr's initializer. This
8144 helps the compiler to eliminate unnecessary temporaries. */
8145 tree init = TREE_OPERAND (rhs, 1);
8147 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8148 TREE_OPERAND (rhs, 1) = init;
8150 if (eptype)
8151 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8152 return rhs;
8155 if (type_unknown_p (rhs))
8157 if (complain & tf_error)
8158 error_at (cp_expr_loc_or_input_loc (rhs),
8159 "no context to resolve type of %qE", rhs);
8160 return error_mark_node;
8163 tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8164 if (eptype)
8165 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8166 return ret;
8169 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8170 casts away constness. CAST gives the type of cast. Returns true
8171 if the cast is ill-formed, false if it is well-formed.
8173 ??? This function warns for casting away any qualifier not just
8174 const. We would like to specify exactly what qualifiers are casted
8175 away.
8178 static bool
8179 check_for_casting_away_constness (location_t loc, tree src_type,
8180 tree dest_type, enum tree_code cast,
8181 tsubst_flags_t complain)
8183 /* C-style casts are allowed to cast away constness. With
8184 WARN_CAST_QUAL, we still want to issue a warning. */
8185 if (cast == CAST_EXPR && !warn_cast_qual)
8186 return false;
8188 if (!casts_away_constness (src_type, dest_type, complain))
8189 return false;
8191 switch (cast)
8193 case CAST_EXPR:
8194 if (complain & tf_warning)
8195 warning_at (loc, OPT_Wcast_qual,
8196 "cast from type %qT to type %qT casts away qualifiers",
8197 src_type, dest_type);
8198 return false;
8200 case STATIC_CAST_EXPR:
8201 if (complain & tf_error)
8202 error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8203 "away qualifiers",
8204 src_type, dest_type);
8205 return true;
8207 case REINTERPRET_CAST_EXPR:
8208 if (complain & tf_error)
8209 error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8210 "casts away qualifiers",
8211 src_type, dest_type);
8212 return true;
8214 default:
8215 gcc_unreachable();
8219 /* Warns if the cast from expression EXPR to type TYPE is useless. */
8220 void
8221 maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8222 tsubst_flags_t complain)
8224 if (warn_useless_cast
8225 && complain & tf_warning)
8227 if (TYPE_REF_P (type)
8228 ? ((TYPE_REF_IS_RVALUE (type)
8229 ? xvalue_p (expr) : lvalue_p (expr))
8230 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8231 /* Don't warn when converting a class object to a non-reference type,
8232 because that's a common way to create a temporary. */
8233 : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8234 warning_at (loc, OPT_Wuseless_cast,
8235 "useless cast to type %q#T", type);
8239 /* Warns if the cast ignores cv-qualifiers on TYPE. */
8240 static void
8241 maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8242 tsubst_flags_t complain)
8244 if (warn_ignored_qualifiers
8245 && complain & tf_warning
8246 && !CLASS_TYPE_P (type)
8247 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8248 warning_at (loc, OPT_Wignored_qualifiers,
8249 "type qualifiers ignored on cast result type");
8252 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8253 (another pointer-to-member type in the same hierarchy) and return
8254 the converted expression. If ALLOW_INVERSE_P is permitted, a
8255 pointer-to-derived may be converted to pointer-to-base; otherwise,
8256 only the other direction is permitted. If C_CAST_P is true, this
8257 conversion is taking place as part of a C-style cast. */
8259 tree
8260 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8261 bool c_cast_p, tsubst_flags_t complain)
8263 if (same_type_p (type, TREE_TYPE (expr)))
8264 return expr;
8266 if (TYPE_PTRDATAMEM_P (type))
8268 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8269 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8270 tree delta = (get_delta_difference
8271 (obase, nbase,
8272 allow_inverse_p, c_cast_p, complain));
8274 if (delta == error_mark_node)
8275 return error_mark_node;
8277 if (!same_type_p (obase, nbase))
8279 if (TREE_CODE (expr) == PTRMEM_CST)
8280 expr = cplus_expand_constant (expr);
8282 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8283 build_int_cst (TREE_TYPE (expr), -1),
8284 complain);
8285 tree op1 = build_nop (ptrdiff_type_node, expr);
8286 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8287 complain);
8289 expr = fold_build3_loc (input_location,
8290 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8293 return build_nop (type, expr);
8295 else
8296 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8297 allow_inverse_p, c_cast_p, complain);
8300 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8301 this static_cast is being attempted as one of the possible casts
8302 allowed by a C-style cast. (In that case, accessibility of base
8303 classes is not considered, and it is OK to cast away
8304 constness.) Return the result of the cast. *VALID_P is set to
8305 indicate whether or not the cast was valid. */
8307 static tree
8308 build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8309 bool *valid_p, tsubst_flags_t complain)
8311 tree intype;
8312 tree result;
8313 cp_lvalue_kind clk;
8315 /* Assume the cast is valid. */
8316 *valid_p = true;
8318 intype = unlowered_expr_type (expr);
8320 /* Save casted types in the function's used types hash table. */
8321 used_types_insert (type);
8323 /* A prvalue of non-class type is cv-unqualified. */
8324 if (!CLASS_TYPE_P (type))
8325 type = cv_unqualified (type);
8327 /* [expr.static.cast]
8329 An lvalue of type "cv1 B", where B is a class type, can be cast
8330 to type "reference to cv2 D", where D is a class derived (clause
8331 _class.derived_) from B, if a valid standard conversion from
8332 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8333 same cv-qualification as, or greater cv-qualification than, cv1,
8334 and B is not a virtual base class of D. */
8335 /* We check this case before checking the validity of "TYPE t =
8336 EXPR;" below because for this case:
8338 struct B {};
8339 struct D : public B { D(const B&); };
8340 extern B& b;
8341 void f() { static_cast<const D&>(b); }
8343 we want to avoid constructing a new D. The standard is not
8344 completely clear about this issue, but our interpretation is
8345 consistent with other compilers. */
8346 if (TYPE_REF_P (type)
8347 && CLASS_TYPE_P (TREE_TYPE (type))
8348 && CLASS_TYPE_P (intype)
8349 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8350 && DERIVED_FROM_P (intype, TREE_TYPE (type))
8351 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8352 build_pointer_type (TYPE_MAIN_VARIANT
8353 (TREE_TYPE (type))),
8354 complain)
8355 && (c_cast_p
8356 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8358 tree base;
8360 if (processing_template_decl)
8361 return expr;
8363 /* There is a standard conversion from "D*" to "B*" even if "B"
8364 is ambiguous or inaccessible. If this is really a
8365 static_cast, then we check both for inaccessibility and
8366 ambiguity. However, if this is a static_cast being performed
8367 because the user wrote a C-style cast, then accessibility is
8368 not considered. */
8369 base = lookup_base (TREE_TYPE (type), intype,
8370 c_cast_p ? ba_unique : ba_check,
8371 NULL, complain);
8372 expr = cp_build_addr_expr (expr, complain);
8374 if (sanitize_flags_p (SANITIZE_VPTR))
8376 tree ubsan_check
8377 = cp_ubsan_maybe_instrument_downcast (loc, type,
8378 intype, expr);
8379 if (ubsan_check)
8380 expr = ubsan_check;
8383 /* Convert from "B*" to "D*". This function will check that "B"
8384 is not a virtual base of "D". Even if we don't have a guarantee
8385 that expr is NULL, if the static_cast is to a reference type,
8386 it is UB if it would be NULL, so omit the non-NULL check. */
8387 expr = build_base_path (MINUS_EXPR, expr, base,
8388 /*nonnull=*/flag_delete_null_pointer_checks,
8389 complain);
8391 /* Convert the pointer to a reference -- but then remember that
8392 there are no expressions with reference type in C++.
8394 We call rvalue so that there's an actual tree code
8395 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8396 is a variable with the same type, the conversion would get folded
8397 away, leaving just the variable and causing lvalue_kind to give
8398 the wrong answer. */
8399 expr = cp_fold_convert (type, expr);
8401 /* When -fsanitize=null, make sure to diagnose reference binding to
8402 NULL even when the reference is converted to pointer later on. */
8403 if (sanitize_flags_p (SANITIZE_NULL)
8404 && TREE_CODE (expr) == COND_EXPR
8405 && TREE_OPERAND (expr, 2)
8406 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8407 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8408 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8410 return convert_from_reference (rvalue (expr));
8413 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8414 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8415 if (TYPE_REF_P (type)
8416 && TYPE_REF_IS_RVALUE (type)
8417 && (clk = real_lvalue_p (expr))
8418 && reference_compatible_p (TREE_TYPE (type), intype)
8419 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8421 if (processing_template_decl)
8422 return expr;
8423 if (clk == clk_ordinary)
8425 /* Handle the (non-bit-field) lvalue case here by casting to
8426 lvalue reference and then changing it to an rvalue reference.
8427 Casting an xvalue to rvalue reference will be handled by the
8428 main code path. */
8429 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8430 result = (perform_direct_initialization_if_possible
8431 (lref, expr, c_cast_p, complain));
8432 result = build1 (NON_LVALUE_EXPR, type, result);
8433 return convert_from_reference (result);
8435 else
8436 /* For a bit-field or packed field, bind to a temporary. */
8437 expr = rvalue (expr);
8440 /* Resolve overloaded address here rather than once in
8441 implicit_conversion and again in the inverse code below. */
8442 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8444 expr = instantiate_type (type, expr, complain);
8445 intype = TREE_TYPE (expr);
8448 /* [expr.static.cast]
8450 Any expression can be explicitly converted to type cv void. */
8451 if (VOID_TYPE_P (type))
8453 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8454 expr = TREE_OPERAND (expr, 0);
8455 return convert_to_void (expr, ICV_CAST, complain);
8458 /* [class.abstract]
8459 An abstract class shall not be used ... as the type of an explicit
8460 conversion. */
8461 if (abstract_virtuals_error (ACU_CAST, type, complain))
8462 return error_mark_node;
8464 /* [expr.static.cast]
8466 An expression e can be explicitly converted to a type T using a
8467 static_cast of the form static_cast<T>(e) if the declaration T
8468 t(e);" is well-formed, for some invented temporary variable
8469 t. */
8470 result = perform_direct_initialization_if_possible (type, expr,
8471 c_cast_p, complain);
8472 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8473 which initialize the first element of the aggregate. We need to handle
8474 the array case specifically. */
8475 if (result == NULL_TREE
8476 && cxx_dialect >= cxx20
8477 && TREE_CODE (type) == ARRAY_TYPE)
8479 /* Create { EXPR } and perform direct-initialization from it. */
8480 tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8481 CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8482 CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8483 result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8484 complain);
8486 if (result)
8488 if (processing_template_decl)
8489 return expr;
8491 result = convert_from_reference (result);
8493 /* [expr.static.cast]
8495 If T is a reference type, the result is an lvalue; otherwise,
8496 the result is an rvalue. */
8497 if (!TYPE_REF_P (type))
8499 result = rvalue (result);
8501 if (result == expr && SCALAR_TYPE_P (type))
8502 /* Leave some record of the cast. */
8503 result = build_nop (type, expr);
8505 return result;
8508 /* [expr.static.cast]
8510 The inverse of any standard conversion sequence (clause _conv_),
8511 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8512 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8513 (_conv.bool_) conversions, can be performed explicitly using
8514 static_cast subject to the restriction that the explicit
8515 conversion does not cast away constness (_expr.const.cast_), and
8516 the following additional rules for specific cases: */
8517 /* For reference, the conversions not excluded are: integral
8518 promotions, floating-point promotion, integral conversions,
8519 floating-point conversions, floating-integral conversions,
8520 pointer conversions, and pointer to member conversions. */
8521 /* DR 128
8523 A value of integral _or enumeration_ type can be explicitly
8524 converted to an enumeration type. */
8525 /* The effect of all that is that any conversion between any two
8526 types which are integral, floating, or enumeration types can be
8527 performed. */
8528 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8529 || SCALAR_FLOAT_TYPE_P (type))
8530 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8531 || SCALAR_FLOAT_TYPE_P (intype)))
8533 if (processing_template_decl)
8534 return expr;
8535 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8536 expr = TREE_OPERAND (expr, 0);
8537 /* [expr.static.cast]: "If the value is not a bit-field, the result
8538 refers to the object or the specified base class subobject thereof;
8539 otherwise, the lvalue-to-rvalue conversion is applied to the
8540 bit-field and the resulting prvalue is used as the operand of the
8541 static_cast." There are no prvalue bit-fields; the l-to-r conversion
8542 will give us an object of the underlying type of the bit-field. */
8543 expr = decay_conversion (expr, complain);
8544 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8547 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8548 && CLASS_TYPE_P (TREE_TYPE (type))
8549 && CLASS_TYPE_P (TREE_TYPE (intype))
8550 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8551 (TREE_TYPE (intype))),
8552 build_pointer_type (TYPE_MAIN_VARIANT
8553 (TREE_TYPE (type))),
8554 complain))
8556 tree base;
8558 if (processing_template_decl)
8559 return expr;
8561 if (!c_cast_p
8562 && check_for_casting_away_constness (loc, intype, type,
8563 STATIC_CAST_EXPR,
8564 complain))
8565 return error_mark_node;
8566 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8567 c_cast_p ? ba_unique : ba_check,
8568 NULL, complain);
8569 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8570 complain);
8572 if (sanitize_flags_p (SANITIZE_VPTR))
8574 tree ubsan_check
8575 = cp_ubsan_maybe_instrument_downcast (loc, type,
8576 intype, expr);
8577 if (ubsan_check)
8578 expr = ubsan_check;
8581 return cp_fold_convert (type, expr);
8584 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8585 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8587 tree c1;
8588 tree c2;
8589 tree t1;
8590 tree t2;
8592 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8593 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8595 if (TYPE_PTRDATAMEM_P (type))
8597 t1 = (build_ptrmem_type
8598 (c1,
8599 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8600 t2 = (build_ptrmem_type
8601 (c2,
8602 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8604 else
8606 t1 = intype;
8607 t2 = type;
8609 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8611 if (!c_cast_p
8612 && check_for_casting_away_constness (loc, intype, type,
8613 STATIC_CAST_EXPR,
8614 complain))
8615 return error_mark_node;
8616 if (processing_template_decl)
8617 return expr;
8618 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8619 c_cast_p, complain);
8623 /* [expr.static.cast]
8625 An rvalue of type "pointer to cv void" can be explicitly
8626 converted to a pointer to object type. A value of type pointer
8627 to object converted to "pointer to cv void" and back to the
8628 original pointer type will have its original value. */
8629 if (TYPE_PTR_P (intype)
8630 && VOID_TYPE_P (TREE_TYPE (intype))
8631 && TYPE_PTROB_P (type))
8633 if (!c_cast_p
8634 && check_for_casting_away_constness (loc, intype, type,
8635 STATIC_CAST_EXPR,
8636 complain))
8637 return error_mark_node;
8638 if (processing_template_decl)
8639 return expr;
8640 return build_nop (type, expr);
8643 *valid_p = false;
8644 return error_mark_node;
8647 /* Return an expression representing static_cast<TYPE>(EXPR). */
8649 tree
8650 build_static_cast (location_t loc, tree type, tree oexpr,
8651 tsubst_flags_t complain)
8653 tree expr = oexpr;
8654 tree result;
8655 bool valid_p;
8657 if (type == error_mark_node || expr == error_mark_node)
8658 return error_mark_node;
8660 bool dependent = (dependent_type_p (type)
8661 || type_dependent_expression_p (expr));
8662 if (dependent)
8664 tmpl:
8665 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8666 /* We don't know if it will or will not have side effects. */
8667 TREE_SIDE_EFFECTS (expr) = 1;
8668 result = convert_from_reference (expr);
8669 protected_set_expr_location (result, loc);
8670 return result;
8673 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8674 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8675 if (!TYPE_REF_P (type)
8676 && TREE_CODE (expr) == NOP_EXPR
8677 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8678 expr = TREE_OPERAND (expr, 0);
8680 result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8681 &valid_p, complain);
8682 if (valid_p)
8684 if (result != error_mark_node)
8686 maybe_warn_about_useless_cast (loc, type, expr, complain);
8687 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8689 if (processing_template_decl)
8690 goto tmpl;
8691 protected_set_expr_location (result, loc);
8692 return result;
8695 if (complain & tf_error)
8697 error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8698 TREE_TYPE (expr), type);
8699 if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8700 && CLASS_TYPE_P (TREE_TYPE (type))
8701 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8702 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8703 "class type %qT is incomplete", TREE_TYPE (type));
8704 tree expr_type = TREE_TYPE (expr);
8705 if (TYPE_PTR_P (expr_type))
8706 expr_type = TREE_TYPE (expr_type);
8707 if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8708 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8709 "class type %qT is incomplete", expr_type);
8711 return error_mark_node;
8714 /* EXPR is an expression with member function or pointer-to-member
8715 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8716 not permitted by ISO C++, but we accept it in some modes. If we
8717 are not in one of those modes, issue a diagnostic. Return the
8718 converted expression. */
8720 tree
8721 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8723 tree intype;
8724 tree decl;
8726 intype = TREE_TYPE (expr);
8727 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8728 || TREE_CODE (intype) == METHOD_TYPE);
8730 if (!(complain & tf_warning_or_error))
8731 return error_mark_node;
8733 location_t loc = cp_expr_loc_or_input_loc (expr);
8735 if (pedantic || warn_pmf2ptr)
8736 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8737 "converting from %qH to %qI", intype, type);
8739 STRIP_ANY_LOCATION_WRAPPER (expr);
8741 if (TREE_CODE (intype) == METHOD_TYPE)
8742 expr = build_addr_func (expr, complain);
8743 else if (TREE_CODE (expr) == PTRMEM_CST)
8744 expr = build_address (PTRMEM_CST_MEMBER (expr));
8745 else
8747 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8748 decl = build_address (decl);
8749 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8752 if (expr == error_mark_node)
8753 return error_mark_node;
8755 expr = build_nop (type, expr);
8756 SET_EXPR_LOCATION (expr, loc);
8757 return expr;
8760 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8761 constexpr evaluation knows to reject it. */
8763 static tree
8764 build_nop_reinterpret (tree type, tree expr)
8766 tree ret = build_nop (type, expr);
8767 if (ret != expr)
8768 REINTERPRET_CAST_P (ret) = true;
8769 return ret;
8772 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8773 If C_CAST_P is true, this reinterpret cast is being done as part of
8774 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8775 indicate whether or not reinterpret_cast was valid. */
8777 static tree
8778 build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8779 bool c_cast_p, bool *valid_p,
8780 tsubst_flags_t complain)
8782 tree intype;
8784 /* Assume the cast is invalid. */
8785 if (valid_p)
8786 *valid_p = true;
8788 if (type == error_mark_node || error_operand_p (expr))
8789 return error_mark_node;
8791 intype = TREE_TYPE (expr);
8793 /* Save casted types in the function's used types hash table. */
8794 used_types_insert (type);
8796 /* A prvalue of non-class type is cv-unqualified. */
8797 if (!CLASS_TYPE_P (type))
8798 type = cv_unqualified (type);
8800 /* [expr.reinterpret.cast]
8801 A glvalue of type T1, designating an object x, can be cast to the type
8802 "reference to T2" if an expression of type "pointer to T1" can be
8803 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8804 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8805 of type "pointer to T1". No temporary is created, no copy is made, and no
8806 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8807 if (TYPE_REF_P (type))
8809 if (!glvalue_p (expr))
8811 if (complain & tf_error)
8812 error_at (loc, "invalid cast of a prvalue expression of type "
8813 "%qT to type %qT",
8814 intype, type);
8815 return error_mark_node;
8818 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8819 "B" are related class types; the reinterpret_cast does not
8820 adjust the pointer. */
8821 if (TYPE_PTR_P (intype)
8822 && (complain & tf_warning)
8823 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8824 COMPARE_BASE | COMPARE_DERIVED)))
8825 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8826 intype, type);
8828 expr = cp_build_addr_expr (expr, complain);
8830 if (warn_strict_aliasing > 2)
8831 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8833 if (expr != error_mark_node)
8834 expr = build_reinterpret_cast_1
8835 (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8836 valid_p, complain);
8837 if (expr != error_mark_node)
8838 /* cp_build_indirect_ref isn't right for rvalue refs. */
8839 expr = convert_from_reference (fold_convert (type, expr));
8840 return expr;
8843 /* As a G++ extension, we consider conversions from member
8844 functions, and pointers to member functions to
8845 pointer-to-function and pointer-to-void types. If
8846 -Wno-pmf-conversions has not been specified,
8847 convert_member_func_to_ptr will issue an error message. */
8848 if ((TYPE_PTRMEMFUNC_P (intype)
8849 || TREE_CODE (intype) == METHOD_TYPE)
8850 && TYPE_PTR_P (type)
8851 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
8852 || VOID_TYPE_P (TREE_TYPE (type))))
8853 return convert_member_func_to_ptr (type, expr, complain);
8855 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8856 array-to-pointer, and function-to-pointer conversions are
8857 performed. */
8858 expr = decay_conversion (expr, complain);
8860 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8861 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8862 if (TREE_CODE (expr) == NOP_EXPR
8863 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8864 expr = TREE_OPERAND (expr, 0);
8866 if (error_operand_p (expr))
8867 return error_mark_node;
8869 intype = TREE_TYPE (expr);
8871 /* [expr.reinterpret.cast]
8872 A pointer can be converted to any integral type large enough to
8873 hold it. ... A value of type std::nullptr_t can be converted to
8874 an integral type; the conversion has the same meaning and
8875 validity as a conversion of (void*)0 to the integral type. */
8876 if (CP_INTEGRAL_TYPE_P (type)
8877 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8879 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
8881 if (complain & tf_error)
8882 permerror (loc, "cast from %qH to %qI loses precision",
8883 intype, type);
8884 else
8885 return error_mark_node;
8887 if (NULLPTR_TYPE_P (intype))
8888 return build_int_cst (type, 0);
8890 /* [expr.reinterpret.cast]
8891 A value of integral or enumeration type can be explicitly
8892 converted to a pointer. */
8893 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
8894 /* OK */
8896 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8897 || TYPE_PTR_OR_PTRMEM_P (type))
8898 && same_type_p (type, intype))
8899 /* DR 799 */
8900 return rvalue (expr);
8901 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
8903 if ((complain & tf_warning)
8904 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
8905 TREE_TYPE (intype)))
8906 warning_at (loc, OPT_Wcast_function_type,
8907 "cast between incompatible function types"
8908 " from %qH to %qI", intype, type);
8909 return build_nop_reinterpret (type, expr);
8911 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
8913 if ((complain & tf_warning)
8914 && !cxx_safe_function_type_cast_p
8915 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
8916 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
8917 warning_at (loc, OPT_Wcast_function_type,
8918 "cast between incompatible pointer to member types"
8919 " from %qH to %qI", intype, type);
8920 return build_nop_reinterpret (type, expr);
8922 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8923 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
8925 if (!c_cast_p
8926 && check_for_casting_away_constness (loc, intype, type,
8927 REINTERPRET_CAST_EXPR,
8928 complain))
8929 return error_mark_node;
8930 /* Warn about possible alignment problems. */
8931 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8932 && (complain & tf_warning)
8933 && !VOID_TYPE_P (type)
8934 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
8935 && COMPLETE_TYPE_P (TREE_TYPE (type))
8936 && COMPLETE_TYPE_P (TREE_TYPE (intype))
8937 && min_align_of_type (TREE_TYPE (type))
8938 > min_align_of_type (TREE_TYPE (intype)))
8939 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8940 "increases required alignment of target type",
8941 intype, type);
8943 if (warn_strict_aliasing <= 2)
8944 /* strict_aliasing_warning STRIP_NOPs its expr. */
8945 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8947 return build_nop_reinterpret (type, expr);
8949 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
8950 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
8952 if (complain & tf_warning)
8953 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8954 object pointer type or vice versa is conditionally-supported." */
8955 warning_at (loc, OPT_Wconditionally_supported,
8956 "casting between pointer-to-function and "
8957 "pointer-to-object is conditionally-supported");
8958 return build_nop_reinterpret (type, expr);
8960 else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
8961 return convert_to_vector (type, rvalue (expr));
8962 else if (gnu_vector_type_p (intype)
8963 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8964 return convert_to_integer_nofold (type, expr);
8965 else
8967 if (valid_p)
8968 *valid_p = false;
8969 if (complain & tf_error)
8970 error_at (loc, "invalid cast from type %qT to type %qT",
8971 intype, type);
8972 return error_mark_node;
8975 expr = cp_convert (type, expr, complain);
8976 if (TREE_CODE (expr) == NOP_EXPR)
8977 /* Mark any nop_expr that created as a reintepret_cast. */
8978 REINTERPRET_CAST_P (expr) = true;
8979 return expr;
8982 tree
8983 build_reinterpret_cast (location_t loc, tree type, tree expr,
8984 tsubst_flags_t complain)
8986 tree r;
8988 if (type == error_mark_node || expr == error_mark_node)
8989 return error_mark_node;
8991 if (processing_template_decl)
8993 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
8995 if (!TREE_SIDE_EFFECTS (t)
8996 && type_dependent_expression_p (expr))
8997 /* There might turn out to be side effects inside expr. */
8998 TREE_SIDE_EFFECTS (t) = 1;
8999 r = convert_from_reference (t);
9000 protected_set_expr_location (r, loc);
9001 return r;
9004 r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9005 /*valid_p=*/NULL, complain);
9006 if (r != error_mark_node)
9008 maybe_warn_about_useless_cast (loc, type, expr, complain);
9009 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9011 protected_set_expr_location (r, loc);
9012 return r;
9015 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9016 return an appropriate expression. Otherwise, return
9017 error_mark_node. If the cast is not valid, and COMPLAIN is true,
9018 then a diagnostic will be issued. If VALID_P is non-NULL, we are
9019 performing a C-style cast, its value upon return will indicate
9020 whether or not the conversion succeeded. */
9022 static tree
9023 build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9024 tsubst_flags_t complain, bool *valid_p)
9026 tree src_type;
9027 tree reference_type;
9029 /* Callers are responsible for handling error_mark_node as a
9030 destination type. */
9031 gcc_assert (dst_type != error_mark_node);
9032 /* In a template, callers should be building syntactic
9033 representations of casts, not using this machinery. */
9034 gcc_assert (!processing_template_decl);
9036 /* Assume the conversion is invalid. */
9037 if (valid_p)
9038 *valid_p = false;
9040 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9042 if (complain & tf_error)
9043 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9044 "which is not a pointer, reference, "
9045 "nor a pointer-to-data-member type", dst_type);
9046 return error_mark_node;
9049 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9051 if (complain & tf_error)
9052 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9053 "which is a pointer or reference to a function type",
9054 dst_type);
9055 return error_mark_node;
9058 /* A prvalue of non-class type is cv-unqualified. */
9059 dst_type = cv_unqualified (dst_type);
9061 /* Save casted types in the function's used types hash table. */
9062 used_types_insert (dst_type);
9064 src_type = TREE_TYPE (expr);
9065 /* Expressions do not really have reference types. */
9066 if (TYPE_REF_P (src_type))
9067 src_type = TREE_TYPE (src_type);
9069 /* [expr.const.cast]
9071 For two object types T1 and T2, if a pointer to T1 can be explicitly
9072 converted to the type "pointer to T2" using a const_cast, then the
9073 following conversions can also be made:
9075 -- an lvalue of type T1 can be explicitly converted to an lvalue of
9076 type T2 using the cast const_cast<T2&>;
9078 -- a glvalue of type T1 can be explicitly converted to an xvalue of
9079 type T2 using the cast const_cast<T2&&>; and
9081 -- if T1 is a class type, a prvalue of type T1 can be explicitly
9082 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9084 if (TYPE_REF_P (dst_type))
9086 reference_type = dst_type;
9087 if (!TYPE_REF_IS_RVALUE (dst_type)
9088 ? lvalue_p (expr)
9089 : obvalue_p (expr))
9090 /* OK. */;
9091 else
9093 if (complain & tf_error)
9094 error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9095 "to type %qT",
9096 src_type, dst_type);
9097 return error_mark_node;
9099 dst_type = build_pointer_type (TREE_TYPE (dst_type));
9100 src_type = build_pointer_type (src_type);
9102 else
9104 reference_type = NULL_TREE;
9105 /* If the destination type is not a reference type, the
9106 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9107 conversions are performed. */
9108 src_type = type_decays_to (src_type);
9109 if (src_type == error_mark_node)
9110 return error_mark_node;
9113 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9115 if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9117 if (valid_p)
9119 *valid_p = true;
9120 /* This cast is actually a C-style cast. Issue a warning if
9121 the user is making a potentially unsafe cast. */
9122 check_for_casting_away_constness (loc, src_type, dst_type,
9123 CAST_EXPR, complain);
9124 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9125 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9126 && (complain & tf_warning)
9127 && min_align_of_type (TREE_TYPE (dst_type))
9128 > min_align_of_type (TREE_TYPE (src_type)))
9129 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9130 "increases required alignment of target type",
9131 src_type, dst_type);
9133 if (reference_type)
9135 expr = cp_build_addr_expr (expr, complain);
9136 if (expr == error_mark_node)
9137 return error_mark_node;
9138 expr = build_nop (reference_type, expr);
9139 return convert_from_reference (expr);
9141 else
9143 expr = decay_conversion (expr, complain);
9144 if (expr == error_mark_node)
9145 return error_mark_node;
9147 /* build_c_cast puts on a NOP_EXPR to make the result not an
9148 lvalue. Strip such NOP_EXPRs if VALUE is being used in
9149 non-lvalue context. */
9150 if (TREE_CODE (expr) == NOP_EXPR
9151 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9152 expr = TREE_OPERAND (expr, 0);
9153 return build_nop (dst_type, expr);
9156 else if (valid_p
9157 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9158 TREE_TYPE (src_type)))
9159 check_for_casting_away_constness (loc, src_type, dst_type,
9160 CAST_EXPR, complain);
9163 if (complain & tf_error)
9164 error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9165 src_type, dst_type);
9166 return error_mark_node;
9169 tree
9170 build_const_cast (location_t loc, tree type, tree expr,
9171 tsubst_flags_t complain)
9173 tree r;
9175 if (type == error_mark_node || error_operand_p (expr))
9176 return error_mark_node;
9178 if (processing_template_decl)
9180 tree t = build_min (CONST_CAST_EXPR, type, expr);
9182 if (!TREE_SIDE_EFFECTS (t)
9183 && type_dependent_expression_p (expr))
9184 /* There might turn out to be side effects inside expr. */
9185 TREE_SIDE_EFFECTS (t) = 1;
9186 r = convert_from_reference (t);
9187 protected_set_expr_location (r, loc);
9188 return r;
9191 r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9192 if (r != error_mark_node)
9194 maybe_warn_about_useless_cast (loc, type, expr, complain);
9195 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9197 protected_set_expr_location (r, loc);
9198 return r;
9201 /* Like cp_build_c_cast, but for the c-common bits. */
9203 tree
9204 build_c_cast (location_t loc, tree type, tree expr)
9206 return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9209 /* Like the "build_c_cast" used for c-common, but using cp_expr to
9210 preserve location information even for tree nodes that don't
9211 support it. */
9213 cp_expr
9214 build_c_cast (location_t loc, tree type, cp_expr expr)
9216 cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9217 result.set_location (loc);
9218 return result;
9221 /* Build an expression representing an explicit C-style cast to type
9222 TYPE of expression EXPR. */
9224 tree
9225 cp_build_c_cast (location_t loc, tree type, tree expr,
9226 tsubst_flags_t complain)
9228 tree value = expr;
9229 tree result;
9230 bool valid_p;
9232 if (type == error_mark_node || error_operand_p (expr))
9233 return error_mark_node;
9235 if (processing_template_decl)
9237 tree t = build_min (CAST_EXPR, type,
9238 tree_cons (NULL_TREE, value, NULL_TREE));
9239 /* We don't know if it will or will not have side effects. */
9240 TREE_SIDE_EFFECTS (t) = 1;
9241 return convert_from_reference (t);
9244 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9245 'Class') should always be retained, because this information aids
9246 in method lookup. */
9247 if (objc_is_object_ptr (type)
9248 && objc_is_object_ptr (TREE_TYPE (expr)))
9249 return build_nop (type, expr);
9251 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9252 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9253 if (!TYPE_REF_P (type)
9254 && TREE_CODE (value) == NOP_EXPR
9255 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9256 value = TREE_OPERAND (value, 0);
9258 if (TREE_CODE (type) == ARRAY_TYPE)
9260 /* Allow casting from T1* to T2[] because Cfront allows it.
9261 NIHCL uses it. It is not valid ISO C++ however. */
9262 if (TYPE_PTR_P (TREE_TYPE (expr)))
9264 if (complain & tf_error)
9265 permerror (loc, "ISO C++ forbids casting to an array type %qT",
9266 type);
9267 else
9268 return error_mark_node;
9269 type = build_pointer_type (TREE_TYPE (type));
9271 else
9273 if (complain & tf_error)
9274 error_at (loc, "ISO C++ forbids casting to an array type %qT",
9275 type);
9276 return error_mark_node;
9280 if (FUNC_OR_METHOD_TYPE_P (type))
9282 if (complain & tf_error)
9283 error_at (loc, "invalid cast to function type %qT", type);
9284 return error_mark_node;
9287 if (TYPE_PTR_P (type)
9288 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9289 /* Casting to an integer of smaller size is an error detected elsewhere. */
9290 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9291 /* Don't warn about converting any constant. */
9292 && !TREE_CONSTANT (value))
9293 warning_at (loc, OPT_Wint_to_pointer_cast,
9294 "cast to pointer from integer of different size");
9296 /* A C-style cast can be a const_cast. */
9297 result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9298 &valid_p);
9299 if (valid_p)
9301 if (result != error_mark_node)
9303 maybe_warn_about_useless_cast (loc, type, value, complain);
9304 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9306 else if (complain & tf_error)
9307 build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9308 return result;
9311 /* Or a static cast. */
9312 result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9313 &valid_p, complain);
9314 /* Or a reinterpret_cast. */
9315 if (!valid_p)
9316 result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9317 &valid_p, complain);
9318 /* The static_cast or reinterpret_cast may be followed by a
9319 const_cast. */
9320 if (valid_p
9321 /* A valid cast may result in errors if, for example, a
9322 conversion to an ambiguous base class is required. */
9323 && !error_operand_p (result))
9325 tree result_type;
9327 maybe_warn_about_useless_cast (loc, type, value, complain);
9328 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9330 /* Non-class rvalues always have cv-unqualified type. */
9331 if (!CLASS_TYPE_P (type))
9332 type = TYPE_MAIN_VARIANT (type);
9333 result_type = TREE_TYPE (result);
9334 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9335 result_type = TYPE_MAIN_VARIANT (result_type);
9336 /* If the type of RESULT does not match TYPE, perform a
9337 const_cast to make it match. If the static_cast or
9338 reinterpret_cast succeeded, we will differ by at most
9339 cv-qualification, so the follow-on const_cast is guaranteed
9340 to succeed. */
9341 if (!same_type_p (non_reference (type), non_reference (result_type)))
9343 result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9344 gcc_assert (valid_p);
9346 return result;
9349 return error_mark_node;
9352 /* Warn when a value is moved to itself with std::move. LHS is the target,
9353 RHS may be the std::move call, and LOC is the location of the whole
9354 assignment. */
9356 static void
9357 maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9359 if (!warn_self_move)
9360 return;
9362 /* C++98 doesn't know move. */
9363 if (cxx_dialect < cxx11)
9364 return;
9366 if (processing_template_decl)
9367 return;
9369 if (!REFERENCE_REF_P (rhs)
9370 || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9371 return;
9372 tree fn = TREE_OPERAND (rhs, 0);
9373 if (!is_std_move_p (fn))
9374 return;
9376 /* Just a little helper to strip * and various NOPs. */
9377 auto extract_op = [] (tree &op) {
9378 STRIP_NOPS (op);
9379 while (INDIRECT_REF_P (op))
9380 op = TREE_OPERAND (op, 0);
9381 op = maybe_undo_parenthesized_ref (op);
9382 STRIP_ANY_LOCATION_WRAPPER (op);
9385 tree arg = CALL_EXPR_ARG (fn, 0);
9386 extract_op (arg);
9387 if (TREE_CODE (arg) == ADDR_EXPR)
9388 arg = TREE_OPERAND (arg, 0);
9389 tree type = TREE_TYPE (lhs);
9390 tree orig_lhs = lhs;
9391 extract_op (lhs);
9392 if (cp_tree_equal (lhs, arg))
9394 auto_diagnostic_group d;
9395 if (warning_at (loc, OPT_Wself_move,
9396 "moving %qE of type %qT to itself", orig_lhs, type))
9397 inform (loc, "remove %<std::move%> call");
9401 /* For use from the C common bits. */
9402 tree
9403 build_modify_expr (location_t location,
9404 tree lhs, tree /*lhs_origtype*/,
9405 enum tree_code modifycode,
9406 location_t /*rhs_location*/, tree rhs,
9407 tree /*rhs_origtype*/)
9409 return cp_build_modify_expr (location, lhs, modifycode, rhs,
9410 tf_warning_or_error);
9413 /* Build an assignment expression of lvalue LHS from value RHS.
9414 MODIFYCODE is the code for a binary operator that we use
9415 to combine the old value of LHS with RHS to get the new value.
9416 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9418 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9420 tree
9421 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9422 tree rhs, tsubst_flags_t complain)
9424 lhs = mark_lvalue_use_nonread (lhs);
9426 tree result = NULL_TREE;
9427 tree newrhs = rhs;
9428 tree lhstype = TREE_TYPE (lhs);
9429 tree olhs = lhs;
9430 tree olhstype = lhstype;
9431 bool plain_assign = (modifycode == NOP_EXPR);
9432 bool compound_side_effects_p = false;
9433 tree preeval = NULL_TREE;
9435 /* Avoid duplicate error messages from operands that had errors. */
9436 if (error_operand_p (lhs) || error_operand_p (rhs))
9437 return error_mark_node;
9439 while (TREE_CODE (lhs) == COMPOUND_EXPR)
9441 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9442 compound_side_effects_p = true;
9443 lhs = TREE_OPERAND (lhs, 1);
9446 /* Handle control structure constructs used as "lvalues". Note that we
9447 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9448 switch (TREE_CODE (lhs))
9450 /* Handle --foo = 5; as these are valid constructs in C++. */
9451 case PREDECREMENT_EXPR:
9452 case PREINCREMENT_EXPR:
9453 if (compound_side_effects_p)
9454 newrhs = rhs = stabilize_expr (rhs, &preeval);
9455 lhs = genericize_compound_lvalue (lhs);
9456 maybe_add_compound:
9457 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9458 and looked through the COMPOUND_EXPRs, readd them now around
9459 the resulting lhs. */
9460 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9462 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9463 tree *ptr = &TREE_OPERAND (lhs, 1);
9464 for (olhs = TREE_OPERAND (olhs, 1);
9465 TREE_CODE (olhs) == COMPOUND_EXPR;
9466 olhs = TREE_OPERAND (olhs, 1))
9468 *ptr = build2 (COMPOUND_EXPR, lhstype,
9469 TREE_OPERAND (olhs, 0), *ptr);
9470 ptr = &TREE_OPERAND (*ptr, 1);
9473 break;
9475 case MODIFY_EXPR:
9476 if (compound_side_effects_p)
9477 newrhs = rhs = stabilize_expr (rhs, &preeval);
9478 lhs = genericize_compound_lvalue (lhs);
9479 goto maybe_add_compound;
9481 case MIN_EXPR:
9482 case MAX_EXPR:
9483 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9484 when neither operand has side-effects. */
9485 if (!lvalue_or_else (lhs, lv_assign, complain))
9486 return error_mark_node;
9488 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9489 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9491 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9492 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9493 boolean_type_node,
9494 TREE_OPERAND (lhs, 0),
9495 TREE_OPERAND (lhs, 1)),
9496 TREE_OPERAND (lhs, 0),
9497 TREE_OPERAND (lhs, 1));
9498 gcc_fallthrough ();
9500 /* Handle (a ? b : c) used as an "lvalue". */
9501 case COND_EXPR:
9503 /* Produce (a ? (b = rhs) : (c = rhs))
9504 except that the RHS goes through a save-expr
9505 so the code to compute it is only emitted once. */
9506 if (VOID_TYPE_P (TREE_TYPE (rhs)))
9508 if (complain & tf_error)
9509 error_at (cp_expr_loc_or_loc (rhs, loc),
9510 "void value not ignored as it ought to be");
9511 return error_mark_node;
9514 rhs = stabilize_expr (rhs, &preeval);
9516 /* Check this here to avoid odd errors when trying to convert
9517 a throw to the type of the COND_EXPR. */
9518 if (!lvalue_or_else (lhs, lv_assign, complain))
9519 return error_mark_node;
9521 tree op1 = TREE_OPERAND (lhs, 1);
9522 if (TREE_CODE (op1) != THROW_EXPR)
9523 op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9524 /* When sanitizing undefined behavior, even when rhs doesn't need
9525 stabilization at this point, the sanitization might add extra
9526 SAVE_EXPRs in there and so make sure there is no tree sharing
9527 in the rhs, otherwise those SAVE_EXPRs will have initialization
9528 only in one of the two branches. */
9529 if (sanitize_flags_p (SANITIZE_UNDEFINED
9530 | SANITIZE_UNDEFINED_NONDEFAULT))
9531 rhs = unshare_expr (rhs);
9532 tree op2 = TREE_OPERAND (lhs, 2);
9533 if (TREE_CODE (op2) != THROW_EXPR)
9534 op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9535 tree cond = build_conditional_expr (input_location,
9536 TREE_OPERAND (lhs, 0), op1, op2,
9537 complain);
9539 if (cond == error_mark_node)
9540 return cond;
9541 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9542 and looked through the COMPOUND_EXPRs, readd them now around
9543 the resulting cond before adding the preevaluated rhs. */
9544 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9546 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9547 TREE_OPERAND (olhs, 0), cond);
9548 tree *ptr = &TREE_OPERAND (cond, 1);
9549 for (olhs = TREE_OPERAND (olhs, 1);
9550 TREE_CODE (olhs) == COMPOUND_EXPR;
9551 olhs = TREE_OPERAND (olhs, 1))
9553 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9554 TREE_OPERAND (olhs, 0), *ptr);
9555 ptr = &TREE_OPERAND (*ptr, 1);
9558 /* Make sure the code to compute the rhs comes out
9559 before the split. */
9560 result = cond;
9561 goto ret;
9564 default:
9565 lhs = olhs;
9566 break;
9569 if (modifycode == INIT_EXPR)
9571 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9572 /* Do the default thing. */;
9573 else if (TREE_CODE (rhs) == CONSTRUCTOR)
9575 /* Compound literal. */
9576 if (! same_type_p (TREE_TYPE (rhs), lhstype))
9577 /* Call convert to generate an error; see PR 11063. */
9578 rhs = convert (lhstype, rhs);
9579 result = cp_build_init_expr (lhs, rhs);
9580 TREE_SIDE_EFFECTS (result) = 1;
9581 goto ret;
9583 else if (! MAYBE_CLASS_TYPE_P (lhstype))
9584 /* Do the default thing. */;
9585 else
9587 releasing_vec rhs_vec = make_tree_vector_single (rhs);
9588 result = build_special_member_call (lhs, complete_ctor_identifier,
9589 &rhs_vec, lhstype, LOOKUP_NORMAL,
9590 complain);
9591 if (result == NULL_TREE)
9592 return error_mark_node;
9593 goto ret;
9596 else
9598 lhs = require_complete_type (lhs, complain);
9599 if (lhs == error_mark_node)
9600 return error_mark_node;
9602 if (modifycode == NOP_EXPR)
9604 maybe_warn_self_move (loc, lhs, rhs);
9606 if (c_dialect_objc ())
9608 result = objc_maybe_build_modify_expr (lhs, rhs);
9609 if (result)
9610 goto ret;
9613 /* `operator=' is not an inheritable operator. */
9614 if (! MAYBE_CLASS_TYPE_P (lhstype))
9615 /* Do the default thing. */;
9616 else
9618 result = build_new_op (input_location, MODIFY_EXPR,
9619 LOOKUP_NORMAL, lhs, rhs,
9620 make_node (NOP_EXPR), NULL_TREE,
9621 /*overload=*/NULL, complain);
9622 if (result == NULL_TREE)
9623 return error_mark_node;
9624 goto ret;
9626 lhstype = olhstype;
9628 else
9630 tree init = NULL_TREE;
9632 /* A binary op has been requested. Combine the old LHS
9633 value with the RHS producing the value we should actually
9634 store into the LHS. */
9635 gcc_assert (!((TYPE_REF_P (lhstype)
9636 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9637 || MAYBE_CLASS_TYPE_P (lhstype)));
9639 /* Preevaluate the RHS to make sure its evaluation is complete
9640 before the lvalue-to-rvalue conversion of the LHS:
9642 [expr.ass] With respect to an indeterminately-sequenced
9643 function call, the operation of a compound assignment is a
9644 single evaluation. [ Note: Therefore, a function call shall
9645 not intervene between the lvalue-to-rvalue conversion and the
9646 side effect associated with any single compound assignment
9647 operator. -- end note ] */
9648 lhs = cp_stabilize_reference (lhs);
9649 rhs = decay_conversion (rhs, complain);
9650 if (rhs == error_mark_node)
9651 return error_mark_node;
9652 rhs = stabilize_expr (rhs, &init);
9653 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9654 if (newrhs == error_mark_node)
9656 if (complain & tf_error)
9657 inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9658 modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9659 return error_mark_node;
9662 if (init)
9663 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9665 /* Now it looks like a plain assignment. */
9666 modifycode = NOP_EXPR;
9667 if (c_dialect_objc ())
9669 result = objc_maybe_build_modify_expr (lhs, newrhs);
9670 if (result)
9671 goto ret;
9674 gcc_assert (!TYPE_REF_P (lhstype));
9675 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9678 /* The left-hand side must be an lvalue. */
9679 if (!lvalue_or_else (lhs, lv_assign, complain))
9680 return error_mark_node;
9682 /* Warn about modifying something that is `const'. Don't warn if
9683 this is initialization. */
9684 if (modifycode != INIT_EXPR
9685 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9686 /* Functions are not modifiable, even though they are
9687 lvalues. */
9688 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9689 /* If it's an aggregate and any field is const, then it is
9690 effectively const. */
9691 || (CLASS_TYPE_P (lhstype)
9692 && C_TYPE_FIELDS_READONLY (lhstype))))
9694 if (complain & tf_error)
9695 cxx_readonly_error (loc, lhs, lv_assign);
9696 return error_mark_node;
9699 /* If storing into a structure or union member, it may have been given a
9700 lowered bitfield type. We need to convert to the declared type first,
9701 so retrieve it now. */
9703 olhstype = unlowered_expr_type (lhs);
9705 /* Convert new value to destination type. */
9707 if (TREE_CODE (lhstype) == ARRAY_TYPE)
9709 int from_array;
9711 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9713 if (modifycode != INIT_EXPR)
9715 if (complain & tf_error)
9716 error_at (loc,
9717 "assigning to an array from an initializer list");
9718 return error_mark_node;
9720 if (check_array_initializer (lhs, lhstype, newrhs))
9721 return error_mark_node;
9722 newrhs = digest_init (lhstype, newrhs, complain);
9723 if (newrhs == error_mark_node)
9724 return error_mark_node;
9727 /* C++11 8.5/17: "If the destination type is an array of characters,
9728 an array of char16_t, an array of char32_t, or an array of wchar_t,
9729 and the initializer is a string literal...". */
9730 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9731 == STRING_CST)
9732 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9733 && modifycode == INIT_EXPR)
9735 newrhs = digest_init (lhstype, newrhs, complain);
9736 if (newrhs == error_mark_node)
9737 return error_mark_node;
9740 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9741 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9743 if (complain & tf_error)
9744 error_at (loc, "incompatible types in assignment of %qT to %qT",
9745 TREE_TYPE (rhs), lhstype);
9746 return error_mark_node;
9749 /* Allow array assignment in compiler-generated code. */
9750 else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9751 /* OK, used by coroutines (co-await-initlist1.C). */;
9752 else if (!current_function_decl
9753 || !DECL_DEFAULTED_FN (current_function_decl))
9755 /* This routine is used for both initialization and assignment.
9756 Make sure the diagnostic message differentiates the context. */
9757 if (complain & tf_error)
9759 if (modifycode == INIT_EXPR)
9760 error_at (loc, "array used as initializer");
9761 else
9762 error_at (loc, "invalid array assignment");
9764 return error_mark_node;
9767 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9768 ? 1 + (modifycode != INIT_EXPR): 0;
9769 result = build_vec_init (lhs, NULL_TREE, newrhs,
9770 /*explicit_value_init_p=*/false,
9771 from_array, complain);
9772 goto ret;
9775 if (modifycode == INIT_EXPR)
9776 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9777 LOOKUP_ONLYCONVERTING. */
9778 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9779 ICR_INIT, NULL_TREE, 0,
9780 complain | tf_no_cleanup);
9781 else
9782 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9783 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9785 if (!same_type_p (lhstype, olhstype))
9786 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9788 if (modifycode != INIT_EXPR)
9790 if (TREE_CODE (newrhs) == CALL_EXPR
9791 && TYPE_NEEDS_CONSTRUCTING (lhstype))
9792 newrhs = build_cplus_new (lhstype, newrhs, complain);
9794 /* Can't initialize directly from a TARGET_EXPR, since that would
9795 cause the lhs to be constructed twice, and possibly result in
9796 accidental self-initialization. So we force the TARGET_EXPR to be
9797 expanded without a target. */
9798 if (TREE_CODE (newrhs) == TARGET_EXPR)
9799 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9800 TREE_OPERAND (newrhs, 0));
9803 if (newrhs == error_mark_node)
9804 return error_mark_node;
9806 if (c_dialect_objc () && flag_objc_gc)
9808 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9810 if (result)
9811 goto ret;
9814 result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9815 lhstype, lhs, newrhs);
9816 if (modifycode == INIT_EXPR)
9817 set_target_expr_eliding (newrhs);
9819 TREE_SIDE_EFFECTS (result) = 1;
9820 if (!plain_assign)
9821 suppress_warning (result, OPT_Wparentheses);
9823 ret:
9824 if (preeval)
9825 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
9826 return result;
9829 cp_expr
9830 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9831 tree rhs, tree lookups, tsubst_flags_t complain)
9833 tree orig_lhs = lhs;
9834 tree orig_rhs = rhs;
9835 tree overload = NULL_TREE;
9837 if (lhs == error_mark_node || rhs == error_mark_node)
9838 return cp_expr (error_mark_node, loc);
9840 tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
9842 if (processing_template_decl)
9844 if (type_dependent_expression_p (lhs)
9845 || type_dependent_expression_p (rhs))
9847 tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
9848 if (modifycode != NOP_EXPR)
9849 TREE_TYPE (rval)
9850 = build_dependent_operator_type (lookups, modifycode, true);
9851 return rval;
9855 tree rval;
9856 if (modifycode == NOP_EXPR)
9857 rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
9858 else
9859 rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
9860 lhs, rhs, op, lookups, &overload, complain);
9861 if (rval == error_mark_node)
9862 return error_mark_node;
9863 if (processing_template_decl)
9865 if (overload != NULL_TREE)
9866 return (build_min_non_dep_op_overload
9867 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
9869 return (build_min_non_dep
9870 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
9872 return rval;
9875 /* Helper function for get_delta_difference which assumes FROM is a base
9876 class of TO. Returns a delta for the conversion of pointer-to-member
9877 of FROM to pointer-to-member of TO. If the conversion is invalid and
9878 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9879 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9880 If C_CAST_P is true, this conversion is taking place as part of a
9881 C-style cast. */
9883 static tree
9884 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
9885 tsubst_flags_t complain)
9887 tree binfo;
9888 base_kind kind;
9890 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
9891 &kind, complain);
9893 if (binfo == error_mark_node)
9895 if (!(complain & tf_error))
9896 return error_mark_node;
9898 inform (input_location, " in pointer to member function conversion");
9899 return size_zero_node;
9901 else if (binfo)
9903 if (kind != bk_via_virtual)
9904 return BINFO_OFFSET (binfo);
9905 else
9906 /* FROM is a virtual base class of TO. Issue an error or warning
9907 depending on whether or not this is a reinterpret cast. */
9909 if (!(complain & tf_error))
9910 return error_mark_node;
9912 error ("pointer to member conversion via virtual base %qT",
9913 BINFO_TYPE (binfo_from_vbase (binfo)));
9915 return size_zero_node;
9918 else
9919 return NULL_TREE;
9922 /* Get difference in deltas for different pointer to member function
9923 types. If the conversion is invalid and tf_error is not set in
9924 COMPLAIN, returns error_mark_node, otherwise returns an integer
9925 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9926 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9927 conversions as well. If C_CAST_P is true this conversion is taking
9928 place as part of a C-style cast.
9930 Note that the naming of FROM and TO is kind of backwards; the return
9931 value is what we add to a TO in order to get a FROM. They are named
9932 this way because we call this function to find out how to convert from
9933 a pointer to member of FROM to a pointer to member of TO. */
9935 static tree
9936 get_delta_difference (tree from, tree to,
9937 bool allow_inverse_p,
9938 bool c_cast_p, tsubst_flags_t complain)
9940 tree result;
9942 if (same_type_ignoring_top_level_qualifiers_p (from, to))
9943 /* Pointer to member of incomplete class is permitted*/
9944 result = size_zero_node;
9945 else
9946 result = get_delta_difference_1 (from, to, c_cast_p, complain);
9948 if (result == error_mark_node)
9949 return error_mark_node;
9951 if (!result)
9953 if (!allow_inverse_p)
9955 if (!(complain & tf_error))
9956 return error_mark_node;
9958 error_not_base_type (from, to);
9959 inform (input_location, " in pointer to member conversion");
9960 result = size_zero_node;
9962 else
9964 result = get_delta_difference_1 (to, from, c_cast_p, complain);
9966 if (result == error_mark_node)
9967 return error_mark_node;
9969 if (result)
9970 result = size_diffop_loc (input_location,
9971 size_zero_node, result);
9972 else
9974 if (!(complain & tf_error))
9975 return error_mark_node;
9977 error_not_base_type (from, to);
9978 inform (input_location, " in pointer to member conversion");
9979 result = size_zero_node;
9984 return convert_to_integer (ptrdiff_type_node, result);
9987 /* Return a constructor for the pointer-to-member-function TYPE using
9988 the other components as specified. */
9990 tree
9991 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
9993 tree u = NULL_TREE;
9994 tree delta_field;
9995 tree pfn_field;
9996 vec<constructor_elt, va_gc> *v;
9998 /* Pull the FIELD_DECLs out of the type. */
9999 pfn_field = TYPE_FIELDS (type);
10000 delta_field = DECL_CHAIN (pfn_field);
10002 /* Make sure DELTA has the type we want. */
10003 delta = convert_and_check (input_location, delta_type_node, delta);
10005 /* Convert to the correct target type if necessary. */
10006 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10008 /* Finish creating the initializer. */
10009 vec_alloc (v, 2);
10010 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10011 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10012 u = build_constructor (type, v);
10013 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10014 TREE_STATIC (u) = (TREE_CONSTANT (u)
10015 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10016 != NULL_TREE)
10017 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10018 != NULL_TREE));
10019 return u;
10022 /* Build a constructor for a pointer to member function. It can be
10023 used to initialize global variables, local variable, or used
10024 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10025 want to be.
10027 If FORCE is nonzero, then force this conversion, even if
10028 we would rather not do it. Usually set when using an explicit
10029 cast. A C-style cast is being processed iff C_CAST_P is true.
10031 Return error_mark_node, if something goes wrong. */
10033 tree
10034 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10035 tsubst_flags_t complain)
10037 tree fn;
10038 tree pfn_type;
10039 tree to_type;
10041 if (error_operand_p (pfn))
10042 return error_mark_node;
10044 pfn_type = TREE_TYPE (pfn);
10045 to_type = build_ptrmemfunc_type (type);
10047 /* Handle multiple conversions of pointer to member functions. */
10048 if (TYPE_PTRMEMFUNC_P (pfn_type))
10050 tree delta = NULL_TREE;
10051 tree npfn = NULL_TREE;
10052 tree n;
10054 if (!force
10055 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10056 LOOKUP_NORMAL, complain))
10058 if (complain & tf_error)
10059 error ("invalid conversion to type %qT from type %qT",
10060 to_type, pfn_type);
10061 else
10062 return error_mark_node;
10065 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10066 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10067 force,
10068 c_cast_p, complain);
10069 if (n == error_mark_node)
10070 return error_mark_node;
10072 STRIP_ANY_LOCATION_WRAPPER (pfn);
10074 /* We don't have to do any conversion to convert a
10075 pointer-to-member to its own type. But, we don't want to
10076 just return a PTRMEM_CST if there's an explicit cast; that
10077 cast should make the expression an invalid template argument. */
10078 if (TREE_CODE (pfn) != PTRMEM_CST
10079 && same_type_p (to_type, pfn_type))
10080 return pfn;
10082 if (TREE_SIDE_EFFECTS (pfn))
10083 pfn = save_expr (pfn);
10085 /* Obtain the function pointer and the current DELTA. */
10086 if (TREE_CODE (pfn) == PTRMEM_CST)
10087 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10088 else
10090 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10091 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10094 /* Just adjust the DELTA field. */
10095 gcc_assert (same_type_ignoring_top_level_qualifiers_p
10096 (TREE_TYPE (delta), ptrdiff_type_node));
10097 if (!integer_zerop (n))
10099 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10100 n = cp_build_binary_op (input_location,
10101 LSHIFT_EXPR, n, integer_one_node,
10102 complain);
10103 delta = cp_build_binary_op (input_location,
10104 PLUS_EXPR, delta, n, complain);
10106 return build_ptrmemfunc1 (to_type, delta, npfn);
10109 /* Handle null pointer to member function conversions. */
10110 if (null_ptr_cst_p (pfn))
10112 pfn = cp_build_c_cast (input_location,
10113 TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10114 pfn, complain);
10115 return build_ptrmemfunc1 (to_type,
10116 integer_zero_node,
10117 pfn);
10120 if (type_unknown_p (pfn))
10121 return instantiate_type (type, pfn, complain);
10123 fn = TREE_OPERAND (pfn, 0);
10124 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10125 /* In a template, we will have preserved the
10126 OFFSET_REF. */
10127 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10128 return make_ptrmem_cst (to_type, fn);
10131 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10132 given by CST.
10134 ??? There is no consistency as to the types returned for the above
10135 values. Some code acts as if it were a sizetype and some as if it were
10136 integer_type_node. */
10138 void
10139 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10141 tree type = TREE_TYPE (cst);
10142 tree fn = PTRMEM_CST_MEMBER (cst);
10143 tree ptr_class, fn_class;
10145 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10147 /* The class that the function belongs to. */
10148 fn_class = DECL_CONTEXT (fn);
10150 /* The class that we're creating a pointer to member of. */
10151 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10153 /* First, calculate the adjustment to the function's class. */
10154 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10155 /*c_cast_p=*/0, tf_warning_or_error);
10157 if (!DECL_VIRTUAL_P (fn))
10159 tree t = build_addr_func (fn, tf_warning_or_error);
10160 if (TREE_CODE (t) == ADDR_EXPR)
10161 SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10162 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10164 else
10166 /* If we're dealing with a virtual function, we have to adjust 'this'
10167 again, to point to the base which provides the vtable entry for
10168 fn; the call will do the opposite adjustment. */
10169 tree orig_class = DECL_CONTEXT (fn);
10170 tree binfo = binfo_or_else (orig_class, fn_class);
10171 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10172 *delta, BINFO_OFFSET (binfo));
10174 /* We set PFN to the vtable offset at which the function can be
10175 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10176 case delta is shifted left, and then incremented). */
10177 *pfn = DECL_VINDEX (fn);
10178 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10179 TYPE_SIZE_UNIT (vtable_entry_type));
10181 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10183 case ptrmemfunc_vbit_in_pfn:
10184 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10185 integer_one_node);
10186 break;
10188 case ptrmemfunc_vbit_in_delta:
10189 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10190 *delta, integer_one_node);
10191 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10192 *delta, integer_one_node);
10193 break;
10195 default:
10196 gcc_unreachable ();
10199 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10203 /* Return an expression for PFN from the pointer-to-member function
10204 given by T. */
10206 static tree
10207 pfn_from_ptrmemfunc (tree t)
10209 if (TREE_CODE (t) == PTRMEM_CST)
10211 tree delta;
10212 tree pfn;
10214 expand_ptrmemfunc_cst (t, &delta, &pfn);
10215 if (pfn)
10216 return pfn;
10219 return build_ptrmemfunc_access_expr (t, pfn_identifier);
10222 /* Return an expression for DELTA from the pointer-to-member function
10223 given by T. */
10225 static tree
10226 delta_from_ptrmemfunc (tree t)
10228 if (TREE_CODE (t) == PTRMEM_CST)
10230 tree delta;
10231 tree pfn;
10233 expand_ptrmemfunc_cst (t, &delta, &pfn);
10234 if (delta)
10235 return delta;
10238 return build_ptrmemfunc_access_expr (t, delta_identifier);
10241 /* Convert value RHS to type TYPE as preparation for an assignment to
10242 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10243 implicit conversion is. If FNDECL is non-NULL, we are doing the
10244 conversion in order to pass the PARMNUMth argument of FNDECL.
10245 If FNDECL is NULL, we are doing the conversion in function pointer
10246 argument passing, conversion in initialization, etc. */
10248 static tree
10249 convert_for_assignment (tree type, tree rhs,
10250 impl_conv_rhs errtype, tree fndecl, int parmnum,
10251 tsubst_flags_t complain, int flags)
10253 tree rhstype;
10254 enum tree_code coder;
10256 location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10257 bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10258 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10259 but preserve location wrappers. */
10260 if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10261 && !location_wrapper_p (rhs))
10262 rhs = TREE_OPERAND (rhs, 0);
10264 /* Handle [dcl.init.list] direct-list-initialization from
10265 single element of enumeration with a fixed underlying type. */
10266 if (is_direct_enum_init (type, rhs))
10268 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10269 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10271 warning_sentinel w (warn_useless_cast);
10272 warning_sentinel w2 (warn_ignored_qualifiers);
10273 rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10275 else
10276 rhs = error_mark_node;
10279 rhstype = TREE_TYPE (rhs);
10280 coder = TREE_CODE (rhstype);
10282 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10283 && vector_types_convertible_p (type, rhstype, true))
10285 rhs = mark_rvalue_use (rhs);
10286 return convert (type, rhs);
10289 if (rhs == error_mark_node || rhstype == error_mark_node)
10290 return error_mark_node;
10291 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10292 return error_mark_node;
10294 /* The RHS of an assignment cannot have void type. */
10295 if (coder == VOID_TYPE)
10297 if (complain & tf_error)
10298 error_at (rhs_loc, "void value not ignored as it ought to be");
10299 return error_mark_node;
10302 if (c_dialect_objc ())
10304 int parmno;
10305 tree selector;
10306 tree rname = fndecl;
10308 switch (errtype)
10310 case ICR_ASSIGN:
10311 parmno = -1;
10312 break;
10313 case ICR_INIT:
10314 parmno = -2;
10315 break;
10316 default:
10317 selector = objc_message_selector ();
10318 parmno = parmnum;
10319 if (selector && parmno > 1)
10321 rname = selector;
10322 parmno -= 1;
10326 if (objc_compare_types (type, rhstype, parmno, rname))
10328 rhs = mark_rvalue_use (rhs);
10329 return convert (type, rhs);
10333 /* [expr.ass]
10335 The expression is implicitly converted (clause _conv_) to the
10336 cv-unqualified type of the left operand.
10338 We allow bad conversions here because by the time we get to this point
10339 we are committed to doing the conversion. If we end up doing a bad
10340 conversion, convert_like will complain. */
10341 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10343 /* When -Wno-pmf-conversions is use, we just silently allow
10344 conversions from pointers-to-members to plain pointers. If
10345 the conversion doesn't work, cp_convert will complain. */
10346 if (!warn_pmf2ptr
10347 && TYPE_PTR_P (type)
10348 && TYPE_PTRMEMFUNC_P (rhstype))
10349 rhs = cp_convert (strip_top_quals (type), rhs, complain);
10350 else
10352 if (complain & tf_error)
10354 /* If the right-hand side has unknown type, then it is an
10355 overloaded function. Call instantiate_type to get error
10356 messages. */
10357 if (rhstype == unknown_type_node)
10359 tree r = instantiate_type (type, rhs, tf_warning_or_error);
10360 /* -fpermissive might allow this; recurse. */
10361 if (!seen_error ())
10362 return convert_for_assignment (type, r, errtype, fndecl,
10363 parmnum, complain, flags);
10365 else if (fndecl)
10366 complain_about_bad_argument (rhs_loc,
10367 rhstype, type,
10368 fndecl, parmnum);
10369 else
10371 range_label_for_type_mismatch label (rhstype, type);
10372 gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
10373 auto_diagnostic_group d;
10375 switch (errtype)
10377 case ICR_DEFAULT_ARGUMENT:
10378 error_at (&richloc,
10379 "cannot convert %qH to %qI in default argument",
10380 rhstype, type);
10381 break;
10382 case ICR_ARGPASS:
10383 error_at (&richloc,
10384 "cannot convert %qH to %qI in argument passing",
10385 rhstype, type);
10386 break;
10387 case ICR_CONVERTING:
10388 error_at (&richloc, "cannot convert %qH to %qI",
10389 rhstype, type);
10390 break;
10391 case ICR_INIT:
10392 error_at (&richloc,
10393 "cannot convert %qH to %qI in initialization",
10394 rhstype, type);
10395 break;
10396 case ICR_RETURN:
10397 error_at (&richloc, "cannot convert %qH to %qI in return",
10398 rhstype, type);
10399 break;
10400 case ICR_ASSIGN:
10401 error_at (&richloc,
10402 "cannot convert %qH to %qI in assignment",
10403 rhstype, type);
10404 break;
10405 default:
10406 gcc_unreachable();
10410 /* See if we can be more helpful. */
10411 maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10413 if (TYPE_PTR_P (rhstype)
10414 && TYPE_PTR_P (type)
10415 && CLASS_TYPE_P (TREE_TYPE (rhstype))
10416 && CLASS_TYPE_P (TREE_TYPE (type))
10417 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10418 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10419 (TREE_TYPE (rhstype))),
10420 "class type %qT is incomplete", TREE_TYPE (rhstype));
10422 return error_mark_node;
10425 if (warn_suggest_attribute_format)
10427 const enum tree_code codel = TREE_CODE (type);
10428 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10429 && coder == codel
10430 && check_missing_format_attribute (type, rhstype)
10431 && (complain & tf_warning))
10432 switch (errtype)
10434 case ICR_ARGPASS:
10435 case ICR_DEFAULT_ARGUMENT:
10436 if (fndecl)
10437 warning (OPT_Wsuggest_attribute_format,
10438 "parameter %qP of %qD might be a candidate "
10439 "for a format attribute", parmnum, fndecl);
10440 else
10441 warning (OPT_Wsuggest_attribute_format,
10442 "parameter might be a candidate "
10443 "for a format attribute");
10444 break;
10445 case ICR_CONVERTING:
10446 warning (OPT_Wsuggest_attribute_format,
10447 "target of conversion might be a candidate "
10448 "for a format attribute");
10449 break;
10450 case ICR_INIT:
10451 warning (OPT_Wsuggest_attribute_format,
10452 "target of initialization might be a candidate "
10453 "for a format attribute");
10454 break;
10455 case ICR_RETURN:
10456 warning (OPT_Wsuggest_attribute_format,
10457 "return type might be a candidate "
10458 "for a format attribute");
10459 break;
10460 case ICR_ASSIGN:
10461 warning (OPT_Wsuggest_attribute_format,
10462 "left-hand side of assignment might be a candidate "
10463 "for a format attribute");
10464 break;
10465 default:
10466 gcc_unreachable();
10470 if (TREE_CODE (type) == BOOLEAN_TYPE)
10471 maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10473 if (complain & tf_warning)
10474 warn_for_address_of_packed_member (type, rhs);
10476 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10477 complain, flags);
10480 /* Convert RHS to be of type TYPE.
10481 If EXP is nonzero, it is the target of the initialization.
10482 ERRTYPE indicates what kind of error the implicit conversion is.
10484 Two major differences between the behavior of
10485 `convert_for_assignment' and `convert_for_initialization'
10486 are that references are bashed in the former, while
10487 copied in the latter, and aggregates are assigned in
10488 the former (operator=) while initialized in the
10489 latter (X(X&)).
10491 If using constructor make sure no conversion operator exists, if one does
10492 exist, an ambiguity exists. */
10494 tree
10495 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10496 impl_conv_rhs errtype, tree fndecl, int parmnum,
10497 tsubst_flags_t complain)
10499 enum tree_code codel = TREE_CODE (type);
10500 tree rhstype;
10501 enum tree_code coder;
10503 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10504 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10505 if (TREE_CODE (rhs) == NOP_EXPR
10506 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10507 && codel != REFERENCE_TYPE)
10508 rhs = TREE_OPERAND (rhs, 0);
10510 if (type == error_mark_node
10511 || rhs == error_mark_node
10512 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10513 return error_mark_node;
10515 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10517 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10518 && TREE_CODE (type) != ARRAY_TYPE
10519 && (!TYPE_REF_P (type)
10520 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10521 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10522 && !TYPE_REFFN_P (type))
10523 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10524 rhs = decay_conversion (rhs, complain);
10526 rhstype = TREE_TYPE (rhs);
10527 coder = TREE_CODE (rhstype);
10529 if (coder == ERROR_MARK)
10530 return error_mark_node;
10532 /* We accept references to incomplete types, so we can
10533 return here before checking if RHS is of complete type. */
10535 if (codel == REFERENCE_TYPE)
10537 auto_diagnostic_group d;
10538 /* This should eventually happen in convert_arguments. */
10539 int savew = 0, savee = 0;
10541 if (fndecl)
10542 savew = warningcount + werrorcount, savee = errorcount;
10543 rhs = initialize_reference (type, rhs, flags, complain);
10545 if (fndecl
10546 && (warningcount + werrorcount > savew || errorcount > savee))
10547 inform (get_fndecl_argument_location (fndecl, parmnum),
10548 "in passing argument %P of %qD", parmnum, fndecl);
10549 return rhs;
10552 if (exp != 0)
10553 exp = require_complete_type (exp, complain);
10554 if (exp == error_mark_node)
10555 return error_mark_node;
10557 type = complete_type (type);
10559 if (DIRECT_INIT_EXPR_P (type, rhs))
10560 /* Don't try to do copy-initialization if we already have
10561 direct-initialization. */
10562 return rhs;
10564 if (MAYBE_CLASS_TYPE_P (type))
10565 return perform_implicit_conversion_flags (type, rhs, complain, flags);
10567 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10568 complain, flags);
10571 /* If RETVAL is the address of, or a reference to, a local variable or
10572 temporary give an appropriate warning and return true. */
10574 static bool
10575 maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10577 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10578 tree whats_returned = fold_for_warn (retval);
10579 if (!loc)
10580 loc = cp_expr_loc_or_input_loc (retval);
10582 for (;;)
10584 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10585 whats_returned = TREE_OPERAND (whats_returned, 1);
10586 else if (CONVERT_EXPR_P (whats_returned)
10587 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10588 whats_returned = TREE_OPERAND (whats_returned, 0);
10589 else
10590 break;
10593 if (TREE_CODE (whats_returned) == TARGET_EXPR
10594 && is_std_init_list (TREE_TYPE (whats_returned)))
10596 tree init = TARGET_EXPR_INITIAL (whats_returned);
10597 if (TREE_CODE (init) == CONSTRUCTOR)
10598 /* Pull out the array address. */
10599 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10600 else if (TREE_CODE (init) == INDIRECT_REF)
10601 /* The source of a trivial copy looks like *(T*)&var. */
10602 whats_returned = TREE_OPERAND (init, 0);
10603 else
10604 return false;
10605 STRIP_NOPS (whats_returned);
10608 /* As a special case, we handle a call to std::move or std::forward. */
10609 if (TREE_CODE (whats_returned) == CALL_EXPR
10610 && (is_std_move_p (whats_returned)
10611 || is_std_forward_p (whats_returned)))
10613 tree arg = CALL_EXPR_ARG (whats_returned, 0);
10614 return maybe_warn_about_returning_address_of_local (arg, loc);
10617 if (TREE_CODE (whats_returned) != ADDR_EXPR)
10618 return false;
10619 whats_returned = TREE_OPERAND (whats_returned, 0);
10621 while (TREE_CODE (whats_returned) == COMPONENT_REF
10622 || TREE_CODE (whats_returned) == ARRAY_REF)
10623 whats_returned = TREE_OPERAND (whats_returned, 0);
10625 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10626 || TREE_CODE (whats_returned) == TARGET_EXPR)
10628 if (TYPE_REF_P (valtype))
10629 warning_at (loc, OPT_Wreturn_local_addr,
10630 "returning reference to temporary");
10631 else if (TYPE_PTR_P (valtype))
10632 warning_at (loc, OPT_Wreturn_local_addr,
10633 "returning pointer to temporary");
10634 else if (is_std_init_list (valtype))
10635 warning_at (loc, OPT_Winit_list_lifetime,
10636 "returning temporary %<initializer_list%> does not extend "
10637 "the lifetime of the underlying array");
10638 return true;
10641 STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10643 if (DECL_P (whats_returned)
10644 && DECL_NAME (whats_returned)
10645 && DECL_FUNCTION_SCOPE_P (whats_returned)
10646 && !is_capture_proxy (whats_returned)
10647 && !(TREE_STATIC (whats_returned)
10648 || TREE_PUBLIC (whats_returned)))
10650 if (VAR_P (whats_returned)
10651 && DECL_DECOMPOSITION_P (whats_returned)
10652 && DECL_DECOMP_BASE (whats_returned)
10653 && DECL_HAS_VALUE_EXPR_P (whats_returned))
10655 /* When returning address of a structured binding, if the structured
10656 binding is not a reference, continue normally, if it is a
10657 reference, recurse on the initializer of the structured
10658 binding. */
10659 tree base = DECL_DECOMP_BASE (whats_returned);
10660 if (TYPE_REF_P (TREE_TYPE (base)))
10662 if (tree init = DECL_INITIAL (base))
10663 return maybe_warn_about_returning_address_of_local (init, loc);
10664 else
10665 return false;
10668 bool w = false;
10669 auto_diagnostic_group d;
10670 if (TYPE_REF_P (valtype))
10671 w = warning_at (loc, OPT_Wreturn_local_addr,
10672 "reference to local variable %qD returned",
10673 whats_returned);
10674 else if (is_std_init_list (valtype))
10675 w = warning_at (loc, OPT_Winit_list_lifetime,
10676 "returning local %<initializer_list%> variable %qD "
10677 "does not extend the lifetime of the underlying array",
10678 whats_returned);
10679 else if (POINTER_TYPE_P (valtype)
10680 && TREE_CODE (whats_returned) == LABEL_DECL)
10681 w = warning_at (loc, OPT_Wreturn_local_addr,
10682 "address of label %qD returned",
10683 whats_returned);
10684 else if (POINTER_TYPE_P (valtype))
10685 w = warning_at (loc, OPT_Wreturn_local_addr,
10686 "address of local variable %qD returned",
10687 whats_returned);
10688 if (w)
10689 inform (DECL_SOURCE_LOCATION (whats_returned),
10690 "declared here");
10691 return true;
10694 return false;
10697 /* Returns true if DECL is in the std namespace. */
10699 bool
10700 decl_in_std_namespace_p (tree decl)
10702 while (decl)
10704 decl = decl_namespace_context (decl);
10705 if (DECL_NAMESPACE_STD_P (decl))
10706 return true;
10707 /* Allow inline namespaces inside of std namespace, e.g. with
10708 --enable-symvers=gnu-versioned-namespace std::forward would be
10709 actually std::_8::forward. */
10710 if (!DECL_NAMESPACE_INLINE_P (decl))
10711 return false;
10712 decl = CP_DECL_CONTEXT (decl);
10714 return false;
10717 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10719 static bool
10720 is_std_forward_p (tree fn)
10722 /* std::forward only takes one argument. */
10723 if (call_expr_nargs (fn) != 1)
10724 return false;
10726 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10727 if (!decl_in_std_namespace_p (fndecl))
10728 return false;
10730 tree name = DECL_NAME (fndecl);
10731 return name && id_equal (name, "forward");
10734 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10736 static bool
10737 is_std_move_p (tree fn)
10739 /* std::move only takes one argument. */
10740 if (call_expr_nargs (fn) != 1)
10741 return false;
10743 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10744 if (!decl_in_std_namespace_p (fndecl))
10745 return false;
10747 tree name = DECL_NAME (fndecl);
10748 return name && id_equal (name, "move");
10751 /* Returns true if RETVAL is a good candidate for the NRVO as per
10752 [class.copy.elision]. FUNCTYPE is the type the function is declared
10753 to return. */
10755 static bool
10756 can_do_nrvo_p (tree retval, tree functype)
10758 if (functype == error_mark_node)
10759 return false;
10760 if (retval)
10761 STRIP_ANY_LOCATION_WRAPPER (retval);
10762 tree result = DECL_RESULT (current_function_decl);
10763 return (retval != NULL_TREE
10764 && !processing_template_decl
10765 /* Must be a local, automatic variable. */
10766 && VAR_P (retval)
10767 && DECL_CONTEXT (retval) == current_function_decl
10768 && !TREE_STATIC (retval)
10769 /* And not a lambda or anonymous union proxy. */
10770 && !DECL_HAS_VALUE_EXPR_P (retval)
10771 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10772 /* The cv-unqualified type of the returned value must be the
10773 same as the cv-unqualified return type of the
10774 function. */
10775 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10776 TYPE_MAIN_VARIANT (functype))
10777 /* And the returned value must be non-volatile. */
10778 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10781 /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10782 would otherwise return in memory. */
10784 static bool
10785 want_nrvo_p (tree retval, tree functype)
10787 return (can_do_nrvo_p (retval, functype)
10788 && aggregate_value_p (functype, current_function_decl));
10791 /* Like can_do_nrvo_p, but we check if we're trying to move a class
10792 prvalue. */
10794 static bool
10795 can_elide_copy_prvalue_p (tree retval, tree functype)
10797 if (functype == error_mark_node)
10798 return false;
10799 if (retval)
10800 STRIP_ANY_LOCATION_WRAPPER (retval);
10801 return (retval != NULL_TREE
10802 && !glvalue_p (retval)
10803 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10804 TYPE_MAIN_VARIANT (functype))
10805 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10808 /* If we should treat RETVAL, an expression being returned, as if it were
10809 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10810 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10811 context (rather than throw). */
10813 tree
10814 treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10816 if (cxx_dialect == cxx98)
10817 return NULL_TREE;
10819 tree retval = expr;
10820 STRIP_ANY_LOCATION_WRAPPER (retval);
10821 if (REFERENCE_REF_P (retval))
10822 retval = TREE_OPERAND (retval, 0);
10824 /* An implicitly movable entity is a variable of automatic storage duration
10825 that is either a non-volatile object or (C++20) an rvalue reference to a
10826 non-volatile object type. */
10827 if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
10828 || TREE_CODE (retval) == PARM_DECL)
10829 && !TREE_STATIC (retval)
10830 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
10831 && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
10832 || (cxx_dialect >= cxx20
10833 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
10834 return NULL_TREE;
10836 /* If the expression in a return or co_return statement is a (possibly
10837 parenthesized) id-expression that names an implicitly movable entity
10838 declared in the body or parameter-declaration-clause of the innermost
10839 enclosing function or lambda-expression, */
10840 if (DECL_CONTEXT (retval) != current_function_decl)
10841 return NULL_TREE;
10842 if (return_p)
10844 expr = move (expr);
10845 if (expr == error_mark_node)
10846 return NULL_TREE;
10847 return set_implicit_rvalue_p (expr);
10850 /* if the operand of a throw-expression is a (possibly parenthesized)
10851 id-expression that names an implicitly movable entity whose scope does not
10852 extend beyond the compound-statement of the innermost try-block or
10853 function-try-block (if any) whose compound-statement or ctor-initializer
10854 encloses the throw-expression, */
10856 /* C++20 added move on throw of parms. */
10857 if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
10858 return NULL_TREE;
10860 for (cp_binding_level *b = current_binding_level;
10861 ; b = b->level_chain)
10863 for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
10864 if (decl == retval)
10865 return set_implicit_rvalue_p (move (expr));
10866 if (b->kind == sk_function_parms
10867 || b->kind == sk_try
10868 || b->kind == sk_namespace)
10869 return NULL_TREE;
10873 /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10874 is true). EXPR is the std::move expression; TYPE is the type of the object
10875 being initialized. */
10877 void
10878 maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
10880 if (!(warn_pessimizing_move || warn_redundant_move))
10881 return;
10883 const location_t loc = cp_expr_loc_or_input_loc (expr);
10885 /* C++98 doesn't know move. */
10886 if (cxx_dialect < cxx11)
10887 return;
10889 /* Wait until instantiation time, since we can't gauge if we should do
10890 the NRVO until then. */
10891 if (processing_template_decl)
10892 return;
10894 /* This is only interesting for class types. */
10895 if (!CLASS_TYPE_P (type))
10896 return;
10898 bool wrapped_p = false;
10899 /* A a = std::move (A()); */
10900 if (TREE_CODE (expr) == TREE_LIST)
10902 if (list_length (expr) == 1)
10904 expr = TREE_VALUE (expr);
10905 wrapped_p = true;
10907 else
10908 return;
10910 /* A a = {std::move (A())};
10911 A a{std::move (A())}; */
10912 else if (TREE_CODE (expr) == CONSTRUCTOR)
10914 if (CONSTRUCTOR_NELTS (expr) == 1)
10916 expr = CONSTRUCTOR_ELT (expr, 0)->value;
10917 wrapped_p = true;
10919 else
10920 return;
10923 /* First, check if this is a call to std::move. */
10924 if (!REFERENCE_REF_P (expr)
10925 || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
10926 return;
10927 tree fn = TREE_OPERAND (expr, 0);
10928 if (!is_std_move_p (fn))
10929 return;
10930 tree arg = CALL_EXPR_ARG (fn, 0);
10931 if (TREE_CODE (arg) != NOP_EXPR)
10932 return;
10933 /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
10934 and implicitly-movable warnings. */
10935 if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
10937 arg = TREE_OPERAND (arg, 0);
10938 arg = TREE_OPERAND (arg, 0);
10939 arg = convert_from_reference (arg);
10940 if (can_elide_copy_prvalue_p (arg, type))
10942 auto_diagnostic_group d;
10943 if (warning_at (loc, OPT_Wpessimizing_move,
10944 "moving a temporary object prevents copy elision"))
10945 inform (loc, "remove %<std::move%> call");
10947 /* The rest of the warnings is only relevant for when we are returning
10948 from a function. */
10949 if (!return_p)
10950 return;
10952 tree moved;
10953 /* Warn if we could do copy elision were it not for the move. */
10954 if (can_do_nrvo_p (arg, type))
10956 auto_diagnostic_group d;
10957 if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
10958 && warning_at (loc, OPT_Wpessimizing_move,
10959 "moving a local object in a return statement "
10960 "prevents copy elision"))
10961 inform (loc, "remove %<std::move%> call");
10963 /* Warn if the move is redundant. It is redundant when we would
10964 do maybe-rvalue overload resolution even without std::move. */
10965 else if (warn_redundant_move
10966 /* This doesn't apply for return {std::move (t)};. */
10967 && !wrapped_p
10968 && !warning_suppressed_p (expr, OPT_Wredundant_move)
10969 && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
10971 /* Make sure that overload resolution would actually succeed
10972 if we removed the std::move call. */
10973 tree t = convert_for_initialization (NULL_TREE, type,
10974 moved,
10975 (LOOKUP_NORMAL
10976 | LOOKUP_ONLYCONVERTING),
10977 ICR_RETURN, NULL_TREE, 0,
10978 tf_none);
10979 /* If this worked, implicit rvalue would work, so the call to
10980 std::move is redundant. */
10981 if (t != error_mark_node)
10983 auto_diagnostic_group d;
10984 if (warning_at (loc, OPT_Wredundant_move,
10985 "redundant move in return statement"))
10986 inform (loc, "remove %<std::move%> call");
10990 /* Also try to warn about redundant std::move in code such as
10991 T f (const T& t)
10993 return std::move(t);
10995 for which EXPR will be something like
10996 *std::move<const T&> ((const struct T &) (const struct T *) t)
10997 and where the std::move does nothing if T does not have a T(const T&&)
10998 constructor, because the argument is const. It will not use T(T&&)
10999 because that would mean losing the const. */
11000 else if (warn_redundant_move
11001 && !warning_suppressed_p (expr, OPT_Wredundant_move)
11002 && TYPE_REF_P (TREE_TYPE (arg))
11003 && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11005 tree rtype = TREE_TYPE (TREE_TYPE (arg));
11006 if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11007 return;
11008 /* Check for the unlikely case there's T(const T&&) (we don't care if
11009 it's deleted). */
11010 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11011 if (move_fn_p (fn))
11013 tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11014 if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11015 return;
11017 auto_diagnostic_group d;
11018 if (return_p
11019 ? warning_at (loc, OPT_Wredundant_move,
11020 "redundant move in return statement")
11021 : warning_at (loc, OPT_Wredundant_move,
11022 "redundant move in initialization"))
11023 inform (loc, "remove %<std::move%> call");
11027 /* Check that returning RETVAL from the current function is valid.
11028 Return an expression explicitly showing all conversions required to
11029 change RETVAL into the function return type, and to assign it to
11030 the DECL_RESULT for the function. Set *NO_WARNING to true if
11031 code reaches end of non-void function warning shouldn't be issued
11032 on this RETURN_EXPR. Set *DANGLING to true if code returns the
11033 address of a local variable. */
11035 tree
11036 check_return_expr (tree retval, bool *no_warning, bool *dangling)
11038 tree result;
11039 /* The type actually returned by the function. */
11040 tree valtype;
11041 /* The type the function is declared to return, or void if
11042 the declared type is incomplete. */
11043 tree functype;
11044 int fn_returns_value_p;
11045 location_t loc = cp_expr_loc_or_input_loc (retval);
11047 *no_warning = false;
11048 *dangling = false;
11050 /* A `volatile' function is one that isn't supposed to return, ever.
11051 (This is a G++ extension, used to get better code for functions
11052 that call the `volatile' function.) */
11053 if (TREE_THIS_VOLATILE (current_function_decl))
11054 warning (0, "function declared %<noreturn%> has a %<return%> statement");
11056 /* Check for various simple errors. */
11057 if (DECL_DESTRUCTOR_P (current_function_decl))
11059 if (retval)
11060 error_at (loc, "returning a value from a destructor");
11062 if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11063 retval = current_class_ptr;
11064 else
11065 return NULL_TREE;
11067 else if (DECL_CONSTRUCTOR_P (current_function_decl))
11069 if (in_function_try_handler)
11070 /* If a return statement appears in a handler of the
11071 function-try-block of a constructor, the program is ill-formed. */
11072 error ("cannot return from a handler of a function-try-block of a constructor");
11073 else if (retval)
11074 /* You can't return a value from a constructor. */
11075 error_at (loc, "returning a value from a constructor");
11077 if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11078 retval = current_class_ptr;
11079 else
11080 return NULL_TREE;
11083 const tree saved_retval = retval;
11085 if (processing_template_decl)
11087 current_function_returns_value = 1;
11089 if (check_for_bare_parameter_packs (retval))
11090 return error_mark_node;
11092 /* If one of the types might be void, we can't tell whether we're
11093 returning a value. */
11094 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11095 && !FNDECL_USED_AUTO (current_function_decl))
11096 || (retval != NULL_TREE
11097 && (TREE_TYPE (retval) == NULL_TREE
11098 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11099 goto dependent;
11102 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11104 /* Deduce auto return type from a return statement. */
11105 if (FNDECL_USED_AUTO (current_function_decl))
11107 tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11108 tree auto_node;
11109 tree type;
11111 if (!retval && !is_auto (pattern))
11113 /* Give a helpful error message. */
11114 error ("return-statement with no value, in function returning %qT",
11115 pattern);
11116 inform (input_location, "only plain %<auto%> return type can be "
11117 "deduced to %<void%>");
11118 type = error_mark_node;
11120 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11122 error ("returning initializer list");
11123 type = error_mark_node;
11125 else
11127 if (!retval)
11128 retval = void_node;
11129 auto_node = type_uses_auto (pattern);
11130 type = do_auto_deduction (pattern, retval, auto_node,
11131 tf_warning_or_error, adc_return_type);
11134 if (type == error_mark_node)
11135 /* Leave it. */;
11136 else if (functype == pattern)
11137 apply_deduced_return_type (current_function_decl, type);
11138 else if (!same_type_p (type, functype))
11140 if (LAMBDA_FUNCTION_P (current_function_decl))
11141 error_at (loc, "inconsistent types %qT and %qT deduced for "
11142 "lambda return type", functype, type);
11143 else
11144 error_at (loc, "inconsistent deduction for auto return type: "
11145 "%qT and then %qT", functype, type);
11147 functype = type;
11150 result = DECL_RESULT (current_function_decl);
11151 valtype = TREE_TYPE (result);
11152 gcc_assert (valtype != NULL_TREE);
11153 fn_returns_value_p = !VOID_TYPE_P (valtype);
11155 /* Check for a return statement with no return value in a function
11156 that's supposed to return a value. */
11157 if (!retval && fn_returns_value_p)
11159 if (functype != error_mark_node)
11160 permerror (input_location, "return-statement with no value, in "
11161 "function returning %qT", valtype);
11162 /* Remember that this function did return. */
11163 current_function_returns_value = 1;
11164 /* And signal caller that TREE_NO_WARNING should be set on the
11165 RETURN_EXPR to avoid control reaches end of non-void function
11166 warnings in tree-cfg.cc. */
11167 *no_warning = true;
11169 /* Check for a return statement with a value in a function that
11170 isn't supposed to return a value. */
11171 else if (retval && !fn_returns_value_p)
11173 if (VOID_TYPE_P (TREE_TYPE (retval)))
11174 /* You can return a `void' value from a function of `void'
11175 type. In that case, we have to evaluate the expression for
11176 its side-effects. */
11177 finish_expr_stmt (retval);
11178 else if (retval != error_mark_node)
11179 permerror (loc, "return-statement with a value, in function "
11180 "returning %qT", valtype);
11181 current_function_returns_null = 1;
11183 /* There's really no value to return, after all. */
11184 return NULL_TREE;
11186 else if (!retval)
11187 /* Remember that this function can sometimes return without a
11188 value. */
11189 current_function_returns_null = 1;
11190 else
11191 /* Remember that this function did return a value. */
11192 current_function_returns_value = 1;
11194 /* Check for erroneous operands -- but after giving ourselves a
11195 chance to provide an error about returning a value from a void
11196 function. */
11197 if (error_operand_p (retval))
11199 current_function_return_value = error_mark_node;
11200 return error_mark_node;
11203 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11204 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11205 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11206 && ! flag_check_new
11207 && retval && null_ptr_cst_p (retval))
11208 warning (0, "%<operator new%> must not return NULL unless it is "
11209 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11211 /* Effective C++ rule 15. See also start_function. */
11212 if (warn_ecpp
11213 && DECL_NAME (current_function_decl) == assign_op_identifier
11214 && !type_dependent_expression_p (retval))
11216 bool warn = true;
11218 /* The function return type must be a reference to the current
11219 class. */
11220 if (TYPE_REF_P (valtype)
11221 && same_type_ignoring_top_level_qualifiers_p
11222 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11224 /* Returning '*this' is obviously OK. */
11225 if (retval == current_class_ref)
11226 warn = false;
11227 /* If we are calling a function whose return type is the same of
11228 the current class reference, it is ok. */
11229 else if (INDIRECT_REF_P (retval)
11230 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11231 warn = false;
11234 if (warn)
11235 warning_at (loc, OPT_Weffc__,
11236 "%<operator=%> should return a reference to %<*this%>");
11239 if (dependent_type_p (functype)
11240 || type_dependent_expression_p (retval))
11242 dependent:
11243 /* We should not have changed the return value. */
11244 gcc_assert (retval == saved_retval);
11245 /* We don't know if this is an lvalue or rvalue use, but
11246 either way we can mark it as read. */
11247 mark_exp_read (retval);
11248 return retval;
11251 /* The fabled Named Return Value optimization, as per [class.copy]/15:
11253 [...] For a function with a class return type, if the expression
11254 in the return statement is the name of a local object, and the cv-
11255 unqualified type of the local object is the same as the function
11256 return type, an implementation is permitted to omit creating the tem-
11257 porary object to hold the function return value [...]
11259 So, if this is a value-returning function that always returns the same
11260 local variable, remember it.
11262 We choose the first suitable variable even if the function sometimes
11263 returns something else, but only if the variable is out of scope at the
11264 other return sites, or else we run the risk of clobbering the variable we
11265 chose if the other returned expression uses the chosen variable somehow.
11267 We don't currently do this if the first return is a non-variable, as it
11268 would be complicated to determine whether an NRV selected later was in
11269 scope at the point of the earlier return. We also don't currently support
11270 multiple variables with non-overlapping scopes (53637).
11272 See finish_function and finalize_nrv for the rest of this optimization. */
11273 tree bare_retval = NULL_TREE;
11274 if (retval)
11276 retval = maybe_undo_parenthesized_ref (retval);
11277 bare_retval = tree_strip_any_location_wrapper (retval);
11280 bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11281 if (fn_returns_value_p && flag_elide_constructors
11282 && current_function_return_value != bare_retval)
11284 if (named_return_value_okay_p
11285 && current_function_return_value == NULL_TREE)
11286 current_function_return_value = bare_retval;
11287 else if (current_function_return_value
11288 && VAR_P (current_function_return_value)
11289 && DECL_NAME (current_function_return_value)
11290 && !decl_in_scope_p (current_function_return_value))
11292 /* The earlier NRV is out of scope at this point, so it's safe to
11293 leave it alone; the current return can't refer to it. */;
11294 if (named_return_value_okay_p
11295 && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11297 warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11298 bare_retval);
11299 suppress_warning (current_function_decl, OPT_Wnrvo);
11302 else
11304 if ((named_return_value_okay_p
11305 || (current_function_return_value
11306 && current_function_return_value != error_mark_node))
11307 && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11309 warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11310 current_function_decl);
11311 suppress_warning (current_function_decl, OPT_Wnrvo);
11313 current_function_return_value = error_mark_node;
11317 /* We don't need to do any conversions when there's nothing being
11318 returned. */
11319 if (!retval)
11320 return NULL_TREE;
11322 if (!named_return_value_okay_p)
11323 maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11325 /* Do any required conversions. */
11326 if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11327 /* No conversions are required. */
11329 else
11331 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11333 /* The functype's return type will have been set to void, if it
11334 was an incomplete type. Just treat this as 'return;' */
11335 if (VOID_TYPE_P (functype))
11336 return error_mark_node;
11338 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11339 treated as an rvalue for the purposes of overload resolution to
11340 favor move constructors over copy constructors.
11342 Note that these conditions are similar to, but not as strict as,
11343 the conditions for the named return value optimization. */
11344 bool converted = false;
11345 tree moved;
11346 /* Until C++23, this was only interesting for class type, but in C++23,
11347 we should do the below when we're converting rom/to a class/reference
11348 (a non-scalar type). */
11349 if ((cxx_dialect < cxx23
11350 ? CLASS_TYPE_P (functype)
11351 : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11352 && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11353 /* In C++20 and earlier we treat the return value as an rvalue
11354 that can bind to lvalue refs. In C++23, such an expression is just
11355 an xvalue (see reference_binding). */
11356 retval = moved;
11358 /* The call in a (lambda) thunk needs no conversions. */
11359 if (TREE_CODE (retval) == CALL_EXPR
11360 && call_from_lambda_thunk_p (retval))
11361 converted = true;
11363 /* First convert the value to the function's return type, then
11364 to the type of return value's location to handle the
11365 case that functype is smaller than the valtype. */
11366 if (!converted)
11367 retval = convert_for_initialization
11368 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11369 tf_warning_or_error);
11370 retval = convert (valtype, retval);
11372 /* If the conversion failed, treat this just like `return;'. */
11373 if (retval == error_mark_node)
11374 return retval;
11375 /* We can't initialize a register from a AGGR_INIT_EXPR. */
11376 else if (! cfun->returns_struct
11377 && TREE_CODE (retval) == TARGET_EXPR
11378 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
11379 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11380 TREE_OPERAND (retval, 0));
11381 else if (!processing_template_decl
11382 && maybe_warn_about_returning_address_of_local (retval, loc)
11383 && INDIRECT_TYPE_P (valtype))
11384 *dangling = true;
11387 /* A naive attempt to reduce the number of -Wdangling-reference false
11388 positives: if we know that this function can return a variable with
11389 static storage duration rather than one of its parameters, suppress
11390 the warning. */
11391 if (warn_dangling_reference
11392 && TYPE_REF_P (functype)
11393 && bare_retval
11394 && VAR_P (bare_retval)
11395 && TREE_STATIC (bare_retval))
11396 suppress_warning (current_function_decl, OPT_Wdangling_reference);
11398 if (processing_template_decl)
11399 return saved_retval;
11401 /* Actually copy the value returned into the appropriate location. */
11402 if (retval && retval != result)
11404 /* If there's a postcondition for a scalar return value, wrap
11405 retval in a call to the postcondition function. */
11406 if (tree post = apply_postcondition_to_return (retval))
11407 retval = post;
11408 retval = cp_build_init_expr (result, retval);
11411 if (current_function_return_value == bare_retval)
11412 INIT_EXPR_NRV_P (retval) = true;
11414 if (tree set = maybe_set_retval_sentinel ())
11415 retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11417 /* If there's a postcondition for an aggregate return value, call the
11418 postcondition function after the return object is initialized. */
11419 if (tree post = apply_postcondition_to_return (result))
11420 retval = build2 (COMPOUND_EXPR, void_type_node, retval, post);
11422 return retval;
11426 /* Returns nonzero if the pointer-type FROM can be converted to the
11427 pointer-type TO via a qualification conversion. If CONSTP is -1,
11428 then we return nonzero if the pointers are similar, and the
11429 cv-qualification signature of FROM is a proper subset of that of TO.
11431 If CONSTP is positive, then all outer pointers have been
11432 const-qualified. */
11434 static bool
11435 comp_ptr_ttypes_real (tree to, tree from, int constp)
11437 bool to_more_cv_qualified = false;
11438 bool is_opaque_pointer = false;
11440 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11442 if (TREE_CODE (to) != TREE_CODE (from))
11443 return false;
11445 if (TREE_CODE (from) == OFFSET_TYPE
11446 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11447 TYPE_OFFSET_BASETYPE (to)))
11448 return false;
11450 /* Const and volatile mean something different for function and
11451 array types, so the usual checks are not appropriate. We'll
11452 check the array type elements in further iterations. */
11453 if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11455 if (!at_least_as_qualified_p (to, from))
11456 return false;
11458 if (!at_least_as_qualified_p (from, to))
11460 if (constp == 0)
11461 return false;
11462 to_more_cv_qualified = true;
11465 if (constp > 0)
11466 constp &= TYPE_READONLY (to);
11469 if (VECTOR_TYPE_P (to))
11470 is_opaque_pointer = vector_targets_convertible_p (to, from);
11472 /* P0388R4 allows a conversion from int[N] to int[] but not the
11473 other way round. When both arrays have bounds but they do
11474 not match, then no conversion is possible. */
11475 if (TREE_CODE (to) == ARRAY_TYPE
11476 && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11477 return false;
11479 if (!TYPE_PTR_P (to)
11480 && !TYPE_PTRDATAMEM_P (to)
11481 /* CWG 330 says we need to look through arrays. */
11482 && TREE_CODE (to) != ARRAY_TYPE)
11483 return ((constp >= 0 || to_more_cv_qualified)
11484 && (is_opaque_pointer
11485 || same_type_ignoring_top_level_qualifiers_p (to, from)));
11489 /* When comparing, say, char ** to char const **, this function takes
11490 the 'char *' and 'char const *'. Do not pass non-pointer/reference
11491 types to this function. */
11494 comp_ptr_ttypes (tree to, tree from)
11496 return comp_ptr_ttypes_real (to, from, 1);
11499 /* Returns true iff FNTYPE is a non-class type that involves
11500 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11501 if a parameter type is ill-formed. */
11503 bool
11504 error_type_p (const_tree type)
11506 tree t;
11508 switch (TREE_CODE (type))
11510 case ERROR_MARK:
11511 return true;
11513 case POINTER_TYPE:
11514 case REFERENCE_TYPE:
11515 case OFFSET_TYPE:
11516 return error_type_p (TREE_TYPE (type));
11518 case FUNCTION_TYPE:
11519 case METHOD_TYPE:
11520 if (error_type_p (TREE_TYPE (type)))
11521 return true;
11522 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11523 if (error_type_p (TREE_VALUE (t)))
11524 return true;
11525 return false;
11527 case RECORD_TYPE:
11528 if (TYPE_PTRMEMFUNC_P (type))
11529 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11530 return false;
11532 default:
11533 return false;
11537 /* Returns true if to and from are (possibly multi-level) pointers to the same
11538 type or inheritance-related types, regardless of cv-quals. */
11540 bool
11541 ptr_reasonably_similar (const_tree to, const_tree from)
11543 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11545 /* Any target type is similar enough to void. */
11546 if (VOID_TYPE_P (to))
11547 return !error_type_p (from);
11548 if (VOID_TYPE_P (from))
11549 return !error_type_p (to);
11551 if (TREE_CODE (to) != TREE_CODE (from))
11552 return false;
11554 if (TREE_CODE (from) == OFFSET_TYPE
11555 && comptypes (TYPE_OFFSET_BASETYPE (to),
11556 TYPE_OFFSET_BASETYPE (from),
11557 COMPARE_BASE | COMPARE_DERIVED))
11558 continue;
11560 if (VECTOR_TYPE_P (to)
11561 && vector_types_convertible_p (to, from, false))
11562 return true;
11564 if (TREE_CODE (to) == INTEGER_TYPE
11565 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11566 return true;
11568 if (TREE_CODE (to) == FUNCTION_TYPE)
11569 return !error_type_p (to) && !error_type_p (from);
11571 if (!TYPE_PTR_P (to))
11573 /* When either type is incomplete avoid DERIVED_FROM_P,
11574 which may call complete_type (c++/57942). */
11575 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11576 return comptypes
11577 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11578 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11583 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11584 pointer-to-member types) are the same, ignoring cv-qualification at
11585 all levels. CB says how we should behave when comparing array bounds. */
11587 bool
11588 comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11590 bool is_opaque_pointer = false;
11592 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11594 if (TREE_CODE (to) != TREE_CODE (from))
11595 return false;
11597 if (TREE_CODE (from) == OFFSET_TYPE
11598 && same_type_p (TYPE_OFFSET_BASETYPE (from),
11599 TYPE_OFFSET_BASETYPE (to)))
11600 continue;
11602 if (VECTOR_TYPE_P (to))
11603 is_opaque_pointer = vector_targets_convertible_p (to, from);
11605 if (TREE_CODE (to) == ARRAY_TYPE
11606 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11607 we must fail. */
11608 && !comp_array_types (to, from, cb, /*strict=*/false))
11609 return false;
11611 /* CWG 330 says we need to look through arrays. */
11612 if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11613 return (is_opaque_pointer
11614 || same_type_ignoring_top_level_qualifiers_p (to, from));
11618 /* Returns the type qualifiers for this type, including the qualifiers on the
11619 elements for an array type. */
11622 cp_type_quals (const_tree type)
11624 int quals;
11625 /* This CONST_CAST is okay because strip_array_types returns its
11626 argument unmodified and we assign it to a const_tree. */
11627 type = strip_array_types (CONST_CAST_TREE (type));
11628 if (type == error_mark_node
11629 /* Quals on a FUNCTION_TYPE are memfn quals. */
11630 || TREE_CODE (type) == FUNCTION_TYPE)
11631 return TYPE_UNQUALIFIED;
11632 quals = TYPE_QUALS (type);
11633 /* METHOD and REFERENCE_TYPEs should never have quals. */
11634 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11635 && !TYPE_REF_P (type))
11636 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11637 == TYPE_UNQUALIFIED));
11638 return quals;
11641 /* Returns the function-ref-qualifier for TYPE */
11643 cp_ref_qualifier
11644 type_memfn_rqual (const_tree type)
11646 gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11648 if (!FUNCTION_REF_QUALIFIED (type))
11649 return REF_QUAL_NONE;
11650 else if (FUNCTION_RVALUE_QUALIFIED (type))
11651 return REF_QUAL_RVALUE;
11652 else
11653 return REF_QUAL_LVALUE;
11656 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11657 METHOD_TYPE. */
11660 type_memfn_quals (const_tree type)
11662 if (TREE_CODE (type) == FUNCTION_TYPE)
11663 return TYPE_QUALS (type);
11664 else if (TREE_CODE (type) == METHOD_TYPE)
11665 return cp_type_quals (class_of_this_parm (type));
11666 else
11667 gcc_unreachable ();
11670 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11671 MEMFN_QUALS and its ref-qualifier to RQUAL. */
11673 tree
11674 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11676 /* Could handle METHOD_TYPE here if necessary. */
11677 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11678 if (TYPE_QUALS (type) == memfn_quals
11679 && type_memfn_rqual (type) == rqual)
11680 return type;
11682 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11683 complex. */
11684 tree result = build_qualified_type (type, memfn_quals);
11685 return build_ref_qualified_type (result, rqual);
11688 /* Returns nonzero if TYPE is const or volatile. */
11690 bool
11691 cv_qualified_p (const_tree type)
11693 int quals = cp_type_quals (type);
11694 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11697 /* Returns nonzero if the TYPE contains a mutable member. */
11699 bool
11700 cp_has_mutable_p (const_tree type)
11702 /* This CONST_CAST is okay because strip_array_types returns its
11703 argument unmodified and we assign it to a const_tree. */
11704 type = strip_array_types (CONST_CAST_TREE(type));
11706 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11709 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11710 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11711 approximation. In particular, consider:
11713 int f();
11714 struct S { int i; };
11715 const S s = { f(); }
11717 Here, we will make "s" as TREE_READONLY (because it is declared
11718 "const") -- only to reverse ourselves upon seeing that the
11719 initializer is non-constant. */
11721 void
11722 cp_apply_type_quals_to_decl (int type_quals, tree decl)
11724 tree type = TREE_TYPE (decl);
11726 if (type == error_mark_node)
11727 return;
11729 if (TREE_CODE (decl) == TYPE_DECL)
11730 return;
11732 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11733 && type_quals != TYPE_UNQUALIFIED));
11735 /* Avoid setting TREE_READONLY incorrectly. */
11736 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11737 constructor can produce constant init, so rely on cp_finish_decl to
11738 clear TREE_READONLY if the variable has non-constant init. */
11740 /* If the type has (or might have) a mutable component, that component
11741 might be modified. */
11742 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11743 type_quals &= ~TYPE_QUAL_CONST;
11745 c_apply_type_quals_to_decl (type_quals, decl);
11748 /* Subroutine of casts_away_constness. Make T1 and T2 point at
11749 exemplar types such that casting T1 to T2 is casting away constness
11750 if and only if there is no implicit conversion from T1 to T2. */
11752 static void
11753 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11755 int quals1;
11756 int quals2;
11758 /* [expr.const.cast]
11760 For multi-level pointer to members and multi-level mixed pointers
11761 and pointers to members (conv.qual), the "member" aspect of a
11762 pointer to member level is ignored when determining if a const
11763 cv-qualifier has been cast away. */
11764 /* [expr.const.cast]
11766 For two pointer types:
11768 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11769 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11770 K is min(N,M)
11772 casting from X1 to X2 casts away constness if, for a non-pointer
11773 type T there does not exist an implicit conversion (clause
11774 _conv_) from:
11776 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11780 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11781 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11782 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11784 *t1 = cp_build_qualified_type (void_type_node,
11785 cp_type_quals (*t1));
11786 *t2 = cp_build_qualified_type (void_type_node,
11787 cp_type_quals (*t2));
11788 return;
11791 quals1 = cp_type_quals (*t1);
11792 quals2 = cp_type_quals (*t2);
11794 if (TYPE_PTRDATAMEM_P (*t1))
11795 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11796 else
11797 *t1 = TREE_TYPE (*t1);
11798 if (TYPE_PTRDATAMEM_P (*t2))
11799 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11800 else
11801 *t2 = TREE_TYPE (*t2);
11803 casts_away_constness_r (t1, t2, complain);
11804 *t1 = build_pointer_type (*t1);
11805 *t2 = build_pointer_type (*t2);
11806 *t1 = cp_build_qualified_type (*t1, quals1);
11807 *t2 = cp_build_qualified_type (*t2, quals2);
11810 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11811 constness.
11813 ??? This function returns non-zero if casting away qualifiers not
11814 just const. We would like to return to the caller exactly which
11815 qualifiers are casted away to give more accurate diagnostics.
11818 static bool
11819 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
11821 if (TYPE_REF_P (t2))
11823 /* [expr.const.cast]
11825 Casting from an lvalue of type T1 to an lvalue of type T2
11826 using a reference cast casts away constness if a cast from an
11827 rvalue of type "pointer to T1" to the type "pointer to T2"
11828 casts away constness. */
11829 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
11830 return casts_away_constness (build_pointer_type (t1),
11831 build_pointer_type (TREE_TYPE (t2)),
11832 complain);
11835 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
11836 /* [expr.const.cast]
11838 Casting from an rvalue of type "pointer to data member of X
11839 of type T1" to the type "pointer to data member of Y of type
11840 T2" casts away constness if a cast from an rvalue of type
11841 "pointer to T1" to the type "pointer to T2" casts away
11842 constness. */
11843 return casts_away_constness
11844 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
11845 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
11846 complain);
11848 /* Casting away constness is only something that makes sense for
11849 pointer or reference types. */
11850 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
11851 return false;
11853 /* Top-level qualifiers don't matter. */
11854 t1 = TYPE_MAIN_VARIANT (t1);
11855 t2 = TYPE_MAIN_VARIANT (t2);
11856 casts_away_constness_r (&t1, &t2, complain);
11857 if (!can_convert (t2, t1, complain))
11858 return true;
11860 return false;
11863 /* If T is a REFERENCE_TYPE return the type to which T refers.
11864 Otherwise, return T itself. */
11866 tree
11867 non_reference (tree t)
11869 if (t && TYPE_REF_P (t))
11870 t = TREE_TYPE (t);
11871 return t;
11875 /* Return nonzero if REF is an lvalue valid for this language;
11876 otherwise, print an error message and return zero. USE says
11877 how the lvalue is being used and so selects the error message. */
11880 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
11882 cp_lvalue_kind kind = lvalue_kind (ref);
11884 if (kind == clk_none)
11886 if (complain & tf_error)
11887 lvalue_error (cp_expr_loc_or_input_loc (ref), use);
11888 return 0;
11890 else if (kind & (clk_rvalueref|clk_class))
11892 if (!(complain & tf_error))
11893 return 0;
11894 /* Make this a permerror because we used to accept it. */
11895 permerror (cp_expr_loc_or_input_loc (ref),
11896 "using rvalue as lvalue");
11898 return 1;
11901 /* Return true if a user-defined literal operator is a raw operator. */
11903 bool
11904 check_raw_literal_operator (const_tree decl)
11906 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11907 tree argtype;
11908 int arity;
11909 bool maybe_raw_p = false;
11911 /* Count the number and type of arguments and check for ellipsis. */
11912 for (argtype = argtypes, arity = 0;
11913 argtype && argtype != void_list_node;
11914 ++arity, argtype = TREE_CHAIN (argtype))
11916 tree t = TREE_VALUE (argtype);
11918 if (same_type_p (t, const_string_type_node))
11919 maybe_raw_p = true;
11921 if (!argtype)
11922 return false; /* Found ellipsis. */
11924 if (!maybe_raw_p || arity != 1)
11925 return false;
11927 return true;
11931 /* Return true if a user-defined literal operator has one of the allowed
11932 argument types. */
11934 bool
11935 check_literal_operator_args (const_tree decl,
11936 bool *long_long_unsigned_p, bool *long_double_p)
11938 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11940 *long_long_unsigned_p = false;
11941 *long_double_p = false;
11942 if (processing_template_decl || processing_specialization)
11943 return argtypes == void_list_node;
11944 else
11946 tree argtype;
11947 int arity;
11948 int max_arity = 2;
11950 /* Count the number and type of arguments and check for ellipsis. */
11951 for (argtype = argtypes, arity = 0;
11952 argtype && argtype != void_list_node;
11953 argtype = TREE_CHAIN (argtype))
11955 tree t = TREE_VALUE (argtype);
11956 ++arity;
11958 if (TYPE_PTR_P (t))
11960 bool maybe_raw_p = false;
11961 t = TREE_TYPE (t);
11962 if (cp_type_quals (t) != TYPE_QUAL_CONST)
11963 return false;
11964 t = TYPE_MAIN_VARIANT (t);
11965 if ((maybe_raw_p = same_type_p (t, char_type_node))
11966 || same_type_p (t, wchar_type_node)
11967 || same_type_p (t, char8_type_node)
11968 || same_type_p (t, char16_type_node)
11969 || same_type_p (t, char32_type_node))
11971 argtype = TREE_CHAIN (argtype);
11972 if (!argtype)
11973 return false;
11974 t = TREE_VALUE (argtype);
11975 if (maybe_raw_p && argtype == void_list_node)
11976 return true;
11977 else if (same_type_p (t, size_type_node))
11979 ++arity;
11980 continue;
11982 else
11983 return false;
11986 else if (same_type_p (t, long_long_unsigned_type_node))
11988 max_arity = 1;
11989 *long_long_unsigned_p = true;
11991 else if (same_type_p (t, long_double_type_node))
11993 max_arity = 1;
11994 *long_double_p = true;
11996 else if (same_type_p (t, char_type_node))
11997 max_arity = 1;
11998 else if (same_type_p (t, wchar_type_node))
11999 max_arity = 1;
12000 else if (same_type_p (t, char8_type_node))
12001 max_arity = 1;
12002 else if (same_type_p (t, char16_type_node))
12003 max_arity = 1;
12004 else if (same_type_p (t, char32_type_node))
12005 max_arity = 1;
12006 else
12007 return false;
12009 if (!argtype)
12010 return false; /* Found ellipsis. */
12012 if (arity != max_arity)
12013 return false;
12015 return true;
12019 /* Always returns false since unlike C90, C++ has no concept of implicit
12020 function declarations. */
12022 bool
12023 c_decl_implicit (const_tree)
12025 return false;