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