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