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