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