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