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