c++: track whether we expect a TARGET_EXPR to be elided
[official-gcc.git] / gcc / cp / typeck.cc
blobb4a8e3c205c26155c0d54064921d4309ab6d060d
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2022 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 (extended2 && !extended1)
298 int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
299 return ret == 3 ? 3 : -ret;
302 const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
303 const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
304 gcc_assert (fmt1->b == 2 && fmt2->b == 2);
305 /* For {ibm,mips}_extended_format formats, the type has variable
306 precision up to ~2150 bits when the first double is around maximum
307 representable double and second double is subnormal minimum.
308 So, e.g. for __ibm128 vs. std::float128_t, they have unordered
309 ranks. */
310 int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
311 ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
312 int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
313 ? fmt2->emax - fmt2->emin + fmt2->p - 1 : fmt2->p);
314 /* The rank of a floating point type T is greater than the rank of
315 any floating-point type whose set of values is a proper subset
316 of the set of values of T. */
317 if ((p1 > p2 && fmt1->emax >= fmt2->emax)
318 || (p1 == p2 && fmt1->emax > fmt2->emax))
319 return 2;
320 if ((p1 < p2 && fmt1->emax <= fmt2->emax)
321 || (p1 == p2 && fmt1->emax < fmt2->emax))
322 return -2;
323 if ((p1 > p2 && fmt1->emax < fmt2->emax)
324 || (p1 < p2 && fmt1->emax > fmt2->emax))
325 return 3;
326 if (!extended1 && !extended2)
328 /* The rank of long double is greater than the rank of double, which
329 is greater than the rank of float. */
330 if (t1 == long_double_type_node)
331 return 2;
332 else if (t2 == long_double_type_node)
333 return -2;
334 if (t1 == double_type_node)
335 return 2;
336 else if (t2 == double_type_node)
337 return -2;
338 if (t1 == float_type_node)
339 return 2;
340 else if (t2 == float_type_node)
341 return -2;
342 return 0;
344 /* Two extended floating-point types with the same set of values have equal
345 ranks. */
346 if (extended1 && extended2)
348 if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES))
350 /* Prefer higher extendedN value. */
351 if (extended1 > extended2)
352 return 1;
353 else if (extended1 < extended2)
354 return -1;
355 else
356 return 0;
358 else if (extended1 <= NUM_FLOATN_TYPES)
359 /* Prefer _FloatN type over _FloatMx type. */
360 return 1;
361 else if (extended2 <= NUM_FLOATN_TYPES)
362 return -1;
363 else
364 return 0;
367 /* gcc_assert (extended1 && !extended2); */
368 tree *p;
369 int cnt = 0;
370 for (p = &float_type_node; p <= &long_double_type_node; ++p)
372 const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
373 gcc_assert (fmt3->b == 2);
374 int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
375 ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
376 if (p1 == p3 && fmt1->emax == fmt3->emax)
377 ++cnt;
379 /* An extended floating-point type with the same set of values
380 as exactly one cv-unqualified standard floating-point type
381 has a rank equal to the rank of that standard floating-point
382 type.
384 An extended floating-point type with the same set of values
385 as more than one cv-unqualified standard floating-point type
386 has a rank equal to the rank of double.
388 Thus, if the latter is true and t2 is long double, t2
389 has higher rank. */
390 if (cnt > 1 && mv2 == long_double_type_node)
391 return -2;
392 /* Otherwise, they have equal rank, but extended types
393 (other than std::bfloat16_t) have higher subrank. */
394 return 1;
397 /* Return the common type for two arithmetic types T1 and T2 under the
398 usual arithmetic conversions. The default conversions have already
399 been applied, and enumerated types converted to their compatible
400 integer types. */
402 static tree
403 cp_common_type (tree t1, tree t2)
405 enum tree_code code1 = TREE_CODE (t1);
406 enum tree_code code2 = TREE_CODE (t2);
407 tree attributes;
408 int i;
411 /* In what follows, we slightly generalize the rules given in [expr] so
412 as to deal with `long long' and `complex'. First, merge the
413 attributes. */
414 attributes = (*targetm.merge_type_attributes) (t1, t2);
416 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
418 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
419 return build_type_attribute_variant (t1, attributes);
420 else
421 return NULL_TREE;
424 /* FIXME: Attributes. */
425 gcc_assert (ARITHMETIC_TYPE_P (t1)
426 || VECTOR_TYPE_P (t1)
427 || UNSCOPED_ENUM_P (t1));
428 gcc_assert (ARITHMETIC_TYPE_P (t2)
429 || VECTOR_TYPE_P (t2)
430 || UNSCOPED_ENUM_P (t2));
432 /* If one type is complex, form the common type of the non-complex
433 components, then make that complex. Use T1 or T2 if it is the
434 required type. */
435 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
437 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
438 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
439 tree subtype
440 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
442 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
443 return build_type_attribute_variant (t1, attributes);
444 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
445 return build_type_attribute_variant (t2, attributes);
446 else
447 return build_type_attribute_variant (build_complex_type (subtype),
448 attributes);
451 if (code1 == VECTOR_TYPE)
453 /* When we get here we should have two vectors of the same size.
454 Just prefer the unsigned one if present. */
455 if (TYPE_UNSIGNED (t1))
456 return merge_type_attributes_from (t1, t2);
457 else
458 return merge_type_attributes_from (t2, t1);
461 /* If only one is real, use it as the result. */
462 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
463 return build_type_attribute_variant (t1, attributes);
464 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
465 return build_type_attribute_variant (t2, attributes);
467 if (code1 == REAL_TYPE
468 && (extended_float_type_p (t1) || extended_float_type_p (t2)))
470 tree mv1 = TYPE_MAIN_VARIANT (t1);
471 tree mv2 = TYPE_MAIN_VARIANT (t2);
472 if (mv1 == mv2)
473 return build_type_attribute_variant (t1, attributes);
475 int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
476 if (cmpret == 3)
477 return error_mark_node;
478 else if (cmpret >= 0)
479 return build_type_attribute_variant (t1, attributes);
480 else
481 return build_type_attribute_variant (t2, attributes);
484 /* Both real or both integers; use the one with greater precision. */
485 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
486 return build_type_attribute_variant (t1, attributes);
487 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
488 return build_type_attribute_variant (t2, attributes);
490 /* The types are the same; no need to do anything fancy. */
491 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
492 return build_type_attribute_variant (t1, attributes);
494 if (code1 != REAL_TYPE)
496 /* If one is unsigned long long, then convert the other to unsigned
497 long long. */
498 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
499 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
500 return build_type_attribute_variant (long_long_unsigned_type_node,
501 attributes);
502 /* If one is a long long, and the other is an unsigned long, and
503 long long can represent all the values of an unsigned long, then
504 convert to a long long. Otherwise, convert to an unsigned long
505 long. Otherwise, if either operand is long long, convert the
506 other to long long.
508 Since we're here, we know the TYPE_PRECISION is the same;
509 therefore converting to long long cannot represent all the values
510 of an unsigned long, so we choose unsigned long long in that
511 case. */
512 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
513 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
515 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
516 ? long_long_unsigned_type_node
517 : long_long_integer_type_node);
518 return build_type_attribute_variant (t, attributes);
521 /* Go through the same procedure, but for longs. */
522 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
523 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
524 return build_type_attribute_variant (long_unsigned_type_node,
525 attributes);
526 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
527 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
529 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
530 ? long_unsigned_type_node : long_integer_type_node);
531 return build_type_attribute_variant (t, attributes);
534 /* For __intN types, either the type is __int128 (and is lower
535 priority than the types checked above, but higher than other
536 128-bit types) or it's known to not be the same size as other
537 types (enforced in toplev.cc). Prefer the unsigned type. */
538 for (i = 0; i < NUM_INT_N_ENTS; i ++)
540 if (int_n_enabled_p [i]
541 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
542 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
543 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
544 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
546 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
547 ? int_n_trees[i].unsigned_type
548 : int_n_trees[i].signed_type);
549 return build_type_attribute_variant (t, attributes);
553 /* Otherwise prefer the unsigned one. */
554 if (TYPE_UNSIGNED (t1))
555 return build_type_attribute_variant (t1, attributes);
556 else
557 return build_type_attribute_variant (t2, attributes);
559 else
561 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
562 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
563 return build_type_attribute_variant (long_double_type_node,
564 attributes);
565 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
566 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
567 return build_type_attribute_variant (double_type_node,
568 attributes);
569 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
570 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
571 return build_type_attribute_variant (float_type_node,
572 attributes);
574 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
575 the standard C++ floating-point types. Logic earlier in this
576 function has already eliminated the possibility that
577 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
578 compelling reason to choose one or the other. */
579 return build_type_attribute_variant (t1, attributes);
583 /* T1 and T2 are arithmetic or enumeration types. Return the type
584 that will result from the "usual arithmetic conversions" on T1 and
585 T2 as described in [expr]. */
587 tree
588 type_after_usual_arithmetic_conversions (tree t1, tree t2)
590 gcc_assert (ARITHMETIC_TYPE_P (t1)
591 || VECTOR_TYPE_P (t1)
592 || UNSCOPED_ENUM_P (t1));
593 gcc_assert (ARITHMETIC_TYPE_P (t2)
594 || VECTOR_TYPE_P (t2)
595 || UNSCOPED_ENUM_P (t2));
597 /* Perform the integral promotions. We do not promote real types here. */
598 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
599 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
601 t1 = type_promotes_to (t1);
602 t2 = type_promotes_to (t2);
605 return cp_common_type (t1, t2);
608 static void
609 composite_pointer_error (const op_location_t &location,
610 diagnostic_t kind, tree t1, tree t2,
611 composite_pointer_operation operation)
613 switch (operation)
615 case CPO_COMPARISON:
616 emit_diagnostic (kind, location, 0,
617 "comparison between "
618 "distinct pointer types %qT and %qT lacks a cast",
619 t1, t2);
620 break;
621 case CPO_CONVERSION:
622 emit_diagnostic (kind, location, 0,
623 "conversion between "
624 "distinct pointer types %qT and %qT lacks a cast",
625 t1, t2);
626 break;
627 case CPO_CONDITIONAL_EXPR:
628 emit_diagnostic (kind, location, 0,
629 "conditional expression between "
630 "distinct pointer types %qT and %qT lacks a cast",
631 t1, t2);
632 break;
633 default:
634 gcc_unreachable ();
638 /* Subroutine of composite_pointer_type to implement the recursive
639 case. See that function for documentation of the parameters. And ADD_CONST
640 is used to track adding "const" where needed. */
642 static tree
643 composite_pointer_type_r (const op_location_t &location,
644 tree t1, tree t2, bool *add_const,
645 composite_pointer_operation operation,
646 tsubst_flags_t complain)
648 tree pointee1;
649 tree pointee2;
650 tree result_type;
651 tree attributes;
653 /* Determine the types pointed to by T1 and T2. */
654 if (TYPE_PTR_P (t1))
656 pointee1 = TREE_TYPE (t1);
657 pointee2 = TREE_TYPE (t2);
659 else
661 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
662 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
665 /* [expr.type]
667 If T1 and T2 are similar types, the result is the cv-combined type of
668 T1 and T2. */
669 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
670 result_type = pointee1;
671 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
672 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
674 result_type = composite_pointer_type_r (location, pointee1, pointee2,
675 add_const, operation, complain);
676 if (result_type == error_mark_node)
677 return error_mark_node;
679 else
681 if (complain & tf_error)
682 composite_pointer_error (location, DK_PERMERROR,
683 t1, t2, operation);
684 else
685 return error_mark_node;
686 result_type = void_type_node;
688 const int q1 = cp_type_quals (pointee1);
689 const int q2 = cp_type_quals (pointee2);
690 const int quals = q1 | q2;
691 result_type = cp_build_qualified_type (result_type,
692 (quals | (*add_const
693 ? TYPE_QUAL_CONST
694 : TYPE_UNQUALIFIED)));
695 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
696 the TLQ). The reason is that both T1 and T2 can then be converted to the
697 cv-combined type of T1 and T2. */
698 if (quals != q1 || quals != q2)
699 *add_const = true;
700 /* If the original types were pointers to members, so is the
701 result. */
702 if (TYPE_PTRMEM_P (t1))
704 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
705 TYPE_PTRMEM_CLASS_TYPE (t2)))
707 if (complain & tf_error)
708 composite_pointer_error (location, DK_PERMERROR,
709 t1, t2, operation);
710 else
711 return error_mark_node;
713 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
714 result_type);
716 else
717 result_type = build_pointer_type (result_type);
719 /* Merge the attributes. */
720 attributes = (*targetm.merge_type_attributes) (t1, t2);
721 return build_type_attribute_variant (result_type, attributes);
724 /* Return the composite pointer type (see [expr.type]) for T1 and T2.
725 ARG1 and ARG2 are the values with those types. The OPERATION is to
726 describe the operation between the pointer types,
727 in case an error occurs.
729 This routine also implements the computation of a common type for
730 pointers-to-members as per [expr.eq]. */
732 tree
733 composite_pointer_type (const op_location_t &location,
734 tree t1, tree t2, tree arg1, tree arg2,
735 composite_pointer_operation operation,
736 tsubst_flags_t complain)
738 tree class1;
739 tree class2;
741 /* [expr.type]
743 If one operand is a null pointer constant, the composite pointer
744 type is the type of the other operand. */
745 if (null_ptr_cst_p (arg1))
746 return t2;
747 if (null_ptr_cst_p (arg2))
748 return t1;
750 /* We have:
752 [expr.type]
754 If one of the operands has type "pointer to cv1 void", then
755 the other has type "pointer to cv2 T", and the composite pointer
756 type is "pointer to cv12 void", where cv12 is the union of cv1
757 and cv2.
759 If either type is a pointer to void, make sure it is T1. */
760 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
761 std::swap (t1, t2);
763 /* Now, if T1 is a pointer to void, merge the qualifiers. */
764 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
766 tree attributes;
767 tree result_type;
769 if (TYPE_PTRFN_P (t2))
771 if (complain & tf_error)
773 switch (operation)
775 case CPO_COMPARISON:
776 pedwarn (location, OPT_Wpedantic,
777 "ISO C++ forbids comparison between pointer "
778 "of type %<void *%> and pointer-to-function");
779 break;
780 case CPO_CONVERSION:
781 pedwarn (location, OPT_Wpedantic,
782 "ISO C++ forbids conversion between pointer "
783 "of type %<void *%> and pointer-to-function");
784 break;
785 case CPO_CONDITIONAL_EXPR:
786 pedwarn (location, OPT_Wpedantic,
787 "ISO C++ forbids conditional expression between "
788 "pointer of type %<void *%> and "
789 "pointer-to-function");
790 break;
791 default:
792 gcc_unreachable ();
795 else
796 return error_mark_node;
798 result_type
799 = cp_build_qualified_type (void_type_node,
800 (cp_type_quals (TREE_TYPE (t1))
801 | cp_type_quals (TREE_TYPE (t2))));
802 result_type = build_pointer_type (result_type);
803 /* Merge the attributes. */
804 attributes = (*targetm.merge_type_attributes) (t1, t2);
805 return build_type_attribute_variant (result_type, attributes);
808 if (c_dialect_objc () && TYPE_PTR_P (t1)
809 && TYPE_PTR_P (t2))
811 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
812 return objc_common_type (t1, t2);
815 /* if T1 or T2 is "pointer to noexcept function" and the other type is
816 "pointer to function", where the function types are otherwise the same,
817 "pointer to function" */
818 if (fnptr_conv_p (t1, t2))
819 return t1;
820 if (fnptr_conv_p (t2, t1))
821 return t2;
823 /* [expr.eq] permits the application of a pointer conversion to
824 bring the pointers to a common type. */
825 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
826 && CLASS_TYPE_P (TREE_TYPE (t1))
827 && CLASS_TYPE_P (TREE_TYPE (t2))
828 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
829 TREE_TYPE (t2)))
831 class1 = TREE_TYPE (t1);
832 class2 = TREE_TYPE (t2);
834 if (DERIVED_FROM_P (class1, class2))
835 t2 = (build_pointer_type
836 (cp_build_qualified_type (class1, cp_type_quals (class2))));
837 else if (DERIVED_FROM_P (class2, class1))
838 t1 = (build_pointer_type
839 (cp_build_qualified_type (class2, cp_type_quals (class1))));
840 else
842 if (complain & tf_error)
843 composite_pointer_error (location, DK_ERROR, t1, t2, operation);
844 return error_mark_node;
847 /* [expr.eq] permits the application of a pointer-to-member
848 conversion to change the class type of one of the types. */
849 else if (TYPE_PTRMEM_P (t1)
850 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
851 TYPE_PTRMEM_CLASS_TYPE (t2)))
853 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
854 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
856 if (DERIVED_FROM_P (class1, class2))
857 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
858 else if (DERIVED_FROM_P (class2, class1))
859 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
860 else
862 if (complain & tf_error)
863 switch (operation)
865 case CPO_COMPARISON:
866 error_at (location, "comparison between distinct "
867 "pointer-to-member types %qT and %qT lacks a cast",
868 t1, t2);
869 break;
870 case CPO_CONVERSION:
871 error_at (location, "conversion between distinct "
872 "pointer-to-member types %qT and %qT lacks a cast",
873 t1, t2);
874 break;
875 case CPO_CONDITIONAL_EXPR:
876 error_at (location, "conditional expression between distinct "
877 "pointer-to-member types %qT and %qT lacks a cast",
878 t1, t2);
879 break;
880 default:
881 gcc_unreachable ();
883 return error_mark_node;
887 bool add_const = false;
888 return composite_pointer_type_r (location, t1, t2, &add_const, operation,
889 complain);
892 /* Return the merged type of two types.
893 We assume that comptypes has already been done and returned 1;
894 if that isn't so, this may crash.
896 This just combines attributes and default arguments; any other
897 differences would cause the two types to compare unalike. */
899 tree
900 merge_types (tree t1, tree t2)
902 enum tree_code code1;
903 enum tree_code code2;
904 tree attributes;
906 /* Save time if the two types are the same. */
907 if (t1 == t2)
908 return t1;
909 if (original_type (t1) == original_type (t2))
910 return t1;
912 /* If one type is nonsense, use the other. */
913 if (t1 == error_mark_node)
914 return t2;
915 if (t2 == error_mark_node)
916 return t1;
918 /* Handle merging an auto redeclaration with a previous deduced
919 return type. */
920 if (is_auto (t1))
921 return t2;
923 /* Merge the attributes. */
924 attributes = (*targetm.merge_type_attributes) (t1, t2);
926 if (TYPE_PTRMEMFUNC_P (t1))
927 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
928 if (TYPE_PTRMEMFUNC_P (t2))
929 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
931 code1 = TREE_CODE (t1);
932 code2 = TREE_CODE (t2);
933 if (code1 != code2)
935 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
936 if (code1 == TYPENAME_TYPE)
938 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
939 code1 = TREE_CODE (t1);
941 else
943 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
944 code2 = TREE_CODE (t2);
948 switch (code1)
950 case POINTER_TYPE:
951 case REFERENCE_TYPE:
952 /* For two pointers, do this recursively on the target type. */
954 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
955 int quals = cp_type_quals (t1);
957 if (code1 == POINTER_TYPE)
959 t1 = build_pointer_type (target);
960 if (TREE_CODE (target) == METHOD_TYPE)
961 t1 = build_ptrmemfunc_type (t1);
963 else
964 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
965 t1 = build_type_attribute_variant (t1, attributes);
966 t1 = cp_build_qualified_type (t1, quals);
968 return t1;
971 case OFFSET_TYPE:
973 int quals;
974 tree pointee;
975 quals = cp_type_quals (t1);
976 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
977 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
978 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
979 pointee);
980 t1 = cp_build_qualified_type (t1, quals);
981 break;
984 case ARRAY_TYPE:
986 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
987 /* Save space: see if the result is identical to one of the args. */
988 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
989 return build_type_attribute_variant (t1, attributes);
990 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
991 return build_type_attribute_variant (t2, attributes);
992 /* Merge the element types, and have a size if either arg has one. */
993 t1 = build_cplus_array_type
994 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
995 break;
998 case FUNCTION_TYPE:
999 /* Function types: prefer the one that specified arg types.
1000 If both do, merge the arg types. Also merge the return types. */
1002 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1003 tree p1 = TYPE_ARG_TYPES (t1);
1004 tree p2 = TYPE_ARG_TYPES (t2);
1005 tree parms;
1007 /* Save space: see if the result is identical to one of the args. */
1008 if (valtype == TREE_TYPE (t1) && ! p2)
1009 return cp_build_type_attribute_variant (t1, attributes);
1010 if (valtype == TREE_TYPE (t2) && ! p1)
1011 return cp_build_type_attribute_variant (t2, attributes);
1013 /* Simple way if one arg fails to specify argument types. */
1014 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1015 parms = p2;
1016 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1017 parms = p1;
1018 else
1019 parms = commonparms (p1, p2);
1021 cp_cv_quals quals = type_memfn_quals (t1);
1022 cp_ref_qualifier rqual = type_memfn_rqual (t1);
1023 gcc_assert (quals == type_memfn_quals (t2));
1024 gcc_assert (rqual == type_memfn_rqual (t2));
1026 tree rval = build_function_type (valtype, parms);
1027 rval = apply_memfn_quals (rval, quals);
1028 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1029 TYPE_RAISES_EXCEPTIONS (t2));
1030 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1031 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1032 break;
1035 case METHOD_TYPE:
1037 /* Get this value the long way, since TYPE_METHOD_BASETYPE
1038 is just the main variant of this. */
1039 tree basetype = class_of_this_parm (t2);
1040 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1041 TYPE_RAISES_EXCEPTIONS (t2));
1042 cp_ref_qualifier rqual = type_memfn_rqual (t1);
1043 tree t3;
1044 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1046 /* If this was a member function type, get back to the
1047 original type of type member function (i.e., without
1048 the class instance variable up front. */
1049 t1 = build_function_type (TREE_TYPE (t1),
1050 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1051 t2 = build_function_type (TREE_TYPE (t2),
1052 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1053 t3 = merge_types (t1, t2);
1054 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1055 TYPE_ARG_TYPES (t3));
1056 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1057 break;
1060 case TYPENAME_TYPE:
1061 /* There is no need to merge attributes into a TYPENAME_TYPE.
1062 When the type is instantiated it will have whatever
1063 attributes result from the instantiation. */
1064 return t1;
1066 default:;
1067 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1068 return t1;
1069 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1070 return t2;
1071 break;
1074 return cp_build_type_attribute_variant (t1, attributes);
1077 /* Return the ARRAY_TYPE type without its domain. */
1079 tree
1080 strip_array_domain (tree type)
1082 tree t2;
1083 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1084 if (TYPE_DOMAIN (type) == NULL_TREE)
1085 return type;
1086 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1087 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1090 /* Wrapper around cp_common_type that is used by c-common.cc and other
1091 front end optimizations that remove promotions.
1093 Return the common type for two arithmetic types T1 and T2 under the
1094 usual arithmetic conversions. The default conversions have already
1095 been applied, and enumerated types converted to their compatible
1096 integer types. */
1098 tree
1099 common_type (tree t1, tree t2)
1101 /* If one type is nonsense, use the other */
1102 if (t1 == error_mark_node)
1103 return t2;
1104 if (t2 == error_mark_node)
1105 return t1;
1107 return cp_common_type (t1, t2);
1110 /* Return the common type of two pointer types T1 and T2. This is the
1111 type for the result of most arithmetic operations if the operands
1112 have the given two types.
1114 We assume that comp_target_types has already been done and returned
1115 nonzero; if that isn't so, this may crash. */
1117 tree
1118 common_pointer_type (tree t1, tree t2)
1120 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1121 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1122 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1124 return composite_pointer_type (input_location, t1, t2,
1125 error_mark_node, error_mark_node,
1126 CPO_CONVERSION, tf_warning_or_error);
1129 /* Compare two exception specifier types for exactness or subsetness, if
1130 allowed. Returns false for mismatch, true for match (same, or
1131 derived and !exact).
1133 [except.spec] "If a class X ... objects of class X or any class publicly
1134 and unambiguously derived from X. Similarly, if a pointer type Y * ...
1135 exceptions of type Y * or that are pointers to any type publicly and
1136 unambiguously derived from Y. Otherwise a function only allows exceptions
1137 that have the same type ..."
1138 This does not mention cv qualifiers and is different to what throw
1139 [except.throw] and catch [except.catch] will do. They will ignore the
1140 top level cv qualifiers, and allow qualifiers in the pointer to class
1141 example.
1143 We implement the letter of the standard. */
1145 static bool
1146 comp_except_types (tree a, tree b, bool exact)
1148 if (same_type_p (a, b))
1149 return true;
1150 else if (!exact)
1152 if (cp_type_quals (a) || cp_type_quals (b))
1153 return false;
1155 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1157 a = TREE_TYPE (a);
1158 b = TREE_TYPE (b);
1159 if (cp_type_quals (a) || cp_type_quals (b))
1160 return false;
1163 if (TREE_CODE (a) != RECORD_TYPE
1164 || TREE_CODE (b) != RECORD_TYPE)
1165 return false;
1167 if (publicly_uniquely_derived_p (a, b))
1168 return true;
1170 return false;
1173 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1174 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1175 If EXACT is ce_type, the C++17 type compatibility rules apply.
1176 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1177 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1178 are unordered, but we've already filtered out duplicates. Most lists will
1179 be in order, we should try to make use of that. */
1181 bool
1182 comp_except_specs (const_tree t1, const_tree t2, int exact)
1184 const_tree probe;
1185 const_tree base;
1186 int length = 0;
1188 if (t1 == t2)
1189 return true;
1191 /* First handle noexcept. */
1192 if (exact < ce_exact)
1194 if (exact == ce_type
1195 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1196 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1197 return true;
1199 /* noexcept(false) is compatible with no exception-specification,
1200 and less strict than any spec. */
1201 if (t1 == noexcept_false_spec)
1202 return t2 == NULL_TREE || exact == ce_derived;
1203 /* Even a derived noexcept(false) is compatible with no
1204 exception-specification. */
1205 if (t2 == noexcept_false_spec)
1206 return t1 == NULL_TREE;
1208 /* Otherwise, if we aren't looking for an exact match, noexcept is
1209 equivalent to throw(). */
1210 if (t1 == noexcept_true_spec)
1211 t1 = empty_except_spec;
1212 if (t2 == noexcept_true_spec)
1213 t2 = empty_except_spec;
1216 /* If any noexcept is left, it is only comparable to itself;
1217 either we're looking for an exact match or we're redeclaring a
1218 template with dependent noexcept. */
1219 if ((t1 && TREE_PURPOSE (t1))
1220 || (t2 && TREE_PURPOSE (t2)))
1221 return (t1 && t2
1222 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1224 if (t1 == NULL_TREE) /* T1 is ... */
1225 return t2 == NULL_TREE || exact == ce_derived;
1226 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1227 return t2 != NULL_TREE && !TREE_VALUE (t2);
1228 if (t2 == NULL_TREE) /* T2 is ... */
1229 return false;
1230 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1231 return exact == ce_derived;
1233 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1234 Count how many we find, to determine exactness. For exact matching and
1235 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1236 O(nm). */
1237 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1239 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1241 tree a = TREE_VALUE (probe);
1242 tree b = TREE_VALUE (t2);
1244 if (comp_except_types (a, b, exact))
1246 if (probe == base && exact > ce_derived)
1247 base = TREE_CHAIN (probe);
1248 length++;
1249 break;
1252 if (probe == NULL_TREE)
1253 return false;
1255 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1258 /* Compare the array types T1 and T2. CB says how we should behave when
1259 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1260 bounds_either says than any array can be [], bounds_first means that
1261 onlt T1 can be an array with unknown bounds. STRICT is true if
1262 qualifiers must match when comparing the types of the array elements. */
1264 static bool
1265 comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1266 bool strict)
1268 tree d1;
1269 tree d2;
1270 tree max1, max2;
1272 if (t1 == t2)
1273 return true;
1275 /* The type of the array elements must be the same. */
1276 if (strict
1277 ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1278 : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1279 return false;
1281 d1 = TYPE_DOMAIN (t1);
1282 d2 = TYPE_DOMAIN (t2);
1284 if (d1 == d2)
1285 return true;
1287 /* If one of the arrays is dimensionless, and the other has a
1288 dimension, they are of different types. However, it is valid to
1289 write:
1291 extern int a[];
1292 int a[3];
1294 by [basic.link]:
1296 declarations for an array object can specify
1297 array types that differ by the presence or absence of a major
1298 array bound (_dcl.array_). */
1299 if (!d1 && d2)
1300 return cb >= bounds_either;
1301 else if (d1 && !d2)
1302 return cb == bounds_either;
1304 /* Check that the dimensions are the same. */
1306 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1307 return false;
1308 max1 = TYPE_MAX_VALUE (d1);
1309 max2 = TYPE_MAX_VALUE (d2);
1311 if (!cp_tree_equal (max1, max2))
1312 return false;
1314 return true;
1317 /* Compare the relative position of T1 and T2 into their respective
1318 template parameter list.
1319 T1 and T2 must be template parameter types.
1320 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1322 static bool
1323 comp_template_parms_position (tree t1, tree t2)
1325 tree index1, index2;
1326 gcc_assert (t1 && t2
1327 && TREE_CODE (t1) == TREE_CODE (t2)
1328 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1329 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1330 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1332 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1333 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1335 /* Then compare their relative position. */
1336 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1337 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1338 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1339 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1340 return false;
1342 /* In C++14 we can end up comparing 'auto' to a normal template
1343 parameter. Don't confuse them. */
1344 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1345 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1347 return true;
1350 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1352 static bool
1353 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1355 t1 = TYPE_MAIN_VARIANT (t1);
1356 t2 = TYPE_MAIN_VARIANT (t2);
1358 if (TYPE_PTR_P (t1)
1359 && TYPE_PTR_P (t2))
1360 return true;
1362 /* The signedness of the parameter matters only when an integral
1363 type smaller than int is promoted to int, otherwise only the
1364 precision of the parameter matters.
1365 This check should make sure that the callee does not see
1366 undefined values in argument registers. */
1367 if (INTEGRAL_TYPE_P (t1)
1368 && INTEGRAL_TYPE_P (t2)
1369 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1370 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1371 || !targetm.calls.promote_prototypes (NULL_TREE)
1372 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1373 return true;
1375 return same_type_p (t1, t2);
1378 /* Check if a type cast between two function types can be considered safe. */
1380 static bool
1381 cxx_safe_function_type_cast_p (tree t1, tree t2)
1383 if (TREE_TYPE (t1) == void_type_node &&
1384 TYPE_ARG_TYPES (t1) == void_list_node)
1385 return true;
1387 if (TREE_TYPE (t2) == void_type_node &&
1388 TYPE_ARG_TYPES (t2) == void_list_node)
1389 return true;
1391 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1392 return false;
1394 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1395 t1 && t2;
1396 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1397 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1398 return false;
1400 return true;
1403 /* Subroutine in comptypes. */
1405 static bool
1406 structural_comptypes (tree t1, tree t2, int strict)
1408 /* Both should be types that are not obviously the same. */
1409 gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1411 /* Suppress typename resolution under spec_hasher::equal in place of calling
1412 push_to_top_level there. */
1413 if (!comparing_specializations)
1415 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1416 current instantiation. */
1417 if (TREE_CODE (t1) == TYPENAME_TYPE)
1418 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1420 if (TREE_CODE (t2) == TYPENAME_TYPE)
1421 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1424 if (TYPE_PTRMEMFUNC_P (t1))
1425 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1426 if (TYPE_PTRMEMFUNC_P (t2))
1427 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1429 /* Different classes of types can't be compatible. */
1430 if (TREE_CODE (t1) != TREE_CODE (t2))
1431 return false;
1433 /* Qualifiers must match. For array types, we will check when we
1434 recur on the array element types. */
1435 if (TREE_CODE (t1) != ARRAY_TYPE
1436 && cp_type_quals (t1) != cp_type_quals (t2))
1437 return false;
1438 if (TREE_CODE (t1) == FUNCTION_TYPE
1439 && type_memfn_quals (t1) != type_memfn_quals (t2))
1440 return false;
1441 /* Need to check this before TYPE_MAIN_VARIANT.
1442 FIXME function qualifiers should really change the main variant. */
1443 if (FUNC_OR_METHOD_TYPE_P (t1))
1445 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1446 return false;
1447 if (flag_noexcept_type
1448 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1449 TYPE_RAISES_EXCEPTIONS (t2),
1450 ce_type))
1451 return false;
1454 /* Allow for two different type nodes which have essentially the same
1455 definition. Note that we already checked for equality of the type
1456 qualifiers (just above). */
1457 if (TREE_CODE (t1) != ARRAY_TYPE
1458 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1459 goto check_alias;
1461 /* Compare the types. Return false on known not-same. Break on not
1462 known. Never return true from this switch -- you'll break
1463 specialization comparison. */
1464 switch (TREE_CODE (t1))
1466 case VOID_TYPE:
1467 case BOOLEAN_TYPE:
1468 /* All void and bool types are the same. */
1469 break;
1471 case OPAQUE_TYPE:
1472 case INTEGER_TYPE:
1473 case FIXED_POINT_TYPE:
1474 case REAL_TYPE:
1475 /* With these nodes, we can't determine type equivalence by
1476 looking at what is stored in the nodes themselves, because
1477 two nodes might have different TYPE_MAIN_VARIANTs but still
1478 represent the same type. For example, wchar_t and int could
1479 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1480 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1481 and are distinct types. On the other hand, int and the
1482 following typedef
1484 typedef int INT __attribute((may_alias));
1486 have identical properties, different TYPE_MAIN_VARIANTs, but
1487 represent the same type. The canonical type system keeps
1488 track of equivalence in this case, so we fall back on it. */
1489 if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1490 return false;
1492 /* We don't need or want the attribute comparison. */
1493 goto check_alias;
1495 case TEMPLATE_TEMPLATE_PARM:
1496 case BOUND_TEMPLATE_TEMPLATE_PARM:
1497 if (!comp_template_parms_position (t1, t2))
1498 return false;
1499 if (!comp_template_parms
1500 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1501 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1502 return false;
1503 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1504 break;
1505 /* Don't check inheritance. */
1506 strict = COMPARE_STRICT;
1507 /* Fall through. */
1509 case RECORD_TYPE:
1510 case UNION_TYPE:
1511 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1512 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1513 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1514 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1515 break;
1517 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1518 break;
1519 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1520 break;
1522 return false;
1524 case OFFSET_TYPE:
1525 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1526 strict & ~COMPARE_REDECLARATION))
1527 return false;
1528 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1529 return false;
1530 break;
1532 case REFERENCE_TYPE:
1533 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1534 return false;
1535 /* fall through to checks for pointer types */
1536 gcc_fallthrough ();
1538 case POINTER_TYPE:
1539 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1540 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1541 return false;
1542 break;
1544 case METHOD_TYPE:
1545 case FUNCTION_TYPE:
1546 /* Exception specs and memfn_rquals were checked above. */
1547 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1548 return false;
1549 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1550 return false;
1551 break;
1553 case ARRAY_TYPE:
1554 /* Target types must match incl. qualifiers. */
1555 if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1556 ? bounds_either : bounds_none),
1557 /*strict=*/true))
1558 return false;
1559 break;
1561 case TEMPLATE_TYPE_PARM:
1562 /* If T1 and T2 don't have the same relative position in their
1563 template parameters set, they can't be equal. */
1564 if (!comp_template_parms_position (t1, t2))
1565 return false;
1566 /* If T1 and T2 don't represent the same class template deduction,
1567 they aren't equal. */
1568 if (CLASS_PLACEHOLDER_TEMPLATE (t1)
1569 != CLASS_PLACEHOLDER_TEMPLATE (t2))
1570 return false;
1571 /* Constrained 'auto's are distinct from parms that don't have the same
1572 constraints. */
1573 if (!equivalent_placeholder_constraints (t1, t2))
1574 return false;
1575 break;
1577 case TYPENAME_TYPE:
1578 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1579 TYPENAME_TYPE_FULLNAME (t2)))
1580 return false;
1581 /* Qualifiers don't matter on scopes. */
1582 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1583 TYPE_CONTEXT (t2)))
1584 return false;
1585 break;
1587 case UNBOUND_CLASS_TEMPLATE:
1588 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1589 return false;
1590 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1591 return false;
1592 break;
1594 case COMPLEX_TYPE:
1595 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1596 return false;
1597 break;
1599 case VECTOR_TYPE:
1600 if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1601 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1602 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1603 return false;
1604 break;
1606 case TYPE_PACK_EXPANSION:
1607 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1608 PACK_EXPANSION_PATTERN (t2))
1609 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1610 PACK_EXPANSION_EXTRA_ARGS (t2)));
1612 case DECLTYPE_TYPE:
1613 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1614 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1615 return false;
1616 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1617 return false;
1618 if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1619 return false;
1620 if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1621 return false;
1622 break;
1624 case TRAIT_TYPE:
1625 if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1626 return false;
1627 if (!same_type_p (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1628 || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1629 return false;
1630 break;
1632 case TYPEOF_TYPE:
1633 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1634 return false;
1635 break;
1637 default:
1638 return false;
1641 /* If we get here, we know that from a target independent POV the
1642 types are the same. Make sure the target attributes are also
1643 the same. */
1644 if (!comp_type_attributes (t1, t2))
1645 return false;
1647 check_alias:
1648 if (comparing_dependent_aliases)
1650 /* Don't treat an alias template specialization with dependent
1651 arguments as equivalent to its underlying type when used as a
1652 template argument; we need them to be distinct so that we
1653 substitute into the specialization arguments at instantiation
1654 time. And aliases can't be equivalent without being ==, so
1655 we don't need to look any deeper. */
1656 ++processing_template_decl;
1657 tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1658 tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1659 --processing_template_decl;
1660 if ((dep1 || dep2) && dep1 != dep2)
1661 return false;
1664 return true;
1667 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1668 is a bitwise-or of the COMPARE_* flags. */
1670 bool
1671 comptypes (tree t1, tree t2, int strict)
1673 gcc_checking_assert (t1 && t2);
1675 /* TYPE_ARGUMENT_PACKS are not really types. */
1676 gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1677 && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1679 if (t1 == t2)
1680 return true;
1682 /* Suppress errors caused by previously reported errors. */
1683 if (t1 == error_mark_node || t2 == error_mark_node)
1684 return false;
1686 if (strict == COMPARE_STRICT)
1688 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1689 /* At least one of the types requires structural equality, so
1690 perform a deep check. */
1691 return structural_comptypes (t1, t2, strict);
1693 if (flag_checking && param_use_canonical_types)
1695 bool result = structural_comptypes (t1, t2, strict);
1697 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1698 /* The two types are structurally equivalent, but their
1699 canonical types were different. This is a failure of the
1700 canonical type propagation code.*/
1701 internal_error
1702 ("canonical types differ for identical types %qT and %qT",
1703 t1, t2);
1704 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1705 /* Two types are structurally different, but the canonical
1706 types are the same. This means we were over-eager in
1707 assigning canonical types. */
1708 internal_error
1709 ("same canonical type node for different types %qT and %qT",
1710 t1, t2);
1712 return result;
1714 if (!flag_checking && param_use_canonical_types)
1715 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1716 else
1717 return structural_comptypes (t1, t2, strict);
1719 else if (strict == COMPARE_STRUCTURAL)
1720 return structural_comptypes (t1, t2, COMPARE_STRICT);
1721 else
1722 return structural_comptypes (t1, t2, strict);
1725 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1726 top-level qualifiers. */
1728 bool
1729 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1731 if (type1 == error_mark_node || type2 == error_mark_node)
1732 return false;
1733 if (type1 == type2)
1734 return true;
1736 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1737 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1738 return same_type_p (type1, type2);
1741 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1743 bool
1744 similar_type_p (tree type1, tree type2)
1746 if (type1 == error_mark_node || type2 == error_mark_node)
1747 return false;
1749 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1750 * they are the same type; or
1751 * they are both pointers, and the pointed-to types are similar; or
1752 * they are both pointers to member of the same class, and the types of
1753 the pointed-to members are similar; or
1754 * they are both arrays of the same size or both arrays of unknown bound,
1755 and the array element types are similar. */
1757 if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1758 return true;
1760 if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1761 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1762 || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1763 return comp_ptr_ttypes_const (type1, type2, bounds_either);
1765 return false;
1768 /* Helper function for layout_compatible_type_p and
1769 is_corresponding_member_aggr. Advance to next members (NULL if
1770 no further ones) and return true if those members are still part of
1771 the common initial sequence. */
1773 bool
1774 next_common_initial_seqence (tree &memb1, tree &memb2)
1776 while (memb1)
1778 if (TREE_CODE (memb1) != FIELD_DECL
1779 || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1781 memb1 = DECL_CHAIN (memb1);
1782 continue;
1784 if (DECL_FIELD_IS_BASE (memb1))
1786 memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1787 continue;
1789 break;
1791 while (memb2)
1793 if (TREE_CODE (memb2) != FIELD_DECL
1794 || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1796 memb2 = DECL_CHAIN (memb2);
1797 continue;
1799 if (DECL_FIELD_IS_BASE (memb2))
1801 memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1802 continue;
1804 break;
1806 if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1807 return true;
1808 if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1809 return false;
1810 if (DECL_BIT_FIELD_TYPE (memb1))
1812 if (!DECL_BIT_FIELD_TYPE (memb2))
1813 return false;
1814 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1815 DECL_BIT_FIELD_TYPE (memb2)))
1816 return false;
1817 if (TYPE_PRECISION (TREE_TYPE (memb1))
1818 != TYPE_PRECISION (TREE_TYPE (memb2)))
1819 return false;
1821 else if (DECL_BIT_FIELD_TYPE (memb2))
1822 return false;
1823 else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1824 return false;
1825 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1826 != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1827 return false;
1828 if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1829 return false;
1830 return true;
1833 /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1835 bool
1836 layout_compatible_type_p (tree type1, tree type2)
1838 if (type1 == error_mark_node || type2 == error_mark_node)
1839 return false;
1840 if (type1 == type2)
1841 return true;
1842 if (TREE_CODE (type1) != TREE_CODE (type2))
1843 return false;
1845 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1846 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1848 if (TREE_CODE (type1) == ENUMERAL_TYPE)
1849 return (TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
1850 && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1851 && same_type_p (finish_underlying_type (type1),
1852 finish_underlying_type (type2)));
1854 if (CLASS_TYPE_P (type1)
1855 && std_layout_type_p (type1)
1856 && std_layout_type_p (type2)
1857 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
1858 && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1860 tree field1 = TYPE_FIELDS (type1);
1861 tree field2 = TYPE_FIELDS (type2);
1862 if (TREE_CODE (type1) == RECORD_TYPE)
1864 while (1)
1866 if (!next_common_initial_seqence (field1, field2))
1867 return false;
1868 if (field1 == NULL_TREE)
1869 return true;
1870 field1 = DECL_CHAIN (field1);
1871 field2 = DECL_CHAIN (field2);
1874 /* Otherwise both types must be union types.
1875 The standard says:
1876 "Two standard-layout unions are layout-compatible if they have
1877 the same number of non-static data members and corresponding
1878 non-static data members (in any order) have layout-compatible
1879 types."
1880 but the code anticipates that bitfield vs. non-bitfield,
1881 different bitfield widths or presence/absence of
1882 [[no_unique_address]] should be checked as well. */
1883 auto_vec<tree, 16> vec;
1884 unsigned int count = 0;
1885 for (; field1; field1 = DECL_CHAIN (field1))
1886 if (TREE_CODE (field1) == FIELD_DECL)
1887 count++;
1888 for (; field2; field2 = DECL_CHAIN (field2))
1889 if (TREE_CODE (field2) == FIELD_DECL)
1890 vec.safe_push (field2);
1891 /* Discussions on core lean towards treating multiple union fields
1892 of the same type as the same field, so this might need changing
1893 in the future. */
1894 if (count != vec.length ())
1895 return false;
1896 for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1898 if (TREE_CODE (field1) != FIELD_DECL)
1899 continue;
1900 unsigned int j;
1901 tree t1 = DECL_BIT_FIELD_TYPE (field1);
1902 if (t1 == NULL_TREE)
1903 t1 = TREE_TYPE (field1);
1904 FOR_EACH_VEC_ELT (vec, j, field2)
1906 tree t2 = DECL_BIT_FIELD_TYPE (field2);
1907 if (t2 == NULL_TREE)
1908 t2 = TREE_TYPE (field2);
1909 if (DECL_BIT_FIELD_TYPE (field1))
1911 if (!DECL_BIT_FIELD_TYPE (field2))
1912 continue;
1913 if (TYPE_PRECISION (TREE_TYPE (field1))
1914 != TYPE_PRECISION (TREE_TYPE (field2)))
1915 continue;
1917 else if (DECL_BIT_FIELD_TYPE (field2))
1918 continue;
1919 if (!layout_compatible_type_p (t1, t2))
1920 continue;
1921 if ((!lookup_attribute ("no_unique_address",
1922 DECL_ATTRIBUTES (field1)))
1923 != !lookup_attribute ("no_unique_address",
1924 DECL_ATTRIBUTES (field2)))
1925 continue;
1926 break;
1928 if (j == vec.length ())
1929 return false;
1930 vec.unordered_remove (j);
1932 return true;
1935 return same_type_p (type1, type2);
1938 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1940 bool
1941 at_least_as_qualified_p (const_tree type1, const_tree type2)
1943 int q1 = cp_type_quals (type1);
1944 int q2 = cp_type_quals (type2);
1946 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1947 return (q1 & q2) == q2;
1950 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1951 more cv-qualified that TYPE1, and 0 otherwise. */
1954 comp_cv_qualification (int q1, int q2)
1956 if (q1 == q2)
1957 return 0;
1959 if ((q1 & q2) == q2)
1960 return 1;
1961 else if ((q1 & q2) == q1)
1962 return -1;
1964 return 0;
1968 comp_cv_qualification (const_tree type1, const_tree type2)
1970 int q1 = cp_type_quals (type1);
1971 int q2 = cp_type_quals (type2);
1972 return comp_cv_qualification (q1, q2);
1975 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1976 subset of the cv-qualification signature of TYPE2, and the types
1977 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1980 comp_cv_qual_signature (tree type1, tree type2)
1982 if (comp_ptr_ttypes_real (type2, type1, -1))
1983 return 1;
1984 else if (comp_ptr_ttypes_real (type1, type2, -1))
1985 return -1;
1986 else
1987 return 0;
1990 /* Subroutines of `comptypes'. */
1992 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1993 equivalent in the sense that functions with those parameter types
1994 can have equivalent types. The two lists must be equivalent,
1995 element by element. */
1997 bool
1998 compparms (const_tree parms1, const_tree parms2)
2000 const_tree t1, t2;
2002 /* An unspecified parmlist matches any specified parmlist
2003 whose argument types don't need default promotions. */
2005 for (t1 = parms1, t2 = parms2;
2006 t1 || t2;
2007 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2009 /* If one parmlist is shorter than the other,
2010 they fail to match. */
2011 if (!t1 || !t2)
2012 return false;
2013 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2014 return false;
2016 return true;
2020 /* Process a sizeof or alignof expression where the operand is a type.
2021 STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2022 or GNU (preferred alignment) semantics; it is ignored if OP is
2023 SIZEOF_EXPR. */
2025 tree
2026 cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2027 bool std_alignof, bool complain)
2029 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2030 if (type == error_mark_node)
2031 return error_mark_node;
2033 type = non_reference (type);
2034 if (TREE_CODE (type) == METHOD_TYPE)
2036 if (complain)
2038 pedwarn (loc, OPT_Wpointer_arith,
2039 "invalid application of %qs to a member function",
2040 OVL_OP_INFO (false, op)->name);
2041 return size_one_node;
2043 else
2044 return error_mark_node;
2046 else if (VOID_TYPE_P (type) && std_alignof)
2048 if (complain)
2049 error_at (loc, "invalid application of %qs to a void type",
2050 OVL_OP_INFO (false, op)->name);
2051 return error_mark_node;
2054 bool dependent_p = dependent_type_p (type);
2055 if (!dependent_p)
2056 complete_type (type);
2057 if (dependent_p
2058 /* VLA types will have a non-constant size. In the body of an
2059 uninstantiated template, we don't need to try to compute the
2060 value, because the sizeof expression is not an integral
2061 constant expression in that case. And, if we do try to
2062 compute the value, we'll likely end up with SAVE_EXPRs, which
2063 the template substitution machinery does not expect to see. */
2064 || (processing_template_decl
2065 && COMPLETE_TYPE_P (type)
2066 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2068 tree value = build_min (op, size_type_node, type);
2069 TREE_READONLY (value) = 1;
2070 if (op == ALIGNOF_EXPR && std_alignof)
2071 ALIGNOF_EXPR_STD_P (value) = true;
2072 SET_EXPR_LOCATION (value, loc);
2073 return value;
2076 return c_sizeof_or_alignof_type (loc, complete_type (type),
2077 op == SIZEOF_EXPR, std_alignof,
2078 complain);
2081 /* Return the size of the type, without producing any warnings for
2082 types whose size cannot be taken. This routine should be used only
2083 in some other routine that has already produced a diagnostic about
2084 using the size of such a type. */
2085 tree
2086 cxx_sizeof_nowarn (tree type)
2088 if (TREE_CODE (type) == FUNCTION_TYPE
2089 || VOID_TYPE_P (type)
2090 || TREE_CODE (type) == ERROR_MARK)
2091 return size_one_node;
2092 else if (!COMPLETE_TYPE_P (type))
2093 return size_zero_node;
2094 else
2095 return cxx_sizeof_or_alignof_type (input_location, type,
2096 SIZEOF_EXPR, false, false);
2099 /* Process a sizeof expression where the operand is an expression. */
2101 static tree
2102 cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2104 if (e == error_mark_node)
2105 return error_mark_node;
2107 if (instantiation_dependent_uneval_expression_p (e))
2109 e = build_min (SIZEOF_EXPR, size_type_node, e);
2110 TREE_SIDE_EFFECTS (e) = 0;
2111 TREE_READONLY (e) = 1;
2112 SET_EXPR_LOCATION (e, loc);
2114 return e;
2117 location_t e_loc = cp_expr_loc_or_loc (e, loc);
2118 STRIP_ANY_LOCATION_WRAPPER (e);
2120 /* To get the size of a static data member declared as an array of
2121 unknown bound, we need to instantiate it. */
2122 if (VAR_P (e)
2123 && VAR_HAD_UNKNOWN_BOUND (e)
2124 && DECL_TEMPLATE_INSTANTIATION (e))
2125 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
2127 if (TREE_CODE (e) == PARM_DECL
2128 && DECL_ARRAY_PARAMETER_P (e)
2129 && (complain & tf_warning))
2131 auto_diagnostic_group d;
2132 if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2133 "%<sizeof%> on array function parameter %qE "
2134 "will return size of %qT", e, TREE_TYPE (e)))
2135 inform (DECL_SOURCE_LOCATION (e), "declared here");
2138 e = mark_type_use (e);
2140 if (bitfield_p (e))
2142 if (complain & tf_error)
2143 error_at (e_loc,
2144 "invalid application of %<sizeof%> to a bit-field");
2145 else
2146 return error_mark_node;
2147 e = char_type_node;
2149 else if (is_overloaded_fn (e))
2151 if (complain & tf_error)
2152 permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2153 "an expression of function type");
2154 else
2155 return error_mark_node;
2156 e = char_type_node;
2158 else if (type_unknown_p (e))
2160 if (complain & tf_error)
2161 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2162 else
2163 return error_mark_node;
2164 e = char_type_node;
2166 else
2167 e = TREE_TYPE (e);
2169 return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2170 complain & tf_error);
2173 /* Implement the __alignof keyword: Return the minimum required
2174 alignment of E, measured in bytes. For VAR_DECL's and
2175 FIELD_DECL's return DECL_ALIGN (which can be set from an
2176 "aligned" __attribute__ specification). STD_ALIGNOF acts
2177 like in cxx_sizeof_or_alignof_type. */
2179 static tree
2180 cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2181 tsubst_flags_t complain)
2183 tree t;
2185 if (e == error_mark_node)
2186 return error_mark_node;
2188 if (processing_template_decl)
2190 e = build_min (ALIGNOF_EXPR, size_type_node, e);
2191 TREE_SIDE_EFFECTS (e) = 0;
2192 TREE_READONLY (e) = 1;
2193 SET_EXPR_LOCATION (e, loc);
2194 ALIGNOF_EXPR_STD_P (e) = std_alignof;
2196 return e;
2199 location_t e_loc = cp_expr_loc_or_loc (e, loc);
2200 STRIP_ANY_LOCATION_WRAPPER (e);
2202 e = mark_type_use (e);
2204 if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2205 !(complain & tf_error)))
2207 if (!(complain & tf_error))
2208 return error_mark_node;
2209 t = size_one_node;
2211 else if (VAR_P (e))
2212 t = size_int (DECL_ALIGN_UNIT (e));
2213 else if (bitfield_p (e))
2215 if (complain & tf_error)
2216 error_at (e_loc,
2217 "invalid application of %<__alignof%> to a bit-field");
2218 else
2219 return error_mark_node;
2220 t = size_one_node;
2222 else if (TREE_CODE (e) == COMPONENT_REF
2223 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2224 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2225 else if (is_overloaded_fn (e))
2227 if (complain & tf_error)
2228 permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2229 "an expression of function type");
2230 else
2231 return error_mark_node;
2232 if (TREE_CODE (e) == FUNCTION_DECL)
2233 t = size_int (DECL_ALIGN_UNIT (e));
2234 else
2235 t = size_one_node;
2237 else if (type_unknown_p (e))
2239 if (complain & tf_error)
2240 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2241 else
2242 return error_mark_node;
2243 t = size_one_node;
2245 else
2246 return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2247 ALIGNOF_EXPR, std_alignof,
2248 complain & tf_error);
2250 return fold_convert_loc (loc, size_type_node, t);
2253 /* Process a sizeof or alignof expression E with code OP where the operand
2254 is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2256 tree
2257 cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2258 bool std_alignof, bool complain)
2260 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2261 if (op == SIZEOF_EXPR)
2262 return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2263 else
2264 return cxx_alignof_expr (loc, e, std_alignof,
2265 complain? tf_warning_or_error : tf_none);
2268 /* Build a representation of an expression 'alignas(E).' Return the
2269 folded integer value of E if it is an integral constant expression
2270 that resolves to a valid alignment. If E depends on a template
2271 parameter, return a syntactic representation tree of kind
2272 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2273 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2275 tree
2276 cxx_alignas_expr (tree e)
2278 if (e == NULL_TREE || e == error_mark_node
2279 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2280 return e;
2282 if (TYPE_P (e))
2283 /* [dcl.align]/3:
2285 When the alignment-specifier is of the form
2286 alignas(type-id), it shall have the same effect as
2287 alignas(alignof(type-id)). */
2289 return cxx_sizeof_or_alignof_type (input_location,
2290 e, ALIGNOF_EXPR,
2291 /*std_alignof=*/true,
2292 /*complain=*/true);
2294 /* If we reach this point, it means the alignas expression if of
2295 the form "alignas(assignment-expression)", so we should follow
2296 what is stated by [dcl.align]/2. */
2298 if (value_dependent_expression_p (e))
2299 /* Leave value-dependent expression alone for now. */
2300 return e;
2302 e = instantiate_non_dependent_expr (e);
2303 e = mark_rvalue_use (e);
2305 /* [dcl.align]/2 says:
2307 the assignment-expression shall be an integral constant
2308 expression. */
2310 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2312 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2313 return error_mark_node;
2316 return cxx_constant_value (e);
2320 /* EXPR is being used in a context that is not a function call.
2321 Enforce:
2323 [expr.ref]
2325 The expression can be used only as the left-hand operand of a
2326 member function call.
2328 [expr.mptr.operator]
2330 If the result of .* or ->* is a function, then that result can be
2331 used only as the operand for the function call operator ().
2333 by issuing an error message if appropriate. Returns true iff EXPR
2334 violates these rules. */
2336 bool
2337 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2339 if (expr == NULL_TREE)
2340 return false;
2341 /* Don't enforce this in MS mode. */
2342 if (flag_ms_extensions)
2343 return false;
2344 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2345 expr = get_first_fn (expr);
2346 if (TREE_TYPE (expr)
2347 && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
2349 if (complain & tf_error)
2351 if (DECL_P (expr))
2353 error_at (loc, "invalid use of non-static member function %qD",
2354 expr);
2355 inform (DECL_SOURCE_LOCATION (expr), "declared here");
2357 else
2358 error_at (loc, "invalid use of non-static member function of "
2359 "type %qT", TREE_TYPE (expr));
2361 return true;
2363 return false;
2366 /* If EXP is a reference to a bit-field, and the type of EXP does not
2367 match the declared type of the bit-field, return the declared type
2368 of the bit-field. Otherwise, return NULL_TREE. */
2370 tree
2371 is_bitfield_expr_with_lowered_type (const_tree exp)
2373 switch (TREE_CODE (exp))
2375 case COND_EXPR:
2376 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2377 ? TREE_OPERAND (exp, 1)
2378 : TREE_OPERAND (exp, 0)))
2379 return NULL_TREE;
2380 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2382 case COMPOUND_EXPR:
2383 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2385 case MODIFY_EXPR:
2386 case SAVE_EXPR:
2387 case UNARY_PLUS_EXPR:
2388 case PREDECREMENT_EXPR:
2389 case PREINCREMENT_EXPR:
2390 case POSTDECREMENT_EXPR:
2391 case POSTINCREMENT_EXPR:
2392 case NEGATE_EXPR:
2393 case NON_LVALUE_EXPR:
2394 case BIT_NOT_EXPR:
2395 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2397 case COMPONENT_REF:
2399 tree field;
2401 field = TREE_OPERAND (exp, 1);
2402 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2403 return NULL_TREE;
2404 if (same_type_ignoring_top_level_qualifiers_p
2405 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2406 return NULL_TREE;
2407 return DECL_BIT_FIELD_TYPE (field);
2410 case VAR_DECL:
2411 if (DECL_HAS_VALUE_EXPR_P (exp))
2412 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2413 (CONST_CAST_TREE (exp)));
2414 return NULL_TREE;
2416 case VIEW_CONVERT_EXPR:
2417 if (location_wrapper_p (exp))
2418 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2419 else
2420 return NULL_TREE;
2422 default:
2423 return NULL_TREE;
2427 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2428 bitfield with a lowered type, the type of EXP is returned, rather
2429 than NULL_TREE. */
2431 tree
2432 unlowered_expr_type (const_tree exp)
2434 tree type;
2435 tree etype = TREE_TYPE (exp);
2437 type = is_bitfield_expr_with_lowered_type (exp);
2438 if (type)
2439 type = cp_build_qualified_type (type, cp_type_quals (etype));
2440 else
2441 type = etype;
2443 return type;
2446 /* Perform the conversions in [expr] that apply when an lvalue appears
2447 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2448 function-to-pointer conversions. In addition, bitfield references are
2449 converted to their declared types. Note that this function does not perform
2450 the lvalue-to-rvalue conversion for class types. If you need that conversion
2451 for class types, then you probably need to use force_rvalue.
2453 Although the returned value is being used as an rvalue, this
2454 function does not wrap the returned expression in a
2455 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2456 that the return value is no longer an lvalue. */
2458 tree
2459 decay_conversion (tree exp,
2460 tsubst_flags_t complain,
2461 bool reject_builtin /* = true */)
2463 tree type;
2464 enum tree_code code;
2465 location_t loc = cp_expr_loc_or_input_loc (exp);
2467 type = TREE_TYPE (exp);
2468 if (type == error_mark_node)
2469 return error_mark_node;
2471 exp = resolve_nondeduced_context_or_error (exp, complain);
2473 code = TREE_CODE (type);
2475 if (error_operand_p (exp))
2476 return error_mark_node;
2478 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2480 mark_rvalue_use (exp, loc, reject_builtin);
2481 return nullptr_node;
2484 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2485 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2486 if (code == VOID_TYPE)
2488 if (complain & tf_error)
2489 error_at (loc, "void value not ignored as it ought to be");
2490 return error_mark_node;
2492 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2493 return error_mark_node;
2494 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2496 exp = mark_lvalue_use (exp);
2497 if (reject_builtin && reject_gcc_builtin (exp, loc))
2498 return error_mark_node;
2499 return cp_build_addr_expr (exp, complain);
2501 if (code == ARRAY_TYPE)
2503 tree adr;
2504 tree ptrtype;
2506 exp = mark_lvalue_use (exp);
2508 if (INDIRECT_REF_P (exp))
2509 return build_nop (build_pointer_type (TREE_TYPE (type)),
2510 TREE_OPERAND (exp, 0));
2512 if (TREE_CODE (exp) == COMPOUND_EXPR)
2514 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2515 if (op1 == error_mark_node)
2516 return error_mark_node;
2517 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2518 TREE_OPERAND (exp, 0), op1);
2521 if (!obvalue_p (exp)
2522 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2524 if (complain & tf_error)
2525 error_at (loc, "invalid use of non-lvalue array");
2526 return error_mark_node;
2529 /* Don't let an array compound literal decay to a pointer. It can
2530 still be used to initialize an array or bind to a reference. */
2531 if (TREE_CODE (exp) == TARGET_EXPR)
2533 if (complain & tf_error)
2534 error_at (loc, "taking address of temporary array");
2535 return error_mark_node;
2538 ptrtype = build_pointer_type (TREE_TYPE (type));
2540 if (VAR_P (exp))
2542 if (!cxx_mark_addressable (exp))
2543 return error_mark_node;
2544 adr = build_nop (ptrtype, build_address (exp));
2545 return adr;
2547 /* This way is better for a COMPONENT_REF since it can
2548 simplify the offset for a component. */
2549 adr = cp_build_addr_expr (exp, complain);
2550 return cp_convert (ptrtype, adr, complain);
2553 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2554 exp = mark_rvalue_use (exp, loc, reject_builtin);
2556 /* If a bitfield is used in a context where integral promotion
2557 applies, then the caller is expected to have used
2558 default_conversion. That function promotes bitfields correctly
2559 before calling this function. At this point, if we have a
2560 bitfield referenced, we may assume that is not subject to
2561 promotion, and that, therefore, the type of the resulting rvalue
2562 is the declared type of the bitfield. */
2563 exp = convert_bitfield_to_declared_type (exp);
2565 /* We do not call rvalue() here because we do not want to wrap EXP
2566 in a NON_LVALUE_EXPR. */
2568 /* [basic.lval]
2570 Non-class rvalues always have cv-unqualified types. */
2571 type = TREE_TYPE (exp);
2572 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2573 exp = build_nop (cv_unqualified (type), exp);
2575 if (!complete_type_or_maybe_complain (type, exp, complain))
2576 return error_mark_node;
2578 return exp;
2581 /* Perform preparatory conversions, as part of the "usual arithmetic
2582 conversions". In particular, as per [expr]:
2584 Whenever an lvalue expression appears as an operand of an
2585 operator that expects the rvalue for that operand, the
2586 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2587 standard conversions are applied to convert the expression to an
2588 rvalue.
2590 In addition, we perform integral promotions here, as those are
2591 applied to both operands to a binary operator before determining
2592 what additional conversions should apply. */
2594 static tree
2595 cp_default_conversion (tree exp, tsubst_flags_t complain)
2597 /* Check for target-specific promotions. */
2598 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2599 if (promoted_type)
2600 exp = cp_convert (promoted_type, exp, complain);
2601 /* Perform the integral promotions first so that bitfield
2602 expressions (which may promote to "int", even if the bitfield is
2603 declared "unsigned") are promoted correctly. */
2604 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2605 exp = cp_perform_integral_promotions (exp, complain);
2606 /* Perform the other conversions. */
2607 exp = decay_conversion (exp, complain);
2609 return exp;
2612 /* C version. */
2614 tree
2615 default_conversion (tree exp)
2617 return cp_default_conversion (exp, tf_warning_or_error);
2620 /* EXPR is an expression with an integral or enumeration type.
2621 Perform the integral promotions in [conv.prom], and return the
2622 converted value. */
2624 tree
2625 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2627 tree type;
2628 tree promoted_type;
2630 expr = mark_rvalue_use (expr);
2631 if (error_operand_p (expr))
2632 return error_mark_node;
2634 type = TREE_TYPE (expr);
2636 /* [conv.prom]
2638 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2639 of type int if int can represent all the values of the bit-field;
2640 otherwise, it can be converted to unsigned int if unsigned int can
2641 represent all the values of the bit-field. If the bit-field is larger yet,
2642 no integral promotion applies to it. If the bit-field has an enumerated
2643 type, it is treated as any other value of that type for promotion
2644 purposes. */
2645 tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2646 if (bitfield_type
2647 && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2648 || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2649 type = bitfield_type;
2651 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2652 /* Scoped enums don't promote. */
2653 if (SCOPED_ENUM_P (type))
2654 return expr;
2655 promoted_type = type_promotes_to (type);
2656 if (type != promoted_type)
2657 expr = cp_convert (promoted_type, expr, complain);
2658 else if (bitfield_type && bitfield_type != type)
2659 /* Prevent decay_conversion from converting to bitfield_type. */
2660 expr = build_nop (type, expr);
2661 return expr;
2664 /* C version. */
2666 tree
2667 perform_integral_promotions (tree expr)
2669 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2672 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2673 decay_conversion to one. */
2676 string_conv_p (const_tree totype, const_tree exp, int warn)
2678 tree t;
2680 if (!TYPE_PTR_P (totype))
2681 return 0;
2683 t = TREE_TYPE (totype);
2684 if (!same_type_p (t, char_type_node)
2685 && !same_type_p (t, char8_type_node)
2686 && !same_type_p (t, char16_type_node)
2687 && !same_type_p (t, char32_type_node)
2688 && !same_type_p (t, wchar_type_node))
2689 return 0;
2691 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2693 STRIP_ANY_LOCATION_WRAPPER (exp);
2695 if (TREE_CODE (exp) == STRING_CST)
2697 /* Make sure that we don't try to convert between char and wide chars. */
2698 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2699 return 0;
2701 else
2703 /* Is this a string constant which has decayed to 'const char *'? */
2704 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2705 if (!same_type_p (TREE_TYPE (exp), t))
2706 return 0;
2707 STRIP_NOPS (exp);
2708 if (TREE_CODE (exp) != ADDR_EXPR
2709 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2710 return 0;
2712 if (warn)
2714 if (cxx_dialect >= cxx11)
2715 pedwarn (loc, OPT_Wwrite_strings,
2716 "ISO C++ forbids converting a string constant to %qT",
2717 totype);
2718 else
2719 warning_at (loc, OPT_Wwrite_strings,
2720 "deprecated conversion from string constant to %qT",
2721 totype);
2724 return 1;
2727 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2728 can, for example, use as an lvalue. This code used to be in
2729 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2730 expressions, where we're dealing with aggregates. But now it's again only
2731 called from unary_complex_lvalue. The case (in particular) that led to
2732 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2733 get it there. */
2735 static tree
2736 rationalize_conditional_expr (enum tree_code code, tree t,
2737 tsubst_flags_t complain)
2739 location_t loc = cp_expr_loc_or_input_loc (t);
2741 /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2742 the first operand is always the one to be used if both operands
2743 are equal, so we know what conditional expression this used to be. */
2744 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2746 tree op0 = TREE_OPERAND (t, 0);
2747 tree op1 = TREE_OPERAND (t, 1);
2749 /* The following code is incorrect if either operand side-effects. */
2750 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2751 && !TREE_SIDE_EFFECTS (op1));
2752 return
2753 build_conditional_expr (loc,
2754 build_x_binary_op (loc,
2755 (TREE_CODE (t) == MIN_EXPR
2756 ? LE_EXPR : GE_EXPR),
2757 op0, TREE_CODE (op0),
2758 op1, TREE_CODE (op1),
2759 NULL_TREE,
2760 /*overload=*/NULL,
2761 complain),
2762 cp_build_unary_op (code, op0, false, complain),
2763 cp_build_unary_op (code, op1, false, complain),
2764 complain);
2767 tree op1 = TREE_OPERAND (t, 1);
2768 if (TREE_CODE (op1) != THROW_EXPR)
2769 op1 = cp_build_unary_op (code, op1, false, complain);
2770 tree op2 = TREE_OPERAND (t, 2);
2771 if (TREE_CODE (op2) != THROW_EXPR)
2772 op2 = cp_build_unary_op (code, op2, false, complain);
2774 return
2775 build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2778 /* Given the TYPE of an anonymous union field inside T, return the
2779 FIELD_DECL for the field. If not found return NULL_TREE. Because
2780 anonymous unions can nest, we must also search all anonymous unions
2781 that are directly reachable. */
2783 tree
2784 lookup_anon_field (tree, tree type)
2786 tree field;
2788 type = TYPE_MAIN_VARIANT (type);
2789 field = ANON_AGGR_TYPE_FIELD (type);
2790 gcc_assert (field);
2791 return field;
2794 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2795 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2796 non-NULL, it indicates the path to the base used to name MEMBER.
2797 If PRESERVE_REFERENCE is true, the expression returned will have
2798 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2799 returned will have the type referred to by the reference.
2801 This function does not perform access control; that is either done
2802 earlier by the parser when the name of MEMBER is resolved to MEMBER
2803 itself, or later when overload resolution selects one of the
2804 functions indicated by MEMBER. */
2806 tree
2807 build_class_member_access_expr (cp_expr object, tree member,
2808 tree access_path, bool preserve_reference,
2809 tsubst_flags_t complain)
2811 tree object_type;
2812 tree member_scope;
2813 tree result = NULL_TREE;
2814 tree using_decl = NULL_TREE;
2816 if (error_operand_p (object) || error_operand_p (member))
2817 return error_mark_node;
2819 gcc_assert (DECL_P (member) || BASELINK_P (member));
2821 /* [expr.ref]
2823 The type of the first expression shall be "class object" (of a
2824 complete type). */
2825 object_type = TREE_TYPE (object);
2826 if (!currently_open_class (object_type)
2827 && !complete_type_or_maybe_complain (object_type, object, complain))
2828 return error_mark_node;
2829 if (!CLASS_TYPE_P (object_type))
2831 if (complain & tf_error)
2833 if (INDIRECT_TYPE_P (object_type)
2834 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2835 error ("request for member %qD in %qE, which is of pointer "
2836 "type %qT (maybe you meant to use %<->%> ?)",
2837 member, object.get_value (), object_type);
2838 else
2839 error ("request for member %qD in %qE, which is of non-class "
2840 "type %qT", member, object.get_value (), object_type);
2842 return error_mark_node;
2845 /* The standard does not seem to actually say that MEMBER must be a
2846 member of OBJECT_TYPE. However, that is clearly what is
2847 intended. */
2848 if (DECL_P (member))
2850 member_scope = DECL_CLASS_CONTEXT (member);
2851 if (!mark_used (member, complain) && !(complain & tf_error))
2852 return error_mark_node;
2854 if (TREE_UNAVAILABLE (member))
2855 error_unavailable_use (member, NULL_TREE);
2856 else if (TREE_DEPRECATED (member))
2857 warn_deprecated_use (member, NULL_TREE);
2859 else
2860 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2861 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2862 presently be the anonymous union. Go outwards until we find a
2863 type related to OBJECT_TYPE. */
2864 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2865 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2866 object_type))
2867 member_scope = TYPE_CONTEXT (member_scope);
2868 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2870 if (complain & tf_error)
2872 if (TREE_CODE (member) == FIELD_DECL)
2873 error ("invalid use of non-static data member %qE", member);
2874 else
2875 error ("%qD is not a member of %qT", member, object_type);
2877 return error_mark_node;
2880 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2881 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2882 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2883 if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2885 temp = cp_build_fold_indirect_ref (temp);
2886 if (!lvalue_p (object) && lvalue_p (temp))
2887 /* Preserve rvalueness. */
2888 temp = move (temp);
2889 object = temp;
2892 /* In [expr.ref], there is an explicit list of the valid choices for
2893 MEMBER. We check for each of those cases here. */
2894 if (VAR_P (member))
2896 /* A static data member. */
2897 result = member;
2898 mark_exp_read (object);
2900 if (tree wrap = maybe_get_tls_wrapper_call (result))
2901 /* Replace an evaluated use of the thread_local variable with
2902 a call to its wrapper. */
2903 result = wrap;
2905 /* If OBJECT has side-effects, they are supposed to occur. */
2906 if (TREE_SIDE_EFFECTS (object))
2907 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2909 else if (TREE_CODE (member) == FIELD_DECL)
2911 /* A non-static data member. */
2912 bool null_object_p;
2913 int type_quals;
2914 tree member_type;
2916 if (INDIRECT_REF_P (object))
2917 null_object_p =
2918 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2919 else
2920 null_object_p = false;
2922 /* Convert OBJECT to the type of MEMBER. */
2923 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2924 TYPE_MAIN_VARIANT (member_scope)))
2926 tree binfo;
2927 base_kind kind;
2929 /* We didn't complain above about a currently open class, but now we
2930 must: we don't know how to refer to a base member before layout is
2931 complete. But still don't complain in a template. */
2932 if (!cp_unevaluated_operand
2933 && !dependent_type_p (object_type)
2934 && !complete_type_or_maybe_complain (object_type, object,
2935 complain))
2936 return error_mark_node;
2938 binfo = lookup_base (access_path ? access_path : object_type,
2939 member_scope, ba_unique, &kind, complain);
2940 if (binfo == error_mark_node)
2941 return error_mark_node;
2943 /* It is invalid to try to get to a virtual base of a
2944 NULL object. The most common cause is invalid use of
2945 offsetof macro. */
2946 if (null_object_p && kind == bk_via_virtual)
2948 if (complain & tf_error)
2950 error ("invalid access to non-static data member %qD in "
2951 "virtual base of NULL object", member);
2953 return error_mark_node;
2956 /* Convert to the base. */
2957 object = build_base_path (PLUS_EXPR, object, binfo,
2958 /*nonnull=*/1, complain);
2959 /* If we found the base successfully then we should be able
2960 to convert to it successfully. */
2961 gcc_assert (object != error_mark_node);
2964 /* If MEMBER is from an anonymous aggregate, we have converted
2965 OBJECT so that it refers to the class containing the
2966 anonymous union. Generate a reference to the anonymous union
2967 itself, and recur to find MEMBER. */
2968 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2969 /* When this code is called from build_field_call, the
2970 object already has the type of the anonymous union.
2971 That is because the COMPONENT_REF was already
2972 constructed, and was then disassembled before calling
2973 build_field_call. After the function-call code is
2974 cleaned up, this waste can be eliminated. */
2975 && (!same_type_ignoring_top_level_qualifiers_p
2976 (TREE_TYPE (object), DECL_CONTEXT (member))))
2978 tree anonymous_union;
2980 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2981 DECL_CONTEXT (member));
2982 object = build_class_member_access_expr (object,
2983 anonymous_union,
2984 /*access_path=*/NULL_TREE,
2985 preserve_reference,
2986 complain);
2989 /* Compute the type of the field, as described in [expr.ref]. */
2990 type_quals = TYPE_UNQUALIFIED;
2991 member_type = TREE_TYPE (member);
2992 if (!TYPE_REF_P (member_type))
2994 type_quals = (cp_type_quals (member_type)
2995 | cp_type_quals (object_type));
2997 /* A field is const (volatile) if the enclosing object, or the
2998 field itself, is const (volatile). But, a mutable field is
2999 not const, even within a const object. */
3000 if (DECL_MUTABLE_P (member))
3001 type_quals &= ~TYPE_QUAL_CONST;
3002 member_type = cp_build_qualified_type (member_type, type_quals);
3005 result = build3_loc (input_location, COMPONENT_REF, member_type,
3006 object, member, NULL_TREE);
3008 /* Mark the expression const or volatile, as appropriate. Even
3009 though we've dealt with the type above, we still have to mark the
3010 expression itself. */
3011 if (type_quals & TYPE_QUAL_CONST)
3012 TREE_READONLY (result) = 1;
3013 if (type_quals & TYPE_QUAL_VOLATILE)
3014 TREE_THIS_VOLATILE (result) = 1;
3016 else if (BASELINK_P (member))
3018 /* The member is a (possibly overloaded) member function. */
3019 tree functions;
3020 tree type;
3022 /* If the MEMBER is exactly one static member function, then we
3023 know the type of the expression. Otherwise, we must wait
3024 until overload resolution has been performed. */
3025 functions = BASELINK_FUNCTIONS (member);
3026 if (TREE_CODE (functions) == FUNCTION_DECL
3027 && DECL_STATIC_FUNCTION_P (functions))
3028 type = TREE_TYPE (functions);
3029 else
3030 type = unknown_type_node;
3031 /* Note that we do not convert OBJECT to the BASELINK_BINFO
3032 base. That will happen when the function is called. */
3033 result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3034 NULL_TREE);
3036 else if (TREE_CODE (member) == CONST_DECL)
3038 /* The member is an enumerator. */
3039 result = member;
3040 /* If OBJECT has side-effects, they are supposed to occur. */
3041 if (TREE_SIDE_EFFECTS (object))
3042 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3043 object, result);
3045 else if ((using_decl = strip_using_decl (member)) != member)
3046 result = build_class_member_access_expr (object,
3047 using_decl,
3048 access_path, preserve_reference,
3049 complain);
3050 else
3052 if (complain & tf_error)
3053 error ("invalid use of %qD", member);
3054 return error_mark_node;
3057 if (!preserve_reference)
3058 /* [expr.ref]
3060 If E2 is declared to have type "reference to T", then ... the
3061 type of E1.E2 is T. */
3062 result = convert_from_reference (result);
3064 return result;
3067 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3068 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3070 tree
3071 lookup_destructor (tree object, tree scope, tree dtor_name,
3072 tsubst_flags_t complain)
3074 tree object_type = TREE_TYPE (object);
3075 tree dtor_type = TREE_OPERAND (dtor_name, 0);
3076 tree expr;
3078 /* We've already complained about this destructor. */
3079 if (dtor_type == error_mark_node)
3080 return error_mark_node;
3082 if (scope && !check_dtor_name (scope, dtor_type))
3084 if (complain & tf_error)
3085 error ("qualified type %qT does not match destructor name ~%qT",
3086 scope, dtor_type);
3087 return error_mark_node;
3089 if (is_auto (dtor_type))
3090 dtor_type = object_type;
3091 else if (identifier_p (dtor_type))
3093 /* In a template, names we can't find a match for are still accepted
3094 destructor names, and we check them here. */
3095 if (check_dtor_name (object_type, dtor_type))
3096 dtor_type = object_type;
3097 else
3099 if (complain & tf_error)
3100 error ("object type %qT does not match destructor name ~%qT",
3101 object_type, dtor_type);
3102 return error_mark_node;
3106 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3108 if (complain & tf_error)
3109 error ("the type being destroyed is %qT, but the destructor "
3110 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3111 return error_mark_node;
3113 expr = lookup_member (dtor_type, complete_dtor_identifier,
3114 /*protect=*/1, /*want_type=*/false,
3115 tf_warning_or_error);
3116 if (!expr)
3118 if (complain & tf_error)
3119 cxx_incomplete_type_error (dtor_name, dtor_type);
3120 return error_mark_node;
3122 expr = (adjust_result_of_qualified_name_lookup
3123 (expr, dtor_type, object_type));
3124 if (scope == NULL_TREE)
3125 /* We need to call adjust_result_of_qualified_name_lookup in case the
3126 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3127 that we still get virtual function binding. */
3128 BASELINK_QUALIFIED_P (expr) = false;
3129 return expr;
3132 /* An expression of the form "A::template B" has been resolved to
3133 DECL. Issue a diagnostic if B is not a template or template
3134 specialization. */
3136 void
3137 check_template_keyword (tree decl)
3139 /* The standard says:
3141 [temp.names]
3143 If a name prefixed by the keyword template is not a member
3144 template, the program is ill-formed.
3146 DR 228 removed the restriction that the template be a member
3147 template.
3149 DR 96, if accepted would add the further restriction that explicit
3150 template arguments must be provided if the template keyword is
3151 used, but, as of 2005-10-16, that DR is still in "drafting". If
3152 this DR is accepted, then the semantic checks here can be
3153 simplified, as the entity named must in fact be a template
3154 specialization, rather than, as at present, a set of overloaded
3155 functions containing at least one template function. */
3156 if (TREE_CODE (decl) != TEMPLATE_DECL
3157 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3159 if (VAR_P (decl))
3161 if (DECL_USE_TEMPLATE (decl)
3162 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3164 else
3165 permerror (input_location, "%qD is not a template", decl);
3167 else if (!is_overloaded_fn (decl))
3168 permerror (input_location, "%qD is not a template", decl);
3169 else
3171 bool found = false;
3173 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3174 !found && iter; ++iter)
3176 tree fn = *iter;
3177 if (TREE_CODE (fn) == TEMPLATE_DECL
3178 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3179 || (TREE_CODE (fn) == FUNCTION_DECL
3180 && DECL_USE_TEMPLATE (fn)
3181 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3182 found = true;
3184 if (!found)
3185 permerror (input_location, "%qD is not a template", decl);
3190 /* Record that an access failure occurred on BASETYPE_PATH attempting
3191 to access DECL, where DIAG_DECL should be used for diagnostics. */
3193 void
3194 access_failure_info::record_access_failure (tree basetype_path,
3195 tree decl, tree diag_decl)
3197 m_was_inaccessible = true;
3198 m_basetype_path = basetype_path;
3199 m_decl = decl;
3200 m_diag_decl = diag_decl;
3203 /* If an access failure was recorded, then attempt to locate an
3204 accessor function for the pertinent field.
3205 Otherwise, return NULL_TREE. */
3207 tree
3208 access_failure_info::get_any_accessor (bool const_p) const
3210 if (!was_inaccessible_p ())
3211 return NULL_TREE;
3213 tree accessor
3214 = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3215 if (!accessor)
3216 return NULL_TREE;
3218 /* The accessor must itself be accessible for it to be a reasonable
3219 suggestion. */
3220 if (!accessible_p (m_basetype_path, accessor, true))
3221 return NULL_TREE;
3223 return accessor;
3226 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3227 replacing the primary location in RICHLOC with "accessor()". */
3229 void
3230 access_failure_info::add_fixit_hint (rich_location *richloc,
3231 tree accessor_decl)
3233 pretty_printer pp;
3234 pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3235 pp_string (&pp, "()");
3236 richloc->add_fixit_replace (pp_formatted_text (&pp));
3239 /* If an access failure was recorded, then attempt to locate an
3240 accessor function for the pertinent field, and if one is
3241 available, add a note and fix-it hint suggesting using it. */
3243 void
3244 access_failure_info::maybe_suggest_accessor (bool const_p) const
3246 tree accessor = get_any_accessor (const_p);
3247 if (accessor == NULL_TREE)
3248 return;
3249 rich_location richloc (line_table, input_location);
3250 add_fixit_hint (&richloc, accessor);
3251 inform (&richloc, "field %q#D can be accessed via %q#D",
3252 m_diag_decl, accessor);
3255 /* Subroutine of finish_class_member_access_expr.
3256 Issue an error about NAME not being a member of ACCESS_PATH (or
3257 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3258 names. */
3260 static void
3261 complain_about_unrecognized_member (tree access_path, tree name,
3262 tree object_type)
3264 /* Attempt to provide a hint about misspelled names. */
3265 tree guessed_id = lookup_member_fuzzy (access_path, name,
3266 /*want_type=*/false);
3267 if (guessed_id == NULL_TREE)
3269 /* No hint. */
3270 error ("%q#T has no member named %qE",
3271 TREE_CODE (access_path) == TREE_BINFO
3272 ? TREE_TYPE (access_path) : object_type, name);
3273 return;
3276 location_t bogus_component_loc = input_location;
3277 gcc_rich_location rich_loc (bogus_component_loc);
3279 /* Check that the guessed name is accessible along access_path. */
3280 access_failure_info afi;
3281 lookup_member (access_path, guessed_id, /*protect=*/1,
3282 /*want_type=*/false, /*complain=*/false,
3283 &afi);
3284 if (afi.was_inaccessible_p ())
3286 tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3287 if (accessor)
3289 /* The guessed name isn't directly accessible, but can be accessed
3290 via an accessor member function. */
3291 afi.add_fixit_hint (&rich_loc, accessor);
3292 error_at (&rich_loc,
3293 "%q#T has no member named %qE;"
3294 " did you mean %q#D? (accessible via %q#D)",
3295 TREE_CODE (access_path) == TREE_BINFO
3296 ? TREE_TYPE (access_path) : object_type,
3297 name, afi.get_diag_decl (), accessor);
3299 else
3301 /* The guessed name isn't directly accessible, and no accessor
3302 member function could be found. */
3303 error_at (&rich_loc,
3304 "%q#T has no member named %qE;"
3305 " did you mean %q#D? (not accessible from this context)",
3306 TREE_CODE (access_path) == TREE_BINFO
3307 ? TREE_TYPE (access_path) : object_type,
3308 name, afi.get_diag_decl ());
3309 complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3310 afi.get_diag_decl (), false, ak_none);
3313 else
3315 /* The guessed name is directly accessible; suggest it. */
3316 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3317 guessed_id);
3318 error_at (&rich_loc,
3319 "%q#T has no member named %qE;"
3320 " did you mean %qE?",
3321 TREE_CODE (access_path) == TREE_BINFO
3322 ? TREE_TYPE (access_path) : object_type,
3323 name, guessed_id);
3327 /* This function is called by the parser to process a class member
3328 access expression of the form OBJECT.NAME. NAME is a node used by
3329 the parser to represent a name; it is not yet a DECL. It may,
3330 however, be a BASELINK where the BASELINK_FUNCTIONS is a
3331 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3332 there is no reason to do the lookup twice, so the parser keeps the
3333 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3334 be a template via the use of the "A::template B" syntax. */
3336 tree
3337 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3338 tsubst_flags_t complain)
3340 tree expr;
3341 tree object_type;
3342 tree member;
3343 tree access_path = NULL_TREE;
3344 tree orig_object = object;
3345 tree orig_name = name;
3347 if (object == error_mark_node || name == error_mark_node)
3348 return error_mark_node;
3350 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3351 if (!objc_is_public (object, name))
3352 return error_mark_node;
3354 object_type = TREE_TYPE (object);
3356 if (processing_template_decl)
3358 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3359 type_dependent_object_expression_p (object)
3360 /* If NAME is "f<args>", where either 'f' or 'args' is
3361 dependent, then the expression is dependent. */
3362 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3363 && dependent_template_id_p (TREE_OPERAND (name, 0),
3364 TREE_OPERAND (name, 1)))
3365 /* If NAME is "T::X" where "T" is dependent, then the
3366 expression is dependent. */
3367 || (TREE_CODE (name) == SCOPE_REF
3368 && TYPE_P (TREE_OPERAND (name, 0))
3369 && dependent_scope_p (TREE_OPERAND (name, 0)))
3370 /* If NAME is operator T where "T" is dependent, we can't
3371 lookup until we instantiate the T. */
3372 || (TREE_CODE (name) == IDENTIFIER_NODE
3373 && IDENTIFIER_CONV_OP_P (name)
3374 && dependent_type_p (TREE_TYPE (name))))
3376 dependent:
3377 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3378 orig_object, orig_name, NULL_TREE);
3380 object = build_non_dependent_expr (object);
3382 else if (c_dialect_objc ()
3383 && identifier_p (name)
3384 && (expr = objc_maybe_build_component_ref (object, name)))
3385 return expr;
3387 /* [expr.ref]
3389 The type of the first expression shall be "class object" (of a
3390 complete type). */
3391 if (!currently_open_class (object_type)
3392 && !complete_type_or_maybe_complain (object_type, object, complain))
3393 return error_mark_node;
3394 if (!CLASS_TYPE_P (object_type))
3396 if (complain & tf_error)
3398 if (INDIRECT_TYPE_P (object_type)
3399 && CLASS_TYPE_P (TREE_TYPE (object_type)))
3400 error ("request for member %qD in %qE, which is of pointer "
3401 "type %qT (maybe you meant to use %<->%> ?)",
3402 name, object.get_value (), object_type);
3403 else
3404 error ("request for member %qD in %qE, which is of non-class "
3405 "type %qT", name, object.get_value (), object_type);
3407 return error_mark_node;
3410 if (BASELINK_P (name))
3411 /* A member function that has already been looked up. */
3412 member = name;
3413 else
3415 bool is_template_id = false;
3416 tree template_args = NULL_TREE;
3417 tree scope = NULL_TREE;
3419 access_path = object_type;
3421 if (TREE_CODE (name) == SCOPE_REF)
3423 /* A qualified name. The qualifying class or namespace `S'
3424 has already been looked up; it is either a TYPE or a
3425 NAMESPACE_DECL. */
3426 scope = TREE_OPERAND (name, 0);
3427 name = TREE_OPERAND (name, 1);
3429 /* If SCOPE is a namespace, then the qualified name does not
3430 name a member of OBJECT_TYPE. */
3431 if (TREE_CODE (scope) == NAMESPACE_DECL)
3433 if (complain & tf_error)
3434 error ("%<%D::%D%> is not a member of %qT",
3435 scope, name, object_type);
3436 return error_mark_node;
3440 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3442 is_template_id = true;
3443 template_args = TREE_OPERAND (name, 1);
3444 name = TREE_OPERAND (name, 0);
3446 if (!identifier_p (name))
3447 name = OVL_NAME (name);
3450 if (scope)
3452 if (TREE_CODE (scope) == ENUMERAL_TYPE)
3454 gcc_assert (!is_template_id);
3455 /* Looking up a member enumerator (c++/56793). */
3456 if (!TYPE_CLASS_SCOPE_P (scope)
3457 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3459 if (complain & tf_error)
3460 error ("%<%D::%D%> is not a member of %qT",
3461 scope, name, object_type);
3462 return error_mark_node;
3464 tree val = lookup_enumerator (scope, name);
3465 if (!val)
3467 if (complain & tf_error)
3468 error ("%qD is not a member of %qD",
3469 name, scope);
3470 return error_mark_node;
3473 if (TREE_SIDE_EFFECTS (object))
3474 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3475 return val;
3478 gcc_assert (CLASS_TYPE_P (scope));
3479 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3481 if (constructor_name_p (name, scope))
3483 if (complain & tf_error)
3484 error ("cannot call constructor %<%T::%D%> directly",
3485 scope, name);
3486 return error_mark_node;
3489 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3490 access_path = lookup_base (object_type, scope, ba_check,
3491 NULL, complain);
3492 if (access_path == error_mark_node)
3493 return error_mark_node;
3494 if (!access_path)
3496 if (any_dependent_bases_p (object_type))
3497 goto dependent;
3498 if (complain & tf_error)
3499 error ("%qT is not a base of %qT", scope, object_type);
3500 return error_mark_node;
3504 if (TREE_CODE (name) == BIT_NOT_EXPR)
3506 if (dependent_type_p (object_type))
3507 /* The destructor isn't declared yet. */
3508 goto dependent;
3509 member = lookup_destructor (object, scope, name, complain);
3511 else
3513 /* Look up the member. */
3514 access_failure_info afi;
3515 if (processing_template_decl)
3516 /* Even though this class member access expression is at this
3517 point not dependent, the member itself may be dependent, and
3518 we must not potentially push a access check for a dependent
3519 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3520 ahead of time here; we're going to redo this member lookup at
3521 instantiation time anyway. */
3522 push_deferring_access_checks (dk_no_check);
3523 member = lookup_member (access_path, name, /*protect=*/1,
3524 /*want_type=*/false, complain,
3525 &afi);
3526 if (processing_template_decl)
3527 pop_deferring_access_checks ();
3528 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3529 if (member == NULL_TREE)
3531 if (dependent_type_p (object_type))
3532 /* Try again at instantiation time. */
3533 goto dependent;
3534 if (complain & tf_error)
3535 complain_about_unrecognized_member (access_path, name,
3536 object_type);
3537 return error_mark_node;
3539 if (member == error_mark_node)
3540 return error_mark_node;
3541 if (DECL_P (member)
3542 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3543 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3544 wrong, so don't use it. */
3545 goto dependent;
3546 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3547 goto dependent;
3550 if (is_template_id)
3552 tree templ = member;
3554 if (BASELINK_P (templ))
3555 member = lookup_template_function (templ, template_args);
3556 else if (variable_template_p (templ))
3557 member = (lookup_and_finish_template_variable
3558 (templ, template_args, complain));
3559 else
3561 if (complain & tf_error)
3562 error ("%qD is not a member template function", name);
3563 return error_mark_node;
3568 if (TREE_UNAVAILABLE (member))
3569 error_unavailable_use (member, NULL_TREE);
3570 else if (TREE_DEPRECATED (member))
3571 warn_deprecated_use (member, NULL_TREE);
3573 if (template_p)
3574 check_template_keyword (member);
3576 expr = build_class_member_access_expr (object, member, access_path,
3577 /*preserve_reference=*/false,
3578 complain);
3579 if (processing_template_decl && expr != error_mark_node)
3581 if (BASELINK_P (member))
3583 if (TREE_CODE (orig_name) == SCOPE_REF)
3584 BASELINK_QUALIFIED_P (member) = 1;
3585 orig_name = member;
3587 return build_min_non_dep (COMPONENT_REF, expr,
3588 orig_object, orig_name,
3589 NULL_TREE);
3592 return expr;
3595 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3596 type. */
3598 tree
3599 build_simple_component_ref (tree object, tree member)
3601 tree type = cp_build_qualified_type (TREE_TYPE (member),
3602 cp_type_quals (TREE_TYPE (object)));
3603 return build3_loc (input_location,
3604 COMPONENT_REF, type,
3605 object, member, NULL_TREE);
3608 /* Return an expression for the MEMBER_NAME field in the internal
3609 representation of PTRMEM, a pointer-to-member function. (Each
3610 pointer-to-member function type gets its own RECORD_TYPE so it is
3611 more convenient to access the fields by name than by FIELD_DECL.)
3612 This routine converts the NAME to a FIELD_DECL and then creates the
3613 node for the complete expression. */
3615 tree
3616 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3618 tree ptrmem_type;
3619 tree member;
3621 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3623 for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3624 if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3625 return e.value;
3626 gcc_unreachable ();
3629 /* This code is a stripped down version of
3630 build_class_member_access_expr. It does not work to use that
3631 routine directly because it expects the object to be of class
3632 type. */
3633 ptrmem_type = TREE_TYPE (ptrmem);
3634 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3635 for (member = TYPE_FIELDS (ptrmem_type); member;
3636 member = DECL_CHAIN (member))
3637 if (DECL_NAME (member) == member_name)
3638 break;
3639 return build_simple_component_ref (ptrmem, member);
3642 /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3643 and for any other relevant operator. */
3645 static tree
3646 op_unqualified_lookup (tree_code code, bool is_assign)
3648 tree lookups = NULL_TREE;
3650 if (cxx_dialect >= cxx20 && !is_assign)
3652 if (code == NE_EXPR)
3654 /* != can get rewritten in terms of ==. */
3655 tree fnname = ovl_op_identifier (false, EQ_EXPR);
3656 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3657 lookups = tree_cons (fnname, fns, lookups);
3659 else if (code == GT_EXPR || code == LE_EXPR
3660 || code == LT_EXPR || code == GE_EXPR)
3662 /* These can get rewritten in terms of <=>. */
3663 tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3664 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3665 lookups = tree_cons (fnname, fns, lookups);
3669 tree fnname = ovl_op_identifier (is_assign, code);
3670 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3671 lookups = tree_cons (fnname, fns, lookups);
3673 if (lookups)
3674 return lookups;
3675 else
3676 return build_tree_list (NULL_TREE, NULL_TREE);
3679 /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3680 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3681 name lookup for the given operator. */
3683 tree
3684 build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3686 if (lookups)
3687 /* We're partially instantiating a dependent operator expression, and
3688 LOOKUPS is the result of phase 1 name lookup that we performed
3689 earlier at template definition time, so just reuse the corresponding
3690 DEPENDENT_OPERATOR_TYPE. */
3691 return TREE_TYPE (lookups);
3693 /* Otherwise we're processing a dependent operator expression at template
3694 definition time, so perform phase 1 name lookup now. */
3695 lookups = op_unqualified_lookup (code, is_assign);
3697 tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3698 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3699 TREE_TYPE (lookups) = type;
3700 return type;
3703 /* Given an expression PTR for a pointer, return an expression
3704 for the value pointed to.
3705 ERRORSTRING is the name of the operator to appear in error messages.
3707 This function may need to overload OPERATOR_FNNAME.
3708 Must also handle REFERENCE_TYPEs for C++. */
3710 tree
3711 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3712 tree lookups, tsubst_flags_t complain)
3714 tree orig_expr = expr;
3715 tree rval;
3716 tree overload = NULL_TREE;
3718 if (processing_template_decl)
3720 /* Retain the type if we know the operand is a pointer. */
3721 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3723 if (expr == current_class_ptr
3724 || (TREE_CODE (expr) == NOP_EXPR
3725 && TREE_OPERAND (expr, 0) == current_class_ptr
3726 && (same_type_ignoring_top_level_qualifiers_p
3727 (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3728 return current_class_ref;
3729 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3731 if (type_dependent_expression_p (expr))
3733 expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3734 TREE_TYPE (expr)
3735 = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3736 return expr;
3738 expr = build_non_dependent_expr (expr);
3741 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3742 NULL_TREE, NULL_TREE, lookups,
3743 &overload, complain);
3744 if (!rval)
3745 rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3747 if (processing_template_decl && rval != error_mark_node)
3749 if (overload != NULL_TREE)
3750 return (build_min_non_dep_op_overload
3751 (INDIRECT_REF, rval, overload, orig_expr));
3753 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3755 else
3756 return rval;
3759 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3760 types or expressions. */
3762 static bool
3763 cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3765 if (processing_template_decl)
3767 tree e = expr;
3768 STRIP_NOPS (e);
3769 if (dependent_type_p (type) || type_dependent_expression_p (e))
3770 return false;
3772 return strict_aliasing_warning (loc, type, expr);
3775 /* The implementation of the above, and of indirection implied by other
3776 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3778 static tree
3779 cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3780 tsubst_flags_t complain, bool do_fold)
3782 tree pointer, type;
3784 /* RO_NULL should only be used with the folding entry points below, not
3785 cp_build_indirect_ref. */
3786 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3788 if (ptr == current_class_ptr
3789 || (TREE_CODE (ptr) == NOP_EXPR
3790 && TREE_OPERAND (ptr, 0) == current_class_ptr
3791 && (same_type_ignoring_top_level_qualifiers_p
3792 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3793 return current_class_ref;
3795 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3796 ? ptr : decay_conversion (ptr, complain));
3797 if (pointer == error_mark_node)
3798 return error_mark_node;
3800 type = TREE_TYPE (pointer);
3802 if (INDIRECT_TYPE_P (type))
3804 /* [expr.unary.op]
3806 If the type of the expression is "pointer to T," the type
3807 of the result is "T." */
3808 tree t = TREE_TYPE (type);
3810 if ((CONVERT_EXPR_P (ptr)
3811 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3812 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3814 /* If a warning is issued, mark it to avoid duplicates from
3815 the backend. This only needs to be done at
3816 warn_strict_aliasing > 2. */
3817 if (warn_strict_aliasing > 2
3818 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3819 type, TREE_OPERAND (ptr, 0)))
3820 suppress_warning (ptr, OPT_Wstrict_aliasing);
3823 if (VOID_TYPE_P (t))
3825 /* A pointer to incomplete type (other than cv void) can be
3826 dereferenced [expr.unary.op]/1 */
3827 if (complain & tf_error)
3828 error_at (loc, "%qT is not a pointer-to-object type", type);
3829 return error_mark_node;
3831 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3832 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3833 /* The POINTER was something like `&x'. We simplify `*&x' to
3834 `x'. */
3835 return TREE_OPERAND (pointer, 0);
3836 else
3838 tree ref = build1 (INDIRECT_REF, t, pointer);
3840 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3841 so that we get the proper error message if the result is used
3842 to assign to. Also, &* is supposed to be a no-op. */
3843 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3844 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3845 TREE_SIDE_EFFECTS (ref)
3846 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3847 return ref;
3850 else if (!(complain & tf_error))
3851 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3853 /* `pointer' won't be an error_mark_node if we were given a
3854 pointer to member, so it's cool to check for this here. */
3855 else if (TYPE_PTRMEM_P (type))
3856 switch (errorstring)
3858 case RO_ARRAY_INDEXING:
3859 error_at (loc,
3860 "invalid use of array indexing on pointer to member");
3861 break;
3862 case RO_UNARY_STAR:
3863 error_at (loc, "invalid use of unary %<*%> on pointer to member");
3864 break;
3865 case RO_IMPLICIT_CONVERSION:
3866 error_at (loc, "invalid use of implicit conversion on pointer "
3867 "to member");
3868 break;
3869 case RO_ARROW_STAR:
3870 error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3871 "class, but is a pointer to member of type %qT", type);
3872 break;
3873 default:
3874 gcc_unreachable ();
3876 else if (pointer != error_mark_node)
3877 invalid_indirection_error (loc, type, errorstring);
3879 return error_mark_node;
3882 /* Entry point used by c-common, which expects folding. */
3884 tree
3885 build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3887 return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3888 tf_warning_or_error, true);
3891 /* Entry point used by internal indirection needs that don't correspond to any
3892 syntactic construct. */
3894 tree
3895 cp_build_fold_indirect_ref (tree pointer)
3897 return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3898 tf_warning_or_error, true);
3901 /* Entry point used by indirection needs that correspond to some syntactic
3902 construct. */
3904 tree
3905 cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3906 tsubst_flags_t complain)
3908 return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3911 /* This handles expressions of the form "a[i]", which denotes
3912 an array reference.
3914 This is logically equivalent in C to *(a+i), but we may do it differently.
3915 If A is a variable or a member, we generate a primitive ARRAY_REF.
3916 This avoids forcing the array out of registers, and can work on
3917 arrays that are not lvalues (for example, members of structures returned
3918 by functions).
3920 If INDEX is of some user-defined type, it must be converted to
3921 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3922 will inherit the type of the array, which will be some pointer type.
3924 LOC is the location to use in building the array reference. */
3926 tree
3927 cp_build_array_ref (location_t loc, tree array, tree idx,
3928 tsubst_flags_t complain)
3930 tree ret;
3932 if (idx == 0)
3934 if (complain & tf_error)
3935 error_at (loc, "subscript missing in array reference");
3936 return error_mark_node;
3939 if (TREE_TYPE (array) == error_mark_node
3940 || TREE_TYPE (idx) == error_mark_node)
3941 return error_mark_node;
3943 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3944 inside it. */
3945 switch (TREE_CODE (array))
3947 case COMPOUND_EXPR:
3949 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3950 complain);
3951 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3952 TREE_OPERAND (array, 0), value);
3953 SET_EXPR_LOCATION (ret, loc);
3954 return ret;
3957 case COND_EXPR:
3958 ret = build_conditional_expr
3959 (loc, TREE_OPERAND (array, 0),
3960 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3961 complain),
3962 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3963 complain),
3964 complain);
3965 protected_set_expr_location (ret, loc);
3966 return ret;
3968 default:
3969 break;
3972 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3974 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3976 tree rval, type;
3978 warn_array_subscript_with_type_char (loc, idx);
3980 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3982 if (complain & tf_error)
3983 error_at (loc, "array subscript is not an integer");
3984 return error_mark_node;
3987 /* Apply integral promotions *after* noticing character types.
3988 (It is unclear why we do these promotions -- the standard
3989 does not say that we should. In fact, the natural thing would
3990 seem to be to convert IDX to ptrdiff_t; we're performing
3991 pointer arithmetic.) */
3992 idx = cp_perform_integral_promotions (idx, complain);
3994 idx = maybe_fold_non_dependent_expr (idx, complain);
3996 /* An array that is indexed by a non-constant
3997 cannot be stored in a register; we must be able to do
3998 address arithmetic on its address.
3999 Likewise an array of elements of variable size. */
4000 if (TREE_CODE (idx) != INTEGER_CST
4001 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4002 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4003 != INTEGER_CST)))
4005 if (!cxx_mark_addressable (array, true))
4006 return error_mark_node;
4009 /* An array that is indexed by a constant value which is not within
4010 the array bounds cannot be stored in a register either; because we
4011 would get a crash in store_bit_field/extract_bit_field when trying
4012 to access a non-existent part of the register. */
4013 if (TREE_CODE (idx) == INTEGER_CST
4014 && TYPE_DOMAIN (TREE_TYPE (array))
4015 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4017 if (!cxx_mark_addressable (array))
4018 return error_mark_node;
4021 /* Note in C++ it is valid to subscript a `register' array, since
4022 it is valid to take the address of something with that
4023 storage specification. */
4024 if (extra_warnings)
4026 tree foo = array;
4027 while (TREE_CODE (foo) == COMPONENT_REF)
4028 foo = TREE_OPERAND (foo, 0);
4029 if (VAR_P (foo) && DECL_REGISTER (foo)
4030 && (complain & tf_warning))
4031 warning_at (loc, OPT_Wextra,
4032 "subscripting array declared %<register%>");
4035 type = TREE_TYPE (TREE_TYPE (array));
4036 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4037 /* Array ref is const/volatile if the array elements are
4038 or if the array is.. */
4039 TREE_READONLY (rval)
4040 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4041 TREE_SIDE_EFFECTS (rval)
4042 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4043 TREE_THIS_VOLATILE (rval)
4044 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4045 ret = require_complete_type (rval, complain);
4046 protected_set_expr_location (ret, loc);
4047 if (non_lvalue)
4048 ret = non_lvalue_loc (loc, ret);
4049 return ret;
4053 tree ar = cp_default_conversion (array, complain);
4054 tree ind = cp_default_conversion (idx, complain);
4055 tree first = NULL_TREE;
4057 if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
4058 ar = first = save_expr (ar);
4060 /* Put the integer in IND to simplify error checking. */
4061 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4062 std::swap (ar, ind);
4064 if (ar == error_mark_node || ind == error_mark_node)
4065 return error_mark_node;
4067 if (!TYPE_PTR_P (TREE_TYPE (ar)))
4069 if (complain & tf_error)
4070 error_at (loc, "subscripted value is neither array nor pointer");
4071 return error_mark_node;
4073 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4075 if (complain & tf_error)
4076 error_at (loc, "array subscript is not an integer");
4077 return error_mark_node;
4080 warn_array_subscript_with_type_char (loc, idx);
4082 ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4083 if (first)
4084 ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4085 ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4086 protected_set_expr_location (ret, loc);
4087 if (non_lvalue)
4088 ret = non_lvalue_loc (loc, ret);
4089 return ret;
4093 /* Entry point for Obj-C++. */
4095 tree
4096 build_array_ref (location_t loc, tree array, tree idx)
4098 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4101 /* Resolve a pointer to member function. INSTANCE is the object
4102 instance to use, if the member points to a virtual member.
4104 This used to avoid checking for virtual functions if basetype
4105 has no virtual functions, according to an earlier ANSI draft.
4106 With the final ISO C++ rules, such an optimization is
4107 incorrect: A pointer to a derived member can be static_cast
4108 to pointer-to-base-member, as long as the dynamic object
4109 later has the right member. So now we only do this optimization
4110 when we know the dynamic type of the object. */
4112 tree
4113 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4114 tsubst_flags_t complain)
4116 if (TREE_CODE (function) == OFFSET_REF)
4117 function = TREE_OPERAND (function, 1);
4119 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4121 tree idx, delta, e1, e2, e3, vtbl;
4122 bool nonvirtual;
4123 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4124 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4126 tree instance_ptr = *instance_ptrptr;
4127 tree instance_save_expr = 0;
4128 if (instance_ptr == error_mark_node)
4130 if (TREE_CODE (function) == PTRMEM_CST)
4132 /* Extracting the function address from a pmf is only
4133 allowed with -Wno-pmf-conversions. It only works for
4134 pmf constants. */
4135 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4136 e1 = convert (fntype, e1);
4137 return e1;
4139 else
4141 if (complain & tf_error)
4142 error ("object missing in use of %qE", function);
4143 return error_mark_node;
4147 /* True if we know that the dynamic type of the object doesn't have
4148 virtual functions, so we can assume the PFN field is a pointer. */
4149 nonvirtual = (COMPLETE_TYPE_P (basetype)
4150 && !TYPE_POLYMORPHIC_P (basetype)
4151 && resolves_to_fixed_type_p (instance_ptr, 0));
4153 /* If we don't really have an object (i.e. in an ill-formed
4154 conversion from PMF to pointer), we can't resolve virtual
4155 functions anyway. */
4156 if (!nonvirtual && is_dummy_object (instance_ptr))
4157 nonvirtual = true;
4159 if (TREE_SIDE_EFFECTS (instance_ptr))
4160 instance_ptr = instance_save_expr = save_expr (instance_ptr);
4162 if (TREE_SIDE_EFFECTS (function))
4163 function = save_expr (function);
4165 /* Start by extracting all the information from the PMF itself. */
4166 e3 = pfn_from_ptrmemfunc (function);
4167 delta = delta_from_ptrmemfunc (function);
4168 idx = build1 (NOP_EXPR, vtable_index_type, e3);
4169 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4171 int flag_sanitize_save;
4172 case ptrmemfunc_vbit_in_pfn:
4173 e1 = cp_build_binary_op (input_location,
4174 BIT_AND_EXPR, idx, integer_one_node,
4175 complain);
4176 idx = cp_build_binary_op (input_location,
4177 MINUS_EXPR, idx, integer_one_node,
4178 complain);
4179 if (idx == error_mark_node)
4180 return error_mark_node;
4181 break;
4183 case ptrmemfunc_vbit_in_delta:
4184 e1 = cp_build_binary_op (input_location,
4185 BIT_AND_EXPR, delta, integer_one_node,
4186 complain);
4187 /* Don't instrument the RSHIFT_EXPR we're about to create because
4188 we're going to use DELTA number of times, and that wouldn't play
4189 well with SAVE_EXPRs therein. */
4190 flag_sanitize_save = flag_sanitize;
4191 flag_sanitize = 0;
4192 delta = cp_build_binary_op (input_location,
4193 RSHIFT_EXPR, delta, integer_one_node,
4194 complain);
4195 flag_sanitize = flag_sanitize_save;
4196 if (delta == error_mark_node)
4197 return error_mark_node;
4198 break;
4200 default:
4201 gcc_unreachable ();
4204 if (e1 == error_mark_node)
4205 return error_mark_node;
4207 /* Convert down to the right base before using the instance. A
4208 special case is that in a pointer to member of class C, C may
4209 be incomplete. In that case, the function will of course be
4210 a member of C, and no conversion is required. In fact,
4211 lookup_base will fail in that case, because incomplete
4212 classes do not have BINFOs. */
4213 if (!same_type_ignoring_top_level_qualifiers_p
4214 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4216 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4217 basetype, ba_check, NULL, complain);
4218 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4219 1, complain);
4220 if (instance_ptr == error_mark_node)
4221 return error_mark_node;
4223 /* ...and then the delta in the PMF. */
4224 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4226 /* Hand back the adjusted 'this' argument to our caller. */
4227 *instance_ptrptr = instance_ptr;
4229 if (nonvirtual)
4230 /* Now just return the pointer. */
4231 return e3;
4233 /* Next extract the vtable pointer from the object. */
4234 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4235 instance_ptr);
4236 vtbl = cp_build_fold_indirect_ref (vtbl);
4237 if (vtbl == error_mark_node)
4238 return error_mark_node;
4240 /* Finally, extract the function pointer from the vtable. */
4241 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4242 e2 = cp_build_fold_indirect_ref (e2);
4243 if (e2 == error_mark_node)
4244 return error_mark_node;
4245 TREE_CONSTANT (e2) = 1;
4247 /* When using function descriptors, the address of the
4248 vtable entry is treated as a function pointer. */
4249 if (TARGET_VTABLE_USES_DESCRIPTORS)
4250 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4251 cp_build_addr_expr (e2, complain));
4253 e2 = fold_convert (TREE_TYPE (e3), e2);
4254 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4255 if (e1 == error_mark_node)
4256 return error_mark_node;
4258 /* Make sure this doesn't get evaluated first inside one of the
4259 branches of the COND_EXPR. */
4260 if (instance_save_expr)
4261 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4262 instance_save_expr, e1);
4264 function = e1;
4266 return function;
4269 /* Used by the C-common bits. */
4270 tree
4271 build_function_call (location_t /*loc*/,
4272 tree function, tree params)
4274 return cp_build_function_call (function, params, tf_warning_or_error);
4277 /* Used by the C-common bits. */
4278 tree
4279 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4280 tree function, vec<tree, va_gc> *params,
4281 vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4283 vec<tree, va_gc> *orig_params = params;
4284 tree ret = cp_build_function_call_vec (function, &params,
4285 tf_warning_or_error, orig_function);
4287 /* cp_build_function_call_vec can reallocate PARAMS by adding
4288 default arguments. That should never happen here. Verify
4289 that. */
4290 gcc_assert (params == orig_params);
4292 return ret;
4295 /* Build a function call using a tree list of arguments. */
4297 static tree
4298 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4300 tree ret;
4302 releasing_vec vec;
4303 for (; params != NULL_TREE; params = TREE_CHAIN (params))
4304 vec_safe_push (vec, TREE_VALUE (params));
4305 ret = cp_build_function_call_vec (function, &vec, complain);
4306 return ret;
4309 /* Build a function call using varargs. */
4311 tree
4312 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4314 va_list args;
4315 tree ret, t;
4317 releasing_vec vec;
4318 va_start (args, complain);
4319 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4320 vec_safe_push (vec, t);
4321 va_end (args);
4322 ret = cp_build_function_call_vec (function, &vec, complain);
4323 return ret;
4326 /* Build a function call using a vector of arguments.
4327 If FUNCTION is the result of resolving an overloaded target built-in,
4328 ORIG_FNDECL is the original function decl, otherwise it is null.
4329 PARAMS may be NULL if there are no parameters. This changes the
4330 contents of PARAMS. */
4332 tree
4333 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4334 tsubst_flags_t complain, tree orig_fndecl)
4336 tree fntype, fndecl;
4337 int is_method;
4338 tree original = function;
4339 int nargs;
4340 tree *argarray;
4341 tree parm_types;
4342 vec<tree, va_gc> *allocated = NULL;
4343 tree ret;
4345 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4346 expressions, like those used for ObjC messenger dispatches. */
4347 if (params != NULL && !vec_safe_is_empty (*params))
4348 function = objc_rewrite_function_call (function, (**params)[0]);
4350 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4351 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4352 if (TREE_CODE (function) == NOP_EXPR
4353 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4354 function = TREE_OPERAND (function, 0);
4356 if (TREE_CODE (function) == FUNCTION_DECL)
4358 if (!mark_used (function, complain))
4359 return error_mark_node;
4360 fndecl = function;
4362 /* Convert anything with function type to a pointer-to-function. */
4363 if (DECL_MAIN_P (function))
4365 if (complain & tf_error)
4366 pedwarn (input_location, OPT_Wpedantic,
4367 "ISO C++ forbids calling %<::main%> from within program");
4368 else
4369 return error_mark_node;
4371 function = build_addr_func (function, complain);
4373 else
4375 fndecl = NULL_TREE;
4377 function = build_addr_func (function, complain);
4380 if (function == error_mark_node)
4381 return error_mark_node;
4383 fntype = TREE_TYPE (function);
4385 if (TYPE_PTRMEMFUNC_P (fntype))
4387 if (complain & tf_error)
4388 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4389 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4390 original, original);
4391 return error_mark_node;
4394 is_method = (TYPE_PTR_P (fntype)
4395 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4397 if (!(TYPE_PTRFN_P (fntype)
4398 || is_method
4399 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4401 if (complain & tf_error)
4403 if (!flag_diagnostics_show_caret)
4404 error_at (input_location,
4405 "%qE cannot be used as a function", original);
4406 else if (DECL_P (original))
4407 error_at (input_location,
4408 "%qD cannot be used as a function", original);
4409 else
4410 error_at (input_location,
4411 "expression cannot be used as a function");
4414 return error_mark_node;
4417 /* fntype now gets the type of function pointed to. */
4418 fntype = TREE_TYPE (fntype);
4419 parm_types = TYPE_ARG_TYPES (fntype);
4421 if (params == NULL)
4423 allocated = make_tree_vector ();
4424 params = &allocated;
4427 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4428 complain);
4429 if (nargs < 0)
4430 return error_mark_node;
4432 argarray = (*params)->address ();
4434 /* Check for errors in format strings and inappropriately
4435 null parameters. */
4436 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
4437 nargs, argarray, NULL);
4439 ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4441 if (warned_p)
4443 tree c = extract_call_expr (ret);
4444 if (TREE_CODE (c) == CALL_EXPR)
4445 suppress_warning (c, OPT_Wnonnull);
4448 if (allocated != NULL)
4449 release_tree_vector (allocated);
4451 return ret;
4454 /* Subroutine of convert_arguments.
4455 Print an error message about a wrong number of arguments. */
4457 static void
4458 error_args_num (location_t loc, tree fndecl, bool too_many_p)
4460 if (fndecl)
4462 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4464 if (DECL_NAME (fndecl) == NULL_TREE
4465 || (DECL_NAME (fndecl)
4466 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4467 error_at (loc,
4468 too_many_p
4469 ? G_("too many arguments to constructor %q#D")
4470 : G_("too few arguments to constructor %q#D"),
4471 fndecl);
4472 else
4473 error_at (loc,
4474 too_many_p
4475 ? G_("too many arguments to member function %q#D")
4476 : G_("too few arguments to member function %q#D"),
4477 fndecl);
4479 else
4480 error_at (loc,
4481 too_many_p
4482 ? G_("too many arguments to function %q#D")
4483 : G_("too few arguments to function %q#D"),
4484 fndecl);
4485 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4486 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4488 else
4490 if (c_dialect_objc () && objc_message_selector ())
4491 error_at (loc,
4492 too_many_p
4493 ? G_("too many arguments to method %q#D")
4494 : G_("too few arguments to method %q#D"),
4495 objc_message_selector ());
4496 else
4497 error_at (loc, too_many_p ? G_("too many arguments to function")
4498 : G_("too few arguments to function"));
4502 /* Convert the actual parameter expressions in the list VALUES to the
4503 types in the list TYPELIST. The converted expressions are stored
4504 back in the VALUES vector.
4505 If parmdecls is exhausted, or when an element has NULL as its type,
4506 perform the default conversions.
4508 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4510 This is also where warnings about wrong number of args are generated.
4512 Returns the actual number of arguments processed (which might be less
4513 than the length of the vector), or -1 on error.
4515 In C++, unspecified trailing parameters can be filled in with their
4516 default arguments, if such were specified. Do so here. */
4518 static int
4519 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4520 int flags, tsubst_flags_t complain)
4522 tree typetail;
4523 unsigned int i;
4525 /* Argument passing is always copy-initialization. */
4526 flags |= LOOKUP_ONLYCONVERTING;
4528 for (i = 0, typetail = typelist;
4529 i < vec_safe_length (*values);
4530 i++)
4532 tree type = typetail ? TREE_VALUE (typetail) : 0;
4533 tree val = (**values)[i];
4535 if (val == error_mark_node || type == error_mark_node)
4536 return -1;
4538 if (type == void_type_node)
4540 if (complain & tf_error)
4542 error_args_num (input_location, fndecl, /*too_many_p=*/true);
4543 return i;
4545 else
4546 return -1;
4549 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4550 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4551 if (TREE_CODE (val) == NOP_EXPR
4552 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4553 && (type == 0 || !TYPE_REF_P (type)))
4554 val = TREE_OPERAND (val, 0);
4556 if (type == 0 || !TYPE_REF_P (type))
4558 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4559 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4560 val = decay_conversion (val, complain);
4563 if (val == error_mark_node)
4564 return -1;
4566 if (type != 0)
4568 /* Formal parm type is specified by a function prototype. */
4569 tree parmval;
4571 if (!COMPLETE_TYPE_P (complete_type (type)))
4573 if (complain & tf_error)
4575 location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4576 if (fndecl)
4578 auto_diagnostic_group d;
4579 error_at (loc,
4580 "parameter %P of %qD has incomplete type %qT",
4581 i, fndecl, type);
4582 inform (get_fndecl_argument_location (fndecl, i),
4583 " declared here");
4585 else
4586 error_at (loc, "parameter %P has incomplete type %qT", i,
4587 type);
4589 parmval = error_mark_node;
4591 else
4593 parmval = convert_for_initialization
4594 (NULL_TREE, type, val, flags,
4595 ICR_ARGPASS, fndecl, i, complain);
4596 parmval = convert_for_arg_passing (type, parmval, complain);
4599 if (parmval == error_mark_node)
4600 return -1;
4602 (**values)[i] = parmval;
4604 else
4606 if (fndecl && magic_varargs_p (fndecl))
4607 /* Don't do ellipsis conversion for __built_in_constant_p
4608 as this will result in spurious errors for non-trivial
4609 types. */
4610 val = require_complete_type (val, complain);
4611 else
4612 val = convert_arg_to_ellipsis (val, complain);
4614 (**values)[i] = val;
4617 if (typetail)
4618 typetail = TREE_CHAIN (typetail);
4621 if (typetail != 0 && typetail != void_list_node)
4623 /* See if there are default arguments that can be used. Because
4624 we hold default arguments in the FUNCTION_TYPE (which is so
4625 wrong), we can see default parameters here from deduced
4626 contexts (and via typeof) for indirect function calls.
4627 Fortunately we know whether we have a function decl to
4628 provide default arguments in a language conformant
4629 manner. */
4630 if (fndecl && TREE_PURPOSE (typetail)
4631 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4633 for (; typetail != void_list_node; ++i)
4635 /* After DR777, with explicit template args we can end up with a
4636 default argument followed by no default argument. */
4637 if (!TREE_PURPOSE (typetail))
4638 break;
4639 tree parmval
4640 = convert_default_arg (TREE_VALUE (typetail),
4641 TREE_PURPOSE (typetail),
4642 fndecl, i, complain);
4644 if (parmval == error_mark_node)
4645 return -1;
4647 vec_safe_push (*values, parmval);
4648 typetail = TREE_CHAIN (typetail);
4649 /* ends with `...'. */
4650 if (typetail == NULL_TREE)
4651 break;
4655 if (typetail && typetail != void_list_node)
4657 if (complain & tf_error)
4658 error_args_num (input_location, fndecl, /*too_many_p=*/false);
4659 return -1;
4663 return (int) i;
4666 /* Build a binary-operation expression, after performing default
4667 conversions on the operands. CODE is the kind of expression to
4668 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4669 are the tree codes which correspond to ARG1 and ARG2 when issuing
4670 warnings about possibly misplaced parentheses. They may differ
4671 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4672 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4673 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4674 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4675 ARG2_CODE as ERROR_MARK. */
4677 tree
4678 build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4679 enum tree_code arg1_code, tree arg2,
4680 enum tree_code arg2_code, tree lookups,
4681 tree *overload_p, tsubst_flags_t complain)
4683 tree orig_arg1;
4684 tree orig_arg2;
4685 tree expr;
4686 tree overload = NULL_TREE;
4688 orig_arg1 = arg1;
4689 orig_arg2 = arg2;
4691 if (processing_template_decl)
4693 if (type_dependent_expression_p (arg1)
4694 || type_dependent_expression_p (arg2))
4696 expr = build_min_nt_loc (loc, code, arg1, arg2);
4697 TREE_TYPE (expr)
4698 = build_dependent_operator_type (lookups, code, false);
4699 return expr;
4701 arg1 = build_non_dependent_expr (arg1);
4702 arg2 = build_non_dependent_expr (arg2);
4705 if (code == DOTSTAR_EXPR)
4706 expr = build_m_component_ref (arg1, arg2, complain);
4707 else
4708 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4709 lookups, &overload, complain);
4711 if (overload_p != NULL)
4712 *overload_p = overload;
4714 /* Check for cases such as x+y<<z which users are likely to
4715 misinterpret. But don't warn about obj << x + y, since that is a
4716 common idiom for I/O. */
4717 if (warn_parentheses
4718 && (complain & tf_warning)
4719 && !processing_template_decl
4720 && !error_operand_p (arg1)
4721 && !error_operand_p (arg2)
4722 && (code != LSHIFT_EXPR
4723 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4724 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4725 arg2_code, orig_arg2);
4727 if (processing_template_decl && expr != error_mark_node)
4729 if (overload != NULL_TREE)
4730 return (build_min_non_dep_op_overload
4731 (code, expr, overload, orig_arg1, orig_arg2));
4733 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4736 return expr;
4739 /* Build and return an ARRAY_REF expression. */
4741 tree
4742 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4743 tsubst_flags_t complain)
4745 tree orig_arg1 = arg1;
4746 tree orig_arg2 = arg2;
4747 tree expr;
4748 tree overload = NULL_TREE;
4750 if (processing_template_decl)
4752 if (type_dependent_expression_p (arg1)
4753 || type_dependent_expression_p (arg2))
4754 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4755 NULL_TREE, NULL_TREE);
4756 arg1 = build_non_dependent_expr (arg1);
4757 arg2 = build_non_dependent_expr (arg2);
4760 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4761 NULL_TREE, NULL_TREE, &overload, complain);
4763 if (processing_template_decl && expr != error_mark_node)
4765 if (overload != NULL_TREE)
4766 return (build_min_non_dep_op_overload
4767 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4769 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4770 NULL_TREE, NULL_TREE);
4772 return expr;
4775 /* Return whether OP is an expression of enum type cast to integer
4776 type. In C++ even unsigned enum types are cast to signed integer
4777 types. We do not want to issue warnings about comparisons between
4778 signed and unsigned types when one of the types is an enum type.
4779 Those warnings are always false positives in practice. */
4781 static bool
4782 enum_cast_to_int (tree op)
4784 if (CONVERT_EXPR_P (op)
4785 && TREE_TYPE (op) == integer_type_node
4786 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4787 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4788 return true;
4790 /* The cast may have been pushed into a COND_EXPR. */
4791 if (TREE_CODE (op) == COND_EXPR)
4792 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4793 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4795 return false;
4798 /* For the c-common bits. */
4799 tree
4800 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4801 bool /*convert_p*/)
4803 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4806 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4807 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4809 static tree
4810 build_vec_cmp (tree_code code, tree type,
4811 tree arg0, tree arg1)
4813 tree zero_vec = build_zero_cst (type);
4814 tree minus_one_vec = build_minus_one_cst (type);
4815 tree cmp_type = truth_type_for (type);
4816 tree cmp = build2 (code, cmp_type, arg0, arg1);
4817 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4820 /* Possibly warn about an address never being NULL. */
4822 static void
4823 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4825 /* Prevent warnings issued for macro expansion. */
4826 if (!warn_address
4827 || (complain & tf_warning) == 0
4828 || c_inhibit_evaluation_warnings != 0
4829 || from_macro_expansion_at (location)
4830 || warning_suppressed_p (op, OPT_Waddress))
4831 return;
4833 if (TREE_CODE (op) == NON_DEPENDENT_EXPR)
4834 op = TREE_OPERAND (op, 0);
4836 tree cop = fold_for_warn (op);
4838 if (TREE_CODE (cop) == NON_LVALUE_EXPR)
4839 /* Unwrap the expression for C++ 98. */
4840 cop = TREE_OPERAND (cop, 0);
4842 if (TREE_CODE (cop) == PTRMEM_CST)
4844 /* The address of a nonstatic data member is never null. */
4845 warning_at (location, OPT_Waddress,
4846 "the address %qE will never be NULL",
4847 cop);
4848 return;
4851 if (TREE_CODE (cop) == NOP_EXPR)
4853 /* Allow casts to intptr_t to suppress the warning. */
4854 tree type = TREE_TYPE (cop);
4855 if (TREE_CODE (type) == INTEGER_TYPE)
4856 return;
4858 STRIP_NOPS (cop);
4861 bool warned = false;
4862 if (TREE_CODE (cop) == ADDR_EXPR)
4864 cop = TREE_OPERAND (cop, 0);
4866 /* Set to true in the loop below if OP dereferences its operand.
4867 In such a case the ultimate target need not be a decl for
4868 the null [in]equality test to be necessarily constant. */
4869 bool deref = false;
4871 /* Get the outermost array or object, or member. */
4872 while (handled_component_p (cop))
4874 if (TREE_CODE (cop) == COMPONENT_REF)
4876 /* Get the member (its address is never null). */
4877 cop = TREE_OPERAND (cop, 1);
4878 break;
4881 /* Get the outer array/object to refer to in the warning. */
4882 cop = TREE_OPERAND (cop, 0);
4883 deref = true;
4886 if ((!deref && !decl_with_nonnull_addr_p (cop))
4887 || from_macro_expansion_at (location)
4888 || warning_suppressed_p (cop, OPT_Waddress))
4889 return;
4891 warned = warning_at (location, OPT_Waddress,
4892 "the address of %qD will never be NULL", cop);
4893 op = cop;
4895 else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
4897 /* Adding zero to the null pointer is well-defined in C++. When
4898 the offset is unknown (i.e., not a constant) warn anyway since
4899 it's less likely that the pointer operand is null than not. */
4900 tree off = TREE_OPERAND (cop, 1);
4901 if (!integer_zerop (off)
4902 && !warning_suppressed_p (cop, OPT_Waddress))
4904 tree base = TREE_OPERAND (cop, 0);
4905 STRIP_NOPS (base);
4906 if (TYPE_REF_P (TREE_TYPE (base)))
4907 warning_at (location, OPT_Waddress, "the compiler can assume that "
4908 "the address of %qE will never be NULL", base);
4909 else
4910 warning_at (location, OPT_Waddress, "comparing the result of "
4911 "pointer addition %qE and NULL", cop);
4913 return;
4915 else if (CONVERT_EXPR_P (op)
4916 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4918 STRIP_NOPS (op);
4920 if (TREE_CODE (op) == COMPONENT_REF)
4921 op = TREE_OPERAND (op, 1);
4923 if (DECL_P (op))
4924 warned = warning_at (location, OPT_Waddress,
4925 "the compiler can assume that the address of "
4926 "%qD will never be NULL", op);
4929 if (warned && DECL_P (op))
4930 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
4933 /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
4934 the other operand is of a different enumeration type or a floating-point
4935 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
4936 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
4937 and LOC is the location for the whole binary expression.
4938 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
4940 static void
4941 do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
4942 tree type1)
4944 if (TREE_CODE (type0) == ENUMERAL_TYPE
4945 && TREE_CODE (type1) == ENUMERAL_TYPE
4946 && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
4948 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
4949 Otherwise, warn if -Wenum-conversion is on. */
4950 enum opt_code opt;
4951 if (warn_deprecated_enum_enum_conv)
4952 opt = OPT_Wdeprecated_enum_enum_conversion;
4953 else if (warn_enum_conversion)
4954 opt = OPT_Wenum_conversion;
4955 else
4956 return;
4958 switch (code)
4960 case GT_EXPR:
4961 case LT_EXPR:
4962 case GE_EXPR:
4963 case LE_EXPR:
4964 case EQ_EXPR:
4965 case NE_EXPR:
4966 /* Comparisons are handled by -Wenum-compare. */
4967 return;
4968 case SPACESHIP_EXPR:
4969 /* This is invalid, don't warn. */
4970 return;
4971 case BIT_AND_EXPR:
4972 case BIT_IOR_EXPR:
4973 case BIT_XOR_EXPR:
4974 warning_at (loc, opt, "bitwise operation between different "
4975 "enumeration types %qT and %qT is deprecated",
4976 type0, type1);
4977 return;
4978 default:
4979 warning_at (loc, opt, "arithmetic between different enumeration "
4980 "types %qT and %qT is deprecated", type0, type1);
4981 return;
4984 else if ((TREE_CODE (type0) == ENUMERAL_TYPE
4985 && TREE_CODE (type1) == REAL_TYPE)
4986 || (TREE_CODE (type0) == REAL_TYPE
4987 && TREE_CODE (type1) == ENUMERAL_TYPE))
4989 const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
4990 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
4991 Otherwise, warn if -Wenum-conversion is on. */
4992 enum opt_code opt;
4993 if (warn_deprecated_enum_float_conv)
4994 opt = OPT_Wdeprecated_enum_float_conversion;
4995 else if (warn_enum_conversion)
4996 opt = OPT_Wenum_conversion;
4997 else
4998 return;
5000 switch (code)
5002 case GT_EXPR:
5003 case LT_EXPR:
5004 case GE_EXPR:
5005 case LE_EXPR:
5006 case EQ_EXPR:
5007 case NE_EXPR:
5008 if (enum_first_p)
5009 warning_at (loc, opt, "comparison of enumeration type %qT with "
5010 "floating-point type %qT is deprecated",
5011 type0, type1);
5012 else
5013 warning_at (loc, opt, "comparison of floating-point type %qT "
5014 "with enumeration type %qT is deprecated",
5015 type0, type1);
5016 return;
5017 case SPACESHIP_EXPR:
5018 /* This is invalid, don't warn. */
5019 return;
5020 default:
5021 if (enum_first_p)
5022 warning_at (loc, opt, "arithmetic between enumeration type %qT "
5023 "and floating-point type %qT is deprecated",
5024 type0, type1);
5025 else
5026 warning_at (loc, opt, "arithmetic between floating-point type %qT "
5027 "and enumeration type %qT is deprecated",
5028 type0, type1);
5029 return;
5034 /* Build a binary-operation expression without default conversions.
5035 CODE is the kind of expression to build.
5036 LOCATION is the location_t of the operator in the source code.
5037 This function differs from `build' in several ways:
5038 the data type of the result is computed and recorded in it,
5039 warnings are generated if arg data types are invalid,
5040 special handling for addition and subtraction of pointers is known,
5041 and some optimization is done (operations on narrow ints
5042 are done in the narrower type when that gives the same result).
5043 Constant folding is also done before the result is returned.
5045 Note that the operands will never have enumeral types
5046 because either they have just had the default conversions performed
5047 or they have both just been converted to some other type in which
5048 the arithmetic is to be done.
5050 C++: must do special pointer arithmetic when implementing
5051 multiple inheritance, and deal with pointer to member functions. */
5053 tree
5054 cp_build_binary_op (const op_location_t &location,
5055 enum tree_code code, tree orig_op0, tree orig_op1,
5056 tsubst_flags_t complain)
5058 tree op0, op1;
5059 enum tree_code code0, code1;
5060 tree type0, type1;
5061 const char *invalid_op_diag;
5063 /* Expression code to give to the expression when it is built.
5064 Normally this is CODE, which is what the caller asked for,
5065 but in some special cases we change it. */
5066 enum tree_code resultcode = code;
5068 /* Data type in which the computation is to be performed.
5069 In the simplest cases this is the common type of the arguments. */
5070 tree result_type = NULL_TREE;
5072 /* Nonzero means operands have already been type-converted
5073 in whatever way is necessary.
5074 Zero means they need to be converted to RESULT_TYPE. */
5075 int converted = 0;
5077 /* Nonzero means create the expression with this type, rather than
5078 RESULT_TYPE. */
5079 tree build_type = 0;
5081 /* Nonzero means after finally constructing the expression
5082 convert it to this type. */
5083 tree final_type = 0;
5085 tree result;
5087 /* Nonzero if this is an operation like MIN or MAX which can
5088 safely be computed in short if both args are promoted shorts.
5089 Also implies COMMON.
5090 -1 indicates a bitwise operation; this makes a difference
5091 in the exact conditions for when it is safe to do the operation
5092 in a narrower mode. */
5093 int shorten = 0;
5095 /* Nonzero if this is a comparison operation;
5096 if both args are promoted shorts, compare the original shorts.
5097 Also implies COMMON. */
5098 int short_compare = 0;
5100 /* Nonzero if this is a right-shift operation, which can be computed on the
5101 original short and then promoted if the operand is a promoted short. */
5102 int short_shift = 0;
5104 /* Nonzero means set RESULT_TYPE to the common type of the args. */
5105 int common = 0;
5107 /* True if both operands have arithmetic type. */
5108 bool arithmetic_types_p;
5110 /* Remember whether we're doing / or %. */
5111 bool doing_div_or_mod = false;
5113 /* Remember whether we're doing << or >>. */
5114 bool doing_shift = false;
5116 /* Tree holding instrumentation expression. */
5117 tree instrument_expr = NULL_TREE;
5119 /* Apply default conversions. */
5120 op0 = resolve_nondeduced_context (orig_op0, complain);
5121 op1 = resolve_nondeduced_context (orig_op1, complain);
5123 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5124 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5125 || code == TRUTH_XOR_EXPR)
5127 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5128 op0 = decay_conversion (op0, complain);
5129 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5130 op1 = decay_conversion (op1, complain);
5132 else
5134 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5135 op0 = cp_default_conversion (op0, complain);
5136 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5137 op1 = cp_default_conversion (op1, complain);
5140 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5141 STRIP_TYPE_NOPS (op0);
5142 STRIP_TYPE_NOPS (op1);
5144 /* DTRT if one side is an overloaded function, but complain about it. */
5145 if (type_unknown_p (op0))
5147 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5148 if (t != error_mark_node)
5150 if (complain & tf_error)
5151 permerror (location,
5152 "assuming cast to type %qT from overloaded function",
5153 TREE_TYPE (t));
5154 op0 = t;
5157 if (type_unknown_p (op1))
5159 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5160 if (t != error_mark_node)
5162 if (complain & tf_error)
5163 permerror (location,
5164 "assuming cast to type %qT from overloaded function",
5165 TREE_TYPE (t));
5166 op1 = t;
5170 type0 = TREE_TYPE (op0);
5171 type1 = TREE_TYPE (op1);
5173 /* The expression codes of the data types of the arguments tell us
5174 whether the arguments are integers, floating, pointers, etc. */
5175 code0 = TREE_CODE (type0);
5176 code1 = TREE_CODE (type1);
5178 /* If an error was already reported for one of the arguments,
5179 avoid reporting another error. */
5180 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5181 return error_mark_node;
5183 if ((invalid_op_diag
5184 = targetm.invalid_binary_op (code, type0, type1)))
5186 if (complain & tf_error)
5188 if (code0 == REAL_TYPE
5189 && code1 == REAL_TYPE
5190 && (extended_float_type_p (type0)
5191 || extended_float_type_p (type1))
5192 && cp_compare_floating_point_conversion_ranks (type0,
5193 type1) == 3)
5195 rich_location richloc (line_table, location);
5196 binary_op_error (&richloc, code, type0, type1);
5198 else
5199 error (invalid_op_diag);
5201 return error_mark_node;
5204 /* Issue warnings about peculiar, but valid, uses of NULL. */
5205 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5206 /* It's reasonable to use pointer values as operands of &&
5207 and ||, so NULL is no exception. */
5208 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5209 && ( /* Both are NULL (or 0) and the operation was not a
5210 comparison or a pointer subtraction. */
5211 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5212 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5213 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5214 || (!null_ptr_cst_p (orig_op0)
5215 && !TYPE_PTR_OR_PTRMEM_P (type0))
5216 || (!null_ptr_cst_p (orig_op1)
5217 && !TYPE_PTR_OR_PTRMEM_P (type1)))
5218 && (complain & tf_warning))
5220 location_t loc =
5221 expansion_point_location_if_in_system_header (input_location);
5223 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5226 /* In case when one of the operands of the binary operation is
5227 a vector and another is a scalar -- convert scalar to vector. */
5228 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5229 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5231 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
5232 complain & tf_error);
5234 switch (convert_flag)
5236 case stv_error:
5237 return error_mark_node;
5238 case stv_firstarg:
5240 op0 = convert (TREE_TYPE (type1), op0);
5241 op0 = save_expr (op0);
5242 op0 = build_vector_from_val (type1, op0);
5243 type0 = TREE_TYPE (op0);
5244 code0 = TREE_CODE (type0);
5245 converted = 1;
5246 break;
5248 case stv_secondarg:
5250 op1 = convert (TREE_TYPE (type0), op1);
5251 op1 = save_expr (op1);
5252 op1 = build_vector_from_val (type0, op1);
5253 type1 = TREE_TYPE (op1);
5254 code1 = TREE_CODE (type1);
5255 converted = 1;
5256 break;
5258 default:
5259 break;
5263 switch (code)
5265 case MINUS_EXPR:
5266 /* Subtraction of two similar pointers.
5267 We must subtract them as integers, then divide by object size. */
5268 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5269 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5270 TREE_TYPE (type1)))
5272 result = pointer_diff (location, op0, op1,
5273 common_pointer_type (type0, type1), complain,
5274 &instrument_expr);
5275 if (instrument_expr != NULL)
5276 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5277 instrument_expr, result);
5279 return result;
5281 /* In all other cases except pointer - int, the usual arithmetic
5282 rules apply. */
5283 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5285 common = 1;
5286 break;
5288 /* The pointer - int case is just like pointer + int; fall
5289 through. */
5290 gcc_fallthrough ();
5291 case PLUS_EXPR:
5292 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5293 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5295 tree ptr_operand;
5296 tree int_operand;
5297 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5298 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5299 if (processing_template_decl)
5301 result_type = TREE_TYPE (ptr_operand);
5302 break;
5304 return cp_pointer_int_sum (location, code,
5305 ptr_operand,
5306 int_operand,
5307 complain);
5309 common = 1;
5310 break;
5312 case MULT_EXPR:
5313 common = 1;
5314 break;
5316 case TRUNC_DIV_EXPR:
5317 case CEIL_DIV_EXPR:
5318 case FLOOR_DIV_EXPR:
5319 case ROUND_DIV_EXPR:
5320 case EXACT_DIV_EXPR:
5321 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5323 tree type0 = TREE_OPERAND (op0, 0);
5324 tree type1 = TREE_OPERAND (op1, 0);
5325 tree first_arg = tree_strip_any_location_wrapper (type0);
5326 if (!TYPE_P (type0))
5327 type0 = TREE_TYPE (type0);
5328 if (!TYPE_P (type1))
5329 type1 = TREE_TYPE (type1);
5330 if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1))
5332 if (!(TREE_CODE (first_arg) == PARM_DECL
5333 && DECL_ARRAY_PARAMETER_P (first_arg)
5334 && warn_sizeof_array_argument)
5335 && (complain & tf_warning))
5337 auto_diagnostic_group d;
5338 if (warning_at (location, OPT_Wsizeof_pointer_div,
5339 "division %<sizeof (%T) / sizeof (%T)%> does "
5340 "not compute the number of array elements",
5341 type0, type1))
5342 if (DECL_P (first_arg))
5343 inform (DECL_SOURCE_LOCATION (first_arg),
5344 "first %<sizeof%> operand was declared here");
5347 else if (TREE_CODE (type0) == ARRAY_TYPE
5348 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5349 /* Set by finish_parenthesized_expr. */
5350 && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5351 && (complain & tf_warning))
5352 maybe_warn_sizeof_array_div (location, first_arg, type0,
5353 op1, non_reference (type1));
5356 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5357 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5358 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5359 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5361 enum tree_code tcode0 = code0, tcode1 = code1;
5362 doing_div_or_mod = true;
5363 warn_for_div_by_zero (location, fold_for_warn (op1));
5365 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5366 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5367 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5368 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5370 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5371 resultcode = RDIV_EXPR;
5372 else
5374 /* When dividing two signed integers, we have to promote to int.
5375 unless we divide by a constant != -1. Note that default
5376 conversion will have been performed on the operands at this
5377 point, so we have to dig out the original type to find out if
5378 it was unsigned. */
5379 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5380 shorten = ((TREE_CODE (op0) == NOP_EXPR
5381 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
5382 || (TREE_CODE (stripped_op1) == INTEGER_CST
5383 && ! integer_all_onesp (stripped_op1)));
5386 common = 1;
5388 break;
5390 case BIT_AND_EXPR:
5391 case BIT_IOR_EXPR:
5392 case BIT_XOR_EXPR:
5393 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5394 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5395 && !VECTOR_FLOAT_TYPE_P (type0)
5396 && !VECTOR_FLOAT_TYPE_P (type1)))
5397 shorten = -1;
5398 break;
5400 case TRUNC_MOD_EXPR:
5401 case FLOOR_MOD_EXPR:
5402 doing_div_or_mod = true;
5403 warn_for_div_by_zero (location, fold_for_warn (op1));
5405 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5406 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5407 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5408 common = 1;
5409 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5411 /* Although it would be tempting to shorten always here, that loses
5412 on some targets, since the modulo instruction is undefined if the
5413 quotient can't be represented in the computation mode. We shorten
5414 only if unsigned or if dividing by something we know != -1. */
5415 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5416 shorten = ((TREE_CODE (op0) == NOP_EXPR
5417 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
5418 || (TREE_CODE (stripped_op1) == INTEGER_CST
5419 && ! integer_all_onesp (stripped_op1)));
5420 common = 1;
5422 break;
5424 case TRUTH_ANDIF_EXPR:
5425 case TRUTH_ORIF_EXPR:
5426 case TRUTH_AND_EXPR:
5427 case TRUTH_OR_EXPR:
5428 if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5430 if (!COMPARISON_CLASS_P (op1))
5431 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5432 build_zero_cst (type1), complain);
5433 if (code == TRUTH_ANDIF_EXPR)
5435 tree z = build_zero_cst (TREE_TYPE (op1));
5436 return build_conditional_expr (location, op0, op1, z, complain);
5438 else if (code == TRUTH_ORIF_EXPR)
5440 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5441 return build_conditional_expr (location, op0, m1, op1, complain);
5443 else
5444 gcc_unreachable ();
5446 if (gnu_vector_type_p (type0)
5447 && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5449 if (!COMPARISON_CLASS_P (op0))
5450 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5451 build_zero_cst (type0), complain);
5452 if (!VECTOR_TYPE_P (type1))
5454 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5455 tree z = build_zero_cst (TREE_TYPE (op0));
5456 op1 = build_conditional_expr (location, op1, m1, z, complain);
5458 else if (!COMPARISON_CLASS_P (op1))
5459 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5460 build_zero_cst (type1), complain);
5462 if (code == TRUTH_ANDIF_EXPR)
5463 code = BIT_AND_EXPR;
5464 else if (code == TRUTH_ORIF_EXPR)
5465 code = BIT_IOR_EXPR;
5466 else
5467 gcc_unreachable ();
5469 return cp_build_binary_op (location, code, op0, op1, complain);
5472 result_type = boolean_type_node;
5473 break;
5475 /* Shift operations: result has same type as first operand;
5476 always convert second operand to int.
5477 Also set SHORT_SHIFT if shifting rightward. */
5479 case RSHIFT_EXPR:
5480 if (gnu_vector_type_p (type0)
5481 && code1 == INTEGER_TYPE
5482 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5484 result_type = type0;
5485 converted = 1;
5487 else if (gnu_vector_type_p (type0)
5488 && gnu_vector_type_p (type1)
5489 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5490 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5491 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5492 TYPE_VECTOR_SUBPARTS (type1)))
5494 result_type = type0;
5495 converted = 1;
5497 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5499 tree const_op1 = fold_for_warn (op1);
5500 if (TREE_CODE (const_op1) != INTEGER_CST)
5501 const_op1 = op1;
5502 result_type = type0;
5503 doing_shift = true;
5504 if (TREE_CODE (const_op1) == INTEGER_CST)
5506 if (tree_int_cst_lt (const_op1, integer_zero_node))
5508 if ((complain & tf_warning)
5509 && c_inhibit_evaluation_warnings == 0)
5510 warning_at (location, OPT_Wshift_count_negative,
5511 "right shift count is negative");
5513 else
5515 if (!integer_zerop (const_op1))
5516 short_shift = 1;
5518 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5519 && (complain & tf_warning)
5520 && c_inhibit_evaluation_warnings == 0)
5521 warning_at (location, OPT_Wshift_count_overflow,
5522 "right shift count >= width of type");
5525 /* Avoid converting op1 to result_type later. */
5526 converted = 1;
5528 break;
5530 case LSHIFT_EXPR:
5531 if (gnu_vector_type_p (type0)
5532 && code1 == INTEGER_TYPE
5533 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5535 result_type = type0;
5536 converted = 1;
5538 else if (gnu_vector_type_p (type0)
5539 && gnu_vector_type_p (type1)
5540 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5541 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5542 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5543 TYPE_VECTOR_SUBPARTS (type1)))
5545 result_type = type0;
5546 converted = 1;
5548 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5550 tree const_op0 = fold_for_warn (op0);
5551 if (TREE_CODE (const_op0) != INTEGER_CST)
5552 const_op0 = op0;
5553 tree const_op1 = fold_for_warn (op1);
5554 if (TREE_CODE (const_op1) != INTEGER_CST)
5555 const_op1 = op1;
5556 result_type = type0;
5557 doing_shift = true;
5558 if (TREE_CODE (const_op0) == INTEGER_CST
5559 && tree_int_cst_sgn (const_op0) < 0
5560 && !TYPE_OVERFLOW_WRAPS (type0)
5561 && (complain & tf_warning)
5562 && c_inhibit_evaluation_warnings == 0)
5563 warning_at (location, OPT_Wshift_negative_value,
5564 "left shift of negative value");
5565 if (TREE_CODE (const_op1) == INTEGER_CST)
5567 if (tree_int_cst_lt (const_op1, integer_zero_node))
5569 if ((complain & tf_warning)
5570 && c_inhibit_evaluation_warnings == 0)
5571 warning_at (location, OPT_Wshift_count_negative,
5572 "left shift count is negative");
5574 else if (compare_tree_int (const_op1,
5575 TYPE_PRECISION (type0)) >= 0)
5577 if ((complain & tf_warning)
5578 && c_inhibit_evaluation_warnings == 0)
5579 warning_at (location, OPT_Wshift_count_overflow,
5580 "left shift count >= width of type");
5582 else if (TREE_CODE (const_op0) == INTEGER_CST
5583 && (complain & tf_warning))
5584 maybe_warn_shift_overflow (location, const_op0, const_op1);
5586 /* Avoid converting op1 to result_type later. */
5587 converted = 1;
5589 break;
5591 case EQ_EXPR:
5592 case NE_EXPR:
5593 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5594 goto vector_compare;
5595 if ((complain & tf_warning)
5596 && c_inhibit_evaluation_warnings == 0
5597 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5598 warning_at (location, OPT_Wfloat_equal,
5599 "comparing floating-point with %<==%> "
5600 "or %<!=%> is unsafe");
5601 if (complain & tf_warning)
5603 tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5604 tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5605 if ((TREE_CODE (stripped_orig_op0) == STRING_CST
5606 && !integer_zerop (cp_fully_fold (op1)))
5607 || (TREE_CODE (stripped_orig_op1) == STRING_CST
5608 && !integer_zerop (cp_fully_fold (op0))))
5609 warning_at (location, OPT_Waddress,
5610 "comparison with string literal results in "
5611 "unspecified behavior");
5612 else if (warn_array_compare
5613 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5614 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5615 do_warn_array_compare (location, code, stripped_orig_op0,
5616 stripped_orig_op1);
5619 build_type = boolean_type_node;
5620 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5621 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5622 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5623 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5624 short_compare = 1;
5625 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5626 && null_ptr_cst_p (orig_op1))
5627 /* Handle, eg, (void*)0 (c++/43906), and more. */
5628 || (code0 == POINTER_TYPE
5629 && TYPE_PTR_P (type1) && integer_zerop (op1)))
5631 if (TYPE_PTR_P (type1))
5632 result_type = composite_pointer_type (location,
5633 type0, type1, op0, op1,
5634 CPO_COMPARISON, complain);
5635 else
5636 result_type = type0;
5638 if (char_type_p (TREE_TYPE (orig_op1)))
5640 auto_diagnostic_group d;
5641 if (warning_at (location, OPT_Wpointer_compare,
5642 "comparison between pointer and zero character "
5643 "constant"))
5644 inform (location,
5645 "did you mean to dereference the pointer?");
5647 warn_for_null_address (location, op0, complain);
5649 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5650 && null_ptr_cst_p (orig_op0))
5651 /* Handle, eg, (void*)0 (c++/43906), and more. */
5652 || (code1 == POINTER_TYPE
5653 && TYPE_PTR_P (type0) && integer_zerop (op0)))
5655 if (TYPE_PTR_P (type0))
5656 result_type = composite_pointer_type (location,
5657 type0, type1, op0, op1,
5658 CPO_COMPARISON, complain);
5659 else
5660 result_type = type1;
5662 if (char_type_p (TREE_TYPE (orig_op0)))
5664 auto_diagnostic_group d;
5665 if (warning_at (location, OPT_Wpointer_compare,
5666 "comparison between pointer and zero character "
5667 "constant"))
5668 inform (location,
5669 "did you mean to dereference the pointer?");
5671 warn_for_null_address (location, op1, complain);
5673 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5674 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5675 result_type = composite_pointer_type (location,
5676 type0, type1, op0, op1,
5677 CPO_COMPARISON, complain);
5678 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5679 /* One of the operands must be of nullptr_t type. */
5680 result_type = TREE_TYPE (nullptr_node);
5681 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5683 result_type = type0;
5684 if (complain & tf_error)
5685 permerror (location, "ISO C++ forbids comparison between "
5686 "pointer and integer");
5687 else
5688 return error_mark_node;
5690 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5692 result_type = type1;
5693 if (complain & tf_error)
5694 permerror (location, "ISO C++ forbids comparison between "
5695 "pointer and integer");
5696 else
5697 return error_mark_node;
5699 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5701 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5702 == ptrmemfunc_vbit_in_delta)
5704 tree pfn0, delta0, e1, e2;
5706 if (TREE_SIDE_EFFECTS (op0))
5707 op0 = cp_save_expr (op0);
5709 pfn0 = pfn_from_ptrmemfunc (op0);
5710 delta0 = delta_from_ptrmemfunc (op0);
5711 e1 = cp_build_binary_op (location,
5712 EQ_EXPR,
5713 pfn0,
5714 build_zero_cst (TREE_TYPE (pfn0)),
5715 complain);
5716 e2 = cp_build_binary_op (location,
5717 BIT_AND_EXPR,
5718 delta0,
5719 integer_one_node,
5720 complain);
5722 if (complain & tf_warning)
5723 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5725 e2 = cp_build_binary_op (location,
5726 EQ_EXPR, e2, integer_zero_node,
5727 complain);
5728 op0 = cp_build_binary_op (location,
5729 TRUTH_ANDIF_EXPR, e1, e2,
5730 complain);
5731 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5733 else
5735 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5736 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5738 result_type = TREE_TYPE (op0);
5740 warn_for_null_address (location, orig_op0, complain);
5742 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5743 return cp_build_binary_op (location, code, op1, op0, complain);
5744 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5746 tree type;
5747 /* E will be the final comparison. */
5748 tree e;
5749 /* E1 and E2 are for scratch. */
5750 tree e1;
5751 tree e2;
5752 tree pfn0;
5753 tree pfn1;
5754 tree delta0;
5755 tree delta1;
5757 type = composite_pointer_type (location, type0, type1, op0, op1,
5758 CPO_COMPARISON, complain);
5760 if (!same_type_p (TREE_TYPE (op0), type))
5761 op0 = cp_convert_and_check (type, op0, complain);
5762 if (!same_type_p (TREE_TYPE (op1), type))
5763 op1 = cp_convert_and_check (type, op1, complain);
5765 if (op0 == error_mark_node || op1 == error_mark_node)
5766 return error_mark_node;
5768 if (TREE_SIDE_EFFECTS (op0))
5769 op0 = save_expr (op0);
5770 if (TREE_SIDE_EFFECTS (op1))
5771 op1 = save_expr (op1);
5773 pfn0 = pfn_from_ptrmemfunc (op0);
5774 pfn0 = cp_fully_fold (pfn0);
5775 /* Avoid -Waddress warnings (c++/64877). */
5776 if (TREE_CODE (pfn0) == ADDR_EXPR)
5777 suppress_warning (pfn0, OPT_Waddress);
5778 pfn1 = pfn_from_ptrmemfunc (op1);
5779 pfn1 = cp_fully_fold (pfn1);
5780 delta0 = delta_from_ptrmemfunc (op0);
5781 delta1 = delta_from_ptrmemfunc (op1);
5782 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5783 == ptrmemfunc_vbit_in_delta)
5785 /* We generate:
5787 (op0.pfn == op1.pfn
5788 && ((op0.delta == op1.delta)
5789 || (!op0.pfn && op0.delta & 1 == 0
5790 && op1.delta & 1 == 0))
5792 The reason for the `!op0.pfn' bit is that a NULL
5793 pointer-to-member is any member with a zero PFN and
5794 LSB of the DELTA field is 0. */
5796 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
5797 delta0,
5798 integer_one_node,
5799 complain);
5800 e1 = cp_build_binary_op (location,
5801 EQ_EXPR, e1, integer_zero_node,
5802 complain);
5803 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
5804 delta1,
5805 integer_one_node,
5806 complain);
5807 e2 = cp_build_binary_op (location,
5808 EQ_EXPR, e2, integer_zero_node,
5809 complain);
5810 e1 = cp_build_binary_op (location,
5811 TRUTH_ANDIF_EXPR, e2, e1,
5812 complain);
5813 e2 = cp_build_binary_op (location, EQ_EXPR,
5814 pfn0,
5815 build_zero_cst (TREE_TYPE (pfn0)),
5816 complain);
5817 e2 = cp_build_binary_op (location,
5818 TRUTH_ANDIF_EXPR, e2, e1, complain);
5819 e1 = cp_build_binary_op (location,
5820 EQ_EXPR, delta0, delta1, complain);
5821 e1 = cp_build_binary_op (location,
5822 TRUTH_ORIF_EXPR, e1, e2, complain);
5824 else
5826 /* We generate:
5828 (op0.pfn == op1.pfn
5829 && (!op0.pfn || op0.delta == op1.delta))
5831 The reason for the `!op0.pfn' bit is that a NULL
5832 pointer-to-member is any member with a zero PFN; the
5833 DELTA field is unspecified. */
5835 e1 = cp_build_binary_op (location,
5836 EQ_EXPR, delta0, delta1, complain);
5837 e2 = cp_build_binary_op (location,
5838 EQ_EXPR,
5839 pfn0,
5840 build_zero_cst (TREE_TYPE (pfn0)),
5841 complain);
5842 e1 = cp_build_binary_op (location,
5843 TRUTH_ORIF_EXPR, e1, e2, complain);
5845 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
5846 e = cp_build_binary_op (location,
5847 TRUTH_ANDIF_EXPR, e2, e1, complain);
5848 if (code == EQ_EXPR)
5849 return e;
5850 return cp_build_binary_op (location,
5851 EQ_EXPR, e, integer_zero_node, complain);
5853 else
5855 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
5856 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
5857 type1));
5858 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
5859 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
5860 type0));
5863 break;
5865 case MAX_EXPR:
5866 case MIN_EXPR:
5867 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
5868 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
5869 shorten = 1;
5870 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5871 result_type = composite_pointer_type (location,
5872 type0, type1, op0, op1,
5873 CPO_COMPARISON, complain);
5874 break;
5876 case LE_EXPR:
5877 case GE_EXPR:
5878 case LT_EXPR:
5879 case GT_EXPR:
5880 case SPACESHIP_EXPR:
5881 if (TREE_CODE (orig_op0) == STRING_CST
5882 || TREE_CODE (orig_op1) == STRING_CST)
5884 if (complain & tf_warning)
5885 warning_at (location, OPT_Waddress,
5886 "comparison with string literal results "
5887 "in unspecified behavior");
5889 else if (warn_array_compare
5890 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5891 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
5892 && code != SPACESHIP_EXPR
5893 && (complain & tf_warning))
5894 do_warn_array_compare (location, code,
5895 tree_strip_any_location_wrapper (orig_op0),
5896 tree_strip_any_location_wrapper (orig_op1));
5898 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5900 vector_compare:
5901 tree intt;
5902 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5903 TREE_TYPE (type1))
5904 && !vector_types_compatible_elements_p (type0, type1))
5906 if (complain & tf_error)
5908 error_at (location, "comparing vectors with different "
5909 "element types");
5910 inform (location, "operand types are %qT and %qT",
5911 type0, type1);
5913 return error_mark_node;
5916 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5917 TYPE_VECTOR_SUBPARTS (type1)))
5919 if (complain & tf_error)
5921 error_at (location, "comparing vectors with different "
5922 "number of elements");
5923 inform (location, "operand types are %qT and %qT",
5924 type0, type1);
5926 return error_mark_node;
5929 /* It's not precisely specified how the usual arithmetic
5930 conversions apply to the vector types. Here, we use
5931 the unsigned type if one of the operands is signed and
5932 the other one is unsigned. */
5933 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5935 if (!TYPE_UNSIGNED (type0))
5936 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5937 else
5938 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5939 warning_at (location, OPT_Wsign_compare, "comparison between "
5940 "types %qT and %qT", type0, type1);
5943 if (resultcode == SPACESHIP_EXPR)
5945 if (complain & tf_error)
5946 sorry_at (location, "three-way comparison of vectors");
5947 return error_mark_node;
5950 /* Always construct signed integer vector type. */
5951 intt = c_common_type_for_size
5952 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5953 if (!intt)
5955 if (complain & tf_error)
5956 error_at (location, "could not find an integer type "
5957 "of the same size as %qT", TREE_TYPE (type0));
5958 return error_mark_node;
5960 result_type = build_opaque_vector_type (intt,
5961 TYPE_VECTOR_SUBPARTS (type0));
5962 return build_vec_cmp (resultcode, result_type, op0, op1);
5964 build_type = boolean_type_node;
5965 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5966 || code0 == ENUMERAL_TYPE)
5967 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5968 || code1 == ENUMERAL_TYPE))
5969 short_compare = 1;
5970 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5971 result_type = composite_pointer_type (location,
5972 type0, type1, op0, op1,
5973 CPO_COMPARISON, complain);
5974 else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5975 || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5976 || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
5978 /* Core Issue 1512 made this ill-formed. */
5979 if (complain & tf_error)
5980 error_at (location, "ordered comparison of pointer with "
5981 "integer zero (%qT and %qT)", type0, type1);
5982 return error_mark_node;
5984 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5986 result_type = type0;
5987 if (complain & tf_error)
5988 permerror (location, "ISO C++ forbids comparison between "
5989 "pointer and integer");
5990 else
5991 return error_mark_node;
5993 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5995 result_type = type1;
5996 if (complain & tf_error)
5997 permerror (location, "ISO C++ forbids comparison between "
5998 "pointer and integer");
5999 else
6000 return error_mark_node;
6003 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6004 && !processing_template_decl
6005 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6007 op0 = save_expr (op0);
6008 op1 = save_expr (op1);
6010 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6011 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6014 break;
6016 case UNORDERED_EXPR:
6017 case ORDERED_EXPR:
6018 case UNLT_EXPR:
6019 case UNLE_EXPR:
6020 case UNGT_EXPR:
6021 case UNGE_EXPR:
6022 case UNEQ_EXPR:
6023 build_type = integer_type_node;
6024 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6026 if (complain & tf_error)
6027 error ("unordered comparison on non-floating-point argument");
6028 return error_mark_node;
6030 common = 1;
6031 break;
6033 default:
6034 break;
6037 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6038 || code0 == ENUMERAL_TYPE)
6039 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6040 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6041 arithmetic_types_p = 1;
6042 else
6044 arithmetic_types_p = 0;
6045 /* Vector arithmetic is only allowed when both sides are vectors. */
6046 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6048 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6049 || !vector_types_compatible_elements_p (type0, type1))
6051 if (complain & tf_error)
6053 /* "location" already embeds the locations of the
6054 operands, so we don't need to add them separately
6055 to richloc. */
6056 rich_location richloc (line_table, location);
6057 binary_op_error (&richloc, code, type0, type1);
6059 return error_mark_node;
6061 arithmetic_types_p = 1;
6064 /* Determine the RESULT_TYPE, if it is not already known. */
6065 if (!result_type
6066 && arithmetic_types_p
6067 && (shorten || common || short_compare))
6069 result_type = cp_common_type (type0, type1);
6070 if (result_type == error_mark_node
6071 && code0 == REAL_TYPE
6072 && code1 == REAL_TYPE
6073 && (extended_float_type_p (type0) || extended_float_type_p (type1))
6074 && cp_compare_floating_point_conversion_ranks (type0, type1) == 3)
6076 if (complain & tf_error)
6078 rich_location richloc (line_table, location);
6079 binary_op_error (&richloc, code, type0, type1);
6081 return error_mark_node;
6083 if (complain & tf_warning)
6085 do_warn_double_promotion (result_type, type0, type1,
6086 "implicit conversion from %qH to %qI "
6087 "to match other operand of binary "
6088 "expression",
6089 location);
6090 do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6091 TREE_TYPE (orig_op1));
6095 if (code == SPACESHIP_EXPR)
6097 iloc_sentinel s (location);
6099 tree orig_type0 = TREE_TYPE (orig_op0);
6100 tree_code orig_code0 = TREE_CODE (orig_type0);
6101 tree orig_type1 = TREE_TYPE (orig_op1);
6102 tree_code orig_code1 = TREE_CODE (orig_type1);
6103 if (!result_type)
6104 /* Nope. */;
6105 else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6106 /* "If one of the operands is of type bool and the other is not, the
6107 program is ill-formed." */
6108 result_type = NULL_TREE;
6109 else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6110 && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6111 /* We only do array/function-to-pointer conversion if "at least one of
6112 the operands is of pointer type". */
6113 result_type = NULL_TREE;
6114 else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6115 /* <=> no longer supports equality relations. */
6116 result_type = NULL_TREE;
6117 else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6118 && !(same_type_ignoring_top_level_qualifiers_p
6119 (orig_type0, orig_type1)))
6120 /* "If both operands have arithmetic types, or one operand has integral
6121 type and the other operand has unscoped enumeration type, the usual
6122 arithmetic conversions are applied to the operands." So we don't do
6123 arithmetic conversions if the operands both have enumeral type. */
6124 result_type = NULL_TREE;
6125 else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6126 || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6127 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6128 [where one is of enumeration type and the other is of a different
6129 enumeration type or a floating-point type] are ill-formed. */
6130 result_type = NULL_TREE;
6132 if (result_type)
6134 build_type = spaceship_type (result_type, complain);
6135 if (build_type == error_mark_node)
6136 return error_mark_node;
6139 if (result_type && arithmetic_types_p)
6141 /* If a narrowing conversion is required, other than from an integral
6142 type to a floating point type, the program is ill-formed. */
6143 bool ok = true;
6144 if (TREE_CODE (result_type) == REAL_TYPE
6145 && CP_INTEGRAL_TYPE_P (orig_type0))
6146 /* OK */;
6147 else if (!check_narrowing (result_type, orig_op0, complain))
6148 ok = false;
6149 if (TREE_CODE (result_type) == REAL_TYPE
6150 && CP_INTEGRAL_TYPE_P (orig_type1))
6151 /* OK */;
6152 else if (!check_narrowing (result_type, orig_op1, complain))
6153 ok = false;
6154 if (!ok && !(complain & tf_error))
6155 return error_mark_node;
6159 if (!result_type)
6161 if (complain & tf_error)
6163 binary_op_rich_location richloc (location,
6164 orig_op0, orig_op1, true);
6165 error_at (&richloc,
6166 "invalid operands of types %qT and %qT to binary %qO",
6167 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6169 return error_mark_node;
6172 /* If we're in a template, the only thing we need to know is the
6173 RESULT_TYPE. */
6174 if (processing_template_decl)
6176 /* Since the middle-end checks the type when doing a build2, we
6177 need to build the tree in pieces. This built tree will never
6178 get out of the front-end as we replace it when instantiating
6179 the template. */
6180 tree tmp = build2 (resultcode,
6181 build_type ? build_type : result_type,
6182 NULL_TREE, op1);
6183 TREE_OPERAND (tmp, 0) = op0;
6184 return tmp;
6187 /* Remember the original type; RESULT_TYPE might be changed later on
6188 by shorten_binary_op. */
6189 tree orig_type = result_type;
6191 if (arithmetic_types_p)
6193 bool first_complex = (code0 == COMPLEX_TYPE);
6194 bool second_complex = (code1 == COMPLEX_TYPE);
6195 int none_complex = (!first_complex && !second_complex);
6197 /* Adapted from patch for c/24581. */
6198 if (first_complex != second_complex
6199 && (code == PLUS_EXPR
6200 || code == MINUS_EXPR
6201 || code == MULT_EXPR
6202 || (code == TRUNC_DIV_EXPR && first_complex))
6203 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6204 && flag_signed_zeros)
6206 /* An operation on mixed real/complex operands must be
6207 handled specially, but the language-independent code can
6208 more easily optimize the plain complex arithmetic if
6209 -fno-signed-zeros. */
6210 tree real_type = TREE_TYPE (result_type);
6211 tree real, imag;
6212 if (first_complex)
6214 if (TREE_TYPE (op0) != result_type)
6215 op0 = cp_convert_and_check (result_type, op0, complain);
6216 if (TREE_TYPE (op1) != real_type)
6217 op1 = cp_convert_and_check (real_type, op1, complain);
6219 else
6221 if (TREE_TYPE (op0) != real_type)
6222 op0 = cp_convert_and_check (real_type, op0, complain);
6223 if (TREE_TYPE (op1) != result_type)
6224 op1 = cp_convert_and_check (result_type, op1, complain);
6226 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6227 return error_mark_node;
6228 if (first_complex)
6230 op0 = save_expr (op0);
6231 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6232 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6233 switch (code)
6235 case MULT_EXPR:
6236 case TRUNC_DIV_EXPR:
6237 op1 = save_expr (op1);
6238 imag = build2 (resultcode, real_type, imag, op1);
6239 /* Fall through. */
6240 case PLUS_EXPR:
6241 case MINUS_EXPR:
6242 real = build2 (resultcode, real_type, real, op1);
6243 break;
6244 default:
6245 gcc_unreachable();
6248 else
6250 op1 = save_expr (op1);
6251 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6252 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6253 switch (code)
6255 case MULT_EXPR:
6256 op0 = save_expr (op0);
6257 imag = build2 (resultcode, real_type, op0, imag);
6258 /* Fall through. */
6259 case PLUS_EXPR:
6260 real = build2 (resultcode, real_type, op0, real);
6261 break;
6262 case MINUS_EXPR:
6263 real = build2 (resultcode, real_type, op0, real);
6264 imag = build1 (NEGATE_EXPR, real_type, imag);
6265 break;
6266 default:
6267 gcc_unreachable();
6270 result = build2 (COMPLEX_EXPR, result_type, real, imag);
6271 return result;
6274 /* For certain operations (which identify themselves by shorten != 0)
6275 if both args were extended from the same smaller type,
6276 do the arithmetic in that type and then extend.
6278 shorten !=0 and !=1 indicates a bitwise operation.
6279 For them, this optimization is safe only if
6280 both args are zero-extended or both are sign-extended.
6281 Otherwise, we might change the result.
6282 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6283 but calculated in (unsigned short) it would be (unsigned short)-1. */
6285 if (shorten && none_complex)
6287 final_type = result_type;
6288 result_type = shorten_binary_op (result_type, op0, op1,
6289 shorten == -1);
6292 /* Shifts can be shortened if shifting right. */
6294 if (short_shift)
6296 int unsigned_arg;
6297 tree arg0 = get_narrower (op0, &unsigned_arg);
6298 /* We're not really warning here but when we set short_shift we
6299 used fold_for_warn to fold the operand. */
6300 tree const_op1 = fold_for_warn (op1);
6302 final_type = result_type;
6304 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6305 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6307 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6308 && tree_int_cst_sgn (const_op1) > 0
6309 /* We can shorten only if the shift count is less than the
6310 number of bits in the smaller type size. */
6311 && compare_tree_int (const_op1,
6312 TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6313 /* We cannot drop an unsigned shift after sign-extension. */
6314 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6316 /* Do an unsigned shift if the operand was zero-extended. */
6317 result_type
6318 = c_common_signed_or_unsigned_type (unsigned_arg,
6319 TREE_TYPE (arg0));
6320 /* Convert value-to-be-shifted to that type. */
6321 if (TREE_TYPE (op0) != result_type)
6322 op0 = convert (result_type, op0);
6323 converted = 1;
6327 /* Comparison operations are shortened too but differently.
6328 They identify themselves by setting short_compare = 1. */
6330 if (short_compare)
6332 /* We call shorten_compare only for diagnostics. */
6333 tree xop0 = fold_simple (op0);
6334 tree xop1 = fold_simple (op1);
6335 tree xresult_type = result_type;
6336 enum tree_code xresultcode = resultcode;
6337 shorten_compare (location, &xop0, &xop1, &xresult_type,
6338 &xresultcode);
6341 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6342 && warn_sign_compare
6343 /* Do not warn until the template is instantiated; we cannot
6344 bound the ranges of the arguments until that point. */
6345 && !processing_template_decl
6346 && (complain & tf_warning)
6347 && c_inhibit_evaluation_warnings == 0
6348 /* Even unsigned enum types promote to signed int. We don't
6349 want to issue -Wsign-compare warnings for this case. */
6350 && !enum_cast_to_int (orig_op0)
6351 && !enum_cast_to_int (orig_op1))
6353 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6354 result_type, resultcode);
6358 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6359 Then the expression will be built.
6360 It will be given type FINAL_TYPE if that is nonzero;
6361 otherwise, it will be given type RESULT_TYPE. */
6362 if (! converted)
6364 warning_sentinel w (warn_sign_conversion, short_compare);
6365 if (!same_type_p (TREE_TYPE (op0), result_type))
6366 op0 = cp_convert_and_check (result_type, op0, complain);
6367 if (!same_type_p (TREE_TYPE (op1), result_type))
6368 op1 = cp_convert_and_check (result_type, op1, complain);
6370 if (op0 == error_mark_node || op1 == error_mark_node)
6371 return error_mark_node;
6374 if (build_type == NULL_TREE)
6375 build_type = result_type;
6377 if (doing_shift
6378 && flag_strong_eval_order == 2
6379 && TREE_SIDE_EFFECTS (op1)
6380 && !processing_template_decl)
6382 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6383 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6384 op0 = cp_save_expr (op0);
6385 instrument_expr = op0;
6388 if (sanitize_flags_p ((SANITIZE_SHIFT
6389 | SANITIZE_DIVIDE
6390 | SANITIZE_FLOAT_DIVIDE
6391 | SANITIZE_SI_OVERFLOW))
6392 && current_function_decl != NULL_TREE
6393 && !processing_template_decl
6394 && (doing_div_or_mod || doing_shift))
6396 /* OP0 and/or OP1 might have side-effects. */
6397 op0 = cp_save_expr (op0);
6398 op1 = cp_save_expr (op1);
6399 op0 = fold_non_dependent_expr (op0, complain);
6400 op1 = fold_non_dependent_expr (op1, complain);
6401 tree instrument_expr1 = NULL_TREE;
6402 if (doing_div_or_mod
6403 && sanitize_flags_p (SANITIZE_DIVIDE
6404 | SANITIZE_FLOAT_DIVIDE
6405 | SANITIZE_SI_OVERFLOW))
6407 /* For diagnostics we want to use the promoted types without
6408 shorten_binary_op. So convert the arguments to the
6409 original result_type. */
6410 tree cop0 = op0;
6411 tree cop1 = op1;
6412 if (TREE_TYPE (cop0) != orig_type)
6413 cop0 = cp_convert (orig_type, op0, complain);
6414 if (TREE_TYPE (cop1) != orig_type)
6415 cop1 = cp_convert (orig_type, op1, complain);
6416 instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6418 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6419 instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6420 if (instrument_expr != NULL)
6421 instrument_expr = add_stmt_to_compound (instrument_expr,
6422 instrument_expr1);
6423 else
6424 instrument_expr = instrument_expr1;
6427 result = build2_loc (location, resultcode, build_type, op0, op1);
6428 if (final_type != 0)
6429 result = cp_convert (final_type, result, complain);
6431 if (instrument_expr != NULL)
6432 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6433 instrument_expr, result);
6435 if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6436 result = get_target_expr (result, complain);
6438 if (!c_inhibit_evaluation_warnings)
6440 if (!processing_template_decl)
6442 op0 = cp_fully_fold (op0);
6443 /* Only consider the second argument if the first isn't overflowed. */
6444 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6445 return result;
6446 op1 = cp_fully_fold (op1);
6447 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6448 return result;
6450 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6451 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6452 return result;
6454 tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6455 if (TREE_OVERFLOW_P (result_ovl))
6456 overflow_warning (location, result_ovl);
6459 return result;
6462 /* Build a VEC_PERM_EXPR.
6463 This is a simple wrapper for c_build_vec_perm_expr. */
6464 tree
6465 build_x_vec_perm_expr (location_t loc,
6466 tree arg0, tree arg1, tree arg2,
6467 tsubst_flags_t complain)
6469 tree orig_arg0 = arg0;
6470 tree orig_arg1 = arg1;
6471 tree orig_arg2 = arg2;
6472 if (processing_template_decl)
6474 if (type_dependent_expression_p (arg0)
6475 || type_dependent_expression_p (arg1)
6476 || type_dependent_expression_p (arg2))
6477 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6478 arg0 = build_non_dependent_expr (arg0);
6479 if (arg1)
6480 arg1 = build_non_dependent_expr (arg1);
6481 arg2 = build_non_dependent_expr (arg2);
6483 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6484 if (processing_template_decl && exp != error_mark_node)
6485 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6486 orig_arg1, orig_arg2);
6487 return exp;
6490 /* Build a VEC_PERM_EXPR.
6491 This is a simple wrapper for c_build_shufflevector. */
6492 tree
6493 build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6494 tsubst_flags_t complain)
6496 tree arg0 = (*args)[0];
6497 tree arg1 = (*args)[1];
6498 if (processing_template_decl)
6500 for (unsigned i = 0; i < args->length (); ++i)
6501 if (i <= 1
6502 ? type_dependent_expression_p ((*args)[i])
6503 : instantiation_dependent_expression_p ((*args)[i]))
6505 tree exp = build_min_nt_call_vec (NULL, args);
6506 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6507 return exp;
6509 arg0 = build_non_dependent_expr (arg0);
6510 arg1 = build_non_dependent_expr (arg1);
6511 /* ??? Nothing needed for the index arguments? */
6513 auto_vec<tree, 16> mask;
6514 for (unsigned i = 2; i < args->length (); ++i)
6516 tree idx = fold_non_dependent_expr ((*args)[i], complain);
6517 mask.safe_push (idx);
6519 tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6520 if (processing_template_decl && exp != error_mark_node)
6522 exp = build_min_non_dep_call_vec (exp, NULL, args);
6523 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6525 return exp;
6528 /* Return a tree for the sum or difference (RESULTCODE says which)
6529 of pointer PTROP and integer INTOP. */
6531 static tree
6532 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6533 tree intop, tsubst_flags_t complain)
6535 tree res_type = TREE_TYPE (ptrop);
6537 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6538 in certain circumstance (when it's valid to do so). So we need
6539 to make sure it's complete. We don't need to check here, if we
6540 can actually complete it at all, as those checks will be done in
6541 pointer_int_sum() anyway. */
6542 complete_type (TREE_TYPE (res_type));
6544 return pointer_int_sum (loc, resultcode, ptrop,
6545 intop, complain & tf_warning_or_error);
6548 /* Return a tree for the difference of pointers OP0 and OP1.
6549 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6550 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6552 static tree
6553 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6554 tsubst_flags_t complain, tree *instrument_expr)
6556 tree result, inttype;
6557 tree restype = ptrdiff_type_node;
6558 tree target_type = TREE_TYPE (ptrtype);
6560 if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6561 return error_mark_node;
6563 if (VOID_TYPE_P (target_type))
6565 if (complain & tf_error)
6566 permerror (loc, "ISO C++ forbids using pointer of "
6567 "type %<void *%> in subtraction");
6568 else
6569 return error_mark_node;
6571 if (TREE_CODE (target_type) == FUNCTION_TYPE)
6573 if (complain & tf_error)
6574 permerror (loc, "ISO C++ forbids using pointer to "
6575 "a function in subtraction");
6576 else
6577 return error_mark_node;
6579 if (TREE_CODE (target_type) == METHOD_TYPE)
6581 if (complain & tf_error)
6582 permerror (loc, "ISO C++ forbids using pointer to "
6583 "a method in subtraction");
6584 else
6585 return error_mark_node;
6587 else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6588 TREE_TYPE (TREE_TYPE (op0)),
6589 !(complain & tf_error))
6590 || !verify_type_context (loc, TCTX_POINTER_ARITH,
6591 TREE_TYPE (TREE_TYPE (op1)),
6592 !(complain & tf_error)))
6593 return error_mark_node;
6595 /* Determine integer type result of the subtraction. This will usually
6596 be the same as the result type (ptrdiff_t), but may need to be a wider
6597 type if pointers for the address space are wider than ptrdiff_t. */
6598 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6599 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6600 else
6601 inttype = restype;
6603 if (!processing_template_decl
6604 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6606 op0 = save_expr (op0);
6607 op1 = save_expr (op1);
6609 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6610 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6613 /* First do the subtraction, then build the divide operator
6614 and only convert at the very end.
6615 Do not do default conversions in case restype is a short type. */
6617 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6618 pointers. If some platform cannot provide that, or has a larger
6619 ptrdiff_type to support differences larger than half the address
6620 space, cast the pointers to some larger integer type and do the
6621 computations in that type. */
6622 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6623 op0 = cp_build_binary_op (loc,
6624 MINUS_EXPR,
6625 cp_convert (inttype, op0, complain),
6626 cp_convert (inttype, op1, complain),
6627 complain);
6628 else
6629 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6631 /* This generates an error if op1 is a pointer to an incomplete type. */
6632 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6634 if (complain & tf_error)
6635 error_at (loc, "invalid use of a pointer to an incomplete type in "
6636 "pointer arithmetic");
6637 else
6638 return error_mark_node;
6641 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6643 if (complain & tf_error)
6644 error_at (loc, "arithmetic on pointer to an empty aggregate");
6645 else
6646 return error_mark_node;
6649 op1 = (TYPE_PTROB_P (ptrtype)
6650 ? size_in_bytes_loc (loc, target_type)
6651 : integer_one_node);
6653 /* Do the division. */
6655 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
6656 cp_convert (inttype, op1, complain));
6657 return cp_convert (restype, result, complain);
6660 /* Construct and perhaps optimize a tree representation
6661 for a unary operation. CODE, a tree_code, specifies the operation
6662 and XARG is the operand. */
6664 tree
6665 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
6666 tree lookups, tsubst_flags_t complain)
6668 tree orig_expr = xarg;
6669 tree exp;
6670 int ptrmem = 0;
6671 tree overload = NULL_TREE;
6673 if (processing_template_decl)
6675 if (type_dependent_expression_p (xarg))
6677 tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6678 TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
6679 return e;
6682 xarg = build_non_dependent_expr (xarg);
6685 exp = NULL_TREE;
6687 /* [expr.unary.op] says:
6689 The address of an object of incomplete type can be taken.
6691 (And is just the ordinary address operator, not an overloaded
6692 "operator &".) However, if the type is a template
6693 specialization, we must complete the type at this point so that
6694 an overloaded "operator &" will be available if required. */
6695 if (code == ADDR_EXPR
6696 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6697 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6698 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6699 || (TREE_CODE (xarg) == OFFSET_REF)))
6700 /* Don't look for a function. */;
6701 else
6702 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6703 NULL_TREE, lookups, &overload, complain);
6705 if (!exp && code == ADDR_EXPR)
6707 if (is_overloaded_fn (xarg))
6709 tree fn = get_first_fn (xarg);
6710 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
6712 if (complain & tf_error)
6713 error_at (loc, DECL_CONSTRUCTOR_P (fn)
6714 ? G_("taking address of constructor %qD")
6715 : G_("taking address of destructor %qD"),
6716 fn);
6717 return error_mark_node;
6721 /* A pointer to member-function can be formed only by saying
6722 &X::mf. */
6723 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
6724 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
6726 if (TREE_CODE (xarg) != OFFSET_REF
6727 || !TYPE_P (TREE_OPERAND (xarg, 0)))
6729 if (complain & tf_error)
6731 error_at (loc, "invalid use of %qE to form a "
6732 "pointer-to-member-function", xarg.get_value ());
6733 if (TREE_CODE (xarg) != OFFSET_REF)
6734 inform (loc, " a qualified-id is required");
6736 return error_mark_node;
6738 else
6740 if (complain & tf_error)
6741 error_at (loc, "parentheses around %qE cannot be used to "
6742 "form a pointer-to-member-function",
6743 xarg.get_value ());
6744 else
6745 return error_mark_node;
6746 PTRMEM_OK_P (xarg) = 1;
6750 if (TREE_CODE (xarg) == OFFSET_REF)
6752 ptrmem = PTRMEM_OK_P (xarg);
6754 if (!ptrmem && !flag_ms_extensions
6755 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
6757 /* A single non-static member, make sure we don't allow a
6758 pointer-to-member. */
6759 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
6760 TREE_OPERAND (xarg, 0),
6761 ovl_make (TREE_OPERAND (xarg, 1)));
6762 PTRMEM_OK_P (xarg) = ptrmem;
6766 exp = cp_build_addr_expr_strict (xarg, complain);
6768 if (TREE_CODE (exp) == PTRMEM_CST)
6769 PTRMEM_CST_LOCATION (exp) = loc;
6770 else
6771 protected_set_expr_location (exp, loc);
6774 if (processing_template_decl && exp != error_mark_node)
6776 if (overload != NULL_TREE)
6777 return (build_min_non_dep_op_overload
6778 (code, exp, overload, orig_expr, integer_zero_node));
6780 exp = build_min_non_dep (code, exp, orig_expr,
6781 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
6783 if (TREE_CODE (exp) == ADDR_EXPR)
6784 PTRMEM_OK_P (exp) = ptrmem;
6785 return exp;
6788 /* Construct and perhaps optimize a tree representation
6789 for __builtin_addressof operation. ARG specifies the operand. */
6791 tree
6792 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
6794 tree orig_expr = arg;
6796 if (processing_template_decl)
6798 if (type_dependent_expression_p (arg))
6799 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
6801 arg = build_non_dependent_expr (arg);
6804 tree exp = cp_build_addr_expr_strict (arg, complain);
6806 if (processing_template_decl && exp != error_mark_node)
6807 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
6808 return exp;
6811 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6812 constants, where a null value is represented by an INTEGER_CST of
6813 -1. */
6815 tree
6816 cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
6818 tree type = TREE_TYPE (expr);
6819 location_t loc = cp_expr_loc_or_input_loc (expr);
6820 if (TYPE_PTR_OR_PTRMEM_P (type)
6821 /* Avoid ICE on invalid use of non-static member function. */
6822 || TREE_CODE (expr) == FUNCTION_DECL)
6823 return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
6824 else
6825 return c_common_truthvalue_conversion (loc, expr);
6828 /* Returns EXPR contextually converted to bool. */
6830 tree
6831 contextual_conv_bool (tree expr, tsubst_flags_t complain)
6833 return perform_implicit_conversion_flags (boolean_type_node, expr,
6834 complain, LOOKUP_NORMAL);
6837 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
6838 is a low-level function; most callers should use maybe_convert_cond. */
6840 tree
6841 condition_conversion (tree expr)
6843 tree t = contextual_conv_bool (expr, tf_warning_or_error);
6844 if (!processing_template_decl)
6845 t = fold_build_cleanup_point_expr (boolean_type_node, t);
6846 return t;
6849 /* Returns the address of T. This function will fold away
6850 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
6851 most places should use cp_build_addr_expr instead. */
6853 tree
6854 build_address (tree t)
6856 if (error_operand_p (t) || !cxx_mark_addressable (t))
6857 return error_mark_node;
6858 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
6859 || processing_template_decl);
6860 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
6861 if (TREE_CODE (t) != ADDR_EXPR)
6862 t = rvalue (t);
6863 return t;
6866 /* Return a NOP_EXPR converting EXPR to TYPE. */
6868 tree
6869 build_nop (tree type, tree expr)
6871 if (type == error_mark_node || error_operand_p (expr))
6872 return expr;
6873 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
6876 /* Take the address of ARG, whatever that means under C++ semantics.
6877 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
6878 and class rvalues as well.
6880 Nothing should call this function directly; instead, callers should use
6881 cp_build_addr_expr or cp_build_addr_expr_strict. */
6883 static tree
6884 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
6886 tree argtype;
6887 tree val;
6889 if (!arg || error_operand_p (arg))
6890 return error_mark_node;
6892 arg = mark_lvalue_use (arg);
6893 if (error_operand_p (arg))
6894 return error_mark_node;
6896 argtype = lvalue_type (arg);
6897 location_t loc = cp_expr_loc_or_input_loc (arg);
6899 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
6901 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
6902 && !really_overloaded_fn (arg))
6904 /* They're trying to take the address of a unique non-static
6905 member function. This is ill-formed (except in MS-land),
6906 but let's try to DTRT.
6907 Note: We only handle unique functions here because we don't
6908 want to complain if there's a static overload; non-unique
6909 cases will be handled by instantiate_type. But we need to
6910 handle this case here to allow casts on the resulting PMF.
6911 We could defer this in non-MS mode, but it's easier to give
6912 a useful error here. */
6914 /* Inside constant member functions, the `this' pointer
6915 contains an extra const qualifier. TYPE_MAIN_VARIANT
6916 is used here to remove this const from the diagnostics
6917 and the created OFFSET_REF. */
6918 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
6919 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
6920 if (!mark_used (fn, complain) && !(complain & tf_error))
6921 return error_mark_node;
6923 if (! flag_ms_extensions)
6925 tree name = DECL_NAME (fn);
6926 if (!(complain & tf_error))
6927 return error_mark_node;
6928 else if (current_class_type
6929 && TREE_OPERAND (arg, 0) == current_class_ref)
6930 /* An expression like &memfn. */
6931 permerror (loc,
6932 "ISO C++ forbids taking the address of an unqualified"
6933 " or parenthesized non-static member function to form"
6934 " a pointer to member function. Say %<&%T::%D%>",
6935 base, name);
6936 else
6937 permerror (loc,
6938 "ISO C++ forbids taking the address of a bound member"
6939 " function to form a pointer to member function."
6940 " Say %<&%T::%D%>",
6941 base, name);
6943 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
6946 /* Uninstantiated types are all functions. Taking the
6947 address of a function is a no-op, so just return the
6948 argument. */
6949 if (type_unknown_p (arg))
6950 return build1 (ADDR_EXPR, unknown_type_node, arg);
6952 if (TREE_CODE (arg) == OFFSET_REF)
6953 /* We want a pointer to member; bypass all the code for actually taking
6954 the address of something. */
6955 goto offset_ref;
6957 /* Anything not already handled and not a true memory reference
6958 is an error. */
6959 if (!FUNC_OR_METHOD_TYPE_P (argtype))
6961 cp_lvalue_kind kind = lvalue_kind (arg);
6962 if (kind == clk_none)
6964 if (complain & tf_error)
6965 lvalue_error (loc, lv_addressof);
6966 return error_mark_node;
6968 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
6970 if (!(complain & tf_error))
6971 return error_mark_node;
6972 /* Make this a permerror because we used to accept it. */
6973 permerror (loc, "taking address of rvalue");
6977 if (TYPE_REF_P (argtype))
6979 tree type = build_pointer_type (TREE_TYPE (argtype));
6980 arg = build1 (CONVERT_EXPR, type, arg);
6981 return arg;
6983 else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
6985 /* ARM $3.4 */
6986 /* Apparently a lot of autoconf scripts for C++ packages do this,
6987 so only complain if -Wpedantic. */
6988 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
6989 pedwarn (loc, OPT_Wpedantic,
6990 "ISO C++ forbids taking address of function %<::main%>");
6991 else if (flag_pedantic_errors)
6992 return error_mark_node;
6995 /* Let &* cancel out to simplify resulting code. */
6996 if (INDIRECT_REF_P (arg))
6998 arg = TREE_OPERAND (arg, 0);
6999 if (TYPE_REF_P (TREE_TYPE (arg)))
7001 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7002 arg = build1 (CONVERT_EXPR, type, arg);
7004 else
7005 /* Don't let this be an lvalue. */
7006 arg = rvalue (arg);
7007 return arg;
7010 /* Handle complex lvalues (when permitted)
7011 by reduction to simpler cases. */
7012 val = unary_complex_lvalue (ADDR_EXPR, arg);
7013 if (val != 0)
7014 return val;
7016 switch (TREE_CODE (arg))
7018 CASE_CONVERT:
7019 case FLOAT_EXPR:
7020 case FIX_TRUNC_EXPR:
7021 /* We should have handled this above in the lvalue_kind check. */
7022 gcc_unreachable ();
7023 break;
7025 case BASELINK:
7026 arg = BASELINK_FUNCTIONS (arg);
7027 /* Fall through. */
7029 case OVERLOAD:
7030 arg = OVL_FIRST (arg);
7031 break;
7033 case OFFSET_REF:
7034 offset_ref:
7035 /* Turn a reference to a non-static data member into a
7036 pointer-to-member. */
7038 tree type;
7039 tree t;
7041 gcc_assert (PTRMEM_OK_P (arg));
7043 t = TREE_OPERAND (arg, 1);
7044 if (TYPE_REF_P (TREE_TYPE (t)))
7046 if (complain & tf_error)
7047 error_at (loc,
7048 "cannot create pointer to reference member %qD", t);
7049 return error_mark_node;
7052 /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7053 if (TREE_CODE (t) == FUNCTION_DECL
7054 && !DECL_PURE_VIRTUAL_P (t)
7055 && !mark_used (t, complain) && !(complain & tf_error))
7056 return error_mark_node;
7058 type = build_ptrmem_type (context_for_name_lookup (t),
7059 TREE_TYPE (t));
7060 t = make_ptrmem_cst (type, t);
7061 return t;
7064 default:
7065 break;
7068 if (argtype != error_mark_node)
7069 argtype = build_pointer_type (argtype);
7071 if (bitfield_p (arg))
7073 if (complain & tf_error)
7074 error_at (loc, "attempt to take address of bit-field");
7075 return error_mark_node;
7078 /* In a template, we are processing a non-dependent expression
7079 so we can just form an ADDR_EXPR with the correct type. */
7080 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7082 if (!mark_single_function (arg, complain))
7083 return error_mark_node;
7084 val = build_address (arg);
7085 if (TREE_CODE (arg) == OFFSET_REF)
7086 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7088 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7090 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7092 /* We can only get here with a single static member
7093 function. */
7094 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7095 && DECL_STATIC_FUNCTION_P (fn));
7096 if (!mark_used (fn, complain) && !(complain & tf_error))
7097 return error_mark_node;
7098 val = build_address (fn);
7099 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7100 /* Do not lose object's side effects. */
7101 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7102 TREE_OPERAND (arg, 0), val);
7104 else
7106 tree object = TREE_OPERAND (arg, 0);
7107 tree field = TREE_OPERAND (arg, 1);
7108 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7109 (TREE_TYPE (object), decl_type_context (field)));
7110 val = build_address (arg);
7113 if (TYPE_PTR_P (argtype)
7114 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7116 build_ptrmemfunc_type (argtype);
7117 val = build_ptrmemfunc (argtype, val, 0,
7118 /*c_cast_p=*/false,
7119 complain);
7122 /* For addresses of immediate functions ensure we have EXPR_LOCATION
7123 set for possible later diagnostics. */
7124 if (TREE_CODE (val) == ADDR_EXPR
7125 && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL
7126 && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (val, 0)))
7127 SET_EXPR_LOCATION (val, input_location);
7129 return val;
7132 /* Take the address of ARG if it has one, even if it's an rvalue. */
7134 tree
7135 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7137 return cp_build_addr_expr_1 (arg, 0, complain);
7140 /* Take the address of ARG, but only if it's an lvalue. */
7142 static tree
7143 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7145 return cp_build_addr_expr_1 (arg, 1, complain);
7148 /* C++: Must handle pointers to members.
7150 Perhaps type instantiation should be extended to handle conversion
7151 from aggregates to types we don't yet know we want? (Or are those
7152 cases typically errors which should be reported?)
7154 NOCONVERT suppresses the default promotions (such as from short to int). */
7156 tree
7157 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7158 tsubst_flags_t complain)
7160 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7161 tree arg = xarg;
7162 location_t location = cp_expr_loc_or_input_loc (arg);
7163 tree argtype = 0;
7164 const char *errstring = NULL;
7165 tree val;
7166 const char *invalid_op_diag;
7168 if (!arg || error_operand_p (arg))
7169 return error_mark_node;
7171 arg = resolve_nondeduced_context (arg, complain);
7173 if ((invalid_op_diag
7174 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7175 ? CONVERT_EXPR
7176 : code),
7177 TREE_TYPE (arg))))
7179 if (complain & tf_error)
7180 error (invalid_op_diag);
7181 return error_mark_node;
7184 switch (code)
7186 case UNARY_PLUS_EXPR:
7187 case NEGATE_EXPR:
7189 int flags = WANT_ARITH | WANT_ENUM;
7190 /* Unary plus (but not unary minus) is allowed on pointers. */
7191 if (code == UNARY_PLUS_EXPR)
7192 flags |= WANT_POINTER;
7193 arg = build_expr_type_conversion (flags, arg, true);
7194 if (!arg)
7195 errstring = (code == NEGATE_EXPR
7196 ? _("wrong type argument to unary minus")
7197 : _("wrong type argument to unary plus"));
7198 else
7200 if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7201 arg = cp_perform_integral_promotions (arg, complain);
7203 /* Make sure the result is not an lvalue: a unary plus or minus
7204 expression is always a rvalue. */
7205 arg = rvalue (arg);
7208 break;
7210 case BIT_NOT_EXPR:
7211 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7213 code = CONJ_EXPR;
7214 if (!noconvert)
7216 arg = cp_default_conversion (arg, complain);
7217 if (arg == error_mark_node)
7218 return error_mark_node;
7221 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7222 | WANT_VECTOR_OR_COMPLEX,
7223 arg, true)))
7224 errstring = _("wrong type argument to bit-complement");
7225 else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7227 /* Warn if the expression has boolean value. */
7228 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7229 && (complain & tf_warning)
7230 && warning_at (location, OPT_Wbool_operation,
7231 "%<~%> on an expression of type %<bool%>"))
7232 inform (location, "did you mean to use logical not (%<!%>)?");
7233 arg = cp_perform_integral_promotions (arg, complain);
7235 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7236 arg = mark_rvalue_use (arg);
7237 break;
7239 case ABS_EXPR:
7240 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7241 errstring = _("wrong type argument to abs");
7242 else if (!noconvert)
7244 arg = cp_default_conversion (arg, complain);
7245 if (arg == error_mark_node)
7246 return error_mark_node;
7248 break;
7250 case CONJ_EXPR:
7251 /* Conjugating a real value is a no-op, but allow it anyway. */
7252 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7253 errstring = _("wrong type argument to conjugation");
7254 else if (!noconvert)
7256 arg = cp_default_conversion (arg, complain);
7257 if (arg == error_mark_node)
7258 return error_mark_node;
7260 break;
7262 case TRUTH_NOT_EXPR:
7263 if (gnu_vector_type_p (TREE_TYPE (arg)))
7264 return cp_build_binary_op (input_location, EQ_EXPR, arg,
7265 build_zero_cst (TREE_TYPE (arg)), complain);
7266 arg = perform_implicit_conversion (boolean_type_node, arg,
7267 complain);
7268 val = invert_truthvalue_loc (location, arg);
7269 if (arg != error_mark_node)
7270 return val;
7271 errstring = _("in argument to unary !");
7272 break;
7274 case NOP_EXPR:
7275 break;
7277 case REALPART_EXPR:
7278 case IMAGPART_EXPR:
7279 arg = build_real_imag_expr (input_location, code, arg);
7280 return arg;
7282 case PREINCREMENT_EXPR:
7283 case POSTINCREMENT_EXPR:
7284 case PREDECREMENT_EXPR:
7285 case POSTDECREMENT_EXPR:
7286 /* Handle complex lvalues (when permitted)
7287 by reduction to simpler cases. */
7289 val = unary_complex_lvalue (code, arg);
7290 if (val != 0)
7291 return val;
7293 arg = mark_lvalue_use (arg);
7295 /* Increment or decrement the real part of the value,
7296 and don't change the imaginary part. */
7297 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7299 tree real, imag;
7301 arg = cp_stabilize_reference (arg);
7302 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7303 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7304 real = cp_build_unary_op (code, real, true, complain);
7305 if (real == error_mark_node || imag == error_mark_node)
7306 return error_mark_node;
7307 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
7308 real, imag);
7311 /* Report invalid types. */
7313 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7314 arg, true)))
7316 if (code == PREINCREMENT_EXPR)
7317 errstring = _("no pre-increment operator for type");
7318 else if (code == POSTINCREMENT_EXPR)
7319 errstring = _("no post-increment operator for type");
7320 else if (code == PREDECREMENT_EXPR)
7321 errstring = _("no pre-decrement operator for type");
7322 else
7323 errstring = _("no post-decrement operator for type");
7324 break;
7326 else if (arg == error_mark_node)
7327 return error_mark_node;
7329 /* Report something read-only. */
7331 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7332 || TREE_READONLY (arg))
7334 if (complain & tf_error)
7335 cxx_readonly_error (location, arg,
7336 ((code == PREINCREMENT_EXPR
7337 || code == POSTINCREMENT_EXPR)
7338 ? lv_increment : lv_decrement));
7339 else
7340 return error_mark_node;
7344 tree inc;
7345 tree declared_type = unlowered_expr_type (arg);
7347 argtype = TREE_TYPE (arg);
7349 /* ARM $5.2.5 last annotation says this should be forbidden. */
7350 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7352 if (complain & tf_error)
7353 permerror (location, (code == PREINCREMENT_EXPR
7354 || code == POSTINCREMENT_EXPR)
7355 ? G_("ISO C++ forbids incrementing an enum")
7356 : G_("ISO C++ forbids decrementing an enum"));
7357 else
7358 return error_mark_node;
7361 /* Compute the increment. */
7363 if (TYPE_PTR_P (argtype))
7365 tree type = complete_type (TREE_TYPE (argtype));
7367 if (!COMPLETE_OR_VOID_TYPE_P (type))
7369 if (complain & tf_error)
7370 error_at (location, ((code == PREINCREMENT_EXPR
7371 || code == POSTINCREMENT_EXPR))
7372 ? G_("cannot increment a pointer to incomplete "
7373 "type %qT")
7374 : G_("cannot decrement a pointer to incomplete "
7375 "type %qT"),
7376 TREE_TYPE (argtype));
7377 else
7378 return error_mark_node;
7380 else if (!TYPE_PTROB_P (argtype))
7382 if (complain & tf_error)
7383 pedwarn (location, OPT_Wpointer_arith,
7384 (code == PREINCREMENT_EXPR
7385 || code == POSTINCREMENT_EXPR)
7386 ? G_("ISO C++ forbids incrementing a pointer "
7387 "of type %qT")
7388 : G_("ISO C++ forbids decrementing a pointer "
7389 "of type %qT"),
7390 argtype);
7391 else
7392 return error_mark_node;
7394 else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7395 TREE_TYPE (argtype),
7396 !(complain & tf_error)))
7397 return error_mark_node;
7399 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7401 else
7402 inc = VECTOR_TYPE_P (argtype)
7403 ? build_one_cst (argtype)
7404 : integer_one_node;
7406 inc = cp_convert (argtype, inc, complain);
7408 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7409 need to ask Objective-C to build the increment or decrement
7410 expression for it. */
7411 if (objc_is_property_ref (arg))
7412 return objc_build_incr_expr_for_property_ref (input_location, code,
7413 arg, inc);
7415 /* Complain about anything else that is not a true lvalue. */
7416 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7417 || code == POSTINCREMENT_EXPR)
7418 ? lv_increment : lv_decrement),
7419 complain))
7420 return error_mark_node;
7422 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7423 prefix ++ and -- expressions of volatile-qualified arithmetic
7424 and pointer types are deprecated." */
7425 if (TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7426 warning_at (location, OPT_Wvolatile,
7427 "%qs expression of %<volatile%>-qualified type is "
7428 "deprecated",
7429 ((code == PREINCREMENT_EXPR
7430 || code == POSTINCREMENT_EXPR)
7431 ? "++" : "--"));
7433 /* Forbid using -- or ++ in C++17 on `bool'. */
7434 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7436 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7438 if (complain & tf_error)
7439 error_at (location,
7440 "use of an operand of type %qT in %<operator--%> "
7441 "is forbidden", boolean_type_node);
7442 return error_mark_node;
7444 else
7446 if (cxx_dialect >= cxx17)
7448 if (complain & tf_error)
7449 error_at (location,
7450 "use of an operand of type %qT in "
7451 "%<operator++%> is forbidden in C++17",
7452 boolean_type_node);
7453 return error_mark_node;
7455 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7456 else
7457 warning_at (location, OPT_Wdeprecated,
7458 "use of an operand of type %qT "
7459 "in %<operator++%> is deprecated",
7460 boolean_type_node);
7462 val = boolean_increment (code, arg);
7464 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7465 /* An rvalue has no cv-qualifiers. */
7466 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7467 else
7468 val = build2 (code, TREE_TYPE (arg), arg, inc);
7470 TREE_SIDE_EFFECTS (val) = 1;
7471 return val;
7474 case ADDR_EXPR:
7475 /* Note that this operation never does default_conversion
7476 regardless of NOCONVERT. */
7477 return cp_build_addr_expr (arg, complain);
7479 default:
7480 break;
7483 if (!errstring)
7485 if (argtype == 0)
7486 argtype = TREE_TYPE (arg);
7487 return build1 (code, argtype, arg);
7490 if (complain & tf_error)
7491 error_at (location, "%s", errstring);
7492 return error_mark_node;
7495 /* Hook for the c-common bits that build a unary op. */
7496 tree
7497 build_unary_op (location_t /*location*/,
7498 enum tree_code code, tree xarg, bool noconvert)
7500 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7503 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7504 so that it is a valid lvalue even for GENERIC by replacing
7505 (lhs = rhs) with ((lhs = rhs), lhs)
7506 (--lhs) with ((--lhs), lhs)
7507 (++lhs) with ((++lhs), lhs)
7508 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7509 that it can be evaluated multiple times. */
7511 tree
7512 genericize_compound_lvalue (tree lvalue)
7514 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7515 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7516 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7517 TREE_OPERAND (lvalue, 1));
7518 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7519 lvalue, TREE_OPERAND (lvalue, 0));
7522 /* Apply unary lvalue-demanding operator CODE to the expression ARG
7523 for certain kinds of expressions which are not really lvalues
7524 but which we can accept as lvalues.
7526 If ARG is not a kind of expression we can handle, return
7527 NULL_TREE. */
7529 tree
7530 unary_complex_lvalue (enum tree_code code, tree arg)
7532 /* Inside a template, making these kinds of adjustments is
7533 pointless; we are only concerned with the type of the
7534 expression. */
7535 if (processing_template_decl)
7536 return NULL_TREE;
7538 /* Handle (a, b) used as an "lvalue". */
7539 if (TREE_CODE (arg) == COMPOUND_EXPR)
7541 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7542 tf_warning_or_error);
7543 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7544 TREE_OPERAND (arg, 0), real_result);
7547 /* Handle (a ? b : c) used as an "lvalue". */
7548 if (TREE_CODE (arg) == COND_EXPR
7549 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7550 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7552 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7553 if (TREE_CODE (arg) == MODIFY_EXPR
7554 || TREE_CODE (arg) == PREINCREMENT_EXPR
7555 || TREE_CODE (arg) == PREDECREMENT_EXPR)
7556 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7558 if (code != ADDR_EXPR)
7559 return NULL_TREE;
7561 /* Handle (a = b) used as an "lvalue" for `&'. */
7562 if (TREE_CODE (arg) == MODIFY_EXPR
7563 || TREE_CODE (arg) == INIT_EXPR)
7565 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7566 tf_warning_or_error);
7567 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7568 arg, real_result);
7569 suppress_warning (arg /* What warning? */);
7570 return arg;
7573 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7574 || TREE_CODE (arg) == OFFSET_REF)
7575 return NULL_TREE;
7577 /* We permit compiler to make function calls returning
7578 objects of aggregate type look like lvalues. */
7580 tree targ = arg;
7582 if (TREE_CODE (targ) == SAVE_EXPR)
7583 targ = TREE_OPERAND (targ, 0);
7585 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7587 if (TREE_CODE (arg) == SAVE_EXPR)
7588 targ = arg;
7589 else
7590 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7591 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7594 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
7595 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
7596 TREE_OPERAND (targ, 0), current_function_decl, NULL);
7599 /* Don't let anything else be handled specially. */
7600 return NULL_TREE;
7603 /* Mark EXP saying that we need to be able to take the
7604 address of it; it should not be allocated in a register.
7605 Value is true if successful. ARRAY_REF_P is true if this
7606 is for ARRAY_REF construction - in that case we don't want
7607 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7608 it is fine to use ARRAY_REFs for vector subscripts on vector
7609 register variables.
7611 C++: we do not allow `current_class_ptr' to be addressable. */
7613 bool
7614 cxx_mark_addressable (tree exp, bool array_ref_p)
7616 tree x = exp;
7618 while (1)
7619 switch (TREE_CODE (x))
7621 case VIEW_CONVERT_EXPR:
7622 if (array_ref_p
7623 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7624 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
7625 return true;
7626 x = TREE_OPERAND (x, 0);
7627 break;
7629 case COMPONENT_REF:
7630 if (bitfield_p (x))
7631 error ("attempt to take address of bit-field");
7632 /* FALLTHRU */
7633 case ADDR_EXPR:
7634 case ARRAY_REF:
7635 case REALPART_EXPR:
7636 case IMAGPART_EXPR:
7637 x = TREE_OPERAND (x, 0);
7638 break;
7640 case PARM_DECL:
7641 if (x == current_class_ptr)
7643 error ("cannot take the address of %<this%>, which is an rvalue expression");
7644 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
7645 return true;
7647 /* Fall through. */
7649 case VAR_DECL:
7650 /* Caller should not be trying to mark initialized
7651 constant fields addressable. */
7652 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
7653 || DECL_IN_AGGR_P (x) == 0
7654 || TREE_STATIC (x)
7655 || DECL_EXTERNAL (x));
7656 /* Fall through. */
7658 case RESULT_DECL:
7659 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
7660 && !DECL_ARTIFICIAL (x))
7662 if (VAR_P (x) && DECL_HARD_REGISTER (x))
7664 error
7665 ("address of explicit register variable %qD requested", x);
7666 return false;
7668 else if (extra_warnings)
7669 warning
7670 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
7672 TREE_ADDRESSABLE (x) = 1;
7673 return true;
7675 case CONST_DECL:
7676 case FUNCTION_DECL:
7677 TREE_ADDRESSABLE (x) = 1;
7678 return true;
7680 case CONSTRUCTOR:
7681 TREE_ADDRESSABLE (x) = 1;
7682 return true;
7684 case TARGET_EXPR:
7685 TREE_ADDRESSABLE (x) = 1;
7686 cxx_mark_addressable (TREE_OPERAND (x, 0));
7687 return true;
7689 default:
7690 return true;
7694 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7696 tree
7697 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
7698 tsubst_flags_t complain)
7700 tree orig_ifexp = ifexp;
7701 tree orig_op1 = op1;
7702 tree orig_op2 = op2;
7703 tree expr;
7705 if (processing_template_decl)
7707 /* The standard says that the expression is type-dependent if
7708 IFEXP is type-dependent, even though the eventual type of the
7709 expression doesn't dependent on IFEXP. */
7710 if (type_dependent_expression_p (ifexp)
7711 /* As a GNU extension, the middle operand may be omitted. */
7712 || (op1 && type_dependent_expression_p (op1))
7713 || type_dependent_expression_p (op2))
7714 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
7715 ifexp = build_non_dependent_expr (ifexp);
7716 if (op1)
7717 op1 = build_non_dependent_expr (op1);
7718 op2 = build_non_dependent_expr (op2);
7721 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
7722 if (processing_template_decl && expr != error_mark_node)
7724 tree min = build_min_non_dep (COND_EXPR, expr,
7725 orig_ifexp, orig_op1, orig_op2);
7726 expr = convert_from_reference (min);
7728 return expr;
7731 /* Given a list of expressions, return a compound expression
7732 that performs them all and returns the value of the last of them. */
7734 tree
7735 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
7736 tsubst_flags_t complain)
7738 tree expr = TREE_VALUE (list);
7740 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7741 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
7743 if (complain & tf_error)
7744 pedwarn (cp_expr_loc_or_input_loc (expr), 0,
7745 "list-initializer for non-class type must not "
7746 "be parenthesized");
7747 else
7748 return error_mark_node;
7751 if (TREE_CHAIN (list))
7753 if (complain & tf_error)
7754 switch (exp)
7756 case ELK_INIT:
7757 permerror (input_location, "expression list treated as compound "
7758 "expression in initializer");
7759 break;
7760 case ELK_MEM_INIT:
7761 permerror (input_location, "expression list treated as compound "
7762 "expression in mem-initializer");
7763 break;
7764 case ELK_FUNC_CAST:
7765 permerror (input_location, "expression list treated as compound "
7766 "expression in functional cast");
7767 break;
7768 default:
7769 gcc_unreachable ();
7771 else
7772 return error_mark_node;
7774 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
7775 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
7776 expr, TREE_VALUE (list), NULL_TREE,
7777 complain);
7780 return expr;
7783 /* Like build_x_compound_expr_from_list, but using a VEC. */
7785 tree
7786 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
7787 tsubst_flags_t complain)
7789 if (vec_safe_is_empty (vec))
7790 return NULL_TREE;
7791 else if (vec->length () == 1)
7792 return (*vec)[0];
7793 else
7795 tree expr;
7796 unsigned int ix;
7797 tree t;
7799 if (msg != NULL)
7801 if (complain & tf_error)
7802 permerror (input_location,
7803 "%s expression list treated as compound expression",
7804 msg);
7805 else
7806 return error_mark_node;
7809 expr = (*vec)[0];
7810 for (ix = 1; vec->iterate (ix, &t); ++ix)
7811 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
7812 t, NULL_TREE, complain);
7814 return expr;
7818 /* Handle overloading of the ',' operator when needed. */
7820 tree
7821 build_x_compound_expr (location_t loc, tree op1, tree op2,
7822 tree lookups, tsubst_flags_t complain)
7824 tree result;
7825 tree orig_op1 = op1;
7826 tree orig_op2 = op2;
7827 tree overload = NULL_TREE;
7829 if (processing_template_decl)
7831 if (type_dependent_expression_p (op1)
7832 || type_dependent_expression_p (op2))
7834 result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
7835 TREE_TYPE (result)
7836 = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
7837 return result;
7839 op1 = build_non_dependent_expr (op1);
7840 op2 = build_non_dependent_expr (op2);
7843 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
7844 NULL_TREE, lookups, &overload, complain);
7845 if (!result)
7846 result = cp_build_compound_expr (op1, op2, complain);
7848 if (processing_template_decl && result != error_mark_node)
7850 if (overload != NULL_TREE)
7851 return (build_min_non_dep_op_overload
7852 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
7854 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
7857 return result;
7860 /* Like cp_build_compound_expr, but for the c-common bits. */
7862 tree
7863 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
7865 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
7868 /* Build a compound expression. */
7870 tree
7871 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
7873 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
7875 if (lhs == error_mark_node || rhs == error_mark_node)
7876 return error_mark_node;
7878 if (TREE_CODE (rhs) == TARGET_EXPR)
7880 /* If the rhs is a TARGET_EXPR, then build the compound
7881 expression inside the target_expr's initializer. This
7882 helps the compiler to eliminate unnecessary temporaries. */
7883 tree init = TREE_OPERAND (rhs, 1);
7885 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
7886 TREE_OPERAND (rhs, 1) = init;
7888 return rhs;
7891 if (type_unknown_p (rhs))
7893 if (complain & tf_error)
7894 error_at (cp_expr_loc_or_input_loc (rhs),
7895 "no context to resolve type of %qE", rhs);
7896 return error_mark_node;
7899 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
7902 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
7903 casts away constness. CAST gives the type of cast. Returns true
7904 if the cast is ill-formed, false if it is well-formed.
7906 ??? This function warns for casting away any qualifier not just
7907 const. We would like to specify exactly what qualifiers are casted
7908 away.
7911 static bool
7912 check_for_casting_away_constness (location_t loc, tree src_type,
7913 tree dest_type, enum tree_code cast,
7914 tsubst_flags_t complain)
7916 /* C-style casts are allowed to cast away constness. With
7917 WARN_CAST_QUAL, we still want to issue a warning. */
7918 if (cast == CAST_EXPR && !warn_cast_qual)
7919 return false;
7921 if (!casts_away_constness (src_type, dest_type, complain))
7922 return false;
7924 switch (cast)
7926 case CAST_EXPR:
7927 if (complain & tf_warning)
7928 warning_at (loc, OPT_Wcast_qual,
7929 "cast from type %qT to type %qT casts away qualifiers",
7930 src_type, dest_type);
7931 return false;
7933 case STATIC_CAST_EXPR:
7934 if (complain & tf_error)
7935 error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
7936 "away qualifiers",
7937 src_type, dest_type);
7938 return true;
7940 case REINTERPRET_CAST_EXPR:
7941 if (complain & tf_error)
7942 error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
7943 "casts away qualifiers",
7944 src_type, dest_type);
7945 return true;
7947 default:
7948 gcc_unreachable();
7952 /* Warns if the cast from expression EXPR to type TYPE is useless. */
7953 void
7954 maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
7955 tsubst_flags_t complain)
7957 if (warn_useless_cast
7958 && complain & tf_warning)
7960 if ((TYPE_REF_P (type)
7961 && (TYPE_REF_IS_RVALUE (type)
7962 ? xvalue_p (expr) : lvalue_p (expr))
7963 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
7964 || same_type_p (TREE_TYPE (expr), type))
7965 warning_at (loc, OPT_Wuseless_cast,
7966 "useless cast to type %q#T", type);
7970 /* Warns if the cast ignores cv-qualifiers on TYPE. */
7971 static void
7972 maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
7973 tsubst_flags_t complain)
7975 if (warn_ignored_qualifiers
7976 && complain & tf_warning
7977 && !CLASS_TYPE_P (type)
7978 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
7979 warning_at (loc, OPT_Wignored_qualifiers,
7980 "type qualifiers ignored on cast result type");
7983 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
7984 (another pointer-to-member type in the same hierarchy) and return
7985 the converted expression. If ALLOW_INVERSE_P is permitted, a
7986 pointer-to-derived may be converted to pointer-to-base; otherwise,
7987 only the other direction is permitted. If C_CAST_P is true, this
7988 conversion is taking place as part of a C-style cast. */
7990 tree
7991 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
7992 bool c_cast_p, tsubst_flags_t complain)
7994 if (same_type_p (type, TREE_TYPE (expr)))
7995 return expr;
7997 if (TYPE_PTRDATAMEM_P (type))
7999 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8000 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8001 tree delta = (get_delta_difference
8002 (obase, nbase,
8003 allow_inverse_p, c_cast_p, complain));
8005 if (delta == error_mark_node)
8006 return error_mark_node;
8008 if (!same_type_p (obase, nbase))
8010 if (TREE_CODE (expr) == PTRMEM_CST)
8011 expr = cplus_expand_constant (expr);
8013 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8014 build_int_cst (TREE_TYPE (expr), -1),
8015 complain);
8016 tree op1 = build_nop (ptrdiff_type_node, expr);
8017 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8018 complain);
8020 expr = fold_build3_loc (input_location,
8021 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8024 return build_nop (type, expr);
8026 else
8027 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8028 allow_inverse_p, c_cast_p, complain);
8031 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8032 this static_cast is being attempted as one of the possible casts
8033 allowed by a C-style cast. (In that case, accessibility of base
8034 classes is not considered, and it is OK to cast away
8035 constness.) Return the result of the cast. *VALID_P is set to
8036 indicate whether or not the cast was valid. */
8038 static tree
8039 build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8040 bool *valid_p, tsubst_flags_t complain)
8042 tree intype;
8043 tree result;
8044 cp_lvalue_kind clk;
8046 /* Assume the cast is valid. */
8047 *valid_p = true;
8049 intype = unlowered_expr_type (expr);
8051 /* Save casted types in the function's used types hash table. */
8052 used_types_insert (type);
8054 /* A prvalue of non-class type is cv-unqualified. */
8055 if (!CLASS_TYPE_P (type))
8056 type = cv_unqualified (type);
8058 /* [expr.static.cast]
8060 An lvalue of type "cv1 B", where B is a class type, can be cast
8061 to type "reference to cv2 D", where D is a class derived (clause
8062 _class.derived_) from B, if a valid standard conversion from
8063 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8064 same cv-qualification as, or greater cv-qualification than, cv1,
8065 and B is not a virtual base class of D. */
8066 /* We check this case before checking the validity of "TYPE t =
8067 EXPR;" below because for this case:
8069 struct B {};
8070 struct D : public B { D(const B&); };
8071 extern B& b;
8072 void f() { static_cast<const D&>(b); }
8074 we want to avoid constructing a new D. The standard is not
8075 completely clear about this issue, but our interpretation is
8076 consistent with other compilers. */
8077 if (TYPE_REF_P (type)
8078 && CLASS_TYPE_P (TREE_TYPE (type))
8079 && CLASS_TYPE_P (intype)
8080 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8081 && DERIVED_FROM_P (intype, TREE_TYPE (type))
8082 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8083 build_pointer_type (TYPE_MAIN_VARIANT
8084 (TREE_TYPE (type))),
8085 complain)
8086 && (c_cast_p
8087 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8089 tree base;
8091 if (processing_template_decl)
8092 return expr;
8094 /* There is a standard conversion from "D*" to "B*" even if "B"
8095 is ambiguous or inaccessible. If this is really a
8096 static_cast, then we check both for inaccessibility and
8097 ambiguity. However, if this is a static_cast being performed
8098 because the user wrote a C-style cast, then accessibility is
8099 not considered. */
8100 base = lookup_base (TREE_TYPE (type), intype,
8101 c_cast_p ? ba_unique : ba_check,
8102 NULL, complain);
8103 expr = cp_build_addr_expr (expr, complain);
8105 if (sanitize_flags_p (SANITIZE_VPTR))
8107 tree ubsan_check
8108 = cp_ubsan_maybe_instrument_downcast (loc, type,
8109 intype, expr);
8110 if (ubsan_check)
8111 expr = ubsan_check;
8114 /* Convert from "B*" to "D*". This function will check that "B"
8115 is not a virtual base of "D". Even if we don't have a guarantee
8116 that expr is NULL, if the static_cast is to a reference type,
8117 it is UB if it would be NULL, so omit the non-NULL check. */
8118 expr = build_base_path (MINUS_EXPR, expr, base,
8119 /*nonnull=*/flag_delete_null_pointer_checks,
8120 complain);
8122 /* Convert the pointer to a reference -- but then remember that
8123 there are no expressions with reference type in C++.
8125 We call rvalue so that there's an actual tree code
8126 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8127 is a variable with the same type, the conversion would get folded
8128 away, leaving just the variable and causing lvalue_kind to give
8129 the wrong answer. */
8130 expr = cp_fold_convert (type, expr);
8132 /* When -fsanitize=null, make sure to diagnose reference binding to
8133 NULL even when the reference is converted to pointer later on. */
8134 if (sanitize_flags_p (SANITIZE_NULL)
8135 && TREE_CODE (expr) == COND_EXPR
8136 && TREE_OPERAND (expr, 2)
8137 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8138 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8139 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8141 return convert_from_reference (rvalue (expr));
8144 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8145 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8146 if (TYPE_REF_P (type)
8147 && TYPE_REF_IS_RVALUE (type)
8148 && (clk = real_lvalue_p (expr))
8149 && reference_compatible_p (TREE_TYPE (type), intype)
8150 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8152 if (processing_template_decl)
8153 return expr;
8154 if (clk == clk_ordinary)
8156 /* Handle the (non-bit-field) lvalue case here by casting to
8157 lvalue reference and then changing it to an rvalue reference.
8158 Casting an xvalue to rvalue reference will be handled by the
8159 main code path. */
8160 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8161 result = (perform_direct_initialization_if_possible
8162 (lref, expr, c_cast_p, complain));
8163 result = build1 (NON_LVALUE_EXPR, type, result);
8164 return convert_from_reference (result);
8166 else
8167 /* For a bit-field or packed field, bind to a temporary. */
8168 expr = rvalue (expr);
8171 /* Resolve overloaded address here rather than once in
8172 implicit_conversion and again in the inverse code below. */
8173 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8175 expr = instantiate_type (type, expr, complain);
8176 intype = TREE_TYPE (expr);
8179 /* [expr.static.cast]
8181 Any expression can be explicitly converted to type cv void. */
8182 if (VOID_TYPE_P (type))
8183 return convert_to_void (expr, ICV_CAST, complain);
8185 /* [class.abstract]
8186 An abstract class shall not be used ... as the type of an explicit
8187 conversion. */
8188 if (abstract_virtuals_error (ACU_CAST, type, complain))
8189 return error_mark_node;
8191 /* [expr.static.cast]
8193 An expression e can be explicitly converted to a type T using a
8194 static_cast of the form static_cast<T>(e) if the declaration T
8195 t(e);" is well-formed, for some invented temporary variable
8196 t. */
8197 result = perform_direct_initialization_if_possible (type, expr,
8198 c_cast_p, complain);
8199 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8200 which initialize the first element of the aggregate. We need to handle
8201 the array case specifically. */
8202 if (result == NULL_TREE
8203 && cxx_dialect >= cxx20
8204 && TREE_CODE (type) == ARRAY_TYPE)
8206 /* Create { EXPR } and perform direct-initialization from it. */
8207 tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8208 CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8209 CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8210 result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8211 complain);
8213 if (result)
8215 if (processing_template_decl)
8216 return expr;
8218 result = convert_from_reference (result);
8220 /* [expr.static.cast]
8222 If T is a reference type, the result is an lvalue; otherwise,
8223 the result is an rvalue. */
8224 if (!TYPE_REF_P (type))
8226 result = rvalue (result);
8228 if (result == expr && SCALAR_TYPE_P (type))
8229 /* Leave some record of the cast. */
8230 result = build_nop (type, expr);
8232 return result;
8235 /* [expr.static.cast]
8237 The inverse of any standard conversion sequence (clause _conv_),
8238 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8239 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8240 (_conv.bool_) conversions, can be performed explicitly using
8241 static_cast subject to the restriction that the explicit
8242 conversion does not cast away constness (_expr.const.cast_), and
8243 the following additional rules for specific cases: */
8244 /* For reference, the conversions not excluded are: integral
8245 promotions, floating-point promotion, integral conversions,
8246 floating-point conversions, floating-integral conversions,
8247 pointer conversions, and pointer to member conversions. */
8248 /* DR 128
8250 A value of integral _or enumeration_ type can be explicitly
8251 converted to an enumeration type. */
8252 /* The effect of all that is that any conversion between any two
8253 types which are integral, floating, or enumeration types can be
8254 performed. */
8255 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8256 || SCALAR_FLOAT_TYPE_P (type))
8257 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8258 || SCALAR_FLOAT_TYPE_P (intype)))
8260 if (processing_template_decl)
8261 return expr;
8262 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8265 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8266 && CLASS_TYPE_P (TREE_TYPE (type))
8267 && CLASS_TYPE_P (TREE_TYPE (intype))
8268 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8269 (TREE_TYPE (intype))),
8270 build_pointer_type (TYPE_MAIN_VARIANT
8271 (TREE_TYPE (type))),
8272 complain))
8274 tree base;
8276 if (processing_template_decl)
8277 return expr;
8279 if (!c_cast_p
8280 && check_for_casting_away_constness (loc, intype, type,
8281 STATIC_CAST_EXPR,
8282 complain))
8283 return error_mark_node;
8284 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8285 c_cast_p ? ba_unique : ba_check,
8286 NULL, complain);
8287 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8288 complain);
8290 if (sanitize_flags_p (SANITIZE_VPTR))
8292 tree ubsan_check
8293 = cp_ubsan_maybe_instrument_downcast (loc, type,
8294 intype, expr);
8295 if (ubsan_check)
8296 expr = ubsan_check;
8299 return cp_fold_convert (type, expr);
8302 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8303 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8305 tree c1;
8306 tree c2;
8307 tree t1;
8308 tree t2;
8310 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8311 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8313 if (TYPE_PTRDATAMEM_P (type))
8315 t1 = (build_ptrmem_type
8316 (c1,
8317 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8318 t2 = (build_ptrmem_type
8319 (c2,
8320 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8322 else
8324 t1 = intype;
8325 t2 = type;
8327 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8329 if (!c_cast_p
8330 && check_for_casting_away_constness (loc, intype, type,
8331 STATIC_CAST_EXPR,
8332 complain))
8333 return error_mark_node;
8334 if (processing_template_decl)
8335 return expr;
8336 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8337 c_cast_p, complain);
8341 /* [expr.static.cast]
8343 An rvalue of type "pointer to cv void" can be explicitly
8344 converted to a pointer to object type. A value of type pointer
8345 to object converted to "pointer to cv void" and back to the
8346 original pointer type will have its original value. */
8347 if (TYPE_PTR_P (intype)
8348 && VOID_TYPE_P (TREE_TYPE (intype))
8349 && TYPE_PTROB_P (type))
8351 if (!c_cast_p
8352 && check_for_casting_away_constness (loc, intype, type,
8353 STATIC_CAST_EXPR,
8354 complain))
8355 return error_mark_node;
8356 if (processing_template_decl)
8357 return expr;
8358 return build_nop (type, expr);
8361 *valid_p = false;
8362 return error_mark_node;
8365 /* Return an expression representing static_cast<TYPE>(EXPR). */
8367 tree
8368 build_static_cast (location_t loc, tree type, tree oexpr,
8369 tsubst_flags_t complain)
8371 tree expr = oexpr;
8372 tree result;
8373 bool valid_p;
8375 if (type == error_mark_node || expr == error_mark_node)
8376 return error_mark_node;
8378 bool dependent = (dependent_type_p (type)
8379 || type_dependent_expression_p (expr));
8380 if (dependent)
8382 tmpl:
8383 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8384 /* We don't know if it will or will not have side effects. */
8385 TREE_SIDE_EFFECTS (expr) = 1;
8386 result = convert_from_reference (expr);
8387 protected_set_expr_location (result, loc);
8388 return result;
8390 else if (processing_template_decl)
8391 expr = build_non_dependent_expr (expr);
8393 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8394 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8395 if (!TYPE_REF_P (type)
8396 && TREE_CODE (expr) == NOP_EXPR
8397 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8398 expr = TREE_OPERAND (expr, 0);
8400 result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8401 &valid_p, complain);
8402 if (valid_p)
8404 if (result != error_mark_node)
8406 maybe_warn_about_useless_cast (loc, type, expr, complain);
8407 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8409 if (processing_template_decl)
8410 goto tmpl;
8411 protected_set_expr_location (result, loc);
8412 return result;
8415 if (complain & tf_error)
8417 error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8418 TREE_TYPE (expr), type);
8419 if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8420 && CLASS_TYPE_P (TREE_TYPE (type))
8421 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8422 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8423 "class type %qT is incomplete", TREE_TYPE (type));
8424 tree expr_type = TREE_TYPE (expr);
8425 if (TYPE_PTR_P (expr_type))
8426 expr_type = TREE_TYPE (expr_type);
8427 if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8428 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8429 "class type %qT is incomplete", expr_type);
8431 return error_mark_node;
8434 /* EXPR is an expression with member function or pointer-to-member
8435 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8436 not permitted by ISO C++, but we accept it in some modes. If we
8437 are not in one of those modes, issue a diagnostic. Return the
8438 converted expression. */
8440 tree
8441 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8443 tree intype;
8444 tree decl;
8446 intype = TREE_TYPE (expr);
8447 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8448 || TREE_CODE (intype) == METHOD_TYPE);
8450 if (!(complain & tf_warning_or_error))
8451 return error_mark_node;
8453 location_t loc = cp_expr_loc_or_input_loc (expr);
8455 if (pedantic || warn_pmf2ptr)
8456 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8457 "converting from %qH to %qI", intype, type);
8459 STRIP_ANY_LOCATION_WRAPPER (expr);
8461 if (TREE_CODE (intype) == METHOD_TYPE)
8462 expr = build_addr_func (expr, complain);
8463 else if (TREE_CODE (expr) == PTRMEM_CST)
8464 expr = build_address (PTRMEM_CST_MEMBER (expr));
8465 else
8467 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8468 decl = build_address (decl);
8469 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8472 if (expr == error_mark_node)
8473 return error_mark_node;
8475 expr = build_nop (type, expr);
8476 SET_EXPR_LOCATION (expr, loc);
8477 return expr;
8480 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8481 constexpr evaluation knows to reject it. */
8483 static tree
8484 build_nop_reinterpret (tree type, tree expr)
8486 tree ret = build_nop (type, expr);
8487 if (ret != expr)
8488 REINTERPRET_CAST_P (ret) = true;
8489 return ret;
8492 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8493 If C_CAST_P is true, this reinterpret cast is being done as part of
8494 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8495 indicate whether or not reinterpret_cast was valid. */
8497 static tree
8498 build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8499 bool c_cast_p, bool *valid_p,
8500 tsubst_flags_t complain)
8502 tree intype;
8504 /* Assume the cast is invalid. */
8505 if (valid_p)
8506 *valid_p = true;
8508 if (type == error_mark_node || error_operand_p (expr))
8509 return error_mark_node;
8511 intype = TREE_TYPE (expr);
8513 /* Save casted types in the function's used types hash table. */
8514 used_types_insert (type);
8516 /* A prvalue of non-class type is cv-unqualified. */
8517 if (!CLASS_TYPE_P (type))
8518 type = cv_unqualified (type);
8520 /* [expr.reinterpret.cast]
8521 A glvalue of type T1, designating an object x, can be cast to the type
8522 "reference to T2" if an expression of type "pointer to T1" can be
8523 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8524 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8525 of type "pointer to T1". No temporary is created, no copy is made, and no
8526 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8527 if (TYPE_REF_P (type))
8529 if (!glvalue_p (expr))
8531 if (complain & tf_error)
8532 error_at (loc, "invalid cast of a prvalue expression of type "
8533 "%qT to type %qT",
8534 intype, type);
8535 return error_mark_node;
8538 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8539 "B" are related class types; the reinterpret_cast does not
8540 adjust the pointer. */
8541 if (TYPE_PTR_P (intype)
8542 && (complain & tf_warning)
8543 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8544 COMPARE_BASE | COMPARE_DERIVED)))
8545 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8546 intype, type);
8548 expr = cp_build_addr_expr (expr, complain);
8550 if (warn_strict_aliasing > 2)
8551 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8553 if (expr != error_mark_node)
8554 expr = build_reinterpret_cast_1
8555 (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8556 valid_p, complain);
8557 if (expr != error_mark_node)
8558 /* cp_build_indirect_ref isn't right for rvalue refs. */
8559 expr = convert_from_reference (fold_convert (type, expr));
8560 return expr;
8563 /* As a G++ extension, we consider conversions from member
8564 functions, and pointers to member functions to
8565 pointer-to-function and pointer-to-void types. If
8566 -Wno-pmf-conversions has not been specified,
8567 convert_member_func_to_ptr will issue an error message. */
8568 if ((TYPE_PTRMEMFUNC_P (intype)
8569 || TREE_CODE (intype) == METHOD_TYPE)
8570 && TYPE_PTR_P (type)
8571 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
8572 || VOID_TYPE_P (TREE_TYPE (type))))
8573 return convert_member_func_to_ptr (type, expr, complain);
8575 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8576 array-to-pointer, and function-to-pointer conversions are
8577 performed. */
8578 expr = decay_conversion (expr, complain);
8580 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8581 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8582 if (TREE_CODE (expr) == NOP_EXPR
8583 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8584 expr = TREE_OPERAND (expr, 0);
8586 if (error_operand_p (expr))
8587 return error_mark_node;
8589 intype = TREE_TYPE (expr);
8591 /* [expr.reinterpret.cast]
8592 A pointer can be converted to any integral type large enough to
8593 hold it. ... A value of type std::nullptr_t can be converted to
8594 an integral type; the conversion has the same meaning and
8595 validity as a conversion of (void*)0 to the integral type. */
8596 if (CP_INTEGRAL_TYPE_P (type)
8597 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8599 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
8601 if (complain & tf_error)
8602 permerror (loc, "cast from %qH to %qI loses precision",
8603 intype, type);
8604 else
8605 return error_mark_node;
8607 if (NULLPTR_TYPE_P (intype))
8608 return build_int_cst (type, 0);
8610 /* [expr.reinterpret.cast]
8611 A value of integral or enumeration type can be explicitly
8612 converted to a pointer. */
8613 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
8614 /* OK */
8616 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8617 || TYPE_PTR_OR_PTRMEM_P (type))
8618 && same_type_p (type, intype))
8619 /* DR 799 */
8620 return rvalue (expr);
8621 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
8623 if ((complain & tf_warning)
8624 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
8625 TREE_TYPE (intype)))
8626 warning_at (loc, OPT_Wcast_function_type,
8627 "cast between incompatible function types"
8628 " from %qH to %qI", intype, type);
8629 return build_nop_reinterpret (type, expr);
8631 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
8633 if ((complain & tf_warning)
8634 && !cxx_safe_function_type_cast_p
8635 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
8636 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
8637 warning_at (loc, OPT_Wcast_function_type,
8638 "cast between incompatible pointer to member types"
8639 " from %qH to %qI", intype, type);
8640 return build_nop_reinterpret (type, expr);
8642 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8643 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
8645 if (!c_cast_p
8646 && check_for_casting_away_constness (loc, intype, type,
8647 REINTERPRET_CAST_EXPR,
8648 complain))
8649 return error_mark_node;
8650 /* Warn about possible alignment problems. */
8651 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8652 && (complain & tf_warning)
8653 && !VOID_TYPE_P (type)
8654 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
8655 && COMPLETE_TYPE_P (TREE_TYPE (type))
8656 && COMPLETE_TYPE_P (TREE_TYPE (intype))
8657 && min_align_of_type (TREE_TYPE (type))
8658 > min_align_of_type (TREE_TYPE (intype)))
8659 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8660 "increases required alignment of target type",
8661 intype, type);
8663 if (warn_strict_aliasing <= 2)
8664 /* strict_aliasing_warning STRIP_NOPs its expr. */
8665 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8667 return build_nop_reinterpret (type, expr);
8669 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
8670 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
8672 if (complain & tf_warning)
8673 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8674 object pointer type or vice versa is conditionally-supported." */
8675 warning_at (loc, OPT_Wconditionally_supported,
8676 "casting between pointer-to-function and "
8677 "pointer-to-object is conditionally-supported");
8678 return build_nop_reinterpret (type, expr);
8680 else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
8681 return convert_to_vector (type, rvalue (expr));
8682 else if (gnu_vector_type_p (intype)
8683 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8684 return convert_to_integer_nofold (type, expr);
8685 else
8687 if (valid_p)
8688 *valid_p = false;
8689 if (complain & tf_error)
8690 error_at (loc, "invalid cast from type %qT to type %qT",
8691 intype, type);
8692 return error_mark_node;
8695 expr = cp_convert (type, expr, complain);
8696 if (TREE_CODE (expr) == NOP_EXPR)
8697 /* Mark any nop_expr that created as a reintepret_cast. */
8698 REINTERPRET_CAST_P (expr) = true;
8699 return expr;
8702 tree
8703 build_reinterpret_cast (location_t loc, tree type, tree expr,
8704 tsubst_flags_t complain)
8706 tree r;
8708 if (type == error_mark_node || expr == error_mark_node)
8709 return error_mark_node;
8711 if (processing_template_decl)
8713 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
8715 if (!TREE_SIDE_EFFECTS (t)
8716 && type_dependent_expression_p (expr))
8717 /* There might turn out to be side effects inside expr. */
8718 TREE_SIDE_EFFECTS (t) = 1;
8719 r = convert_from_reference (t);
8720 protected_set_expr_location (r, loc);
8721 return r;
8724 r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8725 /*valid_p=*/NULL, complain);
8726 if (r != error_mark_node)
8728 maybe_warn_about_useless_cast (loc, type, expr, complain);
8729 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8731 protected_set_expr_location (r, loc);
8732 return r;
8735 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
8736 return an appropriate expression. Otherwise, return
8737 error_mark_node. If the cast is not valid, and COMPLAIN is true,
8738 then a diagnostic will be issued. If VALID_P is non-NULL, we are
8739 performing a C-style cast, its value upon return will indicate
8740 whether or not the conversion succeeded. */
8742 static tree
8743 build_const_cast_1 (location_t loc, tree dst_type, tree expr,
8744 tsubst_flags_t complain, bool *valid_p)
8746 tree src_type;
8747 tree reference_type;
8749 /* Callers are responsible for handling error_mark_node as a
8750 destination type. */
8751 gcc_assert (dst_type != error_mark_node);
8752 /* In a template, callers should be building syntactic
8753 representations of casts, not using this machinery. */
8754 gcc_assert (!processing_template_decl);
8756 /* Assume the conversion is invalid. */
8757 if (valid_p)
8758 *valid_p = false;
8760 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
8762 if (complain & tf_error)
8763 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8764 "which is not a pointer, reference, "
8765 "nor a pointer-to-data-member type", dst_type);
8766 return error_mark_node;
8769 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
8771 if (complain & tf_error)
8772 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8773 "which is a pointer or reference to a function type",
8774 dst_type);
8775 return error_mark_node;
8778 /* A prvalue of non-class type is cv-unqualified. */
8779 dst_type = cv_unqualified (dst_type);
8781 /* Save casted types in the function's used types hash table. */
8782 used_types_insert (dst_type);
8784 src_type = TREE_TYPE (expr);
8785 /* Expressions do not really have reference types. */
8786 if (TYPE_REF_P (src_type))
8787 src_type = TREE_TYPE (src_type);
8789 /* [expr.const.cast]
8791 For two object types T1 and T2, if a pointer to T1 can be explicitly
8792 converted to the type "pointer to T2" using a const_cast, then the
8793 following conversions can also be made:
8795 -- an lvalue of type T1 can be explicitly converted to an lvalue of
8796 type T2 using the cast const_cast<T2&>;
8798 -- a glvalue of type T1 can be explicitly converted to an xvalue of
8799 type T2 using the cast const_cast<T2&&>; and
8801 -- if T1 is a class type, a prvalue of type T1 can be explicitly
8802 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
8804 if (TYPE_REF_P (dst_type))
8806 reference_type = dst_type;
8807 if (!TYPE_REF_IS_RVALUE (dst_type)
8808 ? lvalue_p (expr)
8809 : obvalue_p (expr))
8810 /* OK. */;
8811 else
8813 if (complain & tf_error)
8814 error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
8815 "to type %qT",
8816 src_type, dst_type);
8817 return error_mark_node;
8819 dst_type = build_pointer_type (TREE_TYPE (dst_type));
8820 src_type = build_pointer_type (src_type);
8822 else
8824 reference_type = NULL_TREE;
8825 /* If the destination type is not a reference type, the
8826 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8827 conversions are performed. */
8828 src_type = type_decays_to (src_type);
8829 if (src_type == error_mark_node)
8830 return error_mark_node;
8833 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
8835 if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
8837 if (valid_p)
8839 *valid_p = true;
8840 /* This cast is actually a C-style cast. Issue a warning if
8841 the user is making a potentially unsafe cast. */
8842 check_for_casting_away_constness (loc, src_type, dst_type,
8843 CAST_EXPR, complain);
8844 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
8845 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8846 && (complain & tf_warning)
8847 && min_align_of_type (TREE_TYPE (dst_type))
8848 > min_align_of_type (TREE_TYPE (src_type)))
8849 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8850 "increases required alignment of target type",
8851 src_type, dst_type);
8853 if (reference_type)
8855 expr = cp_build_addr_expr (expr, complain);
8856 if (expr == error_mark_node)
8857 return error_mark_node;
8858 expr = build_nop (reference_type, expr);
8859 return convert_from_reference (expr);
8861 else
8863 expr = decay_conversion (expr, complain);
8864 if (expr == error_mark_node)
8865 return error_mark_node;
8867 /* build_c_cast puts on a NOP_EXPR to make the result not an
8868 lvalue. Strip such NOP_EXPRs if VALUE is being used in
8869 non-lvalue context. */
8870 if (TREE_CODE (expr) == NOP_EXPR
8871 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8872 expr = TREE_OPERAND (expr, 0);
8873 return build_nop (dst_type, expr);
8876 else if (valid_p
8877 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
8878 TREE_TYPE (src_type)))
8879 check_for_casting_away_constness (loc, src_type, dst_type,
8880 CAST_EXPR, complain);
8883 if (complain & tf_error)
8884 error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
8885 src_type, dst_type);
8886 return error_mark_node;
8889 tree
8890 build_const_cast (location_t loc, tree type, tree expr,
8891 tsubst_flags_t complain)
8893 tree r;
8895 if (type == error_mark_node || error_operand_p (expr))
8896 return error_mark_node;
8898 if (processing_template_decl)
8900 tree t = build_min (CONST_CAST_EXPR, type, expr);
8902 if (!TREE_SIDE_EFFECTS (t)
8903 && type_dependent_expression_p (expr))
8904 /* There might turn out to be side effects inside expr. */
8905 TREE_SIDE_EFFECTS (t) = 1;
8906 r = convert_from_reference (t);
8907 protected_set_expr_location (r, loc);
8908 return r;
8911 r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
8912 if (r != error_mark_node)
8914 maybe_warn_about_useless_cast (loc, type, expr, complain);
8915 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8917 protected_set_expr_location (r, loc);
8918 return r;
8921 /* Like cp_build_c_cast, but for the c-common bits. */
8923 tree
8924 build_c_cast (location_t loc, tree type, tree expr)
8926 return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8929 /* Like the "build_c_cast" used for c-common, but using cp_expr to
8930 preserve location information even for tree nodes that don't
8931 support it. */
8933 cp_expr
8934 build_c_cast (location_t loc, tree type, cp_expr expr)
8936 cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8937 result.set_location (loc);
8938 return result;
8941 /* Build an expression representing an explicit C-style cast to type
8942 TYPE of expression EXPR. */
8944 tree
8945 cp_build_c_cast (location_t loc, tree type, tree expr,
8946 tsubst_flags_t complain)
8948 tree value = expr;
8949 tree result;
8950 bool valid_p;
8952 if (type == error_mark_node || error_operand_p (expr))
8953 return error_mark_node;
8955 if (processing_template_decl)
8957 tree t = build_min (CAST_EXPR, type,
8958 tree_cons (NULL_TREE, value, NULL_TREE));
8959 /* We don't know if it will or will not have side effects. */
8960 TREE_SIDE_EFFECTS (t) = 1;
8961 return convert_from_reference (t);
8964 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
8965 'Class') should always be retained, because this information aids
8966 in method lookup. */
8967 if (objc_is_object_ptr (type)
8968 && objc_is_object_ptr (TREE_TYPE (expr)))
8969 return build_nop (type, expr);
8971 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8972 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8973 if (!TYPE_REF_P (type)
8974 && TREE_CODE (value) == NOP_EXPR
8975 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
8976 value = TREE_OPERAND (value, 0);
8978 if (TREE_CODE (type) == ARRAY_TYPE)
8980 /* Allow casting from T1* to T2[] because Cfront allows it.
8981 NIHCL uses it. It is not valid ISO C++ however. */
8982 if (TYPE_PTR_P (TREE_TYPE (expr)))
8984 if (complain & tf_error)
8985 permerror (loc, "ISO C++ forbids casting to an array type %qT",
8986 type);
8987 else
8988 return error_mark_node;
8989 type = build_pointer_type (TREE_TYPE (type));
8991 else
8993 if (complain & tf_error)
8994 error_at (loc, "ISO C++ forbids casting to an array type %qT",
8995 type);
8996 return error_mark_node;
9000 if (FUNC_OR_METHOD_TYPE_P (type))
9002 if (complain & tf_error)
9003 error_at (loc, "invalid cast to function type %qT", type);
9004 return error_mark_node;
9007 if (TYPE_PTR_P (type)
9008 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9009 /* Casting to an integer of smaller size is an error detected elsewhere. */
9010 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9011 /* Don't warn about converting any constant. */
9012 && !TREE_CONSTANT (value))
9013 warning_at (loc, OPT_Wint_to_pointer_cast,
9014 "cast to pointer from integer of different size");
9016 /* A C-style cast can be a const_cast. */
9017 result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9018 &valid_p);
9019 if (valid_p)
9021 if (result != error_mark_node)
9023 maybe_warn_about_useless_cast (loc, type, value, complain);
9024 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9026 return result;
9029 /* Or a static cast. */
9030 result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9031 &valid_p, complain);
9032 /* Or a reinterpret_cast. */
9033 if (!valid_p)
9034 result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9035 &valid_p, complain);
9036 /* The static_cast or reinterpret_cast may be followed by a
9037 const_cast. */
9038 if (valid_p
9039 /* A valid cast may result in errors if, for example, a
9040 conversion to an ambiguous base class is required. */
9041 && !error_operand_p (result))
9043 tree result_type;
9045 maybe_warn_about_useless_cast (loc, type, value, complain);
9046 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9048 /* Non-class rvalues always have cv-unqualified type. */
9049 if (!CLASS_TYPE_P (type))
9050 type = TYPE_MAIN_VARIANT (type);
9051 result_type = TREE_TYPE (result);
9052 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9053 result_type = TYPE_MAIN_VARIANT (result_type);
9054 /* If the type of RESULT does not match TYPE, perform a
9055 const_cast to make it match. If the static_cast or
9056 reinterpret_cast succeeded, we will differ by at most
9057 cv-qualification, so the follow-on const_cast is guaranteed
9058 to succeed. */
9059 if (!same_type_p (non_reference (type), non_reference (result_type)))
9061 result = build_const_cast_1 (loc, type, result, false, &valid_p);
9062 gcc_assert (valid_p);
9064 return result;
9067 return error_mark_node;
9070 /* Warn when a value is moved to itself with std::move. LHS is the target,
9071 RHS may be the std::move call, and LOC is the location of the whole
9072 assignment. */
9074 static void
9075 maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9077 if (!warn_self_move)
9078 return;
9080 /* C++98 doesn't know move. */
9081 if (cxx_dialect < cxx11)
9082 return;
9084 if (processing_template_decl)
9085 return;
9087 if (!REFERENCE_REF_P (rhs)
9088 || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9089 return;
9090 tree fn = TREE_OPERAND (rhs, 0);
9091 if (!is_std_move_p (fn))
9092 return;
9094 /* Just a little helper to strip * and various NOPs. */
9095 auto extract_op = [] (tree &op) {
9096 STRIP_NOPS (op);
9097 while (INDIRECT_REF_P (op))
9098 op = TREE_OPERAND (op, 0);
9099 op = maybe_undo_parenthesized_ref (op);
9100 STRIP_ANY_LOCATION_WRAPPER (op);
9103 tree arg = CALL_EXPR_ARG (fn, 0);
9104 extract_op (arg);
9105 if (TREE_CODE (arg) == ADDR_EXPR)
9106 arg = TREE_OPERAND (arg, 0);
9107 tree type = TREE_TYPE (lhs);
9108 tree orig_lhs = lhs;
9109 extract_op (lhs);
9110 if (cp_tree_equal (lhs, arg))
9112 auto_diagnostic_group d;
9113 if (warning_at (loc, OPT_Wself_move,
9114 "moving %qE of type %qT to itself", orig_lhs, type))
9115 inform (loc, "remove %<std::move%> call");
9119 /* For use from the C common bits. */
9120 tree
9121 build_modify_expr (location_t location,
9122 tree lhs, tree /*lhs_origtype*/,
9123 enum tree_code modifycode,
9124 location_t /*rhs_location*/, tree rhs,
9125 tree /*rhs_origtype*/)
9127 return cp_build_modify_expr (location, lhs, modifycode, rhs,
9128 tf_warning_or_error);
9131 /* Build an assignment expression of lvalue LHS from value RHS.
9132 MODIFYCODE is the code for a binary operator that we use
9133 to combine the old value of LHS with RHS to get the new value.
9134 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9136 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9138 tree
9139 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9140 tree rhs, tsubst_flags_t complain)
9142 lhs = mark_lvalue_use_nonread (lhs);
9144 tree result = NULL_TREE;
9145 tree newrhs = rhs;
9146 tree lhstype = TREE_TYPE (lhs);
9147 tree olhs = lhs;
9148 tree olhstype = lhstype;
9149 bool plain_assign = (modifycode == NOP_EXPR);
9150 bool compound_side_effects_p = false;
9151 tree preeval = NULL_TREE;
9153 /* Avoid duplicate error messages from operands that had errors. */
9154 if (error_operand_p (lhs) || error_operand_p (rhs))
9155 return error_mark_node;
9157 while (TREE_CODE (lhs) == COMPOUND_EXPR)
9159 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9160 compound_side_effects_p = true;
9161 lhs = TREE_OPERAND (lhs, 1);
9164 /* Handle control structure constructs used as "lvalues". Note that we
9165 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9166 switch (TREE_CODE (lhs))
9168 /* Handle --foo = 5; as these are valid constructs in C++. */
9169 case PREDECREMENT_EXPR:
9170 case PREINCREMENT_EXPR:
9171 if (compound_side_effects_p)
9172 newrhs = rhs = stabilize_expr (rhs, &preeval);
9173 lhs = genericize_compound_lvalue (lhs);
9174 maybe_add_compound:
9175 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9176 and looked through the COMPOUND_EXPRs, readd them now around
9177 the resulting lhs. */
9178 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9180 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9181 tree *ptr = &TREE_OPERAND (lhs, 1);
9182 for (olhs = TREE_OPERAND (olhs, 1);
9183 TREE_CODE (olhs) == COMPOUND_EXPR;
9184 olhs = TREE_OPERAND (olhs, 1))
9186 *ptr = build2 (COMPOUND_EXPR, lhstype,
9187 TREE_OPERAND (olhs, 0), *ptr);
9188 ptr = &TREE_OPERAND (*ptr, 1);
9191 break;
9193 case MODIFY_EXPR:
9194 if (compound_side_effects_p)
9195 newrhs = rhs = stabilize_expr (rhs, &preeval);
9196 lhs = genericize_compound_lvalue (lhs);
9197 goto maybe_add_compound;
9199 case MIN_EXPR:
9200 case MAX_EXPR:
9201 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9202 when neither operand has side-effects. */
9203 if (!lvalue_or_else (lhs, lv_assign, complain))
9204 return error_mark_node;
9206 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9207 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9209 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9210 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9211 boolean_type_node,
9212 TREE_OPERAND (lhs, 0),
9213 TREE_OPERAND (lhs, 1)),
9214 TREE_OPERAND (lhs, 0),
9215 TREE_OPERAND (lhs, 1));
9216 gcc_fallthrough ();
9218 /* Handle (a ? b : c) used as an "lvalue". */
9219 case COND_EXPR:
9221 /* Produce (a ? (b = rhs) : (c = rhs))
9222 except that the RHS goes through a save-expr
9223 so the code to compute it is only emitted once. */
9224 if (VOID_TYPE_P (TREE_TYPE (rhs)))
9226 if (complain & tf_error)
9227 error_at (cp_expr_loc_or_loc (rhs, loc),
9228 "void value not ignored as it ought to be");
9229 return error_mark_node;
9232 rhs = stabilize_expr (rhs, &preeval);
9234 /* Check this here to avoid odd errors when trying to convert
9235 a throw to the type of the COND_EXPR. */
9236 if (!lvalue_or_else (lhs, lv_assign, complain))
9237 return error_mark_node;
9239 tree op1 = TREE_OPERAND (lhs, 1);
9240 if (TREE_CODE (op1) != THROW_EXPR)
9241 op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9242 /* When sanitizing undefined behavior, even when rhs doesn't need
9243 stabilization at this point, the sanitization might add extra
9244 SAVE_EXPRs in there and so make sure there is no tree sharing
9245 in the rhs, otherwise those SAVE_EXPRs will have initialization
9246 only in one of the two branches. */
9247 if (sanitize_flags_p (SANITIZE_UNDEFINED
9248 | SANITIZE_UNDEFINED_NONDEFAULT))
9249 rhs = unshare_expr (rhs);
9250 tree op2 = TREE_OPERAND (lhs, 2);
9251 if (TREE_CODE (op2) != THROW_EXPR)
9252 op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9253 tree cond = build_conditional_expr (input_location,
9254 TREE_OPERAND (lhs, 0), op1, op2,
9255 complain);
9257 if (cond == error_mark_node)
9258 return cond;
9259 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9260 and looked through the COMPOUND_EXPRs, readd them now around
9261 the resulting cond before adding the preevaluated rhs. */
9262 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9264 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9265 TREE_OPERAND (olhs, 0), cond);
9266 tree *ptr = &TREE_OPERAND (cond, 1);
9267 for (olhs = TREE_OPERAND (olhs, 1);
9268 TREE_CODE (olhs) == COMPOUND_EXPR;
9269 olhs = TREE_OPERAND (olhs, 1))
9271 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9272 TREE_OPERAND (olhs, 0), *ptr);
9273 ptr = &TREE_OPERAND (*ptr, 1);
9276 /* Make sure the code to compute the rhs comes out
9277 before the split. */
9278 result = cond;
9279 goto ret;
9282 default:
9283 lhs = olhs;
9284 break;
9287 if (modifycode == INIT_EXPR)
9289 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9290 /* Do the default thing. */;
9291 else if (TREE_CODE (rhs) == CONSTRUCTOR)
9293 /* Compound literal. */
9294 if (! same_type_p (TREE_TYPE (rhs), lhstype))
9295 /* Call convert to generate an error; see PR 11063. */
9296 rhs = convert (lhstype, rhs);
9297 result = cp_build_init_expr (lhs, rhs);
9298 TREE_SIDE_EFFECTS (result) = 1;
9299 goto ret;
9301 else if (! MAYBE_CLASS_TYPE_P (lhstype))
9302 /* Do the default thing. */;
9303 else
9305 releasing_vec rhs_vec = make_tree_vector_single (rhs);
9306 result = build_special_member_call (lhs, complete_ctor_identifier,
9307 &rhs_vec, lhstype, LOOKUP_NORMAL,
9308 complain);
9309 if (result == NULL_TREE)
9310 return error_mark_node;
9311 goto ret;
9314 else
9316 lhs = require_complete_type (lhs, complain);
9317 if (lhs == error_mark_node)
9318 return error_mark_node;
9320 if (modifycode == NOP_EXPR)
9322 maybe_warn_self_move (loc, lhs, rhs);
9324 if (c_dialect_objc ())
9326 result = objc_maybe_build_modify_expr (lhs, rhs);
9327 if (result)
9328 goto ret;
9331 /* `operator=' is not an inheritable operator. */
9332 if (! MAYBE_CLASS_TYPE_P (lhstype))
9333 /* Do the default thing. */;
9334 else
9336 result = build_new_op (input_location, MODIFY_EXPR,
9337 LOOKUP_NORMAL, lhs, rhs,
9338 make_node (NOP_EXPR), NULL_TREE,
9339 /*overload=*/NULL, complain);
9340 if (result == NULL_TREE)
9341 return error_mark_node;
9342 goto ret;
9344 lhstype = olhstype;
9346 else
9348 tree init = NULL_TREE;
9350 /* A binary op has been requested. Combine the old LHS
9351 value with the RHS producing the value we should actually
9352 store into the LHS. */
9353 gcc_assert (!((TYPE_REF_P (lhstype)
9354 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9355 || MAYBE_CLASS_TYPE_P (lhstype)));
9357 /* An expression of the form E1 op= E2. [expr.ass] says:
9358 "Such expressions are deprecated if E1 has volatile-qualified
9359 type and op is not one of the bitwise operators |, &, ^."
9360 We warn here rather than in cp_genericize_r because
9361 for compound assignments we are supposed to warn even if the
9362 assignment is a discarded-value expression. */
9363 if (modifycode != BIT_AND_EXPR
9364 && modifycode != BIT_IOR_EXPR
9365 && modifycode != BIT_XOR_EXPR
9366 && (TREE_THIS_VOLATILE (lhs) || CP_TYPE_VOLATILE_P (lhstype)))
9367 warning_at (loc, OPT_Wvolatile,
9368 "compound assignment with %<volatile%>-qualified left "
9369 "operand is deprecated");
9370 /* Preevaluate the RHS to make sure its evaluation is complete
9371 before the lvalue-to-rvalue conversion of the LHS:
9373 [expr.ass] With respect to an indeterminately-sequenced
9374 function call, the operation of a compound assignment is a
9375 single evaluation. [ Note: Therefore, a function call shall
9376 not intervene between the lvalue-to-rvalue conversion and the
9377 side effect associated with any single compound assignment
9378 operator. -- end note ] */
9379 lhs = cp_stabilize_reference (lhs);
9380 rhs = decay_conversion (rhs, complain);
9381 if (rhs == error_mark_node)
9382 return error_mark_node;
9383 rhs = stabilize_expr (rhs, &init);
9384 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9385 if (newrhs == error_mark_node)
9387 if (complain & tf_error)
9388 inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9389 modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9390 return error_mark_node;
9393 if (init)
9394 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9396 /* Now it looks like a plain assignment. */
9397 modifycode = NOP_EXPR;
9398 if (c_dialect_objc ())
9400 result = objc_maybe_build_modify_expr (lhs, newrhs);
9401 if (result)
9402 goto ret;
9405 gcc_assert (!TYPE_REF_P (lhstype));
9406 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9409 /* The left-hand side must be an lvalue. */
9410 if (!lvalue_or_else (lhs, lv_assign, complain))
9411 return error_mark_node;
9413 /* Warn about modifying something that is `const'. Don't warn if
9414 this is initialization. */
9415 if (modifycode != INIT_EXPR
9416 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9417 /* Functions are not modifiable, even though they are
9418 lvalues. */
9419 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9420 /* If it's an aggregate and any field is const, then it is
9421 effectively const. */
9422 || (CLASS_TYPE_P (lhstype)
9423 && C_TYPE_FIELDS_READONLY (lhstype))))
9425 if (complain & tf_error)
9426 cxx_readonly_error (loc, lhs, lv_assign);
9427 return error_mark_node;
9430 /* If storing into a structure or union member, it may have been given a
9431 lowered bitfield type. We need to convert to the declared type first,
9432 so retrieve it now. */
9434 olhstype = unlowered_expr_type (lhs);
9436 /* Convert new value to destination type. */
9438 if (TREE_CODE (lhstype) == ARRAY_TYPE)
9440 int from_array;
9442 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9444 if (modifycode != INIT_EXPR)
9446 if (complain & tf_error)
9447 error_at (loc,
9448 "assigning to an array from an initializer list");
9449 return error_mark_node;
9451 if (check_array_initializer (lhs, lhstype, newrhs))
9452 return error_mark_node;
9453 newrhs = digest_init (lhstype, newrhs, complain);
9454 if (newrhs == error_mark_node)
9455 return error_mark_node;
9458 /* C++11 8.5/17: "If the destination type is an array of characters,
9459 an array of char16_t, an array of char32_t, or an array of wchar_t,
9460 and the initializer is a string literal...". */
9461 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9462 == STRING_CST)
9463 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9464 && modifycode == INIT_EXPR)
9466 newrhs = digest_init (lhstype, newrhs, complain);
9467 if (newrhs == error_mark_node)
9468 return error_mark_node;
9471 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9472 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9474 if (complain & tf_error)
9475 error_at (loc, "incompatible types in assignment of %qT to %qT",
9476 TREE_TYPE (rhs), lhstype);
9477 return error_mark_node;
9480 /* Allow array assignment in compiler-generated code. */
9481 else if (!current_function_decl
9482 || !DECL_DEFAULTED_FN (current_function_decl))
9484 /* This routine is used for both initialization and assignment.
9485 Make sure the diagnostic message differentiates the context. */
9486 if (complain & tf_error)
9488 if (modifycode == INIT_EXPR)
9489 error_at (loc, "array used as initializer");
9490 else
9491 error_at (loc, "invalid array assignment");
9493 return error_mark_node;
9496 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9497 ? 1 + (modifycode != INIT_EXPR): 0;
9498 result = build_vec_init (lhs, NULL_TREE, newrhs,
9499 /*explicit_value_init_p=*/false,
9500 from_array, complain);
9501 goto ret;
9504 if (modifycode == INIT_EXPR)
9505 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9506 LOOKUP_ONLYCONVERTING. */
9507 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9508 ICR_INIT, NULL_TREE, 0,
9509 complain | tf_no_cleanup);
9510 else
9511 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9512 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9514 if (!same_type_p (lhstype, olhstype))
9515 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9517 if (modifycode != INIT_EXPR)
9519 if (TREE_CODE (newrhs) == CALL_EXPR
9520 && TYPE_NEEDS_CONSTRUCTING (lhstype))
9521 newrhs = build_cplus_new (lhstype, newrhs, complain);
9523 /* Can't initialize directly from a TARGET_EXPR, since that would
9524 cause the lhs to be constructed twice, and possibly result in
9525 accidental self-initialization. So we force the TARGET_EXPR to be
9526 expanded without a target. */
9527 if (TREE_CODE (newrhs) == TARGET_EXPR)
9528 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9529 TREE_OPERAND (newrhs, 0));
9532 if (newrhs == error_mark_node)
9533 return error_mark_node;
9535 if (c_dialect_objc () && flag_objc_gc)
9537 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9539 if (result)
9540 goto ret;
9543 result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9544 lhstype, lhs, newrhs);
9545 if (modifycode == INIT_EXPR)
9546 set_target_expr_eliding (newrhs);
9548 TREE_SIDE_EFFECTS (result) = 1;
9549 if (!plain_assign)
9550 suppress_warning (result, OPT_Wparentheses);
9552 ret:
9553 if (preeval)
9554 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
9555 return result;
9558 cp_expr
9559 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9560 tree rhs, tree lookups, tsubst_flags_t complain)
9562 tree orig_lhs = lhs;
9563 tree orig_rhs = rhs;
9564 tree overload = NULL_TREE;
9566 if (lhs == error_mark_node || rhs == error_mark_node)
9567 return cp_expr (error_mark_node, loc);
9569 if (processing_template_decl)
9571 if (modifycode == NOP_EXPR
9572 || type_dependent_expression_p (lhs)
9573 || type_dependent_expression_p (rhs))
9575 tree op = build_min_nt_loc (loc, modifycode, NULL_TREE, NULL_TREE);
9576 tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
9577 if (modifycode != NOP_EXPR)
9578 TREE_TYPE (rval)
9579 = build_dependent_operator_type (lookups, modifycode, true);
9580 return rval;
9583 lhs = build_non_dependent_expr (lhs);
9584 rhs = build_non_dependent_expr (rhs);
9587 if (modifycode != NOP_EXPR)
9589 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
9590 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
9591 lhs, rhs, op, lookups, &overload, complain);
9592 if (rval)
9594 if (rval == error_mark_node)
9595 return rval;
9596 suppress_warning (rval /* What warning? */);
9597 if (processing_template_decl)
9599 if (overload != NULL_TREE)
9600 return (build_min_non_dep_op_overload
9601 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
9603 return (build_min_non_dep
9604 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
9606 return rval;
9609 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
9612 /* Helper function for get_delta_difference which assumes FROM is a base
9613 class of TO. Returns a delta for the conversion of pointer-to-member
9614 of FROM to pointer-to-member of TO. If the conversion is invalid and
9615 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9616 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9617 If C_CAST_P is true, this conversion is taking place as part of a
9618 C-style cast. */
9620 static tree
9621 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
9622 tsubst_flags_t complain)
9624 tree binfo;
9625 base_kind kind;
9627 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
9628 &kind, complain);
9630 if (binfo == error_mark_node)
9632 if (!(complain & tf_error))
9633 return error_mark_node;
9635 inform (input_location, " in pointer to member function conversion");
9636 return size_zero_node;
9638 else if (binfo)
9640 if (kind != bk_via_virtual)
9641 return BINFO_OFFSET (binfo);
9642 else
9643 /* FROM is a virtual base class of TO. Issue an error or warning
9644 depending on whether or not this is a reinterpret cast. */
9646 if (!(complain & tf_error))
9647 return error_mark_node;
9649 error ("pointer to member conversion via virtual base %qT",
9650 BINFO_TYPE (binfo_from_vbase (binfo)));
9652 return size_zero_node;
9655 else
9656 return NULL_TREE;
9659 /* Get difference in deltas for different pointer to member function
9660 types. If the conversion is invalid and tf_error is not set in
9661 COMPLAIN, returns error_mark_node, otherwise returns an integer
9662 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9663 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9664 conversions as well. If C_CAST_P is true this conversion is taking
9665 place as part of a C-style cast.
9667 Note that the naming of FROM and TO is kind of backwards; the return
9668 value is what we add to a TO in order to get a FROM. They are named
9669 this way because we call this function to find out how to convert from
9670 a pointer to member of FROM to a pointer to member of TO. */
9672 static tree
9673 get_delta_difference (tree from, tree to,
9674 bool allow_inverse_p,
9675 bool c_cast_p, tsubst_flags_t complain)
9677 tree result;
9679 if (same_type_ignoring_top_level_qualifiers_p (from, to))
9680 /* Pointer to member of incomplete class is permitted*/
9681 result = size_zero_node;
9682 else
9683 result = get_delta_difference_1 (from, to, c_cast_p, complain);
9685 if (result == error_mark_node)
9686 return error_mark_node;
9688 if (!result)
9690 if (!allow_inverse_p)
9692 if (!(complain & tf_error))
9693 return error_mark_node;
9695 error_not_base_type (from, to);
9696 inform (input_location, " in pointer to member conversion");
9697 result = size_zero_node;
9699 else
9701 result = get_delta_difference_1 (to, from, c_cast_p, complain);
9703 if (result == error_mark_node)
9704 return error_mark_node;
9706 if (result)
9707 result = size_diffop_loc (input_location,
9708 size_zero_node, result);
9709 else
9711 if (!(complain & tf_error))
9712 return error_mark_node;
9714 error_not_base_type (from, to);
9715 inform (input_location, " in pointer to member conversion");
9716 result = size_zero_node;
9721 return convert_to_integer (ptrdiff_type_node, result);
9724 /* Return a constructor for the pointer-to-member-function TYPE using
9725 the other components as specified. */
9727 tree
9728 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
9730 tree u = NULL_TREE;
9731 tree delta_field;
9732 tree pfn_field;
9733 vec<constructor_elt, va_gc> *v;
9735 /* Pull the FIELD_DECLs out of the type. */
9736 pfn_field = TYPE_FIELDS (type);
9737 delta_field = DECL_CHAIN (pfn_field);
9739 /* Make sure DELTA has the type we want. */
9740 delta = convert_and_check (input_location, delta_type_node, delta);
9742 /* Convert to the correct target type if necessary. */
9743 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
9745 /* Finish creating the initializer. */
9746 vec_alloc (v, 2);
9747 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
9748 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
9749 u = build_constructor (type, v);
9750 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
9751 TREE_STATIC (u) = (TREE_CONSTANT (u)
9752 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
9753 != NULL_TREE)
9754 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
9755 != NULL_TREE));
9756 return u;
9759 /* Build a constructor for a pointer to member function. It can be
9760 used to initialize global variables, local variable, or used
9761 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
9762 want to be.
9764 If FORCE is nonzero, then force this conversion, even if
9765 we would rather not do it. Usually set when using an explicit
9766 cast. A C-style cast is being processed iff C_CAST_P is true.
9768 Return error_mark_node, if something goes wrong. */
9770 tree
9771 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
9772 tsubst_flags_t complain)
9774 tree fn;
9775 tree pfn_type;
9776 tree to_type;
9778 if (error_operand_p (pfn))
9779 return error_mark_node;
9781 pfn_type = TREE_TYPE (pfn);
9782 to_type = build_ptrmemfunc_type (type);
9784 /* Handle multiple conversions of pointer to member functions. */
9785 if (TYPE_PTRMEMFUNC_P (pfn_type))
9787 tree delta = NULL_TREE;
9788 tree npfn = NULL_TREE;
9789 tree n;
9791 if (!force
9792 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
9793 LOOKUP_NORMAL, complain))
9795 if (complain & tf_error)
9796 error ("invalid conversion to type %qT from type %qT",
9797 to_type, pfn_type);
9798 else
9799 return error_mark_node;
9802 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
9803 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
9804 force,
9805 c_cast_p, complain);
9806 if (n == error_mark_node)
9807 return error_mark_node;
9809 /* We don't have to do any conversion to convert a
9810 pointer-to-member to its own type. But, we don't want to
9811 just return a PTRMEM_CST if there's an explicit cast; that
9812 cast should make the expression an invalid template argument. */
9813 if (TREE_CODE (pfn) != PTRMEM_CST)
9815 if (same_type_p (to_type, pfn_type))
9816 return pfn;
9817 else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
9818 return build_reinterpret_cast (input_location, to_type, pfn,
9819 complain);
9822 if (TREE_SIDE_EFFECTS (pfn))
9823 pfn = save_expr (pfn);
9825 /* Obtain the function pointer and the current DELTA. */
9826 if (TREE_CODE (pfn) == PTRMEM_CST)
9827 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
9828 else
9830 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
9831 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
9834 /* Just adjust the DELTA field. */
9835 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9836 (TREE_TYPE (delta), ptrdiff_type_node));
9837 if (!integer_zerop (n))
9839 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
9840 n = cp_build_binary_op (input_location,
9841 LSHIFT_EXPR, n, integer_one_node,
9842 complain);
9843 delta = cp_build_binary_op (input_location,
9844 PLUS_EXPR, delta, n, complain);
9846 return build_ptrmemfunc1 (to_type, delta, npfn);
9849 /* Handle null pointer to member function conversions. */
9850 if (null_ptr_cst_p (pfn))
9852 pfn = cp_build_c_cast (input_location,
9853 TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
9854 pfn, complain);
9855 return build_ptrmemfunc1 (to_type,
9856 integer_zero_node,
9857 pfn);
9860 if (type_unknown_p (pfn))
9861 return instantiate_type (type, pfn, complain);
9863 fn = TREE_OPERAND (pfn, 0);
9864 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9865 /* In a template, we will have preserved the
9866 OFFSET_REF. */
9867 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
9868 return make_ptrmem_cst (to_type, fn);
9871 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
9872 given by CST.
9874 ??? There is no consistency as to the types returned for the above
9875 values. Some code acts as if it were a sizetype and some as if it were
9876 integer_type_node. */
9878 void
9879 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
9881 tree type = TREE_TYPE (cst);
9882 tree fn = PTRMEM_CST_MEMBER (cst);
9883 tree ptr_class, fn_class;
9885 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9887 /* The class that the function belongs to. */
9888 fn_class = DECL_CONTEXT (fn);
9890 /* The class that we're creating a pointer to member of. */
9891 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
9893 /* First, calculate the adjustment to the function's class. */
9894 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
9895 /*c_cast_p=*/0, tf_warning_or_error);
9897 if (!DECL_VIRTUAL_P (fn))
9899 tree t = build_addr_func (fn, tf_warning_or_error);
9900 if (TREE_CODE (t) == ADDR_EXPR)
9901 SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
9902 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
9904 else
9906 /* If we're dealing with a virtual function, we have to adjust 'this'
9907 again, to point to the base which provides the vtable entry for
9908 fn; the call will do the opposite adjustment. */
9909 tree orig_class = DECL_CONTEXT (fn);
9910 tree binfo = binfo_or_else (orig_class, fn_class);
9911 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9912 *delta, BINFO_OFFSET (binfo));
9914 /* We set PFN to the vtable offset at which the function can be
9915 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
9916 case delta is shifted left, and then incremented). */
9917 *pfn = DECL_VINDEX (fn);
9918 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
9919 TYPE_SIZE_UNIT (vtable_entry_type));
9921 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
9923 case ptrmemfunc_vbit_in_pfn:
9924 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
9925 integer_one_node);
9926 break;
9928 case ptrmemfunc_vbit_in_delta:
9929 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
9930 *delta, integer_one_node);
9931 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9932 *delta, integer_one_node);
9933 break;
9935 default:
9936 gcc_unreachable ();
9939 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
9943 /* Return an expression for PFN from the pointer-to-member function
9944 given by T. */
9946 static tree
9947 pfn_from_ptrmemfunc (tree t)
9949 if (TREE_CODE (t) == PTRMEM_CST)
9951 tree delta;
9952 tree pfn;
9954 expand_ptrmemfunc_cst (t, &delta, &pfn);
9955 if (pfn)
9956 return pfn;
9959 return build_ptrmemfunc_access_expr (t, pfn_identifier);
9962 /* Return an expression for DELTA from the pointer-to-member function
9963 given by T. */
9965 static tree
9966 delta_from_ptrmemfunc (tree t)
9968 if (TREE_CODE (t) == PTRMEM_CST)
9970 tree delta;
9971 tree pfn;
9973 expand_ptrmemfunc_cst (t, &delta, &pfn);
9974 if (delta)
9975 return delta;
9978 return build_ptrmemfunc_access_expr (t, delta_identifier);
9981 /* Convert value RHS to type TYPE as preparation for an assignment to
9982 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
9983 implicit conversion is. If FNDECL is non-NULL, we are doing the
9984 conversion in order to pass the PARMNUMth argument of FNDECL.
9985 If FNDECL is NULL, we are doing the conversion in function pointer
9986 argument passing, conversion in initialization, etc. */
9988 static tree
9989 convert_for_assignment (tree type, tree rhs,
9990 impl_conv_rhs errtype, tree fndecl, int parmnum,
9991 tsubst_flags_t complain, int flags)
9993 tree rhstype;
9994 enum tree_code coder;
9996 location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
9997 bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
9998 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
9999 but preserve location wrappers. */
10000 if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10001 && !location_wrapper_p (rhs))
10002 rhs = TREE_OPERAND (rhs, 0);
10004 /* Handle [dcl.init.list] direct-list-initialization from
10005 single element of enumeration with a fixed underlying type. */
10006 if (is_direct_enum_init (type, rhs))
10008 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10009 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10011 warning_sentinel w (warn_useless_cast);
10012 warning_sentinel w2 (warn_ignored_qualifiers);
10013 rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10015 else
10016 rhs = error_mark_node;
10019 rhstype = TREE_TYPE (rhs);
10020 coder = TREE_CODE (rhstype);
10022 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10023 && vector_types_convertible_p (type, rhstype, true))
10025 rhs = mark_rvalue_use (rhs);
10026 return convert (type, rhs);
10029 if (rhs == error_mark_node || rhstype == error_mark_node)
10030 return error_mark_node;
10031 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10032 return error_mark_node;
10034 /* The RHS of an assignment cannot have void type. */
10035 if (coder == VOID_TYPE)
10037 if (complain & tf_error)
10038 error_at (rhs_loc, "void value not ignored as it ought to be");
10039 return error_mark_node;
10042 if (c_dialect_objc ())
10044 int parmno;
10045 tree selector;
10046 tree rname = fndecl;
10048 switch (errtype)
10050 case ICR_ASSIGN:
10051 parmno = -1;
10052 break;
10053 case ICR_INIT:
10054 parmno = -2;
10055 break;
10056 default:
10057 selector = objc_message_selector ();
10058 parmno = parmnum;
10059 if (selector && parmno > 1)
10061 rname = selector;
10062 parmno -= 1;
10066 if (objc_compare_types (type, rhstype, parmno, rname))
10068 rhs = mark_rvalue_use (rhs);
10069 return convert (type, rhs);
10073 /* [expr.ass]
10075 The expression is implicitly converted (clause _conv_) to the
10076 cv-unqualified type of the left operand.
10078 We allow bad conversions here because by the time we get to this point
10079 we are committed to doing the conversion. If we end up doing a bad
10080 conversion, convert_like will complain. */
10081 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10083 /* When -Wno-pmf-conversions is use, we just silently allow
10084 conversions from pointers-to-members to plain pointers. If
10085 the conversion doesn't work, cp_convert will complain. */
10086 if (!warn_pmf2ptr
10087 && TYPE_PTR_P (type)
10088 && TYPE_PTRMEMFUNC_P (rhstype))
10089 rhs = cp_convert (strip_top_quals (type), rhs, complain);
10090 else
10092 if (complain & tf_error)
10094 /* If the right-hand side has unknown type, then it is an
10095 overloaded function. Call instantiate_type to get error
10096 messages. */
10097 if (rhstype == unknown_type_node)
10099 tree r = instantiate_type (type, rhs, tf_warning_or_error);
10100 /* -fpermissive might allow this; recurse. */
10101 if (!seen_error ())
10102 return convert_for_assignment (type, r, errtype, fndecl,
10103 parmnum, complain, flags);
10105 else if (fndecl)
10106 complain_about_bad_argument (rhs_loc,
10107 rhstype, type,
10108 fndecl, parmnum);
10109 else
10111 range_label_for_type_mismatch label (rhstype, type);
10112 gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
10113 switch (errtype)
10115 case ICR_DEFAULT_ARGUMENT:
10116 error_at (&richloc,
10117 "cannot convert %qH to %qI in default argument",
10118 rhstype, type);
10119 break;
10120 case ICR_ARGPASS:
10121 error_at (&richloc,
10122 "cannot convert %qH to %qI in argument passing",
10123 rhstype, type);
10124 break;
10125 case ICR_CONVERTING:
10126 error_at (&richloc, "cannot convert %qH to %qI",
10127 rhstype, type);
10128 break;
10129 case ICR_INIT:
10130 error_at (&richloc,
10131 "cannot convert %qH to %qI in initialization",
10132 rhstype, type);
10133 break;
10134 case ICR_RETURN:
10135 error_at (&richloc, "cannot convert %qH to %qI in return",
10136 rhstype, type);
10137 break;
10138 case ICR_ASSIGN:
10139 error_at (&richloc,
10140 "cannot convert %qH to %qI in assignment",
10141 rhstype, type);
10142 break;
10143 default:
10144 gcc_unreachable();
10147 if (TYPE_PTR_P (rhstype)
10148 && TYPE_PTR_P (type)
10149 && CLASS_TYPE_P (TREE_TYPE (rhstype))
10150 && CLASS_TYPE_P (TREE_TYPE (type))
10151 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10152 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10153 (TREE_TYPE (rhstype))),
10154 "class type %qT is incomplete", TREE_TYPE (rhstype));
10156 return error_mark_node;
10159 if (warn_suggest_attribute_format)
10161 const enum tree_code codel = TREE_CODE (type);
10162 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10163 && coder == codel
10164 && check_missing_format_attribute (type, rhstype)
10165 && (complain & tf_warning))
10166 switch (errtype)
10168 case ICR_ARGPASS:
10169 case ICR_DEFAULT_ARGUMENT:
10170 if (fndecl)
10171 warning (OPT_Wsuggest_attribute_format,
10172 "parameter %qP of %qD might be a candidate "
10173 "for a format attribute", parmnum, fndecl);
10174 else
10175 warning (OPT_Wsuggest_attribute_format,
10176 "parameter might be a candidate "
10177 "for a format attribute");
10178 break;
10179 case ICR_CONVERTING:
10180 warning (OPT_Wsuggest_attribute_format,
10181 "target of conversion might be a candidate "
10182 "for a format attribute");
10183 break;
10184 case ICR_INIT:
10185 warning (OPT_Wsuggest_attribute_format,
10186 "target of initialization might be a candidate "
10187 "for a format attribute");
10188 break;
10189 case ICR_RETURN:
10190 warning (OPT_Wsuggest_attribute_format,
10191 "return type might be a candidate "
10192 "for a format attribute");
10193 break;
10194 case ICR_ASSIGN:
10195 warning (OPT_Wsuggest_attribute_format,
10196 "left-hand side of assignment might be a candidate "
10197 "for a format attribute");
10198 break;
10199 default:
10200 gcc_unreachable();
10204 /* If -Wparentheses, warn about a = b = c when a has type bool and b
10205 does not. */
10206 if (warn_parentheses
10207 && TREE_CODE (type) == BOOLEAN_TYPE
10208 && TREE_CODE (rhs) == MODIFY_EXPR
10209 && !warning_suppressed_p (rhs, OPT_Wparentheses)
10210 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
10211 && (complain & tf_warning)
10212 && warning_at (rhs_loc, OPT_Wparentheses,
10213 "suggest parentheses around assignment used as "
10214 "truth value"))
10215 suppress_warning (rhs, OPT_Wparentheses);
10217 if (complain & tf_warning)
10218 warn_for_address_or_pointer_of_packed_member (type, rhs);
10220 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10221 complain, flags);
10224 /* Convert RHS to be of type TYPE.
10225 If EXP is nonzero, it is the target of the initialization.
10226 ERRTYPE indicates what kind of error the implicit conversion is.
10228 Two major differences between the behavior of
10229 `convert_for_assignment' and `convert_for_initialization'
10230 are that references are bashed in the former, while
10231 copied in the latter, and aggregates are assigned in
10232 the former (operator=) while initialized in the
10233 latter (X(X&)).
10235 If using constructor make sure no conversion operator exists, if one does
10236 exist, an ambiguity exists. */
10238 tree
10239 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10240 impl_conv_rhs errtype, tree fndecl, int parmnum,
10241 tsubst_flags_t complain)
10243 enum tree_code codel = TREE_CODE (type);
10244 tree rhstype;
10245 enum tree_code coder;
10247 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10248 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10249 if (TREE_CODE (rhs) == NOP_EXPR
10250 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10251 && codel != REFERENCE_TYPE)
10252 rhs = TREE_OPERAND (rhs, 0);
10254 if (type == error_mark_node
10255 || rhs == error_mark_node
10256 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10257 return error_mark_node;
10259 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10261 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10262 && TREE_CODE (type) != ARRAY_TYPE
10263 && (!TYPE_REF_P (type)
10264 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10265 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10266 && !TYPE_REFFN_P (type))
10267 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10268 rhs = decay_conversion (rhs, complain);
10270 rhstype = TREE_TYPE (rhs);
10271 coder = TREE_CODE (rhstype);
10273 if (coder == ERROR_MARK)
10274 return error_mark_node;
10276 /* We accept references to incomplete types, so we can
10277 return here before checking if RHS is of complete type. */
10279 if (codel == REFERENCE_TYPE)
10281 auto_diagnostic_group d;
10282 /* This should eventually happen in convert_arguments. */
10283 int savew = 0, savee = 0;
10285 if (fndecl)
10286 savew = warningcount + werrorcount, savee = errorcount;
10287 rhs = initialize_reference (type, rhs, flags, complain);
10289 if (fndecl
10290 && (warningcount + werrorcount > savew || errorcount > savee))
10291 inform (get_fndecl_argument_location (fndecl, parmnum),
10292 "in passing argument %P of %qD", parmnum, fndecl);
10293 return rhs;
10296 if (exp != 0)
10297 exp = require_complete_type (exp, complain);
10298 if (exp == error_mark_node)
10299 return error_mark_node;
10301 type = complete_type (type);
10303 if (DIRECT_INIT_EXPR_P (type, rhs))
10304 /* Don't try to do copy-initialization if we already have
10305 direct-initialization. */
10306 return rhs;
10308 if (MAYBE_CLASS_TYPE_P (type))
10309 return perform_implicit_conversion_flags (type, rhs, complain, flags);
10311 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10312 complain, flags);
10315 /* If RETVAL is the address of, or a reference to, a local variable or
10316 temporary give an appropriate warning and return true. */
10318 static bool
10319 maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10321 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10322 tree whats_returned = fold_for_warn (retval);
10323 if (!loc)
10324 loc = cp_expr_loc_or_input_loc (retval);
10326 for (;;)
10328 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10329 whats_returned = TREE_OPERAND (whats_returned, 1);
10330 else if (CONVERT_EXPR_P (whats_returned)
10331 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10332 whats_returned = TREE_OPERAND (whats_returned, 0);
10333 else
10334 break;
10337 if (TREE_CODE (whats_returned) == TARGET_EXPR
10338 && is_std_init_list (TREE_TYPE (whats_returned)))
10340 tree init = TARGET_EXPR_INITIAL (whats_returned);
10341 if (TREE_CODE (init) == CONSTRUCTOR)
10342 /* Pull out the array address. */
10343 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10344 else if (TREE_CODE (init) == INDIRECT_REF)
10345 /* The source of a trivial copy looks like *(T*)&var. */
10346 whats_returned = TREE_OPERAND (init, 0);
10347 else
10348 return false;
10349 STRIP_NOPS (whats_returned);
10352 /* As a special case, we handle a call to std::move or std::forward. */
10353 if (TREE_CODE (whats_returned) == CALL_EXPR
10354 && (is_std_move_p (whats_returned)
10355 || is_std_forward_p (whats_returned)))
10357 tree arg = CALL_EXPR_ARG (whats_returned, 0);
10358 return maybe_warn_about_returning_address_of_local (arg, loc);
10361 if (TREE_CODE (whats_returned) != ADDR_EXPR)
10362 return false;
10363 whats_returned = TREE_OPERAND (whats_returned, 0);
10365 while (TREE_CODE (whats_returned) == COMPONENT_REF
10366 || TREE_CODE (whats_returned) == ARRAY_REF)
10367 whats_returned = TREE_OPERAND (whats_returned, 0);
10369 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10370 || TREE_CODE (whats_returned) == TARGET_EXPR)
10372 if (TYPE_REF_P (valtype))
10373 warning_at (loc, OPT_Wreturn_local_addr,
10374 "returning reference to temporary");
10375 else if (is_std_init_list (valtype))
10376 warning_at (loc, OPT_Winit_list_lifetime,
10377 "returning temporary %<initializer_list%> does not extend "
10378 "the lifetime of the underlying array");
10379 return true;
10382 STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10384 if (DECL_P (whats_returned)
10385 && DECL_NAME (whats_returned)
10386 && DECL_FUNCTION_SCOPE_P (whats_returned)
10387 && !is_capture_proxy (whats_returned)
10388 && !(TREE_STATIC (whats_returned)
10389 || TREE_PUBLIC (whats_returned)))
10391 if (VAR_P (whats_returned)
10392 && DECL_DECOMPOSITION_P (whats_returned)
10393 && DECL_DECOMP_BASE (whats_returned)
10394 && DECL_HAS_VALUE_EXPR_P (whats_returned))
10396 /* When returning address of a structured binding, if the structured
10397 binding is not a reference, continue normally, if it is a
10398 reference, recurse on the initializer of the structured
10399 binding. */
10400 tree base = DECL_DECOMP_BASE (whats_returned);
10401 if (TYPE_REF_P (TREE_TYPE (base)))
10403 if (tree init = DECL_INITIAL (base))
10404 return maybe_warn_about_returning_address_of_local (init, loc);
10405 else
10406 return false;
10409 bool w = false;
10410 auto_diagnostic_group d;
10411 if (TYPE_REF_P (valtype))
10412 w = warning_at (loc, OPT_Wreturn_local_addr,
10413 "reference to local variable %qD returned",
10414 whats_returned);
10415 else if (is_std_init_list (valtype))
10416 w = warning_at (loc, OPT_Winit_list_lifetime,
10417 "returning local %<initializer_list%> variable %qD "
10418 "does not extend the lifetime of the underlying array",
10419 whats_returned);
10420 else if (POINTER_TYPE_P (valtype)
10421 && TREE_CODE (whats_returned) == LABEL_DECL)
10422 w = warning_at (loc, OPT_Wreturn_local_addr,
10423 "address of label %qD returned",
10424 whats_returned);
10425 else if (POINTER_TYPE_P (valtype))
10426 w = warning_at (loc, OPT_Wreturn_local_addr,
10427 "address of local variable %qD returned",
10428 whats_returned);
10429 if (w)
10430 inform (DECL_SOURCE_LOCATION (whats_returned),
10431 "declared here");
10432 return true;
10435 return false;
10438 /* Returns true if DECL is in the std namespace. */
10440 bool
10441 decl_in_std_namespace_p (tree decl)
10443 while (decl)
10445 decl = decl_namespace_context (decl);
10446 if (DECL_NAMESPACE_STD_P (decl))
10447 return true;
10448 /* Allow inline namespaces inside of std namespace, e.g. with
10449 --enable-symvers=gnu-versioned-namespace std::forward would be
10450 actually std::_8::forward. */
10451 if (!DECL_NAMESPACE_INLINE_P (decl))
10452 return false;
10453 decl = CP_DECL_CONTEXT (decl);
10455 return false;
10458 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10460 static bool
10461 is_std_forward_p (tree fn)
10463 /* std::forward only takes one argument. */
10464 if (call_expr_nargs (fn) != 1)
10465 return false;
10467 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10468 if (!decl_in_std_namespace_p (fndecl))
10469 return false;
10471 tree name = DECL_NAME (fndecl);
10472 return name && id_equal (name, "forward");
10475 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10477 static bool
10478 is_std_move_p (tree fn)
10480 /* std::move only takes one argument. */
10481 if (call_expr_nargs (fn) != 1)
10482 return false;
10484 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10485 if (!decl_in_std_namespace_p (fndecl))
10486 return false;
10488 tree name = DECL_NAME (fndecl);
10489 return name && id_equal (name, "move");
10492 /* Returns true if RETVAL is a good candidate for the NRVO as per
10493 [class.copy.elision]. FUNCTYPE is the type the function is declared
10494 to return. */
10496 static bool
10497 can_do_nrvo_p (tree retval, tree functype)
10499 if (functype == error_mark_node)
10500 return false;
10501 if (retval)
10502 STRIP_ANY_LOCATION_WRAPPER (retval);
10503 tree result = DECL_RESULT (current_function_decl);
10504 return (retval != NULL_TREE
10505 && !processing_template_decl
10506 /* Must be a local, automatic variable. */
10507 && VAR_P (retval)
10508 && DECL_CONTEXT (retval) == current_function_decl
10509 && !TREE_STATIC (retval)
10510 /* And not a lambda or anonymous union proxy. */
10511 && !DECL_HAS_VALUE_EXPR_P (retval)
10512 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10513 /* The cv-unqualified type of the returned value must be the
10514 same as the cv-unqualified return type of the
10515 function. */
10516 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10517 TYPE_MAIN_VARIANT (functype))
10518 /* And the returned value must be non-volatile. */
10519 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10522 /* Like can_do_nrvo_p, but we check if we're trying to move a class
10523 prvalue. */
10525 static bool
10526 can_elide_copy_prvalue_p (tree retval, tree functype)
10528 if (functype == error_mark_node)
10529 return false;
10530 if (retval)
10531 STRIP_ANY_LOCATION_WRAPPER (retval);
10532 return (retval != NULL_TREE
10533 && !glvalue_p (retval)
10534 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10535 TYPE_MAIN_VARIANT (functype))
10536 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10539 /* If we should treat RETVAL, an expression being returned, as if it were
10540 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10541 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10542 context (rather than throw). */
10544 tree
10545 treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10547 if (cxx_dialect == cxx98)
10548 return NULL_TREE;
10550 tree retval = expr;
10551 STRIP_ANY_LOCATION_WRAPPER (retval);
10552 if (REFERENCE_REF_P (retval))
10553 retval = TREE_OPERAND (retval, 0);
10555 /* An implicitly movable entity is a variable of automatic storage duration
10556 that is either a non-volatile object or (C++20) an rvalue reference to a
10557 non-volatile object type. */
10558 if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
10559 || TREE_CODE (retval) == PARM_DECL)
10560 && !TREE_STATIC (retval)
10561 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
10562 && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
10563 || (cxx_dialect >= cxx20
10564 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
10565 return NULL_TREE;
10567 /* If the expression in a return or co_return statement is a (possibly
10568 parenthesized) id-expression that names an implicitly movable entity
10569 declared in the body or parameter-declaration-clause of the innermost
10570 enclosing function or lambda-expression, */
10571 if (DECL_CONTEXT (retval) != current_function_decl)
10572 return NULL_TREE;
10573 if (return_p)
10574 return set_implicit_rvalue_p (move (expr));
10576 /* if the operand of a throw-expression is a (possibly parenthesized)
10577 id-expression that names an implicitly movable entity whose scope does not
10578 extend beyond the compound-statement of the innermost try-block or
10579 function-try-block (if any) whose compound-statement or ctor-initializer
10580 encloses the throw-expression, */
10582 /* C++20 added move on throw of parms. */
10583 if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
10584 return NULL_TREE;
10586 for (cp_binding_level *b = current_binding_level;
10587 ; b = b->level_chain)
10589 for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
10590 if (decl == retval)
10591 return set_implicit_rvalue_p (move (expr));
10592 if (b->kind == sk_function_parms || b->kind == sk_try)
10593 return NULL_TREE;
10597 /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10598 is true). EXPR is the std::move expression; TYPE is the type of the object
10599 being initialized. */
10601 void
10602 maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
10604 if (!(warn_pessimizing_move || warn_redundant_move))
10605 return;
10607 const location_t loc = cp_expr_loc_or_input_loc (expr);
10609 /* C++98 doesn't know move. */
10610 if (cxx_dialect < cxx11)
10611 return;
10613 /* Wait until instantiation time, since we can't gauge if we should do
10614 the NRVO until then. */
10615 if (processing_template_decl)
10616 return;
10618 /* This is only interesting for class types. */
10619 if (!CLASS_TYPE_P (type))
10620 return;
10622 bool wrapped_p = false;
10623 /* A a = std::move (A()); */
10624 if (TREE_CODE (expr) == TREE_LIST)
10626 if (list_length (expr) == 1)
10628 expr = TREE_VALUE (expr);
10629 wrapped_p = true;
10631 else
10632 return;
10634 /* A a = {std::move (A())};
10635 A a{std::move (A())}; */
10636 else if (TREE_CODE (expr) == CONSTRUCTOR)
10638 if (CONSTRUCTOR_NELTS (expr) == 1)
10640 expr = CONSTRUCTOR_ELT (expr, 0)->value;
10641 wrapped_p = true;
10643 else
10644 return;
10647 /* First, check if this is a call to std::move. */
10648 if (!REFERENCE_REF_P (expr)
10649 || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
10650 return;
10651 tree fn = TREE_OPERAND (expr, 0);
10652 if (!is_std_move_p (fn))
10653 return;
10654 tree arg = CALL_EXPR_ARG (fn, 0);
10655 if (TREE_CODE (arg) != NOP_EXPR)
10656 return;
10657 /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
10658 and implicitly-movable warnings. */
10659 if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
10661 arg = TREE_OPERAND (arg, 0);
10662 arg = TREE_OPERAND (arg, 0);
10663 arg = convert_from_reference (arg);
10664 if (can_elide_copy_prvalue_p (arg, type))
10666 auto_diagnostic_group d;
10667 if (warning_at (loc, OPT_Wpessimizing_move,
10668 "moving a temporary object prevents copy elision"))
10669 inform (loc, "remove %<std::move%> call");
10671 /* The rest of the warnings is only relevant for when we are returning
10672 from a function. */
10673 if (!return_p)
10674 return;
10676 tree moved;
10677 /* Warn if we could do copy elision were it not for the move. */
10678 if (can_do_nrvo_p (arg, type))
10680 auto_diagnostic_group d;
10681 if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
10682 && warning_at (loc, OPT_Wpessimizing_move,
10683 "moving a local object in a return statement "
10684 "prevents copy elision"))
10685 inform (loc, "remove %<std::move%> call");
10687 /* Warn if the move is redundant. It is redundant when we would
10688 do maybe-rvalue overload resolution even without std::move. */
10689 else if (warn_redundant_move
10690 /* This doesn't apply for return {std::move (t)};. */
10691 && !wrapped_p
10692 && !warning_suppressed_p (expr, OPT_Wredundant_move)
10693 && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
10695 /* Make sure that overload resolution would actually succeed
10696 if we removed the std::move call. */
10697 tree t = convert_for_initialization (NULL_TREE, type,
10698 moved,
10699 (LOOKUP_NORMAL
10700 | LOOKUP_ONLYCONVERTING
10701 | LOOKUP_PREFER_RVALUE),
10702 ICR_RETURN, NULL_TREE, 0,
10703 tf_none);
10704 /* If this worked, implicit rvalue would work, so the call to
10705 std::move is redundant. */
10706 if (t != error_mark_node
10707 /* Trying to move something const will never succeed unless
10708 there's T(const T&&), which it almost never is, and if
10709 so, T wouldn't be error_mark_node now: the above convert_
10710 call with LOOKUP_PREFER_RVALUE returns an error if a const T&
10711 overload is selected. */
10712 || (CP_TYPE_CONST_P (TREE_TYPE (arg))
10713 && same_type_ignoring_top_level_qualifiers_p
10714 (TREE_TYPE (arg), type)))
10716 auto_diagnostic_group d;
10717 if (warning_at (loc, OPT_Wredundant_move,
10718 "redundant move in return statement"))
10719 inform (loc, "remove %<std::move%> call");
10723 /* Also try to warn about redundant std::move in code such as
10724 T f (const T& t)
10726 return std::move(t);
10728 for which EXPR will be something like
10729 *std::move<const T&> ((const struct T &) (const struct T *) t)
10730 and where the std::move does nothing if T does not have a T(const T&&)
10731 constructor, because the argument is const. It will not use T(T&&)
10732 because that would mean losing the const. */
10733 else if (TYPE_REF_P (TREE_TYPE (arg))
10734 && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
10736 tree rtype = TREE_TYPE (TREE_TYPE (arg));
10737 if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
10738 return;
10739 /* Check for the unlikely case there's T(const T&&) (we don't care if
10740 it's deleted). */
10741 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
10742 if (move_fn_p (fn))
10744 tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
10745 if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
10746 return;
10748 auto_diagnostic_group d;
10749 if (warning_at (loc, OPT_Wredundant_move,
10750 "redundant move in return statement"))
10751 inform (loc, "remove %<std::move%> call");
10755 /* Check that returning RETVAL from the current function is valid.
10756 Return an expression explicitly showing all conversions required to
10757 change RETVAL into the function return type, and to assign it to
10758 the DECL_RESULT for the function. Set *NO_WARNING to true if
10759 code reaches end of non-void function warning shouldn't be issued
10760 on this RETURN_EXPR. */
10762 tree
10763 check_return_expr (tree retval, bool *no_warning)
10765 tree result;
10766 /* The type actually returned by the function. */
10767 tree valtype;
10768 /* The type the function is declared to return, or void if
10769 the declared type is incomplete. */
10770 tree functype;
10771 int fn_returns_value_p;
10772 location_t loc = cp_expr_loc_or_input_loc (retval);
10774 *no_warning = false;
10776 /* A `volatile' function is one that isn't supposed to return, ever.
10777 (This is a G++ extension, used to get better code for functions
10778 that call the `volatile' function.) */
10779 if (TREE_THIS_VOLATILE (current_function_decl))
10780 warning (0, "function declared %<noreturn%> has a %<return%> statement");
10782 /* Check for various simple errors. */
10783 if (DECL_DESTRUCTOR_P (current_function_decl))
10785 if (retval)
10786 error_at (loc, "returning a value from a destructor");
10788 if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
10789 retval = current_class_ptr;
10790 else
10791 return NULL_TREE;
10793 else if (DECL_CONSTRUCTOR_P (current_function_decl))
10795 if (in_function_try_handler)
10796 /* If a return statement appears in a handler of the
10797 function-try-block of a constructor, the program is ill-formed. */
10798 error ("cannot return from a handler of a function-try-block of a constructor");
10799 else if (retval)
10800 /* You can't return a value from a constructor. */
10801 error_at (loc, "returning a value from a constructor");
10803 if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
10804 retval = current_class_ptr;
10805 else
10806 return NULL_TREE;
10809 const tree saved_retval = retval;
10811 if (processing_template_decl)
10813 current_function_returns_value = 1;
10815 if (check_for_bare_parameter_packs (retval))
10816 return error_mark_node;
10818 /* If one of the types might be void, we can't tell whether we're
10819 returning a value. */
10820 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
10821 && !FNDECL_USED_AUTO (current_function_decl))
10822 || (retval != NULL_TREE
10823 && (TREE_TYPE (retval) == NULL_TREE
10824 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
10825 goto dependent;
10828 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
10830 /* Deduce auto return type from a return statement. */
10831 if (FNDECL_USED_AUTO (current_function_decl))
10833 tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
10834 tree auto_node;
10835 tree type;
10837 if (!retval && !is_auto (pattern))
10839 /* Give a helpful error message. */
10840 error ("return-statement with no value, in function returning %qT",
10841 pattern);
10842 inform (input_location, "only plain %<auto%> return type can be "
10843 "deduced to %<void%>");
10844 type = error_mark_node;
10846 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
10848 error ("returning initializer list");
10849 type = error_mark_node;
10851 else
10853 if (!retval)
10854 retval = void_node;
10855 auto_node = type_uses_auto (pattern);
10856 type = do_auto_deduction (pattern, retval, auto_node,
10857 tf_warning_or_error, adc_return_type);
10860 if (type == error_mark_node)
10861 /* Leave it. */;
10862 else if (functype == pattern)
10863 apply_deduced_return_type (current_function_decl, type);
10864 else if (!same_type_p (type, functype))
10866 if (LAMBDA_FUNCTION_P (current_function_decl))
10867 error_at (loc, "inconsistent types %qT and %qT deduced for "
10868 "lambda return type", functype, type);
10869 else
10870 error_at (loc, "inconsistent deduction for auto return type: "
10871 "%qT and then %qT", functype, type);
10873 functype = type;
10876 result = DECL_RESULT (current_function_decl);
10877 valtype = TREE_TYPE (result);
10878 gcc_assert (valtype != NULL_TREE);
10879 fn_returns_value_p = !VOID_TYPE_P (valtype);
10881 /* Check for a return statement with no return value in a function
10882 that's supposed to return a value. */
10883 if (!retval && fn_returns_value_p)
10885 if (functype != error_mark_node)
10886 permerror (input_location, "return-statement with no value, in "
10887 "function returning %qT", valtype);
10888 /* Remember that this function did return. */
10889 current_function_returns_value = 1;
10890 /* And signal caller that TREE_NO_WARNING should be set on the
10891 RETURN_EXPR to avoid control reaches end of non-void function
10892 warnings in tree-cfg.cc. */
10893 *no_warning = true;
10895 /* Check for a return statement with a value in a function that
10896 isn't supposed to return a value. */
10897 else if (retval && !fn_returns_value_p)
10899 if (VOID_TYPE_P (TREE_TYPE (retval)))
10900 /* You can return a `void' value from a function of `void'
10901 type. In that case, we have to evaluate the expression for
10902 its side-effects. */
10903 finish_expr_stmt (retval);
10904 else if (retval != error_mark_node)
10905 permerror (loc, "return-statement with a value, in function "
10906 "returning %qT", valtype);
10907 current_function_returns_null = 1;
10909 /* There's really no value to return, after all. */
10910 return NULL_TREE;
10912 else if (!retval)
10913 /* Remember that this function can sometimes return without a
10914 value. */
10915 current_function_returns_null = 1;
10916 else
10917 /* Remember that this function did return a value. */
10918 current_function_returns_value = 1;
10920 /* Check for erroneous operands -- but after giving ourselves a
10921 chance to provide an error about returning a value from a void
10922 function. */
10923 if (error_operand_p (retval))
10925 current_function_return_value = error_mark_node;
10926 return error_mark_node;
10929 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
10930 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
10931 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
10932 && ! flag_check_new
10933 && retval && null_ptr_cst_p (retval))
10934 warning (0, "%<operator new%> must not return NULL unless it is "
10935 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
10937 /* Effective C++ rule 15. See also start_function. */
10938 if (warn_ecpp
10939 && DECL_NAME (current_function_decl) == assign_op_identifier
10940 && !type_dependent_expression_p (retval))
10942 bool warn = true;
10944 /* The function return type must be a reference to the current
10945 class. */
10946 if (TYPE_REF_P (valtype)
10947 && same_type_ignoring_top_level_qualifiers_p
10948 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
10950 /* Returning '*this' is obviously OK. */
10951 if (retval == current_class_ref)
10952 warn = false;
10953 /* If we are calling a function whose return type is the same of
10954 the current class reference, it is ok. */
10955 else if (INDIRECT_REF_P (retval)
10956 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
10957 warn = false;
10960 if (warn)
10961 warning_at (loc, OPT_Weffc__,
10962 "%<operator=%> should return a reference to %<*this%>");
10965 if (dependent_type_p (functype)
10966 || type_dependent_expression_p (retval))
10968 dependent:
10969 /* We should not have changed the return value. */
10970 gcc_assert (retval == saved_retval);
10971 /* We don't know if this is an lvalue or rvalue use, but
10972 either way we can mark it as read. */
10973 mark_exp_read (retval);
10974 /* Disable our std::move warnings when we're returning
10975 a dependent expression (c++/89780). */
10976 if (retval && TREE_CODE (retval) == CALL_EXPR)
10977 /* This also suppresses -Wredundant-move. */
10978 suppress_warning (retval, OPT_Wpessimizing_move);
10979 return retval;
10982 /* The fabled Named Return Value optimization, as per [class.copy]/15:
10984 [...] For a function with a class return type, if the expression
10985 in the return statement is the name of a local object, and the cv-
10986 unqualified type of the local object is the same as the function
10987 return type, an implementation is permitted to omit creating the tem-
10988 porary object to hold the function return value [...]
10990 So, if this is a value-returning function that always returns the same
10991 local variable, remember it.
10993 It might be nice to be more flexible, and choose the first suitable
10994 variable even if the function sometimes returns something else, but
10995 then we run the risk of clobbering the variable we chose if the other
10996 returned expression uses the chosen variable somehow. And people expect
10997 this restriction, anyway. (jason 2000-11-19)
10999 See finish_function and finalize_nrv for the rest of this optimization. */
11000 tree bare_retval = NULL_TREE;
11001 if (retval)
11003 retval = maybe_undo_parenthesized_ref (retval);
11004 bare_retval = tree_strip_any_location_wrapper (retval);
11007 bool named_return_value_okay_p = can_do_nrvo_p (bare_retval, functype);
11008 if (fn_returns_value_p && flag_elide_constructors)
11010 if (named_return_value_okay_p
11011 && (current_function_return_value == NULL_TREE
11012 || current_function_return_value == bare_retval))
11013 current_function_return_value = bare_retval;
11014 else
11015 current_function_return_value = error_mark_node;
11018 /* We don't need to do any conversions when there's nothing being
11019 returned. */
11020 if (!retval)
11021 return NULL_TREE;
11023 if (!named_return_value_okay_p)
11024 maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11026 /* Do any required conversions. */
11027 if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11028 /* No conversions are required. */
11030 else
11032 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11034 /* The functype's return type will have been set to void, if it
11035 was an incomplete type. Just treat this as 'return;' */
11036 if (VOID_TYPE_P (functype))
11037 return error_mark_node;
11039 if (processing_template_decl)
11040 retval = build_non_dependent_expr (retval);
11042 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11043 treated as an rvalue for the purposes of overload resolution to
11044 favor move constructors over copy constructors.
11046 Note that these conditions are similar to, but not as strict as,
11047 the conditions for the named return value optimization. */
11048 bool converted = false;
11049 tree moved;
11050 /* Until C++23, this was only interesting for class type, but in C++23,
11051 we should do the below when we're converting rom/to a class/reference
11052 (a non-scalar type). */
11053 if ((cxx_dialect < cxx23
11054 ? CLASS_TYPE_P (functype)
11055 : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11056 && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11058 if (cxx_dialect < cxx20)
11060 moved = convert_for_initialization
11061 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
11062 ICR_RETURN, NULL_TREE, 0, tf_none);
11063 if (moved != error_mark_node)
11065 retval = moved;
11066 converted = true;
11069 else
11070 /* In C++20 we just treat the return value as an rvalue that
11071 can bind to lvalue refs. */
11072 retval = moved;
11075 /* The call in a (lambda) thunk needs no conversions. */
11076 if (TREE_CODE (retval) == CALL_EXPR
11077 && call_from_lambda_thunk_p (retval))
11078 converted = true;
11080 /* First convert the value to the function's return type, then
11081 to the type of return value's location to handle the
11082 case that functype is smaller than the valtype. */
11083 if (!converted)
11084 retval = convert_for_initialization
11085 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11086 tf_warning_or_error);
11087 retval = convert (valtype, retval);
11089 /* If the conversion failed, treat this just like `return;'. */
11090 if (retval == error_mark_node)
11091 return retval;
11092 /* We can't initialize a register from a AGGR_INIT_EXPR. */
11093 else if (! cfun->returns_struct
11094 && TREE_CODE (retval) == TARGET_EXPR
11095 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
11096 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11097 TREE_OPERAND (retval, 0));
11098 else if (!processing_template_decl
11099 && maybe_warn_about_returning_address_of_local (retval, loc)
11100 && INDIRECT_TYPE_P (valtype))
11101 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11102 build_zero_cst (TREE_TYPE (retval)));
11105 if (processing_template_decl)
11106 return saved_retval;
11108 /* Actually copy the value returned into the appropriate location. */
11109 if (retval && retval != result)
11110 retval = cp_build_init_expr (result, retval);
11112 if (tree set = maybe_set_retval_sentinel ())
11113 retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11115 return retval;
11119 /* Returns nonzero if the pointer-type FROM can be converted to the
11120 pointer-type TO via a qualification conversion. If CONSTP is -1,
11121 then we return nonzero if the pointers are similar, and the
11122 cv-qualification signature of FROM is a proper subset of that of TO.
11124 If CONSTP is positive, then all outer pointers have been
11125 const-qualified. */
11127 static bool
11128 comp_ptr_ttypes_real (tree to, tree from, int constp)
11130 bool to_more_cv_qualified = false;
11131 bool is_opaque_pointer = false;
11133 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11135 if (TREE_CODE (to) != TREE_CODE (from))
11136 return false;
11138 if (TREE_CODE (from) == OFFSET_TYPE
11139 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11140 TYPE_OFFSET_BASETYPE (to)))
11141 return false;
11143 /* Const and volatile mean something different for function and
11144 array types, so the usual checks are not appropriate. We'll
11145 check the array type elements in further iterations. */
11146 if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11148 if (!at_least_as_qualified_p (to, from))
11149 return false;
11151 if (!at_least_as_qualified_p (from, to))
11153 if (constp == 0)
11154 return false;
11155 to_more_cv_qualified = true;
11158 if (constp > 0)
11159 constp &= TYPE_READONLY (to);
11162 if (VECTOR_TYPE_P (to))
11163 is_opaque_pointer = vector_targets_convertible_p (to, from);
11165 /* P0388R4 allows a conversion from int[N] to int[] but not the
11166 other way round. When both arrays have bounds but they do
11167 not match, then no conversion is possible. */
11168 if (TREE_CODE (to) == ARRAY_TYPE
11169 && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11170 return false;
11172 if (!TYPE_PTR_P (to)
11173 && !TYPE_PTRDATAMEM_P (to)
11174 /* CWG 330 says we need to look through arrays. */
11175 && TREE_CODE (to) != ARRAY_TYPE)
11176 return ((constp >= 0 || to_more_cv_qualified)
11177 && (is_opaque_pointer
11178 || same_type_ignoring_top_level_qualifiers_p (to, from)));
11182 /* When comparing, say, char ** to char const **, this function takes
11183 the 'char *' and 'char const *'. Do not pass non-pointer/reference
11184 types to this function. */
11187 comp_ptr_ttypes (tree to, tree from)
11189 return comp_ptr_ttypes_real (to, from, 1);
11192 /* Returns true iff FNTYPE is a non-class type that involves
11193 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11194 if a parameter type is ill-formed. */
11196 bool
11197 error_type_p (const_tree type)
11199 tree t;
11201 switch (TREE_CODE (type))
11203 case ERROR_MARK:
11204 return true;
11206 case POINTER_TYPE:
11207 case REFERENCE_TYPE:
11208 case OFFSET_TYPE:
11209 return error_type_p (TREE_TYPE (type));
11211 case FUNCTION_TYPE:
11212 case METHOD_TYPE:
11213 if (error_type_p (TREE_TYPE (type)))
11214 return true;
11215 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11216 if (error_type_p (TREE_VALUE (t)))
11217 return true;
11218 return false;
11220 case RECORD_TYPE:
11221 if (TYPE_PTRMEMFUNC_P (type))
11222 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11223 return false;
11225 default:
11226 return false;
11230 /* Returns true if to and from are (possibly multi-level) pointers to the same
11231 type or inheritance-related types, regardless of cv-quals. */
11233 bool
11234 ptr_reasonably_similar (const_tree to, const_tree from)
11236 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11238 /* Any target type is similar enough to void. */
11239 if (VOID_TYPE_P (to))
11240 return !error_type_p (from);
11241 if (VOID_TYPE_P (from))
11242 return !error_type_p (to);
11244 if (TREE_CODE (to) != TREE_CODE (from))
11245 return false;
11247 if (TREE_CODE (from) == OFFSET_TYPE
11248 && comptypes (TYPE_OFFSET_BASETYPE (to),
11249 TYPE_OFFSET_BASETYPE (from),
11250 COMPARE_BASE | COMPARE_DERIVED))
11251 continue;
11253 if (VECTOR_TYPE_P (to)
11254 && vector_types_convertible_p (to, from, false))
11255 return true;
11257 if (TREE_CODE (to) == INTEGER_TYPE
11258 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11259 return true;
11261 if (TREE_CODE (to) == FUNCTION_TYPE)
11262 return !error_type_p (to) && !error_type_p (from);
11264 if (!TYPE_PTR_P (to))
11266 /* When either type is incomplete avoid DERIVED_FROM_P,
11267 which may call complete_type (c++/57942). */
11268 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11269 return comptypes
11270 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11271 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11276 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11277 pointer-to-member types) are the same, ignoring cv-qualification at
11278 all levels. CB says how we should behave when comparing array bounds. */
11280 bool
11281 comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11283 bool is_opaque_pointer = false;
11285 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11287 if (TREE_CODE (to) != TREE_CODE (from))
11288 return false;
11290 if (TREE_CODE (from) == OFFSET_TYPE
11291 && same_type_p (TYPE_OFFSET_BASETYPE (from),
11292 TYPE_OFFSET_BASETYPE (to)))
11293 continue;
11295 if (VECTOR_TYPE_P (to))
11296 is_opaque_pointer = vector_targets_convertible_p (to, from);
11298 if (TREE_CODE (to) == ARRAY_TYPE
11299 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11300 we must fail. */
11301 && !comp_array_types (to, from, cb, /*strict=*/false))
11302 return false;
11304 /* CWG 330 says we need to look through arrays. */
11305 if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11306 return (is_opaque_pointer
11307 || same_type_ignoring_top_level_qualifiers_p (to, from));
11311 /* Returns the type qualifiers for this type, including the qualifiers on the
11312 elements for an array type. */
11315 cp_type_quals (const_tree type)
11317 int quals;
11318 /* This CONST_CAST is okay because strip_array_types returns its
11319 argument unmodified and we assign it to a const_tree. */
11320 type = strip_array_types (CONST_CAST_TREE (type));
11321 if (type == error_mark_node
11322 /* Quals on a FUNCTION_TYPE are memfn quals. */
11323 || TREE_CODE (type) == FUNCTION_TYPE)
11324 return TYPE_UNQUALIFIED;
11325 quals = TYPE_QUALS (type);
11326 /* METHOD and REFERENCE_TYPEs should never have quals. */
11327 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11328 && !TYPE_REF_P (type))
11329 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11330 == TYPE_UNQUALIFIED));
11331 return quals;
11334 /* Returns the function-ref-qualifier for TYPE */
11336 cp_ref_qualifier
11337 type_memfn_rqual (const_tree type)
11339 gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11341 if (!FUNCTION_REF_QUALIFIED (type))
11342 return REF_QUAL_NONE;
11343 else if (FUNCTION_RVALUE_QUALIFIED (type))
11344 return REF_QUAL_RVALUE;
11345 else
11346 return REF_QUAL_LVALUE;
11349 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11350 METHOD_TYPE. */
11353 type_memfn_quals (const_tree type)
11355 if (TREE_CODE (type) == FUNCTION_TYPE)
11356 return TYPE_QUALS (type);
11357 else if (TREE_CODE (type) == METHOD_TYPE)
11358 return cp_type_quals (class_of_this_parm (type));
11359 else
11360 gcc_unreachable ();
11363 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11364 MEMFN_QUALS and its ref-qualifier to RQUAL. */
11366 tree
11367 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11369 /* Could handle METHOD_TYPE here if necessary. */
11370 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11371 if (TYPE_QUALS (type) == memfn_quals
11372 && type_memfn_rqual (type) == rqual)
11373 return type;
11375 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11376 complex. */
11377 tree result = build_qualified_type (type, memfn_quals);
11378 return build_ref_qualified_type (result, rqual);
11381 /* Returns nonzero if TYPE is const or volatile. */
11383 bool
11384 cv_qualified_p (const_tree type)
11386 int quals = cp_type_quals (type);
11387 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11390 /* Returns nonzero if the TYPE contains a mutable member. */
11392 bool
11393 cp_has_mutable_p (const_tree type)
11395 /* This CONST_CAST is okay because strip_array_types returns its
11396 argument unmodified and we assign it to a const_tree. */
11397 type = strip_array_types (CONST_CAST_TREE(type));
11399 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11402 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11403 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11404 approximation. In particular, consider:
11406 int f();
11407 struct S { int i; };
11408 const S s = { f(); }
11410 Here, we will make "s" as TREE_READONLY (because it is declared
11411 "const") -- only to reverse ourselves upon seeing that the
11412 initializer is non-constant. */
11414 void
11415 cp_apply_type_quals_to_decl (int type_quals, tree decl)
11417 tree type = TREE_TYPE (decl);
11419 if (type == error_mark_node)
11420 return;
11422 if (TREE_CODE (decl) == TYPE_DECL)
11423 return;
11425 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11426 && type_quals != TYPE_UNQUALIFIED));
11428 /* Avoid setting TREE_READONLY incorrectly. */
11429 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11430 constructor can produce constant init, so rely on cp_finish_decl to
11431 clear TREE_READONLY if the variable has non-constant init. */
11433 /* If the type has (or might have) a mutable component, that component
11434 might be modified. */
11435 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11436 type_quals &= ~TYPE_QUAL_CONST;
11438 c_apply_type_quals_to_decl (type_quals, decl);
11441 /* Subroutine of casts_away_constness. Make T1 and T2 point at
11442 exemplar types such that casting T1 to T2 is casting away constness
11443 if and only if there is no implicit conversion from T1 to T2. */
11445 static void
11446 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11448 int quals1;
11449 int quals2;
11451 /* [expr.const.cast]
11453 For multi-level pointer to members and multi-level mixed pointers
11454 and pointers to members (conv.qual), the "member" aspect of a
11455 pointer to member level is ignored when determining if a const
11456 cv-qualifier has been cast away. */
11457 /* [expr.const.cast]
11459 For two pointer types:
11461 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11462 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11463 K is min(N,M)
11465 casting from X1 to X2 casts away constness if, for a non-pointer
11466 type T there does not exist an implicit conversion (clause
11467 _conv_) from:
11469 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11473 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11474 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11475 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11477 *t1 = cp_build_qualified_type (void_type_node,
11478 cp_type_quals (*t1));
11479 *t2 = cp_build_qualified_type (void_type_node,
11480 cp_type_quals (*t2));
11481 return;
11484 quals1 = cp_type_quals (*t1);
11485 quals2 = cp_type_quals (*t2);
11487 if (TYPE_PTRDATAMEM_P (*t1))
11488 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11489 else
11490 *t1 = TREE_TYPE (*t1);
11491 if (TYPE_PTRDATAMEM_P (*t2))
11492 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11493 else
11494 *t2 = TREE_TYPE (*t2);
11496 casts_away_constness_r (t1, t2, complain);
11497 *t1 = build_pointer_type (*t1);
11498 *t2 = build_pointer_type (*t2);
11499 *t1 = cp_build_qualified_type (*t1, quals1);
11500 *t2 = cp_build_qualified_type (*t2, quals2);
11503 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11504 constness.
11506 ??? This function returns non-zero if casting away qualifiers not
11507 just const. We would like to return to the caller exactly which
11508 qualifiers are casted away to give more accurate diagnostics.
11511 static bool
11512 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
11514 if (TYPE_REF_P (t2))
11516 /* [expr.const.cast]
11518 Casting from an lvalue of type T1 to an lvalue of type T2
11519 using a reference cast casts away constness if a cast from an
11520 rvalue of type "pointer to T1" to the type "pointer to T2"
11521 casts away constness. */
11522 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
11523 return casts_away_constness (build_pointer_type (t1),
11524 build_pointer_type (TREE_TYPE (t2)),
11525 complain);
11528 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
11529 /* [expr.const.cast]
11531 Casting from an rvalue of type "pointer to data member of X
11532 of type T1" to the type "pointer to data member of Y of type
11533 T2" casts away constness if a cast from an rvalue of type
11534 "pointer to T1" to the type "pointer to T2" casts away
11535 constness. */
11536 return casts_away_constness
11537 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
11538 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
11539 complain);
11541 /* Casting away constness is only something that makes sense for
11542 pointer or reference types. */
11543 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
11544 return false;
11546 /* Top-level qualifiers don't matter. */
11547 t1 = TYPE_MAIN_VARIANT (t1);
11548 t2 = TYPE_MAIN_VARIANT (t2);
11549 casts_away_constness_r (&t1, &t2, complain);
11550 if (!can_convert (t2, t1, complain))
11551 return true;
11553 return false;
11556 /* If T is a REFERENCE_TYPE return the type to which T refers.
11557 Otherwise, return T itself. */
11559 tree
11560 non_reference (tree t)
11562 if (t && TYPE_REF_P (t))
11563 t = TREE_TYPE (t);
11564 return t;
11568 /* Return nonzero if REF is an lvalue valid for this language;
11569 otherwise, print an error message and return zero. USE says
11570 how the lvalue is being used and so selects the error message. */
11573 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
11575 cp_lvalue_kind kind = lvalue_kind (ref);
11577 if (kind == clk_none)
11579 if (complain & tf_error)
11580 lvalue_error (cp_expr_loc_or_input_loc (ref), use);
11581 return 0;
11583 else if (kind & (clk_rvalueref|clk_class))
11585 if (!(complain & tf_error))
11586 return 0;
11587 /* Make this a permerror because we used to accept it. */
11588 permerror (cp_expr_loc_or_input_loc (ref),
11589 "using rvalue as lvalue");
11591 return 1;
11594 /* Return true if a user-defined literal operator is a raw operator. */
11596 bool
11597 check_raw_literal_operator (const_tree decl)
11599 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11600 tree argtype;
11601 int arity;
11602 bool maybe_raw_p = false;
11604 /* Count the number and type of arguments and check for ellipsis. */
11605 for (argtype = argtypes, arity = 0;
11606 argtype && argtype != void_list_node;
11607 ++arity, argtype = TREE_CHAIN (argtype))
11609 tree t = TREE_VALUE (argtype);
11611 if (same_type_p (t, const_string_type_node))
11612 maybe_raw_p = true;
11614 if (!argtype)
11615 return false; /* Found ellipsis. */
11617 if (!maybe_raw_p || arity != 1)
11618 return false;
11620 return true;
11624 /* Return true if a user-defined literal operator has one of the allowed
11625 argument types. */
11627 bool
11628 check_literal_operator_args (const_tree decl,
11629 bool *long_long_unsigned_p, bool *long_double_p)
11631 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11633 *long_long_unsigned_p = false;
11634 *long_double_p = false;
11635 if (processing_template_decl || processing_specialization)
11636 return argtypes == void_list_node;
11637 else
11639 tree argtype;
11640 int arity;
11641 int max_arity = 2;
11643 /* Count the number and type of arguments and check for ellipsis. */
11644 for (argtype = argtypes, arity = 0;
11645 argtype && argtype != void_list_node;
11646 argtype = TREE_CHAIN (argtype))
11648 tree t = TREE_VALUE (argtype);
11649 ++arity;
11651 if (TYPE_PTR_P (t))
11653 bool maybe_raw_p = false;
11654 t = TREE_TYPE (t);
11655 if (cp_type_quals (t) != TYPE_QUAL_CONST)
11656 return false;
11657 t = TYPE_MAIN_VARIANT (t);
11658 if ((maybe_raw_p = same_type_p (t, char_type_node))
11659 || same_type_p (t, wchar_type_node)
11660 || same_type_p (t, char8_type_node)
11661 || same_type_p (t, char16_type_node)
11662 || same_type_p (t, char32_type_node))
11664 argtype = TREE_CHAIN (argtype);
11665 if (!argtype)
11666 return false;
11667 t = TREE_VALUE (argtype);
11668 if (maybe_raw_p && argtype == void_list_node)
11669 return true;
11670 else if (same_type_p (t, size_type_node))
11672 ++arity;
11673 continue;
11675 else
11676 return false;
11679 else if (same_type_p (t, long_long_unsigned_type_node))
11681 max_arity = 1;
11682 *long_long_unsigned_p = true;
11684 else if (same_type_p (t, long_double_type_node))
11686 max_arity = 1;
11687 *long_double_p = true;
11689 else if (same_type_p (t, char_type_node))
11690 max_arity = 1;
11691 else if (same_type_p (t, wchar_type_node))
11692 max_arity = 1;
11693 else if (same_type_p (t, char8_type_node))
11694 max_arity = 1;
11695 else if (same_type_p (t, char16_type_node))
11696 max_arity = 1;
11697 else if (same_type_p (t, char32_type_node))
11698 max_arity = 1;
11699 else
11700 return false;
11702 if (!argtype)
11703 return false; /* Found ellipsis. */
11705 if (arity != max_arity)
11706 return false;
11708 return true;
11712 /* Always returns false since unlike C90, C++ has no concept of implicit
11713 function declarations. */
11715 bool
11716 c_decl_implicit (const_tree)
11718 return false;