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