1/* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
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. */
26
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"
43
44static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46static tree pfn_from_ptrmemfunc (tree);
47static tree delta_from_ptrmemfunc (tree);
48static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 tsubst_flags_t, int);
50static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 tsubst_flags_t);
52static tree rationalize_conditional_expr (enum tree_code, tree,
53 tsubst_flags_t);
54static bool comp_ptr_ttypes_real (tree, tree, int);
55static bool comp_except_types (tree, tree, bool);
56static bool comp_array_types (const_tree, const_tree, compare_bounds_t, bool);
57static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60static bool casts_away_constness (tree, tree, tsubst_flags_t);
61static bool maybe_warn_about_returning_address_of_local (tree, location_t = UNKNOWN_LOCATION);
62static void error_args_num (location_t, tree, bool);
63static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
64 tsubst_flags_t);
65static bool is_std_move_p (tree);
66static bool is_std_forward_p (tree);
67
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. */
72
73tree
74require_complete_type (tree value,
75 tsubst_flags_t complain /* = tf_warning_or_error */)
76{
77 tree type;
78
79 if (processing_template_decl || value == error_mark_node)
80 return value;
81
82 if (TREE_CODE (value) == OVERLOAD)
83 type = unknown_type_node;
84 else
85 type = TREE_TYPE (value);
86
87 if (type == error_mark_node)
88 return error_mark_node;
89
90 /* First, detect a valid value with a complete type. */
91 if (COMPLETE_TYPE_P (type))
92 return value;
93
94 if (complete_type_or_maybe_complain (type, value, complain))
95 return value;
96 else
97 return error_mark_node;
98}
99
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. */
104
105tree
106complete_type (tree type)
107{
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;
112
113 if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 ;
115 else if (TREE_CODE (type) == ARRAY_TYPE)
116 {
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))
126 {
127 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
129 }
130 }
131 else if (CLASS_TYPE_P (type))
132 {
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)));
136
137 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 instantiate_class_template (TYPE_MAIN_VARIANT (type));
139 }
140
141 return type;
142}
143
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. */
147
148tree
149complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150{
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))
156 {
157 if (complain & tf_error)
158 cxx_incomplete_type_diagnostic (value, type, diag_kind: DK_ERROR);
159 note_failed_type_completion_for_satisfaction (type);
160 return NULL_TREE;
161 }
162 else
163 return type;
164}
165
166tree
167complete_type_or_else (tree type, tree value)
168{
169 return complete_type_or_maybe_complain (type, value, complain: tf_warning_or_error);
170}
171
172
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.
176
177 As an optimization, free the space we allocate if the parameter
178 lists are already common. */
179
180static tree
181commonparms (tree p1, tree p2)
182{
183 tree oldargs = p1, newargs, n;
184 int i, len;
185 int any_change = 0;
186
187 len = list_length (p1);
188 newargs = tree_last (p1);
189
190 if (newargs == void_list_node)
191 i = 1;
192 else
193 {
194 i = 0;
195 newargs = 0;
196 }
197
198 for (; i < len; i++)
199 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
200
201 n = newargs;
202
203 for (i = 0; p1;
204 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
205 {
206 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
207 {
208 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 any_change = 1;
210 }
211 else if (! TREE_PURPOSE (p1))
212 {
213 if (TREE_PURPOSE (p2))
214 {
215 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 any_change = 1;
217 }
218 }
219 else
220 {
221 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 any_change = 1;
223 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
224 }
225 if (TREE_VALUE (p1) != TREE_VALUE (p2))
226 {
227 any_change = 1;
228 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
229 }
230 else
231 TREE_VALUE (n) = TREE_VALUE (p1);
232 }
233 if (! any_change)
234 return oldargs;
235
236 return newargs;
237}
238
239/* Given a type, perhaps copied for a typedef,
240 find the "original" version of it. */
241static tree
242original_type (tree t)
243{
244 int quals = cp_type_quals (t);
245 while (t != error_mark_node
246 && TYPE_NAME (t) != NULL_TREE)
247 {
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;
255 }
256 return cp_build_qualified_type (t, quals);
257}
258
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. */
261
262static tree
263merge_type_attributes_from (tree type, tree other_type)
264{
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);
268}
269
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. */
277
278int
279cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
280{
281 tree mv1 = TYPE_MAIN_VARIANT (t1);
282 tree mv2 = TYPE_MAIN_VARIANT (t2);
283 int extended1 = 0;
284 int extended2 = 0;
285
286 if (mv1 == mv2)
287 return 0;
288
289 for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
290 {
291 if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 extended1 = i + 1;
293 if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 extended2 = i + 1;
295 }
296 if (mv1 == bfloat16_type_node)
297 extended1 = true;
298 if (mv2 == bfloat16_type_node)
299 extended2 = true;
300 if (extended2 && !extended1)
301 {
302 int ret = cp_compare_floating_point_conversion_ranks (t1: t2, t2: t1);
303 return ret == 3 ? 3 : -ret;
304 }
305
306 const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 gcc_assert (fmt1->b == 2 && fmt2->b == 2);
309 /* For {ibm,mips}_extended_format formats, the type has variable
310 precision up to ~2150 bits when the first double is around maximum
311 representable double and second double is subnormal minimum.
312 So, e.g. for __ibm128 vs. std::float128_t, they have unordered
313 ranks. */
314 int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 ? fmt2->emax - fmt2->emin + fmt2->p - 1 : fmt2->p);
318 /* The rank of a floating point type T is greater than the rank of
319 any floating-point type whose set of values is a proper subset
320 of the set of values of T. */
321 if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 || (p1 == p2 && fmt1->emax > fmt2->emax))
323 return 2;
324 if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 || (p1 == p2 && fmt1->emax < fmt2->emax))
326 return -2;
327 if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 || (p1 < p2 && fmt1->emax > fmt2->emax))
329 return 3;
330 if (!extended1 && !extended2)
331 {
332 /* The rank of long double is greater than the rank of double, which
333 is greater than the rank of float. */
334 if (t1 == long_double_type_node)
335 return 2;
336 else if (t2 == long_double_type_node)
337 return -2;
338 if (t1 == double_type_node)
339 return 2;
340 else if (t2 == double_type_node)
341 return -2;
342 if (t1 == float_type_node)
343 return 2;
344 else if (t2 == float_type_node)
345 return -2;
346 return 0;
347 }
348 /* Two extended floating-point types with the same set of values have equal
349 ranks. */
350 if (extended1 && extended2)
351 {
352 if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES))
353 {
354 /* Prefer higher extendedN value. */
355 if (extended1 > extended2)
356 return 1;
357 else if (extended1 < extended2)
358 return -1;
359 else
360 return 0;
361 }
362 else if (extended1 <= NUM_FLOATN_TYPES)
363 /* Prefer _FloatN type over _FloatMx type. */
364 return 1;
365 else if (extended2 <= NUM_FLOATN_TYPES)
366 return -1;
367 else
368 return 0;
369 }
370
371 /* gcc_assert (extended1 && !extended2); */
372 tree *p;
373 int cnt = 0;
374 for (p = &float_type_node; p <= &long_double_type_node; ++p)
375 {
376 const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 gcc_assert (fmt3->b == 2);
378 int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 if (p1 == p3 && fmt1->emax == fmt3->emax)
381 ++cnt;
382 }
383 /* An extended floating-point type with the same set of values
384 as exactly one cv-unqualified standard floating-point type
385 has a rank equal to the rank of that standard floating-point
386 type.
387
388 An extended floating-point type with the same set of values
389 as more than one cv-unqualified standard floating-point type
390 has a rank equal to the rank of double.
391
392 Thus, if the latter is true and t2 is long double, t2
393 has higher rank. */
394 if (cnt > 1 && mv2 == long_double_type_node)
395 return -2;
396 /* Otherwise, they have equal rank, but extended types
397 (other than std::bfloat16_t) have higher subrank.
398 std::bfloat16_t shouldn't have equal rank to any standard
399 floating point type. */
400 return 1;
401}
402
403/* Return the common type for two arithmetic types T1 and T2 under the
404 usual arithmetic conversions. The default conversions have already
405 been applied, and enumerated types converted to their compatible
406 integer types. */
407
408static tree
409cp_common_type (tree t1, tree t2)
410{
411 enum tree_code code1 = TREE_CODE (t1);
412 enum tree_code code2 = TREE_CODE (t2);
413 tree attributes;
414 int i;
415
416
417 /* In what follows, we slightly generalize the rules given in [expr] so
418 as to deal with `long long' and `complex'. First, merge the
419 attributes. */
420 attributes = (*targetm.merge_type_attributes) (t1, t2);
421
422 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
423 {
424 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
425 return build_type_attribute_variant (t1, attributes);
426 else
427 return NULL_TREE;
428 }
429
430 /* FIXME: Attributes. */
431 gcc_assert (ARITHMETIC_TYPE_P (t1)
432 || VECTOR_TYPE_P (t1)
433 || UNSCOPED_ENUM_P (t1));
434 gcc_assert (ARITHMETIC_TYPE_P (t2)
435 || VECTOR_TYPE_P (t2)
436 || UNSCOPED_ENUM_P (t2));
437
438 /* If one type is complex, form the common type of the non-complex
439 components, then make that complex. Use T1 or T2 if it is the
440 required type. */
441 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
442 {
443 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
444 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
445 tree subtype
446 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
447
448 if (subtype == error_mark_node)
449 return subtype;
450 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
451 return build_type_attribute_variant (t1, attributes);
452 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
453 return build_type_attribute_variant (t2, attributes);
454 else
455 return build_type_attribute_variant (build_complex_type (subtype),
456 attributes);
457 }
458
459 if (code1 == VECTOR_TYPE)
460 {
461 /* When we get here we should have two vectors of the same size.
462 Just prefer the unsigned one if present. */
463 if (TYPE_UNSIGNED (t1))
464 return merge_type_attributes_from (type: t1, other_type: t2);
465 else
466 return merge_type_attributes_from (type: t2, other_type: t1);
467 }
468
469 /* If only one is real, use it as the result. */
470 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
471 return build_type_attribute_variant (t1, attributes);
472 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
473 return build_type_attribute_variant (t2, attributes);
474
475 if (code1 == REAL_TYPE
476 && (extended_float_type_p (type: t1) || extended_float_type_p (type: t2)))
477 {
478 tree mv1 = TYPE_MAIN_VARIANT (t1);
479 tree mv2 = TYPE_MAIN_VARIANT (t2);
480 if (mv1 == mv2)
481 return build_type_attribute_variant (t1, attributes);
482
483 int cmpret = cp_compare_floating_point_conversion_ranks (t1: mv1, t2: mv2);
484 if (cmpret == 3)
485 return error_mark_node;
486 else if (cmpret >= 0)
487 return build_type_attribute_variant (t1, attributes);
488 else
489 return build_type_attribute_variant (t2, attributes);
490 }
491
492 /* Both real or both integers; use the one with greater precision. */
493 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
494 return build_type_attribute_variant (t1, attributes);
495 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
496 return build_type_attribute_variant (t2, attributes);
497
498 /* The types are the same; no need to do anything fancy. */
499 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
500 return build_type_attribute_variant (t1, attributes);
501
502 if (code1 != REAL_TYPE)
503 {
504 /* If one is unsigned long long, then convert the other to unsigned
505 long long. */
506 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
507 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
508 return build_type_attribute_variant (long_long_unsigned_type_node,
509 attributes);
510 /* If one is a long long, and the other is an unsigned long, and
511 long long can represent all the values of an unsigned long, then
512 convert to a long long. Otherwise, convert to an unsigned long
513 long. Otherwise, if either operand is long long, convert the
514 other to long long.
515
516 Since we're here, we know the TYPE_PRECISION is the same;
517 therefore converting to long long cannot represent all the values
518 of an unsigned long, so we choose unsigned long long in that
519 case. */
520 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
521 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
522 {
523 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
524 ? long_long_unsigned_type_node
525 : long_long_integer_type_node);
526 return build_type_attribute_variant (t, attributes);
527 }
528
529 /* Go through the same procedure, but for longs. */
530 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
531 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
532 return build_type_attribute_variant (long_unsigned_type_node,
533 attributes);
534 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
535 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
536 {
537 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
538 ? long_unsigned_type_node : long_integer_type_node);
539 return build_type_attribute_variant (t, attributes);
540 }
541
542 /* For __intN types, either the type is __int128 (and is lower
543 priority than the types checked above, but higher than other
544 128-bit types) or it's known to not be the same size as other
545 types (enforced in toplev.cc). Prefer the unsigned type. */
546 for (i = 0; i < NUM_INT_N_ENTS; i ++)
547 {
548 if (int_n_enabled_p [i]
549 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
550 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
551 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
552 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
553 {
554 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
555 ? int_n_trees[i].unsigned_type
556 : int_n_trees[i].signed_type);
557 return build_type_attribute_variant (t, attributes);
558 }
559 }
560
561 /* Otherwise prefer the unsigned one. */
562 if (TYPE_UNSIGNED (t1))
563 return build_type_attribute_variant (t1, attributes);
564 else
565 return build_type_attribute_variant (t2, attributes);
566 }
567 else
568 {
569 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
570 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
571 return build_type_attribute_variant (long_double_type_node,
572 attributes);
573 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
574 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
575 return build_type_attribute_variant (double_type_node,
576 attributes);
577 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
578 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
579 return build_type_attribute_variant (float_type_node,
580 attributes);
581
582 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
583 the standard C++ floating-point types. Logic earlier in this
584 function has already eliminated the possibility that
585 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
586 compelling reason to choose one or the other. */
587 return build_type_attribute_variant (t1, attributes);
588 }
589}
590
591/* T1 and T2 are arithmetic or enumeration types. Return the type
592 that will result from the "usual arithmetic conversions" on T1 and
593 T2 as described in [expr]. */
594
595tree
596type_after_usual_arithmetic_conversions (tree t1, tree t2)
597{
598 gcc_assert (ARITHMETIC_TYPE_P (t1)
599 || VECTOR_TYPE_P (t1)
600 || UNSCOPED_ENUM_P (t1));
601 gcc_assert (ARITHMETIC_TYPE_P (t2)
602 || VECTOR_TYPE_P (t2)
603 || UNSCOPED_ENUM_P (t2));
604
605 /* Perform the integral promotions. We do not promote real types here. */
606 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
607 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
608 {
609 t1 = type_promotes_to (t1);
610 t2 = type_promotes_to (t2);
611 }
612
613 return cp_common_type (t1, t2);
614}
615
616static void
617composite_pointer_error (const op_location_t &location,
618 diagnostic_t kind, tree t1, tree t2,
619 composite_pointer_operation operation)
620{
621 switch (operation)
622 {
623 case CPO_COMPARISON:
624 emit_diagnostic (kind, location, 0,
625 "comparison between "
626 "distinct pointer types %qT and %qT lacks a cast",
627 t1, t2);
628 break;
629 case CPO_CONVERSION:
630 emit_diagnostic (kind, location, 0,
631 "conversion between "
632 "distinct pointer types %qT and %qT lacks a cast",
633 t1, t2);
634 break;
635 case CPO_CONDITIONAL_EXPR:
636 emit_diagnostic (kind, location, 0,
637 "conditional expression between "
638 "distinct pointer types %qT and %qT lacks a cast",
639 t1, t2);
640 break;
641 default:
642 gcc_unreachable ();
643 }
644}
645
646/* Subroutine of composite_pointer_type to implement the recursive
647 case. See that function for documentation of the parameters. And ADD_CONST
648 is used to track adding "const" where needed. */
649
650static tree
651composite_pointer_type_r (const op_location_t &location,
652 tree t1, tree t2, bool *add_const,
653 composite_pointer_operation operation,
654 tsubst_flags_t complain)
655{
656 tree pointee1;
657 tree pointee2;
658 tree result_type;
659 tree attributes;
660
661 /* Determine the types pointed to by T1 and T2. */
662 if (TYPE_PTR_P (t1))
663 {
664 pointee1 = TREE_TYPE (t1);
665 pointee2 = TREE_TYPE (t2);
666 }
667 else
668 {
669 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
670 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
671 }
672
673 /* [expr.type]
674
675 If T1 and T2 are similar types, the result is the cv-combined type of
676 T1 and T2. */
677 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
678 result_type = pointee1;
679 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
680 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
681 {
682 result_type = composite_pointer_type_r (location, t1: pointee1, t2: pointee2,
683 add_const, operation, complain);
684 if (result_type == error_mark_node)
685 return error_mark_node;
686 }
687 else
688 {
689 if (complain & tf_error)
690 composite_pointer_error (location, kind: DK_PERMERROR,
691 t1, t2, operation);
692 else
693 return error_mark_node;
694 result_type = void_type_node;
695 }
696 const int q1 = cp_type_quals (pointee1);
697 const int q2 = cp_type_quals (pointee2);
698 const int quals = q1 | q2;
699 result_type = cp_build_qualified_type (result_type,
700 (quals | (*add_const
701 ? TYPE_QUAL_CONST
702 : TYPE_UNQUALIFIED)));
703 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
704 the TLQ). The reason is that both T1 and T2 can then be converted to the
705 cv-combined type of T1 and T2. */
706 if (quals != q1 || quals != q2)
707 *add_const = true;
708 /* If the original types were pointers to members, so is the
709 result. */
710 if (TYPE_PTRMEM_P (t1))
711 {
712 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
713 TYPE_PTRMEM_CLASS_TYPE (t2)))
714 {
715 if (complain & tf_error)
716 composite_pointer_error (location, kind: DK_PERMERROR,
717 t1, t2, operation);
718 else
719 return error_mark_node;
720 }
721 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
722 result_type);
723 }
724 else
725 result_type = build_pointer_type (result_type);
726
727 /* Merge the attributes. */
728 attributes = (*targetm.merge_type_attributes) (t1, t2);
729 return build_type_attribute_variant (result_type, attributes);
730}
731
732/* Return the composite pointer type (see [expr.type]) for T1 and T2.
733 ARG1 and ARG2 are the values with those types. The OPERATION is to
734 describe the operation between the pointer types,
735 in case an error occurs.
736
737 This routine also implements the computation of a common type for
738 pointers-to-members as per [expr.eq]. */
739
740tree
741composite_pointer_type (const op_location_t &location,
742 tree t1, tree t2, tree arg1, tree arg2,
743 composite_pointer_operation operation,
744 tsubst_flags_t complain)
745{
746 tree class1;
747 tree class2;
748
749 /* [expr.type]
750
751 If one operand is a null pointer constant, the composite pointer
752 type is the type of the other operand. */
753 if (null_ptr_cst_p (arg1))
754 return t2;
755 if (null_ptr_cst_p (arg2))
756 return t1;
757
758 /* We have:
759
760 [expr.type]
761
762 If one of the operands has type "pointer to cv1 void", then
763 the other has type "pointer to cv2 T", and the composite pointer
764 type is "pointer to cv12 void", where cv12 is the union of cv1
765 and cv2.
766
767 If either type is a pointer to void, make sure it is T1. */
768 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
769 std::swap (a&: t1, b&: t2);
770
771 /* Now, if T1 is a pointer to void, merge the qualifiers. */
772 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
773 {
774 tree attributes;
775 tree result_type;
776
777 if (TYPE_PTRFN_P (t2))
778 {
779 if (complain & tf_error)
780 {
781 switch (operation)
782 {
783 case CPO_COMPARISON:
784 pedwarn (location, OPT_Wpedantic,
785 "ISO C++ forbids comparison between pointer "
786 "of type %<void *%> and pointer-to-function");
787 break;
788 case CPO_CONVERSION:
789 pedwarn (location, OPT_Wpedantic,
790 "ISO C++ forbids conversion between pointer "
791 "of type %<void *%> and pointer-to-function");
792 break;
793 case CPO_CONDITIONAL_EXPR:
794 pedwarn (location, OPT_Wpedantic,
795 "ISO C++ forbids conditional expression between "
796 "pointer of type %<void *%> and "
797 "pointer-to-function");
798 break;
799 default:
800 gcc_unreachable ();
801 }
802 }
803 else
804 return error_mark_node;
805 }
806 result_type
807 = cp_build_qualified_type (void_type_node,
808 (cp_type_quals (TREE_TYPE (t1))
809 | cp_type_quals (TREE_TYPE (t2))));
810 result_type = build_pointer_type (result_type);
811 /* Merge the attributes. */
812 attributes = (*targetm.merge_type_attributes) (t1, t2);
813 return build_type_attribute_variant (result_type, attributes);
814 }
815
816 if (c_dialect_objc () && TYPE_PTR_P (t1)
817 && TYPE_PTR_P (t2))
818 {
819 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
820 return objc_common_type (t1, t2);
821 }
822
823 /* if T1 or T2 is "pointer to noexcept function" and the other type is
824 "pointer to function", where the function types are otherwise the same,
825 "pointer to function" */
826 if (fnptr_conv_p (t1, t2))
827 return t1;
828 if (fnptr_conv_p (t2, t1))
829 return t2;
830
831 /* [expr.eq] permits the application of a pointer conversion to
832 bring the pointers to a common type. */
833 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
834 && CLASS_TYPE_P (TREE_TYPE (t1))
835 && CLASS_TYPE_P (TREE_TYPE (t2))
836 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
837 TREE_TYPE (t2)))
838 {
839 class1 = TREE_TYPE (t1);
840 class2 = TREE_TYPE (t2);
841
842 if (DERIVED_FROM_P (class1, class2))
843 t2 = (build_pointer_type
844 (cp_build_qualified_type (class1, cp_type_quals (class2))));
845 else if (DERIVED_FROM_P (class2, class1))
846 t1 = (build_pointer_type
847 (cp_build_qualified_type (class2, cp_type_quals (class1))));
848 else
849 {
850 if (complain & tf_error)
851 composite_pointer_error (location, kind: DK_ERROR, t1, t2, operation);
852 return error_mark_node;
853 }
854 }
855 /* [expr.eq] permits the application of a pointer-to-member
856 conversion to change the class type of one of the types. */
857 else if (TYPE_PTRMEM_P (t1)
858 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
859 TYPE_PTRMEM_CLASS_TYPE (t2)))
860 {
861 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
862 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
863
864 if (DERIVED_FROM_P (class1, class2))
865 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
866 else if (DERIVED_FROM_P (class2, class1))
867 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
868 else
869 {
870 if (complain & tf_error)
871 switch (operation)
872 {
873 case CPO_COMPARISON:
874 error_at (location, "comparison between distinct "
875 "pointer-to-member types %qT and %qT lacks a cast",
876 t1, t2);
877 break;
878 case CPO_CONVERSION:
879 error_at (location, "conversion between distinct "
880 "pointer-to-member types %qT and %qT lacks a cast",
881 t1, t2);
882 break;
883 case CPO_CONDITIONAL_EXPR:
884 error_at (location, "conditional expression between distinct "
885 "pointer-to-member types %qT and %qT lacks a cast",
886 t1, t2);
887 break;
888 default:
889 gcc_unreachable ();
890 }
891 return error_mark_node;
892 }
893 }
894
895 bool add_const = false;
896 return composite_pointer_type_r (location, t1, t2, add_const: &add_const, operation,
897 complain);
898}
899
900/* Return the merged type of two types.
901 We assume that comptypes has already been done and returned 1;
902 if that isn't so, this may crash.
903
904 This just combines attributes and default arguments; any other
905 differences would cause the two types to compare unalike. */
906
907tree
908merge_types (tree t1, tree t2)
909{
910 enum tree_code code1;
911 enum tree_code code2;
912 tree attributes;
913
914 /* Save time if the two types are the same. */
915 if (t1 == t2)
916 return t1;
917 if (original_type (t: t1) == original_type (t: t2))
918 return t1;
919
920 /* If one type is nonsense, use the other. */
921 if (t1 == error_mark_node)
922 return t2;
923 if (t2 == error_mark_node)
924 return t1;
925
926 /* Handle merging an auto redeclaration with a previous deduced
927 return type. */
928 if (is_auto (t1))
929 return t2;
930
931 /* Merge the attributes. */
932 attributes = (*targetm.merge_type_attributes) (t1, t2);
933
934 if (TYPE_PTRMEMFUNC_P (t1))
935 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
936 if (TYPE_PTRMEMFUNC_P (t2))
937 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
938
939 code1 = TREE_CODE (t1);
940 code2 = TREE_CODE (t2);
941 if (code1 != code2)
942 {
943 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
944 if (code1 == TYPENAME_TYPE)
945 {
946 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
947 code1 = TREE_CODE (t1);
948 }
949 else
950 {
951 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
952 code2 = TREE_CODE (t2);
953 }
954 }
955
956 switch (code1)
957 {
958 case POINTER_TYPE:
959 case REFERENCE_TYPE:
960 /* For two pointers, do this recursively on the target type. */
961 {
962 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
963 int quals = cp_type_quals (t1);
964
965 if (code1 == POINTER_TYPE)
966 {
967 t1 = build_pointer_type (target);
968 if (TREE_CODE (target) == METHOD_TYPE)
969 t1 = build_ptrmemfunc_type (t1);
970 }
971 else
972 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
973 t1 = build_type_attribute_variant (t1, attributes);
974 t1 = cp_build_qualified_type (t1, quals);
975
976 return t1;
977 }
978
979 case OFFSET_TYPE:
980 {
981 int quals;
982 tree pointee;
983 quals = cp_type_quals (t1);
984 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
985 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
986 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
987 pointee);
988 t1 = cp_build_qualified_type (t1, quals);
989 break;
990 }
991
992 case ARRAY_TYPE:
993 {
994 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
995 /* Save space: see if the result is identical to one of the args. */
996 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
997 return build_type_attribute_variant (t1, attributes);
998 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
999 return build_type_attribute_variant (t2, attributes);
1000 /* Merge the element types, and have a size if either arg has one. */
1001 t1 = build_cplus_array_type
1002 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
1003 break;
1004 }
1005
1006 case FUNCTION_TYPE:
1007 /* Function types: prefer the one that specified arg types.
1008 If both do, merge the arg types. Also merge the return types. */
1009 {
1010 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1011 tree p1 = TYPE_ARG_TYPES (t1);
1012 tree p2 = TYPE_ARG_TYPES (t2);
1013 tree parms;
1014
1015 /* Save space: see if the result is identical to one of the args. */
1016 if (valtype == TREE_TYPE (t1) && ! p2)
1017 return cp_build_type_attribute_variant (t1, attributes);
1018 if (valtype == TREE_TYPE (t2) && ! p1)
1019 return cp_build_type_attribute_variant (t2, attributes);
1020
1021 /* Simple way if one arg fails to specify argument types. */
1022 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1023 parms = p2;
1024 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1025 parms = p1;
1026 else
1027 parms = commonparms (p1, p2);
1028
1029 cp_cv_quals quals = type_memfn_quals (t1);
1030 cp_ref_qualifier rqual = type_memfn_rqual (t1);
1031 gcc_assert (quals == type_memfn_quals (t2));
1032 gcc_assert (rqual == type_memfn_rqual (t2));
1033
1034 tree rval = build_function_type (valtype, parms);
1035 rval = apply_memfn_quals (rval, quals);
1036 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1037 TYPE_RAISES_EXCEPTIONS (t2));
1038 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1039 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1040 break;
1041 }
1042
1043 case METHOD_TYPE:
1044 {
1045 /* Get this value the long way, since TYPE_METHOD_BASETYPE
1046 is just the main variant of this. */
1047 tree basetype = class_of_this_parm (fntype: t2);
1048 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1049 TYPE_RAISES_EXCEPTIONS (t2));
1050 cp_ref_qualifier rqual = type_memfn_rqual (t1);
1051 tree t3;
1052 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1053
1054 /* If this was a member function type, get back to the
1055 original type of type member function (i.e., without
1056 the class instance variable up front. */
1057 t1 = build_function_type (TREE_TYPE (t1),
1058 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1059 t2 = build_function_type (TREE_TYPE (t2),
1060 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1061 t3 = merge_types (t1, t2);
1062 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1063 TYPE_ARG_TYPES (t3));
1064 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1065 break;
1066 }
1067
1068 case TYPENAME_TYPE:
1069 /* There is no need to merge attributes into a TYPENAME_TYPE.
1070 When the type is instantiated it will have whatever
1071 attributes result from the instantiation. */
1072 return t1;
1073
1074 default:;
1075 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1076 return t1;
1077 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1078 return t2;
1079 break;
1080 }
1081
1082 return cp_build_type_attribute_variant (t1, attributes);
1083}
1084
1085/* Return the ARRAY_TYPE type without its domain. */
1086
1087tree
1088strip_array_domain (tree type)
1089{
1090 tree t2;
1091 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1092 if (TYPE_DOMAIN (type) == NULL_TREE)
1093 return type;
1094 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1095 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1096}
1097
1098/* Wrapper around cp_common_type that is used by c-common.cc and other
1099 front end optimizations that remove promotions.
1100
1101 Return the common type for two arithmetic types T1 and T2 under the
1102 usual arithmetic conversions. The default conversions have already
1103 been applied, and enumerated types converted to their compatible
1104 integer types. */
1105
1106tree
1107common_type (tree t1, tree t2)
1108{
1109 /* If one type is nonsense, use the other */
1110 if (t1 == error_mark_node)
1111 return t2;
1112 if (t2 == error_mark_node)
1113 return t1;
1114
1115 return cp_common_type (t1, t2);
1116}
1117
1118/* Return the common type of two pointer types T1 and T2. This is the
1119 type for the result of most arithmetic operations if the operands
1120 have the given two types.
1121
1122 We assume that comp_target_types has already been done and returned
1123 nonzero; if that isn't so, this may crash. */
1124
1125tree
1126common_pointer_type (tree t1, tree t2)
1127{
1128 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1129 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1130 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1131
1132 return composite_pointer_type (location: input_location, t1, t2,
1133 error_mark_node, error_mark_node,
1134 operation: CPO_CONVERSION, complain: tf_warning_or_error);
1135}
1136
1137/* Compare two exception specifier types for exactness or subsetness, if
1138 allowed. Returns false for mismatch, true for match (same, or
1139 derived and !exact).
1140
1141 [except.spec] "If a class X ... objects of class X or any class publicly
1142 and unambiguously derived from X. Similarly, if a pointer type Y * ...
1143 exceptions of type Y * or that are pointers to any type publicly and
1144 unambiguously derived from Y. Otherwise a function only allows exceptions
1145 that have the same type ..."
1146 This does not mention cv qualifiers and is different to what throw
1147 [except.throw] and catch [except.catch] will do. They will ignore the
1148 top level cv qualifiers, and allow qualifiers in the pointer to class
1149 example.
1150
1151 We implement the letter of the standard. */
1152
1153static bool
1154comp_except_types (tree a, tree b, bool exact)
1155{
1156 if (same_type_p (a, b))
1157 return true;
1158 else if (!exact)
1159 {
1160 if (cp_type_quals (a) || cp_type_quals (b))
1161 return false;
1162
1163 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1164 {
1165 a = TREE_TYPE (a);
1166 b = TREE_TYPE (b);
1167 if (cp_type_quals (a) || cp_type_quals (b))
1168 return false;
1169 }
1170
1171 if (TREE_CODE (a) != RECORD_TYPE
1172 || TREE_CODE (b) != RECORD_TYPE)
1173 return false;
1174
1175 if (publicly_uniquely_derived_p (a, b))
1176 return true;
1177 }
1178 return false;
1179}
1180
1181/* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1182 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1183 If EXACT is ce_type, the C++17 type compatibility rules apply.
1184 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1185 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1186 are unordered, but we've already filtered out duplicates. Most lists will
1187 be in order, we should try to make use of that. */
1188
1189bool
1190comp_except_specs (const_tree t1, const_tree t2, int exact)
1191{
1192 const_tree probe;
1193 const_tree base;
1194 int length = 0;
1195
1196 if (t1 == t2)
1197 return true;
1198
1199 /* First handle noexcept. */
1200 if (exact < ce_exact)
1201 {
1202 if (exact == ce_type
1203 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1204 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1205 return true;
1206
1207 /* noexcept(false) is compatible with no exception-specification,
1208 and less strict than any spec. */
1209 if (t1 == noexcept_false_spec)
1210 return t2 == NULL_TREE || exact == ce_derived;
1211 /* Even a derived noexcept(false) is compatible with no
1212 exception-specification. */
1213 if (t2 == noexcept_false_spec)
1214 return t1 == NULL_TREE;
1215
1216 /* Otherwise, if we aren't looking for an exact match, noexcept is
1217 equivalent to throw(). */
1218 if (t1 == noexcept_true_spec)
1219 t1 = empty_except_spec;
1220 if (t2 == noexcept_true_spec)
1221 t2 = empty_except_spec;
1222 }
1223
1224 /* If any noexcept is left, it is only comparable to itself;
1225 either we're looking for an exact match or we're redeclaring a
1226 template with dependent noexcept. */
1227 if ((t1 && TREE_PURPOSE (t1))
1228 || (t2 && TREE_PURPOSE (t2)))
1229 return (t1 && t2
1230 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1231
1232 if (t1 == NULL_TREE) /* T1 is ... */
1233 return t2 == NULL_TREE || exact == ce_derived;
1234 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1235 return t2 != NULL_TREE && !TREE_VALUE (t2);
1236 if (t2 == NULL_TREE) /* T2 is ... */
1237 return false;
1238 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1239 return exact == ce_derived;
1240
1241 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1242 Count how many we find, to determine exactness. For exact matching and
1243 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1244 O(nm). */
1245 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1246 {
1247 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1248 {
1249 tree a = TREE_VALUE (probe);
1250 tree b = TREE_VALUE (t2);
1251
1252 if (comp_except_types (a, b, exact))
1253 {
1254 if (probe == base && exact > ce_derived)
1255 base = TREE_CHAIN (probe);
1256 length++;
1257 break;
1258 }
1259 }
1260 if (probe == NULL_TREE)
1261 return false;
1262 }
1263 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1264}
1265
1266/* Compare the array types T1 and T2. CB says how we should behave when
1267 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1268 bounds_either says than any array can be [], bounds_first means that
1269 onlt T1 can be an array with unknown bounds. STRICT is true if
1270 qualifiers must match when comparing the types of the array elements. */
1271
1272static bool
1273comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1274 bool strict)
1275{
1276 tree d1;
1277 tree d2;
1278 tree max1, max2;
1279
1280 if (t1 == t2)
1281 return true;
1282
1283 /* The type of the array elements must be the same. */
1284 if (strict
1285 ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1286 : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1287 return false;
1288
1289 d1 = TYPE_DOMAIN (t1);
1290 d2 = TYPE_DOMAIN (t2);
1291
1292 if (d1 == d2)
1293 return true;
1294
1295 /* If one of the arrays is dimensionless, and the other has a
1296 dimension, they are of different types. However, it is valid to
1297 write:
1298
1299 extern int a[];
1300 int a[3];
1301
1302 by [basic.link]:
1303
1304 declarations for an array object can specify
1305 array types that differ by the presence or absence of a major
1306 array bound (_dcl.array_). */
1307 if (!d1 && d2)
1308 return cb >= bounds_either;
1309 else if (d1 && !d2)
1310 return cb == bounds_either;
1311
1312 /* Check that the dimensions are the same. */
1313
1314 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1315 return false;
1316 max1 = TYPE_MAX_VALUE (d1);
1317 max2 = TYPE_MAX_VALUE (d2);
1318
1319 if (!cp_tree_equal (max1, max2))
1320 return false;
1321
1322 return true;
1323}
1324
1325/* Compare the relative position of T1 and T2 into their respective
1326 template parameter list.
1327 T1 and T2 must be template parameter types.
1328 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1329
1330static bool
1331comp_template_parms_position (tree t1, tree t2)
1332{
1333 tree index1, index2;
1334 gcc_assert (t1 && t2
1335 && TREE_CODE (t1) == TREE_CODE (t2)
1336 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1337 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1338 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1339
1340 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1341 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1342
1343 /* Then compare their relative position. */
1344 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1345 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1346 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1347 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1348 return false;
1349
1350 /* In C++14 we can end up comparing 'auto' to a normal template
1351 parameter. Don't confuse them. */
1352 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1353 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1354
1355 return true;
1356}
1357
1358/* Heuristic check if two parameter types can be considered ABI-equivalent. */
1359
1360static bool
1361cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1362{
1363 t1 = TYPE_MAIN_VARIANT (t1);
1364 t2 = TYPE_MAIN_VARIANT (t2);
1365
1366 if (TYPE_PTR_P (t1)
1367 && TYPE_PTR_P (t2))
1368 return true;
1369
1370 /* The signedness of the parameter matters only when an integral
1371 type smaller than int is promoted to int, otherwise only the
1372 precision of the parameter matters.
1373 This check should make sure that the callee does not see
1374 undefined values in argument registers. */
1375 if (INTEGRAL_TYPE_P (t1)
1376 && INTEGRAL_TYPE_P (t2)
1377 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1378 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1379 || !targetm.calls.promote_prototypes (NULL_TREE)
1380 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1381 return true;
1382
1383 return same_type_p (t1, t2);
1384}
1385
1386/* Check if a type cast between two function types can be considered safe. */
1387
1388static bool
1389cxx_safe_function_type_cast_p (tree t1, tree t2)
1390{
1391 if (TREE_TYPE (t1) == void_type_node &&
1392 TYPE_ARG_TYPES (t1) == void_list_node)
1393 return true;
1394
1395 if (TREE_TYPE (t2) == void_type_node &&
1396 TYPE_ARG_TYPES (t2) == void_list_node)
1397 return true;
1398
1399 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1400 return false;
1401
1402 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1403 t1 && t2;
1404 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1405 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1406 return false;
1407
1408 return true;
1409}
1410
1411/* Subroutine in comptypes. */
1412
1413static bool
1414structural_comptypes (tree t1, tree t2, int strict)
1415{
1416 /* Both should be types that are not obviously the same. */
1417 gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1418
1419 /* Suppress typename resolution under spec_hasher::equal in place of calling
1420 push_to_top_level there. */
1421 if (!comparing_specializations)
1422 {
1423 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1424 current instantiation. */
1425 if (TREE_CODE (t1) == TYPENAME_TYPE)
1426 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1427
1428 if (TREE_CODE (t2) == TYPENAME_TYPE)
1429 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1430 }
1431
1432 if (TYPE_PTRMEMFUNC_P (t1))
1433 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1434 if (TYPE_PTRMEMFUNC_P (t2))
1435 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1436
1437 /* Different classes of types can't be compatible. */
1438 if (TREE_CODE (t1) != TREE_CODE (t2))
1439 return false;
1440
1441 /* Qualifiers must match. For array types, we will check when we
1442 recur on the array element types. */
1443 if (TREE_CODE (t1) != ARRAY_TYPE
1444 && cp_type_quals (t1) != cp_type_quals (t2))
1445 return false;
1446 if (TREE_CODE (t1) == FUNCTION_TYPE
1447 && type_memfn_quals (t1) != type_memfn_quals (t2))
1448 return false;
1449 /* Need to check this before TYPE_MAIN_VARIANT.
1450 FIXME function qualifiers should really change the main variant. */
1451 if (FUNC_OR_METHOD_TYPE_P (t1))
1452 {
1453 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1454 return false;
1455 if (flag_noexcept_type
1456 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1457 TYPE_RAISES_EXCEPTIONS (t2),
1458 exact: ce_type))
1459 return false;
1460 }
1461
1462 /* Allow for two different type nodes which have essentially the same
1463 definition. Note that we already checked for equality of the type
1464 qualifiers (just above). */
1465 if (TREE_CODE (t1) != ARRAY_TYPE
1466 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1467 goto check_alias;
1468
1469 /* Compare the types. Return false on known not-same. Break on not
1470 known. Never return true from this switch -- you'll break
1471 specialization comparison. */
1472 switch (TREE_CODE (t1))
1473 {
1474 case VOID_TYPE:
1475 case BOOLEAN_TYPE:
1476 /* All void and bool types are the same. */
1477 break;
1478
1479 case OPAQUE_TYPE:
1480 case INTEGER_TYPE:
1481 case FIXED_POINT_TYPE:
1482 case REAL_TYPE:
1483 /* With these nodes, we can't determine type equivalence by
1484 looking at what is stored in the nodes themselves, because
1485 two nodes might have different TYPE_MAIN_VARIANTs but still
1486 represent the same type. For example, wchar_t and int could
1487 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1488 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1489 and are distinct types. On the other hand, int and the
1490 following typedef
1491
1492 typedef int INT __attribute((may_alias));
1493
1494 have identical properties, different TYPE_MAIN_VARIANTs, but
1495 represent the same type. The canonical type system keeps
1496 track of equivalence in this case, so we fall back on it. */
1497 if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1498 return false;
1499
1500 /* We don't need or want the attribute comparison. */
1501 goto check_alias;
1502
1503 case TEMPLATE_TEMPLATE_PARM:
1504 case BOUND_TEMPLATE_TEMPLATE_PARM:
1505 if (!comp_template_parms_position (t1, t2))
1506 return false;
1507 if (!comp_template_parms
1508 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1509 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1510 return false;
1511 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1512 break;
1513 /* Don't check inheritance. */
1514 strict = COMPARE_STRICT;
1515 /* Fall through. */
1516
1517 case RECORD_TYPE:
1518 case UNION_TYPE:
1519 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1520 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1521 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1522 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1523 break;
1524
1525 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1526 break;
1527 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1528 break;
1529
1530 return false;
1531
1532 case OFFSET_TYPE:
1533 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1534 strict & ~COMPARE_REDECLARATION))
1535 return false;
1536 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1537 return false;
1538 break;
1539
1540 case REFERENCE_TYPE:
1541 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1542 return false;
1543 /* fall through to checks for pointer types */
1544 gcc_fallthrough ();
1545
1546 case POINTER_TYPE:
1547 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1548 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1549 return false;
1550 break;
1551
1552 case METHOD_TYPE:
1553 case FUNCTION_TYPE:
1554 /* Exception specs and memfn_rquals were checked above. */
1555 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1556 return false;
1557 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1558 return false;
1559 break;
1560
1561 case ARRAY_TYPE:
1562 /* Target types must match incl. qualifiers. */
1563 if (!comp_array_types (t1, t2, cb: ((strict & COMPARE_REDECLARATION)
1564 ? bounds_either : bounds_none),
1565 /*strict=*/true))
1566 return false;
1567 break;
1568
1569 case TEMPLATE_TYPE_PARM:
1570 /* If T1 and T2 don't have the same relative position in their
1571 template parameters set, they can't be equal. */
1572 if (!comp_template_parms_position (t1, t2))
1573 return false;
1574 /* If T1 and T2 don't represent the same class template deduction,
1575 they aren't equal. */
1576 if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1577 CLASS_PLACEHOLDER_TEMPLATE (t2)))
1578 return false;
1579 /* Constrained 'auto's are distinct from parms that don't have the same
1580 constraints. */
1581 if (!equivalent_placeholder_constraints (t1, t2))
1582 return false;
1583 break;
1584
1585 case TYPENAME_TYPE:
1586 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1587 TYPENAME_TYPE_FULLNAME (t2)))
1588 return false;
1589 /* Qualifiers don't matter on scopes. */
1590 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1591 TYPE_CONTEXT (t2)))
1592 return false;
1593 break;
1594
1595 case UNBOUND_CLASS_TEMPLATE:
1596 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1597 return false;
1598 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1599 return false;
1600 break;
1601
1602 case COMPLEX_TYPE:
1603 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1604 return false;
1605 break;
1606
1607 case VECTOR_TYPE:
1608 if (gnu_vector_type_p (type: t1) != gnu_vector_type_p (type: t2)
1609 || maybe_ne (a: TYPE_VECTOR_SUBPARTS (node: t1), b: TYPE_VECTOR_SUBPARTS (node: t2))
1610 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1611 return false;
1612 break;
1613
1614 case TYPE_PACK_EXPANSION:
1615 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1616 PACK_EXPANSION_PATTERN (t2))
1617 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1618 PACK_EXPANSION_EXTRA_ARGS (t2)));
1619
1620 case DECLTYPE_TYPE:
1621 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1622 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1623 return false;
1624 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1625 return false;
1626 if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1627 return false;
1628 if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1629 return false;
1630 break;
1631
1632 case TRAIT_TYPE:
1633 if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1634 return false;
1635 if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1636 || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1637 return false;
1638 break;
1639
1640 case TYPEOF_TYPE:
1641 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1642 return false;
1643 break;
1644
1645 default:
1646 return false;
1647 }
1648
1649 /* If we get here, we know that from a target independent POV the
1650 types are the same. Make sure the target attributes are also
1651 the same. */
1652 if (!comp_type_attributes (t1, t2))
1653 return false;
1654
1655 check_alias:
1656 if (comparing_dependent_aliases)
1657 {
1658 /* Don't treat an alias template specialization with dependent
1659 arguments as equivalent to its underlying type when used as a
1660 template argument; we need them to be distinct so that we
1661 substitute into the specialization arguments at instantiation
1662 time. And aliases can't be equivalent without being ==, so
1663 we don't need to look any deeper. */
1664 ++processing_template_decl;
1665 tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1666 tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1667 --processing_template_decl;
1668 if ((dep1 || dep2) && dep1 != dep2)
1669 return false;
1670 }
1671
1672 return true;
1673}
1674
1675/* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1676 is a bitwise-or of the COMPARE_* flags. */
1677
1678bool
1679comptypes (tree t1, tree t2, int strict)
1680{
1681 gcc_checking_assert (t1 && t2);
1682
1683 /* TYPE_ARGUMENT_PACKS are not really types. */
1684 gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1685 && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1686
1687 if (t1 == t2)
1688 return true;
1689
1690 /* Suppress errors caused by previously reported errors. */
1691 if (t1 == error_mark_node || t2 == error_mark_node)
1692 return false;
1693
1694 if (strict == COMPARE_STRICT)
1695 {
1696 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1697 /* At least one of the types requires structural equality, so
1698 perform a deep check. */
1699 return structural_comptypes (t1, t2, strict);
1700
1701 if (flag_checking && param_use_canonical_types)
1702 {
1703 bool result = structural_comptypes (t1, t2, strict);
1704
1705 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1706 /* The two types are structurally equivalent, but their
1707 canonical types were different. This is a failure of the
1708 canonical type propagation code.*/
1709 internal_error
1710 ("canonical types differ for identical types %qT and %qT",
1711 t1, t2);
1712 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1713 /* Two types are structurally different, but the canonical
1714 types are the same. This means we were over-eager in
1715 assigning canonical types. */
1716 internal_error
1717 ("same canonical type node for different types %qT and %qT",
1718 t1, t2);
1719
1720 return result;
1721 }
1722 if (!flag_checking && param_use_canonical_types)
1723 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1724 else
1725 return structural_comptypes (t1, t2, strict);
1726 }
1727 else if (strict == COMPARE_STRUCTURAL)
1728 return structural_comptypes (t1, t2, COMPARE_STRICT);
1729 else
1730 return structural_comptypes (t1, t2, strict);
1731}
1732
1733/* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1734 top-level qualifiers. */
1735
1736bool
1737same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1738{
1739 if (type1 == error_mark_node || type2 == error_mark_node)
1740 return false;
1741 if (type1 == type2)
1742 return true;
1743
1744 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1745 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1746 return same_type_p (type1, type2);
1747}
1748
1749/* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1750
1751bool
1752similar_type_p (tree type1, tree type2)
1753{
1754 if (type1 == error_mark_node || type2 == error_mark_node)
1755 return false;
1756
1757 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1758 * they are the same type; or
1759 * they are both pointers, and the pointed-to types are similar; or
1760 * they are both pointers to member of the same class, and the types of
1761 the pointed-to members are similar; or
1762 * they are both arrays of the same size or both arrays of unknown bound,
1763 and the array element types are similar. */
1764
1765 if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1766 return true;
1767
1768 if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1769 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1770 || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1771 return comp_ptr_ttypes_const (type1, type2, bounds_either);
1772
1773 return false;
1774}
1775
1776/* Helper function for layout_compatible_type_p and
1777 is_corresponding_member_aggr. Advance to next members (NULL if
1778 no further ones) and return true if those members are still part of
1779 the common initial sequence. */
1780
1781bool
1782next_common_initial_sequence (tree &memb1, tree &memb2)
1783{
1784 while (memb1)
1785 {
1786 if (TREE_CODE (memb1) != FIELD_DECL
1787 || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1788 {
1789 memb1 = DECL_CHAIN (memb1);
1790 continue;
1791 }
1792 if (DECL_FIELD_IS_BASE (memb1))
1793 {
1794 memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1795 continue;
1796 }
1797 break;
1798 }
1799 while (memb2)
1800 {
1801 if (TREE_CODE (memb2) != FIELD_DECL
1802 || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1803 {
1804 memb2 = DECL_CHAIN (memb2);
1805 continue;
1806 }
1807 if (DECL_FIELD_IS_BASE (memb2))
1808 {
1809 memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1810 continue;
1811 }
1812 break;
1813 }
1814 if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1815 return true;
1816 if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1817 return false;
1818 if (DECL_BIT_FIELD_TYPE (memb1))
1819 {
1820 if (!DECL_BIT_FIELD_TYPE (memb2))
1821 return false;
1822 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1823 DECL_BIT_FIELD_TYPE (memb2)))
1824 return false;
1825 if (TYPE_PRECISION (TREE_TYPE (memb1))
1826 != TYPE_PRECISION (TREE_TYPE (memb2)))
1827 return false;
1828 }
1829 else if (DECL_BIT_FIELD_TYPE (memb2))
1830 return false;
1831 else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1832 return false;
1833 if ((!lookup_attribute (attr_name: "no_unique_address", DECL_ATTRIBUTES (memb1)))
1834 != !lookup_attribute (attr_name: "no_unique_address", DECL_ATTRIBUTES (memb2)))
1835 return false;
1836 if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1837 return false;
1838 if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1839 return false;
1840 return true;
1841}
1842
1843/* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1844
1845bool
1846layout_compatible_type_p (tree type1, tree type2)
1847{
1848 if (type1 == error_mark_node || type2 == error_mark_node)
1849 return false;
1850 if (type1 == type2)
1851 return true;
1852 if (TREE_CODE (type1) != TREE_CODE (type2))
1853 return false;
1854
1855 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1856 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1857
1858 if (TREE_CODE (type1) == ENUMERAL_TYPE)
1859 return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1860 && same_type_p (finish_underlying_type (type1),
1861 finish_underlying_type (type2)));
1862
1863 if (CLASS_TYPE_P (type1)
1864 && std_layout_type_p (type1)
1865 && std_layout_type_p (type2)
1866 && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1867 {
1868 tree field1 = TYPE_FIELDS (type1);
1869 tree field2 = TYPE_FIELDS (type2);
1870 if (TREE_CODE (type1) == RECORD_TYPE)
1871 {
1872 while (1)
1873 {
1874 if (!next_common_initial_sequence (memb1&: field1, memb2&: field2))
1875 return false;
1876 if (field1 == NULL_TREE)
1877 return true;
1878 field1 = DECL_CHAIN (field1);
1879 field2 = DECL_CHAIN (field2);
1880 }
1881 }
1882 /* Otherwise both types must be union types.
1883 The standard says:
1884 "Two standard-layout unions are layout-compatible if they have
1885 the same number of non-static data members and corresponding
1886 non-static data members (in any order) have layout-compatible
1887 types."
1888 but the code anticipates that bitfield vs. non-bitfield,
1889 different bitfield widths or presence/absence of
1890 [[no_unique_address]] should be checked as well. */
1891 auto_vec<tree, 16> vec;
1892 unsigned int count = 0;
1893 for (; field1; field1 = DECL_CHAIN (field1))
1894 if (TREE_CODE (field1) == FIELD_DECL)
1895 count++;
1896 for (; field2; field2 = DECL_CHAIN (field2))
1897 if (TREE_CODE (field2) == FIELD_DECL)
1898 vec.safe_push (obj: field2);
1899 /* Discussions on core lean towards treating multiple union fields
1900 of the same type as the same field, so this might need changing
1901 in the future. */
1902 if (count != vec.length ())
1903 return false;
1904 for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1905 {
1906 if (TREE_CODE (field1) != FIELD_DECL)
1907 continue;
1908 unsigned int j;
1909 tree t1 = DECL_BIT_FIELD_TYPE (field1);
1910 if (t1 == NULL_TREE)
1911 t1 = TREE_TYPE (field1);
1912 FOR_EACH_VEC_ELT (vec, j, field2)
1913 {
1914 tree t2 = DECL_BIT_FIELD_TYPE (field2);
1915 if (t2 == NULL_TREE)
1916 t2 = TREE_TYPE (field2);
1917 if (DECL_BIT_FIELD_TYPE (field1))
1918 {
1919 if (!DECL_BIT_FIELD_TYPE (field2))
1920 continue;
1921 if (TYPE_PRECISION (TREE_TYPE (field1))
1922 != TYPE_PRECISION (TREE_TYPE (field2)))
1923 continue;
1924 }
1925 else if (DECL_BIT_FIELD_TYPE (field2))
1926 continue;
1927 if (!layout_compatible_type_p (type1: t1, type2: t2))
1928 continue;
1929 if ((!lookup_attribute (attr_name: "no_unique_address",
1930 DECL_ATTRIBUTES (field1)))
1931 != !lookup_attribute (attr_name: "no_unique_address",
1932 DECL_ATTRIBUTES (field2)))
1933 continue;
1934 break;
1935 }
1936 if (j == vec.length ())
1937 return false;
1938 vec.unordered_remove (ix: j);
1939 }
1940 return true;
1941 }
1942
1943 return same_type_p (type1, type2);
1944}
1945
1946/* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1947
1948bool
1949at_least_as_qualified_p (const_tree type1, const_tree type2)
1950{
1951 int q1 = cp_type_quals (type1);
1952 int q2 = cp_type_quals (type2);
1953
1954 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1955 return (q1 & q2) == q2;
1956}
1957
1958/* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1959 more cv-qualified that TYPE1, and 0 otherwise. */
1960
1961int
1962comp_cv_qualification (int q1, int q2)
1963{
1964 if (q1 == q2)
1965 return 0;
1966
1967 if ((q1 & q2) == q2)
1968 return 1;
1969 else if ((q1 & q2) == q1)
1970 return -1;
1971
1972 return 0;
1973}
1974
1975int
1976comp_cv_qualification (const_tree type1, const_tree type2)
1977{
1978 int q1 = cp_type_quals (type1);
1979 int q2 = cp_type_quals (type2);
1980 return comp_cv_qualification (q1, q2);
1981}
1982
1983/* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1984 subset of the cv-qualification signature of TYPE2, and the types
1985 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1986
1987int
1988comp_cv_qual_signature (tree type1, tree type2)
1989{
1990 if (comp_ptr_ttypes_real (type2, type1, -1))
1991 return 1;
1992 else if (comp_ptr_ttypes_real (type1, type2, -1))
1993 return -1;
1994 else
1995 return 0;
1996}
1997
1998/* Subroutines of `comptypes'. */
1999
2000/* Return true if two parameter type lists PARMS1 and PARMS2 are
2001 equivalent in the sense that functions with those parameter types
2002 can have equivalent types. The two lists must be equivalent,
2003 element by element. */
2004
2005bool
2006compparms (const_tree parms1, const_tree parms2)
2007{
2008 const_tree t1, t2;
2009
2010 /* An unspecified parmlist matches any specified parmlist
2011 whose argument types don't need default promotions. */
2012
2013 for (t1 = parms1, t2 = parms2;
2014 t1 || t2;
2015 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2016 {
2017 /* If one parmlist is shorter than the other,
2018 they fail to match. */
2019 if (!t1 || !t2)
2020 return false;
2021 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2022 return false;
2023 }
2024 return true;
2025}
2026
2027
2028/* Process a sizeof or alignof expression where the operand is a type.
2029 STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2030 or GNU (preferred alignment) semantics; it is ignored if OP is
2031 SIZEOF_EXPR. */
2032
2033tree
2034cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2035 bool std_alignof, bool complain)
2036{
2037 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2038 if (type == error_mark_node)
2039 return error_mark_node;
2040
2041 type = non_reference (type);
2042 if (TREE_CODE (type) == METHOD_TYPE)
2043 {
2044 if (complain)
2045 {
2046 pedwarn (loc, OPT_Wpointer_arith,
2047 "invalid application of %qs to a member function",
2048 OVL_OP_INFO (false, op)->name);
2049 return size_one_node;
2050 }
2051 else
2052 return error_mark_node;
2053 }
2054 else if (VOID_TYPE_P (type) && std_alignof)
2055 {
2056 if (complain)
2057 error_at (loc, "invalid application of %qs to a void type",
2058 OVL_OP_INFO (false, op)->name);
2059 return error_mark_node;
2060 }
2061
2062 bool dependent_p = dependent_type_p (type);
2063 if (!dependent_p)
2064 complete_type (type);
2065 if (dependent_p
2066 /* VLA types will have a non-constant size. In the body of an
2067 uninstantiated template, we don't need to try to compute the
2068 value, because the sizeof expression is not an integral
2069 constant expression in that case. And, if we do try to
2070 compute the value, we'll likely end up with SAVE_EXPRs, which
2071 the template substitution machinery does not expect to see. */
2072 || (processing_template_decl
2073 && COMPLETE_TYPE_P (type)
2074 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2075 {
2076 tree value = build_min (op, size_type_node, type);
2077 TREE_READONLY (value) = 1;
2078 if (op == ALIGNOF_EXPR && std_alignof)
2079 ALIGNOF_EXPR_STD_P (value) = true;
2080 SET_EXPR_LOCATION (value, loc);
2081 return value;
2082 }
2083
2084 return c_sizeof_or_alignof_type (loc, complete_type (type),
2085 op == SIZEOF_EXPR, std_alignof,
2086 complain);
2087}
2088
2089/* Return the size of the type, without producing any warnings for
2090 types whose size cannot be taken. This routine should be used only
2091 in some other routine that has already produced a diagnostic about
2092 using the size of such a type. */
2093tree
2094cxx_sizeof_nowarn (tree type)
2095{
2096 if (TREE_CODE (type) == FUNCTION_TYPE
2097 || VOID_TYPE_P (type)
2098 || TREE_CODE (type) == ERROR_MARK)
2099 return size_one_node;
2100 else if (!COMPLETE_TYPE_P (type))
2101 return size_zero_node;
2102 else
2103 return cxx_sizeof_or_alignof_type (loc: input_location, type,
2104 op: SIZEOF_EXPR, std_alignof: false, complain: false);
2105}
2106
2107/* Process a sizeof expression where the operand is an expression. */
2108
2109static tree
2110cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2111{
2112 if (e == error_mark_node)
2113 return error_mark_node;
2114
2115 if (instantiation_dependent_uneval_expression_p (e))
2116 {
2117 e = build_min (SIZEOF_EXPR, size_type_node, e);
2118 TREE_SIDE_EFFECTS (e) = 0;
2119 TREE_READONLY (e) = 1;
2120 SET_EXPR_LOCATION (e, loc);
2121
2122 return e;
2123 }
2124
2125 location_t e_loc = cp_expr_loc_or_loc (t: e, or_loc: loc);
2126 STRIP_ANY_LOCATION_WRAPPER (e);
2127
2128 /* To get the size of a static data member declared as an array of
2129 unknown bound, we need to instantiate it. */
2130 if (VAR_P (e)
2131 && VAR_HAD_UNKNOWN_BOUND (e)
2132 && DECL_TEMPLATE_INSTANTIATION (e))
2133 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
2134
2135 if (TREE_CODE (e) == PARM_DECL
2136 && DECL_ARRAY_PARAMETER_P (e)
2137 && (complain & tf_warning))
2138 {
2139 auto_diagnostic_group d;
2140 if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2141 "%<sizeof%> on array function parameter %qE "
2142 "will return size of %qT", e, TREE_TYPE (e)))
2143 inform (DECL_SOURCE_LOCATION (e), "declared here");
2144 }
2145
2146 e = mark_type_use (e);
2147
2148 if (bitfield_p (e))
2149 {
2150 if (complain & tf_error)
2151 error_at (e_loc,
2152 "invalid application of %<sizeof%> to a bit-field");
2153 else
2154 return error_mark_node;
2155 e = char_type_node;
2156 }
2157 else if (is_overloaded_fn (e))
2158 {
2159 if (complain & tf_error)
2160 permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2161 "an expression of function type");
2162 else
2163 return error_mark_node;
2164 e = char_type_node;
2165 }
2166 else if (type_unknown_p (expr: e))
2167 {
2168 if (complain & tf_error)
2169 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2170 else
2171 return error_mark_node;
2172 e = char_type_node;
2173 }
2174 else
2175 e = TREE_TYPE (e);
2176
2177 return cxx_sizeof_or_alignof_type (loc, type: e, op: SIZEOF_EXPR, std_alignof: false,
2178 complain: complain & tf_error);
2179}
2180
2181/* Implement the __alignof keyword: Return the minimum required
2182 alignment of E, measured in bytes. For VAR_DECL's and
2183 FIELD_DECL's return DECL_ALIGN (which can be set from an
2184 "aligned" __attribute__ specification). STD_ALIGNOF acts
2185 like in cxx_sizeof_or_alignof_type. */
2186
2187static tree
2188cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2189 tsubst_flags_t complain)
2190{
2191 tree t;
2192
2193 if (e == error_mark_node)
2194 return error_mark_node;
2195
2196 if (processing_template_decl)
2197 {
2198 e = build_min (ALIGNOF_EXPR, size_type_node, e);
2199 TREE_SIDE_EFFECTS (e) = 0;
2200 TREE_READONLY (e) = 1;
2201 SET_EXPR_LOCATION (e, loc);
2202 ALIGNOF_EXPR_STD_P (e) = std_alignof;
2203
2204 return e;
2205 }
2206
2207 location_t e_loc = cp_expr_loc_or_loc (t: e, or_loc: loc);
2208 STRIP_ANY_LOCATION_WRAPPER (e);
2209
2210 e = mark_type_use (e);
2211
2212 if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2213 !(complain & tf_error)))
2214 {
2215 if (!(complain & tf_error))
2216 return error_mark_node;
2217 t = size_one_node;
2218 }
2219 else if (VAR_P (e))
2220 t = size_int (DECL_ALIGN_UNIT (e));
2221 else if (bitfield_p (e))
2222 {
2223 if (complain & tf_error)
2224 error_at (e_loc,
2225 "invalid application of %<__alignof%> to a bit-field");
2226 else
2227 return error_mark_node;
2228 t = size_one_node;
2229 }
2230 else if (TREE_CODE (e) == COMPONENT_REF
2231 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2232 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2233 else if (is_overloaded_fn (e))
2234 {
2235 if (complain & tf_error)
2236 permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2237 "an expression of function type");
2238 else
2239 return error_mark_node;
2240 if (TREE_CODE (e) == FUNCTION_DECL)
2241 t = size_int (DECL_ALIGN_UNIT (e));
2242 else
2243 t = size_one_node;
2244 }
2245 else if (type_unknown_p (expr: e))
2246 {
2247 if (complain & tf_error)
2248 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2249 else
2250 return error_mark_node;
2251 t = size_one_node;
2252 }
2253 else
2254 return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2255 op: ALIGNOF_EXPR, std_alignof,
2256 complain: complain & tf_error);
2257
2258 return fold_convert_loc (loc, size_type_node, t);
2259}
2260
2261/* Process a sizeof or alignof expression E with code OP where the operand
2262 is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2263
2264tree
2265cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2266 bool std_alignof, bool complain)
2267{
2268 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2269 if (op == SIZEOF_EXPR)
2270 return cxx_sizeof_expr (loc, e, complain: complain? tf_warning_or_error : tf_none);
2271 else
2272 return cxx_alignof_expr (loc, e, std_alignof,
2273 complain: complain? tf_warning_or_error : tf_none);
2274}
2275
2276/* Build a representation of an expression 'alignas(E).' Return the
2277 folded integer value of E if it is an integral constant expression
2278 that resolves to a valid alignment. If E depends on a template
2279 parameter, return a syntactic representation tree of kind
2280 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2281 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2282
2283tree
2284cxx_alignas_expr (tree e)
2285{
2286 if (e == NULL_TREE || e == error_mark_node
2287 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2288 return e;
2289
2290 if (TYPE_P (e))
2291 /* [dcl.align]/3:
2292
2293 When the alignment-specifier is of the form
2294 alignas(type-id), it shall have the same effect as
2295 alignas(alignof(type-id)). */
2296
2297 return cxx_sizeof_or_alignof_type (loc: input_location,
2298 type: e, op: ALIGNOF_EXPR,
2299 /*std_alignof=*/true,
2300 /*complain=*/true);
2301
2302 /* If we reach this point, it means the alignas expression if of
2303 the form "alignas(assignment-expression)", so we should follow
2304 what is stated by [dcl.align]/2. */
2305
2306 if (value_dependent_expression_p (e))
2307 /* Leave value-dependent expression alone for now. */
2308 return e;
2309
2310 e = instantiate_non_dependent_expr (e);
2311 e = mark_rvalue_use (e);
2312
2313 /* [dcl.align]/2 says:
2314
2315 the assignment-expression shall be an integral constant
2316 expression. */
2317
2318 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2319 {
2320 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2321 return error_mark_node;
2322 }
2323
2324 return cxx_constant_value (e);
2325}
2326
2327
2328/* EXPR is being used in a context that is not a function call.
2329 Enforce:
2330
2331 [expr.ref]
2332
2333 The expression can be used only as the left-hand operand of a
2334 member function call.
2335
2336 [expr.mptr.operator]
2337
2338 If the result of .* or ->* is a function, then that result can be
2339 used only as the operand for the function call operator ().
2340
2341 by issuing an error message if appropriate. Returns true iff EXPR
2342 violates these rules. */
2343
2344bool
2345invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2346{
2347 if (expr == NULL_TREE)
2348 return false;
2349 /* Don't enforce this in MS mode. */
2350 if (flag_ms_extensions)
2351 return false;
2352 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2353 expr = get_first_fn (expr);
2354 if (TREE_TYPE (expr)
2355 && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2356 {
2357 if (complain & tf_error)
2358 {
2359 if (DECL_P (expr))
2360 {
2361 error_at (loc, "invalid use of non-static member function %qD",
2362 expr);
2363 inform (DECL_SOURCE_LOCATION (expr), "declared here");
2364 }
2365 else
2366 error_at (loc, "invalid use of non-static member function of "
2367 "type %qT", TREE_TYPE (expr));
2368 }
2369 return true;
2370 }
2371 return false;
2372}
2373
2374/* If EXP is a reference to a bit-field, and the type of EXP does not
2375 match the declared type of the bit-field, return the declared type
2376 of the bit-field. Otherwise, return NULL_TREE. */
2377
2378tree
2379is_bitfield_expr_with_lowered_type (const_tree exp)
2380{
2381 switch (TREE_CODE (exp))
2382 {
2383 case COND_EXPR:
2384 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2385 ? TREE_OPERAND (exp, 1)
2386 : TREE_OPERAND (exp, 0)))
2387 return NULL_TREE;
2388 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2389
2390 case COMPOUND_EXPR:
2391 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2392
2393 case MODIFY_EXPR:
2394 case SAVE_EXPR:
2395 case UNARY_PLUS_EXPR:
2396 case PREDECREMENT_EXPR:
2397 case PREINCREMENT_EXPR:
2398 case POSTDECREMENT_EXPR:
2399 case POSTINCREMENT_EXPR:
2400 case NEGATE_EXPR:
2401 case NON_LVALUE_EXPR:
2402 case BIT_NOT_EXPR:
2403 case CLEANUP_POINT_EXPR:
2404 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2405
2406 case COMPONENT_REF:
2407 {
2408 tree field;
2409
2410 field = TREE_OPERAND (exp, 1);
2411 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2412 return NULL_TREE;
2413 if (same_type_ignoring_top_level_qualifiers_p
2414 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2415 return NULL_TREE;
2416 return DECL_BIT_FIELD_TYPE (field);
2417 }
2418
2419 case VAR_DECL:
2420 if (DECL_HAS_VALUE_EXPR_P (exp))
2421 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2422 (CONST_CAST_TREE (exp)));
2423 return NULL_TREE;
2424
2425 case VIEW_CONVERT_EXPR:
2426 if (location_wrapper_p (exp))
2427 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2428 else
2429 return NULL_TREE;
2430
2431 default:
2432 return NULL_TREE;
2433 }
2434}
2435
2436/* Like is_bitfield_with_lowered_type, except that if EXP is not a
2437 bitfield with a lowered type, the type of EXP is returned, rather
2438 than NULL_TREE. */
2439
2440tree
2441unlowered_expr_type (const_tree exp)
2442{
2443 tree type;
2444 tree etype = TREE_TYPE (exp);
2445
2446 type = is_bitfield_expr_with_lowered_type (exp);
2447 if (type)
2448 type = cp_build_qualified_type (type, cp_type_quals (etype));
2449 else
2450 type = etype;
2451
2452 return type;
2453}
2454
2455/* Perform the conversions in [expr] that apply when an lvalue appears
2456 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2457 function-to-pointer conversions. In addition, bitfield references are
2458 converted to their declared types. Note that this function does not perform
2459 the lvalue-to-rvalue conversion for class types. If you need that conversion
2460 for class types, then you probably need to use force_rvalue.
2461
2462 Although the returned value is being used as an rvalue, this
2463 function does not wrap the returned expression in a
2464 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2465 that the return value is no longer an lvalue. */
2466
2467tree
2468decay_conversion (tree exp,
2469 tsubst_flags_t complain,
2470 bool reject_builtin /* = true */)
2471{
2472 tree type;
2473 enum tree_code code;
2474 location_t loc = cp_expr_loc_or_input_loc (t: exp);
2475
2476 type = TREE_TYPE (exp);
2477 if (type == error_mark_node)
2478 return error_mark_node;
2479
2480 exp = resolve_nondeduced_context_or_error (exp, complain);
2481
2482 code = TREE_CODE (type);
2483
2484 if (error_operand_p (t: exp))
2485 return error_mark_node;
2486
2487 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2488 {
2489 mark_rvalue_use (exp, loc, reject_builtin);
2490 return nullptr_node;
2491 }
2492
2493 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2494 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2495 if (code == VOID_TYPE)
2496 {
2497 if (complain & tf_error)
2498 error_at (loc, "void value not ignored as it ought to be");
2499 return error_mark_node;
2500 }
2501 if (invalid_nonstatic_memfn_p (loc, expr: exp, complain))
2502 return error_mark_node;
2503 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2504 {
2505 exp = mark_lvalue_use (exp);
2506 if (reject_builtin && reject_gcc_builtin (exp, loc))
2507 return error_mark_node;
2508 return cp_build_addr_expr (exp, complain);
2509 }
2510 if (code == ARRAY_TYPE)
2511 {
2512 tree adr;
2513 tree ptrtype;
2514
2515 exp = mark_lvalue_use (exp);
2516
2517 if (INDIRECT_REF_P (exp))
2518 return build_nop (build_pointer_type (TREE_TYPE (type)),
2519 TREE_OPERAND (exp, 0));
2520
2521 if (TREE_CODE (exp) == COMPOUND_EXPR)
2522 {
2523 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2524 if (op1 == error_mark_node)
2525 return error_mark_node;
2526 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2527 TREE_OPERAND (exp, 0), op1);
2528 }
2529
2530 if (!obvalue_p (exp)
2531 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2532 {
2533 if (complain & tf_error)
2534 error_at (loc, "invalid use of non-lvalue array");
2535 return error_mark_node;
2536 }
2537
2538 ptrtype = build_pointer_type (TREE_TYPE (type));
2539
2540 if (VAR_P (exp))
2541 {
2542 if (!cxx_mark_addressable (exp))
2543 return error_mark_node;
2544 adr = build_nop (ptrtype, build_address (exp));
2545 return adr;
2546 }
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);
2551 }
2552
2553 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2554 exp = mark_rvalue_use (exp, loc, reject_builtin);
2555
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);
2564
2565 /* We do not call rvalue() here because we do not want to wrap EXP
2566 in a NON_LVALUE_EXPR. */
2567
2568 /* [basic.lval]
2569
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);
2574
2575 if (!complete_type_or_maybe_complain (type, value: exp, complain))
2576 return error_mark_node;
2577
2578 return exp;
2579}
2580
2581/* Perform preparatory conversions, as part of the "usual arithmetic
2582 conversions". In particular, as per [expr]:
2583
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.
2589
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. */
2593
2594static tree
2595cp_default_conversion (tree exp, tsubst_flags_t complain)
2596{
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);
2608
2609 return exp;
2610}
2611
2612/* C version. */
2613
2614tree
2615default_conversion (tree exp)
2616{
2617 return cp_default_conversion (exp, complain: tf_warning_or_error);
2618}
2619
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. */
2623
2624tree
2625cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2626{
2627 tree type;
2628 tree promoted_type;
2629
2630 expr = mark_rvalue_use (expr);
2631 if (error_operand_p (t: expr))
2632 return error_mark_node;
2633
2634 type = TREE_TYPE (expr);
2635
2636 /* [conv.prom]
2637
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 (exp: 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;
2650
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;
2662}
2663
2664/* C version. */
2665
2666tree
2667perform_integral_promotions (tree expr)
2668{
2669 return cp_perform_integral_promotions (expr, complain: tf_warning_or_error);
2670}
2671
2672/* Returns nonzero iff exp is a STRING_CST or the result of applying
2673 decay_conversion to one. */
2674
2675int
2676string_conv_p (const_tree totype, const_tree exp, int warn)
2677{
2678 tree t;
2679
2680 if (!TYPE_PTR_P (totype))
2681 return 0;
2682
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;
2690
2691 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2692
2693 STRIP_ANY_LOCATION_WRAPPER (exp);
2694
2695 if (TREE_CODE (exp) == STRING_CST)
2696 {
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;
2700 }
2701 else
2702 {
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;
2711 }
2712 if (warn)
2713 {
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);
2722 }
2723
2724 return 1;
2725}
2726
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. */
2734
2735static tree
2736rationalize_conditional_expr (enum tree_code code, tree t,
2737 tsubst_flags_t complain)
2738{
2739 location_t loc = cp_expr_loc_or_input_loc (t);
2740
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)
2745 {
2746 tree op0 = TREE_OPERAND (t, 0);
2747 tree op1 = TREE_OPERAND (t, 1);
2748
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);
2765 }
2766
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);
2773
2774 return
2775 build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2776}
2777
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. */
2782
2783tree
2784lookup_anon_field (tree, tree type)
2785{
2786 tree field;
2787
2788 type = TYPE_MAIN_VARIANT (type);
2789 field = ANON_AGGR_TYPE_FIELD (type);
2790 gcc_assert (field);
2791 return field;
2792}
2793
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.
2800
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. */
2805
2806tree
2807build_class_member_access_expr (cp_expr object, tree member,
2808 tree access_path, bool preserve_reference,
2809 tsubst_flags_t complain)
2810{
2811 tree object_type;
2812 tree member_scope;
2813 tree result = NULL_TREE;
2814 tree using_decl = NULL_TREE;
2815
2816 if (error_operand_p (t: object) || error_operand_p (t: member))
2817 return error_mark_node;
2818
2819 gcc_assert (DECL_P (member) || BASELINK_P (member));
2820
2821 /* [expr.ref]
2822
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 (type: object_type, value: object, complain))
2828 return error_mark_node;
2829 if (!CLASS_TYPE_P (object_type))
2830 {
2831 if (complain & tf_error)
2832 {
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);
2841 }
2842 return error_mark_node;
2843 }
2844
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))
2849 {
2850 member_scope = DECL_CLASS_CONTEXT (member);
2851 if (!mark_used (member, complain) && !(complain & tf_error))
2852 return error_mark_node;
2853
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);
2858 }
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 (type1: member_scope,
2866 type2: object_type))
2867 member_scope = TYPE_CONTEXT (member_scope);
2868 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2869 {
2870 if (complain & tf_error)
2871 {
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);
2876 }
2877 return error_mark_node;
2878 }
2879
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))
2884 {
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;
2890 }
2891
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))
2895 {
2896 /* A static data member. */
2897 result = member;
2898 mark_exp_read (object);
2899
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;
2904
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);
2908 }
2909 else if (TREE_CODE (member) == FIELD_DECL)
2910 {
2911 /* A non-static data member. */
2912 bool null_object_p;
2913 int type_quals;
2914 tree member_type;
2915
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;
2921
2922 /* Convert OBJECT to the type of MEMBER. */
2923 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2924 TYPE_MAIN_VARIANT (member_scope)))
2925 {
2926 tree binfo;
2927 base_kind kind;
2928
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 (type: object_type, value: object,
2935 complain))
2936 return error_mark_node;
2937
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;
2942
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)
2947 {
2948 if (complain & tf_error)
2949 {
2950 error ("invalid access to non-static data member %qD in "
2951 "virtual base of NULL object", member);
2952 }
2953 return error_mark_node;
2954 }
2955
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);
2962 }
2963
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))))
2977 {
2978 tree anonymous_union;
2979
2980 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2981 DECL_CONTEXT (member));
2982 object = build_class_member_access_expr (object,
2983 member: anonymous_union,
2984 /*access_path=*/NULL_TREE,
2985 preserve_reference,
2986 complain);
2987 }
2988
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))
2993 {
2994 type_quals = (cp_type_quals (member_type)
2995 | cp_type_quals (object_type));
2996
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);
3003 }
3004
3005 result = build3_loc (loc: input_location, code: COMPONENT_REF, type: member_type,
3006 arg0: object, arg1: member, NULL_TREE);
3007
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;
3015 }
3016 else if (BASELINK_P (member))
3017 {
3018 /* The member is a (possibly overloaded) member function. */
3019 tree functions;
3020 tree type;
3021
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 (loc: input_location, code: COMPONENT_REF, type, arg0: object, arg1: member,
3034 NULL_TREE);
3035 }
3036 else if (TREE_CODE (member) == CONST_DECL)
3037 {
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);
3044 }
3045 else if ((using_decl = strip_using_decl (member)) != member)
3046 result = build_class_member_access_expr (object,
3047 member: using_decl,
3048 access_path, preserve_reference,
3049 complain);
3050 else
3051 {
3052 if (complain & tf_error)
3053 error ("invalid use of %qD", member);
3054 return error_mark_node;
3055 }
3056
3057 if (!preserve_reference)
3058 /* [expr.ref]
3059
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);
3063
3064 return result;
3065}
3066
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. */
3069
3070tree
3071lookup_destructor (tree object, tree scope, tree dtor_name,
3072 tsubst_flags_t complain)
3073{
3074 tree object_type = TREE_TYPE (object);
3075 tree dtor_type = TREE_OPERAND (dtor_name, 0);
3076 tree expr;
3077
3078 /* We've already complained about this destructor. */
3079 if (dtor_type == error_mark_node)
3080 return error_mark_node;
3081
3082 if (scope && !check_dtor_name (scope, dtor_type))
3083 {
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;
3088 }
3089 if (is_auto (dtor_type))
3090 dtor_type = object_type;
3091 else if (identifier_p (t: dtor_type))
3092 {
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
3098 {
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;
3103 }
3104
3105 }
3106 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3107 {
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;
3112 }
3113 expr = lookup_member (dtor_type, complete_dtor_identifier,
3114 /*protect=*/1, /*want_type=*/false,
3115 tf_warning_or_error);
3116 if (!expr)
3117 {
3118 if (complain & tf_error)
3119 cxx_incomplete_type_error (value: dtor_name, type: dtor_type);
3120 return error_mark_node;
3121 }
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;
3130}
3131
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. */
3135
3136void
3137check_template_keyword (tree decl)
3138{
3139 /* The standard says:
3140
3141 [temp.names]
3142
3143 If a name prefixed by the keyword template is not a member
3144 template, the program is ill-formed.
3145
3146 DR 228 removed the restriction that the template be a member
3147 template.
3148
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)
3158 {
3159 if (VAR_P (decl))
3160 {
3161 if (DECL_USE_TEMPLATE (decl)
3162 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3163 ;
3164 else
3165 permerror (input_location, "%qD is not a template", decl);
3166 }
3167 else if (!is_overloaded_fn (decl))
3168 permerror (input_location, "%qD is not a template", decl);
3169 else
3170 {
3171 bool found = false;
3172
3173 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3174 !found && iter; ++iter)
3175 {
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;
3183 }
3184 if (!found)
3185 permerror (input_location, "%qD is not a template", decl);
3186 }
3187 }
3188}
3189
3190/* Record that an access failure occurred on BASETYPE_PATH attempting
3191 to access DECL, where DIAG_DECL should be used for diagnostics. */
3192
3193void
3194access_failure_info::record_access_failure (tree basetype_path,
3195 tree decl, tree diag_decl)
3196{
3197 m_was_inaccessible = true;
3198 m_basetype_path = basetype_path;
3199 m_decl = decl;
3200 m_diag_decl = diag_decl;
3201}
3202
3203/* If an access failure was recorded, then attempt to locate an
3204 accessor function for the pertinent field.
3205 Otherwise, return NULL_TREE. */
3206
3207tree
3208access_failure_info::get_any_accessor (bool const_p) const
3209{
3210 if (!was_inaccessible_p ())
3211 return NULL_TREE;
3212
3213 tree accessor
3214 = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3215 if (!accessor)
3216 return NULL_TREE;
3217
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;
3222
3223 return accessor;
3224}
3225
3226/* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3227 replacing the primary location in RICHLOC with "accessor()". */
3228
3229void
3230access_failure_info::add_fixit_hint (rich_location *richloc,
3231 tree accessor_decl)
3232{
3233 pretty_printer pp;
3234 pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3235 pp_string (&pp, "()");
3236 richloc->add_fixit_replace (new_content: pp_formatted_text (&pp));
3237}
3238
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. */
3242
3243void
3244access_failure_info::maybe_suggest_accessor (bool const_p) const
3245{
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: &richloc, accessor_decl: accessor);
3251 inform (&richloc, "field %q#D can be accessed via %q#D",
3252 m_diag_decl, accessor);
3253}
3254
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. */
3259
3260static void
3261complain_about_unrecognized_member (tree access_path, tree name,
3262 tree object_type)
3263{
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)
3268 {
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;
3274 }
3275
3276 location_t bogus_component_loc = input_location;
3277 gcc_rich_location rich_loc (bogus_component_loc);
3278
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: &afi);
3284 if (afi.was_inaccessible_p ())
3285 {
3286 tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3287 if (accessor)
3288 {
3289 /* The guessed name isn't directly accessible, but can be accessed
3290 via an accessor member function. */
3291 afi.add_fixit_hint (richloc: &rich_loc, accessor_decl: 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);
3298 }
3299 else
3300 {
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);
3311 }
3312 }
3313 else
3314 {
3315 /* The guessed name is directly accessible; suggest it. */
3316 rich_loc.add_fixit_misspelled_id (misspelled_token_loc: bogus_component_loc,
3317 hint_id: 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);
3324 }
3325}
3326
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. */
3335
3336tree
3337finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3338 tsubst_flags_t complain)
3339{
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;
3346
3347 if (object == error_mark_node || name == error_mark_node)
3348 return error_mark_node;
3349
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;
3353
3354 object_type = TREE_TYPE (object);
3355
3356 if (processing_template_decl)
3357 {
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))))
3375 {
3376 dependent:
3377 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3378 orig_object, orig_name, NULL_TREE);
3379 }
3380 }
3381 else if (c_dialect_objc ()
3382 && identifier_p (t: name)
3383 && (expr = objc_maybe_build_component_ref (object, name)))
3384 return expr;
3385
3386 /* [expr.ref]
3387
3388 The type of the first expression shall be "class object" (of a
3389 complete type). */
3390 if (!currently_open_class (object_type)
3391 && !complete_type_or_maybe_complain (type: object_type, value: object, complain))
3392 return error_mark_node;
3393 if (!CLASS_TYPE_P (object_type))
3394 {
3395 if (complain & tf_error)
3396 {
3397 if (INDIRECT_TYPE_P (object_type)
3398 && CLASS_TYPE_P (TREE_TYPE (object_type)))
3399 error ("request for member %qD in %qE, which is of pointer "
3400 "type %qT (maybe you meant to use %<->%> ?)",
3401 name, object.get_value (), object_type);
3402 else
3403 error ("request for member %qD in %qE, which is of non-class "
3404 "type %qT", name, object.get_value (), object_type);
3405 }
3406 return error_mark_node;
3407 }
3408
3409 if (BASELINK_P (name))
3410 /* A member function that has already been looked up. */
3411 member = name;
3412 else
3413 {
3414 bool is_template_id = false;
3415 tree template_args = NULL_TREE;
3416 tree scope = NULL_TREE;
3417
3418 access_path = object_type;
3419
3420 if (TREE_CODE (name) == SCOPE_REF)
3421 {
3422 /* A qualified name. The qualifying class or namespace `S'
3423 has already been looked up; it is either a TYPE or a
3424 NAMESPACE_DECL. */
3425 scope = TREE_OPERAND (name, 0);
3426 name = TREE_OPERAND (name, 1);
3427
3428 /* If SCOPE is a namespace, then the qualified name does not
3429 name a member of OBJECT_TYPE. */
3430 if (TREE_CODE (scope) == NAMESPACE_DECL)
3431 {
3432 if (complain & tf_error)
3433 error ("%<%D::%D%> is not a member of %qT",
3434 scope, name, object_type);
3435 return error_mark_node;
3436 }
3437 }
3438
3439 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3440 {
3441 is_template_id = true;
3442 template_args = TREE_OPERAND (name, 1);
3443 name = TREE_OPERAND (name, 0);
3444
3445 if (!identifier_p (t: name))
3446 name = OVL_NAME (name);
3447 }
3448
3449 if (scope)
3450 {
3451 if (TREE_CODE (scope) == ENUMERAL_TYPE)
3452 {
3453 gcc_assert (!is_template_id);
3454 /* Looking up a member enumerator (c++/56793). */
3455 if (!TYPE_CLASS_SCOPE_P (scope)
3456 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3457 {
3458 if (complain & tf_error)
3459 error ("%<%D::%D%> is not a member of %qT",
3460 scope, name, object_type);
3461 return error_mark_node;
3462 }
3463 tree val = lookup_enumerator (scope, name);
3464 if (!val)
3465 {
3466 if (complain & tf_error)
3467 error ("%qD is not a member of %qD",
3468 name, scope);
3469 return error_mark_node;
3470 }
3471
3472 if (TREE_SIDE_EFFECTS (object))
3473 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3474 return val;
3475 }
3476
3477 gcc_assert (CLASS_TYPE_P (scope));
3478 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3479
3480 if (constructor_name_p (name, scope))
3481 {
3482 if (complain & tf_error)
3483 error ("cannot call constructor %<%T::%D%> directly",
3484 scope, name);
3485 return error_mark_node;
3486 }
3487
3488 /* NAME may refer to a static data member, in which case there is
3489 one copy of the data member that is shared by all the objects of
3490 the class. So NAME can be unambiguously referred to even if
3491 there are multiple indirect base classes containing NAME. */
3492 const base_access ba = [scope, name] ()
3493 {
3494 if (identifier_p (t: name))
3495 {
3496 tree m = lookup_member (scope, name, /*protect=*/0,
3497 /*want_type=*/false, tf_none);
3498 if (!m || shared_member_p (m))
3499 return ba_any;
3500 }
3501 return ba_check;
3502 } ();
3503
3504 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3505 access_path = lookup_base (object_type, scope, ba, NULL, complain);
3506 if (access_path == error_mark_node)
3507 return error_mark_node;
3508 if (!access_path)
3509 {
3510 if (any_dependent_bases_p (object_type))
3511 goto dependent;
3512 if (complain & tf_error)
3513 error ("%qT is not a base of %qT", scope, object_type);
3514 return error_mark_node;
3515 }
3516 }
3517
3518 if (TREE_CODE (name) == BIT_NOT_EXPR)
3519 {
3520 if (dependent_type_p (object_type))
3521 /* The destructor isn't declared yet. */
3522 goto dependent;
3523 member = lookup_destructor (object, scope, dtor_name: name, complain);
3524 }
3525 else
3526 {
3527 /* Look up the member. */
3528 access_failure_info afi;
3529 if (processing_template_decl)
3530 /* Even though this class member access expression is at this
3531 point not dependent, the member itself may be dependent, and
3532 we must not potentially push a access check for a dependent
3533 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3534 ahead of time here; we're going to redo this member lookup at
3535 instantiation time anyway. */
3536 push_deferring_access_checks (dk_no_check);
3537 member = lookup_member (access_path, name, /*protect=*/1,
3538 /*want_type=*/false, complain,
3539 afi: &afi);
3540 if (processing_template_decl)
3541 pop_deferring_access_checks ();
3542 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3543 if (member == NULL_TREE)
3544 {
3545 if (dependent_type_p (object_type))
3546 /* Try again at instantiation time. */
3547 goto dependent;
3548 if (complain & tf_error)
3549 complain_about_unrecognized_member (access_path, name,
3550 object_type);
3551 return error_mark_node;
3552 }
3553 if (member == error_mark_node)
3554 return error_mark_node;
3555 if (DECL_P (member)
3556 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3557 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3558 wrong, so don't use it. */
3559 goto dependent;
3560 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3561 goto dependent;
3562 }
3563
3564 if (is_template_id)
3565 {
3566 tree templ = member;
3567
3568 if (BASELINK_P (templ))
3569 member = lookup_template_function (templ, template_args);
3570 else if (variable_template_p (t: templ))
3571 member = (lookup_and_finish_template_variable
3572 (templ, template_args, complain));
3573 else
3574 {
3575 if (complain & tf_error)
3576 error ("%qD is not a member template function", name);
3577 return error_mark_node;
3578 }
3579 }
3580 }
3581
3582 if (TREE_UNAVAILABLE (member))
3583 error_unavailable_use (member, NULL_TREE);
3584 else if (TREE_DEPRECATED (member))
3585 warn_deprecated_use (member, NULL_TREE);
3586
3587 if (template_p)
3588 check_template_keyword (decl: member);
3589
3590 expr = build_class_member_access_expr (object, member, access_path,
3591 /*preserve_reference=*/false,
3592 complain);
3593 if (processing_template_decl && expr != error_mark_node)
3594 {
3595 if (BASELINK_P (member))
3596 {
3597 if (TREE_CODE (orig_name) == SCOPE_REF)
3598 BASELINK_QUALIFIED_P (member) = 1;
3599 orig_name = member;
3600 }
3601 return build_min_non_dep (COMPONENT_REF, expr,
3602 orig_object, orig_name,
3603 NULL_TREE);
3604 }
3605
3606 return expr;
3607}
3608
3609/* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3610 type. */
3611
3612tree
3613build_simple_component_ref (tree object, tree member)
3614{
3615 tree type = cp_build_qualified_type (TREE_TYPE (member),
3616 cp_type_quals (TREE_TYPE (object)));
3617 return build3_loc (loc: input_location,
3618 code: COMPONENT_REF, type,
3619 arg0: object, arg1: member, NULL_TREE);
3620}
3621
3622/* Return an expression for the MEMBER_NAME field in the internal
3623 representation of PTRMEM, a pointer-to-member function. (Each
3624 pointer-to-member function type gets its own RECORD_TYPE so it is
3625 more convenient to access the fields by name than by FIELD_DECL.)
3626 This routine converts the NAME to a FIELD_DECL and then creates the
3627 node for the complete expression. */
3628
3629tree
3630build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3631{
3632 tree ptrmem_type;
3633 tree member;
3634
3635 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3636 {
3637 for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3638 if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3639 return e.value;
3640 gcc_unreachable ();
3641 }
3642
3643 /* This code is a stripped down version of
3644 build_class_member_access_expr. It does not work to use that
3645 routine directly because it expects the object to be of class
3646 type. */
3647 ptrmem_type = TREE_TYPE (ptrmem);
3648 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3649 for (member = TYPE_FIELDS (ptrmem_type); member;
3650 member = DECL_CHAIN (member))
3651 if (DECL_NAME (member) == member_name)
3652 break;
3653 return build_simple_component_ref (object: ptrmem, member);
3654}
3655
3656/* Return a TREE_LIST of namespace-scope overloads for the given operator,
3657 and for any other relevant operator. */
3658
3659static tree
3660op_unqualified_lookup (tree_code code, bool is_assign)
3661{
3662 tree lookups = NULL_TREE;
3663
3664 if (cxx_dialect >= cxx20 && !is_assign)
3665 {
3666 if (code == NE_EXPR)
3667 {
3668 /* != can get rewritten in terms of ==. */
3669 tree fnname = ovl_op_identifier (isass: false, code: EQ_EXPR);
3670 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3671 lookups = tree_cons (fnname, fns, lookups);
3672 }
3673 else if (code == GT_EXPR || code == LE_EXPR
3674 || code == LT_EXPR || code == GE_EXPR)
3675 {
3676 /* These can get rewritten in terms of <=>. */
3677 tree fnname = ovl_op_identifier (isass: false, code: SPACESHIP_EXPR);
3678 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3679 lookups = tree_cons (fnname, fns, lookups);
3680 }
3681 }
3682
3683 tree fnname = ovl_op_identifier (isass: is_assign, code);
3684 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3685 lookups = tree_cons (fnname, fns, lookups);
3686
3687 if (lookups)
3688 return lookups;
3689 else
3690 return build_tree_list (NULL_TREE, NULL_TREE);
3691}
3692
3693/* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3694 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3695 name lookup for the given operator. */
3696
3697tree
3698build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3699{
3700 if (lookups)
3701 /* We're partially instantiating a dependent operator expression, and
3702 LOOKUPS is the result of phase 1 name lookup that we performed
3703 earlier at template definition time, so just reuse the corresponding
3704 DEPENDENT_OPERATOR_TYPE. */
3705 return TREE_TYPE (lookups);
3706
3707 /* Otherwise we're processing a dependent operator expression at template
3708 definition time, so perform phase 1 name lookup now. */
3709 lookups = op_unqualified_lookup (code, is_assign);
3710
3711 tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3712 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3713 TREE_TYPE (lookups) = type;
3714 return type;
3715}
3716
3717/* Given an expression PTR for a pointer, return an expression
3718 for the value pointed to.
3719 ERRORSTRING is the name of the operator to appear in error messages.
3720
3721 This function may need to overload OPERATOR_FNNAME.
3722 Must also handle REFERENCE_TYPEs for C++. */
3723
3724tree
3725build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3726 tree lookups, tsubst_flags_t complain)
3727{
3728 tree orig_expr = expr;
3729 tree rval;
3730 tree overload = NULL_TREE;
3731
3732 if (processing_template_decl)
3733 {
3734 /* Retain the type if we know the operand is a pointer. */
3735 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3736 {
3737 if (expr == current_class_ptr
3738 || (TREE_CODE (expr) == NOP_EXPR
3739 && TREE_OPERAND (expr, 0) == current_class_ptr
3740 && (same_type_ignoring_top_level_qualifiers_p
3741 (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3742 return current_class_ref;
3743 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3744 }
3745 if (type_dependent_expression_p (expr))
3746 {
3747 expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3748 TREE_TYPE (expr)
3749 = build_dependent_operator_type (lookups, code: INDIRECT_REF, is_assign: false);
3750 return expr;
3751 }
3752 }
3753
3754 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3755 NULL_TREE, NULL_TREE, lookups,
3756 &overload, complain);
3757 if (!rval)
3758 rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3759
3760 if (processing_template_decl && rval != error_mark_node)
3761 {
3762 if (overload != NULL_TREE)
3763 return (build_min_non_dep_op_overload
3764 (INDIRECT_REF, rval, overload, orig_expr));
3765
3766 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3767 }
3768 else
3769 return rval;
3770}
3771
3772/* Like c-family strict_aliasing_warning, but don't warn for dependent
3773 types or expressions. */
3774
3775static bool
3776cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3777{
3778 if (processing_template_decl)
3779 {
3780 tree e = expr;
3781 STRIP_NOPS (e);
3782 if (dependent_type_p (type) || type_dependent_expression_p (e))
3783 return false;
3784 }
3785 return strict_aliasing_warning (loc, type, expr);
3786}
3787
3788/* The implementation of the above, and of indirection implied by other
3789 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3790
3791static tree
3792cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3793 tsubst_flags_t complain, bool do_fold)
3794{
3795 tree pointer, type;
3796
3797 /* RO_NULL should only be used with the folding entry points below, not
3798 cp_build_indirect_ref. */
3799 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3800
3801 if (ptr == current_class_ptr
3802 || (TREE_CODE (ptr) == NOP_EXPR
3803 && TREE_OPERAND (ptr, 0) == current_class_ptr
3804 && (same_type_ignoring_top_level_qualifiers_p
3805 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3806 return current_class_ref;
3807
3808 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3809 ? ptr : decay_conversion (exp: ptr, complain));
3810 if (pointer == error_mark_node)
3811 return error_mark_node;
3812
3813 type = TREE_TYPE (pointer);
3814
3815 if (INDIRECT_TYPE_P (type))
3816 {
3817 /* [expr.unary.op]
3818
3819 If the type of the expression is "pointer to T," the type
3820 of the result is "T." */
3821 tree t = TREE_TYPE (type);
3822
3823 if ((CONVERT_EXPR_P (ptr)
3824 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3825 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3826 {
3827 /* If a warning is issued, mark it to avoid duplicates from
3828 the backend. This only needs to be done at
3829 warn_strict_aliasing > 2. */
3830 if (warn_strict_aliasing > 2
3831 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3832 type, TREE_OPERAND (ptr, 0)))
3833 suppress_warning (ptr, OPT_Wstrict_aliasing);
3834 }
3835
3836 if (VOID_TYPE_P (t))
3837 {
3838 /* A pointer to incomplete type (other than cv void) can be
3839 dereferenced [expr.unary.op]/1 */
3840 if (complain & tf_error)
3841 error_at (loc, "%qT is not a pointer-to-object type", type);
3842 return error_mark_node;
3843 }
3844 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3845 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3846 /* The POINTER was something like `&x'. We simplify `*&x' to
3847 `x'. */
3848 return TREE_OPERAND (pointer, 0);
3849 else
3850 {
3851 tree ref = build1 (INDIRECT_REF, t, pointer);
3852
3853 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3854 so that we get the proper error message if the result is used
3855 to assign to. Also, &* is supposed to be a no-op. */
3856 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3857 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3858 TREE_SIDE_EFFECTS (ref)
3859 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3860 return ref;
3861 }
3862 }
3863 else if (!(complain & tf_error))
3864 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3865 ;
3866 /* `pointer' won't be an error_mark_node if we were given a
3867 pointer to member, so it's cool to check for this here. */
3868 else if (TYPE_PTRMEM_P (type))
3869 switch (errorstring)
3870 {
3871 case RO_ARRAY_INDEXING:
3872 error_at (loc,
3873 "invalid use of array indexing on pointer to member");
3874 break;
3875 case RO_UNARY_STAR:
3876 error_at (loc, "invalid use of unary %<*%> on pointer to member");
3877 break;
3878 case RO_IMPLICIT_CONVERSION:
3879 error_at (loc, "invalid use of implicit conversion on pointer "
3880 "to member");
3881 break;
3882 case RO_ARROW_STAR:
3883 error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3884 "class, but is a pointer to member of type %qT", type);
3885 break;
3886 default:
3887 gcc_unreachable ();
3888 }
3889 else if (pointer != error_mark_node)
3890 invalid_indirection_error (loc, type, errorstring);
3891
3892 return error_mark_node;
3893}
3894
3895/* Entry point used by c-common, which expects folding. */
3896
3897tree
3898build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3899{
3900 return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3901 complain: tf_warning_or_error, do_fold: true);
3902}
3903
3904/* Entry point used by internal indirection needs that don't correspond to any
3905 syntactic construct. */
3906
3907tree
3908cp_build_fold_indirect_ref (tree pointer)
3909{
3910 return cp_build_indirect_ref_1 (loc: input_location, ptr: pointer, errorstring: RO_NULL,
3911 complain: tf_warning_or_error, do_fold: true);
3912}
3913
3914/* Entry point used by indirection needs that correspond to some syntactic
3915 construct. */
3916
3917tree
3918cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3919 tsubst_flags_t complain)
3920{
3921 return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, do_fold: false);
3922}
3923
3924/* This handles expressions of the form "a[i]", which denotes
3925 an array reference.
3926
3927 This is logically equivalent in C to *(a+i), but we may do it differently.
3928 If A is a variable or a member, we generate a primitive ARRAY_REF.
3929 This avoids forcing the array out of registers, and can work on
3930 arrays that are not lvalues (for example, members of structures returned
3931 by functions).
3932
3933 If INDEX is of some user-defined type, it must be converted to
3934 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3935 will inherit the type of the array, which will be some pointer type.
3936
3937 LOC is the location to use in building the array reference. */
3938
3939tree
3940cp_build_array_ref (location_t loc, tree array, tree idx,
3941 tsubst_flags_t complain)
3942{
3943 tree first = NULL_TREE;
3944 tree ret;
3945
3946 if (idx == 0)
3947 {
3948 if (complain & tf_error)
3949 error_at (loc, "subscript missing in array reference");
3950 return error_mark_node;
3951 }
3952
3953 if (TREE_TYPE (array) == error_mark_node
3954 || TREE_TYPE (idx) == error_mark_node)
3955 return error_mark_node;
3956
3957 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3958 inside it. */
3959 switch (TREE_CODE (array))
3960 {
3961 case COMPOUND_EXPR:
3962 {
3963 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3964 complain);
3965 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3966 TREE_OPERAND (array, 0), value);
3967 SET_EXPR_LOCATION (ret, loc);
3968 return ret;
3969 }
3970
3971 case COND_EXPR:
3972 ret = build_conditional_expr
3973 (loc, TREE_OPERAND (array, 0),
3974 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3975 complain),
3976 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3977 complain),
3978 complain);
3979 protected_set_expr_location (ret, loc);
3980 return ret;
3981
3982 default:
3983 break;
3984 }
3985
3986 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3987
3988 /* 0[array] */
3989 if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
3990 {
3991 std::swap (a&: array, b&: idx);
3992 if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
3993 idx = first = save_expr (idx);
3994 }
3995
3996 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3997 {
3998 tree rval, type;
3999
4000 warn_array_subscript_with_type_char (loc, idx);
4001
4002 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4003 {
4004 if (complain & tf_error)
4005 error_at (loc, "array subscript is not an integer");
4006 return error_mark_node;
4007 }
4008
4009 /* Apply integral promotions *after* noticing character types.
4010 (It is unclear why we do these promotions -- the standard
4011 does not say that we should. In fact, the natural thing would
4012 seem to be to convert IDX to ptrdiff_t; we're performing
4013 pointer arithmetic.) */
4014 idx = cp_perform_integral_promotions (expr: idx, complain);
4015
4016 idx = maybe_fold_non_dependent_expr (idx, complain);
4017
4018 /* An array that is indexed by a non-constant
4019 cannot be stored in a register; we must be able to do
4020 address arithmetic on its address.
4021 Likewise an array of elements of variable size. */
4022 if (TREE_CODE (idx) != INTEGER_CST
4023 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4024 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4025 != INTEGER_CST)))
4026 {
4027 if (!cxx_mark_addressable (array, true))
4028 return error_mark_node;
4029 }
4030
4031 /* An array that is indexed by a constant value which is not within
4032 the array bounds cannot be stored in a register either; because we
4033 would get a crash in store_bit_field/extract_bit_field when trying
4034 to access a non-existent part of the register. */
4035 if (TREE_CODE (idx) == INTEGER_CST
4036 && TYPE_DOMAIN (TREE_TYPE (array))
4037 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4038 {
4039 if (!cxx_mark_addressable (array))
4040 return error_mark_node;
4041 }
4042
4043 /* Note in C++ it is valid to subscript a `register' array, since
4044 it is valid to take the address of something with that
4045 storage specification. */
4046 if (extra_warnings)
4047 {
4048 tree foo = array;
4049 while (TREE_CODE (foo) == COMPONENT_REF)
4050 foo = TREE_OPERAND (foo, 0);
4051 if (VAR_P (foo) && DECL_REGISTER (foo)
4052 && (complain & tf_warning))
4053 warning_at (loc, OPT_Wextra,
4054 "subscripting array declared %<register%>");
4055 }
4056
4057 type = TREE_TYPE (TREE_TYPE (array));
4058 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4059 /* Array ref is const/volatile if the array elements are
4060 or if the array is.. */
4061 TREE_READONLY (rval)
4062 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4063 TREE_SIDE_EFFECTS (rval)
4064 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4065 TREE_THIS_VOLATILE (rval)
4066 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4067 ret = require_complete_type (value: rval, complain);
4068 protected_set_expr_location (ret, loc);
4069 if (non_lvalue)
4070 ret = non_lvalue_loc (loc, ret);
4071 if (first)
4072 ret = build2_loc (loc, code: COMPOUND_EXPR, TREE_TYPE (ret), arg0: first, arg1: ret);
4073 return ret;
4074 }
4075
4076 {
4077 tree ar = cp_default_conversion (exp: array, complain);
4078 tree ind = cp_default_conversion (exp: idx, complain);
4079
4080 if (!first && flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
4081 ar = first = save_expr (ar);
4082
4083 /* Put the integer in IND to simplify error checking. */
4084 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4085 std::swap (a&: ar, b&: ind);
4086
4087 if (ar == error_mark_node || ind == error_mark_node)
4088 return error_mark_node;
4089
4090 if (!TYPE_PTR_P (TREE_TYPE (ar)))
4091 {
4092 if (complain & tf_error)
4093 error_at (loc, "subscripted value is neither array nor pointer");
4094 return error_mark_node;
4095 }
4096 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4097 {
4098 if (complain & tf_error)
4099 error_at (loc, "array subscript is not an integer");
4100 return error_mark_node;
4101 }
4102
4103 warn_array_subscript_with_type_char (loc, idx);
4104
4105 ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4106 if (first)
4107 ret = build2_loc (loc, code: COMPOUND_EXPR, TREE_TYPE (ret), arg0: first, arg1: ret);
4108 ret = cp_build_indirect_ref (loc, ptr: ret, errorstring: RO_ARRAY_INDEXING, complain);
4109 protected_set_expr_location (ret, loc);
4110 if (non_lvalue)
4111 ret = non_lvalue_loc (loc, ret);
4112 return ret;
4113 }
4114}
4115
4116/* Entry point for Obj-C++. */
4117
4118tree
4119build_array_ref (location_t loc, tree array, tree idx)
4120{
4121 return cp_build_array_ref (loc, array, idx, complain: tf_warning_or_error);
4122}
4123
4124/* Resolve a pointer to member function. INSTANCE is the object
4125 instance to use, if the member points to a virtual member.
4126
4127 This used to avoid checking for virtual functions if basetype
4128 has no virtual functions, according to an earlier ANSI draft.
4129 With the final ISO C++ rules, such an optimization is
4130 incorrect: A pointer to a derived member can be static_cast
4131 to pointer-to-base-member, as long as the dynamic object
4132 later has the right member. So now we only do this optimization
4133 when we know the dynamic type of the object. */
4134
4135tree
4136get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4137 tsubst_flags_t complain)
4138{
4139 if (TREE_CODE (function) == OFFSET_REF)
4140 function = TREE_OPERAND (function, 1);
4141
4142 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4143 {
4144 tree idx, delta, e1, e2, e3, vtbl;
4145 bool nonvirtual;
4146 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4147 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4148
4149 tree instance_ptr = *instance_ptrptr;
4150 tree instance_save_expr = 0;
4151 if (instance_ptr == error_mark_node)
4152 {
4153 if (TREE_CODE (function) == PTRMEM_CST)
4154 {
4155 /* Extracting the function address from a pmf is only
4156 allowed with -Wno-pmf-conversions. It only works for
4157 pmf constants. */
4158 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4159 e1 = convert (fntype, e1);
4160 return e1;
4161 }
4162 else
4163 {
4164 if (complain & tf_error)
4165 error ("object missing in use of %qE", function);
4166 return error_mark_node;
4167 }
4168 }
4169
4170 /* True if we know that the dynamic type of the object doesn't have
4171 virtual functions, so we can assume the PFN field is a pointer. */
4172 nonvirtual = (COMPLETE_TYPE_P (basetype)
4173 && !TYPE_POLYMORPHIC_P (basetype)
4174 && resolves_to_fixed_type_p (instance_ptr, 0));
4175
4176 /* If we don't really have an object (i.e. in an ill-formed
4177 conversion from PMF to pointer), we can't resolve virtual
4178 functions anyway. */
4179 if (!nonvirtual && is_dummy_object (instance_ptr))
4180 nonvirtual = true;
4181
4182 if (TREE_SIDE_EFFECTS (instance_ptr))
4183 instance_ptr = instance_save_expr = save_expr (instance_ptr);
4184
4185 if (TREE_SIDE_EFFECTS (function))
4186 function = save_expr (function);
4187
4188 /* Start by extracting all the information from the PMF itself. */
4189 e3 = pfn_from_ptrmemfunc (function);
4190 delta = delta_from_ptrmemfunc (function);
4191 idx = build1 (NOP_EXPR, vtable_index_type, e3);
4192 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4193 {
4194 int flag_sanitize_save;
4195 case ptrmemfunc_vbit_in_pfn:
4196 e1 = cp_build_binary_op (input_location,
4197 BIT_AND_EXPR, idx, integer_one_node,
4198 complain);
4199 idx = cp_build_binary_op (input_location,
4200 MINUS_EXPR, idx, integer_one_node,
4201 complain);
4202 if (idx == error_mark_node)
4203 return error_mark_node;
4204 break;
4205
4206 case ptrmemfunc_vbit_in_delta:
4207 e1 = cp_build_binary_op (input_location,
4208 BIT_AND_EXPR, delta, integer_one_node,
4209 complain);
4210 /* Don't instrument the RSHIFT_EXPR we're about to create because
4211 we're going to use DELTA number of times, and that wouldn't play
4212 well with SAVE_EXPRs therein. */
4213 flag_sanitize_save = flag_sanitize;
4214 flag_sanitize = 0;
4215 delta = cp_build_binary_op (input_location,
4216 RSHIFT_EXPR, delta, integer_one_node,
4217 complain);
4218 flag_sanitize = flag_sanitize_save;
4219 if (delta == error_mark_node)
4220 return error_mark_node;
4221 break;
4222
4223 default:
4224 gcc_unreachable ();
4225 }
4226
4227 if (e1 == error_mark_node)
4228 return error_mark_node;
4229
4230 /* Convert down to the right base before using the instance. A
4231 special case is that in a pointer to member of class C, C may
4232 be incomplete. In that case, the function will of course be
4233 a member of C, and no conversion is required. In fact,
4234 lookup_base will fail in that case, because incomplete
4235 classes do not have BINFOs. */
4236 if (!same_type_ignoring_top_level_qualifiers_p
4237 (type1: basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4238 {
4239 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4240 basetype, ba_check, NULL, complain);
4241 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4242 1, complain);
4243 if (instance_ptr == error_mark_node)
4244 return error_mark_node;
4245 }
4246 /* ...and then the delta in the PMF. */
4247 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4248
4249 /* Hand back the adjusted 'this' argument to our caller. */
4250 *instance_ptrptr = instance_ptr;
4251
4252 if (nonvirtual)
4253 /* Now just return the pointer. */
4254 return e3;
4255
4256 /* Next extract the vtable pointer from the object. */
4257 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4258 instance_ptr);
4259 vtbl = cp_build_fold_indirect_ref (pointer: vtbl);
4260 if (vtbl == error_mark_node)
4261 return error_mark_node;
4262
4263 /* Finally, extract the function pointer from the vtable. */
4264 e2 = fold_build_pointer_plus_loc (loc: input_location, ptr: vtbl, off: idx);
4265 e2 = cp_build_fold_indirect_ref (pointer: e2);
4266 if (e2 == error_mark_node)
4267 return error_mark_node;
4268 TREE_CONSTANT (e2) = 1;
4269
4270 /* When using function descriptors, the address of the
4271 vtable entry is treated as a function pointer. */
4272 if (TARGET_VTABLE_USES_DESCRIPTORS)
4273 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4274 cp_build_addr_expr (e2, complain));
4275
4276 e2 = fold_convert (TREE_TYPE (e3), e2);
4277 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4278 if (e1 == error_mark_node)
4279 return error_mark_node;
4280
4281 /* Make sure this doesn't get evaluated first inside one of the
4282 branches of the COND_EXPR. */
4283 if (instance_save_expr)
4284 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4285 instance_save_expr, e1);
4286
4287 function = e1;
4288 }
4289 return function;
4290}
4291
4292/* Used by the C-common bits. */
4293tree
4294build_function_call (location_t /*loc*/,
4295 tree function, tree params)
4296{
4297 return cp_build_function_call (function, params, tf_warning_or_error);
4298}
4299
4300/* Used by the C-common bits. */
4301tree
4302build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4303 tree function, vec<tree, va_gc> *params,
4304 vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4305{
4306 vec<tree, va_gc> *orig_params = params;
4307 tree ret = cp_build_function_call_vec (function, &params,
4308 tf_warning_or_error, orig_function);
4309
4310 /* cp_build_function_call_vec can reallocate PARAMS by adding
4311 default arguments. That should never happen here. Verify
4312 that. */
4313 gcc_assert (params == orig_params);
4314
4315 return ret;
4316}
4317
4318/* Build a function call using a tree list of arguments. */
4319
4320static tree
4321cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4322{
4323 tree ret;
4324
4325 releasing_vec vec;
4326 for (; params != NULL_TREE; params = TREE_CHAIN (params))
4327 vec_safe_push (r&: vec, TREE_VALUE (params));
4328 ret = cp_build_function_call_vec (function, &vec, complain);
4329 return ret;
4330}
4331
4332/* Build a function call using varargs. */
4333
4334tree
4335cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4336{
4337 va_list args;
4338 tree ret, t;
4339
4340 releasing_vec vec;
4341 va_start (args, complain);
4342 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4343 vec_safe_push (r&: vec, t);
4344 va_end (args);
4345 ret = cp_build_function_call_vec (function, &vec, complain);
4346 return ret;
4347}
4348
4349/* Build a function call using a vector of arguments.
4350 If FUNCTION is the result of resolving an overloaded target built-in,
4351 ORIG_FNDECL is the original function decl, otherwise it is null.
4352 PARAMS may be NULL if there are no parameters. This changes the
4353 contents of PARAMS. */
4354
4355tree
4356cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4357 tsubst_flags_t complain, tree orig_fndecl)
4358{
4359 tree fntype, fndecl;
4360 int is_method;
4361 tree original = function;
4362 int nargs;
4363 tree *argarray;
4364 tree parm_types;
4365 vec<tree, va_gc> *allocated = NULL;
4366 tree ret;
4367
4368 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4369 expressions, like those used for ObjC messenger dispatches. */
4370 if (params != NULL && !vec_safe_is_empty (v: *params))
4371 function = objc_rewrite_function_call (function, (**params)[0]);
4372
4373 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4374 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4375 if (TREE_CODE (function) == NOP_EXPR
4376 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4377 function = TREE_OPERAND (function, 0);
4378
4379 if (TREE_CODE (function) == FUNCTION_DECL)
4380 {
4381 if (!mark_used (function, complain))
4382 return error_mark_node;
4383 fndecl = function;
4384
4385 /* Convert anything with function type to a pointer-to-function. */
4386 if (DECL_MAIN_P (function))
4387 {
4388 if (complain & tf_error)
4389 pedwarn (input_location, OPT_Wpedantic,
4390 "ISO C++ forbids calling %<::main%> from within program");
4391 else
4392 return error_mark_node;
4393 }
4394 function = build_addr_func (function, complain);
4395 }
4396 else
4397 {
4398 fndecl = NULL_TREE;
4399
4400 function = build_addr_func (function, complain);
4401 }
4402
4403 if (function == error_mark_node)
4404 return error_mark_node;
4405
4406 fntype = TREE_TYPE (function);
4407
4408 if (TYPE_PTRMEMFUNC_P (fntype))
4409 {
4410 if (complain & tf_error)
4411 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4412 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4413 original, original);
4414 return error_mark_node;
4415 }
4416
4417 is_method = (TYPE_PTR_P (fntype)
4418 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4419
4420 if (!(TYPE_PTRFN_P (fntype)
4421 || is_method
4422 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4423 {
4424 if (complain & tf_error)
4425 {
4426 if (!flag_diagnostics_show_caret)
4427 error_at (input_location,
4428 "%qE cannot be used as a function", original);
4429 else if (DECL_P (original))
4430 error_at (input_location,
4431 "%qD cannot be used as a function", original);
4432 else
4433 error_at (input_location,
4434 "expression cannot be used as a function");
4435 }
4436
4437 return error_mark_node;
4438 }
4439
4440 /* fntype now gets the type of function pointed to. */
4441 fntype = TREE_TYPE (fntype);
4442 parm_types = TYPE_ARG_TYPES (fntype);
4443
4444 if (params == NULL)
4445 {
4446 allocated = make_tree_vector ();
4447 params = &allocated;
4448 }
4449
4450 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4451 complain);
4452 if (nargs < 0)
4453 return error_mark_node;
4454
4455 argarray = (*params)->address ();
4456
4457 /* Check for errors in format strings and inappropriately
4458 null parameters. */
4459 bool warned_p = check_function_arguments (loc: input_location, fndecl, fntype,
4460 nargs, argarray, NULL);
4461
4462 ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4463
4464 if (warned_p)
4465 {
4466 tree c = extract_call_expr (ret);
4467 if (TREE_CODE (c) == CALL_EXPR)
4468 suppress_warning (c, OPT_Wnonnull);
4469 }
4470
4471 if (allocated != NULL)
4472 release_tree_vector (allocated);
4473
4474 return ret;
4475}
4476
4477/* Subroutine of convert_arguments.
4478 Print an error message about a wrong number of arguments. */
4479
4480static void
4481error_args_num (location_t loc, tree fndecl, bool too_many_p)
4482{
4483 if (fndecl)
4484 {
4485 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4486 {
4487 if (DECL_NAME (fndecl) == NULL_TREE
4488 || (DECL_NAME (fndecl)
4489 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4490 error_at (loc,
4491 too_many_p
4492 ? G_("too many arguments to constructor %q#D")
4493 : G_("too few arguments to constructor %q#D"),
4494 fndecl);
4495 else
4496 error_at (loc,
4497 too_many_p
4498 ? G_("too many arguments to member function %q#D")
4499 : G_("too few arguments to member function %q#D"),
4500 fndecl);
4501 }
4502 else
4503 error_at (loc,
4504 too_many_p
4505 ? G_("too many arguments to function %q#D")
4506 : G_("too few arguments to function %q#D"),
4507 fndecl);
4508 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4509 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4510 }
4511 else
4512 {
4513 if (c_dialect_objc () && objc_message_selector ())
4514 error_at (loc,
4515 too_many_p
4516 ? G_("too many arguments to method %q#D")
4517 : G_("too few arguments to method %q#D"),
4518 objc_message_selector ());
4519 else
4520 error_at (loc, too_many_p ? G_("too many arguments to function")
4521 : G_("too few arguments to function"));
4522 }
4523}
4524
4525/* Convert the actual parameter expressions in the list VALUES to the
4526 types in the list TYPELIST. The converted expressions are stored
4527 back in the VALUES vector.
4528 If parmdecls is exhausted, or when an element has NULL as its type,
4529 perform the default conversions.
4530
4531 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4532
4533 This is also where warnings about wrong number of args are generated.
4534
4535 Returns the actual number of arguments processed (which might be less
4536 than the length of the vector), or -1 on error.
4537
4538 In C++, unspecified trailing parameters can be filled in with their
4539 default arguments, if such were specified. Do so here. */
4540
4541static int
4542convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4543 int flags, tsubst_flags_t complain)
4544{
4545 tree typetail;
4546 unsigned int i;
4547
4548 /* Argument passing is always copy-initialization. */
4549 flags |= LOOKUP_ONLYCONVERTING;
4550
4551 for (i = 0, typetail = typelist;
4552 i < vec_safe_length (v: *values);
4553 i++)
4554 {
4555 tree type = typetail ? TREE_VALUE (typetail) : 0;
4556 tree val = (**values)[i];
4557
4558 if (val == error_mark_node || type == error_mark_node)
4559 return -1;
4560
4561 if (type == void_type_node)
4562 {
4563 if (complain & tf_error)
4564 {
4565 error_args_num (loc: input_location, fndecl, /*too_many_p=*/true);
4566 return i;
4567 }
4568 else
4569 return -1;
4570 }
4571
4572 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4573 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4574 if (TREE_CODE (val) == NOP_EXPR
4575 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4576 && (type == 0 || !TYPE_REF_P (type)))
4577 val = TREE_OPERAND (val, 0);
4578
4579 if (type == 0 || !TYPE_REF_P (type))
4580 {
4581 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4582 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4583 val = decay_conversion (exp: val, complain);
4584 }
4585
4586 if (val == error_mark_node)
4587 return -1;
4588
4589 if (type != 0)
4590 {
4591 /* Formal parm type is specified by a function prototype. */
4592 tree parmval;
4593
4594 if (!COMPLETE_TYPE_P (complete_type (type)))
4595 {
4596 if (complain & tf_error)
4597 {
4598 location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4599 if (fndecl)
4600 {
4601 auto_diagnostic_group d;
4602 error_at (loc,
4603 "parameter %P of %qD has incomplete type %qT",
4604 i, fndecl, type);
4605 inform (get_fndecl_argument_location (fndecl, i),
4606 " declared here");
4607 }
4608 else
4609 error_at (loc, "parameter %P has incomplete type %qT", i,
4610 type);
4611 }
4612 parmval = error_mark_node;
4613 }
4614 else
4615 {
4616 parmval = convert_for_initialization
4617 (NULL_TREE, type, val, flags,
4618 ICR_ARGPASS, fndecl, i, complain);
4619 parmval = convert_for_arg_passing (type, parmval, complain);
4620 }
4621
4622 if (parmval == error_mark_node)
4623 return -1;
4624
4625 (**values)[i] = parmval;
4626 }
4627 else
4628 {
4629 int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4630 if (magic)
4631 {
4632 /* Don't truncate excess precision to the semantic type. */
4633 if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4634 val = TREE_OPERAND (val, 0);
4635 /* Don't do ellipsis conversion for __built_in_constant_p
4636 as this will result in spurious errors for non-trivial
4637 types. */
4638 val = require_complete_type (value: val, complain);
4639 }
4640 else
4641 val = convert_arg_to_ellipsis (val, complain);
4642
4643 (**values)[i] = val;
4644 }
4645
4646 if (typetail)
4647 typetail = TREE_CHAIN (typetail);
4648 }
4649
4650 if (typetail != 0 && typetail != void_list_node)
4651 {
4652 /* See if there are default arguments that can be used. Because
4653 we hold default arguments in the FUNCTION_TYPE (which is so
4654 wrong), we can see default parameters here from deduced
4655 contexts (and via typeof) for indirect function calls.
4656 Fortunately we know whether we have a function decl to
4657 provide default arguments in a language conformant
4658 manner. */
4659 if (fndecl && TREE_PURPOSE (typetail)
4660 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4661 {
4662 for (; typetail != void_list_node; ++i)
4663 {
4664 /* After DR777, with explicit template args we can end up with a
4665 default argument followed by no default argument. */
4666 if (!TREE_PURPOSE (typetail))
4667 break;
4668 tree parmval
4669 = convert_default_arg (TREE_VALUE (typetail),
4670 TREE_PURPOSE (typetail),
4671 fndecl, i, complain);
4672
4673 if (parmval == error_mark_node)
4674 return -1;
4675
4676 vec_safe_push (v&: *values, obj: parmval);
4677 typetail = TREE_CHAIN (typetail);
4678 /* ends with `...'. */
4679 if (typetail == NULL_TREE)
4680 break;
4681 }
4682 }
4683
4684 if (typetail && typetail != void_list_node)
4685 {
4686 if (complain & tf_error)
4687 error_args_num (loc: input_location, fndecl, /*too_many_p=*/false);
4688 return -1;
4689 }
4690 }
4691
4692 return (int) i;
4693}
4694
4695/* Build a binary-operation expression, after performing default
4696 conversions on the operands. CODE is the kind of expression to
4697 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4698 are the tree codes which correspond to ARG1 and ARG2 when issuing
4699 warnings about possibly misplaced parentheses. They may differ
4700 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4701 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4702 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4703 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4704 ARG2_CODE as ERROR_MARK. */
4705
4706tree
4707build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4708 enum tree_code arg1_code, tree arg2,
4709 enum tree_code arg2_code, tree lookups,
4710 tree *overload_p, tsubst_flags_t complain)
4711{
4712 tree orig_arg1;
4713 tree orig_arg2;
4714 tree expr;
4715 tree overload = NULL_TREE;
4716
4717 orig_arg1 = arg1;
4718 orig_arg2 = arg2;
4719
4720 if (processing_template_decl)
4721 {
4722 if (type_dependent_expression_p (arg1)
4723 || type_dependent_expression_p (arg2))
4724 {
4725 expr = build_min_nt_loc (loc, code, arg1, arg2);
4726 TREE_TYPE (expr)
4727 = build_dependent_operator_type (lookups, code, is_assign: false);
4728 return expr;
4729 }
4730 }
4731
4732 if (code == DOTSTAR_EXPR)
4733 expr = build_m_component_ref (arg1, arg2, complain);
4734 else
4735 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4736 lookups, &overload, complain);
4737
4738 if (overload_p != NULL)
4739 *overload_p = overload;
4740
4741 /* Check for cases such as x+y<<z which users are likely to
4742 misinterpret. But don't warn about obj << x + y, since that is a
4743 common idiom for I/O. */
4744 if (warn_parentheses
4745 && (complain & tf_warning)
4746 && !processing_template_decl
4747 && !error_operand_p (t: arg1)
4748 && !error_operand_p (t: arg2)
4749 && (code != LSHIFT_EXPR
4750 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4751 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4752 arg2_code, orig_arg2);
4753
4754 if (processing_template_decl && expr != error_mark_node)
4755 {
4756 if (overload != NULL_TREE)
4757 return (build_min_non_dep_op_overload
4758 (code, expr, overload, orig_arg1, orig_arg2));
4759
4760 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4761 }
4762
4763 return expr;
4764}
4765
4766/* Build and return an ARRAY_REF expression. */
4767
4768tree
4769build_x_array_ref (location_t loc, tree arg1, tree arg2,
4770 tsubst_flags_t complain)
4771{
4772 tree orig_arg1 = arg1;
4773 tree orig_arg2 = arg2;
4774 tree expr;
4775 tree overload = NULL_TREE;
4776
4777 if (processing_template_decl)
4778 {
4779 if (type_dependent_expression_p (arg1)
4780 || type_dependent_expression_p (arg2))
4781 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4782 NULL_TREE, NULL_TREE);
4783 }
4784
4785 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4786 NULL_TREE, NULL_TREE, &overload, complain);
4787
4788 if (processing_template_decl && expr != error_mark_node)
4789 {
4790 if (overload != NULL_TREE)
4791 return (build_min_non_dep_op_overload
4792 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4793
4794 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4795 NULL_TREE, NULL_TREE);
4796 }
4797 return expr;
4798}
4799
4800/* Build an OpenMP array section reference, creating an exact type for the
4801 resulting expression based on the element type and bounds if possible. If
4802 we have variable bounds, create an incomplete array type for the result
4803 instead. */
4804
4805tree
4806build_omp_array_section (location_t loc, tree array_expr, tree index,
4807 tree length)
4808{
4809 tree type = TREE_TYPE (array_expr);
4810 gcc_assert (type);
4811 type = non_reference (type);
4812
4813 tree sectype, eltype = TREE_TYPE (type);
4814
4815 /* It's not an array or pointer type. Just reuse the type of the
4816 original expression as the type of the array section (an error will be
4817 raised anyway, later). */
4818 if (eltype == NULL_TREE)
4819 sectype = TREE_TYPE (array_expr);
4820 else
4821 {
4822 tree idxtype = NULL_TREE;
4823
4824 /* If we know the integer bounds, create an index type with exact
4825 low/high (or zero/length) bounds. Otherwise, create an incomplete
4826 array type. (This mostly only affects diagnostics.) */
4827 if (index != NULL_TREE
4828 && length != NULL_TREE
4829 && TREE_CODE (index) == INTEGER_CST
4830 && TREE_CODE (length) == INTEGER_CST)
4831 {
4832 tree low = fold_convert (sizetype, index);
4833 tree high = fold_convert (sizetype, length);
4834 high = size_binop (PLUS_EXPR, low, high);
4835 high = size_binop (MINUS_EXPR, high, size_one_node);
4836 idxtype = build_range_type (sizetype, low, high);
4837 }
4838 else if ((index == NULL_TREE || integer_zerop (index))
4839 && length != NULL_TREE
4840 && TREE_CODE (length) == INTEGER_CST)
4841 idxtype = build_index_type (length);
4842
4843 sectype = build_array_type (eltype, idxtype);
4844 }
4845
4846 return build3_loc (loc, code: OMP_ARRAY_SECTION, type: sectype, arg0: array_expr, arg1: index,
4847 arg2: length);
4848}
4849
4850/* Return whether OP is an expression of enum type cast to integer
4851 type. In C++ even unsigned enum types are cast to signed integer
4852 types. We do not want to issue warnings about comparisons between
4853 signed and unsigned types when one of the types is an enum type.
4854 Those warnings are always false positives in practice. */
4855
4856static bool
4857enum_cast_to_int (tree op)
4858{
4859 if (CONVERT_EXPR_P (op)
4860 && TREE_TYPE (op) == integer_type_node
4861 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4862 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4863 return true;
4864
4865 /* The cast may have been pushed into a COND_EXPR. */
4866 if (TREE_CODE (op) == COND_EXPR)
4867 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4868 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4869
4870 return false;
4871}
4872
4873/* For the c-common bits. */
4874tree
4875build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4876 bool /*convert_p*/)
4877{
4878 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4879}
4880
4881/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4882 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4883
4884static tree
4885build_vec_cmp (tree_code code, tree type,
4886 tree arg0, tree arg1)
4887{
4888 tree zero_vec = build_zero_cst (type);
4889 tree minus_one_vec = build_minus_one_cst (type);
4890 tree cmp_type = truth_type_for (TREE_TYPE (arg0));
4891 tree cmp = build2 (code, cmp_type, arg0, arg1);
4892 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4893}
4894
4895/* Possibly warn about an address never being NULL. */
4896
4897static void
4898warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4899{
4900 /* Prevent warnings issued for macro expansion. */
4901 if (!warn_address
4902 || (complain & tf_warning) == 0
4903 || c_inhibit_evaluation_warnings != 0
4904 || from_macro_expansion_at (loc: location)
4905 || warning_suppressed_p (op, OPT_Waddress))
4906 return;
4907
4908 tree cop = fold_for_warn (op);
4909
4910 if (TREE_CODE (cop) == NON_LVALUE_EXPR)
4911 /* Unwrap the expression for C++ 98. */
4912 cop = TREE_OPERAND (cop, 0);
4913
4914 if (TREE_CODE (cop) == PTRMEM_CST)
4915 {
4916 /* The address of a nonstatic data member is never null. */
4917 warning_at (location, OPT_Waddress,
4918 "the address %qE will never be NULL",
4919 cop);
4920 return;
4921 }
4922
4923 if (TREE_CODE (cop) == NOP_EXPR)
4924 {
4925 /* Allow casts to intptr_t to suppress the warning. */
4926 tree type = TREE_TYPE (cop);
4927 if (TREE_CODE (type) == INTEGER_TYPE)
4928 return;
4929
4930 STRIP_NOPS (cop);
4931 }
4932
4933 bool warned = false;
4934 if (TREE_CODE (cop) == ADDR_EXPR)
4935 {
4936 cop = TREE_OPERAND (cop, 0);
4937
4938 /* Set to true in the loop below if OP dereferences its operand.
4939 In such a case the ultimate target need not be a decl for
4940 the null [in]equality test to be necessarily constant. */
4941 bool deref = false;
4942
4943 /* Get the outermost array or object, or member. */
4944 while (handled_component_p (t: cop))
4945 {
4946 if (TREE_CODE (cop) == COMPONENT_REF)
4947 {
4948 /* Get the member (its address is never null). */
4949 cop = TREE_OPERAND (cop, 1);
4950 break;
4951 }
4952
4953 /* Get the outer array/object to refer to in the warning. */
4954 cop = TREE_OPERAND (cop, 0);
4955 deref = true;
4956 }
4957
4958 if ((!deref && !decl_with_nonnull_addr_p (cop))
4959 || from_macro_expansion_at (loc: location)
4960 || warning_suppressed_p (cop, OPT_Waddress))
4961 return;
4962
4963 warned = warning_at (location, OPT_Waddress,
4964 "the address of %qD will never be NULL", cop);
4965 op = cop;
4966 }
4967 else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
4968 {
4969 /* Adding zero to the null pointer is well-defined in C++. When
4970 the offset is unknown (i.e., not a constant) warn anyway since
4971 it's less likely that the pointer operand is null than not. */
4972 tree off = TREE_OPERAND (cop, 1);
4973 if (!integer_zerop (off)
4974 && !warning_suppressed_p (cop, OPT_Waddress))
4975 {
4976 tree base = TREE_OPERAND (cop, 0);
4977 STRIP_NOPS (base);
4978 if (TYPE_REF_P (TREE_TYPE (base)))
4979 warning_at (location, OPT_Waddress, "the compiler can assume that "
4980 "the address of %qE will never be NULL", base);
4981 else
4982 warning_at (location, OPT_Waddress, "comparing the result of "
4983 "pointer addition %qE and NULL", cop);
4984 }
4985 return;
4986 }
4987 else if (CONVERT_EXPR_P (op)
4988 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4989 {
4990 STRIP_NOPS (op);
4991
4992 if (TREE_CODE (op) == COMPONENT_REF)
4993 op = TREE_OPERAND (op, 1);
4994
4995 if (DECL_P (op))
4996 warned = warning_at (location, OPT_Waddress,
4997 "the compiler can assume that the address of "
4998 "%qD will never be NULL", op);
4999 }
5000
5001 if (warned && DECL_P (op))
5002 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5003}
5004
5005/* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5006 the other operand is of a different enumeration type or a floating-point
5007 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5008 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5009 and LOC is the location for the whole binary expression.
5010 For C++26 this is ill-formed rather than deprecated.
5011 Return true for SFINAE errors.
5012 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5013
5014static bool
5015do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5016 tree type1, tsubst_flags_t complain)
5017{
5018 if (TREE_CODE (type0) == ENUMERAL_TYPE
5019 && TREE_CODE (type1) == ENUMERAL_TYPE
5020 && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5021 {
5022 if (cxx_dialect >= cxx26)
5023 {
5024 if ((complain & tf_warning_or_error) == 0)
5025 return true;
5026 }
5027 else if ((complain & tf_warning) == 0)
5028 return false;
5029 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5030 Otherwise, warn if -Wenum-conversion is on. */
5031 enum opt_code opt;
5032 if (warn_deprecated_enum_enum_conv)
5033 opt = OPT_Wdeprecated_enum_enum_conversion;
5034 else if (warn_enum_conversion)
5035 opt = OPT_Wenum_conversion;
5036 else
5037 return false;
5038
5039 switch (code)
5040 {
5041 case GT_EXPR:
5042 case LT_EXPR:
5043 case GE_EXPR:
5044 case LE_EXPR:
5045 case EQ_EXPR:
5046 case NE_EXPR:
5047 /* Comparisons are handled by -Wenum-compare. */
5048 return false;
5049 case SPACESHIP_EXPR:
5050 /* This is invalid, don't warn. */
5051 return false;
5052 case BIT_AND_EXPR:
5053 case BIT_IOR_EXPR:
5054 case BIT_XOR_EXPR:
5055 if (cxx_dialect >= cxx26)
5056 pedwarn (loc, opt, "bitwise operation between different "
5057 "enumeration types %qT and %qT", type0, type1);
5058 else
5059 warning_at (loc, opt, "bitwise operation between different "
5060 "enumeration types %qT and %qT is deprecated",
5061 type0, type1);
5062 return false;
5063 default:
5064 if (cxx_dialect >= cxx26)
5065 pedwarn (loc, opt, "arithmetic between different enumeration "
5066 "types %qT and %qT", type0, type1);
5067 else
5068 warning_at (loc, opt, "arithmetic between different enumeration "
5069 "types %qT and %qT is deprecated", type0, type1);
5070 return false;
5071 }
5072 }
5073 else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5074 && SCALAR_FLOAT_TYPE_P (type1))
5075 || (SCALAR_FLOAT_TYPE_P (type0)
5076 && TREE_CODE (type1) == ENUMERAL_TYPE))
5077 {
5078 if (cxx_dialect >= cxx26)
5079 {
5080 if ((complain & tf_warning_or_error) == 0)
5081 return true;
5082 }
5083 else if ((complain & tf_warning) == 0)
5084 return false;
5085 const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5086 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5087 Otherwise, warn if -Wenum-conversion is on. */
5088 enum opt_code opt;
5089 if (warn_deprecated_enum_float_conv)
5090 opt = OPT_Wdeprecated_enum_float_conversion;
5091 else if (warn_enum_conversion)
5092 opt = OPT_Wenum_conversion;
5093 else
5094 return false;
5095
5096 switch (code)
5097 {
5098 case GT_EXPR:
5099 case LT_EXPR:
5100 case GE_EXPR:
5101 case LE_EXPR:
5102 case EQ_EXPR:
5103 case NE_EXPR:
5104 if (enum_first_p && cxx_dialect >= cxx26)
5105 pedwarn (loc, opt, "comparison of enumeration type %qT with "
5106 "floating-point type %qT", type0, type1);
5107 else if (cxx_dialect >= cxx26)
5108 pedwarn (loc, opt, "comparison of floating-point type %qT "
5109 "with enumeration type %qT", type0, type1);
5110 else if (enum_first_p)
5111 warning_at (loc, opt, "comparison of enumeration type %qT with "
5112 "floating-point type %qT is deprecated",
5113 type0, type1);
5114 else
5115 warning_at (loc, opt, "comparison of floating-point type %qT "
5116 "with enumeration type %qT is deprecated",
5117 type0, type1);
5118 return false;
5119 case SPACESHIP_EXPR:
5120 /* This is invalid, don't warn. */
5121 return false;
5122 default:
5123 if (enum_first_p && cxx_dialect >= cxx26)
5124 pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5125 "and floating-point type %qT", type0, type1);
5126 else if (cxx_dialect >= cxx26)
5127 pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5128 "and enumeration type %qT", type0, type1);
5129 else if (enum_first_p)
5130 warning_at (loc, opt, "arithmetic between enumeration type %qT "
5131 "and floating-point type %qT is deprecated",
5132 type0, type1);
5133 else
5134 warning_at (loc, opt, "arithmetic between floating-point type %qT "
5135 "and enumeration type %qT is deprecated",
5136 type0, type1);
5137 return false;
5138 }
5139 }
5140 return false;
5141}
5142
5143/* Build a binary-operation expression without default conversions.
5144 CODE is the kind of expression to build.
5145 LOCATION is the location_t of the operator in the source code.
5146 This function differs from `build' in several ways:
5147 the data type of the result is computed and recorded in it,
5148 warnings are generated if arg data types are invalid,
5149 special handling for addition and subtraction of pointers is known,
5150 and some optimization is done (operations on narrow ints
5151 are done in the narrower type when that gives the same result).
5152 Constant folding is also done before the result is returned.
5153
5154 Note that the operands will never have enumeral types
5155 because either they have just had the default conversions performed
5156 or they have both just been converted to some other type in which
5157 the arithmetic is to be done.
5158
5159 C++: must do special pointer arithmetic when implementing
5160 multiple inheritance, and deal with pointer to member functions. */
5161
5162tree
5163cp_build_binary_op (const op_location_t &location,
5164 enum tree_code code, tree orig_op0, tree orig_op1,
5165 tsubst_flags_t complain)
5166{
5167 tree op0, op1;
5168 enum tree_code code0, code1;
5169 tree type0, type1, orig_type0, orig_type1;
5170 const char *invalid_op_diag;
5171
5172 /* Expression code to give to the expression when it is built.
5173 Normally this is CODE, which is what the caller asked for,
5174 but in some special cases we change it. */
5175 enum tree_code resultcode = code;
5176
5177 /* Data type in which the computation is to be performed.
5178 In the simplest cases this is the common type of the arguments. */
5179 tree result_type = NULL_TREE;
5180
5181 /* When the computation is in excess precision, the type of the
5182 final EXCESS_PRECISION_EXPR. */
5183 tree semantic_result_type = NULL;
5184
5185 /* Nonzero means operands have already been type-converted
5186 in whatever way is necessary.
5187 Zero means they need to be converted to RESULT_TYPE. */
5188 int converted = 0;
5189
5190 /* Nonzero means create the expression with this type, rather than
5191 RESULT_TYPE. */
5192 tree build_type = 0;
5193
5194 /* Nonzero means after finally constructing the expression
5195 convert it to this type. */
5196 tree final_type = 0;
5197
5198 tree result;
5199
5200 /* Nonzero if this is an operation like MIN or MAX which can
5201 safely be computed in short if both args are promoted shorts.
5202 Also implies COMMON.
5203 -1 indicates a bitwise operation; this makes a difference
5204 in the exact conditions for when it is safe to do the operation
5205 in a narrower mode. */
5206 int shorten = 0;
5207
5208 /* Nonzero if this is a comparison operation;
5209 if both args are promoted shorts, compare the original shorts.
5210 Also implies COMMON. */
5211 int short_compare = 0;
5212
5213 /* Nonzero if this is a right-shift operation, which can be computed on the
5214 original short and then promoted if the operand is a promoted short. */
5215 int short_shift = 0;
5216
5217 /* Nonzero means set RESULT_TYPE to the common type of the args. */
5218 int common = 0;
5219
5220 /* True if both operands have arithmetic type. */
5221 bool arithmetic_types_p;
5222
5223 /* Remember whether we're doing / or %. */
5224 bool doing_div_or_mod = false;
5225
5226 /* Remember whether we're doing << or >>. */
5227 bool doing_shift = false;
5228
5229 /* Tree holding instrumentation expression. */
5230 tree instrument_expr = NULL_TREE;
5231
5232 /* True means this is an arithmetic operation that may need excess
5233 precision. */
5234 bool may_need_excess_precision;
5235
5236 /* Apply default conversions. */
5237 op0 = resolve_nondeduced_context (orig_op0, complain);
5238 op1 = resolve_nondeduced_context (orig_op1, complain);
5239
5240 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5241 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5242 || code == TRUTH_XOR_EXPR)
5243 {
5244 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5245 op0 = decay_conversion (exp: op0, complain);
5246 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5247 op1 = decay_conversion (exp: op1, complain);
5248 }
5249 else
5250 {
5251 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5252 op0 = cp_default_conversion (exp: op0, complain);
5253 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5254 op1 = cp_default_conversion (exp: op1, complain);
5255 }
5256
5257 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5258 STRIP_TYPE_NOPS (op0);
5259 STRIP_TYPE_NOPS (op1);
5260
5261 /* DTRT if one side is an overloaded function, but complain about it. */
5262 if (type_unknown_p (expr: op0))
5263 {
5264 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5265 if (t != error_mark_node)
5266 {
5267 if (complain & tf_error)
5268 permerror (location,
5269 "assuming cast to type %qT from overloaded function",
5270 TREE_TYPE (t));
5271 op0 = t;
5272 }
5273 }
5274 if (type_unknown_p (expr: op1))
5275 {
5276 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5277 if (t != error_mark_node)
5278 {
5279 if (complain & tf_error)
5280 permerror (location,
5281 "assuming cast to type %qT from overloaded function",
5282 TREE_TYPE (t));
5283 op1 = t;
5284 }
5285 }
5286
5287 orig_type0 = type0 = TREE_TYPE (op0);
5288 orig_type1 = type1 = TREE_TYPE (op1);
5289 tree non_ep_op0 = op0;
5290 tree non_ep_op1 = op1;
5291
5292 /* The expression codes of the data types of the arguments tell us
5293 whether the arguments are integers, floating, pointers, etc. */
5294 code0 = TREE_CODE (type0);
5295 code1 = TREE_CODE (type1);
5296
5297 /* If an error was already reported for one of the arguments,
5298 avoid reporting another error. */
5299 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5300 return error_mark_node;
5301
5302 if ((invalid_op_diag
5303 = targetm.invalid_binary_op (code, type0, type1)))
5304 {
5305 if (complain & tf_error)
5306 {
5307 if (code0 == REAL_TYPE
5308 && code1 == REAL_TYPE
5309 && (extended_float_type_p (type: type0)
5310 || extended_float_type_p (type: type1))
5311 && cp_compare_floating_point_conversion_ranks (t1: type0,
5312 t2: type1) == 3)
5313 {
5314 rich_location richloc (line_table, location);
5315 binary_op_error (&richloc, code, type0, type1);
5316 }
5317 else
5318 error (invalid_op_diag);
5319 }
5320 return error_mark_node;
5321 }
5322
5323 switch (code)
5324 {
5325 case PLUS_EXPR:
5326 case MINUS_EXPR:
5327 case MULT_EXPR:
5328 case TRUNC_DIV_EXPR:
5329 case CEIL_DIV_EXPR:
5330 case FLOOR_DIV_EXPR:
5331 case ROUND_DIV_EXPR:
5332 case EXACT_DIV_EXPR:
5333 may_need_excess_precision = true;
5334 break;
5335 case EQ_EXPR:
5336 case NE_EXPR:
5337 case LE_EXPR:
5338 case GE_EXPR:
5339 case LT_EXPR:
5340 case GT_EXPR:
5341 case SPACESHIP_EXPR:
5342 /* Excess precision for implicit conversions of integers to
5343 floating point. */
5344 may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5345 || ANY_INTEGRAL_TYPE_P (type1));
5346 break;
5347 default:
5348 may_need_excess_precision = false;
5349 break;
5350 }
5351 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5352 {
5353 op0 = TREE_OPERAND (op0, 0);
5354 type0 = TREE_TYPE (op0);
5355 }
5356 else if (may_need_excess_precision
5357 && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5358 if (tree eptype = excess_precision_type (type0))
5359 {
5360 type0 = eptype;
5361 op0 = convert (eptype, op0);
5362 }
5363 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5364 {
5365 op1 = TREE_OPERAND (op1, 0);
5366 type1 = TREE_TYPE (op1);
5367 }
5368 else if (may_need_excess_precision
5369 && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5370 if (tree eptype = excess_precision_type (type1))
5371 {
5372 type1 = eptype;
5373 op1 = convert (eptype, op1);
5374 }
5375
5376 /* Issue warnings about peculiar, but valid, uses of NULL. */
5377 if ((null_node_p (expr: orig_op0) || null_node_p (expr: orig_op1))
5378 /* It's reasonable to use pointer values as operands of &&
5379 and ||, so NULL is no exception. */
5380 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5381 && ( /* Both are NULL (or 0) and the operation was not a
5382 comparison or a pointer subtraction. */
5383 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5384 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5385 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5386 || (!null_ptr_cst_p (orig_op0)
5387 && !TYPE_PTR_OR_PTRMEM_P (type0))
5388 || (!null_ptr_cst_p (orig_op1)
5389 && !TYPE_PTR_OR_PTRMEM_P (type1)))
5390 && (complain & tf_warning))
5391 {
5392 location_t loc =
5393 expansion_point_location_if_in_system_header (input_location);
5394
5395 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5396 }
5397
5398 /* In case when one of the operands of the binary operation is
5399 a vector and another is a scalar -- convert scalar to vector. */
5400 if ((gnu_vector_type_p (type: type0) && code1 != VECTOR_TYPE)
5401 || (gnu_vector_type_p (type: type1) && code0 != VECTOR_TYPE))
5402 {
5403 enum stv_conv convert_flag
5404 = scalar_to_vector (loc: location, code, op0: non_ep_op0, op1: non_ep_op1,
5405 complain & tf_error);
5406
5407 switch (convert_flag)
5408 {
5409 case stv_error:
5410 return error_mark_node;
5411 case stv_firstarg:
5412 {
5413 op0 = convert (TREE_TYPE (type1), op0);
5414 op0 = save_expr (op0);
5415 op0 = build_vector_from_val (type1, op0);
5416 orig_type0 = type0 = TREE_TYPE (op0);
5417 code0 = TREE_CODE (type0);
5418 converted = 1;
5419 break;
5420 }
5421 case stv_secondarg:
5422 {
5423 op1 = convert (TREE_TYPE (type0), op1);
5424 op1 = save_expr (op1);
5425 op1 = build_vector_from_val (type0, op1);
5426 orig_type1 = type1 = TREE_TYPE (op1);
5427 code1 = TREE_CODE (type1);
5428 converted = 1;
5429 break;
5430 }
5431 default:
5432 break;
5433 }
5434 }
5435
5436 switch (code)
5437 {
5438 case MINUS_EXPR:
5439 /* Subtraction of two similar pointers.
5440 We must subtract them as integers, then divide by object size. */
5441 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5442 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5443 TREE_TYPE (type1)))
5444 {
5445 result = pointer_diff (location, op0, op1,
5446 common_pointer_type (t1: type0, t2: type1), complain,
5447 &instrument_expr);
5448 if (instrument_expr != NULL)
5449 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5450 instrument_expr, result);
5451
5452 return result;
5453 }
5454 /* In all other cases except pointer - int, the usual arithmetic
5455 rules apply. */
5456 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5457 {
5458 common = 1;
5459 break;
5460 }
5461 /* The pointer - int case is just like pointer + int; fall
5462 through. */
5463 gcc_fallthrough ();
5464 case PLUS_EXPR:
5465 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5466 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5467 {
5468 tree ptr_operand;
5469 tree int_operand;
5470 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5471 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5472 if (processing_template_decl)
5473 {
5474 result_type = TREE_TYPE (ptr_operand);
5475 break;
5476 }
5477 return cp_pointer_int_sum (location, code,
5478 ptr_operand,
5479 int_operand,
5480 complain);
5481 }
5482 common = 1;
5483 break;
5484
5485 case MULT_EXPR:
5486 common = 1;
5487 break;
5488
5489 case TRUNC_DIV_EXPR:
5490 case CEIL_DIV_EXPR:
5491 case FLOOR_DIV_EXPR:
5492 case ROUND_DIV_EXPR:
5493 case EXACT_DIV_EXPR:
5494 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5495 {
5496 tree type0 = TREE_OPERAND (op0, 0);
5497 tree type1 = TREE_OPERAND (op1, 0);
5498 tree first_arg = tree_strip_any_location_wrapper (exp: type0);
5499 if (!TYPE_P (type0))
5500 type0 = TREE_TYPE (type0);
5501 if (!TYPE_P (type1))
5502 type1 = TREE_TYPE (type1);
5503 if (type0
5504 && INDIRECT_TYPE_P (type0)
5505 && same_type_p (TREE_TYPE (type0), type1))
5506 {
5507 if (!(TREE_CODE (first_arg) == PARM_DECL
5508 && DECL_ARRAY_PARAMETER_P (first_arg)
5509 && warn_sizeof_array_argument)
5510 && (complain & tf_warning))
5511 {
5512 auto_diagnostic_group d;
5513 if (warning_at (location, OPT_Wsizeof_pointer_div,
5514 "division %<sizeof (%T) / sizeof (%T)%> does "
5515 "not compute the number of array elements",
5516 type0, type1))
5517 if (DECL_P (first_arg))
5518 inform (DECL_SOURCE_LOCATION (first_arg),
5519 "first %<sizeof%> operand was declared here");
5520 }
5521 }
5522 else if (!dependent_type_p (type0)
5523 && !dependent_type_p (type1)
5524 && TREE_CODE (type0) == ARRAY_TYPE
5525 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5526 /* Set by finish_parenthesized_expr. */
5527 && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5528 && (complain & tf_warning))
5529 maybe_warn_sizeof_array_div (location, first_arg, type0,
5530 op1, non_reference (type1));
5531 }
5532
5533 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5534 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5535 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5536 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5537 {
5538 enum tree_code tcode0 = code0, tcode1 = code1;
5539 doing_div_or_mod = true;
5540 warn_for_div_by_zero (location, divisor: fold_for_warn (op1));
5541
5542 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5543 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5544 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5545 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5546
5547 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5548 resultcode = RDIV_EXPR;
5549 else
5550 {
5551 /* When dividing two signed integers, we have to promote to int.
5552 unless we divide by a constant != -1. Note that default
5553 conversion will have been performed on the operands at this
5554 point, so we have to dig out the original type to find out if
5555 it was unsigned. */
5556 tree stripped_op1 = tree_strip_any_location_wrapper (exp: op1);
5557 shorten = may_shorten_divmod (op0, op1: stripped_op1);
5558 }
5559
5560 common = 1;
5561 }
5562 break;
5563
5564 case BIT_AND_EXPR:
5565 case BIT_IOR_EXPR:
5566 case BIT_XOR_EXPR:
5567 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5568 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5569 && !VECTOR_FLOAT_TYPE_P (type0)
5570 && !VECTOR_FLOAT_TYPE_P (type1)))
5571 shorten = -1;
5572 break;
5573
5574 case TRUNC_MOD_EXPR:
5575 case FLOOR_MOD_EXPR:
5576 doing_div_or_mod = true;
5577 warn_for_div_by_zero (location, divisor: fold_for_warn (op1));
5578
5579 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5580 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5581 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5582 common = 1;
5583 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5584 {
5585 /* Although it would be tempting to shorten always here, that loses
5586 on some targets, since the modulo instruction is undefined if the
5587 quotient can't be represented in the computation mode. We shorten
5588 only if unsigned or if dividing by something we know != -1. */
5589 tree stripped_op1 = tree_strip_any_location_wrapper (exp: op1);
5590 shorten = may_shorten_divmod (op0, op1: stripped_op1);
5591 common = 1;
5592 }
5593 break;
5594
5595 case TRUTH_ANDIF_EXPR:
5596 case TRUTH_ORIF_EXPR:
5597 case TRUTH_AND_EXPR:
5598 case TRUTH_OR_EXPR:
5599 if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type: type1))
5600 {
5601 if (!COMPARISON_CLASS_P (op1))
5602 op1 = cp_build_binary_op (EXPR_LOCATION (op1), code: NE_EXPR, orig_op0: op1,
5603 orig_op1: build_zero_cst (type1), complain);
5604 if (code == TRUTH_ANDIF_EXPR)
5605 {
5606 tree z = build_zero_cst (TREE_TYPE (op1));
5607 return build_conditional_expr (location, op0, op1, z, complain);
5608 }
5609 else if (code == TRUTH_ORIF_EXPR)
5610 {
5611 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5612 return build_conditional_expr (location, op0, m1, op1, complain);
5613 }
5614 else
5615 gcc_unreachable ();
5616 }
5617 if (gnu_vector_type_p (type: type0)
5618 && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type: type1)))
5619 {
5620 if (!COMPARISON_CLASS_P (op0))
5621 op0 = cp_build_binary_op (EXPR_LOCATION (op0), code: NE_EXPR, orig_op0: op0,
5622 orig_op1: build_zero_cst (type0), complain);
5623 if (!VECTOR_TYPE_P (type1))
5624 {
5625 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5626 tree z = build_zero_cst (TREE_TYPE (op0));
5627 op1 = build_conditional_expr (location, op1, m1, z, complain);
5628 }
5629 else if (!COMPARISON_CLASS_P (op1))
5630 op1 = cp_build_binary_op (EXPR_LOCATION (op1), code: NE_EXPR, orig_op0: op1,
5631 orig_op1: build_zero_cst (type1), complain);
5632
5633 if (code == TRUTH_ANDIF_EXPR)
5634 code = BIT_AND_EXPR;
5635 else if (code == TRUTH_ORIF_EXPR)
5636 code = BIT_IOR_EXPR;
5637 else
5638 gcc_unreachable ();
5639
5640 return cp_build_binary_op (location, code, orig_op0: op0, orig_op1: op1, complain);
5641 }
5642
5643 result_type = boolean_type_node;
5644 break;
5645
5646 /* Shift operations: result has same type as first operand;
5647 always convert second operand to int.
5648 Also set SHORT_SHIFT if shifting rightward. */
5649
5650 case RSHIFT_EXPR:
5651 if (gnu_vector_type_p (type: type0)
5652 && code1 == INTEGER_TYPE
5653 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5654 {
5655 result_type = type0;
5656 converted = 1;
5657 }
5658 else if (gnu_vector_type_p (type: type0)
5659 && gnu_vector_type_p (type: type1)
5660 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5661 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5662 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5663 TYPE_VECTOR_SUBPARTS (type1)))
5664 {
5665 result_type = type0;
5666 converted = 1;
5667 }
5668 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5669 {
5670 tree const_op1 = fold_for_warn (op1);
5671 if (TREE_CODE (const_op1) != INTEGER_CST)
5672 const_op1 = op1;
5673 result_type = type0;
5674 doing_shift = true;
5675 if (TREE_CODE (const_op1) == INTEGER_CST)
5676 {
5677 if (tree_int_cst_lt (t1: const_op1, integer_zero_node))
5678 {
5679 if ((complain & tf_warning)
5680 && c_inhibit_evaluation_warnings == 0)
5681 warning_at (location, OPT_Wshift_count_negative,
5682 "right shift count is negative");
5683 }
5684 else
5685 {
5686 if (!integer_zerop (const_op1))
5687 short_shift = 1;
5688
5689 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5690 && (complain & tf_warning)
5691 && c_inhibit_evaluation_warnings == 0)
5692 warning_at (location, OPT_Wshift_count_overflow,
5693 "right shift count >= width of type");
5694 }
5695 }
5696 /* Avoid converting op1 to result_type later. */
5697 converted = 1;
5698 }
5699 break;
5700
5701 case LSHIFT_EXPR:
5702 if (gnu_vector_type_p (type: type0)
5703 && code1 == INTEGER_TYPE
5704 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5705 {
5706 result_type = type0;
5707 converted = 1;
5708 }
5709 else if (gnu_vector_type_p (type: type0)
5710 && gnu_vector_type_p (type: type1)
5711 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5712 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5713 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5714 TYPE_VECTOR_SUBPARTS (type1)))
5715 {
5716 result_type = type0;
5717 converted = 1;
5718 }
5719 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5720 {
5721 tree const_op0 = fold_for_warn (op0);
5722 if (TREE_CODE (const_op0) != INTEGER_CST)
5723 const_op0 = op0;
5724 tree const_op1 = fold_for_warn (op1);
5725 if (TREE_CODE (const_op1) != INTEGER_CST)
5726 const_op1 = op1;
5727 result_type = type0;
5728 doing_shift = true;
5729 if (TREE_CODE (const_op0) == INTEGER_CST
5730 && tree_int_cst_sgn (const_op0) < 0
5731 && !TYPE_OVERFLOW_WRAPS (type0)
5732 && (complain & tf_warning)
5733 && c_inhibit_evaluation_warnings == 0)
5734 warning_at (location, OPT_Wshift_negative_value,
5735 "left shift of negative value");
5736 if (TREE_CODE (const_op1) == INTEGER_CST)
5737 {
5738 if (tree_int_cst_lt (t1: const_op1, integer_zero_node))
5739 {
5740 if ((complain & tf_warning)
5741 && c_inhibit_evaluation_warnings == 0)
5742 warning_at (location, OPT_Wshift_count_negative,
5743 "left shift count is negative");
5744 }
5745 else if (compare_tree_int (const_op1,
5746 TYPE_PRECISION (type0)) >= 0)
5747 {
5748 if ((complain & tf_warning)
5749 && c_inhibit_evaluation_warnings == 0)
5750 warning_at (location, OPT_Wshift_count_overflow,
5751 "left shift count >= width of type");
5752 }
5753 else if (TREE_CODE (const_op0) == INTEGER_CST
5754 && (complain & tf_warning))
5755 maybe_warn_shift_overflow (location, const_op0, const_op1);
5756 }
5757 /* Avoid converting op1 to result_type later. */
5758 converted = 1;
5759 }
5760 break;
5761
5762 case EQ_EXPR:
5763 case NE_EXPR:
5764 if (gnu_vector_type_p (type: type0) && gnu_vector_type_p (type: type1))
5765 goto vector_compare;
5766 if ((complain & tf_warning)
5767 && c_inhibit_evaluation_warnings == 0
5768 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5769 warning_at (location, OPT_Wfloat_equal,
5770 "comparing floating-point with %<==%> "
5771 "or %<!=%> is unsafe");
5772 if (complain & tf_warning)
5773 {
5774 tree stripped_orig_op0 = tree_strip_any_location_wrapper (exp: orig_op0);
5775 tree stripped_orig_op1 = tree_strip_any_location_wrapper (exp: orig_op1);
5776 if ((TREE_CODE (stripped_orig_op0) == STRING_CST
5777 && !integer_zerop (cp_fully_fold (op1)))
5778 || (TREE_CODE (stripped_orig_op1) == STRING_CST
5779 && !integer_zerop (cp_fully_fold (op0))))
5780 warning_at (location, OPT_Waddress,
5781 "comparison with string literal results in "
5782 "unspecified behavior");
5783 else if (warn_array_compare
5784 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5785 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5786 do_warn_array_compare (location, code, stripped_orig_op0,
5787 stripped_orig_op1);
5788 }
5789
5790 build_type = boolean_type_node;
5791 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5792 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5793 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5794 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5795 short_compare = 1;
5796 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5797 && null_ptr_cst_p (orig_op1))
5798 /* Handle, eg, (void*)0 (c++/43906), and more. */
5799 || (code0 == POINTER_TYPE
5800 && TYPE_PTR_P (type1) && integer_zerop (op1)))
5801 {
5802 if (TYPE_PTR_P (type1))
5803 result_type = composite_pointer_type (location,
5804 t1: type0, t2: type1, arg1: op0, arg2: op1,
5805 operation: CPO_COMPARISON, complain);
5806 else
5807 result_type = type0;
5808
5809 if (char_type_p (TREE_TYPE (orig_op1)))
5810 {
5811 auto_diagnostic_group d;
5812 if (warning_at (location, OPT_Wpointer_compare,
5813 "comparison between pointer and zero character "
5814 "constant"))
5815 inform (location,
5816 "did you mean to dereference the pointer?");
5817 }
5818 warn_for_null_address (location, op: op0, complain);
5819 }
5820 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5821 && null_ptr_cst_p (orig_op0))
5822 /* Handle, eg, (void*)0 (c++/43906), and more. */
5823 || (code1 == POINTER_TYPE
5824 && TYPE_PTR_P (type0) && integer_zerop (op0)))
5825 {
5826 if (TYPE_PTR_P (type0))
5827 result_type = composite_pointer_type (location,
5828 t1: type0, t2: type1, arg1: op0, arg2: op1,
5829 operation: CPO_COMPARISON, complain);
5830 else
5831 result_type = type1;
5832
5833 if (char_type_p (TREE_TYPE (orig_op0)))
5834 {
5835 auto_diagnostic_group d;
5836 if (warning_at (location, OPT_Wpointer_compare,
5837 "comparison between pointer and zero character "
5838 "constant"))
5839 inform (location,
5840 "did you mean to dereference the pointer?");
5841 }
5842 warn_for_null_address (location, op: op1, complain);
5843 }
5844 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5845 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5846 result_type = composite_pointer_type (location,
5847 t1: type0, t2: type1, arg1: op0, arg2: op1,
5848 operation: CPO_COMPARISON, complain);
5849 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5850 /* One of the operands must be of nullptr_t type. */
5851 result_type = TREE_TYPE (nullptr_node);
5852 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5853 {
5854 result_type = type0;
5855 if (complain & tf_error)
5856 permerror (location, "ISO C++ forbids comparison between "
5857 "pointer and integer");
5858 else
5859 return error_mark_node;
5860 }
5861 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5862 {
5863 result_type = type1;
5864 if (complain & tf_error)
5865 permerror (location, "ISO C++ forbids comparison between "
5866 "pointer and integer");
5867 else
5868 return error_mark_node;
5869 }
5870 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5871 {
5872 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5873 == ptrmemfunc_vbit_in_delta)
5874 {
5875 tree pfn0, delta0, e1, e2;
5876
5877 if (TREE_SIDE_EFFECTS (op0))
5878 op0 = cp_save_expr (op0);
5879
5880 pfn0 = pfn_from_ptrmemfunc (op0);
5881 delta0 = delta_from_ptrmemfunc (op0);
5882 {
5883 /* If we will warn below about a null-address compare
5884 involving the orig_op0 ptrmemfunc, we'd likely also
5885 warn about the pfn0's null-address compare, and
5886 that would be redundant, so suppress it. */
5887 warning_sentinel ws (warn_address);
5888 e1 = cp_build_binary_op (location,
5889 code: EQ_EXPR,
5890 orig_op0: pfn0,
5891 orig_op1: build_zero_cst (TREE_TYPE (pfn0)),
5892 complain);
5893 }
5894 e2 = cp_build_binary_op (location,
5895 code: BIT_AND_EXPR,
5896 orig_op0: delta0,
5897 integer_one_node,
5898 complain);
5899
5900 if (complain & tf_warning)
5901 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5902
5903 e2 = cp_build_binary_op (location,
5904 code: EQ_EXPR, orig_op0: e2, integer_zero_node,
5905 complain);
5906 op0 = cp_build_binary_op (location,
5907 code: TRUTH_ANDIF_EXPR, orig_op0: e1, orig_op1: e2,
5908 complain);
5909 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5910 }
5911 else
5912 {
5913 op0 = build_ptrmemfunc_access_expr (ptrmem: op0, pfn_identifier);
5914 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5915 }
5916 result_type = TREE_TYPE (op0);
5917
5918 warn_for_null_address (location, op: orig_op0, complain);
5919 }
5920 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5921 return cp_build_binary_op (location, code, orig_op0: op1, orig_op1: op0, complain);
5922 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5923 {
5924 tree type;
5925 /* E will be the final comparison. */
5926 tree e;
5927 /* E1 and E2 are for scratch. */
5928 tree e1;
5929 tree e2;
5930 tree pfn0;
5931 tree pfn1;
5932 tree delta0;
5933 tree delta1;
5934
5935 type = composite_pointer_type (location, t1: type0, t2: type1, arg1: op0, arg2: op1,
5936 operation: CPO_COMPARISON, complain);
5937
5938 if (!same_type_p (TREE_TYPE (op0), type))
5939 op0 = cp_convert_and_check (type, op0, complain);
5940 if (!same_type_p (TREE_TYPE (op1), type))
5941 op1 = cp_convert_and_check (type, op1, complain);
5942
5943 if (op0 == error_mark_node || op1 == error_mark_node)
5944 return error_mark_node;
5945
5946 if (TREE_SIDE_EFFECTS (op0))
5947 op0 = save_expr (op0);
5948 if (TREE_SIDE_EFFECTS (op1))
5949 op1 = save_expr (op1);
5950
5951 pfn0 = pfn_from_ptrmemfunc (op0);
5952 pfn0 = cp_fully_fold (pfn0);
5953 /* Avoid -Waddress warnings (c++/64877). */
5954 if (TREE_CODE (pfn0) == ADDR_EXPR)
5955 suppress_warning (pfn0, OPT_Waddress);
5956 pfn1 = pfn_from_ptrmemfunc (op1);
5957 pfn1 = cp_fully_fold (pfn1);
5958 delta0 = delta_from_ptrmemfunc (op0);
5959 delta1 = delta_from_ptrmemfunc (op1);
5960 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5961 == ptrmemfunc_vbit_in_delta)
5962 {
5963 /* We generate:
5964
5965 (op0.pfn == op1.pfn
5966 && ((op0.delta == op1.delta)
5967 || (!op0.pfn && op0.delta & 1 == 0
5968 && op1.delta & 1 == 0))
5969
5970 The reason for the `!op0.pfn' bit is that a NULL
5971 pointer-to-member is any member with a zero PFN and
5972 LSB of the DELTA field is 0. */
5973
5974 e1 = cp_build_binary_op (location, code: BIT_AND_EXPR,
5975 orig_op0: delta0,
5976 integer_one_node,
5977 complain);
5978 e1 = cp_build_binary_op (location,
5979 code: EQ_EXPR, orig_op0: e1, integer_zero_node,
5980 complain);
5981 e2 = cp_build_binary_op (location, code: BIT_AND_EXPR,
5982 orig_op0: delta1,
5983 integer_one_node,
5984 complain);
5985 e2 = cp_build_binary_op (location,
5986 code: EQ_EXPR, orig_op0: e2, integer_zero_node,
5987 complain);
5988 e1 = cp_build_binary_op (location,
5989 code: TRUTH_ANDIF_EXPR, orig_op0: e2, orig_op1: e1,
5990 complain);
5991 e2 = cp_build_binary_op (location, code: EQ_EXPR,
5992 orig_op0: pfn0,
5993 orig_op1: build_zero_cst (TREE_TYPE (pfn0)),
5994 complain);
5995 e2 = cp_build_binary_op (location,
5996 code: TRUTH_ANDIF_EXPR, orig_op0: e2, orig_op1: e1, complain);
5997 e1 = cp_build_binary_op (location,
5998 code: EQ_EXPR, orig_op0: delta0, orig_op1: delta1, complain);
5999 e1 = cp_build_binary_op (location,
6000 code: TRUTH_ORIF_EXPR, orig_op0: e1, orig_op1: e2, complain);
6001 }
6002 else
6003 {
6004 /* We generate:
6005
6006 (op0.pfn == op1.pfn
6007 && (!op0.pfn || op0.delta == op1.delta))
6008
6009 The reason for the `!op0.pfn' bit is that a NULL
6010 pointer-to-member is any member with a zero PFN; the
6011 DELTA field is unspecified. */
6012
6013 e1 = cp_build_binary_op (location,
6014 code: EQ_EXPR, orig_op0: delta0, orig_op1: delta1, complain);
6015 e2 = cp_build_binary_op (location,
6016 code: EQ_EXPR,
6017 orig_op0: pfn0,
6018 orig_op1: build_zero_cst (TREE_TYPE (pfn0)),
6019 complain);
6020 e1 = cp_build_binary_op (location,
6021 code: TRUTH_ORIF_EXPR, orig_op0: e1, orig_op1: e2, complain);
6022 }
6023 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6024 e = cp_build_binary_op (location,
6025 code: TRUTH_ANDIF_EXPR, orig_op0: e2, orig_op1: e1, complain);
6026 if (code == EQ_EXPR)
6027 return e;
6028 return cp_build_binary_op (location,
6029 code: EQ_EXPR, orig_op0: e, integer_zero_node, complain);
6030 }
6031 else
6032 {
6033 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6034 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6035 type1));
6036 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6037 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6038 type0));
6039 }
6040
6041 break;
6042
6043 case MAX_EXPR:
6044 case MIN_EXPR:
6045 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6046 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6047 shorten = 1;
6048 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6049 result_type = composite_pointer_type (location,
6050 t1: type0, t2: type1, arg1: op0, arg2: op1,
6051 operation: CPO_COMPARISON, complain);
6052 break;
6053
6054 case LE_EXPR:
6055 case GE_EXPR:
6056 case LT_EXPR:
6057 case GT_EXPR:
6058 case SPACESHIP_EXPR:
6059 if (TREE_CODE (orig_op0) == STRING_CST
6060 || TREE_CODE (orig_op1) == STRING_CST)
6061 {
6062 if (complain & tf_warning)
6063 warning_at (location, OPT_Waddress,
6064 "comparison with string literal results "
6065 "in unspecified behavior");
6066 }
6067 else if (warn_array_compare
6068 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6069 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6070 && code != SPACESHIP_EXPR
6071 && (complain & tf_warning))
6072 do_warn_array_compare (location, code,
6073 tree_strip_any_location_wrapper (exp: orig_op0),
6074 tree_strip_any_location_wrapper (exp: orig_op1));
6075
6076 if (gnu_vector_type_p (type: type0) && gnu_vector_type_p (type: type1))
6077 {
6078 vector_compare:
6079 tree intt;
6080 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6081 TREE_TYPE (type1))
6082 && !vector_types_compatible_elements_p (type0, type1))
6083 {
6084 if (complain & tf_error)
6085 {
6086 error_at (location, "comparing vectors with different "
6087 "element types");
6088 inform (location, "operand types are %qT and %qT",
6089 type0, type1);
6090 }
6091 return error_mark_node;
6092 }
6093
6094 if (maybe_ne (a: TYPE_VECTOR_SUBPARTS (node: type0),
6095 b: TYPE_VECTOR_SUBPARTS (node: type1)))
6096 {
6097 if (complain & tf_error)
6098 {
6099 error_at (location, "comparing vectors with different "
6100 "number of elements");
6101 inform (location, "operand types are %qT and %qT",
6102 type0, type1);
6103 }
6104 return error_mark_node;
6105 }
6106
6107 /* It's not precisely specified how the usual arithmetic
6108 conversions apply to the vector types. Here, we use
6109 the unsigned type if one of the operands is signed and
6110 the other one is unsigned. */
6111 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6112 {
6113 if (!TYPE_UNSIGNED (type0))
6114 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6115 else
6116 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6117 warning_at (location, OPT_Wsign_compare, "comparison between "
6118 "types %qT and %qT", type0, type1);
6119 }
6120
6121 if (resultcode == SPACESHIP_EXPR)
6122 {
6123 if (complain & tf_error)
6124 sorry_at (location, "three-way comparison of vectors");
6125 return error_mark_node;
6126 }
6127
6128 /* Always construct signed integer vector type. */
6129 intt = c_common_type_for_size
6130 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
6131 if (!intt)
6132 {
6133 if (complain & tf_error)
6134 error_at (location, "could not find an integer type "
6135 "of the same size as %qT", TREE_TYPE (type0));
6136 return error_mark_node;
6137 }
6138 result_type = build_opaque_vector_type (intt,
6139 TYPE_VECTOR_SUBPARTS (node: type0));
6140 return build_vec_cmp (code: resultcode, type: result_type, arg0: op0, arg1: op1);
6141 }
6142 build_type = boolean_type_node;
6143 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6144 || code0 == ENUMERAL_TYPE)
6145 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6146 || code1 == ENUMERAL_TYPE))
6147 short_compare = 1;
6148 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6149 result_type = composite_pointer_type (location,
6150 t1: type0, t2: type1, arg1: op0, arg2: op1,
6151 operation: CPO_COMPARISON, complain);
6152 else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6153 || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6154 || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6155 {
6156 /* Core Issue 1512 made this ill-formed. */
6157 if (complain & tf_error)
6158 error_at (location, "ordered comparison of pointer with "
6159 "integer zero (%qT and %qT)", type0, type1);
6160 return error_mark_node;
6161 }
6162 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6163 {
6164 result_type = type0;
6165 if (complain & tf_error)
6166 permerror (location, "ISO C++ forbids comparison between "
6167 "pointer and integer");
6168 else
6169 return error_mark_node;
6170 }
6171 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6172 {
6173 result_type = type1;
6174 if (complain & tf_error)
6175 permerror (location, "ISO C++ forbids comparison between "
6176 "pointer and integer");
6177 else
6178 return error_mark_node;
6179 }
6180
6181 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6182 && !processing_template_decl
6183 && sanitize_flags_p (flag: SANITIZE_POINTER_COMPARE))
6184 {
6185 op0 = save_expr (op0);
6186 op1 = save_expr (op1);
6187
6188 tree tt = builtin_decl_explicit (fncode: BUILT_IN_ASAN_POINTER_COMPARE);
6189 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6190 }
6191
6192 break;
6193
6194 case UNORDERED_EXPR:
6195 case ORDERED_EXPR:
6196 case UNLT_EXPR:
6197 case UNLE_EXPR:
6198 case UNGT_EXPR:
6199 case UNGE_EXPR:
6200 case UNEQ_EXPR:
6201 build_type = integer_type_node;
6202 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6203 {
6204 if (complain & tf_error)
6205 error ("unordered comparison on non-floating-point argument");
6206 return error_mark_node;
6207 }
6208 common = 1;
6209 break;
6210
6211 default:
6212 break;
6213 }
6214
6215 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6216 || code0 == ENUMERAL_TYPE)
6217 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6218 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6219 arithmetic_types_p = 1;
6220 else
6221 {
6222 arithmetic_types_p = 0;
6223 /* Vector arithmetic is only allowed when both sides are vectors. */
6224 if (gnu_vector_type_p (type: type0) && gnu_vector_type_p (type: type1))
6225 {
6226 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6227 || !vector_types_compatible_elements_p (type0, type1))
6228 {
6229 if (complain & tf_error)
6230 {
6231 /* "location" already embeds the locations of the
6232 operands, so we don't need to add them separately
6233 to richloc. */
6234 rich_location richloc (line_table, location);
6235 binary_op_error (&richloc, code, type0, type1);
6236 }
6237 return error_mark_node;
6238 }
6239 arithmetic_types_p = 1;
6240 }
6241 }
6242 /* Determine the RESULT_TYPE, if it is not already known. */
6243 if (!result_type
6244 && arithmetic_types_p
6245 && (shorten || common || short_compare))
6246 {
6247 result_type = cp_common_type (t1: type0, t2: type1);
6248 if (result_type == error_mark_node)
6249 {
6250 tree t1 = type0;
6251 tree t2 = type1;
6252 if (TREE_CODE (t1) == COMPLEX_TYPE)
6253 t1 = TREE_TYPE (t1);
6254 if (TREE_CODE (t2) == COMPLEX_TYPE)
6255 t2 = TREE_TYPE (t2);
6256 gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6257 && TREE_CODE (t2) == REAL_TYPE
6258 && (extended_float_type_p (t1)
6259 || extended_float_type_p (t2))
6260 && cp_compare_floating_point_conversion_ranks
6261 (t1, t2) == 3);
6262 if (complain & tf_error)
6263 {
6264 rich_location richloc (line_table, location);
6265 binary_op_error (&richloc, code, type0, type1);
6266 }
6267 return error_mark_node;
6268 }
6269 if (complain & tf_warning)
6270 do_warn_double_promotion (result_type, type0, type1,
6271 "implicit conversion from %qH to %qI "
6272 "to match other operand of binary "
6273 "expression", location);
6274 if (do_warn_enum_conversions (loc: location, code, TREE_TYPE (orig_op0),
6275 TREE_TYPE (orig_op1), complain))
6276 return error_mark_node;
6277 }
6278 if (may_need_excess_precision
6279 && (orig_type0 != type0 || orig_type1 != type1)
6280 && build_type == NULL_TREE
6281 && result_type)
6282 {
6283 gcc_assert (common);
6284 semantic_result_type = cp_common_type (t1: orig_type0, t2: orig_type1);
6285 if (semantic_result_type == error_mark_node)
6286 {
6287 tree t1 = orig_type0;
6288 tree t2 = orig_type1;
6289 if (TREE_CODE (t1) == COMPLEX_TYPE)
6290 t1 = TREE_TYPE (t1);
6291 if (TREE_CODE (t2) == COMPLEX_TYPE)
6292 t2 = TREE_TYPE (t2);
6293 gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6294 && TREE_CODE (t2) == REAL_TYPE
6295 && (extended_float_type_p (t1)
6296 || extended_float_type_p (t2))
6297 && cp_compare_floating_point_conversion_ranks
6298 (t1, t2) == 3);
6299 if (complain & tf_error)
6300 {
6301 rich_location richloc (line_table, location);
6302 binary_op_error (&richloc, code, type0, type1);
6303 }
6304 return error_mark_node;
6305 }
6306 }
6307
6308 if (code == SPACESHIP_EXPR)
6309 {
6310 iloc_sentinel s (location);
6311
6312 tree orig_type0 = TREE_TYPE (orig_op0);
6313 tree_code orig_code0 = TREE_CODE (orig_type0);
6314 tree orig_type1 = TREE_TYPE (orig_op1);
6315 tree_code orig_code1 = TREE_CODE (orig_type1);
6316 if (!result_type || result_type == error_mark_node)
6317 /* Nope. */
6318 result_type = NULL_TREE;
6319 else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6320 /* "If one of the operands is of type bool and the other is not, the
6321 program is ill-formed." */
6322 result_type = NULL_TREE;
6323 else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6324 && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6325 /* We only do array/function-to-pointer conversion if "at least one of
6326 the operands is of pointer type". */
6327 result_type = NULL_TREE;
6328 else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6329 /* <=> no longer supports equality relations. */
6330 result_type = NULL_TREE;
6331 else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6332 && !(same_type_ignoring_top_level_qualifiers_p
6333 (type1: orig_type0, type2: orig_type1)))
6334 /* "If both operands have arithmetic types, or one operand has integral
6335 type and the other operand has unscoped enumeration type, the usual
6336 arithmetic conversions are applied to the operands." So we don't do
6337 arithmetic conversions if the operands both have enumeral type. */
6338 result_type = NULL_TREE;
6339 else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6340 || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6341 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6342 [where one is of enumeration type and the other is of a different
6343 enumeration type or a floating-point type] are ill-formed. */
6344 result_type = NULL_TREE;
6345
6346 if (result_type)
6347 {
6348 build_type = spaceship_type (result_type, complain);
6349 if (build_type == error_mark_node)
6350 return error_mark_node;
6351 }
6352
6353 if (result_type && arithmetic_types_p)
6354 {
6355 /* If a narrowing conversion is required, other than from an integral
6356 type to a floating point type, the program is ill-formed. */
6357 bool ok = true;
6358 if (TREE_CODE (result_type) == REAL_TYPE
6359 && CP_INTEGRAL_TYPE_P (orig_type0))
6360 /* OK */;
6361 else if (!check_narrowing (result_type, orig_op0, complain))
6362 ok = false;
6363 if (TREE_CODE (result_type) == REAL_TYPE
6364 && CP_INTEGRAL_TYPE_P (orig_type1))
6365 /* OK */;
6366 else if (!check_narrowing (result_type, orig_op1, complain))
6367 ok = false;
6368 if (!ok && !(complain & tf_error))
6369 return error_mark_node;
6370 }
6371 }
6372
6373 if (!result_type)
6374 {
6375 if (complain & tf_error)
6376 {
6377 binary_op_rich_location richloc (location,
6378 orig_op0, orig_op1, true);
6379 error_at (&richloc,
6380 "invalid operands of types %qT and %qT to binary %qO",
6381 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6382 }
6383 return error_mark_node;
6384 }
6385
6386 /* If we're in a template, the only thing we need to know is the
6387 RESULT_TYPE. */
6388 if (processing_template_decl)
6389 {
6390 /* Since the middle-end checks the type when doing a build2, we
6391 need to build the tree in pieces. This built tree will never
6392 get out of the front-end as we replace it when instantiating
6393 the template. */
6394 tree tmp = build2 (resultcode,
6395 build_type ? build_type : result_type,
6396 NULL_TREE, op1);
6397 TREE_OPERAND (tmp, 0) = op0;
6398 if (semantic_result_type)
6399 tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6400 return tmp;
6401 }
6402
6403 /* Remember the original type; RESULT_TYPE might be changed later on
6404 by shorten_binary_op. */
6405 tree orig_type = result_type;
6406
6407 if (arithmetic_types_p)
6408 {
6409 bool first_complex = (code0 == COMPLEX_TYPE);
6410 bool second_complex = (code1 == COMPLEX_TYPE);
6411 int none_complex = (!first_complex && !second_complex);
6412
6413 /* Adapted from patch for c/24581. */
6414 if (first_complex != second_complex
6415 && (code == PLUS_EXPR
6416 || code == MINUS_EXPR
6417 || code == MULT_EXPR
6418 || (code == TRUNC_DIV_EXPR && first_complex))
6419 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6420 && flag_signed_zeros)
6421 {
6422 /* An operation on mixed real/complex operands must be
6423 handled specially, but the language-independent code can
6424 more easily optimize the plain complex arithmetic if
6425 -fno-signed-zeros. */
6426 tree real_type = TREE_TYPE (result_type);
6427 tree real, imag;
6428 if (first_complex)
6429 {
6430 if (TREE_TYPE (op0) != result_type)
6431 op0 = cp_convert_and_check (result_type, op0, complain);
6432 if (TREE_TYPE (op1) != real_type)
6433 op1 = cp_convert_and_check (real_type, op1, complain);
6434 }
6435 else
6436 {
6437 if (TREE_TYPE (op0) != real_type)
6438 op0 = cp_convert_and_check (real_type, op0, complain);
6439 if (TREE_TYPE (op1) != result_type)
6440 op1 = cp_convert_and_check (result_type, op1, complain);
6441 }
6442 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6443 return error_mark_node;
6444 if (first_complex)
6445 {
6446 op0 = save_expr (op0);
6447 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6448 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6449 switch (code)
6450 {
6451 case MULT_EXPR:
6452 case TRUNC_DIV_EXPR:
6453 op1 = save_expr (op1);
6454 imag = build2 (resultcode, real_type, imag, op1);
6455 /* Fall through. */
6456 case PLUS_EXPR:
6457 case MINUS_EXPR:
6458 real = build2 (resultcode, real_type, real, op1);
6459 break;
6460 default:
6461 gcc_unreachable();
6462 }
6463 }
6464 else
6465 {
6466 op1 = save_expr (op1);
6467 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6468 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6469 switch (code)
6470 {
6471 case MULT_EXPR:
6472 op0 = save_expr (op0);
6473 imag = build2 (resultcode, real_type, op0, imag);
6474 /* Fall through. */
6475 case PLUS_EXPR:
6476 real = build2 (resultcode, real_type, op0, real);
6477 break;
6478 case MINUS_EXPR:
6479 real = build2 (resultcode, real_type, op0, real);
6480 imag = build1 (NEGATE_EXPR, real_type, imag);
6481 break;
6482 default:
6483 gcc_unreachable();
6484 }
6485 }
6486 result = build2 (COMPLEX_EXPR, result_type, real, imag);
6487 if (semantic_result_type)
6488 result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6489 result);
6490 return result;
6491 }
6492
6493 /* For certain operations (which identify themselves by shorten != 0)
6494 if both args were extended from the same smaller type,
6495 do the arithmetic in that type and then extend.
6496
6497 shorten !=0 and !=1 indicates a bitwise operation.
6498 For them, this optimization is safe only if
6499 both args are zero-extended or both are sign-extended.
6500 Otherwise, we might change the result.
6501 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6502 but calculated in (unsigned short) it would be (unsigned short)-1. */
6503
6504 if (shorten && none_complex)
6505 {
6506 final_type = result_type;
6507 result_type = shorten_binary_op (result_type, op0, op1,
6508 bitwise: shorten == -1);
6509 }
6510
6511 /* Shifts can be shortened if shifting right. */
6512
6513 if (short_shift)
6514 {
6515 int unsigned_arg;
6516 tree arg0 = get_narrower (op0, &unsigned_arg);
6517 /* We're not really warning here but when we set short_shift we
6518 used fold_for_warn to fold the operand. */
6519 tree const_op1 = fold_for_warn (op1);
6520
6521 final_type = result_type;
6522
6523 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6524 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6525
6526 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6527 && tree_int_cst_sgn (const_op1) > 0
6528 /* We can shorten only if the shift count is less than the
6529 number of bits in the smaller type size. */
6530 && compare_tree_int (const_op1,
6531 TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6532 /* We cannot drop an unsigned shift after sign-extension. */
6533 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6534 {
6535 /* Do an unsigned shift if the operand was zero-extended. */
6536 result_type
6537 = c_common_signed_or_unsigned_type (unsigned_arg,
6538 TREE_TYPE (arg0));
6539 /* Convert value-to-be-shifted to that type. */
6540 if (TREE_TYPE (op0) != result_type)
6541 op0 = convert (result_type, op0);
6542 converted = 1;
6543 }
6544 }
6545
6546 /* Comparison operations are shortened too but differently.
6547 They identify themselves by setting short_compare = 1. */
6548
6549 if (short_compare)
6550 {
6551 /* We call shorten_compare only for diagnostics. */
6552 tree xop0 = fold_simple (op0);
6553 tree xop1 = fold_simple (op1);
6554 tree xresult_type = result_type;
6555 enum tree_code xresultcode = resultcode;
6556 shorten_compare (location, &xop0, &xop1, &xresult_type,
6557 &xresultcode);
6558 }
6559
6560 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6561 && warn_sign_compare
6562 /* Do not warn until the template is instantiated; we cannot
6563 bound the ranges of the arguments until that point. */
6564 && !processing_template_decl
6565 && (complain & tf_warning)
6566 && c_inhibit_evaluation_warnings == 0
6567 /* Even unsigned enum types promote to signed int. We don't
6568 want to issue -Wsign-compare warnings for this case. */
6569 && !enum_cast_to_int (op: orig_op0)
6570 && !enum_cast_to_int (op: orig_op1))
6571 {
6572 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6573 result_type, resultcode);
6574 }
6575 }
6576
6577 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6578 Then the expression will be built.
6579 It will be given type FINAL_TYPE if that is nonzero;
6580 otherwise, it will be given type RESULT_TYPE. */
6581 if (! converted)
6582 {
6583 warning_sentinel w (warn_sign_conversion, short_compare);
6584 if (!same_type_p (TREE_TYPE (op0), result_type))
6585 op0 = cp_convert_and_check (result_type, op0, complain);
6586 if (!same_type_p (TREE_TYPE (op1), result_type))
6587 op1 = cp_convert_and_check (result_type, op1, complain);
6588
6589 if (op0 == error_mark_node || op1 == error_mark_node)
6590 return error_mark_node;
6591 }
6592
6593 if (build_type == NULL_TREE)
6594 build_type = result_type;
6595
6596 if (doing_shift
6597 && flag_strong_eval_order == 2
6598 && TREE_SIDE_EFFECTS (op1)
6599 && !processing_template_decl)
6600 {
6601 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6602 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6603 op0 = cp_save_expr (op0);
6604 instrument_expr = op0;
6605 }
6606
6607 if (sanitize_flags_p (flag: (SANITIZE_SHIFT
6608 | SANITIZE_DIVIDE
6609 | SANITIZE_FLOAT_DIVIDE
6610 | SANITIZE_SI_OVERFLOW))
6611 && current_function_decl != NULL_TREE
6612 && !processing_template_decl
6613 && (doing_div_or_mod || doing_shift))
6614 {
6615 /* OP0 and/or OP1 might have side-effects. */
6616 op0 = cp_save_expr (op0);
6617 op1 = cp_save_expr (op1);
6618 op0 = fold_non_dependent_expr (op0, complain);
6619 op1 = fold_non_dependent_expr (op1, complain);
6620 tree instrument_expr1 = NULL_TREE;
6621 if (doing_div_or_mod
6622 && sanitize_flags_p (flag: SANITIZE_DIVIDE
6623 | SANITIZE_FLOAT_DIVIDE
6624 | SANITIZE_SI_OVERFLOW))
6625 {
6626 /* For diagnostics we want to use the promoted types without
6627 shorten_binary_op. So convert the arguments to the
6628 original result_type. */
6629 tree cop0 = op0;
6630 tree cop1 = op1;
6631 if (TREE_TYPE (cop0) != orig_type)
6632 cop0 = cp_convert (orig_type, op0, complain);
6633 if (TREE_TYPE (cop1) != orig_type)
6634 cop1 = cp_convert (orig_type, op1, complain);
6635 instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6636 }
6637 else if (doing_shift && sanitize_flags_p (flag: SANITIZE_SHIFT))
6638 instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6639 if (instrument_expr != NULL)
6640 instrument_expr = add_stmt_to_compound (instrument_expr,
6641 instrument_expr1);
6642 else
6643 instrument_expr = instrument_expr1;
6644 }
6645
6646 result = build2_loc (loc: location, code: resultcode, type: build_type, arg0: op0, arg1: op1);
6647 if (final_type != 0)
6648 result = cp_convert (final_type, result, complain);
6649
6650 if (instrument_expr != NULL)
6651 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6652 instrument_expr, result);
6653
6654 if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6655 result = get_target_expr (result, complain);
6656
6657 if (semantic_result_type)
6658 result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6659
6660 if (!c_inhibit_evaluation_warnings)
6661 {
6662 if (!processing_template_decl)
6663 {
6664 op0 = cp_fully_fold (op0);
6665 /* Only consider the second argument if the first isn't overflowed. */
6666 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6667 return result;
6668 op1 = cp_fully_fold (op1);
6669 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6670 return result;
6671 }
6672 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6673 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6674 return result;
6675
6676 tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6677 if (TREE_OVERFLOW_P (result_ovl))
6678 overflow_warning (location, result_ovl);
6679 }
6680
6681 return result;
6682}
6683
6684/* Build a VEC_PERM_EXPR.
6685 This is a simple wrapper for c_build_vec_perm_expr. */
6686tree
6687build_x_vec_perm_expr (location_t loc,
6688 tree arg0, tree arg1, tree arg2,
6689 tsubst_flags_t complain)
6690{
6691 tree orig_arg0 = arg0;
6692 tree orig_arg1 = arg1;
6693 tree orig_arg2 = arg2;
6694 if (processing_template_decl)
6695 {
6696 if (type_dependent_expression_p (arg0)
6697 || type_dependent_expression_p (arg1)
6698 || type_dependent_expression_p (arg2))
6699 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6700 }
6701 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6702 if (processing_template_decl && exp != error_mark_node)
6703 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6704 orig_arg1, orig_arg2);
6705 return exp;
6706}
6707
6708/* Build a VEC_PERM_EXPR.
6709 This is a simple wrapper for c_build_shufflevector. */
6710tree
6711build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6712 tsubst_flags_t complain)
6713{
6714 tree arg0 = (*args)[0];
6715 tree arg1 = (*args)[1];
6716 if (processing_template_decl)
6717 {
6718 for (unsigned i = 0; i < args->length (); ++i)
6719 if (i <= 1
6720 ? type_dependent_expression_p ((*args)[i])
6721 : instantiation_dependent_expression_p ((*args)[i]))
6722 {
6723 tree exp = build_min_nt_call_vec (NULL, args);
6724 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6725 return exp;
6726 }
6727 }
6728 auto_vec<tree, 16> mask;
6729 for (unsigned i = 2; i < args->length (); ++i)
6730 {
6731 tree idx = fold_non_dependent_expr ((*args)[i], complain);
6732 mask.safe_push (obj: idx);
6733 }
6734 tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6735 if (processing_template_decl && exp != error_mark_node)
6736 {
6737 exp = build_min_non_dep_call_vec (exp, NULL, args);
6738 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6739 }
6740 return exp;
6741}
6742
6743/* Return a tree for the sum or difference (RESULTCODE says which)
6744 of pointer PTROP and integer INTOP. */
6745
6746static tree
6747cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6748 tree intop, tsubst_flags_t complain)
6749{
6750 tree res_type = TREE_TYPE (ptrop);
6751
6752 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6753 in certain circumstance (when it's valid to do so). So we need
6754 to make sure it's complete. We don't need to check here, if we
6755 can actually complete it at all, as those checks will be done in
6756 pointer_int_sum() anyway. */
6757 complete_type (TREE_TYPE (res_type));
6758
6759 return pointer_int_sum (loc, resultcode, ptrop,
6760 intop, complain & tf_warning_or_error);
6761}
6762
6763/* Return a tree for the difference of pointers OP0 and OP1.
6764 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6765 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6766
6767static tree
6768pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6769 tsubst_flags_t complain, tree *instrument_expr)
6770{
6771 tree result, inttype;
6772 tree restype = ptrdiff_type_node;
6773 tree target_type = TREE_TYPE (ptrtype);
6774
6775 if (!complete_type_or_maybe_complain (type: target_type, NULL_TREE, complain))
6776 return error_mark_node;
6777
6778 if (VOID_TYPE_P (target_type))
6779 {
6780 if (complain & tf_error)
6781 permerror (loc, "ISO C++ forbids using pointer of "
6782 "type %<void *%> in subtraction");
6783 else
6784 return error_mark_node;
6785 }
6786 if (TREE_CODE (target_type) == FUNCTION_TYPE)
6787 {
6788 if (complain & tf_error)
6789 permerror (loc, "ISO C++ forbids using pointer to "
6790 "a function in subtraction");
6791 else
6792 return error_mark_node;
6793 }
6794 if (TREE_CODE (target_type) == METHOD_TYPE)
6795 {
6796 if (complain & tf_error)
6797 permerror (loc, "ISO C++ forbids using pointer to "
6798 "a method in subtraction");
6799 else
6800 return error_mark_node;
6801 }
6802 else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6803 TREE_TYPE (TREE_TYPE (op0)),
6804 !(complain & tf_error))
6805 || !verify_type_context (loc, TCTX_POINTER_ARITH,
6806 TREE_TYPE (TREE_TYPE (op1)),
6807 !(complain & tf_error)))
6808 return error_mark_node;
6809
6810 /* Determine integer type result of the subtraction. This will usually
6811 be the same as the result type (ptrdiff_t), but may need to be a wider
6812 type if pointers for the address space are wider than ptrdiff_t. */
6813 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6814 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6815 else
6816 inttype = restype;
6817
6818 if (!processing_template_decl
6819 && sanitize_flags_p (flag: SANITIZE_POINTER_SUBTRACT))
6820 {
6821 op0 = save_expr (op0);
6822 op1 = save_expr (op1);
6823
6824 tree tt = builtin_decl_explicit (fncode: BUILT_IN_ASAN_POINTER_SUBTRACT);
6825 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6826 }
6827
6828 /* First do the subtraction, then build the divide operator
6829 and only convert at the very end.
6830 Do not do default conversions in case restype is a short type. */
6831
6832 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6833 pointers. If some platform cannot provide that, or has a larger
6834 ptrdiff_type to support differences larger than half the address
6835 space, cast the pointers to some larger integer type and do the
6836 computations in that type. */
6837 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6838 op0 = cp_build_binary_op (location: loc,
6839 code: MINUS_EXPR,
6840 orig_op0: cp_convert (inttype, op0, complain),
6841 orig_op1: cp_convert (inttype, op1, complain),
6842 complain);
6843 else
6844 op0 = build2_loc (loc, code: POINTER_DIFF_EXPR, type: inttype, arg0: op0, arg1: op1);
6845
6846 /* This generates an error if op1 is a pointer to an incomplete type. */
6847 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6848 {
6849 if (complain & tf_error)
6850 error_at (loc, "invalid use of a pointer to an incomplete type in "
6851 "pointer arithmetic");
6852 else
6853 return error_mark_node;
6854 }
6855
6856 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6857 {
6858 if (complain & tf_error)
6859 error_at (loc, "arithmetic on pointer to an empty aggregate");
6860 else
6861 return error_mark_node;
6862 }
6863
6864 op1 = (TYPE_PTROB_P (ptrtype)
6865 ? size_in_bytes_loc (loc, target_type)
6866 : integer_one_node);
6867
6868 /* Do the division. */
6869
6870 result = build2_loc (loc, code: EXACT_DIV_EXPR, type: inttype, arg0: op0,
6871 arg1: cp_convert (inttype, op1, complain));
6872 return cp_convert (restype, result, complain);
6873}
6874
6875/* Construct and perhaps optimize a tree representation
6876 for a unary operation. CODE, a tree_code, specifies the operation
6877 and XARG is the operand. */
6878
6879tree
6880build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
6881 tree lookups, tsubst_flags_t complain)
6882{
6883 tree orig_expr = xarg;
6884 tree exp;
6885 int ptrmem = 0;
6886 tree overload = NULL_TREE;
6887
6888 if (processing_template_decl)
6889 {
6890 if (type_dependent_expression_p (xarg))
6891 {
6892 tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6893 TREE_TYPE (e) = build_dependent_operator_type (lookups, code, is_assign: false);
6894 return e;
6895 }
6896 }
6897
6898 exp = NULL_TREE;
6899
6900 /* [expr.unary.op] says:
6901
6902 The address of an object of incomplete type can be taken.
6903
6904 (And is just the ordinary address operator, not an overloaded
6905 "operator &".) However, if the type is a template
6906 specialization, we must complete the type at this point so that
6907 an overloaded "operator &" will be available if required. */
6908 if (code == ADDR_EXPR
6909 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6910 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6911 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6912 || (TREE_CODE (xarg) == OFFSET_REF)))
6913 /* Don't look for a function. */;
6914 else
6915 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6916 NULL_TREE, lookups, &overload, complain);
6917
6918 if (!exp && code == ADDR_EXPR)
6919 {
6920 if (is_overloaded_fn (xarg))
6921 {
6922 tree fn = get_first_fn (xarg);
6923 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
6924 {
6925 if (complain & tf_error)
6926 error_at (loc, DECL_CONSTRUCTOR_P (fn)
6927 ? G_("taking address of constructor %qD")
6928 : G_("taking address of destructor %qD"),
6929 fn);
6930 return error_mark_node;
6931 }
6932 }
6933
6934 /* A pointer to member-function can be formed only by saying
6935 &X::mf. */
6936 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
6937 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
6938 {
6939 if (TREE_CODE (xarg) != OFFSET_REF
6940 || !TYPE_P (TREE_OPERAND (xarg, 0)))
6941 {
6942 if (complain & tf_error)
6943 {
6944 error_at (loc, "invalid use of %qE to form a "
6945 "pointer-to-member-function", xarg.get_value ());
6946 if (TREE_CODE (xarg) != OFFSET_REF)
6947 inform (loc, " a qualified-id is required");
6948 }
6949 return error_mark_node;
6950 }
6951 else
6952 {
6953 if (complain & tf_error)
6954 error_at (loc, "parentheses around %qE cannot be used to "
6955 "form a pointer-to-member-function",
6956 xarg.get_value ());
6957 else
6958 return error_mark_node;
6959 PTRMEM_OK_P (xarg) = 1;
6960 }
6961 }
6962
6963 if (TREE_CODE (xarg) == OFFSET_REF)
6964 {
6965 ptrmem = PTRMEM_OK_P (xarg);
6966
6967 if (!ptrmem && !flag_ms_extensions
6968 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
6969 {
6970 /* A single non-static member, make sure we don't allow a
6971 pointer-to-member. */
6972 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
6973 TREE_OPERAND (xarg, 0),
6974 ovl_make (TREE_OPERAND (xarg, 1)));
6975 PTRMEM_OK_P (xarg) = ptrmem;
6976 }
6977 }
6978
6979 exp = cp_build_addr_expr_strict (xarg, complain);
6980
6981 if (TREE_CODE (exp) == PTRMEM_CST)
6982 PTRMEM_CST_LOCATION (exp) = loc;
6983 else
6984 protected_set_expr_location (exp, loc);
6985 }
6986
6987 if (processing_template_decl && exp != error_mark_node)
6988 {
6989 if (overload != NULL_TREE)
6990 return (build_min_non_dep_op_overload
6991 (code, exp, overload, orig_expr, integer_zero_node));
6992
6993 exp = build_min_non_dep (code, exp, orig_expr,
6994 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
6995 }
6996 if (TREE_CODE (exp) == ADDR_EXPR)
6997 PTRMEM_OK_P (exp) = ptrmem;
6998 return exp;
6999}
7000
7001/* Construct and perhaps optimize a tree representation
7002 for __builtin_addressof operation. ARG specifies the operand. */
7003
7004tree
7005cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7006{
7007 tree orig_expr = arg;
7008
7009 if (processing_template_decl)
7010 {
7011 if (type_dependent_expression_p (arg))
7012 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7013 }
7014
7015 tree exp = cp_build_addr_expr_strict (arg, complain);
7016
7017 if (processing_template_decl && exp != error_mark_node)
7018 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7019 return exp;
7020}
7021
7022/* Like c_common_truthvalue_conversion, but handle pointer-to-member
7023 constants, where a null value is represented by an INTEGER_CST of
7024 -1. */
7025
7026tree
7027cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7028{
7029 tree type = TREE_TYPE (expr);
7030 location_t loc = cp_expr_loc_or_input_loc (t: expr);
7031 if (TYPE_PTR_OR_PTRMEM_P (type)
7032 /* Avoid ICE on invalid use of non-static member function. */
7033 || TREE_CODE (expr) == FUNCTION_DECL)
7034 return cp_build_binary_op (location: loc, code: NE_EXPR, orig_op0: expr, nullptr_node, complain);
7035 else
7036 return c_common_truthvalue_conversion (loc, expr);
7037}
7038
7039/* Returns EXPR contextually converted to bool. */
7040
7041tree
7042contextual_conv_bool (tree expr, tsubst_flags_t complain)
7043{
7044 return perform_implicit_conversion_flags (boolean_type_node, expr,
7045 complain, LOOKUP_NORMAL);
7046}
7047
7048/* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7049 is a low-level function; most callers should use maybe_convert_cond. */
7050
7051tree
7052condition_conversion (tree expr)
7053{
7054 tree t = contextual_conv_bool (expr, complain: tf_warning_or_error);
7055 if (!processing_template_decl)
7056 t = fold_build_cleanup_point_expr (boolean_type_node, expr: t);
7057 return t;
7058}
7059
7060/* Returns the address of T. This function will fold away
7061 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7062 most places should use cp_build_addr_expr instead. */
7063
7064tree
7065build_address (tree t)
7066{
7067 if (error_operand_p (t) || !cxx_mark_addressable (t))
7068 return error_mark_node;
7069 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7070 || processing_template_decl);
7071 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7072 if (TREE_CODE (t) != ADDR_EXPR)
7073 t = rvalue (t);
7074 return t;
7075}
7076
7077/* Return a NOP_EXPR converting EXPR to TYPE. */
7078
7079tree
7080build_nop (tree type, tree expr)
7081{
7082 if (type == error_mark_node || error_operand_p (t: expr))
7083 return expr;
7084 return build1_loc (EXPR_LOCATION (expr), code: NOP_EXPR, type, arg1: expr);
7085}
7086
7087/* Take the address of ARG, whatever that means under C++ semantics.
7088 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7089 and class rvalues as well.
7090
7091 Nothing should call this function directly; instead, callers should use
7092 cp_build_addr_expr or cp_build_addr_expr_strict. */
7093
7094static tree
7095cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7096{
7097 tree argtype;
7098 tree val;
7099
7100 if (!arg || error_operand_p (t: arg))
7101 return error_mark_node;
7102
7103 arg = mark_lvalue_use (arg);
7104 if (error_operand_p (t: arg))
7105 return error_mark_node;
7106
7107 argtype = lvalue_type (arg);
7108 location_t loc = cp_expr_loc_or_input_loc (t: arg);
7109
7110 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7111
7112 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (expr: arg)
7113 && !really_overloaded_fn (arg))
7114 {
7115 /* They're trying to take the address of a unique non-static
7116 member function. This is ill-formed (except in MS-land),
7117 but let's try to DTRT.
7118 Note: We only handle unique functions here because we don't
7119 want to complain if there's a static overload; non-unique
7120 cases will be handled by instantiate_type. But we need to
7121 handle this case here to allow casts on the resulting PMF.
7122 We could defer this in non-MS mode, but it's easier to give
7123 a useful error here. */
7124
7125 /* Inside constant member functions, the `this' pointer
7126 contains an extra const qualifier. TYPE_MAIN_VARIANT
7127 is used here to remove this const from the diagnostics
7128 and the created OFFSET_REF. */
7129 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7130 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7131 if (!mark_used (fn, complain) && !(complain & tf_error))
7132 return error_mark_node;
7133 /* Until microsoft headers are known to incorrectly take the address of
7134 unqualified xobj member functions we should not support this
7135 extension.
7136 See comment in class.cc:resolve_address_of_overloaded_function for
7137 the extended reasoning. */
7138 if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7139 {
7140 auto_diagnostic_group d;
7141 tree name = DECL_NAME (fn);
7142 if (!(complain & tf_error))
7143 return error_mark_node;
7144 else if (current_class_type
7145 && TREE_OPERAND (arg, 0) == current_class_ref)
7146 /* An expression like &memfn. */
7147 if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7148 permerror (loc,
7149 "ISO C++ forbids taking the address of an unqualified"
7150 " or parenthesized non-static member function to form"
7151 " a pointer to member function. Say %<&%T::%D%>",
7152 base, name);
7153 else
7154 error_at (loc,
7155 "ISO C++ forbids taking the address of an unqualified"
7156 " or parenthesized non-static member function to form"
7157 " a pointer to explicit object member function");
7158 else
7159 if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7160 permerror (loc,
7161 "ISO C++ forbids taking the address of a bound member"
7162 " function to form a pointer to member function."
7163 " Say %<&%T::%D%>",
7164 base, name);
7165 else
7166 error_at (loc,
7167 "ISO C++ forbids taking the address of a bound member"
7168 " function to form a pointer to explicit object member"
7169 " function");
7170 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7171 inform (loc,
7172 "a pointer to explicit object member function can only be "
7173 "formed with %<&%T::%D%>", base, name);
7174 }
7175 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7176 }
7177
7178 /* Uninstantiated types are all functions. Taking the
7179 address of a function is a no-op, so just return the
7180 argument. */
7181 if (type_unknown_p (expr: arg))
7182 return build1 (ADDR_EXPR, unknown_type_node, arg);
7183
7184 if (TREE_CODE (arg) == OFFSET_REF)
7185 /* We want a pointer to member; bypass all the code for actually taking
7186 the address of something. */
7187 goto offset_ref;
7188
7189 /* Anything not already handled and not a true memory reference
7190 is an error. */
7191 if (!FUNC_OR_METHOD_TYPE_P (argtype))
7192 {
7193 cp_lvalue_kind kind = lvalue_kind (arg);
7194 if (kind == clk_none)
7195 {
7196 if (complain & tf_error)
7197 lvalue_error (loc, lv_addressof);
7198 return error_mark_node;
7199 }
7200 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7201 {
7202 if (!(complain & tf_error))
7203 return error_mark_node;
7204 /* Make this a permerror because we used to accept it. */
7205 permerror (loc, "taking address of rvalue");
7206 }
7207 }
7208
7209 if (TYPE_REF_P (argtype))
7210 {
7211 tree type = build_pointer_type (TREE_TYPE (argtype));
7212 arg = build1 (CONVERT_EXPR, type, arg);
7213 return arg;
7214 }
7215 else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7216 {
7217 /* ARM $3.4 */
7218 /* Apparently a lot of autoconf scripts for C++ packages do this,
7219 so only complain if -Wpedantic. */
7220 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7221 pedwarn (loc, OPT_Wpedantic,
7222 "ISO C++ forbids taking address of function %<::main%>");
7223 else if (flag_pedantic_errors)
7224 return error_mark_node;
7225 }
7226
7227 /* Let &* cancel out to simplify resulting code. */
7228 if (INDIRECT_REF_P (arg))
7229 {
7230 arg = TREE_OPERAND (arg, 0);
7231 if (TYPE_REF_P (TREE_TYPE (arg)))
7232 {
7233 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7234 arg = build1 (CONVERT_EXPR, type, arg);
7235 }
7236 else
7237 /* Don't let this be an lvalue. */
7238 arg = rvalue (arg);
7239 return arg;
7240 }
7241
7242 /* Handle complex lvalues (when permitted)
7243 by reduction to simpler cases. */
7244 val = unary_complex_lvalue (ADDR_EXPR, arg);
7245 if (val != 0)
7246 return val;
7247
7248 switch (TREE_CODE (arg))
7249 {
7250 CASE_CONVERT:
7251 case FLOAT_EXPR:
7252 case FIX_TRUNC_EXPR:
7253 /* We should have handled this above in the lvalue_kind check. */
7254 gcc_unreachable ();
7255 break;
7256
7257 case BASELINK:
7258 arg = BASELINK_FUNCTIONS (arg);
7259 /* Fall through. */
7260
7261 case OVERLOAD:
7262 arg = OVL_FIRST (arg);
7263 break;
7264
7265 case OFFSET_REF:
7266 offset_ref:
7267 /* Turn a reference to a non-static data member into a
7268 pointer-to-member. */
7269 {
7270 tree type;
7271 tree t;
7272
7273 gcc_assert (PTRMEM_OK_P (arg));
7274
7275 t = TREE_OPERAND (arg, 1);
7276 if (TYPE_REF_P (TREE_TYPE (t)))
7277 {
7278 if (complain & tf_error)
7279 error_at (loc,
7280 "cannot create pointer to reference member %qD", t);
7281 return error_mark_node;
7282 }
7283
7284 /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7285 if (TREE_CODE (t) == FUNCTION_DECL
7286 && !DECL_PURE_VIRTUAL_P (t)
7287 && !mark_used (t, complain) && !(complain & tf_error))
7288 return error_mark_node;
7289
7290 /* Pull out the function_decl for a single xobj member function, and
7291 let the rest of this function handle it. This is similar to how
7292 static member functions are handled in the BASELINK case above. */
7293 if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7294 {
7295 arg = t;
7296 break;
7297 }
7298
7299 type = build_ptrmem_type (context_for_name_lookup (t),
7300 TREE_TYPE (t));
7301 t = make_ptrmem_cst (type, t);
7302 return t;
7303 }
7304
7305 default:
7306 break;
7307 }
7308
7309 if (argtype != error_mark_node)
7310 argtype = build_pointer_type (argtype);
7311
7312 if (bitfield_p (arg))
7313 {
7314 if (complain & tf_error)
7315 error_at (loc, "attempt to take address of bit-field");
7316 return error_mark_node;
7317 }
7318
7319 /* In a template, we are processing a non-dependent expression
7320 so we can just form an ADDR_EXPR with the correct type. */
7321 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7322 {
7323 if (!mark_single_function (arg, complain))
7324 return error_mark_node;
7325 val = build_address (t: arg);
7326 if (TREE_CODE (arg) == OFFSET_REF)
7327 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7328 }
7329 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7330 {
7331 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7332
7333 /* We can only get here with a single static member
7334 function. */
7335 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7336 && DECL_STATIC_FUNCTION_P (fn));
7337 if (!mark_used (fn, complain) && !(complain & tf_error))
7338 return error_mark_node;
7339 val = build_address (t: fn);
7340 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7341 /* Do not lose object's side effects. */
7342 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7343 TREE_OPERAND (arg, 0), val);
7344 }
7345 else
7346 {
7347 tree object = TREE_OPERAND (arg, 0);
7348 tree field = TREE_OPERAND (arg, 1);
7349 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7350 (TREE_TYPE (object), decl_type_context (field)));
7351 val = build_address (t: arg);
7352 }
7353
7354 if (TYPE_PTR_P (argtype)
7355 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7356 {
7357 build_ptrmemfunc_type (argtype);
7358 val = build_ptrmemfunc (argtype, val, 0,
7359 /*c_cast_p=*/false,
7360 complain);
7361 }
7362
7363 /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7364 if (TREE_CODE (val) == ADDR_EXPR
7365 && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7366 SET_EXPR_LOCATION (val, input_location);
7367
7368 return val;
7369}
7370
7371/* Take the address of ARG if it has one, even if it's an rvalue. */
7372
7373tree
7374cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7375{
7376 return cp_build_addr_expr_1 (arg, strict_lvalue: 0, complain);
7377}
7378
7379/* Take the address of ARG, but only if it's an lvalue. */
7380
7381static tree
7382cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7383{
7384 return cp_build_addr_expr_1 (arg, strict_lvalue: 1, complain);
7385}
7386
7387/* C++: Must handle pointers to members.
7388
7389 Perhaps type instantiation should be extended to handle conversion
7390 from aggregates to types we don't yet know we want? (Or are those
7391 cases typically errors which should be reported?)
7392
7393 NOCONVERT suppresses the default promotions (such as from short to int). */
7394
7395tree
7396cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7397 tsubst_flags_t complain)
7398{
7399 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7400 tree arg = xarg;
7401 location_t location = cp_expr_loc_or_input_loc (t: arg);
7402 tree argtype = 0;
7403 tree eptype = NULL_TREE;
7404 const char *errstring = NULL;
7405 tree val;
7406 const char *invalid_op_diag;
7407
7408 if (!arg || error_operand_p (t: arg))
7409 return error_mark_node;
7410
7411 arg = resolve_nondeduced_context (arg, complain);
7412
7413 if ((invalid_op_diag
7414 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7415 ? CONVERT_EXPR
7416 : code),
7417 TREE_TYPE (arg))))
7418 {
7419 if (complain & tf_error)
7420 error (invalid_op_diag);
7421 return error_mark_node;
7422 }
7423
7424 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7425 {
7426 eptype = TREE_TYPE (arg);
7427 arg = TREE_OPERAND (arg, 0);
7428 }
7429
7430 switch (code)
7431 {
7432 case UNARY_PLUS_EXPR:
7433 case NEGATE_EXPR:
7434 {
7435 int flags = WANT_ARITH | WANT_ENUM;
7436 /* Unary plus (but not unary minus) is allowed on pointers. */
7437 if (code == UNARY_PLUS_EXPR)
7438 flags |= WANT_POINTER;
7439 arg = build_expr_type_conversion (flags, arg, true);
7440 if (!arg)
7441 errstring = (code == NEGATE_EXPR
7442 ? _("wrong type argument to unary minus")
7443 : _("wrong type argument to unary plus"));
7444 else
7445 {
7446 if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7447 arg = cp_perform_integral_promotions (expr: arg, complain);
7448
7449 /* Make sure the result is not an lvalue: a unary plus or minus
7450 expression is always a rvalue. */
7451 arg = rvalue (arg);
7452 }
7453 }
7454 break;
7455
7456 case BIT_NOT_EXPR:
7457 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7458 {
7459 code = CONJ_EXPR;
7460 if (!noconvert)
7461 {
7462 arg = cp_default_conversion (exp: arg, complain);
7463 if (arg == error_mark_node)
7464 return error_mark_node;
7465 }
7466 }
7467 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7468 | WANT_VECTOR_OR_COMPLEX,
7469 arg, true)))
7470 errstring = _("wrong type argument to bit-complement");
7471 else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7472 {
7473 /* Warn if the expression has boolean value. */
7474 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7475 && (complain & tf_warning)
7476 && warning_at (location, OPT_Wbool_operation,
7477 "%<~%> on an expression of type %<bool%>"))
7478 inform (location, "did you mean to use logical not (%<!%>)?");
7479 arg = cp_perform_integral_promotions (expr: arg, complain);
7480 }
7481 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7482 arg = mark_rvalue_use (arg);
7483 break;
7484
7485 case ABS_EXPR:
7486 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7487 errstring = _("wrong type argument to abs");
7488 else if (!noconvert)
7489 {
7490 arg = cp_default_conversion (exp: arg, complain);
7491 if (arg == error_mark_node)
7492 return error_mark_node;
7493 }
7494 break;
7495
7496 case CONJ_EXPR:
7497 /* Conjugating a real value is a no-op, but allow it anyway. */
7498 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7499 errstring = _("wrong type argument to conjugation");
7500 else if (!noconvert)
7501 {
7502 arg = cp_default_conversion (exp: arg, complain);
7503 if (arg == error_mark_node)
7504 return error_mark_node;
7505 }
7506 break;
7507
7508 case TRUTH_NOT_EXPR:
7509 if (gnu_vector_type_p (TREE_TYPE (arg)))
7510 return cp_build_binary_op (location: input_location, code: EQ_EXPR, orig_op0: arg,
7511 orig_op1: build_zero_cst (TREE_TYPE (arg)), complain);
7512 arg = perform_implicit_conversion (boolean_type_node, arg,
7513 complain);
7514 if (arg != error_mark_node)
7515 {
7516 if (processing_template_decl)
7517 return build1_loc (loc: location, code: TRUTH_NOT_EXPR, boolean_type_node, arg1: arg);
7518 val = invert_truthvalue_loc (location, arg);
7519 if (obvalue_p (val))
7520 val = non_lvalue_loc (location, val);
7521 return val;
7522 }
7523 errstring = _("in argument to unary !");
7524 break;
7525
7526 case NOP_EXPR:
7527 break;
7528
7529 case REALPART_EXPR:
7530 case IMAGPART_EXPR:
7531 val = build_real_imag_expr (input_location, code, arg);
7532 if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7533 val = build1_loc (loc: input_location, code: EXCESS_PRECISION_EXPR,
7534 TREE_TYPE (eptype), arg1: val);
7535 return val;
7536
7537 case PREINCREMENT_EXPR:
7538 case POSTINCREMENT_EXPR:
7539 case PREDECREMENT_EXPR:
7540 case POSTDECREMENT_EXPR:
7541 /* Handle complex lvalues (when permitted)
7542 by reduction to simpler cases. */
7543
7544 val = unary_complex_lvalue (code, arg);
7545 if (val != 0)
7546 goto return_build_unary_op;
7547
7548 arg = mark_lvalue_use (arg);
7549
7550 /* Increment or decrement the real part of the value,
7551 and don't change the imaginary part. */
7552 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7553 {
7554 tree real, imag;
7555
7556 arg = cp_stabilize_reference (arg);
7557 real = cp_build_unary_op (code: REALPART_EXPR, xarg: arg, noconvert: true, complain);
7558 imag = cp_build_unary_op (code: IMAGPART_EXPR, xarg: arg, noconvert: true, complain);
7559 real = cp_build_unary_op (code, xarg: real, noconvert: true, complain);
7560 if (real == error_mark_node || imag == error_mark_node)
7561 return error_mark_node;
7562 val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7563 goto return_build_unary_op;
7564 }
7565
7566 /* Report invalid types. */
7567
7568 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7569 arg, true)))
7570 {
7571 if (code == PREINCREMENT_EXPR)
7572 errstring = _("no pre-increment operator for type");
7573 else if (code == POSTINCREMENT_EXPR)
7574 errstring = _("no post-increment operator for type");
7575 else if (code == PREDECREMENT_EXPR)
7576 errstring = _("no pre-decrement operator for type");
7577 else
7578 errstring = _("no post-decrement operator for type");
7579 break;
7580 }
7581 else if (arg == error_mark_node)
7582 return error_mark_node;
7583
7584 /* Report something read-only. */
7585
7586 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7587 || TREE_READONLY (arg))
7588 {
7589 if (complain & tf_error)
7590 cxx_readonly_error (location, arg,
7591 ((code == PREINCREMENT_EXPR
7592 || code == POSTINCREMENT_EXPR)
7593 ? lv_increment : lv_decrement));
7594 else
7595 return error_mark_node;
7596 }
7597
7598 {
7599 tree inc;
7600 tree declared_type = unlowered_expr_type (exp: arg);
7601
7602 argtype = TREE_TYPE (arg);
7603
7604 /* ARM $5.2.5 last annotation says this should be forbidden. */
7605 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7606 {
7607 if (complain & tf_error)
7608 permerror (location, (code == PREINCREMENT_EXPR
7609 || code == POSTINCREMENT_EXPR)
7610 ? G_("ISO C++ forbids incrementing an enum")
7611 : G_("ISO C++ forbids decrementing an enum"));
7612 else
7613 return error_mark_node;
7614 }
7615
7616 /* Compute the increment. */
7617
7618 if (TYPE_PTR_P (argtype))
7619 {
7620 tree type = complete_type (TREE_TYPE (argtype));
7621
7622 if (!COMPLETE_OR_VOID_TYPE_P (type))
7623 {
7624 if (complain & tf_error)
7625 error_at (location, ((code == PREINCREMENT_EXPR
7626 || code == POSTINCREMENT_EXPR))
7627 ? G_("cannot increment a pointer to incomplete "
7628 "type %qT")
7629 : G_("cannot decrement a pointer to incomplete "
7630 "type %qT"),
7631 TREE_TYPE (argtype));
7632 else
7633 return error_mark_node;
7634 }
7635 else if (!TYPE_PTROB_P (argtype))
7636 {
7637 if (complain & tf_error)
7638 pedwarn (location, OPT_Wpointer_arith,
7639 (code == PREINCREMENT_EXPR
7640 || code == POSTINCREMENT_EXPR)
7641 ? G_("ISO C++ forbids incrementing a pointer "
7642 "of type %qT")
7643 : G_("ISO C++ forbids decrementing a pointer "
7644 "of type %qT"),
7645 argtype);
7646 else
7647 return error_mark_node;
7648 }
7649 else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7650 TREE_TYPE (argtype),
7651 !(complain & tf_error)))
7652 return error_mark_node;
7653
7654 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7655 }
7656 else
7657 inc = VECTOR_TYPE_P (argtype)
7658 ? build_one_cst (argtype)
7659 : integer_one_node;
7660
7661 inc = cp_convert (argtype, inc, complain);
7662
7663 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7664 need to ask Objective-C to build the increment or decrement
7665 expression for it. */
7666 if (objc_is_property_ref (arg))
7667 return objc_build_incr_expr_for_property_ref (input_location, code,
7668 arg, inc);
7669
7670 /* Complain about anything else that is not a true lvalue. */
7671 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7672 || code == POSTINCREMENT_EXPR)
7673 ? lv_increment : lv_decrement),
7674 complain))
7675 return error_mark_node;
7676
7677 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7678 prefix ++ and -- expressions of volatile-qualified arithmetic
7679 and pointer types are deprecated." */
7680 if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7681 && (complain & tf_warning))
7682 warning_at (location, OPT_Wvolatile,
7683 "%qs expression of %<volatile%>-qualified type is "
7684 "deprecated",
7685 ((code == PREINCREMENT_EXPR
7686 || code == POSTINCREMENT_EXPR)
7687 ? "++" : "--"));
7688
7689 /* Forbid using -- or ++ in C++17 on `bool'. */
7690 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7691 {
7692 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7693 {
7694 if (complain & tf_error)
7695 error_at (location,
7696 "use of an operand of type %qT in %<operator--%> "
7697 "is forbidden", boolean_type_node);
7698 return error_mark_node;
7699 }
7700 else
7701 {
7702 if (cxx_dialect >= cxx17)
7703 {
7704 if (complain & tf_error)
7705 error_at (location,
7706 "use of an operand of type %qT in "
7707 "%<operator++%> is forbidden in C++17",
7708 boolean_type_node);
7709 return error_mark_node;
7710 }
7711 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7712 else if (complain & tf_warning)
7713 warning_at (location, OPT_Wdeprecated,
7714 "use of an operand of type %qT "
7715 "in %<operator++%> is deprecated",
7716 boolean_type_node);
7717 }
7718 val = boolean_increment (code, arg);
7719 }
7720 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7721 /* An rvalue has no cv-qualifiers. */
7722 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7723 else
7724 val = build2 (code, TREE_TYPE (arg), arg, inc);
7725
7726 TREE_SIDE_EFFECTS (val) = 1;
7727 goto return_build_unary_op;
7728 }
7729
7730 case ADDR_EXPR:
7731 /* Note that this operation never does default_conversion
7732 regardless of NOCONVERT. */
7733 return cp_build_addr_expr (arg, complain);
7734
7735 default:
7736 break;
7737 }
7738
7739 if (!errstring)
7740 {
7741 if (argtype == 0)
7742 argtype = TREE_TYPE (arg);
7743 val = build1 (code, argtype, arg);
7744 return_build_unary_op:
7745 if (eptype)
7746 val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7747 return val;
7748 }
7749
7750 if (complain & tf_error)
7751 error_at (location, "%s", errstring);
7752 return error_mark_node;
7753}
7754
7755/* Hook for the c-common bits that build a unary op. */
7756tree
7757build_unary_op (location_t /*location*/,
7758 enum tree_code code, tree xarg, bool noconvert)
7759{
7760 return cp_build_unary_op (code, xarg, noconvert, complain: tf_warning_or_error);
7761}
7762
7763/* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7764 so that it is a valid lvalue even for GENERIC by replacing
7765 (lhs = rhs) with ((lhs = rhs), lhs)
7766 (--lhs) with ((--lhs), lhs)
7767 (++lhs) with ((++lhs), lhs)
7768 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7769 that it can be evaluated multiple times. */
7770
7771tree
7772genericize_compound_lvalue (tree lvalue)
7773{
7774 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7775 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7776 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7777 TREE_OPERAND (lvalue, 1));
7778 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7779 lvalue, TREE_OPERAND (lvalue, 0));
7780}
7781
7782/* Apply unary lvalue-demanding operator CODE to the expression ARG
7783 for certain kinds of expressions which are not really lvalues
7784 but which we can accept as lvalues.
7785
7786 If ARG is not a kind of expression we can handle, return
7787 NULL_TREE. */
7788
7789tree
7790unary_complex_lvalue (enum tree_code code, tree arg)
7791{
7792 /* Inside a template, making these kinds of adjustments is
7793 pointless; we are only concerned with the type of the
7794 expression. */
7795 if (processing_template_decl)
7796 return NULL_TREE;
7797
7798 /* Handle (a, b) used as an "lvalue". */
7799 if (TREE_CODE (arg) == COMPOUND_EXPR)
7800 {
7801 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), noconvert: false,
7802 complain: tf_warning_or_error);
7803 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7804 TREE_OPERAND (arg, 0), real_result);
7805 }
7806
7807 /* Handle (a ? b : c) used as an "lvalue". */
7808 if (TREE_CODE (arg) == COND_EXPR
7809 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7810 return rationalize_conditional_expr (code, t: arg, complain: tf_warning_or_error);
7811
7812 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7813 if (TREE_CODE (arg) == MODIFY_EXPR
7814 || TREE_CODE (arg) == PREINCREMENT_EXPR
7815 || TREE_CODE (arg) == PREDECREMENT_EXPR)
7816 return unary_complex_lvalue (code, arg: genericize_compound_lvalue (lvalue: arg));
7817
7818 if (code != ADDR_EXPR)
7819 return NULL_TREE;
7820
7821 /* Handle (a = b) used as an "lvalue" for `&'. */
7822 if (TREE_CODE (arg) == MODIFY_EXPR
7823 || TREE_CODE (arg) == INIT_EXPR)
7824 {
7825 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), noconvert: false,
7826 complain: tf_warning_or_error);
7827 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7828 arg, real_result);
7829 suppress_warning (arg /* What warning? */);
7830 return arg;
7831 }
7832
7833 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7834 || TREE_CODE (arg) == OFFSET_REF)
7835 return NULL_TREE;
7836
7837 /* We permit compiler to make function calls returning
7838 objects of aggregate type look like lvalues. */
7839 {
7840 tree targ = arg;
7841
7842 if (TREE_CODE (targ) == SAVE_EXPR)
7843 targ = TREE_OPERAND (targ, 0);
7844
7845 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7846 {
7847 if (TREE_CODE (arg) == SAVE_EXPR)
7848 targ = arg;
7849 else
7850 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7851 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7852 }
7853
7854 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
7855 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
7856 TREE_OPERAND (targ, 0), current_function_decl, NULL);
7857 }
7858
7859 /* Don't let anything else be handled specially. */
7860 return NULL_TREE;
7861}
7862
7863/* Mark EXP saying that we need to be able to take the
7864 address of it; it should not be allocated in a register.
7865 Value is true if successful. ARRAY_REF_P is true if this
7866 is for ARRAY_REF construction - in that case we don't want
7867 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7868 it is fine to use ARRAY_REFs for vector subscripts on vector
7869 register variables.
7870
7871 C++: we do not allow `current_class_ptr' to be addressable. */
7872
7873bool
7874cxx_mark_addressable (tree exp, bool array_ref_p)
7875{
7876 tree x = exp;
7877
7878 while (1)
7879 switch (TREE_CODE (x))
7880 {
7881 case VIEW_CONVERT_EXPR:
7882 if (array_ref_p
7883 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7884 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
7885 return true;
7886 x = TREE_OPERAND (x, 0);
7887 break;
7888
7889 case COMPONENT_REF:
7890 if (bitfield_p (x))
7891 error ("attempt to take address of bit-field");
7892 /* FALLTHRU */
7893 case ADDR_EXPR:
7894 case ARRAY_REF:
7895 case REALPART_EXPR:
7896 case IMAGPART_EXPR:
7897 x = TREE_OPERAND (x, 0);
7898 break;
7899
7900 case PARM_DECL:
7901 if (x == current_class_ptr)
7902 {
7903 error ("cannot take the address of %<this%>, which is an rvalue expression");
7904 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
7905 return true;
7906 }
7907 /* Fall through. */
7908
7909 case VAR_DECL:
7910 /* Caller should not be trying to mark initialized
7911 constant fields addressable. */
7912 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
7913 || DECL_IN_AGGR_P (x) == 0
7914 || TREE_STATIC (x)
7915 || DECL_EXTERNAL (x));
7916 /* Fall through. */
7917
7918 case RESULT_DECL:
7919 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
7920 && !DECL_ARTIFICIAL (x))
7921 {
7922 if (VAR_P (x) && DECL_HARD_REGISTER (x))
7923 {
7924 error
7925 ("address of explicit register variable %qD requested", x);
7926 return false;
7927 }
7928 else if (extra_warnings)
7929 warning
7930 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
7931 }
7932 TREE_ADDRESSABLE (x) = 1;
7933 return true;
7934
7935 case CONST_DECL:
7936 case FUNCTION_DECL:
7937 TREE_ADDRESSABLE (x) = 1;
7938 return true;
7939
7940 case CONSTRUCTOR:
7941 TREE_ADDRESSABLE (x) = 1;
7942 return true;
7943
7944 case TARGET_EXPR:
7945 TREE_ADDRESSABLE (x) = 1;
7946 cxx_mark_addressable (TREE_OPERAND (x, 0));
7947 return true;
7948
7949 default:
7950 return true;
7951 }
7952}
7953
7954/* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7955
7956tree
7957build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
7958 tsubst_flags_t complain)
7959{
7960 tree orig_ifexp = ifexp;
7961 tree orig_op1 = op1;
7962 tree orig_op2 = op2;
7963 tree expr;
7964
7965 if (processing_template_decl)
7966 {
7967 /* The standard says that the expression is type-dependent if
7968 IFEXP is type-dependent, even though the eventual type of the
7969 expression doesn't dependent on IFEXP. */
7970 if (type_dependent_expression_p (ifexp)
7971 /* As a GNU extension, the middle operand may be omitted. */
7972 || (op1 && type_dependent_expression_p (op1))
7973 || type_dependent_expression_p (op2))
7974 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
7975 }
7976
7977 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
7978 if (processing_template_decl && expr != error_mark_node)
7979 {
7980 tree min = build_min_non_dep (COND_EXPR, expr,
7981 orig_ifexp, orig_op1, orig_op2);
7982 expr = convert_from_reference (min);
7983 }
7984 return expr;
7985}
7986
7987/* Given a list of expressions, return a compound expression
7988 that performs them all and returns the value of the last of them. */
7989
7990tree
7991build_x_compound_expr_from_list (tree list, expr_list_kind exp,
7992 tsubst_flags_t complain)
7993{
7994 tree expr = TREE_VALUE (list);
7995
7996 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7997 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
7998 {
7999 if (complain & tf_error)
8000 pedwarn (cp_expr_loc_or_input_loc (t: expr), 0,
8001 "list-initializer for non-class type must not "
8002 "be parenthesized");
8003 else
8004 return error_mark_node;
8005 }
8006
8007 if (TREE_CHAIN (list))
8008 {
8009 if (complain & tf_error)
8010 switch (exp)
8011 {
8012 case ELK_INIT:
8013 permerror (input_location, "expression list treated as compound "
8014 "expression in initializer");
8015 break;
8016 case ELK_MEM_INIT:
8017 permerror (input_location, "expression list treated as compound "
8018 "expression in mem-initializer");
8019 break;
8020 case ELK_FUNC_CAST:
8021 permerror (input_location, "expression list treated as compound "
8022 "expression in functional cast");
8023 break;
8024 default:
8025 gcc_unreachable ();
8026 }
8027 else
8028 return error_mark_node;
8029
8030 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8031 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8032 expr, TREE_VALUE (list), NULL_TREE,
8033 complain);
8034 }
8035
8036 return expr;
8037}
8038
8039/* Like build_x_compound_expr_from_list, but using a VEC. */
8040
8041tree
8042build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8043 tsubst_flags_t complain)
8044{
8045 if (vec_safe_is_empty (v: vec))
8046 return NULL_TREE;
8047 else if (vec->length () == 1)
8048 return (*vec)[0];
8049 else
8050 {
8051 tree expr;
8052 unsigned int ix;
8053 tree t;
8054
8055 if (msg != NULL)
8056 {
8057 if (complain & tf_error)
8058 permerror (input_location,
8059 "%s expression list treated as compound expression",
8060 msg);
8061 else
8062 return error_mark_node;
8063 }
8064
8065 expr = (*vec)[0];
8066 for (ix = 1; vec->iterate (ix, ptr: &t); ++ix)
8067 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8068 t, NULL_TREE, complain);
8069
8070 return expr;
8071 }
8072}
8073
8074/* Handle overloading of the ',' operator when needed. */
8075
8076tree
8077build_x_compound_expr (location_t loc, tree op1, tree op2,
8078 tree lookups, tsubst_flags_t complain)
8079{
8080 tree result;
8081 tree orig_op1 = op1;
8082 tree orig_op2 = op2;
8083 tree overload = NULL_TREE;
8084
8085 if (processing_template_decl)
8086 {
8087 if (type_dependent_expression_p (op1)
8088 || type_dependent_expression_p (op2))
8089 {
8090 result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8091 TREE_TYPE (result)
8092 = build_dependent_operator_type (lookups, code: COMPOUND_EXPR, is_assign: false);
8093 return result;
8094 }
8095 }
8096
8097 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8098 NULL_TREE, lookups, &overload, complain);
8099 if (!result)
8100 result = cp_build_compound_expr (op1, op2, complain);
8101
8102 if (processing_template_decl && result != error_mark_node)
8103 {
8104 if (overload != NULL_TREE)
8105 return (build_min_non_dep_op_overload
8106 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8107
8108 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8109 }
8110
8111 return result;
8112}
8113
8114/* Like cp_build_compound_expr, but for the c-common bits. */
8115
8116tree
8117build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8118{
8119 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8120}
8121
8122/* Build a compound expression. */
8123
8124tree
8125cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8126{
8127 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8128
8129 if (lhs == error_mark_node || rhs == error_mark_node)
8130 return error_mark_node;
8131
8132 if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8133 lhs = TREE_OPERAND (lhs, 0);
8134 tree eptype = NULL_TREE;
8135 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8136 {
8137 eptype = TREE_TYPE (rhs);
8138 rhs = TREE_OPERAND (rhs, 0);
8139 }
8140
8141 if (TREE_CODE (rhs) == TARGET_EXPR)
8142 {
8143 /* If the rhs is a TARGET_EXPR, then build the compound
8144 expression inside the target_expr's initializer. This
8145 helps the compiler to eliminate unnecessary temporaries. */
8146 tree init = TREE_OPERAND (rhs, 1);
8147
8148 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8149 TREE_OPERAND (rhs, 1) = init;
8150
8151 if (eptype)
8152 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8153 return rhs;
8154 }
8155
8156 if (type_unknown_p (expr: rhs))
8157 {
8158 if (complain & tf_error)
8159 error_at (cp_expr_loc_or_input_loc (t: rhs),
8160 "no context to resolve type of %qE", rhs);
8161 return error_mark_node;
8162 }
8163
8164 tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8165 if (eptype)
8166 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8167 return ret;
8168}
8169
8170/* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8171 casts away constness. CAST gives the type of cast. Returns true
8172 if the cast is ill-formed, false if it is well-formed.
8173
8174 ??? This function warns for casting away any qualifier not just
8175 const. We would like to specify exactly what qualifiers are casted
8176 away.
8177*/
8178
8179static bool
8180check_for_casting_away_constness (location_t loc, tree src_type,
8181 tree dest_type, enum tree_code cast,
8182 tsubst_flags_t complain)
8183{
8184 /* C-style casts are allowed to cast away constness. With
8185 WARN_CAST_QUAL, we still want to issue a warning. */
8186 if (cast == CAST_EXPR && !warn_cast_qual)
8187 return false;
8188
8189 if (!casts_away_constness (src_type, dest_type, complain))
8190 return false;
8191
8192 switch (cast)
8193 {
8194 case CAST_EXPR:
8195 if (complain & tf_warning)
8196 warning_at (loc, OPT_Wcast_qual,
8197 "cast from type %qT to type %qT casts away qualifiers",
8198 src_type, dest_type);
8199 return false;
8200
8201 case STATIC_CAST_EXPR:
8202 if (complain & tf_error)
8203 error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8204 "away qualifiers",
8205 src_type, dest_type);
8206 return true;
8207
8208 case REINTERPRET_CAST_EXPR:
8209 if (complain & tf_error)
8210 error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8211 "casts away qualifiers",
8212 src_type, dest_type);
8213 return true;
8214
8215 default:
8216 gcc_unreachable();
8217 }
8218}
8219
8220/* Warns if the cast from expression EXPR to type TYPE is useless. */
8221void
8222maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8223 tsubst_flags_t complain)
8224{
8225 if (warn_useless_cast
8226 && complain & tf_warning)
8227 {
8228 if (TYPE_REF_P (type)
8229 ? ((TYPE_REF_IS_RVALUE (type)
8230 ? xvalue_p (expr) : lvalue_p (expr))
8231 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8232 /* Don't warn when converting a class object to a non-reference type,
8233 because that's a common way to create a temporary. */
8234 : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8235 warning_at (loc, OPT_Wuseless_cast,
8236 "useless cast to type %q#T", type);
8237 }
8238}
8239
8240/* Warns if the cast ignores cv-qualifiers on TYPE. */
8241static void
8242maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8243 tsubst_flags_t complain)
8244{
8245 if (warn_ignored_qualifiers
8246 && complain & tf_warning
8247 && !CLASS_TYPE_P (type)
8248 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8249 warning_at (loc, OPT_Wignored_qualifiers,
8250 "type qualifiers ignored on cast result type");
8251}
8252
8253/* Convert EXPR (an expression with pointer-to-member type) to TYPE
8254 (another pointer-to-member type in the same hierarchy) and return
8255 the converted expression. If ALLOW_INVERSE_P is permitted, a
8256 pointer-to-derived may be converted to pointer-to-base; otherwise,
8257 only the other direction is permitted. If C_CAST_P is true, this
8258 conversion is taking place as part of a C-style cast. */
8259
8260tree
8261convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8262 bool c_cast_p, tsubst_flags_t complain)
8263{
8264 if (same_type_p (type, TREE_TYPE (expr)))
8265 return expr;
8266
8267 if (TYPE_PTRDATAMEM_P (type))
8268 {
8269 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8270 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8271 tree delta = (get_delta_difference
8272 (obase, nbase,
8273 allow_inverse_p, c_cast_p, complain));
8274
8275 if (delta == error_mark_node)
8276 return error_mark_node;
8277
8278 if (!same_type_p (obase, nbase))
8279 {
8280 if (TREE_CODE (expr) == PTRMEM_CST)
8281 expr = cplus_expand_constant (expr);
8282
8283 tree cond = cp_build_binary_op (location: input_location, code: EQ_EXPR, orig_op0: expr,
8284 orig_op1: build_int_cst (TREE_TYPE (expr), -1),
8285 complain);
8286 tree op1 = build_nop (ptrdiff_type_node, expr);
8287 tree op2 = cp_build_binary_op (location: input_location, code: PLUS_EXPR, orig_op0: op1, orig_op1: delta,
8288 complain);
8289
8290 expr = fold_build3_loc (input_location,
8291 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8292 }
8293
8294 return build_nop (type, expr);
8295 }
8296 else
8297 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8298 allow_inverse_p, c_cast_p, complain);
8299}
8300
8301/* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8302 this static_cast is being attempted as one of the possible casts
8303 allowed by a C-style cast. (In that case, accessibility of base
8304 classes is not considered, and it is OK to cast away
8305 constness.) Return the result of the cast. *VALID_P is set to
8306 indicate whether or not the cast was valid. */
8307
8308static tree
8309build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8310 bool *valid_p, tsubst_flags_t complain)
8311{
8312 tree intype;
8313 tree result;
8314 cp_lvalue_kind clk;
8315
8316 /* Assume the cast is valid. */
8317 *valid_p = true;
8318
8319 intype = unlowered_expr_type (exp: expr);
8320
8321 /* Save casted types in the function's used types hash table. */
8322 used_types_insert (type);
8323
8324 /* A prvalue of non-class type is cv-unqualified. */
8325 if (!CLASS_TYPE_P (type))
8326 type = cv_unqualified (type);
8327
8328 /* [expr.static.cast]
8329
8330 An lvalue of type "cv1 B", where B is a class type, can be cast
8331 to type "reference to cv2 D", where D is a class derived (clause
8332 _class.derived_) from B, if a valid standard conversion from
8333 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8334 same cv-qualification as, or greater cv-qualification than, cv1,
8335 and B is not a virtual base class of D. */
8336 /* We check this case before checking the validity of "TYPE t =
8337 EXPR;" below because for this case:
8338
8339 struct B {};
8340 struct D : public B { D(const B&); };
8341 extern B& b;
8342 void f() { static_cast<const D&>(b); }
8343
8344 we want to avoid constructing a new D. The standard is not
8345 completely clear about this issue, but our interpretation is
8346 consistent with other compilers. */
8347 if (TYPE_REF_P (type)
8348 && CLASS_TYPE_P (TREE_TYPE (type))
8349 && CLASS_TYPE_P (intype)
8350 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8351 && DERIVED_FROM_P (intype, TREE_TYPE (type))
8352 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8353 build_pointer_type (TYPE_MAIN_VARIANT
8354 (TREE_TYPE (type))),
8355 complain)
8356 && (c_cast_p
8357 || at_least_as_qualified_p (TREE_TYPE (type), type2: intype)))
8358 {
8359 tree base;
8360
8361 if (processing_template_decl)
8362 return expr;
8363
8364 /* There is a standard conversion from "D*" to "B*" even if "B"
8365 is ambiguous or inaccessible. If this is really a
8366 static_cast, then we check both for inaccessibility and
8367 ambiguity. However, if this is a static_cast being performed
8368 because the user wrote a C-style cast, then accessibility is
8369 not considered. */
8370 base = lookup_base (TREE_TYPE (type), intype,
8371 c_cast_p ? ba_unique : ba_check,
8372 NULL, complain);
8373 expr = cp_build_addr_expr (arg: expr, complain);
8374
8375 if (sanitize_flags_p (flag: SANITIZE_VPTR))
8376 {
8377 tree ubsan_check
8378 = cp_ubsan_maybe_instrument_downcast (loc, type,
8379 intype, expr);
8380 if (ubsan_check)
8381 expr = ubsan_check;
8382 }
8383
8384 /* Convert from "B*" to "D*". This function will check that "B"
8385 is not a virtual base of "D". Even if we don't have a guarantee
8386 that expr is NULL, if the static_cast is to a reference type,
8387 it is UB if it would be NULL, so omit the non-NULL check. */
8388 expr = build_base_path (MINUS_EXPR, expr, base,
8389 /*nonnull=*/flag_delete_null_pointer_checks,
8390 complain);
8391
8392 /* Convert the pointer to a reference -- but then remember that
8393 there are no expressions with reference type in C++.
8394
8395 We call rvalue so that there's an actual tree code
8396 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8397 is a variable with the same type, the conversion would get folded
8398 away, leaving just the variable and causing lvalue_kind to give
8399 the wrong answer. */
8400 expr = cp_fold_convert (type, expr);
8401
8402 /* When -fsanitize=null, make sure to diagnose reference binding to
8403 NULL even when the reference is converted to pointer later on. */
8404 if (sanitize_flags_p (flag: SANITIZE_NULL)
8405 && TREE_CODE (expr) == COND_EXPR
8406 && TREE_OPERAND (expr, 2)
8407 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8408 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8409 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8410
8411 return convert_from_reference (rvalue (expr));
8412 }
8413
8414 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8415 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8416 if (TYPE_REF_P (type)
8417 && TYPE_REF_IS_RVALUE (type)
8418 && (clk = real_lvalue_p (expr))
8419 && reference_compatible_p (TREE_TYPE (type), intype)
8420 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), type2: intype)))
8421 {
8422 if (processing_template_decl)
8423 return expr;
8424 if (clk == clk_ordinary)
8425 {
8426 /* Handle the (non-bit-field) lvalue case here by casting to
8427 lvalue reference and then changing it to an rvalue reference.
8428 Casting an xvalue to rvalue reference will be handled by the
8429 main code path. */
8430 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8431 result = (perform_direct_initialization_if_possible
8432 (lref, expr, c_cast_p, complain));
8433 result = build1 (NON_LVALUE_EXPR, type, result);
8434 return convert_from_reference (result);
8435 }
8436 else
8437 /* For a bit-field or packed field, bind to a temporary. */
8438 expr = rvalue (expr);
8439 }
8440
8441 /* Resolve overloaded address here rather than once in
8442 implicit_conversion and again in the inverse code below. */
8443 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8444 {
8445 expr = instantiate_type (type, expr, complain);
8446 intype = TREE_TYPE (expr);
8447 }
8448
8449 /* [expr.static.cast]
8450
8451 Any expression can be explicitly converted to type cv void. */
8452 if (VOID_TYPE_P (type))
8453 {
8454 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8455 expr = TREE_OPERAND (expr, 0);
8456 return convert_to_void (expr, ICV_CAST, complain);
8457 }
8458
8459 /* [class.abstract]
8460 An abstract class shall not be used ... as the type of an explicit
8461 conversion. */
8462 if (abstract_virtuals_error (ACU_CAST, type, complain))
8463 return error_mark_node;
8464
8465 /* [expr.static.cast]
8466
8467 An expression e can be explicitly converted to a type T using a
8468 static_cast of the form static_cast<T>(e) if the declaration T
8469 t(e);" is well-formed, for some invented temporary variable
8470 t. */
8471 result = perform_direct_initialization_if_possible (type, expr,
8472 c_cast_p, complain);
8473 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8474 which initialize the first element of the aggregate. We need to handle
8475 the array case specifically. */
8476 if (result == NULL_TREE
8477 && cxx_dialect >= cxx20
8478 && TREE_CODE (type) == ARRAY_TYPE)
8479 {
8480 /* Create { EXPR } and perform direct-initialization from it. */
8481 tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8482 CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8483 CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8484 result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8485 complain);
8486 }
8487 if (result)
8488 {
8489 if (processing_template_decl)
8490 return expr;
8491
8492 result = convert_from_reference (result);
8493
8494 /* [expr.static.cast]
8495
8496 If T is a reference type, the result is an lvalue; otherwise,
8497 the result is an rvalue. */
8498 if (!TYPE_REF_P (type))
8499 {
8500 result = rvalue (result);
8501
8502 if (result == expr && SCALAR_TYPE_P (type))
8503 /* Leave some record of the cast. */
8504 result = build_nop (type, expr);
8505 }
8506 return result;
8507 }
8508
8509 /* [expr.static.cast]
8510
8511 The inverse of any standard conversion sequence (clause _conv_),
8512 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8513 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8514 (_conv.bool_) conversions, can be performed explicitly using
8515 static_cast subject to the restriction that the explicit
8516 conversion does not cast away constness (_expr.const.cast_), and
8517 the following additional rules for specific cases: */
8518 /* For reference, the conversions not excluded are: integral
8519 promotions, floating-point promotion, integral conversions,
8520 floating-point conversions, floating-integral conversions,
8521 pointer conversions, and pointer to member conversions. */
8522 /* DR 128
8523
8524 A value of integral _or enumeration_ type can be explicitly
8525 converted to an enumeration type. */
8526 /* The effect of all that is that any conversion between any two
8527 types which are integral, floating, or enumeration types can be
8528 performed. */
8529 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8530 || SCALAR_FLOAT_TYPE_P (type))
8531 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8532 || SCALAR_FLOAT_TYPE_P (intype)))
8533 {
8534 if (processing_template_decl)
8535 return expr;
8536 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8537 expr = TREE_OPERAND (expr, 0);
8538 /* [expr.static.cast]: "If the value is not a bit-field, the result
8539 refers to the object or the specified base class subobject thereof;
8540 otherwise, the lvalue-to-rvalue conversion is applied to the
8541 bit-field and the resulting prvalue is used as the operand of the
8542 static_cast." There are no prvalue bit-fields; the l-to-r conversion
8543 will give us an object of the underlying type of the bit-field. */
8544 expr = decay_conversion (exp: expr, complain);
8545 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8546 }
8547
8548 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8549 && CLASS_TYPE_P (TREE_TYPE (type))
8550 && CLASS_TYPE_P (TREE_TYPE (intype))
8551 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8552 (TREE_TYPE (intype))),
8553 build_pointer_type (TYPE_MAIN_VARIANT
8554 (TREE_TYPE (type))),
8555 complain))
8556 {
8557 tree base;
8558
8559 if (processing_template_decl)
8560 return expr;
8561
8562 if (!c_cast_p
8563 && check_for_casting_away_constness (loc, src_type: intype, dest_type: type,
8564 cast: STATIC_CAST_EXPR,
8565 complain))
8566 return error_mark_node;
8567 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8568 c_cast_p ? ba_unique : ba_check,
8569 NULL, complain);
8570 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8571 complain);
8572
8573 if (sanitize_flags_p (flag: SANITIZE_VPTR))
8574 {
8575 tree ubsan_check
8576 = cp_ubsan_maybe_instrument_downcast (loc, type,
8577 intype, expr);
8578 if (ubsan_check)
8579 expr = ubsan_check;
8580 }
8581
8582 return cp_fold_convert (type, expr);
8583 }
8584
8585 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8586 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8587 {
8588 tree c1;
8589 tree c2;
8590 tree t1;
8591 tree t2;
8592
8593 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8594 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8595
8596 if (TYPE_PTRDATAMEM_P (type))
8597 {
8598 t1 = (build_ptrmem_type
8599 (c1,
8600 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8601 t2 = (build_ptrmem_type
8602 (c2,
8603 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8604 }
8605 else
8606 {
8607 t1 = intype;
8608 t2 = type;
8609 }
8610 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8611 {
8612 if (!c_cast_p
8613 && check_for_casting_away_constness (loc, src_type: intype, dest_type: type,
8614 cast: STATIC_CAST_EXPR,
8615 complain))
8616 return error_mark_node;
8617 if (processing_template_decl)
8618 return expr;
8619 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8620 c_cast_p, complain);
8621 }
8622 }
8623
8624 /* [expr.static.cast]
8625
8626 An rvalue of type "pointer to cv void" can be explicitly
8627 converted to a pointer to object type. A value of type pointer
8628 to object converted to "pointer to cv void" and back to the
8629 original pointer type will have its original value. */
8630 if (TYPE_PTR_P (intype)
8631 && VOID_TYPE_P (TREE_TYPE (intype))
8632 && TYPE_PTROB_P (type))
8633 {
8634 if (!c_cast_p
8635 && check_for_casting_away_constness (loc, src_type: intype, dest_type: type,
8636 cast: STATIC_CAST_EXPR,
8637 complain))
8638 return error_mark_node;
8639 if (processing_template_decl)
8640 return expr;
8641 return build_nop (type, expr);
8642 }
8643
8644 *valid_p = false;
8645 return error_mark_node;
8646}
8647
8648/* Return an expression representing static_cast<TYPE>(EXPR). */
8649
8650tree
8651build_static_cast (location_t loc, tree type, tree oexpr,
8652 tsubst_flags_t complain)
8653{
8654 tree expr = oexpr;
8655 tree result;
8656 bool valid_p;
8657
8658 if (type == error_mark_node || expr == error_mark_node)
8659 return error_mark_node;
8660
8661 bool dependent = (dependent_type_p (type)
8662 || type_dependent_expression_p (expr));
8663 if (dependent)
8664 {
8665 tmpl:
8666 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8667 /* We don't know if it will or will not have side effects. */
8668 TREE_SIDE_EFFECTS (expr) = 1;
8669 result = convert_from_reference (expr);
8670 protected_set_expr_location (result, loc);
8671 return result;
8672 }
8673
8674 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8675 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8676 if (!TYPE_REF_P (type)
8677 && TREE_CODE (expr) == NOP_EXPR
8678 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8679 expr = TREE_OPERAND (expr, 0);
8680
8681 result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8682 valid_p: &valid_p, complain);
8683 if (valid_p)
8684 {
8685 if (result != error_mark_node)
8686 {
8687 maybe_warn_about_useless_cast (loc, type, expr, complain);
8688 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8689 }
8690 if (processing_template_decl)
8691 goto tmpl;
8692 protected_set_expr_location (result, loc);
8693 return result;
8694 }
8695
8696 if (complain & tf_error)
8697 {
8698 error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8699 TREE_TYPE (expr), type);
8700 if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8701 && CLASS_TYPE_P (TREE_TYPE (type))
8702 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8703 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8704 "class type %qT is incomplete", TREE_TYPE (type));
8705 tree expr_type = TREE_TYPE (expr);
8706 if (TYPE_PTR_P (expr_type))
8707 expr_type = TREE_TYPE (expr_type);
8708 if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8709 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8710 "class type %qT is incomplete", expr_type);
8711 }
8712 return error_mark_node;
8713}
8714
8715/* EXPR is an expression with member function or pointer-to-member
8716 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8717 not permitted by ISO C++, but we accept it in some modes. If we
8718 are not in one of those modes, issue a diagnostic. Return the
8719 converted expression. */
8720
8721tree
8722convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8723{
8724 tree intype;
8725 tree decl;
8726
8727 intype = TREE_TYPE (expr);
8728 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8729 || TREE_CODE (intype) == METHOD_TYPE);
8730
8731 if (!(complain & tf_warning_or_error))
8732 return error_mark_node;
8733
8734 location_t loc = cp_expr_loc_or_input_loc (t: expr);
8735
8736 if (pedantic || warn_pmf2ptr)
8737 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8738 "converting from %qH to %qI", intype, type);
8739
8740 STRIP_ANY_LOCATION_WRAPPER (expr);
8741
8742 if (TREE_CODE (intype) == METHOD_TYPE)
8743 expr = build_addr_func (expr, complain);
8744 else if (TREE_CODE (expr) == PTRMEM_CST)
8745 expr = build_address (PTRMEM_CST_MEMBER (expr));
8746 else
8747 {
8748 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8749 decl = build_address (t: decl);
8750 expr = get_member_function_from_ptrfunc (instance_ptrptr: &decl, function: expr, complain);
8751 }
8752
8753 if (expr == error_mark_node)
8754 return error_mark_node;
8755
8756 expr = build_nop (type, expr);
8757 SET_EXPR_LOCATION (expr, loc);
8758 return expr;
8759}
8760
8761/* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8762 constexpr evaluation knows to reject it. */
8763
8764static tree
8765build_nop_reinterpret (tree type, tree expr)
8766{
8767 tree ret = build_nop (type, expr);
8768 if (ret != expr)
8769 REINTERPRET_CAST_P (ret) = true;
8770 return ret;
8771}
8772
8773/* Return a representation for a reinterpret_cast from EXPR to TYPE.
8774 If C_CAST_P is true, this reinterpret cast is being done as part of
8775 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8776 indicate whether or not reinterpret_cast was valid. */
8777
8778static tree
8779build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8780 bool c_cast_p, bool *valid_p,
8781 tsubst_flags_t complain)
8782{
8783 tree intype;
8784
8785 /* Assume the cast is invalid. */
8786 if (valid_p)
8787 *valid_p = true;
8788
8789 if (type == error_mark_node || error_operand_p (t: expr))
8790 return error_mark_node;
8791
8792 intype = TREE_TYPE (expr);
8793
8794 /* Save casted types in the function's used types hash table. */
8795 used_types_insert (type);
8796
8797 /* A prvalue of non-class type is cv-unqualified. */
8798 if (!CLASS_TYPE_P (type))
8799 type = cv_unqualified (type);
8800
8801 /* [expr.reinterpret.cast]
8802 A glvalue of type T1, designating an object x, can be cast to the type
8803 "reference to T2" if an expression of type "pointer to T1" can be
8804 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8805 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8806 of type "pointer to T1". No temporary is created, no copy is made, and no
8807 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8808 if (TYPE_REF_P (type))
8809 {
8810 if (!glvalue_p (expr))
8811 {
8812 if (complain & tf_error)
8813 error_at (loc, "invalid cast of a prvalue expression of type "
8814 "%qT to type %qT",
8815 intype, type);
8816 return error_mark_node;
8817 }
8818
8819 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8820 "B" are related class types; the reinterpret_cast does not
8821 adjust the pointer. */
8822 if (TYPE_PTR_P (intype)
8823 && (complain & tf_warning)
8824 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8825 COMPARE_BASE | COMPARE_DERIVED)))
8826 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8827 intype, type);
8828
8829 expr = cp_build_addr_expr (arg: expr, complain);
8830
8831 if (warn_strict_aliasing > 2)
8832 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8833
8834 if (expr != error_mark_node)
8835 expr = build_reinterpret_cast_1
8836 (loc, type: build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8837 valid_p, complain);
8838 if (expr != error_mark_node)
8839 /* cp_build_indirect_ref isn't right for rvalue refs. */
8840 expr = convert_from_reference (fold_convert (type, expr));
8841 return expr;
8842 }
8843
8844 /* As a G++ extension, we consider conversions from member
8845 functions, and pointers to member functions to
8846 pointer-to-function and pointer-to-void types. If
8847 -Wno-pmf-conversions has not been specified,
8848 convert_member_func_to_ptr will issue an error message. */
8849 if ((TYPE_PTRMEMFUNC_P (intype)
8850 || TREE_CODE (intype) == METHOD_TYPE)
8851 && TYPE_PTR_P (type)
8852 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
8853 || VOID_TYPE_P (TREE_TYPE (type))))
8854 return convert_member_func_to_ptr (type, expr, complain);
8855
8856 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8857 array-to-pointer, and function-to-pointer conversions are
8858 performed. */
8859 expr = decay_conversion (exp: expr, complain);
8860
8861 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8862 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8863 if (TREE_CODE (expr) == NOP_EXPR
8864 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8865 expr = TREE_OPERAND (expr, 0);
8866
8867 if (error_operand_p (t: expr))
8868 return error_mark_node;
8869
8870 intype = TREE_TYPE (expr);
8871
8872 /* [expr.reinterpret.cast]
8873 A pointer can be converted to any integral type large enough to
8874 hold it. ... A value of type std::nullptr_t can be converted to
8875 an integral type; the conversion has the same meaning and
8876 validity as a conversion of (void*)0 to the integral type. */
8877 if (CP_INTEGRAL_TYPE_P (type)
8878 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8879 {
8880 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
8881 {
8882 if (complain & tf_error)
8883 permerror (loc, "cast from %qH to %qI loses precision",
8884 intype, type);
8885 else
8886 return error_mark_node;
8887 }
8888 if (NULLPTR_TYPE_P (intype))
8889 return build_int_cst (type, 0);
8890 }
8891 /* [expr.reinterpret.cast]
8892 A value of integral or enumeration type can be explicitly
8893 converted to a pointer. */
8894 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
8895 /* OK */
8896 ;
8897 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8898 || TYPE_PTR_OR_PTRMEM_P (type))
8899 && same_type_p (type, intype))
8900 /* DR 799 */
8901 return rvalue (expr);
8902 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
8903 {
8904 if ((complain & tf_warning)
8905 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
8906 TREE_TYPE (intype)))
8907 warning_at (loc, OPT_Wcast_function_type,
8908 "cast between incompatible function types"
8909 " from %qH to %qI", intype, type);
8910 return build_nop_reinterpret (type, expr);
8911 }
8912 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
8913 {
8914 if ((complain & tf_warning)
8915 && !cxx_safe_function_type_cast_p
8916 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
8917 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
8918 warning_at (loc, OPT_Wcast_function_type,
8919 "cast between incompatible pointer to member types"
8920 " from %qH to %qI", intype, type);
8921 return build_nop_reinterpret (type, expr);
8922 }
8923 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8924 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
8925 {
8926 if (!c_cast_p
8927 && check_for_casting_away_constness (loc, src_type: intype, dest_type: type,
8928 cast: REINTERPRET_CAST_EXPR,
8929 complain))
8930 return error_mark_node;
8931 /* Warn about possible alignment problems. */
8932 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8933 && (complain & tf_warning)
8934 && !VOID_TYPE_P (type)
8935 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
8936 && COMPLETE_TYPE_P (TREE_TYPE (type))
8937 && COMPLETE_TYPE_P (TREE_TYPE (intype))
8938 && min_align_of_type (TREE_TYPE (type))
8939 > min_align_of_type (TREE_TYPE (intype)))
8940 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8941 "increases required alignment of target type",
8942 intype, type);
8943
8944 if (warn_strict_aliasing <= 2)
8945 /* strict_aliasing_warning STRIP_NOPs its expr. */
8946 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8947
8948 return build_nop_reinterpret (type, expr);
8949 }
8950 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
8951 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
8952 {
8953 if (complain & tf_warning)
8954 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8955 object pointer type or vice versa is conditionally-supported." */
8956 warning_at (loc, OPT_Wconditionally_supported,
8957 "casting between pointer-to-function and "
8958 "pointer-to-object is conditionally-supported");
8959 return build_nop_reinterpret (type, expr);
8960 }
8961 else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
8962 return convert_to_vector (type, rvalue (expr));
8963 else if (gnu_vector_type_p (type: intype)
8964 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8965 return convert_to_integer_nofold (t: type, x: expr);
8966 else
8967 {
8968 if (valid_p)
8969 *valid_p = false;
8970 if (complain & tf_error)
8971 error_at (loc, "invalid cast from type %qT to type %qT",
8972 intype, type);
8973 return error_mark_node;
8974 }
8975
8976 expr = cp_convert (type, expr, complain);
8977 if (TREE_CODE (expr) == NOP_EXPR)
8978 /* Mark any nop_expr that created as a reintepret_cast. */
8979 REINTERPRET_CAST_P (expr) = true;
8980 return expr;
8981}
8982
8983tree
8984build_reinterpret_cast (location_t loc, tree type, tree expr,
8985 tsubst_flags_t complain)
8986{
8987 tree r;
8988
8989 if (type == error_mark_node || expr == error_mark_node)
8990 return error_mark_node;
8991
8992 if (processing_template_decl)
8993 {
8994 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
8995
8996 if (!TREE_SIDE_EFFECTS (t)
8997 && type_dependent_expression_p (expr))
8998 /* There might turn out to be side effects inside expr. */
8999 TREE_SIDE_EFFECTS (t) = 1;
9000 r = convert_from_reference (t);
9001 protected_set_expr_location (r, loc);
9002 return r;
9003 }
9004
9005 r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9006 /*valid_p=*/NULL, complain);
9007 if (r != error_mark_node)
9008 {
9009 maybe_warn_about_useless_cast (loc, type, expr, complain);
9010 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9011 }
9012 protected_set_expr_location (r, loc);
9013 return r;
9014}
9015
9016/* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9017 return an appropriate expression. Otherwise, return
9018 error_mark_node. If the cast is not valid, and COMPLAIN is true,
9019 then a diagnostic will be issued. If VALID_P is non-NULL, we are
9020 performing a C-style cast, its value upon return will indicate
9021 whether or not the conversion succeeded. */
9022
9023static tree
9024build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9025 tsubst_flags_t complain, bool *valid_p)
9026{
9027 tree src_type;
9028 tree reference_type;
9029
9030 /* Callers are responsible for handling error_mark_node as a
9031 destination type. */
9032 gcc_assert (dst_type != error_mark_node);
9033 /* In a template, callers should be building syntactic
9034 representations of casts, not using this machinery. */
9035 gcc_assert (!processing_template_decl);
9036
9037 /* Assume the conversion is invalid. */
9038 if (valid_p)
9039 *valid_p = false;
9040
9041 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9042 {
9043 if (complain & tf_error)
9044 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9045 "which is not a pointer, reference, "
9046 "nor a pointer-to-data-member type", dst_type);
9047 return error_mark_node;
9048 }
9049
9050 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9051 {
9052 if (complain & tf_error)
9053 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9054 "which is a pointer or reference to a function type",
9055 dst_type);
9056 return error_mark_node;
9057 }
9058
9059 /* A prvalue of non-class type is cv-unqualified. */
9060 dst_type = cv_unqualified (dst_type);
9061
9062 /* Save casted types in the function's used types hash table. */
9063 used_types_insert (dst_type);
9064
9065 src_type = TREE_TYPE (expr);
9066 /* Expressions do not really have reference types. */
9067 if (TYPE_REF_P (src_type))
9068 src_type = TREE_TYPE (src_type);
9069
9070 /* [expr.const.cast]
9071
9072 For two object types T1 and T2, if a pointer to T1 can be explicitly
9073 converted to the type "pointer to T2" using a const_cast, then the
9074 following conversions can also be made:
9075
9076 -- an lvalue of type T1 can be explicitly converted to an lvalue of
9077 type T2 using the cast const_cast<T2&>;
9078
9079 -- a glvalue of type T1 can be explicitly converted to an xvalue of
9080 type T2 using the cast const_cast<T2&&>; and
9081
9082 -- if T1 is a class type, a prvalue of type T1 can be explicitly
9083 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9084
9085 if (TYPE_REF_P (dst_type))
9086 {
9087 reference_type = dst_type;
9088 if (!TYPE_REF_IS_RVALUE (dst_type)
9089 ? lvalue_p (expr)
9090 : obvalue_p (expr))
9091 /* OK. */;
9092 else
9093 {
9094 if (complain & tf_error)
9095 error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9096 "to type %qT",
9097 src_type, dst_type);
9098 return error_mark_node;
9099 }
9100 dst_type = build_pointer_type (TREE_TYPE (dst_type));
9101 src_type = build_pointer_type (src_type);
9102 }
9103 else
9104 {
9105 reference_type = NULL_TREE;
9106 /* If the destination type is not a reference type, the
9107 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9108 conversions are performed. */
9109 src_type = type_decays_to (src_type);
9110 if (src_type == error_mark_node)
9111 return error_mark_node;
9112 }
9113
9114 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9115 {
9116 if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9117 {
9118 if (valid_p)
9119 {
9120 *valid_p = true;
9121 /* This cast is actually a C-style cast. Issue a warning if
9122 the user is making a potentially unsafe cast. */
9123 check_for_casting_away_constness (loc, src_type, dest_type: dst_type,
9124 cast: CAST_EXPR, complain);
9125 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9126 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9127 && (complain & tf_warning)
9128 && min_align_of_type (TREE_TYPE (dst_type))
9129 > min_align_of_type (TREE_TYPE (src_type)))
9130 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9131 "increases required alignment of target type",
9132 src_type, dst_type);
9133 }
9134 if (reference_type)
9135 {
9136 expr = cp_build_addr_expr (arg: expr, complain);
9137 if (expr == error_mark_node)
9138 return error_mark_node;
9139 expr = build_nop (type: reference_type, expr);
9140 return convert_from_reference (expr);
9141 }
9142 else
9143 {
9144 expr = decay_conversion (exp: expr, complain);
9145 if (expr == error_mark_node)
9146 return error_mark_node;
9147
9148 /* build_c_cast puts on a NOP_EXPR to make the result not an
9149 lvalue. Strip such NOP_EXPRs if VALUE is being used in
9150 non-lvalue context. */
9151 if (TREE_CODE (expr) == NOP_EXPR
9152 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9153 expr = TREE_OPERAND (expr, 0);
9154 return build_nop (type: dst_type, expr);
9155 }
9156 }
9157 else if (valid_p
9158 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9159 TREE_TYPE (src_type)))
9160 check_for_casting_away_constness (loc, src_type, dest_type: dst_type,
9161 cast: CAST_EXPR, complain);
9162 }
9163
9164 if (complain & tf_error)
9165 error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9166 src_type, dst_type);
9167 return error_mark_node;
9168}
9169
9170tree
9171build_const_cast (location_t loc, tree type, tree expr,
9172 tsubst_flags_t complain)
9173{
9174 tree r;
9175
9176 if (type == error_mark_node || error_operand_p (t: expr))
9177 return error_mark_node;
9178
9179 if (processing_template_decl)
9180 {
9181 tree t = build_min (CONST_CAST_EXPR, type, expr);
9182
9183 if (!TREE_SIDE_EFFECTS (t)
9184 && type_dependent_expression_p (expr))
9185 /* There might turn out to be side effects inside expr. */
9186 TREE_SIDE_EFFECTS (t) = 1;
9187 r = convert_from_reference (t);
9188 protected_set_expr_location (r, loc);
9189 return r;
9190 }
9191
9192 r = build_const_cast_1 (loc, dst_type: type, expr, complain, /*valid_p=*/NULL);
9193 if (r != error_mark_node)
9194 {
9195 maybe_warn_about_useless_cast (loc, type, expr, complain);
9196 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9197 }
9198 protected_set_expr_location (r, loc);
9199 return r;
9200}
9201
9202/* Like cp_build_c_cast, but for the c-common bits. */
9203
9204tree
9205build_c_cast (location_t loc, tree type, tree expr)
9206{
9207 return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9208}
9209
9210/* Like the "build_c_cast" used for c-common, but using cp_expr to
9211 preserve location information even for tree nodes that don't
9212 support it. */
9213
9214cp_expr
9215build_c_cast (location_t loc, tree type, cp_expr expr)
9216{
9217 cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9218 result.set_location (loc);
9219 return result;
9220}
9221
9222/* Build an expression representing an explicit C-style cast to type
9223 TYPE of expression EXPR. */
9224
9225tree
9226cp_build_c_cast (location_t loc, tree type, tree expr,
9227 tsubst_flags_t complain)
9228{
9229 tree value = expr;
9230 tree result;
9231 bool valid_p;
9232
9233 if (type == error_mark_node || error_operand_p (t: expr))
9234 return error_mark_node;
9235
9236 if (processing_template_decl)
9237 {
9238 tree t = build_min (CAST_EXPR, type,
9239 tree_cons (NULL_TREE, value, NULL_TREE));
9240 /* We don't know if it will or will not have side effects. */
9241 TREE_SIDE_EFFECTS (t) = 1;
9242 return convert_from_reference (t);
9243 }
9244
9245 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9246 'Class') should always be retained, because this information aids
9247 in method lookup. */
9248 if (objc_is_object_ptr (type)
9249 && objc_is_object_ptr (TREE_TYPE (expr)))
9250 return build_nop (type, expr);
9251
9252 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9253 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9254 if (!TYPE_REF_P (type)
9255 && TREE_CODE (value) == NOP_EXPR
9256 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9257 value = TREE_OPERAND (value, 0);
9258
9259 if (TREE_CODE (type) == ARRAY_TYPE)
9260 {
9261 /* Allow casting from T1* to T2[] because Cfront allows it.
9262 NIHCL uses it. It is not valid ISO C++ however. */
9263 if (TYPE_PTR_P (TREE_TYPE (expr)))
9264 {
9265 if (complain & tf_error)
9266 permerror (loc, "ISO C++ forbids casting to an array type %qT",
9267 type);
9268 else
9269 return error_mark_node;
9270 type = build_pointer_type (TREE_TYPE (type));
9271 }
9272 else
9273 {
9274 if (complain & tf_error)
9275 error_at (loc, "ISO C++ forbids casting to an array type %qT",
9276 type);
9277 return error_mark_node;
9278 }
9279 }
9280
9281 if (FUNC_OR_METHOD_TYPE_P (type))
9282 {
9283 if (complain & tf_error)
9284 error_at (loc, "invalid cast to function type %qT", type);
9285 return error_mark_node;
9286 }
9287
9288 if (TYPE_PTR_P (type)
9289 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9290 /* Casting to an integer of smaller size is an error detected elsewhere. */
9291 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9292 /* Don't warn about converting any constant. */
9293 && !TREE_CONSTANT (value))
9294 warning_at (loc, OPT_Wint_to_pointer_cast,
9295 "cast to pointer from integer of different size");
9296
9297 /* A C-style cast can be a const_cast. */
9298 result = build_const_cast_1 (loc, dst_type: type, expr: value, complain: complain & tf_warning,
9299 valid_p: &valid_p);
9300 if (valid_p)
9301 {
9302 if (result != error_mark_node)
9303 {
9304 maybe_warn_about_useless_cast (loc, type, expr: value, complain);
9305 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9306 }
9307 else if (complain & tf_error)
9308 build_const_cast_1 (loc, dst_type: type, expr: value, complain: tf_error, valid_p: &valid_p);
9309 return result;
9310 }
9311
9312 /* Or a static cast. */
9313 result = build_static_cast_1 (loc, type, expr: value, /*c_cast_p=*/true,
9314 valid_p: &valid_p, complain);
9315 /* Or a reinterpret_cast. */
9316 if (!valid_p)
9317 result = build_reinterpret_cast_1 (loc, type, expr: value, /*c_cast_p=*/true,
9318 valid_p: &valid_p, complain);
9319 /* The static_cast or reinterpret_cast may be followed by a
9320 const_cast. */
9321 if (valid_p
9322 /* A valid cast may result in errors if, for example, a
9323 conversion to an ambiguous base class is required. */
9324 && !error_operand_p (t: result))
9325 {
9326 tree result_type;
9327
9328 maybe_warn_about_useless_cast (loc, type, expr: value, complain);
9329 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9330
9331 /* Non-class rvalues always have cv-unqualified type. */
9332 if (!CLASS_TYPE_P (type))
9333 type = TYPE_MAIN_VARIANT (type);
9334 result_type = TREE_TYPE (result);
9335 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9336 result_type = TYPE_MAIN_VARIANT (result_type);
9337 /* If the type of RESULT does not match TYPE, perform a
9338 const_cast to make it match. If the static_cast or
9339 reinterpret_cast succeeded, we will differ by at most
9340 cv-qualification, so the follow-on const_cast is guaranteed
9341 to succeed. */
9342 if (!same_type_p (non_reference (type), non_reference (result_type)))
9343 {
9344 result = build_const_cast_1 (loc, dst_type: type, expr: result, complain: tf_none, valid_p: &valid_p);
9345 gcc_assert (valid_p);
9346 }
9347 return result;
9348 }
9349
9350 return error_mark_node;
9351}
9352
9353/* Warn when a value is moved to itself with std::move. LHS is the target,
9354 RHS may be the std::move call, and LOC is the location of the whole
9355 assignment. */
9356
9357static void
9358maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9359{
9360 if (!warn_self_move)
9361 return;
9362
9363 /* C++98 doesn't know move. */
9364 if (cxx_dialect < cxx11)
9365 return;
9366
9367 if (processing_template_decl)
9368 return;
9369
9370 if (!REFERENCE_REF_P (rhs)
9371 || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9372 return;
9373 tree fn = TREE_OPERAND (rhs, 0);
9374 if (!is_std_move_p (fn))
9375 return;
9376
9377 /* Just a little helper to strip * and various NOPs. */
9378 auto extract_op = [] (tree &op) {
9379 STRIP_NOPS (op);
9380 while (INDIRECT_REF_P (op))
9381 op = TREE_OPERAND (op, 0);
9382 op = maybe_undo_parenthesized_ref (op);
9383 STRIP_ANY_LOCATION_WRAPPER (op);
9384 };
9385
9386 tree arg = CALL_EXPR_ARG (fn, 0);
9387 extract_op (arg);
9388 if (TREE_CODE (arg) == ADDR_EXPR)
9389 arg = TREE_OPERAND (arg, 0);
9390 tree type = TREE_TYPE (lhs);
9391 tree orig_lhs = lhs;
9392 extract_op (lhs);
9393 if (cp_tree_equal (lhs, arg))
9394 {
9395 auto_diagnostic_group d;
9396 if (warning_at (loc, OPT_Wself_move,
9397 "moving %qE of type %qT to itself", orig_lhs, type))
9398 inform (loc, "remove %<std::move%> call");
9399 }
9400}
9401
9402/* For use from the C common bits. */
9403tree
9404build_modify_expr (location_t location,
9405 tree lhs, tree /*lhs_origtype*/,
9406 enum tree_code modifycode,
9407 location_t /*rhs_location*/, tree rhs,
9408 tree /*rhs_origtype*/)
9409{
9410 return cp_build_modify_expr (location, lhs, modifycode, rhs,
9411 tf_warning_or_error);
9412}
9413
9414/* Build an assignment expression of lvalue LHS from value RHS.
9415 MODIFYCODE is the code for a binary operator that we use
9416 to combine the old value of LHS with RHS to get the new value.
9417 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9418
9419 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9420
9421tree
9422cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9423 tree rhs, tsubst_flags_t complain)
9424{
9425 lhs = mark_lvalue_use_nonread (lhs);
9426
9427 tree result = NULL_TREE;
9428 tree newrhs = rhs;
9429 tree lhstype = TREE_TYPE (lhs);
9430 tree olhs = lhs;
9431 tree olhstype = lhstype;
9432 bool plain_assign = (modifycode == NOP_EXPR);
9433 bool compound_side_effects_p = false;
9434 tree preeval = NULL_TREE;
9435
9436 /* Avoid duplicate error messages from operands that had errors. */
9437 if (error_operand_p (t: lhs) || error_operand_p (t: rhs))
9438 return error_mark_node;
9439
9440 while (TREE_CODE (lhs) == COMPOUND_EXPR)
9441 {
9442 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9443 compound_side_effects_p = true;
9444 lhs = TREE_OPERAND (lhs, 1);
9445 }
9446
9447 /* Handle control structure constructs used as "lvalues". Note that we
9448 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9449 switch (TREE_CODE (lhs))
9450 {
9451 /* Handle --foo = 5; as these are valid constructs in C++. */
9452 case PREDECREMENT_EXPR:
9453 case PREINCREMENT_EXPR:
9454 if (compound_side_effects_p)
9455 newrhs = rhs = stabilize_expr (rhs, &preeval);
9456 lhs = genericize_compound_lvalue (lvalue: lhs);
9457 maybe_add_compound:
9458 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9459 and looked through the COMPOUND_EXPRs, readd them now around
9460 the resulting lhs. */
9461 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9462 {
9463 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9464 tree *ptr = &TREE_OPERAND (lhs, 1);
9465 for (olhs = TREE_OPERAND (olhs, 1);
9466 TREE_CODE (olhs) == COMPOUND_EXPR;
9467 olhs = TREE_OPERAND (olhs, 1))
9468 {
9469 *ptr = build2 (COMPOUND_EXPR, lhstype,
9470 TREE_OPERAND (olhs, 0), *ptr);
9471 ptr = &TREE_OPERAND (*ptr, 1);
9472 }
9473 }
9474 break;
9475
9476 case MODIFY_EXPR:
9477 if (compound_side_effects_p)
9478 newrhs = rhs = stabilize_expr (rhs, &preeval);
9479 lhs = genericize_compound_lvalue (lvalue: lhs);
9480 goto maybe_add_compound;
9481
9482 case MIN_EXPR:
9483 case MAX_EXPR:
9484 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9485 when neither operand has side-effects. */
9486 if (!lvalue_or_else (lhs, lv_assign, complain))
9487 return error_mark_node;
9488
9489 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9490 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9491
9492 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9493 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9494 boolean_type_node,
9495 TREE_OPERAND (lhs, 0),
9496 TREE_OPERAND (lhs, 1)),
9497 TREE_OPERAND (lhs, 0),
9498 TREE_OPERAND (lhs, 1));
9499 gcc_fallthrough ();
9500
9501 /* Handle (a ? b : c) used as an "lvalue". */
9502 case COND_EXPR:
9503 {
9504 /* Produce (a ? (b = rhs) : (c = rhs))
9505 except that the RHS goes through a save-expr
9506 so the code to compute it is only emitted once. */
9507 if (VOID_TYPE_P (TREE_TYPE (rhs)))
9508 {
9509 if (complain & tf_error)
9510 error_at (cp_expr_loc_or_loc (t: rhs, or_loc: loc),
9511 "void value not ignored as it ought to be");
9512 return error_mark_node;
9513 }
9514
9515 rhs = stabilize_expr (rhs, &preeval);
9516
9517 /* Check this here to avoid odd errors when trying to convert
9518 a throw to the type of the COND_EXPR. */
9519 if (!lvalue_or_else (lhs, lv_assign, complain))
9520 return error_mark_node;
9521
9522 tree op1 = TREE_OPERAND (lhs, 1);
9523 if (TREE_CODE (op1) != THROW_EXPR)
9524 op1 = cp_build_modify_expr (loc, lhs: op1, modifycode, rhs, complain);
9525 /* When sanitizing undefined behavior, even when rhs doesn't need
9526 stabilization at this point, the sanitization might add extra
9527 SAVE_EXPRs in there and so make sure there is no tree sharing
9528 in the rhs, otherwise those SAVE_EXPRs will have initialization
9529 only in one of the two branches. */
9530 if (sanitize_flags_p (flag: SANITIZE_UNDEFINED
9531 | SANITIZE_UNDEFINED_NONDEFAULT))
9532 rhs = unshare_expr (rhs);
9533 tree op2 = TREE_OPERAND (lhs, 2);
9534 if (TREE_CODE (op2) != THROW_EXPR)
9535 op2 = cp_build_modify_expr (loc, lhs: op2, modifycode, rhs, complain);
9536 tree cond = build_conditional_expr (input_location,
9537 TREE_OPERAND (lhs, 0), op1, op2,
9538 complain);
9539
9540 if (cond == error_mark_node)
9541 return cond;
9542 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9543 and looked through the COMPOUND_EXPRs, readd them now around
9544 the resulting cond before adding the preevaluated rhs. */
9545 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9546 {
9547 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9548 TREE_OPERAND (olhs, 0), cond);
9549 tree *ptr = &TREE_OPERAND (cond, 1);
9550 for (olhs = TREE_OPERAND (olhs, 1);
9551 TREE_CODE (olhs) == COMPOUND_EXPR;
9552 olhs = TREE_OPERAND (olhs, 1))
9553 {
9554 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9555 TREE_OPERAND (olhs, 0), *ptr);
9556 ptr = &TREE_OPERAND (*ptr, 1);
9557 }
9558 }
9559 /* Make sure the code to compute the rhs comes out
9560 before the split. */
9561 result = cond;
9562 goto ret;
9563 }
9564
9565 default:
9566 lhs = olhs;
9567 break;
9568 }
9569
9570 if (modifycode == INIT_EXPR)
9571 {
9572 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9573 /* Do the default thing. */;
9574 else if (TREE_CODE (rhs) == CONSTRUCTOR)
9575 {
9576 /* Compound literal. */
9577 if (! same_type_p (TREE_TYPE (rhs), lhstype))
9578 /* Call convert to generate an error; see PR 11063. */
9579 rhs = convert (lhstype, rhs);
9580 result = cp_build_init_expr (t: lhs, i: rhs);
9581 TREE_SIDE_EFFECTS (result) = 1;
9582 goto ret;
9583 }
9584 else if (! MAYBE_CLASS_TYPE_P (lhstype))
9585 /* Do the default thing. */;
9586 else
9587 {
9588 releasing_vec rhs_vec = make_tree_vector_single (rhs);
9589 result = build_special_member_call (lhs, complete_ctor_identifier,
9590 &rhs_vec, lhstype, LOOKUP_NORMAL,
9591 complain);
9592 if (result == NULL_TREE)
9593 return error_mark_node;
9594 goto ret;
9595 }
9596 }
9597 else
9598 {
9599 lhs = require_complete_type (value: lhs, complain);
9600 if (lhs == error_mark_node)
9601 return error_mark_node;
9602
9603 if (modifycode == NOP_EXPR)
9604 {
9605 maybe_warn_self_move (loc, lhs, rhs);
9606
9607 if (c_dialect_objc ())
9608 {
9609 result = objc_maybe_build_modify_expr (lhs, rhs);
9610 if (result)
9611 goto ret;
9612 }
9613
9614 /* `operator=' is not an inheritable operator. */
9615 if (! MAYBE_CLASS_TYPE_P (lhstype))
9616 /* Do the default thing. */;
9617 else
9618 {
9619 result = build_new_op (input_location, MODIFY_EXPR,
9620 LOOKUP_NORMAL, lhs, rhs,
9621 make_node (NOP_EXPR), NULL_TREE,
9622 /*overload=*/NULL, complain);
9623 if (result == NULL_TREE)
9624 return error_mark_node;
9625 goto ret;
9626 }
9627 lhstype = olhstype;
9628 }
9629 else
9630 {
9631 tree init = NULL_TREE;
9632
9633 /* A binary op has been requested. Combine the old LHS
9634 value with the RHS producing the value we should actually
9635 store into the LHS. */
9636 gcc_assert (!((TYPE_REF_P (lhstype)
9637 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9638 || MAYBE_CLASS_TYPE_P (lhstype)));
9639
9640 /* Preevaluate the RHS to make sure its evaluation is complete
9641 before the lvalue-to-rvalue conversion of the LHS:
9642
9643 [expr.ass] With respect to an indeterminately-sequenced
9644 function call, the operation of a compound assignment is a
9645 single evaluation. [ Note: Therefore, a function call shall
9646 not intervene between the lvalue-to-rvalue conversion and the
9647 side effect associated with any single compound assignment
9648 operator. -- end note ] */
9649 lhs = cp_stabilize_reference (lhs);
9650 rhs = decay_conversion (exp: rhs, complain);
9651 if (rhs == error_mark_node)
9652 return error_mark_node;
9653 rhs = stabilize_expr (rhs, &init);
9654 newrhs = cp_build_binary_op (location: loc, code: modifycode, orig_op0: lhs, orig_op1: rhs, complain);
9655 if (newrhs == error_mark_node)
9656 {
9657 if (complain & tf_error)
9658 inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9659 modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9660 return error_mark_node;
9661 }
9662
9663 if (init)
9664 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9665
9666 /* Now it looks like a plain assignment. */
9667 modifycode = NOP_EXPR;
9668 if (c_dialect_objc ())
9669 {
9670 result = objc_maybe_build_modify_expr (lhs, newrhs);
9671 if (result)
9672 goto ret;
9673 }
9674 }
9675 gcc_assert (!TYPE_REF_P (lhstype));
9676 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9677 }
9678
9679 /* The left-hand side must be an lvalue. */
9680 if (!lvalue_or_else (lhs, lv_assign, complain))
9681 return error_mark_node;
9682
9683 /* Warn about modifying something that is `const'. Don't warn if
9684 this is initialization. */
9685 if (modifycode != INIT_EXPR
9686 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9687 /* Functions are not modifiable, even though they are
9688 lvalues. */
9689 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9690 /* If it's an aggregate and any field is const, then it is
9691 effectively const. */
9692 || (CLASS_TYPE_P (lhstype)
9693 && C_TYPE_FIELDS_READONLY (lhstype))))
9694 {
9695 if (complain & tf_error)
9696 cxx_readonly_error (loc, lhs, lv_assign);
9697 return error_mark_node;
9698 }
9699
9700 /* If storing into a structure or union member, it may have been given a
9701 lowered bitfield type. We need to convert to the declared type first,
9702 so retrieve it now. */
9703
9704 olhstype = unlowered_expr_type (exp: lhs);
9705
9706 /* Convert new value to destination type. */
9707
9708 if (TREE_CODE (lhstype) == ARRAY_TYPE)
9709 {
9710 int from_array;
9711
9712 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9713 {
9714 if (modifycode != INIT_EXPR)
9715 {
9716 if (complain & tf_error)
9717 error_at (loc,
9718 "assigning to an array from an initializer list");
9719 return error_mark_node;
9720 }
9721 if (check_array_initializer (lhs, lhstype, newrhs))
9722 return error_mark_node;
9723 newrhs = digest_init (lhstype, newrhs, complain);
9724 if (newrhs == error_mark_node)
9725 return error_mark_node;
9726 }
9727
9728 /* C++11 8.5/17: "If the destination type is an array of characters,
9729 an array of char16_t, an array of char32_t, or an array of wchar_t,
9730 and the initializer is a string literal...". */
9731 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9732 == STRING_CST)
9733 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9734 && modifycode == INIT_EXPR)
9735 {
9736 newrhs = digest_init (lhstype, newrhs, complain);
9737 if (newrhs == error_mark_node)
9738 return error_mark_node;
9739 }
9740
9741 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9742 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9743 {
9744 if (complain & tf_error)
9745 error_at (loc, "incompatible types in assignment of %qT to %qT",
9746 TREE_TYPE (rhs), lhstype);
9747 return error_mark_node;
9748 }
9749
9750 /* Allow array assignment in compiler-generated code. */
9751 else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9752 /* OK, used by coroutines (co-await-initlist1.C). */;
9753 else if (!current_function_decl
9754 || !DECL_DEFAULTED_FN (current_function_decl))
9755 {
9756 /* This routine is used for both initialization and assignment.
9757 Make sure the diagnostic message differentiates the context. */
9758 if (complain & tf_error)
9759 {
9760 if (modifycode == INIT_EXPR)
9761 error_at (loc, "array used as initializer");
9762 else
9763 error_at (loc, "invalid array assignment");
9764 }
9765 return error_mark_node;
9766 }
9767
9768 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9769 ? 1 + (modifycode != INIT_EXPR): 0;
9770 result = build_vec_init (lhs, NULL_TREE, newrhs,
9771 /*explicit_value_init_p=*/false,
9772 from_array, complain);
9773 goto ret;
9774 }
9775
9776 if (modifycode == INIT_EXPR)
9777 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9778 LOOKUP_ONLYCONVERTING. */
9779 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9780 ICR_INIT, NULL_TREE, 0,
9781 complain | tf_no_cleanup);
9782 else
9783 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9784 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9785
9786 if (!same_type_p (lhstype, olhstype))
9787 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9788
9789 if (modifycode != INIT_EXPR)
9790 {
9791 if (TREE_CODE (newrhs) == CALL_EXPR
9792 && TYPE_NEEDS_CONSTRUCTING (lhstype))
9793 newrhs = build_cplus_new (lhstype, newrhs, complain);
9794
9795 /* Can't initialize directly from a TARGET_EXPR, since that would
9796 cause the lhs to be constructed twice, and possibly result in
9797 accidental self-initialization. So we force the TARGET_EXPR to be
9798 expanded without a target. */
9799 if (TREE_CODE (newrhs) == TARGET_EXPR)
9800 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9801 TREE_OPERAND (newrhs, 0));
9802 }
9803
9804 if (newrhs == error_mark_node)
9805 return error_mark_node;
9806
9807 if (c_dialect_objc () && flag_objc_gc)
9808 {
9809 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9810
9811 if (result)
9812 goto ret;
9813 }
9814
9815 result = build2_loc (loc, code: modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9816 type: lhstype, arg0: lhs, arg1: newrhs);
9817 if (modifycode == INIT_EXPR)
9818 set_target_expr_eliding (newrhs);
9819
9820 TREE_SIDE_EFFECTS (result) = 1;
9821 if (!plain_assign)
9822 suppress_warning (result, OPT_Wparentheses);
9823
9824 ret:
9825 if (preeval)
9826 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
9827 return result;
9828}
9829
9830cp_expr
9831build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9832 tree rhs, tree lookups, tsubst_flags_t complain)
9833{
9834 tree orig_lhs = lhs;
9835 tree orig_rhs = rhs;
9836 tree overload = NULL_TREE;
9837
9838 if (lhs == error_mark_node || rhs == error_mark_node)
9839 return cp_expr (error_mark_node, loc);
9840
9841 tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
9842
9843 if (processing_template_decl)
9844 {
9845 if (type_dependent_expression_p (lhs)
9846 || type_dependent_expression_p (rhs))
9847 {
9848 tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
9849 if (modifycode != NOP_EXPR)
9850 TREE_TYPE (rval)
9851 = build_dependent_operator_type (lookups, code: modifycode, is_assign: true);
9852 return rval;
9853 }
9854 }
9855
9856 tree rval;
9857 if (modifycode == NOP_EXPR)
9858 rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
9859 else
9860 rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
9861 lhs, rhs, op, lookups, &overload, complain);
9862 if (rval == error_mark_node)
9863 return error_mark_node;
9864 if (processing_template_decl)
9865 {
9866 if (overload != NULL_TREE)
9867 return (build_min_non_dep_op_overload
9868 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
9869
9870 return (build_min_non_dep
9871 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
9872 }
9873 return rval;
9874}
9875
9876/* Helper function for get_delta_difference which assumes FROM is a base
9877 class of TO. Returns a delta for the conversion of pointer-to-member
9878 of FROM to pointer-to-member of TO. If the conversion is invalid and
9879 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9880 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9881 If C_CAST_P is true, this conversion is taking place as part of a
9882 C-style cast. */
9883
9884static tree
9885get_delta_difference_1 (tree from, tree to, bool c_cast_p,
9886 tsubst_flags_t complain)
9887{
9888 tree binfo;
9889 base_kind kind;
9890
9891 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
9892 &kind, complain);
9893
9894 if (binfo == error_mark_node)
9895 {
9896 if (!(complain & tf_error))
9897 return error_mark_node;
9898
9899 inform (input_location, " in pointer to member function conversion");
9900 return size_zero_node;
9901 }
9902 else if (binfo)
9903 {
9904 if (kind != bk_via_virtual)
9905 return BINFO_OFFSET (binfo);
9906 else
9907 /* FROM is a virtual base class of TO. Issue an error or warning
9908 depending on whether or not this is a reinterpret cast. */
9909 {
9910 if (!(complain & tf_error))
9911 return error_mark_node;
9912
9913 error ("pointer to member conversion via virtual base %qT",
9914 BINFO_TYPE (binfo_from_vbase (binfo)));
9915
9916 return size_zero_node;
9917 }
9918 }
9919 else
9920 return NULL_TREE;
9921}
9922
9923/* Get difference in deltas for different pointer to member function
9924 types. If the conversion is invalid and tf_error is not set in
9925 COMPLAIN, returns error_mark_node, otherwise returns an integer
9926 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9927 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9928 conversions as well. If C_CAST_P is true this conversion is taking
9929 place as part of a C-style cast.
9930
9931 Note that the naming of FROM and TO is kind of backwards; the return
9932 value is what we add to a TO in order to get a FROM. They are named
9933 this way because we call this function to find out how to convert from
9934 a pointer to member of FROM to a pointer to member of TO. */
9935
9936static tree
9937get_delta_difference (tree from, tree to,
9938 bool allow_inverse_p,
9939 bool c_cast_p, tsubst_flags_t complain)
9940{
9941 tree result;
9942
9943 if (same_type_ignoring_top_level_qualifiers_p (type1: from, type2: to))
9944 /* Pointer to member of incomplete class is permitted*/
9945 result = size_zero_node;
9946 else
9947 result = get_delta_difference_1 (from, to, c_cast_p, complain);
9948
9949 if (result == error_mark_node)
9950 return error_mark_node;
9951
9952 if (!result)
9953 {
9954 if (!allow_inverse_p)
9955 {
9956 if (!(complain & tf_error))
9957 return error_mark_node;
9958
9959 error_not_base_type (from, to);
9960 inform (input_location, " in pointer to member conversion");
9961 result = size_zero_node;
9962 }
9963 else
9964 {
9965 result = get_delta_difference_1 (from: to, to: from, c_cast_p, complain);
9966
9967 if (result == error_mark_node)
9968 return error_mark_node;
9969
9970 if (result)
9971 result = size_diffop_loc (input_location,
9972 size_zero_node, result);
9973 else
9974 {
9975 if (!(complain & tf_error))
9976 return error_mark_node;
9977
9978 error_not_base_type (from, to);
9979 inform (input_location, " in pointer to member conversion");
9980 result = size_zero_node;
9981 }
9982 }
9983 }
9984
9985 return convert_to_integer (ptrdiff_type_node, result);
9986}
9987
9988/* Return a constructor for the pointer-to-member-function TYPE using
9989 the other components as specified. */
9990
9991tree
9992build_ptrmemfunc1 (tree type, tree delta, tree pfn)
9993{
9994 tree u = NULL_TREE;
9995 tree delta_field;
9996 tree pfn_field;
9997 vec<constructor_elt, va_gc> *v;
9998
9999 /* Pull the FIELD_DECLs out of the type. */
10000 pfn_field = TYPE_FIELDS (type);
10001 delta_field = DECL_CHAIN (pfn_field);
10002
10003 /* Make sure DELTA has the type we want. */
10004 delta = convert_and_check (input_location, delta_type_node, delta);
10005
10006 /* Convert to the correct target type if necessary. */
10007 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10008
10009 /* Finish creating the initializer. */
10010 vec_alloc (v, nelems: 2);
10011 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10012 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10013 u = build_constructor (type, v);
10014 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10015 TREE_STATIC (u) = (TREE_CONSTANT (u)
10016 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10017 != NULL_TREE)
10018 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10019 != NULL_TREE));
10020 return u;
10021}
10022
10023/* Build a constructor for a pointer to member function. It can be
10024 used to initialize global variables, local variable, or used
10025 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10026 want to be.
10027
10028 If FORCE is nonzero, then force this conversion, even if
10029 we would rather not do it. Usually set when using an explicit
10030 cast. A C-style cast is being processed iff C_CAST_P is true.
10031
10032 Return error_mark_node, if something goes wrong. */
10033
10034tree
10035build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10036 tsubst_flags_t complain)
10037{
10038 tree fn;
10039 tree pfn_type;
10040 tree to_type;
10041
10042 if (error_operand_p (t: pfn))
10043 return error_mark_node;
10044
10045 pfn_type = TREE_TYPE (pfn);
10046 to_type = build_ptrmemfunc_type (type);
10047
10048 /* Handle multiple conversions of pointer to member functions. */
10049 if (TYPE_PTRMEMFUNC_P (pfn_type))
10050 {
10051 tree delta = NULL_TREE;
10052 tree npfn = NULL_TREE;
10053 tree n;
10054
10055 if (!force
10056 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10057 LOOKUP_NORMAL, complain))
10058 {
10059 if (complain & tf_error)
10060 error ("invalid conversion to type %qT from type %qT",
10061 to_type, pfn_type);
10062 else
10063 return error_mark_node;
10064 }
10065
10066 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10067 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10068 allow_inverse_p: force,
10069 c_cast_p, complain);
10070 if (n == error_mark_node)
10071 return error_mark_node;
10072
10073 STRIP_ANY_LOCATION_WRAPPER (pfn);
10074
10075 /* We don't have to do any conversion to convert a
10076 pointer-to-member to its own type. But, we don't want to
10077 just return a PTRMEM_CST if there's an explicit cast; that
10078 cast should make the expression an invalid template argument. */
10079 if (TREE_CODE (pfn) != PTRMEM_CST
10080 && same_type_p (to_type, pfn_type))
10081 return pfn;
10082
10083 if (TREE_SIDE_EFFECTS (pfn))
10084 pfn = save_expr (pfn);
10085
10086 /* Obtain the function pointer and the current DELTA. */
10087 if (TREE_CODE (pfn) == PTRMEM_CST)
10088 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10089 else
10090 {
10091 npfn = build_ptrmemfunc_access_expr (ptrmem: pfn, pfn_identifier);
10092 delta = build_ptrmemfunc_access_expr (ptrmem: pfn, delta_identifier);
10093 }
10094
10095 /* Just adjust the DELTA field. */
10096 gcc_assert (same_type_ignoring_top_level_qualifiers_p
10097 (TREE_TYPE (delta), ptrdiff_type_node));
10098 if (!integer_zerop (n))
10099 {
10100 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10101 n = cp_build_binary_op (location: input_location,
10102 code: LSHIFT_EXPR, orig_op0: n, integer_one_node,
10103 complain);
10104 delta = cp_build_binary_op (location: input_location,
10105 code: PLUS_EXPR, orig_op0: delta, orig_op1: n, complain);
10106 }
10107 return build_ptrmemfunc1 (type: to_type, delta, pfn: npfn);
10108 }
10109
10110 /* Handle null pointer to member function conversions. */
10111 if (null_ptr_cst_p (pfn))
10112 {
10113 pfn = cp_build_c_cast (loc: input_location,
10114 TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10115 expr: pfn, complain);
10116 return build_ptrmemfunc1 (type: to_type,
10117 integer_zero_node,
10118 pfn);
10119 }
10120
10121 if (type_unknown_p (expr: pfn))
10122 return instantiate_type (type, pfn, complain);
10123
10124 fn = TREE_OPERAND (pfn, 0);
10125 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10126 /* In a template, we will have preserved the
10127 OFFSET_REF. */
10128 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10129 return make_ptrmem_cst (to_type, fn);
10130}
10131
10132/* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10133 given by CST.
10134
10135 ??? There is no consistency as to the types returned for the above
10136 values. Some code acts as if it were a sizetype and some as if it were
10137 integer_type_node. */
10138
10139void
10140expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10141{
10142 tree type = TREE_TYPE (cst);
10143 tree fn = PTRMEM_CST_MEMBER (cst);
10144 tree ptr_class, fn_class;
10145
10146 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10147
10148 /* The class that the function belongs to. */
10149 fn_class = DECL_CONTEXT (fn);
10150
10151 /* The class that we're creating a pointer to member of. */
10152 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10153
10154 /* First, calculate the adjustment to the function's class. */
10155 *delta = get_delta_difference (from: fn_class, to: ptr_class, /*force=*/allow_inverse_p: 0,
10156 /*c_cast_p=*/0, complain: tf_warning_or_error);
10157
10158 if (!DECL_VIRTUAL_P (fn))
10159 {
10160 tree t = build_addr_func (fn, tf_warning_or_error);
10161 if (TREE_CODE (t) == ADDR_EXPR)
10162 SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10163 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10164 }
10165 else
10166 {
10167 /* If we're dealing with a virtual function, we have to adjust 'this'
10168 again, to point to the base which provides the vtable entry for
10169 fn; the call will do the opposite adjustment. */
10170 tree orig_class = DECL_CONTEXT (fn);
10171 tree binfo = binfo_or_else (orig_class, fn_class);
10172 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10173 *delta, BINFO_OFFSET (binfo));
10174
10175 /* We set PFN to the vtable offset at which the function can be
10176 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10177 case delta is shifted left, and then incremented). */
10178 *pfn = DECL_VINDEX (fn);
10179 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10180 TYPE_SIZE_UNIT (vtable_entry_type));
10181
10182 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10183 {
10184 case ptrmemfunc_vbit_in_pfn:
10185 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10186 integer_one_node);
10187 break;
10188
10189 case ptrmemfunc_vbit_in_delta:
10190 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10191 *delta, integer_one_node);
10192 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10193 *delta, integer_one_node);
10194 break;
10195
10196 default:
10197 gcc_unreachable ();
10198 }
10199
10200 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10201 }
10202}
10203
10204/* Return an expression for PFN from the pointer-to-member function
10205 given by T. */
10206
10207static tree
10208pfn_from_ptrmemfunc (tree t)
10209{
10210 if (TREE_CODE (t) == PTRMEM_CST)
10211 {
10212 tree delta;
10213 tree pfn;
10214
10215 expand_ptrmemfunc_cst (cst: t, delta: &delta, pfn: &pfn);
10216 if (pfn)
10217 return pfn;
10218 }
10219
10220 return build_ptrmemfunc_access_expr (ptrmem: t, pfn_identifier);
10221}
10222
10223/* Return an expression for DELTA from the pointer-to-member function
10224 given by T. */
10225
10226static tree
10227delta_from_ptrmemfunc (tree t)
10228{
10229 if (TREE_CODE (t) == PTRMEM_CST)
10230 {
10231 tree delta;
10232 tree pfn;
10233
10234 expand_ptrmemfunc_cst (cst: t, delta: &delta, pfn: &pfn);
10235 if (delta)
10236 return delta;
10237 }
10238
10239 return build_ptrmemfunc_access_expr (ptrmem: t, delta_identifier);
10240}
10241
10242/* Convert value RHS to type TYPE as preparation for an assignment to
10243 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10244 implicit conversion is. If FNDECL is non-NULL, we are doing the
10245 conversion in order to pass the PARMNUMth argument of FNDECL.
10246 If FNDECL is NULL, we are doing the conversion in function pointer
10247 argument passing, conversion in initialization, etc. */
10248
10249static tree
10250convert_for_assignment (tree type, tree rhs,
10251 impl_conv_rhs errtype, tree fndecl, int parmnum,
10252 tsubst_flags_t complain, int flags)
10253{
10254 tree rhstype;
10255 enum tree_code coder;
10256
10257 location_t rhs_loc = cp_expr_loc_or_input_loc (t: rhs);
10258 bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10259 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10260 but preserve location wrappers. */
10261 if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10262 && !location_wrapper_p (exp: rhs))
10263 rhs = TREE_OPERAND (rhs, 0);
10264
10265 /* Handle [dcl.init.list] direct-list-initialization from
10266 single element of enumeration with a fixed underlying type. */
10267 if (is_direct_enum_init (type, rhs))
10268 {
10269 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10270 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10271 {
10272 warning_sentinel w (warn_useless_cast);
10273 warning_sentinel w2 (warn_ignored_qualifiers);
10274 rhs = cp_build_c_cast (loc: rhs_loc, type, expr: elt, complain);
10275 }
10276 else
10277 rhs = error_mark_node;
10278 }
10279
10280 rhstype = TREE_TYPE (rhs);
10281 coder = TREE_CODE (rhstype);
10282
10283 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10284 && vector_types_convertible_p (t1: type, t2: rhstype, emit_lax_note: true))
10285 {
10286 rhs = mark_rvalue_use (rhs);
10287 return convert (type, rhs);
10288 }
10289
10290 if (rhs == error_mark_node || rhstype == error_mark_node)
10291 return error_mark_node;
10292 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10293 return error_mark_node;
10294
10295 /* The RHS of an assignment cannot have void type. */
10296 if (coder == VOID_TYPE)
10297 {
10298 if (complain & tf_error)
10299 error_at (rhs_loc, "void value not ignored as it ought to be");
10300 return error_mark_node;
10301 }
10302
10303 if (c_dialect_objc ())
10304 {
10305 int parmno;
10306 tree selector;
10307 tree rname = fndecl;
10308
10309 switch (errtype)
10310 {
10311 case ICR_ASSIGN:
10312 parmno = -1;
10313 break;
10314 case ICR_INIT:
10315 parmno = -2;
10316 break;
10317 default:
10318 selector = objc_message_selector ();
10319 parmno = parmnum;
10320 if (selector && parmno > 1)
10321 {
10322 rname = selector;
10323 parmno -= 1;
10324 }
10325 }
10326
10327 if (objc_compare_types (type, rhstype, parmno, rname))
10328 {
10329 rhs = mark_rvalue_use (rhs);
10330 return convert (type, rhs);
10331 }
10332 }
10333
10334 /* [expr.ass]
10335
10336 The expression is implicitly converted (clause _conv_) to the
10337 cv-unqualified type of the left operand.
10338
10339 We allow bad conversions here because by the time we get to this point
10340 we are committed to doing the conversion. If we end up doing a bad
10341 conversion, convert_like will complain. */
10342 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10343 {
10344 /* When -Wno-pmf-conversions is use, we just silently allow
10345 conversions from pointers-to-members to plain pointers. If
10346 the conversion doesn't work, cp_convert will complain. */
10347 if (!warn_pmf2ptr
10348 && TYPE_PTR_P (type)
10349 && TYPE_PTRMEMFUNC_P (rhstype))
10350 rhs = cp_convert (strip_top_quals (type), rhs, complain);
10351 else
10352 {
10353 if (complain & tf_error)
10354 {
10355 /* If the right-hand side has unknown type, then it is an
10356 overloaded function. Call instantiate_type to get error
10357 messages. */
10358 if (rhstype == unknown_type_node)
10359 {
10360 tree r = instantiate_type (type, rhs, tf_warning_or_error);
10361 /* -fpermissive might allow this; recurse. */
10362 if (!seen_error ())
10363 return convert_for_assignment (type, rhs: r, errtype, fndecl,
10364 parmnum, complain, flags);
10365 }
10366 else if (fndecl)
10367 complain_about_bad_argument (arg_loc: rhs_loc,
10368 from_type: rhstype, to_type: type,
10369 fndecl, parmnum);
10370 else
10371 {
10372 range_label_for_type_mismatch label (rhstype, type);
10373 gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
10374 auto_diagnostic_group d;
10375
10376 switch (errtype)
10377 {
10378 case ICR_DEFAULT_ARGUMENT:
10379 error_at (&richloc,
10380 "cannot convert %qH to %qI in default argument",
10381 rhstype, type);
10382 break;
10383 case ICR_ARGPASS:
10384 error_at (&richloc,
10385 "cannot convert %qH to %qI in argument passing",
10386 rhstype, type);
10387 break;
10388 case ICR_CONVERTING:
10389 error_at (&richloc, "cannot convert %qH to %qI",
10390 rhstype, type);
10391 break;
10392 case ICR_INIT:
10393 error_at (&richloc,
10394 "cannot convert %qH to %qI in initialization",
10395 rhstype, type);
10396 break;
10397 case ICR_RETURN:
10398 error_at (&richloc, "cannot convert %qH to %qI in return",
10399 rhstype, type);
10400 break;
10401 case ICR_ASSIGN:
10402 error_at (&richloc,
10403 "cannot convert %qH to %qI in assignment",
10404 rhstype, type);
10405 break;
10406 default:
10407 gcc_unreachable();
10408 }
10409 }
10410
10411 /* See if we can be more helpful. */
10412 maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10413
10414 if (TYPE_PTR_P (rhstype)
10415 && TYPE_PTR_P (type)
10416 && CLASS_TYPE_P (TREE_TYPE (rhstype))
10417 && CLASS_TYPE_P (TREE_TYPE (type))
10418 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10419 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10420 (TREE_TYPE (rhstype))),
10421 "class type %qT is incomplete", TREE_TYPE (rhstype));
10422 }
10423 return error_mark_node;
10424 }
10425 }
10426 if (warn_suggest_attribute_format)
10427 {
10428 const enum tree_code codel = TREE_CODE (type);
10429 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10430 && coder == codel
10431 && check_missing_format_attribute (type, rhstype)
10432 && (complain & tf_warning))
10433 switch (errtype)
10434 {
10435 case ICR_ARGPASS:
10436 case ICR_DEFAULT_ARGUMENT:
10437 if (fndecl)
10438 warning (OPT_Wsuggest_attribute_format,
10439 "parameter %qP of %qD might be a candidate "
10440 "for a format attribute", parmnum, fndecl);
10441 else
10442 warning (OPT_Wsuggest_attribute_format,
10443 "parameter might be a candidate "
10444 "for a format attribute");
10445 break;
10446 case ICR_CONVERTING:
10447 warning (OPT_Wsuggest_attribute_format,
10448 "target of conversion might be a candidate "
10449 "for a format attribute");
10450 break;
10451 case ICR_INIT:
10452 warning (OPT_Wsuggest_attribute_format,
10453 "target of initialization might be a candidate "
10454 "for a format attribute");
10455 break;
10456 case ICR_RETURN:
10457 warning (OPT_Wsuggest_attribute_format,
10458 "return type might be a candidate "
10459 "for a format attribute");
10460 break;
10461 case ICR_ASSIGN:
10462 warning (OPT_Wsuggest_attribute_format,
10463 "left-hand side of assignment might be a candidate "
10464 "for a format attribute");
10465 break;
10466 default:
10467 gcc_unreachable();
10468 }
10469 }
10470
10471 if (TREE_CODE (type) == BOOLEAN_TYPE)
10472 maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10473
10474 if (complain & tf_warning)
10475 warn_for_address_of_packed_member (type, rhs);
10476
10477 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10478 complain, flags);
10479}
10480
10481/* Convert RHS to be of type TYPE.
10482 If EXP is nonzero, it is the target of the initialization.
10483 ERRTYPE indicates what kind of error the implicit conversion is.
10484
10485 Two major differences between the behavior of
10486 `convert_for_assignment' and `convert_for_initialization'
10487 are that references are bashed in the former, while
10488 copied in the latter, and aggregates are assigned in
10489 the former (operator=) while initialized in the
10490 latter (X(X&)).
10491
10492 If using constructor make sure no conversion operator exists, if one does
10493 exist, an ambiguity exists. */
10494
10495tree
10496convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10497 impl_conv_rhs errtype, tree fndecl, int parmnum,
10498 tsubst_flags_t complain)
10499{
10500 enum tree_code codel = TREE_CODE (type);
10501 tree rhstype;
10502 enum tree_code coder;
10503
10504 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10505 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10506 if (TREE_CODE (rhs) == NOP_EXPR
10507 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10508 && codel != REFERENCE_TYPE)
10509 rhs = TREE_OPERAND (rhs, 0);
10510
10511 if (type == error_mark_node
10512 || rhs == error_mark_node
10513 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10514 return error_mark_node;
10515
10516 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10517 ;
10518 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10519 && TREE_CODE (type) != ARRAY_TYPE
10520 && (!TYPE_REF_P (type)
10521 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10522 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10523 && !TYPE_REFFN_P (type))
10524 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10525 rhs = decay_conversion (exp: rhs, complain);
10526
10527 rhstype = TREE_TYPE (rhs);
10528 coder = TREE_CODE (rhstype);
10529
10530 if (coder == ERROR_MARK)
10531 return error_mark_node;
10532
10533 /* We accept references to incomplete types, so we can
10534 return here before checking if RHS is of complete type. */
10535
10536 if (codel == REFERENCE_TYPE)
10537 {
10538 auto_diagnostic_group d;
10539 /* This should eventually happen in convert_arguments. */
10540 int savew = 0, savee = 0;
10541
10542 if (fndecl)
10543 savew = warningcount + werrorcount, savee = errorcount;
10544 rhs = initialize_reference (type, rhs, flags, complain);
10545
10546 if (fndecl
10547 && (warningcount + werrorcount > savew || errorcount > savee))
10548 inform (get_fndecl_argument_location (fndecl, parmnum),
10549 "in passing argument %P of %qD", parmnum, fndecl);
10550 return rhs;
10551 }
10552
10553 if (exp != 0)
10554 exp = require_complete_type (value: exp, complain);
10555 if (exp == error_mark_node)
10556 return error_mark_node;
10557
10558 type = complete_type (type);
10559
10560 if (DIRECT_INIT_EXPR_P (type, rhs))
10561 /* Don't try to do copy-initialization if we already have
10562 direct-initialization. */
10563 return rhs;
10564
10565 if (MAYBE_CLASS_TYPE_P (type))
10566 return perform_implicit_conversion_flags (type, rhs, complain, flags);
10567
10568 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10569 complain, flags);
10570}
10571
10572/* If RETVAL is the address of, or a reference to, a local variable or
10573 temporary give an appropriate warning and return true. */
10574
10575static bool
10576maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10577{
10578 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10579 tree whats_returned = fold_for_warn (retval);
10580 if (!loc)
10581 loc = cp_expr_loc_or_input_loc (t: retval);
10582
10583 for (;;)
10584 {
10585 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10586 whats_returned = TREE_OPERAND (whats_returned, 1);
10587 else if (CONVERT_EXPR_P (whats_returned)
10588 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10589 whats_returned = TREE_OPERAND (whats_returned, 0);
10590 else
10591 break;
10592 }
10593
10594 if (TREE_CODE (whats_returned) == TARGET_EXPR
10595 && is_std_init_list (TREE_TYPE (whats_returned)))
10596 {
10597 tree init = TARGET_EXPR_INITIAL (whats_returned);
10598 if (TREE_CODE (init) == CONSTRUCTOR)
10599 /* Pull out the array address. */
10600 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10601 else if (TREE_CODE (init) == INDIRECT_REF)
10602 /* The source of a trivial copy looks like *(T*)&var. */
10603 whats_returned = TREE_OPERAND (init, 0);
10604 else
10605 return false;
10606 STRIP_NOPS (whats_returned);
10607 }
10608
10609 /* As a special case, we handle a call to std::move or std::forward. */
10610 if (TREE_CODE (whats_returned) == CALL_EXPR
10611 && (is_std_move_p (whats_returned)
10612 || is_std_forward_p (whats_returned)))
10613 {
10614 tree arg = CALL_EXPR_ARG (whats_returned, 0);
10615 return maybe_warn_about_returning_address_of_local (retval: arg, loc);
10616 }
10617
10618 if (TREE_CODE (whats_returned) != ADDR_EXPR)
10619 return false;
10620 whats_returned = TREE_OPERAND (whats_returned, 0);
10621
10622 while (TREE_CODE (whats_returned) == COMPONENT_REF
10623 || TREE_CODE (whats_returned) == ARRAY_REF)
10624 whats_returned = TREE_OPERAND (whats_returned, 0);
10625
10626 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10627 || TREE_CODE (whats_returned) == TARGET_EXPR)
10628 {
10629 if (TYPE_REF_P (valtype))
10630 /* P2748 made this an error in C++26. */
10631 emit_diagnostic (cxx_dialect >= cxx26 ? DK_PERMERROR : DK_WARNING,
10632 loc, OPT_Wreturn_local_addr,
10633 "returning reference to temporary");
10634 else if (TYPE_PTR_P (valtype))
10635 warning_at (loc, OPT_Wreturn_local_addr,
10636 "returning pointer to temporary");
10637 else if (is_std_init_list (valtype))
10638 warning_at (loc, OPT_Winit_list_lifetime,
10639 "returning temporary %<initializer_list%> does not extend "
10640 "the lifetime of the underlying array");
10641 return true;
10642 }
10643
10644 STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10645
10646 if (DECL_P (whats_returned)
10647 && DECL_NAME (whats_returned)
10648 && DECL_FUNCTION_SCOPE_P (whats_returned)
10649 && !is_capture_proxy (whats_returned)
10650 && !(TREE_STATIC (whats_returned)
10651 || TREE_PUBLIC (whats_returned)))
10652 {
10653 if (VAR_P (whats_returned)
10654 && DECL_DECOMPOSITION_P (whats_returned)
10655 && DECL_DECOMP_BASE (whats_returned)
10656 && DECL_HAS_VALUE_EXPR_P (whats_returned))
10657 {
10658 /* When returning address of a structured binding, if the structured
10659 binding is not a reference, continue normally, if it is a
10660 reference, recurse on the initializer of the structured
10661 binding. */
10662 tree base = DECL_DECOMP_BASE (whats_returned);
10663 if (TYPE_REF_P (TREE_TYPE (base)))
10664 {
10665 if (tree init = DECL_INITIAL (base))
10666 return maybe_warn_about_returning_address_of_local (retval: init, loc);
10667 else
10668 return false;
10669 }
10670 }
10671 bool w = false;
10672 auto_diagnostic_group d;
10673 if (TYPE_REF_P (valtype))
10674 w = warning_at (loc, OPT_Wreturn_local_addr,
10675 "reference to local variable %qD returned",
10676 whats_returned);
10677 else if (is_std_init_list (valtype))
10678 w = warning_at (loc, OPT_Winit_list_lifetime,
10679 "returning local %<initializer_list%> variable %qD "
10680 "does not extend the lifetime of the underlying array",
10681 whats_returned);
10682 else if (POINTER_TYPE_P (valtype)
10683 && TREE_CODE (whats_returned) == LABEL_DECL)
10684 w = warning_at (loc, OPT_Wreturn_local_addr,
10685 "address of label %qD returned",
10686 whats_returned);
10687 else if (POINTER_TYPE_P (valtype))
10688 w = warning_at (loc, OPT_Wreturn_local_addr,
10689 "address of local variable %qD returned",
10690 whats_returned);
10691 if (w)
10692 inform (DECL_SOURCE_LOCATION (whats_returned),
10693 "declared here");
10694 return true;
10695 }
10696
10697 return false;
10698}
10699
10700/* Returns true if DECL is in the std namespace. */
10701
10702bool
10703decl_in_std_namespace_p (tree decl)
10704{
10705 while (decl)
10706 {
10707 decl = decl_namespace_context (decl);
10708 if (DECL_NAMESPACE_STD_P (decl))
10709 return true;
10710 /* Allow inline namespaces inside of std namespace, e.g. with
10711 --enable-symvers=gnu-versioned-namespace std::forward would be
10712 actually std::_8::forward. */
10713 if (!DECL_NAMESPACE_INLINE_P (decl))
10714 return false;
10715 decl = CP_DECL_CONTEXT (decl);
10716 }
10717 return false;
10718}
10719
10720/* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10721
10722static bool
10723is_std_forward_p (tree fn)
10724{
10725 /* std::forward only takes one argument. */
10726 if (call_expr_nargs (fn) != 1)
10727 return false;
10728
10729 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10730 if (!decl_in_std_namespace_p (decl: fndecl))
10731 return false;
10732
10733 tree name = DECL_NAME (fndecl);
10734 return name && id_equal (id: name, str: "forward");
10735}
10736
10737/* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10738
10739static bool
10740is_std_move_p (tree fn)
10741{
10742 /* std::move only takes one argument. */
10743 if (call_expr_nargs (fn) != 1)
10744 return false;
10745
10746 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10747 if (!decl_in_std_namespace_p (decl: fndecl))
10748 return false;
10749
10750 tree name = DECL_NAME (fndecl);
10751 return name && id_equal (id: name, str: "move");
10752}
10753
10754/* Returns true if RETVAL is a good candidate for the NRVO as per
10755 [class.copy.elision]. FUNCTYPE is the type the function is declared
10756 to return. */
10757
10758static bool
10759can_do_nrvo_p (tree retval, tree functype)
10760{
10761 if (functype == error_mark_node)
10762 return false;
10763 if (retval)
10764 STRIP_ANY_LOCATION_WRAPPER (retval);
10765 tree result = DECL_RESULT (current_function_decl);
10766 return (retval != NULL_TREE
10767 && !processing_template_decl
10768 /* Must be a local, automatic variable. */
10769 && VAR_P (retval)
10770 && DECL_CONTEXT (retval) == current_function_decl
10771 && !TREE_STATIC (retval)
10772 /* And not a lambda or anonymous union proxy. */
10773 && !DECL_HAS_VALUE_EXPR_P (retval)
10774 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10775 /* The cv-unqualified type of the returned value must be the
10776 same as the cv-unqualified return type of the
10777 function. */
10778 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10779 TYPE_MAIN_VARIANT (functype))
10780 /* And the returned value must be non-volatile. */
10781 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10782}
10783
10784/* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10785 would otherwise return in memory. */
10786
10787static bool
10788want_nrvo_p (tree retval, tree functype)
10789{
10790 return (can_do_nrvo_p (retval, functype)
10791 && aggregate_value_p (functype, current_function_decl));
10792}
10793
10794/* Like can_do_nrvo_p, but we check if we're trying to move a class
10795 prvalue. */
10796
10797static bool
10798can_elide_copy_prvalue_p (tree retval, tree functype)
10799{
10800 if (functype == error_mark_node)
10801 return false;
10802 if (retval)
10803 STRIP_ANY_LOCATION_WRAPPER (retval);
10804 return (retval != NULL_TREE
10805 && !glvalue_p (retval)
10806 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10807 TYPE_MAIN_VARIANT (functype))
10808 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10809}
10810
10811/* If we should treat RETVAL, an expression being returned, as if it were
10812 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10813 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10814 context (rather than throw). */
10815
10816tree
10817treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10818{
10819 if (cxx_dialect == cxx98)
10820 return NULL_TREE;
10821
10822 tree retval = expr;
10823 STRIP_ANY_LOCATION_WRAPPER (retval);
10824 if (REFERENCE_REF_P (retval))
10825 retval = TREE_OPERAND (retval, 0);
10826
10827 /* An implicitly movable entity is a variable of automatic storage duration
10828 that is either a non-volatile object or (C++20) an rvalue reference to a
10829 non-volatile object type. */
10830 if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
10831 || TREE_CODE (retval) == PARM_DECL)
10832 && !TREE_STATIC (retval)
10833 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
10834 && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
10835 || (cxx_dialect >= cxx20
10836 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
10837 return NULL_TREE;
10838
10839 /* If the expression in a return or co_return statement is a (possibly
10840 parenthesized) id-expression that names an implicitly movable entity
10841 declared in the body or parameter-declaration-clause of the innermost
10842 enclosing function or lambda-expression, */
10843 if (return_p)
10844 {
10845 if (DECL_CONTEXT (retval) != current_function_decl)
10846 return NULL_TREE;
10847 expr = move (expr);
10848 if (expr == error_mark_node)
10849 return NULL_TREE;
10850 return set_implicit_rvalue_p (expr);
10851 }
10852
10853 /* if the id-expression (possibly parenthesized) is the operand of
10854 a throw-expression, and names an implicitly movable entity that belongs
10855 to a scope that does not contain the compound-statement of the innermost
10856 lambda-expression, try-block, or function-try-block (if any) whose
10857 compound-statement or ctor-initializer contains the throw-expression. */
10858
10859 /* C++20 added move on throw of parms. */
10860 if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
10861 return NULL_TREE;
10862
10863 /* We don't check for lambda-expression here, because we should not get past
10864 the DECL_HAS_VALUE_EXPR_P check above. */
10865 for (cp_binding_level *b = current_binding_level;
10866 b->kind != sk_namespace; b = b->level_chain)
10867 {
10868 for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
10869 if (decl == retval)
10870 return set_implicit_rvalue_p (move (expr));
10871 if (b->kind == sk_try)
10872 return NULL_TREE;
10873 }
10874
10875 return set_implicit_rvalue_p (move (expr));
10876}
10877
10878/* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10879 is true). EXPR is the std::move expression; TYPE is the type of the object
10880 being initialized. */
10881
10882void
10883maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
10884{
10885 if (!(warn_pessimizing_move || warn_redundant_move))
10886 return;
10887
10888 const location_t loc = cp_expr_loc_or_input_loc (t: expr);
10889
10890 /* C++98 doesn't know move. */
10891 if (cxx_dialect < cxx11)
10892 return;
10893
10894 /* Wait until instantiation time, since we can't gauge if we should do
10895 the NRVO until then. */
10896 if (processing_template_decl)
10897 return;
10898
10899 /* This is only interesting for class types. */
10900 if (!CLASS_TYPE_P (type))
10901 return;
10902
10903 bool wrapped_p = false;
10904 /* A a = std::move (A()); */
10905 if (TREE_CODE (expr) == TREE_LIST)
10906 {
10907 if (list_length (expr) == 1)
10908 {
10909 expr = TREE_VALUE (expr);
10910 wrapped_p = true;
10911 }
10912 else
10913 return;
10914 }
10915 /* A a = {std::move (A())};
10916 A a{std::move (A())}; */
10917 else if (TREE_CODE (expr) == CONSTRUCTOR)
10918 {
10919 if (CONSTRUCTOR_NELTS (expr) == 1)
10920 {
10921 expr = CONSTRUCTOR_ELT (expr, 0)->value;
10922 wrapped_p = true;
10923 }
10924 else
10925 return;
10926 }
10927
10928 /* First, check if this is a call to std::move. */
10929 if (!REFERENCE_REF_P (expr)
10930 || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
10931 return;
10932 tree fn = TREE_OPERAND (expr, 0);
10933 if (!is_std_move_p (fn))
10934 return;
10935 tree arg = CALL_EXPR_ARG (fn, 0);
10936 if (TREE_CODE (arg) != NOP_EXPR)
10937 return;
10938 /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
10939 and implicitly-movable warnings. */
10940 if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
10941 {
10942 arg = TREE_OPERAND (arg, 0);
10943 arg = TREE_OPERAND (arg, 0);
10944 arg = convert_from_reference (arg);
10945 if (can_elide_copy_prvalue_p (retval: arg, functype: type))
10946 {
10947 auto_diagnostic_group d;
10948 if (warning_at (loc, OPT_Wpessimizing_move,
10949 "moving a temporary object prevents copy elision"))
10950 inform (loc, "remove %<std::move%> call");
10951 }
10952 /* The rest of the warnings is only relevant for when we are returning
10953 from a function. */
10954 if (!return_p)
10955 return;
10956
10957 tree moved;
10958 /* Warn if we could do copy elision were it not for the move. */
10959 if (can_do_nrvo_p (retval: arg, functype: type))
10960 {
10961 auto_diagnostic_group d;
10962 if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
10963 && warning_at (loc, OPT_Wpessimizing_move,
10964 "moving a local object in a return statement "
10965 "prevents copy elision"))
10966 inform (loc, "remove %<std::move%> call");
10967 }
10968 /* Warn if the move is redundant. It is redundant when we would
10969 do maybe-rvalue overload resolution even without std::move. */
10970 else if (warn_redundant_move
10971 /* This doesn't apply for return {std::move (t)};. */
10972 && !wrapped_p
10973 && !warning_suppressed_p (expr, OPT_Wredundant_move)
10974 && (moved = treat_lvalue_as_rvalue_p (expr: arg, /*return*/return_p: true)))
10975 {
10976 /* Make sure that overload resolution would actually succeed
10977 if we removed the std::move call. */
10978 tree t = convert_for_initialization (NULL_TREE, type,
10979 rhs: moved,
10980 flags: (LOOKUP_NORMAL
10981 | LOOKUP_ONLYCONVERTING),
10982 errtype: ICR_RETURN, NULL_TREE, parmnum: 0,
10983 complain: tf_none);
10984 /* If this worked, implicit rvalue would work, so the call to
10985 std::move is redundant. */
10986 if (t != error_mark_node)
10987 {
10988 auto_diagnostic_group d;
10989 if (warning_at (loc, OPT_Wredundant_move,
10990 "redundant move in return statement"))
10991 inform (loc, "remove %<std::move%> call");
10992 }
10993 }
10994 }
10995 /* Also try to warn about redundant std::move in code such as
10996 T f (const T& t)
10997 {
10998 return std::move(t);
10999 }
11000 for which EXPR will be something like
11001 *std::move<const T&> ((const struct T &) (const struct T *) t)
11002 and where the std::move does nothing if T does not have a T(const T&&)
11003 constructor, because the argument is const. It will not use T(T&&)
11004 because that would mean losing the const. */
11005 else if (warn_redundant_move
11006 && !warning_suppressed_p (expr, OPT_Wredundant_move)
11007 && TYPE_REF_P (TREE_TYPE (arg))
11008 && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11009 {
11010 tree rtype = TREE_TYPE (TREE_TYPE (arg));
11011 if (!same_type_ignoring_top_level_qualifiers_p (type1: rtype, type2: type))
11012 return;
11013 /* Check for the unlikely case there's T(const T&&) (we don't care if
11014 it's deleted). */
11015 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11016 if (move_fn_p (fn))
11017 {
11018 tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11019 if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11020 return;
11021 }
11022 auto_diagnostic_group d;
11023 if (return_p
11024 ? warning_at (loc, OPT_Wredundant_move,
11025 "redundant move in return statement")
11026 : warning_at (loc, OPT_Wredundant_move,
11027 "redundant move in initialization"))
11028 inform (loc, "remove %<std::move%> call");
11029 }
11030}
11031
11032/* Check that returning RETVAL from the current function is valid.
11033 Return an expression explicitly showing all conversions required to
11034 change RETVAL into the function return type, and to assign it to
11035 the DECL_RESULT for the function. Set *NO_WARNING to true if
11036 code reaches end of non-void function warning shouldn't be issued
11037 on this RETURN_EXPR. Set *DANGLING to true if code returns the
11038 address of a local variable. */
11039
11040tree
11041check_return_expr (tree retval, bool *no_warning, bool *dangling)
11042{
11043 tree result;
11044 /* The type actually returned by the function. */
11045 tree valtype;
11046 /* The type the function is declared to return, or void if
11047 the declared type is incomplete. */
11048 tree functype;
11049 int fn_returns_value_p;
11050 location_t loc = cp_expr_loc_or_input_loc (t: retval);
11051
11052 *no_warning = false;
11053 *dangling = false;
11054
11055 /* A `volatile' function is one that isn't supposed to return, ever.
11056 (This is a G++ extension, used to get better code for functions
11057 that call the `volatile' function.) */
11058 if (TREE_THIS_VOLATILE (current_function_decl))
11059 warning (0, "function declared %<noreturn%> has a %<return%> statement");
11060
11061 /* Check for various simple errors. */
11062 if (DECL_DESTRUCTOR_P (current_function_decl))
11063 {
11064 if (retval)
11065 error_at (loc, "returning a value from a destructor");
11066
11067 if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11068 retval = current_class_ptr;
11069 else
11070 return NULL_TREE;
11071 }
11072 else if (DECL_CONSTRUCTOR_P (current_function_decl))
11073 {
11074 if (in_function_try_handler)
11075 /* If a return statement appears in a handler of the
11076 function-try-block of a constructor, the program is ill-formed. */
11077 error ("cannot return from a handler of a function-try-block of a constructor");
11078 else if (retval)
11079 /* You can't return a value from a constructor. */
11080 error_at (loc, "returning a value from a constructor");
11081
11082 if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11083 retval = current_class_ptr;
11084 else
11085 return NULL_TREE;
11086 }
11087
11088 const tree saved_retval = retval;
11089
11090 if (processing_template_decl)
11091 {
11092 current_function_returns_value = 1;
11093
11094 if (check_for_bare_parameter_packs (retval))
11095 return error_mark_node;
11096
11097 /* If one of the types might be void, we can't tell whether we're
11098 returning a value. */
11099 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11100 && !FNDECL_USED_AUTO (current_function_decl))
11101 || (retval != NULL_TREE
11102 && (TREE_TYPE (retval) == NULL_TREE
11103 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11104 goto dependent;
11105 }
11106
11107 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11108
11109 /* Deduce auto return type from a return statement. */
11110 if (FNDECL_USED_AUTO (current_function_decl))
11111 {
11112 tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11113 tree auto_node;
11114 tree type;
11115
11116 if (!retval && !is_auto (pattern))
11117 {
11118 /* Give a helpful error message. */
11119 error ("return-statement with no value, in function returning %qT",
11120 pattern);
11121 inform (input_location, "only plain %<auto%> return type can be "
11122 "deduced to %<void%>");
11123 type = error_mark_node;
11124 }
11125 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11126 {
11127 error ("returning initializer list");
11128 type = error_mark_node;
11129 }
11130 else
11131 {
11132 if (!retval)
11133 retval = void_node;
11134 auto_node = type_uses_auto (pattern);
11135 type = do_auto_deduction (pattern, retval, auto_node,
11136 tf_warning_or_error, adc_return_type);
11137 }
11138
11139 if (type == error_mark_node)
11140 /* Leave it. */;
11141 else if (functype == pattern)
11142 apply_deduced_return_type (current_function_decl, type);
11143 else if (!same_type_p (type, functype))
11144 {
11145 if (LAMBDA_FUNCTION_P (current_function_decl))
11146 error_at (loc, "inconsistent types %qT and %qT deduced for "
11147 "lambda return type", functype, type);
11148 else
11149 error_at (loc, "inconsistent deduction for auto return type: "
11150 "%qT and then %qT", functype, type);
11151 }
11152 functype = type;
11153 }
11154
11155 result = DECL_RESULT (current_function_decl);
11156 valtype = TREE_TYPE (result);
11157 gcc_assert (valtype != NULL_TREE);
11158 fn_returns_value_p = !VOID_TYPE_P (valtype);
11159
11160 /* Check for a return statement with no return value in a function
11161 that's supposed to return a value. */
11162 if (!retval && fn_returns_value_p)
11163 {
11164 if (functype != error_mark_node)
11165 permerror (input_location, "return-statement with no value, in "
11166 "function returning %qT", valtype);
11167 /* Remember that this function did return. */
11168 current_function_returns_value = 1;
11169 /* And signal caller that TREE_NO_WARNING should be set on the
11170 RETURN_EXPR to avoid control reaches end of non-void function
11171 warnings in tree-cfg.cc. */
11172 *no_warning = true;
11173 }
11174 /* Check for a return statement with a value in a function that
11175 isn't supposed to return a value. */
11176 else if (retval && !fn_returns_value_p)
11177 {
11178 if (VOID_TYPE_P (TREE_TYPE (retval)))
11179 /* You can return a `void' value from a function of `void'
11180 type. In that case, we have to evaluate the expression for
11181 its side-effects. */
11182 finish_expr_stmt (retval);
11183 else if (retval != error_mark_node)
11184 permerror (loc, "return-statement with a value, in function "
11185 "returning %qT", valtype);
11186 current_function_returns_null = 1;
11187
11188 /* There's really no value to return, after all. */
11189 return NULL_TREE;
11190 }
11191 else if (!retval)
11192 /* Remember that this function can sometimes return without a
11193 value. */
11194 current_function_returns_null = 1;
11195 else
11196 /* Remember that this function did return a value. */
11197 current_function_returns_value = 1;
11198
11199 /* Check for erroneous operands -- but after giving ourselves a
11200 chance to provide an error about returning a value from a void
11201 function. */
11202 if (error_operand_p (t: retval))
11203 {
11204 current_function_return_value = error_mark_node;
11205 return error_mark_node;
11206 }
11207
11208 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11209 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11210 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11211 && ! flag_check_new
11212 && retval && null_ptr_cst_p (retval))
11213 warning (0, "%<operator new%> must not return NULL unless it is "
11214 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11215
11216 /* Effective C++ rule 15. See also start_function. */
11217 if (warn_ecpp
11218 && DECL_NAME (current_function_decl) == assign_op_identifier
11219 && !type_dependent_expression_p (retval))
11220 {
11221 bool warn = true;
11222
11223 /* The function return type must be a reference to the current
11224 class. */
11225 if (TYPE_REF_P (valtype)
11226 && same_type_ignoring_top_level_qualifiers_p
11227 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11228 {
11229 /* Returning '*this' is obviously OK. */
11230 if (retval == current_class_ref)
11231 warn = false;
11232 /* If we are calling a function whose return type is the same of
11233 the current class reference, it is ok. */
11234 else if (INDIRECT_REF_P (retval)
11235 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11236 warn = false;
11237 }
11238
11239 if (warn)
11240 warning_at (loc, OPT_Weffc__,
11241 "%<operator=%> should return a reference to %<*this%>");
11242 }
11243
11244 if (dependent_type_p (functype)
11245 || type_dependent_expression_p (retval))
11246 {
11247 dependent:
11248 /* We should not have changed the return value. */
11249 gcc_assert (retval == saved_retval);
11250 /* We don't know if this is an lvalue or rvalue use, but
11251 either way we can mark it as read. */
11252 mark_exp_read (retval);
11253 return retval;
11254 }
11255
11256 /* The fabled Named Return Value optimization, as per [class.copy]/15:
11257
11258 [...] For a function with a class return type, if the expression
11259 in the return statement is the name of a local object, and the cv-
11260 unqualified type of the local object is the same as the function
11261 return type, an implementation is permitted to omit creating the tem-
11262 porary object to hold the function return value [...]
11263
11264 So, if this is a value-returning function that always returns the same
11265 local variable, remember it.
11266
11267 We choose the first suitable variable even if the function sometimes
11268 returns something else, but only if the variable is out of scope at the
11269 other return sites, or else we run the risk of clobbering the variable we
11270 chose if the other returned expression uses the chosen variable somehow.
11271
11272 We don't currently do this if the first return is a non-variable, as it
11273 would be complicated to determine whether an NRV selected later was in
11274 scope at the point of the earlier return. We also don't currently support
11275 multiple variables with non-overlapping scopes (53637).
11276
11277 See finish_function and finalize_nrv for the rest of this optimization. */
11278 tree bare_retval = NULL_TREE;
11279 if (retval)
11280 {
11281 retval = maybe_undo_parenthesized_ref (retval);
11282 bare_retval = tree_strip_any_location_wrapper (exp: retval);
11283 }
11284
11285 bool named_return_value_okay_p = want_nrvo_p (retval: bare_retval, functype);
11286 if (fn_returns_value_p && flag_elide_constructors
11287 && current_function_return_value != bare_retval)
11288 {
11289 if (named_return_value_okay_p
11290 && current_function_return_value == NULL_TREE)
11291 current_function_return_value = bare_retval;
11292 else if (current_function_return_value
11293 && VAR_P (current_function_return_value)
11294 && DECL_NAME (current_function_return_value)
11295 && !decl_in_scope_p (current_function_return_value))
11296 {
11297 /* The earlier NRV is out of scope at this point, so it's safe to
11298 leave it alone; the current return can't refer to it. */;
11299 if (named_return_value_okay_p
11300 && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11301 {
11302 warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11303 bare_retval);
11304 suppress_warning (current_function_decl, OPT_Wnrvo);
11305 }
11306 }
11307 else
11308 {
11309 if ((named_return_value_okay_p
11310 || (current_function_return_value
11311 && current_function_return_value != error_mark_node))
11312 && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11313 {
11314 warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11315 current_function_decl);
11316 suppress_warning (current_function_decl, OPT_Wnrvo);
11317 }
11318 current_function_return_value = error_mark_node;
11319 }
11320 }
11321
11322 /* We don't need to do any conversions when there's nothing being
11323 returned. */
11324 if (!retval)
11325 return NULL_TREE;
11326
11327 if (!named_return_value_okay_p)
11328 maybe_warn_pessimizing_move (expr: retval, type: functype, /*return_p*/true);
11329
11330 /* Do any required conversions. */
11331 if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11332 /* No conversions are required. */
11333 ;
11334 else
11335 {
11336 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11337
11338 /* The functype's return type will have been set to void, if it
11339 was an incomplete type. Just treat this as 'return;' */
11340 if (VOID_TYPE_P (functype))
11341 return error_mark_node;
11342
11343 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11344 treated as an rvalue for the purposes of overload resolution to
11345 favor move constructors over copy constructors.
11346
11347 Note that these conditions are similar to, but not as strict as,
11348 the conditions for the named return value optimization. */
11349 bool converted = false;
11350 tree moved;
11351 /* Until C++23, this was only interesting for class type, but in C++23,
11352 we should do the below when we're converting rom/to a class/reference
11353 (a non-scalar type). */
11354 if ((cxx_dialect < cxx23
11355 ? CLASS_TYPE_P (functype)
11356 : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11357 && (moved = treat_lvalue_as_rvalue_p (expr: retval, /*return*/return_p: true)))
11358 /* In C++20 and earlier we treat the return value as an rvalue
11359 that can bind to lvalue refs. In C++23, such an expression is just
11360 an xvalue (see reference_binding). */
11361 retval = moved;
11362
11363 /* The call in a (lambda) thunk needs no conversions. */
11364 if (TREE_CODE (retval) == CALL_EXPR
11365 && call_from_lambda_thunk_p (retval))
11366 converted = true;
11367
11368 /* First convert the value to the function's return type, then
11369 to the type of return value's location to handle the
11370 case that functype is smaller than the valtype. */
11371 if (!converted)
11372 retval = convert_for_initialization
11373 (NULL_TREE, type: functype, rhs: retval, flags, errtype: ICR_RETURN, NULL_TREE, parmnum: 0,
11374 complain: tf_warning_or_error);
11375 retval = convert (valtype, retval);
11376
11377 /* If the conversion failed, treat this just like `return;'. */
11378 if (retval == error_mark_node)
11379 return retval;
11380 /* We can't initialize a register from a AGGR_INIT_EXPR. */
11381 else if (! cfun->returns_struct
11382 && TREE_CODE (retval) == TARGET_EXPR
11383 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
11384 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11385 TREE_OPERAND (retval, 0));
11386 else if (!processing_template_decl
11387 && maybe_warn_about_returning_address_of_local (retval, loc)
11388 && INDIRECT_TYPE_P (valtype))
11389 *dangling = true;
11390 }
11391
11392 /* A naive attempt to reduce the number of -Wdangling-reference false
11393 positives: if we know that this function can return a variable with
11394 static storage duration rather than one of its parameters, suppress
11395 the warning. */
11396 if (warn_dangling_reference
11397 && TYPE_REF_P (functype)
11398 && bare_retval
11399 && VAR_P (bare_retval)
11400 && TREE_STATIC (bare_retval))
11401 suppress_warning (current_function_decl, OPT_Wdangling_reference);
11402
11403 if (processing_template_decl)
11404 return saved_retval;
11405
11406 /* Actually copy the value returned into the appropriate location. */
11407 if (retval && retval != result)
11408 {
11409 /* If there's a postcondition for a scalar return value, wrap
11410 retval in a call to the postcondition function. */
11411 if (tree post = apply_postcondition_to_return (retval))
11412 retval = post;
11413 retval = cp_build_init_expr (t: result, i: retval);
11414 }
11415
11416 if (current_function_return_value == bare_retval)
11417 INIT_EXPR_NRV_P (retval) = true;
11418
11419 if (tree set = maybe_set_retval_sentinel ())
11420 retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11421
11422 /* If there's a postcondition for an aggregate return value, call the
11423 postcondition function after the return object is initialized. */
11424 if (tree post = apply_postcondition_to_return (result))
11425 retval = build2 (COMPOUND_EXPR, void_type_node, retval, post);
11426
11427 return retval;
11428}
11429
11430
11431/* Returns nonzero if the pointer-type FROM can be converted to the
11432 pointer-type TO via a qualification conversion. If CONSTP is -1,
11433 then we return nonzero if the pointers are similar, and the
11434 cv-qualification signature of FROM is a proper subset of that of TO.
11435
11436 If CONSTP is positive, then all outer pointers have been
11437 const-qualified. */
11438
11439static bool
11440comp_ptr_ttypes_real (tree to, tree from, int constp)
11441{
11442 bool to_more_cv_qualified = false;
11443 bool is_opaque_pointer = false;
11444
11445 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11446 {
11447 if (TREE_CODE (to) != TREE_CODE (from))
11448 return false;
11449
11450 if (TREE_CODE (from) == OFFSET_TYPE
11451 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11452 TYPE_OFFSET_BASETYPE (to)))
11453 return false;
11454
11455 /* Const and volatile mean something different for function and
11456 array types, so the usual checks are not appropriate. We'll
11457 check the array type elements in further iterations. */
11458 if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11459 {
11460 if (!at_least_as_qualified_p (type1: to, type2: from))
11461 return false;
11462
11463 if (!at_least_as_qualified_p (type1: from, type2: to))
11464 {
11465 if (constp == 0)
11466 return false;
11467 to_more_cv_qualified = true;
11468 }
11469
11470 if (constp > 0)
11471 constp &= TYPE_READONLY (to);
11472 }
11473
11474 if (VECTOR_TYPE_P (to))
11475 is_opaque_pointer = vector_targets_convertible_p (t1: to, t2: from);
11476
11477 /* P0388R4 allows a conversion from int[N] to int[] but not the
11478 other way round. When both arrays have bounds but they do
11479 not match, then no conversion is possible. */
11480 if (TREE_CODE (to) == ARRAY_TYPE
11481 && !comp_array_types (t1: to, t2: from, cb: bounds_first, /*strict=*/false))
11482 return false;
11483
11484 if (!TYPE_PTR_P (to)
11485 && !TYPE_PTRDATAMEM_P (to)
11486 /* CWG 330 says we need to look through arrays. */
11487 && TREE_CODE (to) != ARRAY_TYPE)
11488 return ((constp >= 0 || to_more_cv_qualified)
11489 && (is_opaque_pointer
11490 || same_type_ignoring_top_level_qualifiers_p (type1: to, type2: from)));
11491 }
11492}
11493
11494/* When comparing, say, char ** to char const **, this function takes
11495 the 'char *' and 'char const *'. Do not pass non-pointer/reference
11496 types to this function. */
11497
11498int
11499comp_ptr_ttypes (tree to, tree from)
11500{
11501 return comp_ptr_ttypes_real (to, from, constp: 1);
11502}
11503
11504/* Returns true iff FNTYPE is a non-class type that involves
11505 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11506 if a parameter type is ill-formed. */
11507
11508bool
11509error_type_p (const_tree type)
11510{
11511 tree t;
11512
11513 switch (TREE_CODE (type))
11514 {
11515 case ERROR_MARK:
11516 return true;
11517
11518 case POINTER_TYPE:
11519 case REFERENCE_TYPE:
11520 case OFFSET_TYPE:
11521 return error_type_p (TREE_TYPE (type));
11522
11523 case FUNCTION_TYPE:
11524 case METHOD_TYPE:
11525 if (error_type_p (TREE_TYPE (type)))
11526 return true;
11527 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11528 if (error_type_p (TREE_VALUE (t)))
11529 return true;
11530 return false;
11531
11532 case RECORD_TYPE:
11533 if (TYPE_PTRMEMFUNC_P (type))
11534 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11535 return false;
11536
11537 default:
11538 return false;
11539 }
11540}
11541
11542/* Returns true if to and from are (possibly multi-level) pointers to the same
11543 type or inheritance-related types, regardless of cv-quals. */
11544
11545bool
11546ptr_reasonably_similar (const_tree to, const_tree from)
11547{
11548 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11549 {
11550 /* Any target type is similar enough to void. */
11551 if (VOID_TYPE_P (to))
11552 return !error_type_p (type: from);
11553 if (VOID_TYPE_P (from))
11554 return !error_type_p (type: to);
11555
11556 if (TREE_CODE (to) != TREE_CODE (from))
11557 return false;
11558
11559 if (TREE_CODE (from) == OFFSET_TYPE
11560 && comptypes (TYPE_OFFSET_BASETYPE (to),
11561 TYPE_OFFSET_BASETYPE (from),
11562 COMPARE_BASE | COMPARE_DERIVED))
11563 continue;
11564
11565 if (VECTOR_TYPE_P (to)
11566 && vector_types_convertible_p (t1: to, t2: from, emit_lax_note: false))
11567 return true;
11568
11569 if (TREE_CODE (to) == INTEGER_TYPE
11570 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11571 return true;
11572
11573 if (TREE_CODE (to) == FUNCTION_TYPE)
11574 return !error_type_p (type: to) && !error_type_p (type: from);
11575
11576 if (!TYPE_PTR_P (to))
11577 {
11578 /* When either type is incomplete avoid DERIVED_FROM_P,
11579 which may call complete_type (c++/57942). */
11580 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11581 return comptypes
11582 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11583 strict: b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11584 }
11585 }
11586}
11587
11588/* Return true if TO and FROM (both of which are POINTER_TYPEs or
11589 pointer-to-member types) are the same, ignoring cv-qualification at
11590 all levels. CB says how we should behave when comparing array bounds. */
11591
11592bool
11593comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11594{
11595 bool is_opaque_pointer = false;
11596
11597 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11598 {
11599 if (TREE_CODE (to) != TREE_CODE (from))
11600 return false;
11601
11602 if (TREE_CODE (from) == OFFSET_TYPE
11603 && same_type_p (TYPE_OFFSET_BASETYPE (from),
11604 TYPE_OFFSET_BASETYPE (to)))
11605 continue;
11606
11607 if (VECTOR_TYPE_P (to))
11608 is_opaque_pointer = vector_targets_convertible_p (t1: to, t2: from);
11609
11610 if (TREE_CODE (to) == ARRAY_TYPE
11611 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11612 we must fail. */
11613 && !comp_array_types (t1: to, t2: from, cb, /*strict=*/false))
11614 return false;
11615
11616 /* CWG 330 says we need to look through arrays. */
11617 if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11618 return (is_opaque_pointer
11619 || same_type_ignoring_top_level_qualifiers_p (type1: to, type2: from));
11620 }
11621}
11622
11623/* Returns the type qualifiers for this type, including the qualifiers on the
11624 elements for an array type. */
11625
11626int
11627cp_type_quals (const_tree type)
11628{
11629 int quals;
11630 /* This CONST_CAST is okay because strip_array_types returns its
11631 argument unmodified and we assign it to a const_tree. */
11632 type = strip_array_types (CONST_CAST_TREE (type));
11633 if (type == error_mark_node
11634 /* Quals on a FUNCTION_TYPE are memfn quals. */
11635 || TREE_CODE (type) == FUNCTION_TYPE)
11636 return TYPE_UNQUALIFIED;
11637 quals = TYPE_QUALS (type);
11638 /* METHOD and REFERENCE_TYPEs should never have quals. */
11639 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11640 && !TYPE_REF_P (type))
11641 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11642 == TYPE_UNQUALIFIED));
11643 return quals;
11644}
11645
11646/* Returns the function-ref-qualifier for TYPE */
11647
11648cp_ref_qualifier
11649type_memfn_rqual (const_tree type)
11650{
11651 gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11652
11653 if (!FUNCTION_REF_QUALIFIED (type))
11654 return REF_QUAL_NONE;
11655 else if (FUNCTION_RVALUE_QUALIFIED (type))
11656 return REF_QUAL_RVALUE;
11657 else
11658 return REF_QUAL_LVALUE;
11659}
11660
11661/* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11662 METHOD_TYPE. */
11663
11664int
11665type_memfn_quals (const_tree type)
11666{
11667 if (TREE_CODE (type) == FUNCTION_TYPE)
11668 return TYPE_QUALS (type);
11669 else if (TREE_CODE (type) == METHOD_TYPE)
11670 return cp_type_quals (type: class_of_this_parm (fntype: type));
11671 else
11672 gcc_unreachable ();
11673}
11674
11675/* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11676 MEMFN_QUALS and its ref-qualifier to RQUAL. */
11677
11678tree
11679apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11680{
11681 /* Could handle METHOD_TYPE here if necessary. */
11682 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11683 if (TYPE_QUALS (type) == memfn_quals
11684 && type_memfn_rqual (type) == rqual)
11685 return type;
11686
11687 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11688 complex. */
11689 tree result = build_qualified_type (type, memfn_quals);
11690 return build_ref_qualified_type (result, rqual);
11691}
11692
11693/* Returns nonzero if TYPE is const or volatile. */
11694
11695bool
11696cv_qualified_p (const_tree type)
11697{
11698 int quals = cp_type_quals (type);
11699 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11700}
11701
11702/* Returns nonzero if the TYPE contains a mutable member. */
11703
11704bool
11705cp_has_mutable_p (const_tree type)
11706{
11707 /* This CONST_CAST is okay because strip_array_types returns its
11708 argument unmodified and we assign it to a const_tree. */
11709 type = strip_array_types (CONST_CAST_TREE(type));
11710
11711 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11712}
11713
11714/* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11715 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11716 approximation. In particular, consider:
11717
11718 int f();
11719 struct S { int i; };
11720 const S s = { f(); }
11721
11722 Here, we will make "s" as TREE_READONLY (because it is declared
11723 "const") -- only to reverse ourselves upon seeing that the
11724 initializer is non-constant. */
11725
11726void
11727cp_apply_type_quals_to_decl (int type_quals, tree decl)
11728{
11729 tree type = TREE_TYPE (decl);
11730
11731 if (type == error_mark_node)
11732 return;
11733
11734 if (TREE_CODE (decl) == TYPE_DECL)
11735 return;
11736
11737 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11738 && type_quals != TYPE_UNQUALIFIED));
11739
11740 /* Avoid setting TREE_READONLY incorrectly. */
11741 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11742 constructor can produce constant init, so rely on cp_finish_decl to
11743 clear TREE_READONLY if the variable has non-constant init. */
11744
11745 /* If the type has (or might have) a mutable component, that component
11746 might be modified. */
11747 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11748 type_quals &= ~TYPE_QUAL_CONST;
11749
11750 c_apply_type_quals_to_decl (type_quals, decl);
11751}
11752
11753/* Subroutine of casts_away_constness. Make T1 and T2 point at
11754 exemplar types such that casting T1 to T2 is casting away constness
11755 if and only if there is no implicit conversion from T1 to T2. */
11756
11757static void
11758casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11759{
11760 int quals1;
11761 int quals2;
11762
11763 /* [expr.const.cast]
11764
11765 For multi-level pointer to members and multi-level mixed pointers
11766 and pointers to members (conv.qual), the "member" aspect of a
11767 pointer to member level is ignored when determining if a const
11768 cv-qualifier has been cast away. */
11769 /* [expr.const.cast]
11770
11771 For two pointer types:
11772
11773 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11774 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11775 K is min(N,M)
11776
11777 casting from X1 to X2 casts away constness if, for a non-pointer
11778 type T there does not exist an implicit conversion (clause
11779 _conv_) from:
11780
11781 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11782
11783 to
11784
11785 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11786 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11787 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11788 {
11789 *t1 = cp_build_qualified_type (void_type_node,
11790 cp_type_quals (type: *t1));
11791 *t2 = cp_build_qualified_type (void_type_node,
11792 cp_type_quals (type: *t2));
11793 return;
11794 }
11795
11796 quals1 = cp_type_quals (type: *t1);
11797 quals2 = cp_type_quals (type: *t2);
11798
11799 if (TYPE_PTRDATAMEM_P (*t1))
11800 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11801 else
11802 *t1 = TREE_TYPE (*t1);
11803 if (TYPE_PTRDATAMEM_P (*t2))
11804 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11805 else
11806 *t2 = TREE_TYPE (*t2);
11807
11808 casts_away_constness_r (t1, t2, complain);
11809 *t1 = build_pointer_type (*t1);
11810 *t2 = build_pointer_type (*t2);
11811 *t1 = cp_build_qualified_type (*t1, quals1);
11812 *t2 = cp_build_qualified_type (*t2, quals2);
11813}
11814
11815/* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11816 constness.
11817
11818 ??? This function returns non-zero if casting away qualifiers not
11819 just const. We would like to return to the caller exactly which
11820 qualifiers are casted away to give more accurate diagnostics.
11821*/
11822
11823static bool
11824casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
11825{
11826 if (TYPE_REF_P (t2))
11827 {
11828 /* [expr.const.cast]
11829
11830 Casting from an lvalue of type T1 to an lvalue of type T2
11831 using a reference cast casts away constness if a cast from an
11832 rvalue of type "pointer to T1" to the type "pointer to T2"
11833 casts away constness. */
11834 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
11835 return casts_away_constness (t1: build_pointer_type (t1),
11836 t2: build_pointer_type (TREE_TYPE (t2)),
11837 complain);
11838 }
11839
11840 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
11841 /* [expr.const.cast]
11842
11843 Casting from an rvalue of type "pointer to data member of X
11844 of type T1" to the type "pointer to data member of Y of type
11845 T2" casts away constness if a cast from an rvalue of type
11846 "pointer to T1" to the type "pointer to T2" casts away
11847 constness. */
11848 return casts_away_constness
11849 (t1: build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
11850 t2: build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
11851 complain);
11852
11853 /* Casting away constness is only something that makes sense for
11854 pointer or reference types. */
11855 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
11856 return false;
11857
11858 /* Top-level qualifiers don't matter. */
11859 t1 = TYPE_MAIN_VARIANT (t1);
11860 t2 = TYPE_MAIN_VARIANT (t2);
11861 casts_away_constness_r (t1: &t1, t2: &t2, complain);
11862 if (!can_convert (t2, t1, complain))
11863 return true;
11864
11865 return false;
11866}
11867
11868/* If T is a REFERENCE_TYPE return the type to which T refers.
11869 Otherwise, return T itself. */
11870
11871tree
11872non_reference (tree t)
11873{
11874 if (t && TYPE_REF_P (t))
11875 t = TREE_TYPE (t);
11876 return t;
11877}
11878
11879
11880/* Return nonzero if REF is an lvalue valid for this language;
11881 otherwise, print an error message and return zero. USE says
11882 how the lvalue is being used and so selects the error message. */
11883
11884int
11885lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
11886{
11887 cp_lvalue_kind kind = lvalue_kind (ref);
11888
11889 if (kind == clk_none)
11890 {
11891 if (complain & tf_error)
11892 lvalue_error (cp_expr_loc_or_input_loc (t: ref), use);
11893 return 0;
11894 }
11895 else if (kind & (clk_rvalueref|clk_class))
11896 {
11897 if (!(complain & tf_error))
11898 return 0;
11899 /* Make this a permerror because we used to accept it. */
11900 permerror (cp_expr_loc_or_input_loc (t: ref),
11901 "using rvalue as lvalue");
11902 }
11903 return 1;
11904}
11905
11906/* Return true if a user-defined literal operator is a raw operator. */
11907
11908bool
11909check_raw_literal_operator (const_tree decl)
11910{
11911 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11912 tree argtype;
11913 int arity;
11914 bool maybe_raw_p = false;
11915
11916 /* Count the number and type of arguments and check for ellipsis. */
11917 for (argtype = argtypes, arity = 0;
11918 argtype && argtype != void_list_node;
11919 ++arity, argtype = TREE_CHAIN (argtype))
11920 {
11921 tree t = TREE_VALUE (argtype);
11922
11923 if (same_type_p (t, const_string_type_node))
11924 maybe_raw_p = true;
11925 }
11926 if (!argtype)
11927 return false; /* Found ellipsis. */
11928
11929 if (!maybe_raw_p || arity != 1)
11930 return false;
11931
11932 return true;
11933}
11934
11935
11936/* Return true if a user-defined literal operator has one of the allowed
11937 argument types. */
11938
11939bool
11940check_literal_operator_args (const_tree decl,
11941 bool *long_long_unsigned_p, bool *long_double_p)
11942{
11943 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11944
11945 *long_long_unsigned_p = false;
11946 *long_double_p = false;
11947 if (processing_template_decl || processing_specialization)
11948 return argtypes == void_list_node;
11949 else
11950 {
11951 tree argtype;
11952 int arity;
11953 int max_arity = 2;
11954
11955 /* Count the number and type of arguments and check for ellipsis. */
11956 for (argtype = argtypes, arity = 0;
11957 argtype && argtype != void_list_node;
11958 argtype = TREE_CHAIN (argtype))
11959 {
11960 tree t = TREE_VALUE (argtype);
11961 ++arity;
11962
11963 if (TYPE_PTR_P (t))
11964 {
11965 bool maybe_raw_p = false;
11966 t = TREE_TYPE (t);
11967 if (cp_type_quals (type: t) != TYPE_QUAL_CONST)
11968 return false;
11969 t = TYPE_MAIN_VARIANT (t);
11970 if ((maybe_raw_p = same_type_p (t, char_type_node))
11971 || same_type_p (t, wchar_type_node)
11972 || same_type_p (t, char8_type_node)
11973 || same_type_p (t, char16_type_node)
11974 || same_type_p (t, char32_type_node))
11975 {
11976 argtype = TREE_CHAIN (argtype);
11977 if (!argtype)
11978 return false;
11979 t = TREE_VALUE (argtype);
11980 if (maybe_raw_p && argtype == void_list_node)
11981 return true;
11982 else if (same_type_p (t, size_type_node))
11983 {
11984 ++arity;
11985 continue;
11986 }
11987 else
11988 return false;
11989 }
11990 }
11991 else if (same_type_p (t, long_long_unsigned_type_node))
11992 {
11993 max_arity = 1;
11994 *long_long_unsigned_p = true;
11995 }
11996 else if (same_type_p (t, long_double_type_node))
11997 {
11998 max_arity = 1;
11999 *long_double_p = true;
12000 }
12001 else if (same_type_p (t, char_type_node))
12002 max_arity = 1;
12003 else if (same_type_p (t, wchar_type_node))
12004 max_arity = 1;
12005 else if (same_type_p (t, char8_type_node))
12006 max_arity = 1;
12007 else if (same_type_p (t, char16_type_node))
12008 max_arity = 1;
12009 else if (same_type_p (t, char32_type_node))
12010 max_arity = 1;
12011 else
12012 return false;
12013 }
12014 if (!argtype)
12015 return false; /* Found ellipsis. */
12016
12017 if (arity != max_arity)
12018 return false;
12019
12020 return true;
12021 }
12022}
12023
12024/* Always returns false since unlike C90, C++ has no concept of implicit
12025 function declarations. */
12026
12027bool
12028c_decl_implicit (const_tree)
12029{
12030 return false;
12031}
12032

source code of gcc/cp/typeck.cc