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