Debian Codegraph @

gcc/cp/typeck.cc

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