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