testsuite: pragma-optimize.c requires ifunc.
[official-gcc.git] / gcc / cp / typeck.c
blob5184b02d3e4ee8bd71826a746e28f188e4e4f084
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
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.
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.
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/>. */
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. */
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"
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);
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. */
73 tree
74 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
76 tree type;
78 if (processing_template_decl || value == error_mark_node)
79 return value;
81 if (TREE_CODE (value) == OVERLOAD)
82 type = unknown_type_node;
83 else
84 type = TREE_TYPE (value);
86 if (type == error_mark_node)
87 return error_mark_node;
89 /* First, detect a valid value with a complete type. */
90 if (COMPLETE_TYPE_P (type))
91 return value;
93 if (complete_type_or_maybe_complain (type, value, complain))
94 return value;
95 else
96 return error_mark_node;
99 tree
100 require_complete_type (tree value)
102 return require_complete_type_sfinae (value, tf_warning_or_error);
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. */
110 tree
111 complete_type (tree type)
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;
118 if (type == error_mark_node || COMPLETE_TYPE_P (type))
120 else if (TREE_CODE (type) == ARRAY_TYPE)
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))
132 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
133 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
136 else if (CLASS_TYPE_P (type))
138 if (modules_p ())
139 /* TYPE could be a class member we've not loaded the definition of. */
140 lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
142 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
143 instantiate_class_template (TYPE_MAIN_VARIANT (type));
146 return type;
149 /* Like complete_type, but issue an error if the TYPE cannot be completed.
150 VALUE is used for informative diagnostics.
151 Returns NULL_TREE if the type cannot be made complete. */
153 tree
154 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
156 type = complete_type (type);
157 if (type == error_mark_node)
158 /* We already issued an error. */
159 return NULL_TREE;
160 else if (!COMPLETE_TYPE_P (type))
162 if (complain & tf_error)
163 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
164 note_failed_type_completion_for_satisfaction (type);
165 return NULL_TREE;
167 else
168 return type;
171 tree
172 complete_type_or_else (tree type, tree value)
174 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
178 /* Return the common type of two parameter lists.
179 We assume that comptypes has already been done and returned 1;
180 if that isn't so, this may crash.
182 As an optimization, free the space we allocate if the parameter
183 lists are already common. */
185 static tree
186 commonparms (tree p1, tree p2)
188 tree oldargs = p1, newargs, n;
189 int i, len;
190 int any_change = 0;
192 len = list_length (p1);
193 newargs = tree_last (p1);
195 if (newargs == void_list_node)
196 i = 1;
197 else
199 i = 0;
200 newargs = 0;
203 for (; i < len; i++)
204 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
206 n = newargs;
208 for (i = 0; p1;
209 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
211 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
213 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
214 any_change = 1;
216 else if (! TREE_PURPOSE (p1))
218 if (TREE_PURPOSE (p2))
220 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 any_change = 1;
224 else
226 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
227 any_change = 1;
228 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
230 if (TREE_VALUE (p1) != TREE_VALUE (p2))
232 any_change = 1;
233 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
235 else
236 TREE_VALUE (n) = TREE_VALUE (p1);
238 if (! any_change)
239 return oldargs;
241 return newargs;
244 /* Given a type, perhaps copied for a typedef,
245 find the "original" version of it. */
246 static tree
247 original_type (tree t)
249 int quals = cp_type_quals (t);
250 while (t != error_mark_node
251 && TYPE_NAME (t) != NULL_TREE)
253 tree x = TYPE_NAME (t);
254 if (TREE_CODE (x) != TYPE_DECL)
255 break;
256 x = DECL_ORIGINAL_TYPE (x);
257 if (x == NULL_TREE)
258 break;
259 t = x;
261 return cp_build_qualified_type (t, quals);
264 /* Merge the attributes of type OTHER_TYPE into the attributes of type TYPE
265 and return a variant of TYPE with the merged attributes. */
267 static tree
268 merge_type_attributes_from (tree type, tree other_type)
270 tree attrs = targetm.merge_type_attributes (type, other_type);
271 attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
272 return cp_build_type_attribute_variant (type, attrs);
275 /* Return the common type for two arithmetic types T1 and T2 under the
276 usual arithmetic conversions. The default conversions have already
277 been applied, and enumerated types converted to their compatible
278 integer types. */
280 static tree
281 cp_common_type (tree t1, tree t2)
283 enum tree_code code1 = TREE_CODE (t1);
284 enum tree_code code2 = TREE_CODE (t2);
285 tree attributes;
286 int i;
289 /* In what follows, we slightly generalize the rules given in [expr] so
290 as to deal with `long long' and `complex'. First, merge the
291 attributes. */
292 attributes = (*targetm.merge_type_attributes) (t1, t2);
294 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
296 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
297 return build_type_attribute_variant (t1, attributes);
298 else
299 return NULL_TREE;
302 /* FIXME: Attributes. */
303 gcc_assert (ARITHMETIC_TYPE_P (t1)
304 || VECTOR_TYPE_P (t1)
305 || UNSCOPED_ENUM_P (t1));
306 gcc_assert (ARITHMETIC_TYPE_P (t2)
307 || VECTOR_TYPE_P (t2)
308 || UNSCOPED_ENUM_P (t2));
310 /* If one type is complex, form the common type of the non-complex
311 components, then make that complex. Use T1 or T2 if it is the
312 required type. */
313 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
315 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
316 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
317 tree subtype
318 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
320 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
321 return build_type_attribute_variant (t1, attributes);
322 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
323 return build_type_attribute_variant (t2, attributes);
324 else
325 return build_type_attribute_variant (build_complex_type (subtype),
326 attributes);
329 if (code1 == VECTOR_TYPE)
331 /* When we get here we should have two vectors of the same size.
332 Just prefer the unsigned one if present. */
333 if (TYPE_UNSIGNED (t1))
334 return merge_type_attributes_from (t1, t2);
335 else
336 return merge_type_attributes_from (t2, t1);
339 /* If only one is real, use it as the result. */
340 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
341 return build_type_attribute_variant (t1, attributes);
342 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
343 return build_type_attribute_variant (t2, attributes);
345 /* Both real or both integers; use the one with greater precision. */
346 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
347 return build_type_attribute_variant (t1, attributes);
348 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
349 return build_type_attribute_variant (t2, attributes);
351 /* The types are the same; no need to do anything fancy. */
352 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
353 return build_type_attribute_variant (t1, attributes);
355 if (code1 != REAL_TYPE)
357 /* If one is unsigned long long, then convert the other to unsigned
358 long long. */
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
361 return build_type_attribute_variant (long_long_unsigned_type_node,
362 attributes);
363 /* If one is a long long, and the other is an unsigned long, and
364 long long can represent all the values of an unsigned long, then
365 convert to a long long. Otherwise, convert to an unsigned long
366 long. Otherwise, if either operand is long long, convert the
367 other to long long.
369 Since we're here, we know the TYPE_PRECISION is the same;
370 therefore converting to long long cannot represent all the values
371 of an unsigned long, so we choose unsigned long long in that
372 case. */
373 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
374 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
376 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
377 ? long_long_unsigned_type_node
378 : long_long_integer_type_node);
379 return build_type_attribute_variant (t, attributes);
382 /* Go through the same procedure, but for longs. */
383 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
384 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
385 return build_type_attribute_variant (long_unsigned_type_node,
386 attributes);
387 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
388 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
390 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
391 ? long_unsigned_type_node : long_integer_type_node);
392 return build_type_attribute_variant (t, attributes);
395 /* For __intN types, either the type is __int128 (and is lower
396 priority than the types checked above, but higher than other
397 128-bit types) or it's known to not be the same size as other
398 types (enforced in toplev.c). Prefer the unsigned type. */
399 for (i = 0; i < NUM_INT_N_ENTS; i ++)
401 if (int_n_enabled_p [i]
402 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
403 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
404 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
407 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
408 ? int_n_trees[i].unsigned_type
409 : int_n_trees[i].signed_type);
410 return build_type_attribute_variant (t, attributes);
414 /* Otherwise prefer the unsigned one. */
415 if (TYPE_UNSIGNED (t1))
416 return build_type_attribute_variant (t1, attributes);
417 else
418 return build_type_attribute_variant (t2, attributes);
420 else
422 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
423 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
424 return build_type_attribute_variant (long_double_type_node,
425 attributes);
426 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
427 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
428 return build_type_attribute_variant (double_type_node,
429 attributes);
430 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
431 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
432 return build_type_attribute_variant (float_type_node,
433 attributes);
435 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
436 the standard C++ floating-point types. Logic earlier in this
437 function has already eliminated the possibility that
438 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
439 compelling reason to choose one or the other. */
440 return build_type_attribute_variant (t1, attributes);
444 /* T1 and T2 are arithmetic or enumeration types. Return the type
445 that will result from the "usual arithmetic conversions" on T1 and
446 T2 as described in [expr]. */
448 tree
449 type_after_usual_arithmetic_conversions (tree t1, tree t2)
451 gcc_assert (ARITHMETIC_TYPE_P (t1)
452 || VECTOR_TYPE_P (t1)
453 || UNSCOPED_ENUM_P (t1));
454 gcc_assert (ARITHMETIC_TYPE_P (t2)
455 || VECTOR_TYPE_P (t2)
456 || UNSCOPED_ENUM_P (t2));
458 /* Perform the integral promotions. We do not promote real types here. */
459 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
460 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
462 t1 = type_promotes_to (t1);
463 t2 = type_promotes_to (t2);
466 return cp_common_type (t1, t2);
469 static void
470 composite_pointer_error (const op_location_t &location,
471 diagnostic_t kind, tree t1, tree t2,
472 composite_pointer_operation operation)
474 switch (operation)
476 case CPO_COMPARISON:
477 emit_diagnostic (kind, location, 0,
478 "comparison between "
479 "distinct pointer types %qT and %qT lacks a cast",
480 t1, t2);
481 break;
482 case CPO_CONVERSION:
483 emit_diagnostic (kind, location, 0,
484 "conversion between "
485 "distinct pointer types %qT and %qT lacks a cast",
486 t1, t2);
487 break;
488 case CPO_CONDITIONAL_EXPR:
489 emit_diagnostic (kind, location, 0,
490 "conditional expression between "
491 "distinct pointer types %qT and %qT lacks a cast",
492 t1, t2);
493 break;
494 default:
495 gcc_unreachable ();
499 /* Subroutine of composite_pointer_type to implement the recursive
500 case. See that function for documentation of the parameters. And ADD_CONST
501 is used to track adding "const" where needed. */
503 static tree
504 composite_pointer_type_r (const op_location_t &location,
505 tree t1, tree t2, bool *add_const,
506 composite_pointer_operation operation,
507 tsubst_flags_t complain)
509 tree pointee1;
510 tree pointee2;
511 tree result_type;
512 tree attributes;
514 /* Determine the types pointed to by T1 and T2. */
515 if (TYPE_PTR_P (t1))
517 pointee1 = TREE_TYPE (t1);
518 pointee2 = TREE_TYPE (t2);
520 else
522 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
523 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
526 /* [expr.type]
528 If T1 and T2 are similar types, the result is the cv-combined type of
529 T1 and T2. */
530 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
531 result_type = pointee1;
532 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
533 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
535 result_type = composite_pointer_type_r (location, pointee1, pointee2,
536 add_const, operation, complain);
537 if (result_type == error_mark_node)
538 return error_mark_node;
540 else
542 if (complain & tf_error)
543 composite_pointer_error (location, DK_PERMERROR,
544 t1, t2, operation);
545 else
546 return error_mark_node;
547 result_type = void_type_node;
549 const int q1 = cp_type_quals (pointee1);
550 const int q2 = cp_type_quals (pointee2);
551 const int quals = q1 | q2;
552 result_type = cp_build_qualified_type (result_type,
553 (quals | (*add_const
554 ? TYPE_QUAL_CONST
555 : TYPE_UNQUALIFIED)));
556 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
557 the TLQ). The reason is that both T1 and T2 can then be converted to the
558 cv-combined type of T1 and T2. */
559 if (quals != q1 || quals != q2)
560 *add_const = true;
561 /* If the original types were pointers to members, so is the
562 result. */
563 if (TYPE_PTRMEM_P (t1))
565 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
566 TYPE_PTRMEM_CLASS_TYPE (t2)))
568 if (complain & tf_error)
569 composite_pointer_error (location, DK_PERMERROR,
570 t1, t2, operation);
571 else
572 return error_mark_node;
574 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
575 result_type);
577 else
578 result_type = build_pointer_type (result_type);
580 /* Merge the attributes. */
581 attributes = (*targetm.merge_type_attributes) (t1, t2);
582 return build_type_attribute_variant (result_type, attributes);
585 /* Return the composite pointer type (see [expr.type]) for T1 and T2.
586 ARG1 and ARG2 are the values with those types. The OPERATION is to
587 describe the operation between the pointer types,
588 in case an error occurs.
590 This routine also implements the computation of a common type for
591 pointers-to-members as per [expr.eq]. */
593 tree
594 composite_pointer_type (const op_location_t &location,
595 tree t1, tree t2, tree arg1, tree arg2,
596 composite_pointer_operation operation,
597 tsubst_flags_t complain)
599 tree class1;
600 tree class2;
602 /* [expr.type]
604 If one operand is a null pointer constant, the composite pointer
605 type is the type of the other operand. */
606 if (null_ptr_cst_p (arg1))
607 return t2;
608 if (null_ptr_cst_p (arg2))
609 return t1;
611 /* We have:
613 [expr.type]
615 If one of the operands has type "pointer to cv1 void", then
616 the other has type "pointer to cv2 T", and the composite pointer
617 type is "pointer to cv12 void", where cv12 is the union of cv1
618 and cv2.
620 If either type is a pointer to void, make sure it is T1. */
621 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
622 std::swap (t1, t2);
624 /* Now, if T1 is a pointer to void, merge the qualifiers. */
625 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
627 tree attributes;
628 tree result_type;
630 if (TYPE_PTRFN_P (t2))
632 if (complain & tf_error)
634 switch (operation)
636 case CPO_COMPARISON:
637 pedwarn (location, OPT_Wpedantic,
638 "ISO C++ forbids comparison between pointer "
639 "of type %<void *%> and pointer-to-function");
640 break;
641 case CPO_CONVERSION:
642 pedwarn (location, OPT_Wpedantic,
643 "ISO C++ forbids conversion between pointer "
644 "of type %<void *%> and pointer-to-function");
645 break;
646 case CPO_CONDITIONAL_EXPR:
647 pedwarn (location, OPT_Wpedantic,
648 "ISO C++ forbids conditional expression between "
649 "pointer of type %<void *%> and "
650 "pointer-to-function");
651 break;
652 default:
653 gcc_unreachable ();
656 else
657 return error_mark_node;
659 result_type
660 = cp_build_qualified_type (void_type_node,
661 (cp_type_quals (TREE_TYPE (t1))
662 | cp_type_quals (TREE_TYPE (t2))));
663 result_type = build_pointer_type (result_type);
664 /* Merge the attributes. */
665 attributes = (*targetm.merge_type_attributes) (t1, t2);
666 return build_type_attribute_variant (result_type, attributes);
669 if (c_dialect_objc () && TYPE_PTR_P (t1)
670 && TYPE_PTR_P (t2))
672 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
673 return objc_common_type (t1, t2);
676 /* if T1 or T2 is "pointer to noexcept function" and the other type is
677 "pointer to function", where the function types are otherwise the same,
678 "pointer to function" */
679 if (fnptr_conv_p (t1, t2))
680 return t1;
681 if (fnptr_conv_p (t2, t1))
682 return t2;
684 /* [expr.eq] permits the application of a pointer conversion to
685 bring the pointers to a common type. */
686 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
687 && CLASS_TYPE_P (TREE_TYPE (t1))
688 && CLASS_TYPE_P (TREE_TYPE (t2))
689 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
690 TREE_TYPE (t2)))
692 class1 = TREE_TYPE (t1);
693 class2 = TREE_TYPE (t2);
695 if (DERIVED_FROM_P (class1, class2))
696 t2 = (build_pointer_type
697 (cp_build_qualified_type (class1, cp_type_quals (class2))));
698 else if (DERIVED_FROM_P (class2, class1))
699 t1 = (build_pointer_type
700 (cp_build_qualified_type (class2, cp_type_quals (class1))));
701 else
703 if (complain & tf_error)
704 composite_pointer_error (location, DK_ERROR, t1, t2, operation);
705 return error_mark_node;
708 /* [expr.eq] permits the application of a pointer-to-member
709 conversion to change the class type of one of the types. */
710 else if (TYPE_PTRMEM_P (t1)
711 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
712 TYPE_PTRMEM_CLASS_TYPE (t2)))
714 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
715 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
717 if (DERIVED_FROM_P (class1, class2))
718 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
719 else if (DERIVED_FROM_P (class2, class1))
720 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
721 else
723 if (complain & tf_error)
724 switch (operation)
726 case CPO_COMPARISON:
727 error_at (location, "comparison between distinct "
728 "pointer-to-member types %qT and %qT lacks a cast",
729 t1, t2);
730 break;
731 case CPO_CONVERSION:
732 error_at (location, "conversion between distinct "
733 "pointer-to-member types %qT and %qT lacks a cast",
734 t1, t2);
735 break;
736 case CPO_CONDITIONAL_EXPR:
737 error_at (location, "conditional expression between distinct "
738 "pointer-to-member types %qT and %qT lacks a cast",
739 t1, t2);
740 break;
741 default:
742 gcc_unreachable ();
744 return error_mark_node;
748 bool add_const = false;
749 return composite_pointer_type_r (location, t1, t2, &add_const, operation,
750 complain);
753 /* Return the merged type of two types.
754 We assume that comptypes has already been done and returned 1;
755 if that isn't so, this may crash.
757 This just combines attributes and default arguments; any other
758 differences would cause the two types to compare unalike. */
760 tree
761 merge_types (tree t1, tree t2)
763 enum tree_code code1;
764 enum tree_code code2;
765 tree attributes;
767 /* Save time if the two types are the same. */
768 if (t1 == t2)
769 return t1;
770 if (original_type (t1) == original_type (t2))
771 return t1;
773 /* If one type is nonsense, use the other. */
774 if (t1 == error_mark_node)
775 return t2;
776 if (t2 == error_mark_node)
777 return t1;
779 /* Handle merging an auto redeclaration with a previous deduced
780 return type. */
781 if (is_auto (t1))
782 return t2;
784 /* Merge the attributes. */
785 attributes = (*targetm.merge_type_attributes) (t1, t2);
787 if (TYPE_PTRMEMFUNC_P (t1))
788 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
789 if (TYPE_PTRMEMFUNC_P (t2))
790 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
792 code1 = TREE_CODE (t1);
793 code2 = TREE_CODE (t2);
794 if (code1 != code2)
796 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
797 if (code1 == TYPENAME_TYPE)
799 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
800 code1 = TREE_CODE (t1);
802 else
804 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
805 code2 = TREE_CODE (t2);
809 switch (code1)
811 case POINTER_TYPE:
812 case REFERENCE_TYPE:
813 /* For two pointers, do this recursively on the target type. */
815 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
816 int quals = cp_type_quals (t1);
818 if (code1 == POINTER_TYPE)
820 t1 = build_pointer_type (target);
821 if (TREE_CODE (target) == METHOD_TYPE)
822 t1 = build_ptrmemfunc_type (t1);
824 else
825 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
826 t1 = build_type_attribute_variant (t1, attributes);
827 t1 = cp_build_qualified_type (t1, quals);
829 return t1;
832 case OFFSET_TYPE:
834 int quals;
835 tree pointee;
836 quals = cp_type_quals (t1);
837 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
838 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
839 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
840 pointee);
841 t1 = cp_build_qualified_type (t1, quals);
842 break;
845 case ARRAY_TYPE:
847 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
848 /* Save space: see if the result is identical to one of the args. */
849 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
850 return build_type_attribute_variant (t1, attributes);
851 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
852 return build_type_attribute_variant (t2, attributes);
853 /* Merge the element types, and have a size if either arg has one. */
854 t1 = build_cplus_array_type
855 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
856 break;
859 case FUNCTION_TYPE:
860 /* Function types: prefer the one that specified arg types.
861 If both do, merge the arg types. Also merge the return types. */
863 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
864 tree p1 = TYPE_ARG_TYPES (t1);
865 tree p2 = TYPE_ARG_TYPES (t2);
866 tree parms;
868 /* Save space: see if the result is identical to one of the args. */
869 if (valtype == TREE_TYPE (t1) && ! p2)
870 return cp_build_type_attribute_variant (t1, attributes);
871 if (valtype == TREE_TYPE (t2) && ! p1)
872 return cp_build_type_attribute_variant (t2, attributes);
874 /* Simple way if one arg fails to specify argument types. */
875 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
876 parms = p2;
877 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
878 parms = p1;
879 else
880 parms = commonparms (p1, p2);
882 cp_cv_quals quals = type_memfn_quals (t1);
883 cp_ref_qualifier rqual = type_memfn_rqual (t1);
884 gcc_assert (quals == type_memfn_quals (t2));
885 gcc_assert (rqual == type_memfn_rqual (t2));
887 tree rval = build_function_type (valtype, parms);
888 rval = apply_memfn_quals (rval, quals);
889 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
890 TYPE_RAISES_EXCEPTIONS (t2));
891 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
892 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
893 break;
896 case METHOD_TYPE:
898 /* Get this value the long way, since TYPE_METHOD_BASETYPE
899 is just the main variant of this. */
900 tree basetype = class_of_this_parm (t2);
901 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
902 TYPE_RAISES_EXCEPTIONS (t2));
903 cp_ref_qualifier rqual = type_memfn_rqual (t1);
904 tree t3;
905 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
907 /* If this was a member function type, get back to the
908 original type of type member function (i.e., without
909 the class instance variable up front. */
910 t1 = build_function_type (TREE_TYPE (t1),
911 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
912 t2 = build_function_type (TREE_TYPE (t2),
913 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
914 t3 = merge_types (t1, t2);
915 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
916 TYPE_ARG_TYPES (t3));
917 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
918 break;
921 case TYPENAME_TYPE:
922 /* There is no need to merge attributes into a TYPENAME_TYPE.
923 When the type is instantiated it will have whatever
924 attributes result from the instantiation. */
925 return t1;
927 default:;
928 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
929 return t1;
930 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
931 return t2;
932 break;
935 return cp_build_type_attribute_variant (t1, attributes);
938 /* Return the ARRAY_TYPE type without its domain. */
940 tree
941 strip_array_domain (tree type)
943 tree t2;
944 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
945 if (TYPE_DOMAIN (type) == NULL_TREE)
946 return type;
947 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
948 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
951 /* Wrapper around cp_common_type that is used by c-common.c and other
952 front end optimizations that remove promotions.
954 Return the common type for two arithmetic types T1 and T2 under the
955 usual arithmetic conversions. The default conversions have already
956 been applied, and enumerated types converted to their compatible
957 integer types. */
959 tree
960 common_type (tree t1, tree t2)
962 /* If one type is nonsense, use the other */
963 if (t1 == error_mark_node)
964 return t2;
965 if (t2 == error_mark_node)
966 return t1;
968 return cp_common_type (t1, t2);
971 /* Return the common type of two pointer types T1 and T2. This is the
972 type for the result of most arithmetic operations if the operands
973 have the given two types.
975 We assume that comp_target_types has already been done and returned
976 nonzero; if that isn't so, this may crash. */
978 tree
979 common_pointer_type (tree t1, tree t2)
981 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
982 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
983 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
985 return composite_pointer_type (input_location, t1, t2,
986 error_mark_node, error_mark_node,
987 CPO_CONVERSION, tf_warning_or_error);
990 /* Compare two exception specifier types for exactness or subsetness, if
991 allowed. Returns false for mismatch, true for match (same, or
992 derived and !exact).
994 [except.spec] "If a class X ... objects of class X or any class publicly
995 and unambiguously derived from X. Similarly, if a pointer type Y * ...
996 exceptions of type Y * or that are pointers to any type publicly and
997 unambiguously derived from Y. Otherwise a function only allows exceptions
998 that have the same type ..."
999 This does not mention cv qualifiers and is different to what throw
1000 [except.throw] and catch [except.catch] will do. They will ignore the
1001 top level cv qualifiers, and allow qualifiers in the pointer to class
1002 example.
1004 We implement the letter of the standard. */
1006 static bool
1007 comp_except_types (tree a, tree b, bool exact)
1009 if (same_type_p (a, b))
1010 return true;
1011 else if (!exact)
1013 if (cp_type_quals (a) || cp_type_quals (b))
1014 return false;
1016 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1018 a = TREE_TYPE (a);
1019 b = TREE_TYPE (b);
1020 if (cp_type_quals (a) || cp_type_quals (b))
1021 return false;
1024 if (TREE_CODE (a) != RECORD_TYPE
1025 || TREE_CODE (b) != RECORD_TYPE)
1026 return false;
1028 if (publicly_uniquely_derived_p (a, b))
1029 return true;
1031 return false;
1034 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1035 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1036 If EXACT is ce_type, the C++17 type compatibility rules apply.
1037 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1038 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1039 are unordered, but we've already filtered out duplicates. Most lists will
1040 be in order, we should try to make use of that. */
1042 bool
1043 comp_except_specs (const_tree t1, const_tree t2, int exact)
1045 const_tree probe;
1046 const_tree base;
1047 int length = 0;
1049 if (t1 == t2)
1050 return true;
1052 /* First handle noexcept. */
1053 if (exact < ce_exact)
1055 if (exact == ce_type
1056 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1057 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1058 return true;
1060 /* noexcept(false) is compatible with no exception-specification,
1061 and less strict than any spec. */
1062 if (t1 == noexcept_false_spec)
1063 return t2 == NULL_TREE || exact == ce_derived;
1064 /* Even a derived noexcept(false) is compatible with no
1065 exception-specification. */
1066 if (t2 == noexcept_false_spec)
1067 return t1 == NULL_TREE;
1069 /* Otherwise, if we aren't looking for an exact match, noexcept is
1070 equivalent to throw(). */
1071 if (t1 == noexcept_true_spec)
1072 t1 = empty_except_spec;
1073 if (t2 == noexcept_true_spec)
1074 t2 = empty_except_spec;
1077 /* If any noexcept is left, it is only comparable to itself;
1078 either we're looking for an exact match or we're redeclaring a
1079 template with dependent noexcept. */
1080 if ((t1 && TREE_PURPOSE (t1))
1081 || (t2 && TREE_PURPOSE (t2)))
1082 return (t1 && t2
1083 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1085 if (t1 == NULL_TREE) /* T1 is ... */
1086 return t2 == NULL_TREE || exact == ce_derived;
1087 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1088 return t2 != NULL_TREE && !TREE_VALUE (t2);
1089 if (t2 == NULL_TREE) /* T2 is ... */
1090 return false;
1091 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1092 return exact == ce_derived;
1094 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1095 Count how many we find, to determine exactness. For exact matching and
1096 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1097 O(nm). */
1098 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1100 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1102 tree a = TREE_VALUE (probe);
1103 tree b = TREE_VALUE (t2);
1105 if (comp_except_types (a, b, exact))
1107 if (probe == base && exact > ce_derived)
1108 base = TREE_CHAIN (probe);
1109 length++;
1110 break;
1113 if (probe == NULL_TREE)
1114 return false;
1116 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1119 /* Compare the array types T1 and T2. CB says how we should behave when
1120 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1121 bounds_either says than any array can be [], bounds_first means that
1122 onlt T1 can be an array with unknown bounds. STRICT is true if
1123 qualifiers must match when comparing the types of the array elements. */
1125 static bool
1126 comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1127 bool strict)
1129 tree d1;
1130 tree d2;
1131 tree max1, max2;
1133 if (t1 == t2)
1134 return true;
1136 /* The type of the array elements must be the same. */
1137 if (strict
1138 ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1139 : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1140 return false;
1142 d1 = TYPE_DOMAIN (t1);
1143 d2 = TYPE_DOMAIN (t2);
1145 if (d1 == d2)
1146 return true;
1148 /* If one of the arrays is dimensionless, and the other has a
1149 dimension, they are of different types. However, it is valid to
1150 write:
1152 extern int a[];
1153 int a[3];
1155 by [basic.link]:
1157 declarations for an array object can specify
1158 array types that differ by the presence or absence of a major
1159 array bound (_dcl.array_). */
1160 if (!d1 && d2)
1161 return cb >= bounds_either;
1162 else if (d1 && !d2)
1163 return cb == bounds_either;
1165 /* Check that the dimensions are the same. */
1167 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1168 return false;
1169 max1 = TYPE_MAX_VALUE (d1);
1170 max2 = TYPE_MAX_VALUE (d2);
1172 if (!cp_tree_equal (max1, max2))
1173 return false;
1175 return true;
1178 /* Compare the relative position of T1 and T2 into their respective
1179 template parameter list.
1180 T1 and T2 must be template parameter types.
1181 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1183 static bool
1184 comp_template_parms_position (tree t1, tree t2)
1186 tree index1, index2;
1187 gcc_assert (t1 && t2
1188 && TREE_CODE (t1) == TREE_CODE (t2)
1189 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1190 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1191 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1193 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1194 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1196 /* Then compare their relative position. */
1197 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1198 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1199 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1200 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1201 return false;
1203 /* In C++14 we can end up comparing 'auto' to a normal template
1204 parameter. Don't confuse them. */
1205 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1206 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1208 return true;
1211 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1213 static bool
1214 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1216 t1 = TYPE_MAIN_VARIANT (t1);
1217 t2 = TYPE_MAIN_VARIANT (t2);
1219 if (TYPE_PTR_P (t1)
1220 && TYPE_PTR_P (t2))
1221 return true;
1223 /* The signedness of the parameter matters only when an integral
1224 type smaller than int is promoted to int, otherwise only the
1225 precision of the parameter matters.
1226 This check should make sure that the callee does not see
1227 undefined values in argument registers. */
1228 if (INTEGRAL_TYPE_P (t1)
1229 && INTEGRAL_TYPE_P (t2)
1230 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1231 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1232 || !targetm.calls.promote_prototypes (NULL_TREE)
1233 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1234 return true;
1236 return same_type_p (t1, t2);
1239 /* Check if a type cast between two function types can be considered safe. */
1241 static bool
1242 cxx_safe_function_type_cast_p (tree t1, tree t2)
1244 if (TREE_TYPE (t1) == void_type_node &&
1245 TYPE_ARG_TYPES (t1) == void_list_node)
1246 return true;
1248 if (TREE_TYPE (t2) == void_type_node &&
1249 TYPE_ARG_TYPES (t2) == void_list_node)
1250 return true;
1252 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1253 return false;
1255 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1256 t1 && t2;
1257 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1258 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1259 return false;
1261 return true;
1264 /* Subroutine in comptypes. */
1266 static bool
1267 structural_comptypes (tree t1, tree t2, int strict)
1269 /* Both should be types that are not obviously the same. */
1270 gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1272 /* Suppress typename resolution under spec_hasher::equal in place of calling
1273 push_to_top_level there. */
1274 if (!comparing_specializations)
1276 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1277 current instantiation. */
1278 if (TREE_CODE (t1) == TYPENAME_TYPE)
1279 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1281 if (TREE_CODE (t2) == TYPENAME_TYPE)
1282 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1285 if (TYPE_PTRMEMFUNC_P (t1))
1286 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1287 if (TYPE_PTRMEMFUNC_P (t2))
1288 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1290 /* Different classes of types can't be compatible. */
1291 if (TREE_CODE (t1) != TREE_CODE (t2))
1292 return false;
1294 /* Qualifiers must match. For array types, we will check when we
1295 recur on the array element types. */
1296 if (TREE_CODE (t1) != ARRAY_TYPE
1297 && cp_type_quals (t1) != cp_type_quals (t2))
1298 return false;
1299 if (TREE_CODE (t1) == FUNCTION_TYPE
1300 && type_memfn_quals (t1) != type_memfn_quals (t2))
1301 return false;
1302 /* Need to check this before TYPE_MAIN_VARIANT.
1303 FIXME function qualifiers should really change the main variant. */
1304 if (FUNC_OR_METHOD_TYPE_P (t1))
1306 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1307 return false;
1308 if (flag_noexcept_type
1309 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1310 TYPE_RAISES_EXCEPTIONS (t2),
1311 ce_type))
1312 return false;
1315 /* Allow for two different type nodes which have essentially the same
1316 definition. Note that we already checked for equality of the type
1317 qualifiers (just above). */
1318 if (TREE_CODE (t1) != ARRAY_TYPE
1319 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1320 goto check_alias;
1322 /* Compare the types. Return false on known not-same. Break on not
1323 known. Never return true from this switch -- you'll break
1324 specialization comparison. */
1325 switch (TREE_CODE (t1))
1327 case VOID_TYPE:
1328 case BOOLEAN_TYPE:
1329 /* All void and bool types are the same. */
1330 break;
1332 case OPAQUE_TYPE:
1333 case INTEGER_TYPE:
1334 case FIXED_POINT_TYPE:
1335 case REAL_TYPE:
1336 /* With these nodes, we can't determine type equivalence by
1337 looking at what is stored in the nodes themselves, because
1338 two nodes might have different TYPE_MAIN_VARIANTs but still
1339 represent the same type. For example, wchar_t and int could
1340 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1341 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1342 and are distinct types. On the other hand, int and the
1343 following typedef
1345 typedef int INT __attribute((may_alias));
1347 have identical properties, different TYPE_MAIN_VARIANTs, but
1348 represent the same type. The canonical type system keeps
1349 track of equivalence in this case, so we fall back on it. */
1350 if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1351 return false;
1353 /* We don't need or want the attribute comparison. */
1354 goto check_alias;
1356 case TEMPLATE_TEMPLATE_PARM:
1357 case BOUND_TEMPLATE_TEMPLATE_PARM:
1358 if (!comp_template_parms_position (t1, t2))
1359 return false;
1360 if (!comp_template_parms
1361 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1362 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1363 return false;
1364 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1365 break;
1366 /* Don't check inheritance. */
1367 strict = COMPARE_STRICT;
1368 /* Fall through. */
1370 case RECORD_TYPE:
1371 case UNION_TYPE:
1372 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1373 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1374 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1375 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1376 break;
1378 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1379 break;
1380 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1381 break;
1383 return false;
1385 case OFFSET_TYPE:
1386 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1387 strict & ~COMPARE_REDECLARATION))
1388 return false;
1389 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1390 return false;
1391 break;
1393 case REFERENCE_TYPE:
1394 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1395 return false;
1396 /* fall through to checks for pointer types */
1397 gcc_fallthrough ();
1399 case POINTER_TYPE:
1400 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1401 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1402 return false;
1403 break;
1405 case METHOD_TYPE:
1406 case FUNCTION_TYPE:
1407 /* Exception specs and memfn_rquals were checked above. */
1408 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1409 return false;
1410 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1411 return false;
1412 break;
1414 case ARRAY_TYPE:
1415 /* Target types must match incl. qualifiers. */
1416 if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1417 ? bounds_either : bounds_none),
1418 /*strict=*/true))
1419 return false;
1420 break;
1422 case TEMPLATE_TYPE_PARM:
1423 /* If T1 and T2 don't have the same relative position in their
1424 template parameters set, they can't be equal. */
1425 if (!comp_template_parms_position (t1, t2))
1426 return false;
1427 /* If T1 and T2 don't represent the same class template deduction,
1428 they aren't equal. */
1429 if (CLASS_PLACEHOLDER_TEMPLATE (t1)
1430 != CLASS_PLACEHOLDER_TEMPLATE (t2))
1431 return false;
1432 /* Constrained 'auto's are distinct from parms that don't have the same
1433 constraints. */
1434 if (!equivalent_placeholder_constraints (t1, t2))
1435 return false;
1436 break;
1438 case TYPENAME_TYPE:
1439 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1440 TYPENAME_TYPE_FULLNAME (t2)))
1441 return false;
1442 /* Qualifiers don't matter on scopes. */
1443 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1444 TYPE_CONTEXT (t2)))
1445 return false;
1446 break;
1448 case UNBOUND_CLASS_TEMPLATE:
1449 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1450 return false;
1451 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1452 return false;
1453 break;
1455 case COMPLEX_TYPE:
1456 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1457 return false;
1458 break;
1460 case VECTOR_TYPE:
1461 if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1462 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1463 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1464 return false;
1465 break;
1467 case TYPE_PACK_EXPANSION:
1468 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1469 PACK_EXPANSION_PATTERN (t2))
1470 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1471 PACK_EXPANSION_EXTRA_ARGS (t2)));
1473 case DECLTYPE_TYPE:
1474 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1475 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1476 return false;
1477 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1478 return false;
1479 if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1480 return false;
1481 if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1482 return false;
1483 break;
1485 case UNDERLYING_TYPE:
1486 if (!same_type_p (UNDERLYING_TYPE_TYPE (t1), UNDERLYING_TYPE_TYPE (t2)))
1487 return false;
1488 break;
1490 case TYPEOF_TYPE:
1491 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1492 return false;
1493 break;
1495 default:
1496 return false;
1499 /* If we get here, we know that from a target independent POV the
1500 types are the same. Make sure the target attributes are also
1501 the same. */
1502 if (!comp_type_attributes (t1, t2))
1503 return false;
1505 check_alias:
1506 if (comparing_dependent_aliases)
1508 /* Don't treat an alias template specialization with dependent
1509 arguments as equivalent to its underlying type when used as a
1510 template argument; we need them to be distinct so that we
1511 substitute into the specialization arguments at instantiation
1512 time. And aliases can't be equivalent without being ==, so
1513 we don't need to look any deeper. */
1514 tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1515 tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1516 if ((dep1 || dep2) && dep1 != dep2)
1517 return false;
1520 return true;
1523 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1524 is a bitwise-or of the COMPARE_* flags. */
1526 bool
1527 comptypes (tree t1, tree t2, int strict)
1529 gcc_checking_assert (t1 && t2);
1531 /* TYPE_ARGUMENT_PACKS are not really types. */
1532 gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1533 && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1535 if (t1 == t2)
1536 return true;
1538 /* Suppress errors caused by previously reported errors. */
1539 if (t1 == error_mark_node || t2 == error_mark_node)
1540 return false;
1542 if (strict == COMPARE_STRICT)
1544 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1545 /* At least one of the types requires structural equality, so
1546 perform a deep check. */
1547 return structural_comptypes (t1, t2, strict);
1549 if (flag_checking && param_use_canonical_types)
1551 bool result = structural_comptypes (t1, t2, strict);
1553 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1554 /* The two types are structurally equivalent, but their
1555 canonical types were different. This is a failure of the
1556 canonical type propagation code.*/
1557 internal_error
1558 ("canonical types differ for identical types %qT and %qT",
1559 t1, t2);
1560 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1561 /* Two types are structurally different, but the canonical
1562 types are the same. This means we were over-eager in
1563 assigning canonical types. */
1564 internal_error
1565 ("same canonical type node for different types %qT and %qT",
1566 t1, t2);
1568 return result;
1570 if (!flag_checking && param_use_canonical_types)
1571 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1572 else
1573 return structural_comptypes (t1, t2, strict);
1575 else if (strict == COMPARE_STRUCTURAL)
1576 return structural_comptypes (t1, t2, COMPARE_STRICT);
1577 else
1578 return structural_comptypes (t1, t2, strict);
1581 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1582 top-level qualifiers. */
1584 bool
1585 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1587 if (type1 == error_mark_node || type2 == error_mark_node)
1588 return false;
1589 if (type1 == type2)
1590 return true;
1592 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1593 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1594 return same_type_p (type1, type2);
1597 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1599 bool
1600 similar_type_p (tree type1, tree type2)
1602 if (type1 == error_mark_node || type2 == error_mark_node)
1603 return false;
1605 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1606 * they are the same type; or
1607 * they are both pointers, and the pointed-to types are similar; or
1608 * they are both pointers to member of the same class, and the types of
1609 the pointed-to members are similar; or
1610 * they are both arrays of the same size or both arrays of unknown bound,
1611 and the array element types are similar. */
1613 if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1614 return true;
1616 if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1617 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1618 || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1619 return comp_ptr_ttypes_const (type1, type2, bounds_either);
1621 return false;
1624 /* Helper function for layout_compatible_type_p and
1625 is_corresponding_member_aggr. Advance to next members (NULL if
1626 no further ones) and return true if those members are still part of
1627 the common initial sequence. */
1629 bool
1630 next_common_initial_seqence (tree &memb1, tree &memb2)
1632 while (memb1)
1634 if (TREE_CODE (memb1) != FIELD_DECL
1635 || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1637 memb1 = DECL_CHAIN (memb1);
1638 continue;
1640 if (DECL_FIELD_IS_BASE (memb1))
1642 memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1643 continue;
1645 break;
1647 while (memb2)
1649 if (TREE_CODE (memb2) != FIELD_DECL
1650 || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1652 memb2 = DECL_CHAIN (memb2);
1653 continue;
1655 if (DECL_FIELD_IS_BASE (memb2))
1657 memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1658 continue;
1660 break;
1662 if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1663 return true;
1664 if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1665 return false;
1666 if (DECL_BIT_FIELD_TYPE (memb1))
1668 if (!DECL_BIT_FIELD_TYPE (memb2))
1669 return false;
1670 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1671 DECL_BIT_FIELD_TYPE (memb2)))
1672 return false;
1673 if (TYPE_PRECISION (TREE_TYPE (memb1))
1674 != TYPE_PRECISION (TREE_TYPE (memb2)))
1675 return false;
1677 else if (DECL_BIT_FIELD_TYPE (memb2))
1678 return false;
1679 else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1680 return false;
1681 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1682 != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1683 return false;
1684 if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1685 return false;
1686 return true;
1689 /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1691 bool
1692 layout_compatible_type_p (tree type1, tree type2)
1694 if (type1 == error_mark_node || type2 == error_mark_node)
1695 return false;
1696 if (type1 == type2)
1697 return true;
1698 if (TREE_CODE (type1) != TREE_CODE (type2))
1699 return false;
1701 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1702 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1704 if (TREE_CODE (type1) == ENUMERAL_TYPE)
1705 return (TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
1706 && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1707 && same_type_p (finish_underlying_type (type1),
1708 finish_underlying_type (type2)));
1710 if (CLASS_TYPE_P (type1)
1711 && std_layout_type_p (type1)
1712 && std_layout_type_p (type2)
1713 && TYPE_ALIGN (type1) == TYPE_ALIGN (type2)
1714 && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1716 tree field1 = TYPE_FIELDS (type1);
1717 tree field2 = TYPE_FIELDS (type2);
1718 if (TREE_CODE (type1) == RECORD_TYPE)
1720 while (1)
1722 if (!next_common_initial_seqence (field1, field2))
1723 return false;
1724 if (field1 == NULL_TREE)
1725 return true;
1726 field1 = DECL_CHAIN (field1);
1727 field2 = DECL_CHAIN (field2);
1730 /* Otherwise both types must be union types.
1731 The standard says:
1732 "Two standard-layout unions are layout-compatible if they have
1733 the same number of non-static data members and corresponding
1734 non-static data members (in any order) have layout-compatible
1735 types."
1736 but the code anticipates that bitfield vs. non-bitfield,
1737 different bitfield widths or presence/absence of
1738 [[no_unique_address]] should be checked as well. */
1739 auto_vec<tree, 16> vec;
1740 unsigned int count = 0;
1741 for (; field1; field1 = DECL_CHAIN (field1))
1742 if (TREE_CODE (field1) == FIELD_DECL)
1743 count++;
1744 for (; field2; field2 = DECL_CHAIN (field2))
1745 if (TREE_CODE (field2) == FIELD_DECL)
1746 vec.safe_push (field2);
1747 /* Discussions on core lean towards treating multiple union fields
1748 of the same type as the same field, so this might need changing
1749 in the future. */
1750 if (count != vec.length ())
1751 return false;
1752 for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1754 if (TREE_CODE (field1) != FIELD_DECL)
1755 continue;
1756 unsigned int j;
1757 tree t1 = DECL_BIT_FIELD_TYPE (field1);
1758 if (t1 == NULL_TREE)
1759 t1 = TREE_TYPE (field1);
1760 FOR_EACH_VEC_ELT (vec, j, field2)
1762 tree t2 = DECL_BIT_FIELD_TYPE (field2);
1763 if (t2 == NULL_TREE)
1764 t2 = TREE_TYPE (field2);
1765 if (DECL_BIT_FIELD_TYPE (field1))
1767 if (!DECL_BIT_FIELD_TYPE (field2))
1768 continue;
1769 if (TYPE_PRECISION (TREE_TYPE (field1))
1770 != TYPE_PRECISION (TREE_TYPE (field2)))
1771 continue;
1773 else if (DECL_BIT_FIELD_TYPE (field2))
1774 continue;
1775 if (!layout_compatible_type_p (t1, t2))
1776 continue;
1777 if ((!lookup_attribute ("no_unique_address",
1778 DECL_ATTRIBUTES (field1)))
1779 != !lookup_attribute ("no_unique_address",
1780 DECL_ATTRIBUTES (field2)))
1781 continue;
1782 break;
1784 if (j == vec.length ())
1785 return false;
1786 vec.unordered_remove (j);
1788 return true;
1791 return same_type_p (type1, type2);
1794 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1796 bool
1797 at_least_as_qualified_p (const_tree type1, const_tree type2)
1799 int q1 = cp_type_quals (type1);
1800 int q2 = cp_type_quals (type2);
1802 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1803 return (q1 & q2) == q2;
1806 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1807 more cv-qualified that TYPE1, and 0 otherwise. */
1810 comp_cv_qualification (int q1, int q2)
1812 if (q1 == q2)
1813 return 0;
1815 if ((q1 & q2) == q2)
1816 return 1;
1817 else if ((q1 & q2) == q1)
1818 return -1;
1820 return 0;
1824 comp_cv_qualification (const_tree type1, const_tree type2)
1826 int q1 = cp_type_quals (type1);
1827 int q2 = cp_type_quals (type2);
1828 return comp_cv_qualification (q1, q2);
1831 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1832 subset of the cv-qualification signature of TYPE2, and the types
1833 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1836 comp_cv_qual_signature (tree type1, tree type2)
1838 if (comp_ptr_ttypes_real (type2, type1, -1))
1839 return 1;
1840 else if (comp_ptr_ttypes_real (type1, type2, -1))
1841 return -1;
1842 else
1843 return 0;
1846 /* Subroutines of `comptypes'. */
1848 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1849 equivalent in the sense that functions with those parameter types
1850 can have equivalent types. The two lists must be equivalent,
1851 element by element. */
1853 bool
1854 compparms (const_tree parms1, const_tree parms2)
1856 const_tree t1, t2;
1858 /* An unspecified parmlist matches any specified parmlist
1859 whose argument types don't need default promotions. */
1861 for (t1 = parms1, t2 = parms2;
1862 t1 || t2;
1863 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1865 /* If one parmlist is shorter than the other,
1866 they fail to match. */
1867 if (!t1 || !t2)
1868 return false;
1869 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1870 return false;
1872 return true;
1876 /* Process a sizeof or alignof expression where the operand is a
1877 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1878 or GNU (preferred alignment) semantics; it is ignored if op is
1879 SIZEOF_EXPR. */
1881 tree
1882 cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
1883 bool std_alignof, bool complain)
1885 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1886 if (type == error_mark_node)
1887 return error_mark_node;
1889 type = non_reference (type);
1890 if (TREE_CODE (type) == METHOD_TYPE)
1892 if (complain)
1894 pedwarn (loc, OPT_Wpointer_arith,
1895 "invalid application of %qs to a member function",
1896 OVL_OP_INFO (false, op)->name);
1897 return size_one_node;
1899 else
1900 return error_mark_node;
1903 bool dependent_p = dependent_type_p (type);
1904 if (!dependent_p)
1905 complete_type (type);
1906 if (dependent_p
1907 /* VLA types will have a non-constant size. In the body of an
1908 uninstantiated template, we don't need to try to compute the
1909 value, because the sizeof expression is not an integral
1910 constant expression in that case. And, if we do try to
1911 compute the value, we'll likely end up with SAVE_EXPRs, which
1912 the template substitution machinery does not expect to see. */
1913 || (processing_template_decl
1914 && COMPLETE_TYPE_P (type)
1915 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1917 tree value = build_min (op, size_type_node, type);
1918 TREE_READONLY (value) = 1;
1919 if (op == ALIGNOF_EXPR && std_alignof)
1920 ALIGNOF_EXPR_STD_P (value) = true;
1921 SET_EXPR_LOCATION (value, loc);
1922 return value;
1925 return c_sizeof_or_alignof_type (loc, complete_type (type),
1926 op == SIZEOF_EXPR, std_alignof,
1927 complain);
1930 /* Return the size of the type, without producing any warnings for
1931 types whose size cannot be taken. This routine should be used only
1932 in some other routine that has already produced a diagnostic about
1933 using the size of such a type. */
1934 tree
1935 cxx_sizeof_nowarn (tree type)
1937 if (TREE_CODE (type) == FUNCTION_TYPE
1938 || VOID_TYPE_P (type)
1939 || TREE_CODE (type) == ERROR_MARK)
1940 return size_one_node;
1941 else if (!COMPLETE_TYPE_P (type))
1942 return size_zero_node;
1943 else
1944 return cxx_sizeof_or_alignof_type (input_location, type,
1945 SIZEOF_EXPR, false, false);
1948 /* Process a sizeof expression where the operand is an expression. */
1950 static tree
1951 cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
1953 if (e == error_mark_node)
1954 return error_mark_node;
1956 if (instantiation_dependent_uneval_expression_p (e))
1958 e = build_min (SIZEOF_EXPR, size_type_node, e);
1959 TREE_SIDE_EFFECTS (e) = 0;
1960 TREE_READONLY (e) = 1;
1961 SET_EXPR_LOCATION (e, loc);
1963 return e;
1966 location_t e_loc = cp_expr_loc_or_loc (e, loc);
1967 STRIP_ANY_LOCATION_WRAPPER (e);
1969 /* To get the size of a static data member declared as an array of
1970 unknown bound, we need to instantiate it. */
1971 if (VAR_P (e)
1972 && VAR_HAD_UNKNOWN_BOUND (e)
1973 && DECL_TEMPLATE_INSTANTIATION (e))
1974 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1976 if (TREE_CODE (e) == PARM_DECL
1977 && DECL_ARRAY_PARAMETER_P (e)
1978 && (complain & tf_warning))
1980 auto_diagnostic_group d;
1981 if (warning_at (e_loc, OPT_Wsizeof_array_argument,
1982 "%<sizeof%> on array function parameter %qE "
1983 "will return size of %qT", e, TREE_TYPE (e)))
1984 inform (DECL_SOURCE_LOCATION (e), "declared here");
1987 e = mark_type_use (e);
1989 if (bitfield_p (e))
1991 if (complain & tf_error)
1992 error_at (e_loc,
1993 "invalid application of %<sizeof%> to a bit-field");
1994 else
1995 return error_mark_node;
1996 e = char_type_node;
1998 else if (is_overloaded_fn (e))
2000 if (complain & tf_error)
2001 permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2002 "an expression of function type");
2003 else
2004 return error_mark_node;
2005 e = char_type_node;
2007 else if (type_unknown_p (e))
2009 if (complain & tf_error)
2010 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2011 else
2012 return error_mark_node;
2013 e = char_type_node;
2015 else
2016 e = TREE_TYPE (e);
2018 return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2019 complain & tf_error);
2022 /* Implement the __alignof keyword: Return the minimum required
2023 alignment of E, measured in bytes. For VAR_DECL's and
2024 FIELD_DECL's return DECL_ALIGN (which can be set from an
2025 "aligned" __attribute__ specification). STD_ALIGNOF acts
2026 like in cxx_sizeof_or_alignof_type. */
2028 static tree
2029 cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2030 tsubst_flags_t complain)
2032 tree t;
2034 if (e == error_mark_node)
2035 return error_mark_node;
2037 if (processing_template_decl)
2039 e = build_min (ALIGNOF_EXPR, size_type_node, e);
2040 TREE_SIDE_EFFECTS (e) = 0;
2041 TREE_READONLY (e) = 1;
2042 SET_EXPR_LOCATION (e, loc);
2043 ALIGNOF_EXPR_STD_P (e) = std_alignof;
2045 return e;
2048 location_t e_loc = cp_expr_loc_or_loc (e, loc);
2049 STRIP_ANY_LOCATION_WRAPPER (e);
2051 e = mark_type_use (e);
2053 if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2054 !(complain & tf_error)))
2056 if (!(complain & tf_error))
2057 return error_mark_node;
2058 t = size_one_node;
2060 else if (VAR_P (e))
2061 t = size_int (DECL_ALIGN_UNIT (e));
2062 else if (bitfield_p (e))
2064 if (complain & tf_error)
2065 error_at (e_loc,
2066 "invalid application of %<__alignof%> to a bit-field");
2067 else
2068 return error_mark_node;
2069 t = size_one_node;
2071 else if (TREE_CODE (e) == COMPONENT_REF
2072 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2073 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2074 else if (is_overloaded_fn (e))
2076 if (complain & tf_error)
2077 permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2078 "an expression of function type");
2079 else
2080 return error_mark_node;
2081 if (TREE_CODE (e) == FUNCTION_DECL)
2082 t = size_int (DECL_ALIGN_UNIT (e));
2083 else
2084 t = size_one_node;
2086 else if (type_unknown_p (e))
2088 if (complain & tf_error)
2089 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2090 else
2091 return error_mark_node;
2092 t = size_one_node;
2094 else
2095 return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2096 ALIGNOF_EXPR, std_alignof,
2097 complain & tf_error);
2099 return fold_convert_loc (loc, size_type_node, t);
2102 /* Process a sizeof or alignof expression E with code OP where the operand
2103 is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2105 tree
2106 cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2107 bool std_alignof, bool complain)
2109 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2110 if (op == SIZEOF_EXPR)
2111 return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2112 else
2113 return cxx_alignof_expr (loc, e, std_alignof,
2114 complain? tf_warning_or_error : tf_none);
2117 /* Build a representation of an expression 'alignas(E).' Return the
2118 folded integer value of E if it is an integral constant expression
2119 that resolves to a valid alignment. If E depends on a template
2120 parameter, return a syntactic representation tree of kind
2121 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2122 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2124 tree
2125 cxx_alignas_expr (tree e)
2127 if (e == NULL_TREE || e == error_mark_node
2128 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2129 return e;
2131 if (TYPE_P (e))
2132 /* [dcl.align]/3:
2134 When the alignment-specifier is of the form
2135 alignas(type-id ), it shall have the same effect as
2136 alignas(alignof(type-id )). */
2138 return cxx_sizeof_or_alignof_type (input_location,
2139 e, ALIGNOF_EXPR, true, false);
2141 /* If we reach this point, it means the alignas expression if of
2142 the form "alignas(assignment-expression)", so we should follow
2143 what is stated by [dcl.align]/2. */
2145 if (value_dependent_expression_p (e))
2146 /* Leave value-dependent expression alone for now. */
2147 return e;
2149 e = instantiate_non_dependent_expr (e);
2150 e = mark_rvalue_use (e);
2152 /* [dcl.align]/2 says:
2154 the assignment-expression shall be an integral constant
2155 expression. */
2157 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2159 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2160 return error_mark_node;
2163 return cxx_constant_value (e);
2167 /* EXPR is being used in a context that is not a function call.
2168 Enforce:
2170 [expr.ref]
2172 The expression can be used only as the left-hand operand of a
2173 member function call.
2175 [expr.mptr.operator]
2177 If the result of .* or ->* is a function, then that result can be
2178 used only as the operand for the function call operator ().
2180 by issuing an error message if appropriate. Returns true iff EXPR
2181 violates these rules. */
2183 bool
2184 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2186 if (expr == NULL_TREE)
2187 return false;
2188 /* Don't enforce this in MS mode. */
2189 if (flag_ms_extensions)
2190 return false;
2191 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2192 expr = get_first_fn (expr);
2193 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
2195 if (complain & tf_error)
2197 if (DECL_P (expr))
2199 error_at (loc, "invalid use of non-static member function %qD",
2200 expr);
2201 inform (DECL_SOURCE_LOCATION (expr), "declared here");
2203 else
2204 error_at (loc, "invalid use of non-static member function of "
2205 "type %qT", TREE_TYPE (expr));
2207 return true;
2209 return false;
2212 /* If EXP is a reference to a bit-field, and the type of EXP does not
2213 match the declared type of the bit-field, return the declared type
2214 of the bit-field. Otherwise, return NULL_TREE. */
2216 tree
2217 is_bitfield_expr_with_lowered_type (const_tree exp)
2219 switch (TREE_CODE (exp))
2221 case COND_EXPR:
2222 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2223 ? TREE_OPERAND (exp, 1)
2224 : TREE_OPERAND (exp, 0)))
2225 return NULL_TREE;
2226 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2228 case COMPOUND_EXPR:
2229 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2231 case MODIFY_EXPR:
2232 case SAVE_EXPR:
2233 case UNARY_PLUS_EXPR:
2234 case PREDECREMENT_EXPR:
2235 case PREINCREMENT_EXPR:
2236 case POSTDECREMENT_EXPR:
2237 case POSTINCREMENT_EXPR:
2238 case NEGATE_EXPR:
2239 case NON_LVALUE_EXPR:
2240 case BIT_NOT_EXPR:
2241 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2243 case COMPONENT_REF:
2245 tree field;
2247 field = TREE_OPERAND (exp, 1);
2248 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2249 return NULL_TREE;
2250 if (same_type_ignoring_top_level_qualifiers_p
2251 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2252 return NULL_TREE;
2253 return DECL_BIT_FIELD_TYPE (field);
2256 case VAR_DECL:
2257 if (DECL_HAS_VALUE_EXPR_P (exp))
2258 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2259 (CONST_CAST_TREE (exp)));
2260 return NULL_TREE;
2262 case VIEW_CONVERT_EXPR:
2263 if (location_wrapper_p (exp))
2264 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2265 else
2266 return NULL_TREE;
2268 default:
2269 return NULL_TREE;
2273 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2274 bitfield with a lowered type, the type of EXP is returned, rather
2275 than NULL_TREE. */
2277 tree
2278 unlowered_expr_type (const_tree exp)
2280 tree type;
2281 tree etype = TREE_TYPE (exp);
2283 type = is_bitfield_expr_with_lowered_type (exp);
2284 if (type)
2285 type = cp_build_qualified_type (type, cp_type_quals (etype));
2286 else
2287 type = etype;
2289 return type;
2292 /* Perform the conversions in [expr] that apply when an lvalue appears
2293 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2294 function-to-pointer conversions. In addition, bitfield references are
2295 converted to their declared types. Note that this function does not perform
2296 the lvalue-to-rvalue conversion for class types. If you need that conversion
2297 for class types, then you probably need to use force_rvalue.
2299 Although the returned value is being used as an rvalue, this
2300 function does not wrap the returned expression in a
2301 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2302 that the return value is no longer an lvalue. */
2304 tree
2305 decay_conversion (tree exp,
2306 tsubst_flags_t complain,
2307 bool reject_builtin /* = true */)
2309 tree type;
2310 enum tree_code code;
2311 location_t loc = cp_expr_loc_or_input_loc (exp);
2313 type = TREE_TYPE (exp);
2314 if (type == error_mark_node)
2315 return error_mark_node;
2317 exp = resolve_nondeduced_context_or_error (exp, complain);
2319 code = TREE_CODE (type);
2321 if (error_operand_p (exp))
2322 return error_mark_node;
2324 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2326 mark_rvalue_use (exp, loc, reject_builtin);
2327 return nullptr_node;
2330 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2331 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2332 if (code == VOID_TYPE)
2334 if (complain & tf_error)
2335 error_at (loc, "void value not ignored as it ought to be");
2336 return error_mark_node;
2338 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2339 return error_mark_node;
2340 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2342 exp = mark_lvalue_use (exp);
2343 if (reject_builtin && reject_gcc_builtin (exp, loc))
2344 return error_mark_node;
2345 return cp_build_addr_expr (exp, complain);
2347 if (code == ARRAY_TYPE)
2349 tree adr;
2350 tree ptrtype;
2352 exp = mark_lvalue_use (exp);
2354 if (INDIRECT_REF_P (exp))
2355 return build_nop (build_pointer_type (TREE_TYPE (type)),
2356 TREE_OPERAND (exp, 0));
2358 if (TREE_CODE (exp) == COMPOUND_EXPR)
2360 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2361 if (op1 == error_mark_node)
2362 return error_mark_node;
2363 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2364 TREE_OPERAND (exp, 0), op1);
2367 if (!obvalue_p (exp)
2368 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2370 if (complain & tf_error)
2371 error_at (loc, "invalid use of non-lvalue array");
2372 return error_mark_node;
2375 /* Don't let an array compound literal decay to a pointer. It can
2376 still be used to initialize an array or bind to a reference. */
2377 if (TREE_CODE (exp) == TARGET_EXPR)
2379 if (complain & tf_error)
2380 error_at (loc, "taking address of temporary array");
2381 return error_mark_node;
2384 ptrtype = build_pointer_type (TREE_TYPE (type));
2386 if (VAR_P (exp))
2388 if (!cxx_mark_addressable (exp))
2389 return error_mark_node;
2390 adr = build_nop (ptrtype, build_address (exp));
2391 return adr;
2393 /* This way is better for a COMPONENT_REF since it can
2394 simplify the offset for a component. */
2395 adr = cp_build_addr_expr (exp, complain);
2396 return cp_convert (ptrtype, adr, complain);
2399 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2400 exp = mark_rvalue_use (exp, loc, reject_builtin);
2402 /* If a bitfield is used in a context where integral promotion
2403 applies, then the caller is expected to have used
2404 default_conversion. That function promotes bitfields correctly
2405 before calling this function. At this point, if we have a
2406 bitfield referenced, we may assume that is not subject to
2407 promotion, and that, therefore, the type of the resulting rvalue
2408 is the declared type of the bitfield. */
2409 exp = convert_bitfield_to_declared_type (exp);
2411 /* We do not call rvalue() here because we do not want to wrap EXP
2412 in a NON_LVALUE_EXPR. */
2414 /* [basic.lval]
2416 Non-class rvalues always have cv-unqualified types. */
2417 type = TREE_TYPE (exp);
2418 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2419 exp = build_nop (cv_unqualified (type), exp);
2421 if (!complete_type_or_maybe_complain (type, exp, complain))
2422 return error_mark_node;
2424 return exp;
2427 /* Perform preparatory conversions, as part of the "usual arithmetic
2428 conversions". In particular, as per [expr]:
2430 Whenever an lvalue expression appears as an operand of an
2431 operator that expects the rvalue for that operand, the
2432 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2433 standard conversions are applied to convert the expression to an
2434 rvalue.
2436 In addition, we perform integral promotions here, as those are
2437 applied to both operands to a binary operator before determining
2438 what additional conversions should apply. */
2440 static tree
2441 cp_default_conversion (tree exp, tsubst_flags_t complain)
2443 /* Check for target-specific promotions. */
2444 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2445 if (promoted_type)
2446 exp = cp_convert (promoted_type, exp, complain);
2447 /* Perform the integral promotions first so that bitfield
2448 expressions (which may promote to "int", even if the bitfield is
2449 declared "unsigned") are promoted correctly. */
2450 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2451 exp = cp_perform_integral_promotions (exp, complain);
2452 /* Perform the other conversions. */
2453 exp = decay_conversion (exp, complain);
2455 return exp;
2458 /* C version. */
2460 tree
2461 default_conversion (tree exp)
2463 return cp_default_conversion (exp, tf_warning_or_error);
2466 /* EXPR is an expression with an integral or enumeration type.
2467 Perform the integral promotions in [conv.prom], and return the
2468 converted value. */
2470 tree
2471 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2473 tree type;
2474 tree promoted_type;
2476 expr = mark_rvalue_use (expr);
2477 if (error_operand_p (expr))
2478 return error_mark_node;
2480 type = TREE_TYPE (expr);
2482 /* [conv.prom]
2484 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2485 of type int if int can represent all the values of the bit-field;
2486 otherwise, it can be converted to unsigned int if unsigned int can
2487 represent all the values of the bit-field. If the bit-field is larger yet,
2488 no integral promotion applies to it. If the bit-field has an enumerated
2489 type, it is treated as any other value of that type for promotion
2490 purposes. */
2491 tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2492 if (bitfield_type
2493 && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2494 || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2495 type = bitfield_type;
2497 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2498 /* Scoped enums don't promote. */
2499 if (SCOPED_ENUM_P (type))
2500 return expr;
2501 promoted_type = type_promotes_to (type);
2502 if (type != promoted_type)
2503 expr = cp_convert (promoted_type, expr, complain);
2504 else if (bitfield_type && bitfield_type != type)
2505 /* Prevent decay_conversion from converting to bitfield_type. */
2506 expr = build_nop (type, expr);
2507 return expr;
2510 /* C version. */
2512 tree
2513 perform_integral_promotions (tree expr)
2515 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2518 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2519 decay_conversion to one. */
2522 string_conv_p (const_tree totype, const_tree exp, int warn)
2524 tree t;
2526 if (!TYPE_PTR_P (totype))
2527 return 0;
2529 t = TREE_TYPE (totype);
2530 if (!same_type_p (t, char_type_node)
2531 && !same_type_p (t, char8_type_node)
2532 && !same_type_p (t, char16_type_node)
2533 && !same_type_p (t, char32_type_node)
2534 && !same_type_p (t, wchar_type_node))
2535 return 0;
2537 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2539 STRIP_ANY_LOCATION_WRAPPER (exp);
2541 if (TREE_CODE (exp) == STRING_CST)
2543 /* Make sure that we don't try to convert between char and wide chars. */
2544 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2545 return 0;
2547 else
2549 /* Is this a string constant which has decayed to 'const char *'? */
2550 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2551 if (!same_type_p (TREE_TYPE (exp), t))
2552 return 0;
2553 STRIP_NOPS (exp);
2554 if (TREE_CODE (exp) != ADDR_EXPR
2555 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2556 return 0;
2558 if (warn)
2560 if (cxx_dialect >= cxx11)
2561 pedwarn (loc, OPT_Wwrite_strings,
2562 "ISO C++ forbids converting a string constant to %qT",
2563 totype);
2564 else
2565 warning_at (loc, OPT_Wwrite_strings,
2566 "deprecated conversion from string constant to %qT",
2567 totype);
2570 return 1;
2573 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2574 can, for example, use as an lvalue. This code used to be in
2575 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2576 expressions, where we're dealing with aggregates. But now it's again only
2577 called from unary_complex_lvalue. The case (in particular) that led to
2578 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2579 get it there. */
2581 static tree
2582 rationalize_conditional_expr (enum tree_code code, tree t,
2583 tsubst_flags_t complain)
2585 location_t loc = cp_expr_loc_or_input_loc (t);
2587 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2588 the first operand is always the one to be used if both operands
2589 are equal, so we know what conditional expression this used to be. */
2590 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2592 tree op0 = TREE_OPERAND (t, 0);
2593 tree op1 = TREE_OPERAND (t, 1);
2595 /* The following code is incorrect if either operand side-effects. */
2596 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2597 && !TREE_SIDE_EFFECTS (op1));
2598 return
2599 build_conditional_expr (loc,
2600 build_x_binary_op (loc,
2601 (TREE_CODE (t) == MIN_EXPR
2602 ? LE_EXPR : GE_EXPR),
2603 op0, TREE_CODE (op0),
2604 op1, TREE_CODE (op1),
2605 NULL_TREE,
2606 /*overload=*/NULL,
2607 complain),
2608 cp_build_unary_op (code, op0, false, complain),
2609 cp_build_unary_op (code, op1, false, complain),
2610 complain);
2613 tree op1 = TREE_OPERAND (t, 1);
2614 if (TREE_CODE (op1) != THROW_EXPR)
2615 op1 = cp_build_unary_op (code, op1, false, complain);
2616 tree op2 = TREE_OPERAND (t, 2);
2617 if (TREE_CODE (op2) != THROW_EXPR)
2618 op2 = cp_build_unary_op (code, op2, false, complain);
2620 return
2621 build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2624 /* Given the TYPE of an anonymous union field inside T, return the
2625 FIELD_DECL for the field. If not found return NULL_TREE. Because
2626 anonymous unions can nest, we must also search all anonymous unions
2627 that are directly reachable. */
2629 tree
2630 lookup_anon_field (tree, tree type)
2632 tree field;
2634 type = TYPE_MAIN_VARIANT (type);
2635 field = ANON_AGGR_TYPE_FIELD (type);
2636 gcc_assert (field);
2637 return field;
2640 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2641 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2642 non-NULL, it indicates the path to the base used to name MEMBER.
2643 If PRESERVE_REFERENCE is true, the expression returned will have
2644 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2645 returned will have the type referred to by the reference.
2647 This function does not perform access control; that is either done
2648 earlier by the parser when the name of MEMBER is resolved to MEMBER
2649 itself, or later when overload resolution selects one of the
2650 functions indicated by MEMBER. */
2652 tree
2653 build_class_member_access_expr (cp_expr object, tree member,
2654 tree access_path, bool preserve_reference,
2655 tsubst_flags_t complain)
2657 tree object_type;
2658 tree member_scope;
2659 tree result = NULL_TREE;
2660 tree using_decl = NULL_TREE;
2662 if (error_operand_p (object) || error_operand_p (member))
2663 return error_mark_node;
2665 gcc_assert (DECL_P (member) || BASELINK_P (member));
2667 /* [expr.ref]
2669 The type of the first expression shall be "class object" (of a
2670 complete type). */
2671 object_type = TREE_TYPE (object);
2672 if (!currently_open_class (object_type)
2673 && !complete_type_or_maybe_complain (object_type, object, complain))
2674 return error_mark_node;
2675 if (!CLASS_TYPE_P (object_type))
2677 if (complain & tf_error)
2679 if (INDIRECT_TYPE_P (object_type)
2680 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2681 error ("request for member %qD in %qE, which is of pointer "
2682 "type %qT (maybe you meant to use %<->%> ?)",
2683 member, object.get_value (), object_type);
2684 else
2685 error ("request for member %qD in %qE, which is of non-class "
2686 "type %qT", member, object.get_value (), object_type);
2688 return error_mark_node;
2691 /* The standard does not seem to actually say that MEMBER must be a
2692 member of OBJECT_TYPE. However, that is clearly what is
2693 intended. */
2694 if (DECL_P (member))
2696 member_scope = DECL_CLASS_CONTEXT (member);
2697 if (!mark_used (member, complain) && !(complain & tf_error))
2698 return error_mark_node;
2700 if (TREE_UNAVAILABLE (member))
2701 error_unavailable_use (member, NULL_TREE);
2702 else if (TREE_DEPRECATED (member))
2703 warn_deprecated_use (member, NULL_TREE);
2705 else
2706 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2707 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2708 presently be the anonymous union. Go outwards until we find a
2709 type related to OBJECT_TYPE. */
2710 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2711 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2712 object_type))
2713 member_scope = TYPE_CONTEXT (member_scope);
2714 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2716 if (complain & tf_error)
2718 if (TREE_CODE (member) == FIELD_DECL)
2719 error ("invalid use of non-static data member %qE", member);
2720 else
2721 error ("%qD is not a member of %qT", member, object_type);
2723 return error_mark_node;
2726 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2727 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2728 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2730 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2731 if (temp)
2733 temp = cp_build_fold_indirect_ref (temp);
2734 if (xvalue_p (object) && !xvalue_p (temp))
2735 /* Preserve xvalue kind. */
2736 temp = move (temp);
2737 object = temp;
2741 /* In [expr.ref], there is an explicit list of the valid choices for
2742 MEMBER. We check for each of those cases here. */
2743 if (VAR_P (member))
2745 /* A static data member. */
2746 result = member;
2747 mark_exp_read (object);
2749 if (tree wrap = maybe_get_tls_wrapper_call (result))
2750 /* Replace an evaluated use of the thread_local variable with
2751 a call to its wrapper. */
2752 result = wrap;
2754 /* If OBJECT has side-effects, they are supposed to occur. */
2755 if (TREE_SIDE_EFFECTS (object))
2756 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2758 else if (TREE_CODE (member) == FIELD_DECL)
2760 /* A non-static data member. */
2761 bool null_object_p;
2762 int type_quals;
2763 tree member_type;
2765 if (INDIRECT_REF_P (object))
2766 null_object_p =
2767 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2768 else
2769 null_object_p = false;
2771 /* Convert OBJECT to the type of MEMBER. */
2772 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2773 TYPE_MAIN_VARIANT (member_scope)))
2775 tree binfo;
2776 base_kind kind;
2778 /* We didn't complain above about a currently open class, but now we
2779 must: we don't know how to refer to a base member before layout is
2780 complete. But still don't complain in a template. */
2781 if (!cp_unevaluated_operand
2782 && !dependent_type_p (object_type)
2783 && !complete_type_or_maybe_complain (object_type, object,
2784 complain))
2785 return error_mark_node;
2787 binfo = lookup_base (access_path ? access_path : object_type,
2788 member_scope, ba_unique, &kind, complain);
2789 if (binfo == error_mark_node)
2790 return error_mark_node;
2792 /* It is invalid to try to get to a virtual base of a
2793 NULL object. The most common cause is invalid use of
2794 offsetof macro. */
2795 if (null_object_p && kind == bk_via_virtual)
2797 if (complain & tf_error)
2799 error ("invalid access to non-static data member %qD in "
2800 "virtual base of NULL object", member);
2802 return error_mark_node;
2805 /* Convert to the base. */
2806 object = build_base_path (PLUS_EXPR, object, binfo,
2807 /*nonnull=*/1, complain);
2808 /* If we found the base successfully then we should be able
2809 to convert to it successfully. */
2810 gcc_assert (object != error_mark_node);
2813 /* If MEMBER is from an anonymous aggregate, we have converted
2814 OBJECT so that it refers to the class containing the
2815 anonymous union. Generate a reference to the anonymous union
2816 itself, and recur to find MEMBER. */
2817 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2818 /* When this code is called from build_field_call, the
2819 object already has the type of the anonymous union.
2820 That is because the COMPONENT_REF was already
2821 constructed, and was then disassembled before calling
2822 build_field_call. After the function-call code is
2823 cleaned up, this waste can be eliminated. */
2824 && (!same_type_ignoring_top_level_qualifiers_p
2825 (TREE_TYPE (object), DECL_CONTEXT (member))))
2827 tree anonymous_union;
2829 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2830 DECL_CONTEXT (member));
2831 object = build_class_member_access_expr (object,
2832 anonymous_union,
2833 /*access_path=*/NULL_TREE,
2834 preserve_reference,
2835 complain);
2838 /* Compute the type of the field, as described in [expr.ref]. */
2839 type_quals = TYPE_UNQUALIFIED;
2840 member_type = TREE_TYPE (member);
2841 if (!TYPE_REF_P (member_type))
2843 type_quals = (cp_type_quals (member_type)
2844 | cp_type_quals (object_type));
2846 /* A field is const (volatile) if the enclosing object, or the
2847 field itself, is const (volatile). But, a mutable field is
2848 not const, even within a const object. */
2849 if (DECL_MUTABLE_P (member))
2850 type_quals &= ~TYPE_QUAL_CONST;
2851 member_type = cp_build_qualified_type (member_type, type_quals);
2854 result = build3_loc (input_location, COMPONENT_REF, member_type,
2855 object, member, NULL_TREE);
2857 /* Mark the expression const or volatile, as appropriate. Even
2858 though we've dealt with the type above, we still have to mark the
2859 expression itself. */
2860 if (type_quals & TYPE_QUAL_CONST)
2861 TREE_READONLY (result) = 1;
2862 if (type_quals & TYPE_QUAL_VOLATILE)
2863 TREE_THIS_VOLATILE (result) = 1;
2865 else if (BASELINK_P (member))
2867 /* The member is a (possibly overloaded) member function. */
2868 tree functions;
2869 tree type;
2871 /* If the MEMBER is exactly one static member function, then we
2872 know the type of the expression. Otherwise, we must wait
2873 until overload resolution has been performed. */
2874 functions = BASELINK_FUNCTIONS (member);
2875 if (TREE_CODE (functions) == FUNCTION_DECL
2876 && DECL_STATIC_FUNCTION_P (functions))
2877 type = TREE_TYPE (functions);
2878 else
2879 type = unknown_type_node;
2880 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2881 base. That will happen when the function is called. */
2882 result = build3_loc (input_location, COMPONENT_REF, type, object, member,
2883 NULL_TREE);
2885 else if (TREE_CODE (member) == CONST_DECL)
2887 /* The member is an enumerator. */
2888 result = member;
2889 /* If OBJECT has side-effects, they are supposed to occur. */
2890 if (TREE_SIDE_EFFECTS (object))
2891 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2892 object, result);
2894 else if ((using_decl = strip_using_decl (member)) != member)
2895 result = build_class_member_access_expr (object,
2896 using_decl,
2897 access_path, preserve_reference,
2898 complain);
2899 else
2901 if (complain & tf_error)
2902 error ("invalid use of %qD", member);
2903 return error_mark_node;
2906 if (!preserve_reference)
2907 /* [expr.ref]
2909 If E2 is declared to have type "reference to T", then ... the
2910 type of E1.E2 is T. */
2911 result = convert_from_reference (result);
2913 return result;
2916 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2917 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2919 tree
2920 lookup_destructor (tree object, tree scope, tree dtor_name,
2921 tsubst_flags_t complain)
2923 tree object_type = TREE_TYPE (object);
2924 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2925 tree expr;
2927 /* We've already complained about this destructor. */
2928 if (dtor_type == error_mark_node)
2929 return error_mark_node;
2931 if (scope && !check_dtor_name (scope, dtor_type))
2933 if (complain & tf_error)
2934 error ("qualified type %qT does not match destructor name ~%qT",
2935 scope, dtor_type);
2936 return error_mark_node;
2938 if (is_auto (dtor_type))
2939 dtor_type = object_type;
2940 else if (identifier_p (dtor_type))
2942 /* In a template, names we can't find a match for are still accepted
2943 destructor names, and we check them here. */
2944 if (check_dtor_name (object_type, dtor_type))
2945 dtor_type = object_type;
2946 else
2948 if (complain & tf_error)
2949 error ("object type %qT does not match destructor name ~%qT",
2950 object_type, dtor_type);
2951 return error_mark_node;
2955 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2957 if (complain & tf_error)
2958 error ("the type being destroyed is %qT, but the destructor "
2959 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2960 return error_mark_node;
2962 expr = lookup_member (dtor_type, complete_dtor_identifier,
2963 /*protect=*/1, /*want_type=*/false,
2964 tf_warning_or_error);
2965 if (!expr)
2967 if (complain & tf_error)
2968 cxx_incomplete_type_error (dtor_name, dtor_type);
2969 return error_mark_node;
2971 expr = (adjust_result_of_qualified_name_lookup
2972 (expr, dtor_type, object_type));
2973 if (scope == NULL_TREE)
2974 /* We need to call adjust_result_of_qualified_name_lookup in case the
2975 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2976 that we still get virtual function binding. */
2977 BASELINK_QUALIFIED_P (expr) = false;
2978 return expr;
2981 /* An expression of the form "A::template B" has been resolved to
2982 DECL. Issue a diagnostic if B is not a template or template
2983 specialization. */
2985 void
2986 check_template_keyword (tree decl)
2988 /* The standard says:
2990 [temp.names]
2992 If a name prefixed by the keyword template is not a member
2993 template, the program is ill-formed.
2995 DR 228 removed the restriction that the template be a member
2996 template.
2998 DR 96, if accepted would add the further restriction that explicit
2999 template arguments must be provided if the template keyword is
3000 used, but, as of 2005-10-16, that DR is still in "drafting". If
3001 this DR is accepted, then the semantic checks here can be
3002 simplified, as the entity named must in fact be a template
3003 specialization, rather than, as at present, a set of overloaded
3004 functions containing at least one template function. */
3005 if (TREE_CODE (decl) != TEMPLATE_DECL
3006 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3008 if (VAR_P (decl))
3010 if (DECL_USE_TEMPLATE (decl)
3011 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3013 else
3014 permerror (input_location, "%qD is not a template", decl);
3016 else if (!is_overloaded_fn (decl))
3017 permerror (input_location, "%qD is not a template", decl);
3018 else
3020 bool found = false;
3022 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3023 !found && iter; ++iter)
3025 tree fn = *iter;
3026 if (TREE_CODE (fn) == TEMPLATE_DECL
3027 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3028 || (TREE_CODE (fn) == FUNCTION_DECL
3029 && DECL_USE_TEMPLATE (fn)
3030 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3031 found = true;
3033 if (!found)
3034 permerror (input_location, "%qD is not a template", decl);
3039 /* Record that an access failure occurred on BASETYPE_PATH attempting
3040 to access DECL, where DIAG_DECL should be used for diagnostics. */
3042 void
3043 access_failure_info::record_access_failure (tree basetype_path,
3044 tree decl, tree diag_decl)
3046 m_was_inaccessible = true;
3047 m_basetype_path = basetype_path;
3048 m_decl = decl;
3049 m_diag_decl = diag_decl;
3052 /* If an access failure was recorded, then attempt to locate an
3053 accessor function for the pertinent field.
3054 Otherwise, return NULL_TREE. */
3056 tree
3057 access_failure_info::get_any_accessor (bool const_p) const
3059 if (!was_inaccessible_p ())
3060 return NULL_TREE;
3062 tree accessor
3063 = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3064 if (!accessor)
3065 return NULL_TREE;
3067 /* The accessor must itself be accessible for it to be a reasonable
3068 suggestion. */
3069 if (!accessible_p (m_basetype_path, accessor, true))
3070 return NULL_TREE;
3072 return accessor;
3075 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3076 replacing the primary location in RICHLOC with "accessor()". */
3078 void
3079 access_failure_info::add_fixit_hint (rich_location *richloc,
3080 tree accessor_decl)
3082 pretty_printer pp;
3083 pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3084 pp_string (&pp, "()");
3085 richloc->add_fixit_replace (pp_formatted_text (&pp));
3088 /* If an access failure was recorded, then attempt to locate an
3089 accessor function for the pertinent field, and if one is
3090 available, add a note and fix-it hint suggesting using it. */
3092 void
3093 access_failure_info::maybe_suggest_accessor (bool const_p) const
3095 tree accessor = get_any_accessor (const_p);
3096 if (accessor == NULL_TREE)
3097 return;
3098 rich_location richloc (line_table, input_location);
3099 add_fixit_hint (&richloc, accessor);
3100 inform (&richloc, "field %q#D can be accessed via %q#D",
3101 m_diag_decl, accessor);
3104 /* Subroutine of finish_class_member_access_expr.
3105 Issue an error about NAME not being a member of ACCESS_PATH (or
3106 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3107 names. */
3109 static void
3110 complain_about_unrecognized_member (tree access_path, tree name,
3111 tree object_type)
3113 /* Attempt to provide a hint about misspelled names. */
3114 tree guessed_id = lookup_member_fuzzy (access_path, name,
3115 /*want_type=*/false);
3116 if (guessed_id == NULL_TREE)
3118 /* No hint. */
3119 error ("%q#T has no member named %qE",
3120 TREE_CODE (access_path) == TREE_BINFO
3121 ? TREE_TYPE (access_path) : object_type, name);
3122 return;
3125 location_t bogus_component_loc = input_location;
3126 gcc_rich_location rich_loc (bogus_component_loc);
3128 /* Check that the guessed name is accessible along access_path. */
3129 access_failure_info afi;
3130 lookup_member (access_path, guessed_id, /*protect=*/1,
3131 /*want_type=*/false, /*complain=*/false,
3132 &afi);
3133 if (afi.was_inaccessible_p ())
3135 tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3136 if (accessor)
3138 /* The guessed name isn't directly accessible, but can be accessed
3139 via an accessor member function. */
3140 afi.add_fixit_hint (&rich_loc, accessor);
3141 error_at (&rich_loc,
3142 "%q#T has no member named %qE;"
3143 " did you mean %q#D? (accessible via %q#D)",
3144 TREE_CODE (access_path) == TREE_BINFO
3145 ? TREE_TYPE (access_path) : object_type,
3146 name, afi.get_diag_decl (), accessor);
3148 else
3150 /* The guessed name isn't directly accessible, and no accessor
3151 member function could be found. */
3152 error_at (&rich_loc,
3153 "%q#T has no member named %qE;"
3154 " did you mean %q#D? (not accessible from this context)",
3155 TREE_CODE (access_path) == TREE_BINFO
3156 ? TREE_TYPE (access_path) : object_type,
3157 name, afi.get_diag_decl ());
3158 complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3159 afi.get_diag_decl (), false, ak_none);
3162 else
3164 /* The guessed name is directly accessible; suggest it. */
3165 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3166 guessed_id);
3167 error_at (&rich_loc,
3168 "%q#T has no member named %qE;"
3169 " did you mean %qE?",
3170 TREE_CODE (access_path) == TREE_BINFO
3171 ? TREE_TYPE (access_path) : object_type,
3172 name, guessed_id);
3176 /* This function is called by the parser to process a class member
3177 access expression of the form OBJECT.NAME. NAME is a node used by
3178 the parser to represent a name; it is not yet a DECL. It may,
3179 however, be a BASELINK where the BASELINK_FUNCTIONS is a
3180 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3181 there is no reason to do the lookup twice, so the parser keeps the
3182 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3183 be a template via the use of the "A::template B" syntax. */
3185 tree
3186 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3187 tsubst_flags_t complain)
3189 tree expr;
3190 tree object_type;
3191 tree member;
3192 tree access_path = NULL_TREE;
3193 tree orig_object = object;
3194 tree orig_name = name;
3196 if (object == error_mark_node || name == error_mark_node)
3197 return error_mark_node;
3199 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3200 if (!objc_is_public (object, name))
3201 return error_mark_node;
3203 object_type = TREE_TYPE (object);
3205 if (processing_template_decl)
3207 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3208 type_dependent_object_expression_p (object)
3209 /* If NAME is "f<args>", where either 'f' or 'args' is
3210 dependent, then the expression is dependent. */
3211 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3212 && dependent_template_id_p (TREE_OPERAND (name, 0),
3213 TREE_OPERAND (name, 1)))
3214 /* If NAME is "T::X" where "T" is dependent, then the
3215 expression is dependent. */
3216 || (TREE_CODE (name) == SCOPE_REF
3217 && TYPE_P (TREE_OPERAND (name, 0))
3218 && dependent_scope_p (TREE_OPERAND (name, 0)))
3219 /* If NAME is operator T where "T" is dependent, we can't
3220 lookup until we instantiate the T. */
3221 || (TREE_CODE (name) == IDENTIFIER_NODE
3222 && IDENTIFIER_CONV_OP_P (name)
3223 && dependent_type_p (TREE_TYPE (name))))
3225 dependent:
3226 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3227 orig_object, orig_name, NULL_TREE);
3229 object = build_non_dependent_expr (object);
3231 else if (c_dialect_objc ()
3232 && identifier_p (name)
3233 && (expr = objc_maybe_build_component_ref (object, name)))
3234 return expr;
3236 /* [expr.ref]
3238 The type of the first expression shall be "class object" (of a
3239 complete type). */
3240 if (!currently_open_class (object_type)
3241 && !complete_type_or_maybe_complain (object_type, object, complain))
3242 return error_mark_node;
3243 if (!CLASS_TYPE_P (object_type))
3245 if (complain & tf_error)
3247 if (INDIRECT_TYPE_P (object_type)
3248 && CLASS_TYPE_P (TREE_TYPE (object_type)))
3249 error ("request for member %qD in %qE, which is of pointer "
3250 "type %qT (maybe you meant to use %<->%> ?)",
3251 name, object.get_value (), object_type);
3252 else
3253 error ("request for member %qD in %qE, which is of non-class "
3254 "type %qT", name, object.get_value (), object_type);
3256 return error_mark_node;
3259 if (BASELINK_P (name))
3260 /* A member function that has already been looked up. */
3261 member = name;
3262 else
3264 bool is_template_id = false;
3265 tree template_args = NULL_TREE;
3266 tree scope = NULL_TREE;
3268 access_path = object_type;
3270 if (TREE_CODE (name) == SCOPE_REF)
3272 /* A qualified name. The qualifying class or namespace `S'
3273 has already been looked up; it is either a TYPE or a
3274 NAMESPACE_DECL. */
3275 scope = TREE_OPERAND (name, 0);
3276 name = TREE_OPERAND (name, 1);
3278 /* If SCOPE is a namespace, then the qualified name does not
3279 name a member of OBJECT_TYPE. */
3280 if (TREE_CODE (scope) == NAMESPACE_DECL)
3282 if (complain & tf_error)
3283 error ("%<%D::%D%> is not a member of %qT",
3284 scope, name, object_type);
3285 return error_mark_node;
3289 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3291 is_template_id = true;
3292 template_args = TREE_OPERAND (name, 1);
3293 name = TREE_OPERAND (name, 0);
3295 if (!identifier_p (name))
3296 name = OVL_NAME (name);
3299 if (scope)
3301 if (TREE_CODE (scope) == ENUMERAL_TYPE)
3303 gcc_assert (!is_template_id);
3304 /* Looking up a member enumerator (c++/56793). */
3305 if (!TYPE_CLASS_SCOPE_P (scope)
3306 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3308 if (complain & tf_error)
3309 error ("%<%D::%D%> is not a member of %qT",
3310 scope, name, object_type);
3311 return error_mark_node;
3313 tree val = lookup_enumerator (scope, name);
3314 if (!val)
3316 if (complain & tf_error)
3317 error ("%qD is not a member of %qD",
3318 name, scope);
3319 return error_mark_node;
3322 if (TREE_SIDE_EFFECTS (object))
3323 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3324 return val;
3327 gcc_assert (CLASS_TYPE_P (scope));
3328 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3330 if (constructor_name_p (name, scope))
3332 if (complain & tf_error)
3333 error ("cannot call constructor %<%T::%D%> directly",
3334 scope, name);
3335 return error_mark_node;
3338 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3339 access_path = lookup_base (object_type, scope, ba_check,
3340 NULL, complain);
3341 if (access_path == error_mark_node)
3342 return error_mark_node;
3343 if (!access_path)
3345 if (any_dependent_bases_p (object_type))
3346 goto dependent;
3347 if (complain & tf_error)
3348 error ("%qT is not a base of %qT", scope, object_type);
3349 return error_mark_node;
3353 if (TREE_CODE (name) == BIT_NOT_EXPR)
3355 if (dependent_type_p (object_type))
3356 /* The destructor isn't declared yet. */
3357 goto dependent;
3358 member = lookup_destructor (object, scope, name, complain);
3360 else
3362 /* Look up the member. */
3363 access_failure_info afi;
3364 if (processing_template_decl)
3365 /* Even though this class member access expression is at this
3366 point not dependent, the member itself may be dependent, and
3367 we must not potentially push a access check for a dependent
3368 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3369 ahead of time here; we're going to redo this member lookup at
3370 instantiation time anyway. */
3371 push_deferring_access_checks (dk_no_check);
3372 member = lookup_member (access_path, name, /*protect=*/1,
3373 /*want_type=*/false, complain,
3374 &afi);
3375 if (processing_template_decl)
3376 pop_deferring_access_checks ();
3377 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3378 if (member == NULL_TREE)
3380 if (dependent_type_p (object_type))
3381 /* Try again at instantiation time. */
3382 goto dependent;
3383 if (complain & tf_error)
3384 complain_about_unrecognized_member (access_path, name,
3385 object_type);
3386 return error_mark_node;
3388 if (member == error_mark_node)
3389 return error_mark_node;
3390 if (DECL_P (member)
3391 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3392 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3393 wrong, so don't use it. */
3394 goto dependent;
3395 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3396 goto dependent;
3399 if (is_template_id)
3401 tree templ = member;
3403 if (BASELINK_P (templ))
3404 member = lookup_template_function (templ, template_args);
3405 else if (variable_template_p (templ))
3406 member = (lookup_and_finish_template_variable
3407 (templ, template_args, complain));
3408 else
3410 if (complain & tf_error)
3411 error ("%qD is not a member template function", name);
3412 return error_mark_node;
3417 if (TREE_UNAVAILABLE (member))
3418 error_unavailable_use (member, NULL_TREE);
3419 else if (TREE_DEPRECATED (member))
3420 warn_deprecated_use (member, NULL_TREE);
3422 if (template_p)
3423 check_template_keyword (member);
3425 expr = build_class_member_access_expr (object, member, access_path,
3426 /*preserve_reference=*/false,
3427 complain);
3428 if (processing_template_decl && expr != error_mark_node)
3430 if (BASELINK_P (member))
3432 if (TREE_CODE (orig_name) == SCOPE_REF)
3433 BASELINK_QUALIFIED_P (member) = 1;
3434 orig_name = member;
3436 return build_min_non_dep (COMPONENT_REF, expr,
3437 orig_object, orig_name,
3438 NULL_TREE);
3441 return expr;
3444 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3445 type. */
3447 tree
3448 build_simple_component_ref (tree object, tree member)
3450 tree type = cp_build_qualified_type (TREE_TYPE (member),
3451 cp_type_quals (TREE_TYPE (object)));
3452 return build3_loc (input_location,
3453 COMPONENT_REF, type,
3454 object, member, NULL_TREE);
3457 /* Return an expression for the MEMBER_NAME field in the internal
3458 representation of PTRMEM, a pointer-to-member function. (Each
3459 pointer-to-member function type gets its own RECORD_TYPE so it is
3460 more convenient to access the fields by name than by FIELD_DECL.)
3461 This routine converts the NAME to a FIELD_DECL and then creates the
3462 node for the complete expression. */
3464 tree
3465 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3467 tree ptrmem_type;
3468 tree member;
3470 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3472 for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3473 if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3474 return e.value;
3475 gcc_unreachable ();
3478 /* This code is a stripped down version of
3479 build_class_member_access_expr. It does not work to use that
3480 routine directly because it expects the object to be of class
3481 type. */
3482 ptrmem_type = TREE_TYPE (ptrmem);
3483 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3484 for (member = TYPE_FIELDS (ptrmem_type); member;
3485 member = DECL_CHAIN (member))
3486 if (DECL_NAME (member) == member_name)
3487 break;
3488 return build_simple_component_ref (ptrmem, member);
3491 /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3492 and for any other relevant operator. */
3494 static tree
3495 op_unqualified_lookup (tree_code code, bool is_assign)
3497 tree lookups = NULL_TREE;
3499 if (cxx_dialect >= cxx20 && !is_assign)
3501 if (code == NE_EXPR)
3503 /* != can get rewritten in terms of ==. */
3504 tree fnname = ovl_op_identifier (false, EQ_EXPR);
3505 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3506 lookups = tree_cons (fnname, fns, lookups);
3508 else if (code == GT_EXPR || code == LE_EXPR
3509 || code == LT_EXPR || code == GE_EXPR)
3511 /* These can get rewritten in terms of <=>. */
3512 tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3513 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3514 lookups = tree_cons (fnname, fns, lookups);
3518 tree fnname = ovl_op_identifier (is_assign, code);
3519 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3520 lookups = tree_cons (fnname, fns, lookups);
3522 if (lookups)
3523 return lookups;
3524 else
3525 return build_tree_list (NULL_TREE, NULL_TREE);
3528 /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3529 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3530 name lookup for the given operator. */
3532 tree
3533 build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3535 if (lookups)
3536 /* We're partially instantiating a dependent operator expression, and
3537 LOOKUPS is the result of phase 1 name lookup that we performed
3538 earlier at template definition time, so just reuse the corresponding
3539 DEPENDENT_OPERATOR_TYPE. */
3540 return TREE_TYPE (lookups);
3542 /* Otherwise we're processing a dependent operator expression at template
3543 definition time, so perform phase 1 name lookup now. */
3544 lookups = op_unqualified_lookup (code, is_assign);
3546 tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3547 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3548 TREE_TYPE (lookups) = type;
3549 return type;
3552 /* Given an expression PTR for a pointer, return an expression
3553 for the value pointed to.
3554 ERRORSTRING is the name of the operator to appear in error messages.
3556 This function may need to overload OPERATOR_FNNAME.
3557 Must also handle REFERENCE_TYPEs for C++. */
3559 tree
3560 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3561 tree lookups, tsubst_flags_t complain)
3563 tree orig_expr = expr;
3564 tree rval;
3565 tree overload = NULL_TREE;
3567 if (processing_template_decl)
3569 /* Retain the type if we know the operand is a pointer. */
3570 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3572 if (expr == current_class_ptr
3573 || (TREE_CODE (expr) == NOP_EXPR
3574 && TREE_OPERAND (expr, 0) == current_class_ptr
3575 && (same_type_ignoring_top_level_qualifiers_p
3576 (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3577 return current_class_ref;
3578 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3580 if (type_dependent_expression_p (expr))
3582 expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3583 TREE_TYPE (expr)
3584 = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3585 return expr;
3587 expr = build_non_dependent_expr (expr);
3590 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3591 NULL_TREE, NULL_TREE, lookups,
3592 &overload, complain);
3593 if (!rval)
3594 rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3596 if (processing_template_decl && rval != error_mark_node)
3598 if (overload != NULL_TREE)
3599 return (build_min_non_dep_op_overload
3600 (INDIRECT_REF, rval, overload, orig_expr));
3602 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3604 else
3605 return rval;
3608 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3609 types or expressions. */
3611 static bool
3612 cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3614 if (processing_template_decl)
3616 tree e = expr;
3617 STRIP_NOPS (e);
3618 if (dependent_type_p (type) || type_dependent_expression_p (e))
3619 return false;
3621 return strict_aliasing_warning (loc, type, expr);
3624 /* The implementation of the above, and of indirection implied by other
3625 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3627 static tree
3628 cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3629 tsubst_flags_t complain, bool do_fold)
3631 tree pointer, type;
3633 /* RO_NULL should only be used with the folding entry points below, not
3634 cp_build_indirect_ref. */
3635 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3637 if (ptr == current_class_ptr
3638 || (TREE_CODE (ptr) == NOP_EXPR
3639 && TREE_OPERAND (ptr, 0) == current_class_ptr
3640 && (same_type_ignoring_top_level_qualifiers_p
3641 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3642 return current_class_ref;
3644 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3645 ? ptr : decay_conversion (ptr, complain));
3646 if (pointer == error_mark_node)
3647 return error_mark_node;
3649 type = TREE_TYPE (pointer);
3651 if (INDIRECT_TYPE_P (type))
3653 /* [expr.unary.op]
3655 If the type of the expression is "pointer to T," the type
3656 of the result is "T." */
3657 tree t = TREE_TYPE (type);
3659 if ((CONVERT_EXPR_P (ptr)
3660 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3661 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3663 /* If a warning is issued, mark it to avoid duplicates from
3664 the backend. This only needs to be done at
3665 warn_strict_aliasing > 2. */
3666 if (warn_strict_aliasing > 2
3667 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3668 type, TREE_OPERAND (ptr, 0)))
3669 suppress_warning (ptr, OPT_Wstrict_aliasing);
3672 if (VOID_TYPE_P (t))
3674 /* A pointer to incomplete type (other than cv void) can be
3675 dereferenced [expr.unary.op]/1 */
3676 if (complain & tf_error)
3677 error_at (loc, "%qT is not a pointer-to-object type", type);
3678 return error_mark_node;
3680 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3681 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3682 /* The POINTER was something like `&x'. We simplify `*&x' to
3683 `x'. */
3684 return TREE_OPERAND (pointer, 0);
3685 else
3687 tree ref = build1 (INDIRECT_REF, t, pointer);
3689 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3690 so that we get the proper error message if the result is used
3691 to assign to. Also, &* is supposed to be a no-op. */
3692 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3693 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3694 TREE_SIDE_EFFECTS (ref)
3695 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3696 return ref;
3699 else if (!(complain & tf_error))
3700 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3702 /* `pointer' won't be an error_mark_node if we were given a
3703 pointer to member, so it's cool to check for this here. */
3704 else if (TYPE_PTRMEM_P (type))
3705 switch (errorstring)
3707 case RO_ARRAY_INDEXING:
3708 error_at (loc,
3709 "invalid use of array indexing on pointer to member");
3710 break;
3711 case RO_UNARY_STAR:
3712 error_at (loc, "invalid use of unary %<*%> on pointer to member");
3713 break;
3714 case RO_IMPLICIT_CONVERSION:
3715 error_at (loc, "invalid use of implicit conversion on pointer "
3716 "to member");
3717 break;
3718 case RO_ARROW_STAR:
3719 error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3720 "class, but is a pointer to member of type %qT", type);
3721 break;
3722 default:
3723 gcc_unreachable ();
3725 else if (pointer != error_mark_node)
3726 invalid_indirection_error (loc, type, errorstring);
3728 return error_mark_node;
3731 /* Entry point used by c-common, which expects folding. */
3733 tree
3734 build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3736 return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3737 tf_warning_or_error, true);
3740 /* Entry point used by internal indirection needs that don't correspond to any
3741 syntactic construct. */
3743 tree
3744 cp_build_fold_indirect_ref (tree pointer)
3746 return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3747 tf_warning_or_error, true);
3750 /* Entry point used by indirection needs that correspond to some syntactic
3751 construct. */
3753 tree
3754 cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3755 tsubst_flags_t complain)
3757 return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3760 /* This handles expressions of the form "a[i]", which denotes
3761 an array reference.
3763 This is logically equivalent in C to *(a+i), but we may do it differently.
3764 If A is a variable or a member, we generate a primitive ARRAY_REF.
3765 This avoids forcing the array out of registers, and can work on
3766 arrays that are not lvalues (for example, members of structures returned
3767 by functions).
3769 If INDEX is of some user-defined type, it must be converted to
3770 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3771 will inherit the type of the array, which will be some pointer type.
3773 LOC is the location to use in building the array reference. */
3775 tree
3776 cp_build_array_ref (location_t loc, tree array, tree idx,
3777 tsubst_flags_t complain)
3779 tree ret;
3781 if (idx == 0)
3783 if (complain & tf_error)
3784 error_at (loc, "subscript missing in array reference");
3785 return error_mark_node;
3788 if (TREE_TYPE (array) == error_mark_node
3789 || TREE_TYPE (idx) == error_mark_node)
3790 return error_mark_node;
3792 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3793 inside it. */
3794 switch (TREE_CODE (array))
3796 case COMPOUND_EXPR:
3798 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3799 complain);
3800 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3801 TREE_OPERAND (array, 0), value);
3802 SET_EXPR_LOCATION (ret, loc);
3803 return ret;
3806 case COND_EXPR:
3807 ret = build_conditional_expr
3808 (loc, TREE_OPERAND (array, 0),
3809 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3810 complain),
3811 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3812 complain),
3813 complain);
3814 protected_set_expr_location (ret, loc);
3815 return ret;
3817 default:
3818 break;
3821 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3823 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3825 tree rval, type;
3827 warn_array_subscript_with_type_char (loc, idx);
3829 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3831 if (complain & tf_error)
3832 error_at (loc, "array subscript is not an integer");
3833 return error_mark_node;
3836 /* Apply integral promotions *after* noticing character types.
3837 (It is unclear why we do these promotions -- the standard
3838 does not say that we should. In fact, the natural thing would
3839 seem to be to convert IDX to ptrdiff_t; we're performing
3840 pointer arithmetic.) */
3841 idx = cp_perform_integral_promotions (idx, complain);
3843 idx = maybe_fold_non_dependent_expr (idx, complain);
3845 /* An array that is indexed by a non-constant
3846 cannot be stored in a register; we must be able to do
3847 address arithmetic on its address.
3848 Likewise an array of elements of variable size. */
3849 if (TREE_CODE (idx) != INTEGER_CST
3850 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3851 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3852 != INTEGER_CST)))
3854 if (!cxx_mark_addressable (array, true))
3855 return error_mark_node;
3858 /* An array that is indexed by a constant value which is not within
3859 the array bounds cannot be stored in a register either; because we
3860 would get a crash in store_bit_field/extract_bit_field when trying
3861 to access a non-existent part of the register. */
3862 if (TREE_CODE (idx) == INTEGER_CST
3863 && TYPE_DOMAIN (TREE_TYPE (array))
3864 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3866 if (!cxx_mark_addressable (array))
3867 return error_mark_node;
3870 /* Note in C++ it is valid to subscript a `register' array, since
3871 it is valid to take the address of something with that
3872 storage specification. */
3873 if (extra_warnings)
3875 tree foo = array;
3876 while (TREE_CODE (foo) == COMPONENT_REF)
3877 foo = TREE_OPERAND (foo, 0);
3878 if (VAR_P (foo) && DECL_REGISTER (foo)
3879 && (complain & tf_warning))
3880 warning_at (loc, OPT_Wextra,
3881 "subscripting array declared %<register%>");
3884 type = TREE_TYPE (TREE_TYPE (array));
3885 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3886 /* Array ref is const/volatile if the array elements are
3887 or if the array is.. */
3888 TREE_READONLY (rval)
3889 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3890 TREE_SIDE_EFFECTS (rval)
3891 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3892 TREE_THIS_VOLATILE (rval)
3893 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3894 ret = require_complete_type_sfinae (rval, complain);
3895 protected_set_expr_location (ret, loc);
3896 if (non_lvalue)
3897 ret = non_lvalue_loc (loc, ret);
3898 return ret;
3902 tree ar = cp_default_conversion (array, complain);
3903 tree ind = cp_default_conversion (idx, complain);
3904 tree first = NULL_TREE;
3906 if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
3907 ar = first = save_expr (ar);
3909 /* Put the integer in IND to simplify error checking. */
3910 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3911 std::swap (ar, ind);
3913 if (ar == error_mark_node || ind == error_mark_node)
3914 return error_mark_node;
3916 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3918 if (complain & tf_error)
3919 error_at (loc, "subscripted value is neither array nor pointer");
3920 return error_mark_node;
3922 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3924 if (complain & tf_error)
3925 error_at (loc, "array subscript is not an integer");
3926 return error_mark_node;
3929 warn_array_subscript_with_type_char (loc, idx);
3931 ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
3932 if (first)
3933 ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
3934 ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
3935 protected_set_expr_location (ret, loc);
3936 if (non_lvalue)
3937 ret = non_lvalue_loc (loc, ret);
3938 return ret;
3942 /* Entry point for Obj-C++. */
3944 tree
3945 build_array_ref (location_t loc, tree array, tree idx)
3947 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3950 /* Resolve a pointer to member function. INSTANCE is the object
3951 instance to use, if the member points to a virtual member.
3953 This used to avoid checking for virtual functions if basetype
3954 has no virtual functions, according to an earlier ANSI draft.
3955 With the final ISO C++ rules, such an optimization is
3956 incorrect: A pointer to a derived member can be static_cast
3957 to pointer-to-base-member, as long as the dynamic object
3958 later has the right member. So now we only do this optimization
3959 when we know the dynamic type of the object. */
3961 tree
3962 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3963 tsubst_flags_t complain)
3965 if (TREE_CODE (function) == OFFSET_REF)
3966 function = TREE_OPERAND (function, 1);
3968 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3970 tree idx, delta, e1, e2, e3, vtbl;
3971 bool nonvirtual;
3972 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3973 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3975 tree instance_ptr = *instance_ptrptr;
3976 tree instance_save_expr = 0;
3977 if (instance_ptr == error_mark_node)
3979 if (TREE_CODE (function) == PTRMEM_CST)
3981 /* Extracting the function address from a pmf is only
3982 allowed with -Wno-pmf-conversions. It only works for
3983 pmf constants. */
3984 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3985 e1 = convert (fntype, e1);
3986 return e1;
3988 else
3990 if (complain & tf_error)
3991 error ("object missing in use of %qE", function);
3992 return error_mark_node;
3996 /* True if we know that the dynamic type of the object doesn't have
3997 virtual functions, so we can assume the PFN field is a pointer. */
3998 nonvirtual = (COMPLETE_TYPE_P (basetype)
3999 && !TYPE_POLYMORPHIC_P (basetype)
4000 && resolves_to_fixed_type_p (instance_ptr, 0));
4002 /* If we don't really have an object (i.e. in an ill-formed
4003 conversion from PMF to pointer), we can't resolve virtual
4004 functions anyway. */
4005 if (!nonvirtual && is_dummy_object (instance_ptr))
4006 nonvirtual = true;
4008 if (TREE_SIDE_EFFECTS (instance_ptr))
4009 instance_ptr = instance_save_expr = save_expr (instance_ptr);
4011 if (TREE_SIDE_EFFECTS (function))
4012 function = save_expr (function);
4014 /* Start by extracting all the information from the PMF itself. */
4015 e3 = pfn_from_ptrmemfunc (function);
4016 delta = delta_from_ptrmemfunc (function);
4017 idx = build1 (NOP_EXPR, vtable_index_type, e3);
4018 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4020 int flag_sanitize_save;
4021 case ptrmemfunc_vbit_in_pfn:
4022 e1 = cp_build_binary_op (input_location,
4023 BIT_AND_EXPR, idx, integer_one_node,
4024 complain);
4025 idx = cp_build_binary_op (input_location,
4026 MINUS_EXPR, idx, integer_one_node,
4027 complain);
4028 if (idx == error_mark_node)
4029 return error_mark_node;
4030 break;
4032 case ptrmemfunc_vbit_in_delta:
4033 e1 = cp_build_binary_op (input_location,
4034 BIT_AND_EXPR, delta, integer_one_node,
4035 complain);
4036 /* Don't instrument the RSHIFT_EXPR we're about to create because
4037 we're going to use DELTA number of times, and that wouldn't play
4038 well with SAVE_EXPRs therein. */
4039 flag_sanitize_save = flag_sanitize;
4040 flag_sanitize = 0;
4041 delta = cp_build_binary_op (input_location,
4042 RSHIFT_EXPR, delta, integer_one_node,
4043 complain);
4044 flag_sanitize = flag_sanitize_save;
4045 if (delta == error_mark_node)
4046 return error_mark_node;
4047 break;
4049 default:
4050 gcc_unreachable ();
4053 if (e1 == error_mark_node)
4054 return error_mark_node;
4056 /* Convert down to the right base before using the instance. A
4057 special case is that in a pointer to member of class C, C may
4058 be incomplete. In that case, the function will of course be
4059 a member of C, and no conversion is required. In fact,
4060 lookup_base will fail in that case, because incomplete
4061 classes do not have BINFOs. */
4062 if (!same_type_ignoring_top_level_qualifiers_p
4063 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4065 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4066 basetype, ba_check, NULL, complain);
4067 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4068 1, complain);
4069 if (instance_ptr == error_mark_node)
4070 return error_mark_node;
4072 /* ...and then the delta in the PMF. */
4073 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4075 /* Hand back the adjusted 'this' argument to our caller. */
4076 *instance_ptrptr = instance_ptr;
4078 if (nonvirtual)
4079 /* Now just return the pointer. */
4080 return e3;
4082 /* Next extract the vtable pointer from the object. */
4083 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4084 instance_ptr);
4085 vtbl = cp_build_fold_indirect_ref (vtbl);
4086 if (vtbl == error_mark_node)
4087 return error_mark_node;
4089 /* Finally, extract the function pointer from the vtable. */
4090 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4091 e2 = cp_build_fold_indirect_ref (e2);
4092 if (e2 == error_mark_node)
4093 return error_mark_node;
4094 TREE_CONSTANT (e2) = 1;
4096 /* When using function descriptors, the address of the
4097 vtable entry is treated as a function pointer. */
4098 if (TARGET_VTABLE_USES_DESCRIPTORS)
4099 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4100 cp_build_addr_expr (e2, complain));
4102 e2 = fold_convert (TREE_TYPE (e3), e2);
4103 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4104 if (e1 == error_mark_node)
4105 return error_mark_node;
4107 /* Make sure this doesn't get evaluated first inside one of the
4108 branches of the COND_EXPR. */
4109 if (instance_save_expr)
4110 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4111 instance_save_expr, e1);
4113 function = e1;
4115 return function;
4118 /* Used by the C-common bits. */
4119 tree
4120 build_function_call (location_t /*loc*/,
4121 tree function, tree params)
4123 return cp_build_function_call (function, params, tf_warning_or_error);
4126 /* Used by the C-common bits. */
4127 tree
4128 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4129 tree function, vec<tree, va_gc> *params,
4130 vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4132 vec<tree, va_gc> *orig_params = params;
4133 tree ret = cp_build_function_call_vec (function, &params,
4134 tf_warning_or_error, orig_function);
4136 /* cp_build_function_call_vec can reallocate PARAMS by adding
4137 default arguments. That should never happen here. Verify
4138 that. */
4139 gcc_assert (params == orig_params);
4141 return ret;
4144 /* Build a function call using a tree list of arguments. */
4146 static tree
4147 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4149 tree ret;
4151 releasing_vec vec;
4152 for (; params != NULL_TREE; params = TREE_CHAIN (params))
4153 vec_safe_push (vec, TREE_VALUE (params));
4154 ret = cp_build_function_call_vec (function, &vec, complain);
4155 return ret;
4158 /* Build a function call using varargs. */
4160 tree
4161 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4163 va_list args;
4164 tree ret, t;
4166 releasing_vec vec;
4167 va_start (args, complain);
4168 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4169 vec_safe_push (vec, t);
4170 va_end (args);
4171 ret = cp_build_function_call_vec (function, &vec, complain);
4172 return ret;
4175 /* Build a function call using a vector of arguments.
4176 If FUNCTION is the result of resolving an overloaded target built-in,
4177 ORIG_FNDECL is the original function decl, otherwise it is null.
4178 PARAMS may be NULL if there are no parameters. This changes the
4179 contents of PARAMS. */
4181 tree
4182 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4183 tsubst_flags_t complain, tree orig_fndecl)
4185 tree fntype, fndecl;
4186 int is_method;
4187 tree original = function;
4188 int nargs;
4189 tree *argarray;
4190 tree parm_types;
4191 vec<tree, va_gc> *allocated = NULL;
4192 tree ret;
4194 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4195 expressions, like those used for ObjC messenger dispatches. */
4196 if (params != NULL && !vec_safe_is_empty (*params))
4197 function = objc_rewrite_function_call (function, (**params)[0]);
4199 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4200 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4201 if (TREE_CODE (function) == NOP_EXPR
4202 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4203 function = TREE_OPERAND (function, 0);
4205 if (TREE_CODE (function) == FUNCTION_DECL)
4207 if (!mark_used (function, complain))
4208 return error_mark_node;
4209 fndecl = function;
4211 /* Convert anything with function type to a pointer-to-function. */
4212 if (DECL_MAIN_P (function))
4214 if (complain & tf_error)
4215 pedwarn (input_location, OPT_Wpedantic,
4216 "ISO C++ forbids calling %<::main%> from within program");
4217 else
4218 return error_mark_node;
4220 function = build_addr_func (function, complain);
4222 else
4224 fndecl = NULL_TREE;
4226 function = build_addr_func (function, complain);
4229 if (function == error_mark_node)
4230 return error_mark_node;
4232 fntype = TREE_TYPE (function);
4234 if (TYPE_PTRMEMFUNC_P (fntype))
4236 if (complain & tf_error)
4237 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4238 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4239 original, original);
4240 return error_mark_node;
4243 is_method = (TYPE_PTR_P (fntype)
4244 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4246 if (!(TYPE_PTRFN_P (fntype)
4247 || is_method
4248 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4250 if (complain & tf_error)
4252 if (!flag_diagnostics_show_caret)
4253 error_at (input_location,
4254 "%qE cannot be used as a function", original);
4255 else if (DECL_P (original))
4256 error_at (input_location,
4257 "%qD cannot be used as a function", original);
4258 else
4259 error_at (input_location,
4260 "expression cannot be used as a function");
4263 return error_mark_node;
4266 /* fntype now gets the type of function pointed to. */
4267 fntype = TREE_TYPE (fntype);
4268 parm_types = TYPE_ARG_TYPES (fntype);
4270 if (params == NULL)
4272 allocated = make_tree_vector ();
4273 params = &allocated;
4276 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4277 complain);
4278 if (nargs < 0)
4279 return error_mark_node;
4281 argarray = (*params)->address ();
4283 /* Check for errors in format strings and inappropriately
4284 null parameters. */
4285 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
4286 nargs, argarray, NULL);
4288 ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4290 if (warned_p)
4292 tree c = extract_call_expr (ret);
4293 if (TREE_CODE (c) == CALL_EXPR)
4294 suppress_warning (c, OPT_Wnonnull);
4297 if (allocated != NULL)
4298 release_tree_vector (allocated);
4300 return ret;
4303 /* Subroutine of convert_arguments.
4304 Print an error message about a wrong number of arguments. */
4306 static void
4307 error_args_num (location_t loc, tree fndecl, bool too_many_p)
4309 if (fndecl)
4311 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4313 if (DECL_NAME (fndecl) == NULL_TREE
4314 || (DECL_NAME (fndecl)
4315 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4316 error_at (loc,
4317 too_many_p
4318 ? G_("too many arguments to constructor %q#D")
4319 : G_("too few arguments to constructor %q#D"),
4320 fndecl);
4321 else
4322 error_at (loc,
4323 too_many_p
4324 ? G_("too many arguments to member function %q#D")
4325 : G_("too few arguments to member function %q#D"),
4326 fndecl);
4328 else
4329 error_at (loc,
4330 too_many_p
4331 ? G_("too many arguments to function %q#D")
4332 : G_("too few arguments to function %q#D"),
4333 fndecl);
4334 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4335 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4337 else
4339 if (c_dialect_objc () && objc_message_selector ())
4340 error_at (loc,
4341 too_many_p
4342 ? G_("too many arguments to method %q#D")
4343 : G_("too few arguments to method %q#D"),
4344 objc_message_selector ());
4345 else
4346 error_at (loc, too_many_p ? G_("too many arguments to function")
4347 : G_("too few arguments to function"));
4351 /* Convert the actual parameter expressions in the list VALUES to the
4352 types in the list TYPELIST. The converted expressions are stored
4353 back in the VALUES vector.
4354 If parmdecls is exhausted, or when an element has NULL as its type,
4355 perform the default conversions.
4357 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4359 This is also where warnings about wrong number of args are generated.
4361 Returns the actual number of arguments processed (which might be less
4362 than the length of the vector), or -1 on error.
4364 In C++, unspecified trailing parameters can be filled in with their
4365 default arguments, if such were specified. Do so here. */
4367 static int
4368 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4369 int flags, tsubst_flags_t complain)
4371 tree typetail;
4372 unsigned int i;
4374 /* Argument passing is always copy-initialization. */
4375 flags |= LOOKUP_ONLYCONVERTING;
4377 for (i = 0, typetail = typelist;
4378 i < vec_safe_length (*values);
4379 i++)
4381 tree type = typetail ? TREE_VALUE (typetail) : 0;
4382 tree val = (**values)[i];
4384 if (val == error_mark_node || type == error_mark_node)
4385 return -1;
4387 if (type == void_type_node)
4389 if (complain & tf_error)
4391 error_args_num (input_location, fndecl, /*too_many_p=*/true);
4392 return i;
4394 else
4395 return -1;
4398 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4399 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4400 if (TREE_CODE (val) == NOP_EXPR
4401 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4402 && (type == 0 || !TYPE_REF_P (type)))
4403 val = TREE_OPERAND (val, 0);
4405 if (type == 0 || !TYPE_REF_P (type))
4407 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4408 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4409 val = decay_conversion (val, complain);
4412 if (val == error_mark_node)
4413 return -1;
4415 if (type != 0)
4417 /* Formal parm type is specified by a function prototype. */
4418 tree parmval;
4420 if (!COMPLETE_TYPE_P (complete_type (type)))
4422 if (complain & tf_error)
4424 location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4425 if (fndecl)
4427 auto_diagnostic_group d;
4428 error_at (loc,
4429 "parameter %P of %qD has incomplete type %qT",
4430 i, fndecl, type);
4431 inform (get_fndecl_argument_location (fndecl, i),
4432 " declared here");
4434 else
4435 error_at (loc, "parameter %P has incomplete type %qT", i,
4436 type);
4438 parmval = error_mark_node;
4440 else
4442 parmval = convert_for_initialization
4443 (NULL_TREE, type, val, flags,
4444 ICR_ARGPASS, fndecl, i, complain);
4445 parmval = convert_for_arg_passing (type, parmval, complain);
4448 if (parmval == error_mark_node)
4449 return -1;
4451 (**values)[i] = parmval;
4453 else
4455 if (fndecl && magic_varargs_p (fndecl))
4456 /* Don't do ellipsis conversion for __built_in_constant_p
4457 as this will result in spurious errors for non-trivial
4458 types. */
4459 val = require_complete_type_sfinae (val, complain);
4460 else
4461 val = convert_arg_to_ellipsis (val, complain);
4463 (**values)[i] = val;
4466 if (typetail)
4467 typetail = TREE_CHAIN (typetail);
4470 if (typetail != 0 && typetail != void_list_node)
4472 /* See if there are default arguments that can be used. Because
4473 we hold default arguments in the FUNCTION_TYPE (which is so
4474 wrong), we can see default parameters here from deduced
4475 contexts (and via typeof) for indirect function calls.
4476 Fortunately we know whether we have a function decl to
4477 provide default arguments in a language conformant
4478 manner. */
4479 if (fndecl && TREE_PURPOSE (typetail)
4480 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4482 for (; typetail != void_list_node; ++i)
4484 /* After DR777, with explicit template args we can end up with a
4485 default argument followed by no default argument. */
4486 if (!TREE_PURPOSE (typetail))
4487 break;
4488 tree parmval
4489 = convert_default_arg (TREE_VALUE (typetail),
4490 TREE_PURPOSE (typetail),
4491 fndecl, i, complain);
4493 if (parmval == error_mark_node)
4494 return -1;
4496 vec_safe_push (*values, parmval);
4497 typetail = TREE_CHAIN (typetail);
4498 /* ends with `...'. */
4499 if (typetail == NULL_TREE)
4500 break;
4504 if (typetail && typetail != void_list_node)
4506 if (complain & tf_error)
4507 error_args_num (input_location, fndecl, /*too_many_p=*/false);
4508 return -1;
4512 return (int) i;
4515 /* Build a binary-operation expression, after performing default
4516 conversions on the operands. CODE is the kind of expression to
4517 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4518 are the tree codes which correspond to ARG1 and ARG2 when issuing
4519 warnings about possibly misplaced parentheses. They may differ
4520 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4521 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4522 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4523 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4524 ARG2_CODE as ERROR_MARK. */
4526 tree
4527 build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4528 enum tree_code arg1_code, tree arg2,
4529 enum tree_code arg2_code, tree lookups,
4530 tree *overload_p, tsubst_flags_t complain)
4532 tree orig_arg1;
4533 tree orig_arg2;
4534 tree expr;
4535 tree overload = NULL_TREE;
4537 orig_arg1 = arg1;
4538 orig_arg2 = arg2;
4540 if (processing_template_decl)
4542 if (type_dependent_expression_p (arg1)
4543 || type_dependent_expression_p (arg2))
4545 expr = build_min_nt_loc (loc, code, arg1, arg2);
4546 TREE_TYPE (expr)
4547 = build_dependent_operator_type (lookups, code, false);
4548 return expr;
4550 arg1 = build_non_dependent_expr (arg1);
4551 arg2 = build_non_dependent_expr (arg2);
4554 if (code == DOTSTAR_EXPR)
4555 expr = build_m_component_ref (arg1, arg2, complain);
4556 else
4557 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4558 lookups, &overload, complain);
4560 if (overload_p != NULL)
4561 *overload_p = overload;
4563 /* Check for cases such as x+y<<z which users are likely to
4564 misinterpret. But don't warn about obj << x + y, since that is a
4565 common idiom for I/O. */
4566 if (warn_parentheses
4567 && (complain & tf_warning)
4568 && !processing_template_decl
4569 && !error_operand_p (arg1)
4570 && !error_operand_p (arg2)
4571 && (code != LSHIFT_EXPR
4572 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4573 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4574 arg2_code, orig_arg2);
4576 if (processing_template_decl && expr != error_mark_node)
4578 if (overload != NULL_TREE)
4579 return (build_min_non_dep_op_overload
4580 (code, expr, overload, orig_arg1, orig_arg2));
4582 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4585 return expr;
4588 /* Build and return an ARRAY_REF expression. */
4590 tree
4591 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4592 tsubst_flags_t complain)
4594 tree orig_arg1 = arg1;
4595 tree orig_arg2 = arg2;
4596 tree expr;
4597 tree overload = NULL_TREE;
4599 if (processing_template_decl)
4601 if (type_dependent_expression_p (arg1)
4602 || type_dependent_expression_p (arg2))
4603 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4604 NULL_TREE, NULL_TREE);
4605 arg1 = build_non_dependent_expr (arg1);
4606 arg2 = build_non_dependent_expr (arg2);
4609 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4610 NULL_TREE, NULL_TREE, &overload, complain);
4612 if (processing_template_decl && expr != error_mark_node)
4614 if (overload != NULL_TREE)
4615 return (build_min_non_dep_op_overload
4616 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4618 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4619 NULL_TREE, NULL_TREE);
4621 return expr;
4624 /* Return whether OP is an expression of enum type cast to integer
4625 type. In C++ even unsigned enum types are cast to signed integer
4626 types. We do not want to issue warnings about comparisons between
4627 signed and unsigned types when one of the types is an enum type.
4628 Those warnings are always false positives in practice. */
4630 static bool
4631 enum_cast_to_int (tree op)
4633 if (CONVERT_EXPR_P (op)
4634 && TREE_TYPE (op) == integer_type_node
4635 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4636 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4637 return true;
4639 /* The cast may have been pushed into a COND_EXPR. */
4640 if (TREE_CODE (op) == COND_EXPR)
4641 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4642 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4644 return false;
4647 /* For the c-common bits. */
4648 tree
4649 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4650 bool /*convert_p*/)
4652 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4655 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4656 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4658 static tree
4659 build_vec_cmp (tree_code code, tree type,
4660 tree arg0, tree arg1)
4662 tree zero_vec = build_zero_cst (type);
4663 tree minus_one_vec = build_minus_one_cst (type);
4664 tree cmp_type = truth_type_for (type);
4665 tree cmp = build2 (code, cmp_type, arg0, arg1);
4666 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4669 /* Possibly warn about an address never being NULL. */
4671 static void
4672 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4674 /* Prevent warnings issued for macro expansion. */
4675 if (!warn_address
4676 || (complain & tf_warning) == 0
4677 || c_inhibit_evaluation_warnings != 0
4678 || from_macro_expansion_at (location)
4679 || warning_suppressed_p (op, OPT_Waddress))
4680 return;
4682 if (TREE_CODE (op) == NON_DEPENDENT_EXPR)
4683 op = TREE_OPERAND (op, 0);
4685 tree cop = fold_for_warn (op);
4687 if (TREE_CODE (cop) == NON_LVALUE_EXPR)
4688 /* Unwrap the expression for C++ 98. */
4689 cop = TREE_OPERAND (cop, 0);
4691 if (TREE_CODE (cop) == PTRMEM_CST)
4693 /* The address of a nonstatic data member is never null. */
4694 warning_at (location, OPT_Waddress,
4695 "the address %qE will never be NULL",
4696 cop);
4697 return;
4700 if (TREE_CODE (cop) == NOP_EXPR)
4702 /* Allow casts to intptr_t to suppress the warning. */
4703 tree type = TREE_TYPE (cop);
4704 if (TREE_CODE (type) == INTEGER_TYPE)
4705 return;
4707 STRIP_NOPS (cop);
4710 bool warned = false;
4711 if (TREE_CODE (cop) == ADDR_EXPR)
4713 cop = TREE_OPERAND (cop, 0);
4715 /* Set to true in the loop below if OP dereferences its operand.
4716 In such a case the ultimate target need not be a decl for
4717 the null [in]equality test to be necessarily constant. */
4718 bool deref = false;
4720 /* Get the outermost array or object, or member. */
4721 while (handled_component_p (cop))
4723 if (TREE_CODE (cop) == COMPONENT_REF)
4725 /* Get the member (its address is never null). */
4726 cop = TREE_OPERAND (cop, 1);
4727 break;
4730 /* Get the outer array/object to refer to in the warning. */
4731 cop = TREE_OPERAND (cop, 0);
4732 deref = true;
4735 if ((!deref && !decl_with_nonnull_addr_p (cop))
4736 || from_macro_expansion_at (location)
4737 || warning_suppressed_p (cop, OPT_Waddress))
4738 return;
4740 warned = warning_at (location, OPT_Waddress,
4741 "the address of %qD will never be NULL", cop);
4742 op = cop;
4744 else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
4746 /* Adding zero to the null pointer is well-defined in C++. When
4747 the offset is unknown (i.e., not a constant) warn anyway since
4748 it's less likely that the pointer operand is null than not. */
4749 tree off = TREE_OPERAND (cop, 1);
4750 if (!integer_zerop (off)
4751 && !warning_suppressed_p (cop, OPT_Waddress))
4752 warning_at (location, OPT_Waddress, "comparing the result of pointer "
4753 "addition %qE and NULL", cop);
4754 return;
4756 else if (CONVERT_EXPR_P (op)
4757 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4759 STRIP_NOPS (op);
4761 if (TREE_CODE (op) == COMPONENT_REF)
4762 op = TREE_OPERAND (op, 1);
4764 if (DECL_P (op))
4765 warned = warning_at (location, OPT_Waddress,
4766 "the compiler can assume that the address of "
4767 "%qD will never be NULL", op);
4770 if (warned && DECL_P (op))
4771 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
4774 /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
4775 the other operand is of a different enumeration type or a floating-point
4776 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
4777 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
4778 and LOC is the location for the whole binary expression.
4779 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
4781 static void
4782 do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
4783 tree type1)
4785 if (TREE_CODE (type0) == ENUMERAL_TYPE
4786 && TREE_CODE (type1) == ENUMERAL_TYPE
4787 && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
4789 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
4790 Otherwise, warn if -Wenum-conversion is on. */
4791 enum opt_code opt;
4792 if (warn_deprecated_enum_enum_conv)
4793 opt = OPT_Wdeprecated_enum_enum_conversion;
4794 else if (warn_enum_conversion)
4795 opt = OPT_Wenum_conversion;
4796 else
4797 return;
4799 switch (code)
4801 case GT_EXPR:
4802 case LT_EXPR:
4803 case GE_EXPR:
4804 case LE_EXPR:
4805 case EQ_EXPR:
4806 case NE_EXPR:
4807 /* Comparisons are handled by -Wenum-compare. */
4808 return;
4809 case SPACESHIP_EXPR:
4810 /* This is invalid, don't warn. */
4811 return;
4812 case BIT_AND_EXPR:
4813 case BIT_IOR_EXPR:
4814 case BIT_XOR_EXPR:
4815 warning_at (loc, opt, "bitwise operation between different "
4816 "enumeration types %qT and %qT is deprecated",
4817 type0, type1);
4818 return;
4819 default:
4820 warning_at (loc, opt, "arithmetic between different enumeration "
4821 "types %qT and %qT is deprecated", type0, type1);
4822 return;
4825 else if ((TREE_CODE (type0) == ENUMERAL_TYPE
4826 && TREE_CODE (type1) == REAL_TYPE)
4827 || (TREE_CODE (type0) == REAL_TYPE
4828 && TREE_CODE (type1) == ENUMERAL_TYPE))
4830 const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
4831 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
4832 Otherwise, warn if -Wenum-conversion is on. */
4833 enum opt_code opt;
4834 if (warn_deprecated_enum_float_conv)
4835 opt = OPT_Wdeprecated_enum_float_conversion;
4836 else if (warn_enum_conversion)
4837 opt = OPT_Wenum_conversion;
4838 else
4839 return;
4841 switch (code)
4843 case GT_EXPR:
4844 case LT_EXPR:
4845 case GE_EXPR:
4846 case LE_EXPR:
4847 case EQ_EXPR:
4848 case NE_EXPR:
4849 if (enum_first_p)
4850 warning_at (loc, opt, "comparison of enumeration type %qT with "
4851 "floating-point type %qT is deprecated",
4852 type0, type1);
4853 else
4854 warning_at (loc, opt, "comparison of floating-point type %qT "
4855 "with enumeration type %qT is deprecated",
4856 type0, type1);
4857 return;
4858 case SPACESHIP_EXPR:
4859 /* This is invalid, don't warn. */
4860 return;
4861 default:
4862 if (enum_first_p)
4863 warning_at (loc, opt, "arithmetic between enumeration type %qT "
4864 "and floating-point type %qT is deprecated",
4865 type0, type1);
4866 else
4867 warning_at (loc, opt, "arithmetic between floating-point type %qT "
4868 "and enumeration type %qT is deprecated",
4869 type0, type1);
4870 return;
4875 /* Build a binary-operation expression without default conversions.
4876 CODE is the kind of expression to build.
4877 LOCATION is the location_t of the operator in the source code.
4878 This function differs from `build' in several ways:
4879 the data type of the result is computed and recorded in it,
4880 warnings are generated if arg data types are invalid,
4881 special handling for addition and subtraction of pointers is known,
4882 and some optimization is done (operations on narrow ints
4883 are done in the narrower type when that gives the same result).
4884 Constant folding is also done before the result is returned.
4886 Note that the operands will never have enumeral types
4887 because either they have just had the default conversions performed
4888 or they have both just been converted to some other type in which
4889 the arithmetic is to be done.
4891 C++: must do special pointer arithmetic when implementing
4892 multiple inheritance, and deal with pointer to member functions. */
4894 tree
4895 cp_build_binary_op (const op_location_t &location,
4896 enum tree_code code, tree orig_op0, tree orig_op1,
4897 tsubst_flags_t complain)
4899 tree op0, op1;
4900 enum tree_code code0, code1;
4901 tree type0, type1;
4902 const char *invalid_op_diag;
4904 /* Expression code to give to the expression when it is built.
4905 Normally this is CODE, which is what the caller asked for,
4906 but in some special cases we change it. */
4907 enum tree_code resultcode = code;
4909 /* Data type in which the computation is to be performed.
4910 In the simplest cases this is the common type of the arguments. */
4911 tree result_type = NULL_TREE;
4913 /* Nonzero means operands have already been type-converted
4914 in whatever way is necessary.
4915 Zero means they need to be converted to RESULT_TYPE. */
4916 int converted = 0;
4918 /* Nonzero means create the expression with this type, rather than
4919 RESULT_TYPE. */
4920 tree build_type = 0;
4922 /* Nonzero means after finally constructing the expression
4923 convert it to this type. */
4924 tree final_type = 0;
4926 tree result, result_ovl;
4928 /* Nonzero if this is an operation like MIN or MAX which can
4929 safely be computed in short if both args are promoted shorts.
4930 Also implies COMMON.
4931 -1 indicates a bitwise operation; this makes a difference
4932 in the exact conditions for when it is safe to do the operation
4933 in a narrower mode. */
4934 int shorten = 0;
4936 /* Nonzero if this is a comparison operation;
4937 if both args are promoted shorts, compare the original shorts.
4938 Also implies COMMON. */
4939 int short_compare = 0;
4941 /* Nonzero if this is a right-shift operation, which can be computed on the
4942 original short and then promoted if the operand is a promoted short. */
4943 int short_shift = 0;
4945 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4946 int common = 0;
4948 /* True if both operands have arithmetic type. */
4949 bool arithmetic_types_p;
4951 /* Remember whether we're doing / or %. */
4952 bool doing_div_or_mod = false;
4954 /* Remember whether we're doing << or >>. */
4955 bool doing_shift = false;
4957 /* Tree holding instrumentation expression. */
4958 tree instrument_expr = NULL_TREE;
4960 /* Apply default conversions. */
4961 op0 = resolve_nondeduced_context (orig_op0, complain);
4962 op1 = resolve_nondeduced_context (orig_op1, complain);
4964 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4965 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4966 || code == TRUTH_XOR_EXPR)
4968 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4969 op0 = decay_conversion (op0, complain);
4970 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4971 op1 = decay_conversion (op1, complain);
4973 else
4975 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4976 op0 = cp_default_conversion (op0, complain);
4977 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4978 op1 = cp_default_conversion (op1, complain);
4981 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4982 STRIP_TYPE_NOPS (op0);
4983 STRIP_TYPE_NOPS (op1);
4985 /* DTRT if one side is an overloaded function, but complain about it. */
4986 if (type_unknown_p (op0))
4988 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4989 if (t != error_mark_node)
4991 if (complain & tf_error)
4992 permerror (location,
4993 "assuming cast to type %qT from overloaded function",
4994 TREE_TYPE (t));
4995 op0 = t;
4998 if (type_unknown_p (op1))
5000 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5001 if (t != error_mark_node)
5003 if (complain & tf_error)
5004 permerror (location,
5005 "assuming cast to type %qT from overloaded function",
5006 TREE_TYPE (t));
5007 op1 = t;
5011 type0 = TREE_TYPE (op0);
5012 type1 = TREE_TYPE (op1);
5014 /* The expression codes of the data types of the arguments tell us
5015 whether the arguments are integers, floating, pointers, etc. */
5016 code0 = TREE_CODE (type0);
5017 code1 = TREE_CODE (type1);
5019 /* If an error was already reported for one of the arguments,
5020 avoid reporting another error. */
5021 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5022 return error_mark_node;
5024 if ((invalid_op_diag
5025 = targetm.invalid_binary_op (code, type0, type1)))
5027 if (complain & tf_error)
5028 error (invalid_op_diag);
5029 return error_mark_node;
5032 /* Issue warnings about peculiar, but valid, uses of NULL. */
5033 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5034 /* It's reasonable to use pointer values as operands of &&
5035 and ||, so NULL is no exception. */
5036 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5037 && ( /* Both are NULL (or 0) and the operation was not a
5038 comparison or a pointer subtraction. */
5039 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5040 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5041 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5042 || (!null_ptr_cst_p (orig_op0)
5043 && !TYPE_PTR_OR_PTRMEM_P (type0))
5044 || (!null_ptr_cst_p (orig_op1)
5045 && !TYPE_PTR_OR_PTRMEM_P (type1)))
5046 && (complain & tf_warning))
5048 location_t loc =
5049 expansion_point_location_if_in_system_header (input_location);
5051 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5054 /* In case when one of the operands of the binary operation is
5055 a vector and another is a scalar -- convert scalar to vector. */
5056 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5057 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5059 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
5060 complain & tf_error);
5062 switch (convert_flag)
5064 case stv_error:
5065 return error_mark_node;
5066 case stv_firstarg:
5068 op0 = convert (TREE_TYPE (type1), op0);
5069 op0 = save_expr (op0);
5070 op0 = build_vector_from_val (type1, op0);
5071 type0 = TREE_TYPE (op0);
5072 code0 = TREE_CODE (type0);
5073 converted = 1;
5074 break;
5076 case stv_secondarg:
5078 op1 = convert (TREE_TYPE (type0), op1);
5079 op1 = save_expr (op1);
5080 op1 = build_vector_from_val (type0, op1);
5081 type1 = TREE_TYPE (op1);
5082 code1 = TREE_CODE (type1);
5083 converted = 1;
5084 break;
5086 default:
5087 break;
5091 switch (code)
5093 case MINUS_EXPR:
5094 /* Subtraction of two similar pointers.
5095 We must subtract them as integers, then divide by object size. */
5096 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5097 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5098 TREE_TYPE (type1)))
5100 result = pointer_diff (location, op0, op1,
5101 common_pointer_type (type0, type1), complain,
5102 &instrument_expr);
5103 if (instrument_expr != NULL)
5104 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5105 instrument_expr, result);
5107 return result;
5109 /* In all other cases except pointer - int, the usual arithmetic
5110 rules apply. */
5111 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5113 common = 1;
5114 break;
5116 /* The pointer - int case is just like pointer + int; fall
5117 through. */
5118 gcc_fallthrough ();
5119 case PLUS_EXPR:
5120 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5121 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5123 tree ptr_operand;
5124 tree int_operand;
5125 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5126 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5127 if (processing_template_decl)
5129 result_type = TREE_TYPE (ptr_operand);
5130 break;
5132 return cp_pointer_int_sum (location, code,
5133 ptr_operand,
5134 int_operand,
5135 complain);
5137 common = 1;
5138 break;
5140 case MULT_EXPR:
5141 common = 1;
5142 break;
5144 case TRUNC_DIV_EXPR:
5145 case CEIL_DIV_EXPR:
5146 case FLOOR_DIV_EXPR:
5147 case ROUND_DIV_EXPR:
5148 case EXACT_DIV_EXPR:
5149 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5151 tree type0 = TREE_OPERAND (op0, 0);
5152 tree type1 = TREE_OPERAND (op1, 0);
5153 tree first_arg = tree_strip_any_location_wrapper (type0);
5154 if (!TYPE_P (type0))
5155 type0 = TREE_TYPE (type0);
5156 if (!TYPE_P (type1))
5157 type1 = TREE_TYPE (type1);
5158 if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1))
5160 if (!(TREE_CODE (first_arg) == PARM_DECL
5161 && DECL_ARRAY_PARAMETER_P (first_arg)
5162 && warn_sizeof_array_argument)
5163 && (complain & tf_warning))
5165 auto_diagnostic_group d;
5166 if (warning_at (location, OPT_Wsizeof_pointer_div,
5167 "division %<sizeof (%T) / sizeof (%T)%> does "
5168 "not compute the number of array elements",
5169 type0, type1))
5170 if (DECL_P (first_arg))
5171 inform (DECL_SOURCE_LOCATION (first_arg),
5172 "first %<sizeof%> operand was declared here");
5175 else if (TREE_CODE (type0) == ARRAY_TYPE
5176 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5177 /* Set by finish_parenthesized_expr. */
5178 && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5179 && (complain & tf_warning))
5180 maybe_warn_sizeof_array_div (location, first_arg, type0,
5181 op1, non_reference (type1));
5184 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5185 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5186 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5187 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5189 enum tree_code tcode0 = code0, tcode1 = code1;
5190 doing_div_or_mod = true;
5191 warn_for_div_by_zero (location, fold_for_warn (op1));
5193 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5194 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5195 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5196 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5198 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5199 resultcode = RDIV_EXPR;
5200 else
5202 /* When dividing two signed integers, we have to promote to int.
5203 unless we divide by a constant != -1. Note that default
5204 conversion will have been performed on the operands at this
5205 point, so we have to dig out the original type to find out if
5206 it was unsigned. */
5207 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5208 shorten = ((TREE_CODE (op0) == NOP_EXPR
5209 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
5210 || (TREE_CODE (stripped_op1) == INTEGER_CST
5211 && ! integer_all_onesp (stripped_op1)));
5214 common = 1;
5216 break;
5218 case BIT_AND_EXPR:
5219 case BIT_IOR_EXPR:
5220 case BIT_XOR_EXPR:
5221 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5222 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5223 && !VECTOR_FLOAT_TYPE_P (type0)
5224 && !VECTOR_FLOAT_TYPE_P (type1)))
5225 shorten = -1;
5226 break;
5228 case TRUNC_MOD_EXPR:
5229 case FLOOR_MOD_EXPR:
5230 doing_div_or_mod = true;
5231 warn_for_div_by_zero (location, fold_for_warn (op1));
5233 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5234 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5235 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5236 common = 1;
5237 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5239 /* Although it would be tempting to shorten always here, that loses
5240 on some targets, since the modulo instruction is undefined if the
5241 quotient can't be represented in the computation mode. We shorten
5242 only if unsigned or if dividing by something we know != -1. */
5243 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5244 shorten = ((TREE_CODE (op0) == NOP_EXPR
5245 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
5246 || (TREE_CODE (stripped_op1) == INTEGER_CST
5247 && ! integer_all_onesp (stripped_op1)));
5248 common = 1;
5250 break;
5252 case TRUTH_ANDIF_EXPR:
5253 case TRUTH_ORIF_EXPR:
5254 case TRUTH_AND_EXPR:
5255 case TRUTH_OR_EXPR:
5256 if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5258 if (!COMPARISON_CLASS_P (op1))
5259 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5260 build_zero_cst (type1), complain);
5261 if (code == TRUTH_ANDIF_EXPR)
5263 tree z = build_zero_cst (TREE_TYPE (op1));
5264 return build_conditional_expr (location, op0, op1, z, complain);
5266 else if (code == TRUTH_ORIF_EXPR)
5268 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5269 return build_conditional_expr (location, op0, m1, op1, complain);
5271 else
5272 gcc_unreachable ();
5274 if (gnu_vector_type_p (type0)
5275 && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5277 if (!COMPARISON_CLASS_P (op0))
5278 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5279 build_zero_cst (type0), complain);
5280 if (!VECTOR_TYPE_P (type1))
5282 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5283 tree z = build_zero_cst (TREE_TYPE (op0));
5284 op1 = build_conditional_expr (location, op1, m1, z, complain);
5286 else if (!COMPARISON_CLASS_P (op1))
5287 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5288 build_zero_cst (type1), complain);
5290 if (code == TRUTH_ANDIF_EXPR)
5291 code = BIT_AND_EXPR;
5292 else if (code == TRUTH_ORIF_EXPR)
5293 code = BIT_IOR_EXPR;
5294 else
5295 gcc_unreachable ();
5297 return cp_build_binary_op (location, code, op0, op1, complain);
5300 result_type = boolean_type_node;
5301 break;
5303 /* Shift operations: result has same type as first operand;
5304 always convert second operand to int.
5305 Also set SHORT_SHIFT if shifting rightward. */
5307 case RSHIFT_EXPR:
5308 if (gnu_vector_type_p (type0)
5309 && code1 == INTEGER_TYPE
5310 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5312 result_type = type0;
5313 converted = 1;
5315 else if (gnu_vector_type_p (type0)
5316 && gnu_vector_type_p (type1)
5317 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5318 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5319 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5320 TYPE_VECTOR_SUBPARTS (type1)))
5322 result_type = type0;
5323 converted = 1;
5325 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5327 tree const_op1 = fold_for_warn (op1);
5328 if (TREE_CODE (const_op1) != INTEGER_CST)
5329 const_op1 = op1;
5330 result_type = type0;
5331 doing_shift = true;
5332 if (TREE_CODE (const_op1) == INTEGER_CST)
5334 if (tree_int_cst_lt (const_op1, integer_zero_node))
5336 if ((complain & tf_warning)
5337 && c_inhibit_evaluation_warnings == 0)
5338 warning_at (location, OPT_Wshift_count_negative,
5339 "right shift count is negative");
5341 else
5343 if (!integer_zerop (const_op1))
5344 short_shift = 1;
5346 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5347 && (complain & tf_warning)
5348 && c_inhibit_evaluation_warnings == 0)
5349 warning_at (location, OPT_Wshift_count_overflow,
5350 "right shift count >= width of type");
5353 /* Avoid converting op1 to result_type later. */
5354 converted = 1;
5356 break;
5358 case LSHIFT_EXPR:
5359 if (gnu_vector_type_p (type0)
5360 && code1 == INTEGER_TYPE
5361 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5363 result_type = type0;
5364 converted = 1;
5366 else if (gnu_vector_type_p (type0)
5367 && gnu_vector_type_p (type1)
5368 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5369 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5370 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5371 TYPE_VECTOR_SUBPARTS (type1)))
5373 result_type = type0;
5374 converted = 1;
5376 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5378 tree const_op0 = fold_for_warn (op0);
5379 if (TREE_CODE (const_op0) != INTEGER_CST)
5380 const_op0 = op0;
5381 tree const_op1 = fold_for_warn (op1);
5382 if (TREE_CODE (const_op1) != INTEGER_CST)
5383 const_op1 = op1;
5384 result_type = type0;
5385 doing_shift = true;
5386 if (TREE_CODE (const_op0) == INTEGER_CST
5387 && tree_int_cst_sgn (const_op0) < 0
5388 && (complain & tf_warning)
5389 && c_inhibit_evaluation_warnings == 0)
5390 warning_at (location, OPT_Wshift_negative_value,
5391 "left shift of negative value");
5392 if (TREE_CODE (const_op1) == INTEGER_CST)
5394 if (tree_int_cst_lt (const_op1, integer_zero_node))
5396 if ((complain & tf_warning)
5397 && c_inhibit_evaluation_warnings == 0)
5398 warning_at (location, OPT_Wshift_count_negative,
5399 "left shift count is negative");
5401 else if (compare_tree_int (const_op1,
5402 TYPE_PRECISION (type0)) >= 0)
5404 if ((complain & tf_warning)
5405 && c_inhibit_evaluation_warnings == 0)
5406 warning_at (location, OPT_Wshift_count_overflow,
5407 "left shift count >= width of type");
5409 else if (TREE_CODE (const_op0) == INTEGER_CST
5410 && (complain & tf_warning))
5411 maybe_warn_shift_overflow (location, const_op0, const_op1);
5413 /* Avoid converting op1 to result_type later. */
5414 converted = 1;
5416 break;
5418 case EQ_EXPR:
5419 case NE_EXPR:
5420 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5421 goto vector_compare;
5422 if ((complain & tf_warning)
5423 && c_inhibit_evaluation_warnings == 0
5424 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5425 warning_at (location, OPT_Wfloat_equal,
5426 "comparing floating-point with %<==%> "
5427 "or %<!=%> is unsafe");
5428 if (complain & tf_warning)
5430 tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5431 tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5432 if ((TREE_CODE (stripped_orig_op0) == STRING_CST
5433 && !integer_zerop (cp_fully_fold (op1)))
5434 || (TREE_CODE (stripped_orig_op1) == STRING_CST
5435 && !integer_zerop (cp_fully_fold (op0))))
5436 warning_at (location, OPT_Waddress,
5437 "comparison with string literal results in "
5438 "unspecified behavior");
5439 else if (warn_array_compare
5440 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5441 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5442 do_warn_array_compare (location, code, stripped_orig_op0,
5443 stripped_orig_op1);
5446 build_type = boolean_type_node;
5447 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5448 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5449 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5450 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5451 short_compare = 1;
5452 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5453 && null_ptr_cst_p (orig_op1))
5454 /* Handle, eg, (void*)0 (c++/43906), and more. */
5455 || (code0 == POINTER_TYPE
5456 && TYPE_PTR_P (type1) && integer_zerop (op1)))
5458 if (TYPE_PTR_P (type1))
5459 result_type = composite_pointer_type (location,
5460 type0, type1, op0, op1,
5461 CPO_COMPARISON, complain);
5462 else
5463 result_type = type0;
5465 if (char_type_p (TREE_TYPE (orig_op1)))
5467 auto_diagnostic_group d;
5468 if (warning_at (location, OPT_Wpointer_compare,
5469 "comparison between pointer and zero character "
5470 "constant"))
5471 inform (location,
5472 "did you mean to dereference the pointer?");
5474 warn_for_null_address (location, op0, complain);
5476 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5477 && null_ptr_cst_p (orig_op0))
5478 /* Handle, eg, (void*)0 (c++/43906), and more. */
5479 || (code1 == POINTER_TYPE
5480 && TYPE_PTR_P (type0) && integer_zerop (op0)))
5482 if (TYPE_PTR_P (type0))
5483 result_type = composite_pointer_type (location,
5484 type0, type1, op0, op1,
5485 CPO_COMPARISON, complain);
5486 else
5487 result_type = type1;
5489 if (char_type_p (TREE_TYPE (orig_op0)))
5491 auto_diagnostic_group d;
5492 if (warning_at (location, OPT_Wpointer_compare,
5493 "comparison between pointer and zero character "
5494 "constant"))
5495 inform (location,
5496 "did you mean to dereference the pointer?");
5498 warn_for_null_address (location, op1, complain);
5500 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5501 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5502 result_type = composite_pointer_type (location,
5503 type0, type1, op0, op1,
5504 CPO_COMPARISON, complain);
5505 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5506 /* One of the operands must be of nullptr_t type. */
5507 result_type = TREE_TYPE (nullptr_node);
5508 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5510 result_type = type0;
5511 if (complain & tf_error)
5512 permerror (location, "ISO C++ forbids comparison between "
5513 "pointer and integer");
5514 else
5515 return error_mark_node;
5517 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5519 result_type = type1;
5520 if (complain & tf_error)
5521 permerror (location, "ISO C++ forbids comparison between "
5522 "pointer and integer");
5523 else
5524 return error_mark_node;
5526 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5528 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5529 == ptrmemfunc_vbit_in_delta)
5531 tree pfn0, delta0, e1, e2;
5533 if (TREE_SIDE_EFFECTS (op0))
5534 op0 = cp_save_expr (op0);
5536 pfn0 = pfn_from_ptrmemfunc (op0);
5537 delta0 = delta_from_ptrmemfunc (op0);
5538 e1 = cp_build_binary_op (location,
5539 EQ_EXPR,
5540 pfn0,
5541 build_zero_cst (TREE_TYPE (pfn0)),
5542 complain);
5543 e2 = cp_build_binary_op (location,
5544 BIT_AND_EXPR,
5545 delta0,
5546 integer_one_node,
5547 complain);
5549 if (complain & tf_warning)
5550 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5552 e2 = cp_build_binary_op (location,
5553 EQ_EXPR, e2, integer_zero_node,
5554 complain);
5555 op0 = cp_build_binary_op (location,
5556 TRUTH_ANDIF_EXPR, e1, e2,
5557 complain);
5558 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5560 else
5562 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5563 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5565 result_type = TREE_TYPE (op0);
5567 warn_for_null_address (location, orig_op0, complain);
5569 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5570 return cp_build_binary_op (location, code, op1, op0, complain);
5571 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5573 tree type;
5574 /* E will be the final comparison. */
5575 tree e;
5576 /* E1 and E2 are for scratch. */
5577 tree e1;
5578 tree e2;
5579 tree pfn0;
5580 tree pfn1;
5581 tree delta0;
5582 tree delta1;
5584 type = composite_pointer_type (location, type0, type1, op0, op1,
5585 CPO_COMPARISON, complain);
5587 if (!same_type_p (TREE_TYPE (op0), type))
5588 op0 = cp_convert_and_check (type, op0, complain);
5589 if (!same_type_p (TREE_TYPE (op1), type))
5590 op1 = cp_convert_and_check (type, op1, complain);
5592 if (op0 == error_mark_node || op1 == error_mark_node)
5593 return error_mark_node;
5595 if (TREE_SIDE_EFFECTS (op0))
5596 op0 = save_expr (op0);
5597 if (TREE_SIDE_EFFECTS (op1))
5598 op1 = save_expr (op1);
5600 pfn0 = pfn_from_ptrmemfunc (op0);
5601 pfn0 = cp_fully_fold (pfn0);
5602 /* Avoid -Waddress warnings (c++/64877). */
5603 if (TREE_CODE (pfn0) == ADDR_EXPR)
5604 suppress_warning (pfn0, OPT_Waddress);
5605 pfn1 = pfn_from_ptrmemfunc (op1);
5606 pfn1 = cp_fully_fold (pfn1);
5607 delta0 = delta_from_ptrmemfunc (op0);
5608 delta1 = delta_from_ptrmemfunc (op1);
5609 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5610 == ptrmemfunc_vbit_in_delta)
5612 /* We generate:
5614 (op0.pfn == op1.pfn
5615 && ((op0.delta == op1.delta)
5616 || (!op0.pfn && op0.delta & 1 == 0
5617 && op1.delta & 1 == 0))
5619 The reason for the `!op0.pfn' bit is that a NULL
5620 pointer-to-member is any member with a zero PFN and
5621 LSB of the DELTA field is 0. */
5623 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
5624 delta0,
5625 integer_one_node,
5626 complain);
5627 e1 = cp_build_binary_op (location,
5628 EQ_EXPR, e1, integer_zero_node,
5629 complain);
5630 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
5631 delta1,
5632 integer_one_node,
5633 complain);
5634 e2 = cp_build_binary_op (location,
5635 EQ_EXPR, e2, integer_zero_node,
5636 complain);
5637 e1 = cp_build_binary_op (location,
5638 TRUTH_ANDIF_EXPR, e2, e1,
5639 complain);
5640 e2 = cp_build_binary_op (location, EQ_EXPR,
5641 pfn0,
5642 build_zero_cst (TREE_TYPE (pfn0)),
5643 complain);
5644 e2 = cp_build_binary_op (location,
5645 TRUTH_ANDIF_EXPR, e2, e1, complain);
5646 e1 = cp_build_binary_op (location,
5647 EQ_EXPR, delta0, delta1, complain);
5648 e1 = cp_build_binary_op (location,
5649 TRUTH_ORIF_EXPR, e1, e2, complain);
5651 else
5653 /* We generate:
5655 (op0.pfn == op1.pfn
5656 && (!op0.pfn || op0.delta == op1.delta))
5658 The reason for the `!op0.pfn' bit is that a NULL
5659 pointer-to-member is any member with a zero PFN; the
5660 DELTA field is unspecified. */
5662 e1 = cp_build_binary_op (location,
5663 EQ_EXPR, delta0, delta1, complain);
5664 e2 = cp_build_binary_op (location,
5665 EQ_EXPR,
5666 pfn0,
5667 build_zero_cst (TREE_TYPE (pfn0)),
5668 complain);
5669 e1 = cp_build_binary_op (location,
5670 TRUTH_ORIF_EXPR, e1, e2, complain);
5672 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
5673 e = cp_build_binary_op (location,
5674 TRUTH_ANDIF_EXPR, e2, e1, complain);
5675 if (code == EQ_EXPR)
5676 return e;
5677 return cp_build_binary_op (location,
5678 EQ_EXPR, e, integer_zero_node, complain);
5680 else
5682 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
5683 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
5684 type1));
5685 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
5686 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
5687 type0));
5690 break;
5692 case MAX_EXPR:
5693 case MIN_EXPR:
5694 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
5695 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
5696 shorten = 1;
5697 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5698 result_type = composite_pointer_type (location,
5699 type0, type1, op0, op1,
5700 CPO_COMPARISON, complain);
5701 break;
5703 case LE_EXPR:
5704 case GE_EXPR:
5705 case LT_EXPR:
5706 case GT_EXPR:
5707 case SPACESHIP_EXPR:
5708 if (TREE_CODE (orig_op0) == STRING_CST
5709 || TREE_CODE (orig_op1) == STRING_CST)
5711 if (complain & tf_warning)
5712 warning_at (location, OPT_Waddress,
5713 "comparison with string literal results "
5714 "in unspecified behavior");
5716 else if (warn_array_compare
5717 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5718 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
5719 && code != SPACESHIP_EXPR
5720 && (complain & tf_warning))
5721 do_warn_array_compare (location, code,
5722 tree_strip_any_location_wrapper (orig_op0),
5723 tree_strip_any_location_wrapper (orig_op1));
5725 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5727 vector_compare:
5728 tree intt;
5729 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5730 TREE_TYPE (type1))
5731 && !vector_types_compatible_elements_p (type0, type1))
5733 if (complain & tf_error)
5735 error_at (location, "comparing vectors with different "
5736 "element types");
5737 inform (location, "operand types are %qT and %qT",
5738 type0, type1);
5740 return error_mark_node;
5743 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5744 TYPE_VECTOR_SUBPARTS (type1)))
5746 if (complain & tf_error)
5748 error_at (location, "comparing vectors with different "
5749 "number of elements");
5750 inform (location, "operand types are %qT and %qT",
5751 type0, type1);
5753 return error_mark_node;
5756 /* It's not precisely specified how the usual arithmetic
5757 conversions apply to the vector types. Here, we use
5758 the unsigned type if one of the operands is signed and
5759 the other one is unsigned. */
5760 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5762 if (!TYPE_UNSIGNED (type0))
5763 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5764 else
5765 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5766 warning_at (location, OPT_Wsign_compare, "comparison between "
5767 "types %qT and %qT", type0, type1);
5770 if (resultcode == SPACESHIP_EXPR)
5772 if (complain & tf_error)
5773 sorry_at (location, "three-way comparison of vectors");
5774 return error_mark_node;
5777 /* Always construct signed integer vector type. */
5778 intt = c_common_type_for_size
5779 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5780 if (!intt)
5782 if (complain & tf_error)
5783 error_at (location, "could not find an integer type "
5784 "of the same size as %qT", TREE_TYPE (type0));
5785 return error_mark_node;
5787 result_type = build_opaque_vector_type (intt,
5788 TYPE_VECTOR_SUBPARTS (type0));
5789 return build_vec_cmp (resultcode, result_type, op0, op1);
5791 build_type = boolean_type_node;
5792 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5793 || code0 == ENUMERAL_TYPE)
5794 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5795 || code1 == ENUMERAL_TYPE))
5796 short_compare = 1;
5797 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5798 result_type = composite_pointer_type (location,
5799 type0, type1, op0, op1,
5800 CPO_COMPARISON, complain);
5801 else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5802 || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5803 || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
5805 /* Core Issue 1512 made this ill-formed. */
5806 if (complain & tf_error)
5807 error_at (location, "ordered comparison of pointer with "
5808 "integer zero (%qT and %qT)", type0, type1);
5809 return error_mark_node;
5811 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5813 result_type = type0;
5814 if (complain & tf_error)
5815 permerror (location, "ISO C++ forbids comparison between "
5816 "pointer and integer");
5817 else
5818 return error_mark_node;
5820 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5822 result_type = type1;
5823 if (complain & tf_error)
5824 permerror (location, "ISO C++ forbids comparison between "
5825 "pointer and integer");
5826 else
5827 return error_mark_node;
5830 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5831 && !processing_template_decl
5832 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5834 op0 = save_expr (op0);
5835 op1 = save_expr (op1);
5837 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5838 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5841 break;
5843 case UNORDERED_EXPR:
5844 case ORDERED_EXPR:
5845 case UNLT_EXPR:
5846 case UNLE_EXPR:
5847 case UNGT_EXPR:
5848 case UNGE_EXPR:
5849 case UNEQ_EXPR:
5850 build_type = integer_type_node;
5851 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5853 if (complain & tf_error)
5854 error ("unordered comparison on non-floating-point argument");
5855 return error_mark_node;
5857 common = 1;
5858 break;
5860 default:
5861 break;
5864 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5865 || code0 == ENUMERAL_TYPE)
5866 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5867 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5868 arithmetic_types_p = 1;
5869 else
5871 arithmetic_types_p = 0;
5872 /* Vector arithmetic is only allowed when both sides are vectors. */
5873 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5875 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5876 || !vector_types_compatible_elements_p (type0, type1))
5878 if (complain & tf_error)
5880 /* "location" already embeds the locations of the
5881 operands, so we don't need to add them separately
5882 to richloc. */
5883 rich_location richloc (line_table, location);
5884 binary_op_error (&richloc, code, type0, type1);
5886 return error_mark_node;
5888 arithmetic_types_p = 1;
5891 /* Determine the RESULT_TYPE, if it is not already known. */
5892 if (!result_type
5893 && arithmetic_types_p
5894 && (shorten || common || short_compare))
5896 result_type = cp_common_type (type0, type1);
5897 if (complain & tf_warning)
5899 do_warn_double_promotion (result_type, type0, type1,
5900 "implicit conversion from %qH to %qI "
5901 "to match other operand of binary "
5902 "expression",
5903 location);
5904 do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
5905 TREE_TYPE (orig_op1));
5909 if (code == SPACESHIP_EXPR)
5911 iloc_sentinel s (location);
5913 tree orig_type0 = TREE_TYPE (orig_op0);
5914 tree_code orig_code0 = TREE_CODE (orig_type0);
5915 tree orig_type1 = TREE_TYPE (orig_op1);
5916 tree_code orig_code1 = TREE_CODE (orig_type1);
5917 if (!result_type)
5918 /* Nope. */;
5919 else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
5920 /* "If one of the operands is of type bool and the other is not, the
5921 program is ill-formed." */
5922 result_type = NULL_TREE;
5923 else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
5924 && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
5925 /* We only do array/function-to-pointer conversion if "at least one of
5926 the operands is of pointer type". */
5927 result_type = NULL_TREE;
5928 else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
5929 /* <=> no longer supports equality relations. */
5930 result_type = NULL_TREE;
5931 else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
5932 && !(same_type_ignoring_top_level_qualifiers_p
5933 (orig_type0, orig_type1)))
5934 /* "If both operands have arithmetic types, or one operand has integral
5935 type and the other operand has unscoped enumeration type, the usual
5936 arithmetic conversions are applied to the operands." So we don't do
5937 arithmetic conversions if the operands both have enumeral type. */
5938 result_type = NULL_TREE;
5939 else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
5940 || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
5941 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
5942 [where one is of enumeration type and the other is of a different
5943 enumeration type or a floating-point type] are ill-formed. */
5944 result_type = NULL_TREE;
5946 if (result_type)
5948 build_type = spaceship_type (result_type, complain);
5949 if (build_type == error_mark_node)
5950 return error_mark_node;
5953 if (result_type && arithmetic_types_p)
5955 /* If a narrowing conversion is required, other than from an integral
5956 type to a floating point type, the program is ill-formed. */
5957 bool ok = true;
5958 if (TREE_CODE (result_type) == REAL_TYPE
5959 && CP_INTEGRAL_TYPE_P (orig_type0))
5960 /* OK */;
5961 else if (!check_narrowing (result_type, orig_op0, complain))
5962 ok = false;
5963 if (TREE_CODE (result_type) == REAL_TYPE
5964 && CP_INTEGRAL_TYPE_P (orig_type1))
5965 /* OK */;
5966 else if (!check_narrowing (result_type, orig_op1, complain))
5967 ok = false;
5968 if (!ok && !(complain & tf_error))
5969 return error_mark_node;
5973 if (!result_type)
5975 if (complain & tf_error)
5977 binary_op_rich_location richloc (location,
5978 orig_op0, orig_op1, true);
5979 error_at (&richloc,
5980 "invalid operands of types %qT and %qT to binary %qO",
5981 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5983 return error_mark_node;
5986 /* If we're in a template, the only thing we need to know is the
5987 RESULT_TYPE. */
5988 if (processing_template_decl)
5990 /* Since the middle-end checks the type when doing a build2, we
5991 need to build the tree in pieces. This built tree will never
5992 get out of the front-end as we replace it when instantiating
5993 the template. */
5994 tree tmp = build2 (resultcode,
5995 build_type ? build_type : result_type,
5996 NULL_TREE, op1);
5997 TREE_OPERAND (tmp, 0) = op0;
5998 return tmp;
6001 /* Remember the original type; RESULT_TYPE might be changed later on
6002 by shorten_binary_op. */
6003 tree orig_type = result_type;
6005 if (arithmetic_types_p)
6007 bool first_complex = (code0 == COMPLEX_TYPE);
6008 bool second_complex = (code1 == COMPLEX_TYPE);
6009 int none_complex = (!first_complex && !second_complex);
6011 /* Adapted from patch for c/24581. */
6012 if (first_complex != second_complex
6013 && (code == PLUS_EXPR
6014 || code == MINUS_EXPR
6015 || code == MULT_EXPR
6016 || (code == TRUNC_DIV_EXPR && first_complex))
6017 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6018 && flag_signed_zeros)
6020 /* An operation on mixed real/complex operands must be
6021 handled specially, but the language-independent code can
6022 more easily optimize the plain complex arithmetic if
6023 -fno-signed-zeros. */
6024 tree real_type = TREE_TYPE (result_type);
6025 tree real, imag;
6026 if (first_complex)
6028 if (TREE_TYPE (op0) != result_type)
6029 op0 = cp_convert_and_check (result_type, op0, complain);
6030 if (TREE_TYPE (op1) != real_type)
6031 op1 = cp_convert_and_check (real_type, op1, complain);
6033 else
6035 if (TREE_TYPE (op0) != real_type)
6036 op0 = cp_convert_and_check (real_type, op0, complain);
6037 if (TREE_TYPE (op1) != result_type)
6038 op1 = cp_convert_and_check (result_type, op1, complain);
6040 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6041 return error_mark_node;
6042 if (first_complex)
6044 op0 = save_expr (op0);
6045 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6046 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6047 switch (code)
6049 case MULT_EXPR:
6050 case TRUNC_DIV_EXPR:
6051 op1 = save_expr (op1);
6052 imag = build2 (resultcode, real_type, imag, op1);
6053 /* Fall through. */
6054 case PLUS_EXPR:
6055 case MINUS_EXPR:
6056 real = build2 (resultcode, real_type, real, op1);
6057 break;
6058 default:
6059 gcc_unreachable();
6062 else
6064 op1 = save_expr (op1);
6065 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6066 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6067 switch (code)
6069 case MULT_EXPR:
6070 op0 = save_expr (op0);
6071 imag = build2 (resultcode, real_type, op0, imag);
6072 /* Fall through. */
6073 case PLUS_EXPR:
6074 real = build2 (resultcode, real_type, op0, real);
6075 break;
6076 case MINUS_EXPR:
6077 real = build2 (resultcode, real_type, op0, real);
6078 imag = build1 (NEGATE_EXPR, real_type, imag);
6079 break;
6080 default:
6081 gcc_unreachable();
6084 result = build2 (COMPLEX_EXPR, result_type, real, imag);
6085 return result;
6088 /* For certain operations (which identify themselves by shorten != 0)
6089 if both args were extended from the same smaller type,
6090 do the arithmetic in that type and then extend.
6092 shorten !=0 and !=1 indicates a bitwise operation.
6093 For them, this optimization is safe only if
6094 both args are zero-extended or both are sign-extended.
6095 Otherwise, we might change the result.
6096 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6097 but calculated in (unsigned short) it would be (unsigned short)-1. */
6099 if (shorten && none_complex)
6101 final_type = result_type;
6102 result_type = shorten_binary_op (result_type, op0, op1,
6103 shorten == -1);
6106 /* Shifts can be shortened if shifting right. */
6108 if (short_shift)
6110 int unsigned_arg;
6111 tree arg0 = get_narrower (op0, &unsigned_arg);
6112 /* We're not really warning here but when we set short_shift we
6113 used fold_for_warn to fold the operand. */
6114 tree const_op1 = fold_for_warn (op1);
6116 final_type = result_type;
6118 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6119 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6121 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6122 && tree_int_cst_sgn (const_op1) > 0
6123 /* We can shorten only if the shift count is less than the
6124 number of bits in the smaller type size. */
6125 && compare_tree_int (const_op1,
6126 TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6127 /* We cannot drop an unsigned shift after sign-extension. */
6128 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6130 /* Do an unsigned shift if the operand was zero-extended. */
6131 result_type
6132 = c_common_signed_or_unsigned_type (unsigned_arg,
6133 TREE_TYPE (arg0));
6134 /* Convert value-to-be-shifted to that type. */
6135 if (TREE_TYPE (op0) != result_type)
6136 op0 = convert (result_type, op0);
6137 converted = 1;
6141 /* Comparison operations are shortened too but differently.
6142 They identify themselves by setting short_compare = 1. */
6144 if (short_compare)
6146 /* We call shorten_compare only for diagnostics. */
6147 tree xop0 = fold_simple (op0);
6148 tree xop1 = fold_simple (op1);
6149 tree xresult_type = result_type;
6150 enum tree_code xresultcode = resultcode;
6151 shorten_compare (location, &xop0, &xop1, &xresult_type,
6152 &xresultcode);
6155 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6156 && warn_sign_compare
6157 /* Do not warn until the template is instantiated; we cannot
6158 bound the ranges of the arguments until that point. */
6159 && !processing_template_decl
6160 && (complain & tf_warning)
6161 && c_inhibit_evaluation_warnings == 0
6162 /* Even unsigned enum types promote to signed int. We don't
6163 want to issue -Wsign-compare warnings for this case. */
6164 && !enum_cast_to_int (orig_op0)
6165 && !enum_cast_to_int (orig_op1))
6167 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6168 result_type, resultcode);
6172 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6173 Then the expression will be built.
6174 It will be given type FINAL_TYPE if that is nonzero;
6175 otherwise, it will be given type RESULT_TYPE. */
6176 if (! converted)
6178 warning_sentinel w (warn_sign_conversion, short_compare);
6179 if (!same_type_p (TREE_TYPE (op0), result_type))
6180 op0 = cp_convert_and_check (result_type, op0, complain);
6181 if (!same_type_p (TREE_TYPE (op1), result_type))
6182 op1 = cp_convert_and_check (result_type, op1, complain);
6184 if (op0 == error_mark_node || op1 == error_mark_node)
6185 return error_mark_node;
6188 if (build_type == NULL_TREE)
6189 build_type = result_type;
6191 if (doing_shift
6192 && flag_strong_eval_order == 2
6193 && TREE_SIDE_EFFECTS (op1)
6194 && !processing_template_decl)
6196 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6197 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6198 op0 = cp_save_expr (op0);
6199 instrument_expr = op0;
6202 if (sanitize_flags_p ((SANITIZE_SHIFT
6203 | SANITIZE_DIVIDE
6204 | SANITIZE_FLOAT_DIVIDE
6205 | SANITIZE_SI_OVERFLOW))
6206 && current_function_decl != NULL_TREE
6207 && !processing_template_decl
6208 && (doing_div_or_mod || doing_shift))
6210 /* OP0 and/or OP1 might have side-effects. */
6211 op0 = cp_save_expr (op0);
6212 op1 = cp_save_expr (op1);
6213 op0 = fold_non_dependent_expr (op0, complain);
6214 op1 = fold_non_dependent_expr (op1, complain);
6215 tree instrument_expr1 = NULL_TREE;
6216 if (doing_div_or_mod
6217 && sanitize_flags_p (SANITIZE_DIVIDE
6218 | SANITIZE_FLOAT_DIVIDE
6219 | SANITIZE_SI_OVERFLOW))
6221 /* For diagnostics we want to use the promoted types without
6222 shorten_binary_op. So convert the arguments to the
6223 original result_type. */
6224 tree cop0 = op0;
6225 tree cop1 = op1;
6226 if (TREE_TYPE (cop0) != orig_type)
6227 cop0 = cp_convert (orig_type, op0, complain);
6228 if (TREE_TYPE (cop1) != orig_type)
6229 cop1 = cp_convert (orig_type, op1, complain);
6230 instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6232 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6233 instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6234 if (instrument_expr != NULL)
6235 instrument_expr = add_stmt_to_compound (instrument_expr,
6236 instrument_expr1);
6237 else
6238 instrument_expr = instrument_expr1;
6241 result = build2_loc (location, resultcode, build_type, op0, op1);
6242 if (final_type != 0)
6243 result = cp_convert (final_type, result, complain);
6245 if (instrument_expr != NULL)
6246 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6247 instrument_expr, result);
6249 if (!processing_template_decl)
6251 if (resultcode == SPACESHIP_EXPR)
6252 result = get_target_expr_sfinae (result, complain);
6253 op0 = cp_fully_fold (op0);
6254 /* Only consider the second argument if the first isn't overflowed. */
6255 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6256 return result;
6257 op1 = cp_fully_fold (op1);
6258 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6259 return result;
6261 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6262 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6263 return result;
6265 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6266 if (TREE_OVERFLOW_P (result_ovl))
6267 overflow_warning (location, result_ovl);
6269 return result;
6272 /* Build a VEC_PERM_EXPR.
6273 This is a simple wrapper for c_build_vec_perm_expr. */
6274 tree
6275 build_x_vec_perm_expr (location_t loc,
6276 tree arg0, tree arg1, tree arg2,
6277 tsubst_flags_t complain)
6279 tree orig_arg0 = arg0;
6280 tree orig_arg1 = arg1;
6281 tree orig_arg2 = arg2;
6282 if (processing_template_decl)
6284 if (type_dependent_expression_p (arg0)
6285 || type_dependent_expression_p (arg1)
6286 || type_dependent_expression_p (arg2))
6287 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6288 arg0 = build_non_dependent_expr (arg0);
6289 if (arg1)
6290 arg1 = build_non_dependent_expr (arg1);
6291 arg2 = build_non_dependent_expr (arg2);
6293 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6294 if (processing_template_decl && exp != error_mark_node)
6295 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6296 orig_arg1, orig_arg2);
6297 return exp;
6300 /* Build a VEC_PERM_EXPR.
6301 This is a simple wrapper for c_build_shufflevector. */
6302 tree
6303 build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6304 tsubst_flags_t complain)
6306 tree arg0 = (*args)[0];
6307 tree arg1 = (*args)[1];
6308 if (processing_template_decl)
6310 for (unsigned i = 0; i < args->length (); ++i)
6311 if (type_dependent_expression_p ((*args)[i]))
6313 tree exp = build_min_nt_call_vec (NULL, args);
6314 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6315 return exp;
6317 arg0 = build_non_dependent_expr (arg0);
6318 arg1 = build_non_dependent_expr (arg1);
6319 /* ??? Nothing needed for the index arguments? */
6321 auto_vec<tree, 16> mask;
6322 for (unsigned i = 2; i < args->length (); ++i)
6324 tree idx = maybe_constant_value ((*args)[i]);
6325 mask.safe_push (idx);
6327 tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6328 if (processing_template_decl && exp != error_mark_node)
6330 exp = build_min_non_dep_call_vec (exp, NULL, args);
6331 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6333 return exp;
6336 /* Return a tree for the sum or difference (RESULTCODE says which)
6337 of pointer PTROP and integer INTOP. */
6339 static tree
6340 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6341 tree intop, tsubst_flags_t complain)
6343 tree res_type = TREE_TYPE (ptrop);
6345 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6346 in certain circumstance (when it's valid to do so). So we need
6347 to make sure it's complete. We don't need to check here, if we
6348 can actually complete it at all, as those checks will be done in
6349 pointer_int_sum() anyway. */
6350 complete_type (TREE_TYPE (res_type));
6352 return pointer_int_sum (loc, resultcode, ptrop,
6353 intop, complain & tf_warning_or_error);
6356 /* Return a tree for the difference of pointers OP0 and OP1.
6357 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6358 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6360 static tree
6361 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6362 tsubst_flags_t complain, tree *instrument_expr)
6364 tree result, inttype;
6365 tree restype = ptrdiff_type_node;
6366 tree target_type = TREE_TYPE (ptrtype);
6368 if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6369 return error_mark_node;
6371 if (VOID_TYPE_P (target_type))
6373 if (complain & tf_error)
6374 permerror (loc, "ISO C++ forbids using pointer of "
6375 "type %<void *%> in subtraction");
6376 else
6377 return error_mark_node;
6379 if (TREE_CODE (target_type) == FUNCTION_TYPE)
6381 if (complain & tf_error)
6382 permerror (loc, "ISO C++ forbids using pointer to "
6383 "a function in subtraction");
6384 else
6385 return error_mark_node;
6387 if (TREE_CODE (target_type) == METHOD_TYPE)
6389 if (complain & tf_error)
6390 permerror (loc, "ISO C++ forbids using pointer to "
6391 "a method in subtraction");
6392 else
6393 return error_mark_node;
6395 else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6396 TREE_TYPE (TREE_TYPE (op0)),
6397 !(complain & tf_error))
6398 || !verify_type_context (loc, TCTX_POINTER_ARITH,
6399 TREE_TYPE (TREE_TYPE (op1)),
6400 !(complain & tf_error)))
6401 return error_mark_node;
6403 /* Determine integer type result of the subtraction. This will usually
6404 be the same as the result type (ptrdiff_t), but may need to be a wider
6405 type if pointers for the address space are wider than ptrdiff_t. */
6406 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6407 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6408 else
6409 inttype = restype;
6411 if (!processing_template_decl
6412 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6414 op0 = save_expr (op0);
6415 op1 = save_expr (op1);
6417 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6418 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6421 /* First do the subtraction, then build the divide operator
6422 and only convert at the very end.
6423 Do not do default conversions in case restype is a short type. */
6425 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6426 pointers. If some platform cannot provide that, or has a larger
6427 ptrdiff_type to support differences larger than half the address
6428 space, cast the pointers to some larger integer type and do the
6429 computations in that type. */
6430 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6431 op0 = cp_build_binary_op (loc,
6432 MINUS_EXPR,
6433 cp_convert (inttype, op0, complain),
6434 cp_convert (inttype, op1, complain),
6435 complain);
6436 else
6437 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6439 /* This generates an error if op1 is a pointer to an incomplete type. */
6440 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6442 if (complain & tf_error)
6443 error_at (loc, "invalid use of a pointer to an incomplete type in "
6444 "pointer arithmetic");
6445 else
6446 return error_mark_node;
6449 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6451 if (complain & tf_error)
6452 error_at (loc, "arithmetic on pointer to an empty aggregate");
6453 else
6454 return error_mark_node;
6457 op1 = (TYPE_PTROB_P (ptrtype)
6458 ? size_in_bytes_loc (loc, target_type)
6459 : integer_one_node);
6461 /* Do the division. */
6463 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
6464 cp_convert (inttype, op1, complain));
6465 return cp_convert (restype, result, complain);
6468 /* Construct and perhaps optimize a tree representation
6469 for a unary operation. CODE, a tree_code, specifies the operation
6470 and XARG is the operand. */
6472 tree
6473 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
6474 tree lookups, tsubst_flags_t complain)
6476 tree orig_expr = xarg;
6477 tree exp;
6478 int ptrmem = 0;
6479 tree overload = NULL_TREE;
6481 if (processing_template_decl)
6483 if (type_dependent_expression_p (xarg))
6485 tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6486 TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
6487 return e;
6490 xarg = build_non_dependent_expr (xarg);
6493 exp = NULL_TREE;
6495 /* [expr.unary.op] says:
6497 The address of an object of incomplete type can be taken.
6499 (And is just the ordinary address operator, not an overloaded
6500 "operator &".) However, if the type is a template
6501 specialization, we must complete the type at this point so that
6502 an overloaded "operator &" will be available if required. */
6503 if (code == ADDR_EXPR
6504 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6505 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6506 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6507 || (TREE_CODE (xarg) == OFFSET_REF)))
6508 /* Don't look for a function. */;
6509 else
6510 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6511 NULL_TREE, lookups, &overload, complain);
6513 if (!exp && code == ADDR_EXPR)
6515 if (is_overloaded_fn (xarg))
6517 tree fn = get_first_fn (xarg);
6518 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
6520 if (complain & tf_error)
6521 error_at (loc, DECL_CONSTRUCTOR_P (fn)
6522 ? G_("taking address of constructor %qD")
6523 : G_("taking address of destructor %qD"),
6524 fn);
6525 return error_mark_node;
6529 /* A pointer to member-function can be formed only by saying
6530 &X::mf. */
6531 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
6532 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
6534 if (TREE_CODE (xarg) != OFFSET_REF
6535 || !TYPE_P (TREE_OPERAND (xarg, 0)))
6537 if (complain & tf_error)
6539 error_at (loc, "invalid use of %qE to form a "
6540 "pointer-to-member-function", xarg.get_value ());
6541 if (TREE_CODE (xarg) != OFFSET_REF)
6542 inform (loc, " a qualified-id is required");
6544 return error_mark_node;
6546 else
6548 if (complain & tf_error)
6549 error_at (loc, "parentheses around %qE cannot be used to "
6550 "form a pointer-to-member-function",
6551 xarg.get_value ());
6552 else
6553 return error_mark_node;
6554 PTRMEM_OK_P (xarg) = 1;
6558 if (TREE_CODE (xarg) == OFFSET_REF)
6560 ptrmem = PTRMEM_OK_P (xarg);
6562 if (!ptrmem && !flag_ms_extensions
6563 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
6565 /* A single non-static member, make sure we don't allow a
6566 pointer-to-member. */
6567 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
6568 TREE_OPERAND (xarg, 0),
6569 ovl_make (TREE_OPERAND (xarg, 1)));
6570 PTRMEM_OK_P (xarg) = ptrmem;
6574 exp = cp_build_addr_expr_strict (xarg, complain);
6576 if (TREE_CODE (exp) == PTRMEM_CST)
6577 PTRMEM_CST_LOCATION (exp) = loc;
6578 else
6579 protected_set_expr_location (exp, loc);
6582 if (processing_template_decl && exp != error_mark_node)
6584 if (overload != NULL_TREE)
6585 return (build_min_non_dep_op_overload
6586 (code, exp, overload, orig_expr, integer_zero_node));
6588 exp = build_min_non_dep (code, exp, orig_expr,
6589 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
6591 if (TREE_CODE (exp) == ADDR_EXPR)
6592 PTRMEM_OK_P (exp) = ptrmem;
6593 return exp;
6596 /* Construct and perhaps optimize a tree representation
6597 for __builtin_addressof operation. ARG specifies the operand. */
6599 tree
6600 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
6602 tree orig_expr = arg;
6604 if (processing_template_decl)
6606 if (type_dependent_expression_p (arg))
6607 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
6609 arg = build_non_dependent_expr (arg);
6612 tree exp = cp_build_addr_expr_strict (arg, complain);
6614 if (processing_template_decl && exp != error_mark_node)
6615 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
6616 return exp;
6619 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6620 constants, where a null value is represented by an INTEGER_CST of
6621 -1. */
6623 tree
6624 cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
6626 tree type = TREE_TYPE (expr);
6627 location_t loc = cp_expr_loc_or_input_loc (expr);
6628 if (TYPE_PTR_OR_PTRMEM_P (type)
6629 /* Avoid ICE on invalid use of non-static member function. */
6630 || TREE_CODE (expr) == FUNCTION_DECL)
6631 return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
6632 else
6633 return c_common_truthvalue_conversion (loc, expr);
6636 /* Returns EXPR contextually converted to bool. */
6638 tree
6639 contextual_conv_bool (tree expr, tsubst_flags_t complain)
6641 return perform_implicit_conversion_flags (boolean_type_node, expr,
6642 complain, LOOKUP_NORMAL);
6645 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
6646 is a low-level function; most callers should use maybe_convert_cond. */
6648 tree
6649 condition_conversion (tree expr)
6651 tree t = contextual_conv_bool (expr, tf_warning_or_error);
6652 if (!processing_template_decl)
6653 t = fold_build_cleanup_point_expr (boolean_type_node, t);
6654 return t;
6657 /* Returns the address of T. This function will fold away
6658 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
6659 most places should use cp_build_addr_expr instead. */
6661 tree
6662 build_address (tree t)
6664 if (error_operand_p (t) || !cxx_mark_addressable (t))
6665 return error_mark_node;
6666 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
6667 || processing_template_decl);
6668 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
6669 if (TREE_CODE (t) != ADDR_EXPR)
6670 t = rvalue (t);
6671 return t;
6674 /* Return a NOP_EXPR converting EXPR to TYPE. */
6676 tree
6677 build_nop (tree type, tree expr)
6679 if (type == error_mark_node || error_operand_p (expr))
6680 return expr;
6681 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
6684 /* Take the address of ARG, whatever that means under C++ semantics.
6685 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
6686 and class rvalues as well.
6688 Nothing should call this function directly; instead, callers should use
6689 cp_build_addr_expr or cp_build_addr_expr_strict. */
6691 static tree
6692 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
6694 tree argtype;
6695 tree val;
6697 if (!arg || error_operand_p (arg))
6698 return error_mark_node;
6700 arg = mark_lvalue_use (arg);
6701 if (error_operand_p (arg))
6702 return error_mark_node;
6704 argtype = lvalue_type (arg);
6705 location_t loc = cp_expr_loc_or_input_loc (arg);
6707 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
6709 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
6710 && !really_overloaded_fn (arg))
6712 /* They're trying to take the address of a unique non-static
6713 member function. This is ill-formed (except in MS-land),
6714 but let's try to DTRT.
6715 Note: We only handle unique functions here because we don't
6716 want to complain if there's a static overload; non-unique
6717 cases will be handled by instantiate_type. But we need to
6718 handle this case here to allow casts on the resulting PMF.
6719 We could defer this in non-MS mode, but it's easier to give
6720 a useful error here. */
6722 /* Inside constant member functions, the `this' pointer
6723 contains an extra const qualifier. TYPE_MAIN_VARIANT
6724 is used here to remove this const from the diagnostics
6725 and the created OFFSET_REF. */
6726 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
6727 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
6728 if (!mark_used (fn, complain) && !(complain & tf_error))
6729 return error_mark_node;
6731 if (! flag_ms_extensions)
6733 tree name = DECL_NAME (fn);
6734 if (!(complain & tf_error))
6735 return error_mark_node;
6736 else if (current_class_type
6737 && TREE_OPERAND (arg, 0) == current_class_ref)
6738 /* An expression like &memfn. */
6739 permerror (loc,
6740 "ISO C++ forbids taking the address of an unqualified"
6741 " or parenthesized non-static member function to form"
6742 " a pointer to member function. Say %<&%T::%D%>",
6743 base, name);
6744 else
6745 permerror (loc,
6746 "ISO C++ forbids taking the address of a bound member"
6747 " function to form a pointer to member function."
6748 " Say %<&%T::%D%>",
6749 base, name);
6751 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
6754 /* Uninstantiated types are all functions. Taking the
6755 address of a function is a no-op, so just return the
6756 argument. */
6757 if (type_unknown_p (arg))
6758 return build1 (ADDR_EXPR, unknown_type_node, arg);
6760 if (TREE_CODE (arg) == OFFSET_REF)
6761 /* We want a pointer to member; bypass all the code for actually taking
6762 the address of something. */
6763 goto offset_ref;
6765 /* Anything not already handled and not a true memory reference
6766 is an error. */
6767 if (!FUNC_OR_METHOD_TYPE_P (argtype))
6769 cp_lvalue_kind kind = lvalue_kind (arg);
6770 if (kind == clk_none)
6772 if (complain & tf_error)
6773 lvalue_error (loc, lv_addressof);
6774 return error_mark_node;
6776 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
6778 if (!(complain & tf_error))
6779 return error_mark_node;
6780 /* Make this a permerror because we used to accept it. */
6781 permerror (loc, "taking address of rvalue");
6785 if (TYPE_REF_P (argtype))
6787 tree type = build_pointer_type (TREE_TYPE (argtype));
6788 arg = build1 (CONVERT_EXPR, type, arg);
6789 return arg;
6791 else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
6793 /* ARM $3.4 */
6794 /* Apparently a lot of autoconf scripts for C++ packages do this,
6795 so only complain if -Wpedantic. */
6796 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
6797 pedwarn (loc, OPT_Wpedantic,
6798 "ISO C++ forbids taking address of function %<::main%>");
6799 else if (flag_pedantic_errors)
6800 return error_mark_node;
6803 /* Let &* cancel out to simplify resulting code. */
6804 if (INDIRECT_REF_P (arg))
6806 arg = TREE_OPERAND (arg, 0);
6807 if (TYPE_REF_P (TREE_TYPE (arg)))
6809 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
6810 arg = build1 (CONVERT_EXPR, type, arg);
6812 else
6813 /* Don't let this be an lvalue. */
6814 arg = rvalue (arg);
6815 return arg;
6818 /* Handle complex lvalues (when permitted)
6819 by reduction to simpler cases. */
6820 val = unary_complex_lvalue (ADDR_EXPR, arg);
6821 if (val != 0)
6822 return val;
6824 switch (TREE_CODE (arg))
6826 CASE_CONVERT:
6827 case FLOAT_EXPR:
6828 case FIX_TRUNC_EXPR:
6829 /* We should have handled this above in the lvalue_kind check. */
6830 gcc_unreachable ();
6831 break;
6833 case BASELINK:
6834 arg = BASELINK_FUNCTIONS (arg);
6835 /* Fall through. */
6837 case OVERLOAD:
6838 arg = OVL_FIRST (arg);
6839 break;
6841 case OFFSET_REF:
6842 offset_ref:
6843 /* Turn a reference to a non-static data member into a
6844 pointer-to-member. */
6846 tree type;
6847 tree t;
6849 gcc_assert (PTRMEM_OK_P (arg));
6851 t = TREE_OPERAND (arg, 1);
6852 if (TYPE_REF_P (TREE_TYPE (t)))
6854 if (complain & tf_error)
6855 error_at (loc,
6856 "cannot create pointer to reference member %qD", t);
6857 return error_mark_node;
6860 type = build_ptrmem_type (context_for_name_lookup (t),
6861 TREE_TYPE (t));
6862 t = make_ptrmem_cst (type, t);
6863 return t;
6866 default:
6867 break;
6870 if (argtype != error_mark_node)
6871 argtype = build_pointer_type (argtype);
6873 if (bitfield_p (arg))
6875 if (complain & tf_error)
6876 error_at (loc, "attempt to take address of bit-field");
6877 return error_mark_node;
6880 /* In a template, we are processing a non-dependent expression
6881 so we can just form an ADDR_EXPR with the correct type. */
6882 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
6884 tree stripped_arg = tree_strip_any_location_wrapper (arg);
6885 if (TREE_CODE (stripped_arg) == FUNCTION_DECL
6886 && !mark_used (stripped_arg, complain) && !(complain & tf_error))
6887 return error_mark_node;
6888 val = build_address (arg);
6889 if (TREE_CODE (arg) == OFFSET_REF)
6890 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
6892 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
6894 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
6896 /* We can only get here with a single static member
6897 function. */
6898 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6899 && DECL_STATIC_FUNCTION_P (fn));
6900 if (!mark_used (fn, complain) && !(complain & tf_error))
6901 return error_mark_node;
6902 val = build_address (fn);
6903 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
6904 /* Do not lose object's side effects. */
6905 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
6906 TREE_OPERAND (arg, 0), val);
6908 else
6910 tree object = TREE_OPERAND (arg, 0);
6911 tree field = TREE_OPERAND (arg, 1);
6912 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6913 (TREE_TYPE (object), decl_type_context (field)));
6914 val = build_address (arg);
6917 if (TYPE_PTR_P (argtype)
6918 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6920 build_ptrmemfunc_type (argtype);
6921 val = build_ptrmemfunc (argtype, val, 0,
6922 /*c_cast_p=*/false,
6923 complain);
6926 /* For addresses of immediate functions ensure we have EXPR_LOCATION
6927 set for possible later diagnostics. */
6928 if (TREE_CODE (val) == ADDR_EXPR
6929 && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL
6930 && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (val, 0)))
6931 SET_EXPR_LOCATION (val, input_location);
6933 return val;
6936 /* Take the address of ARG if it has one, even if it's an rvalue. */
6938 tree
6939 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6941 return cp_build_addr_expr_1 (arg, 0, complain);
6944 /* Take the address of ARG, but only if it's an lvalue. */
6946 static tree
6947 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6949 return cp_build_addr_expr_1 (arg, 1, complain);
6952 /* C++: Must handle pointers to members.
6954 Perhaps type instantiation should be extended to handle conversion
6955 from aggregates to types we don't yet know we want? (Or are those
6956 cases typically errors which should be reported?)
6958 NOCONVERT suppresses the default promotions (such as from short to int). */
6960 tree
6961 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6962 tsubst_flags_t complain)
6964 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6965 tree arg = xarg;
6966 location_t location = cp_expr_loc_or_input_loc (arg);
6967 tree argtype = 0;
6968 const char *errstring = NULL;
6969 tree val;
6970 const char *invalid_op_diag;
6972 if (!arg || error_operand_p (arg))
6973 return error_mark_node;
6975 arg = resolve_nondeduced_context (arg, complain);
6977 if ((invalid_op_diag
6978 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6979 ? CONVERT_EXPR
6980 : code),
6981 TREE_TYPE (arg))))
6983 if (complain & tf_error)
6984 error (invalid_op_diag);
6985 return error_mark_node;
6988 switch (code)
6990 case UNARY_PLUS_EXPR:
6991 case NEGATE_EXPR:
6993 int flags = WANT_ARITH | WANT_ENUM;
6994 /* Unary plus (but not unary minus) is allowed on pointers. */
6995 if (code == UNARY_PLUS_EXPR)
6996 flags |= WANT_POINTER;
6997 arg = build_expr_type_conversion (flags, arg, true);
6998 if (!arg)
6999 errstring = (code == NEGATE_EXPR
7000 ? _("wrong type argument to unary minus")
7001 : _("wrong type argument to unary plus"));
7002 else
7004 if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7005 arg = cp_perform_integral_promotions (arg, complain);
7007 /* Make sure the result is not an lvalue: a unary plus or minus
7008 expression is always a rvalue. */
7009 arg = rvalue (arg);
7012 break;
7014 case BIT_NOT_EXPR:
7015 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7017 code = CONJ_EXPR;
7018 if (!noconvert)
7020 arg = cp_default_conversion (arg, complain);
7021 if (arg == error_mark_node)
7022 return error_mark_node;
7025 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7026 | WANT_VECTOR_OR_COMPLEX,
7027 arg, true)))
7028 errstring = _("wrong type argument to bit-complement");
7029 else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7031 /* Warn if the expression has boolean value. */
7032 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7033 && (complain & tf_warning)
7034 && warning_at (location, OPT_Wbool_operation,
7035 "%<~%> on an expression of type %<bool%>"))
7036 inform (location, "did you mean to use logical not (%<!%>)?");
7037 arg = cp_perform_integral_promotions (arg, complain);
7039 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7040 arg = mark_rvalue_use (arg);
7041 break;
7043 case ABS_EXPR:
7044 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7045 errstring = _("wrong type argument to abs");
7046 else if (!noconvert)
7048 arg = cp_default_conversion (arg, complain);
7049 if (arg == error_mark_node)
7050 return error_mark_node;
7052 break;
7054 case CONJ_EXPR:
7055 /* Conjugating a real value is a no-op, but allow it anyway. */
7056 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7057 errstring = _("wrong type argument to conjugation");
7058 else if (!noconvert)
7060 arg = cp_default_conversion (arg, complain);
7061 if (arg == error_mark_node)
7062 return error_mark_node;
7064 break;
7066 case TRUTH_NOT_EXPR:
7067 if (gnu_vector_type_p (TREE_TYPE (arg)))
7068 return cp_build_binary_op (input_location, EQ_EXPR, arg,
7069 build_zero_cst (TREE_TYPE (arg)), complain);
7070 arg = perform_implicit_conversion (boolean_type_node, arg,
7071 complain);
7072 val = invert_truthvalue_loc (location, arg);
7073 if (arg != error_mark_node)
7074 return val;
7075 errstring = _("in argument to unary !");
7076 break;
7078 case NOP_EXPR:
7079 break;
7081 case REALPART_EXPR:
7082 case IMAGPART_EXPR:
7083 arg = build_real_imag_expr (input_location, code, arg);
7084 return arg;
7086 case PREINCREMENT_EXPR:
7087 case POSTINCREMENT_EXPR:
7088 case PREDECREMENT_EXPR:
7089 case POSTDECREMENT_EXPR:
7090 /* Handle complex lvalues (when permitted)
7091 by reduction to simpler cases. */
7093 val = unary_complex_lvalue (code, arg);
7094 if (val != 0)
7095 return val;
7097 arg = mark_lvalue_use (arg);
7099 /* Increment or decrement the real part of the value,
7100 and don't change the imaginary part. */
7101 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7103 tree real, imag;
7105 arg = cp_stabilize_reference (arg);
7106 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7107 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7108 real = cp_build_unary_op (code, real, true, complain);
7109 if (real == error_mark_node || imag == error_mark_node)
7110 return error_mark_node;
7111 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
7112 real, imag);
7115 /* Report invalid types. */
7117 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7118 arg, true)))
7120 if (code == PREINCREMENT_EXPR)
7121 errstring = _("no pre-increment operator for type");
7122 else if (code == POSTINCREMENT_EXPR)
7123 errstring = _("no post-increment operator for type");
7124 else if (code == PREDECREMENT_EXPR)
7125 errstring = _("no pre-decrement operator for type");
7126 else
7127 errstring = _("no post-decrement operator for type");
7128 break;
7130 else if (arg == error_mark_node)
7131 return error_mark_node;
7133 /* Report something read-only. */
7135 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7136 || TREE_READONLY (arg))
7138 if (complain & tf_error)
7139 cxx_readonly_error (location, arg,
7140 ((code == PREINCREMENT_EXPR
7141 || code == POSTINCREMENT_EXPR)
7142 ? lv_increment : lv_decrement));
7143 else
7144 return error_mark_node;
7148 tree inc;
7149 tree declared_type = unlowered_expr_type (arg);
7151 argtype = TREE_TYPE (arg);
7153 /* ARM $5.2.5 last annotation says this should be forbidden. */
7154 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7156 if (complain & tf_error)
7157 permerror (location, (code == PREINCREMENT_EXPR
7158 || code == POSTINCREMENT_EXPR)
7159 ? G_("ISO C++ forbids incrementing an enum")
7160 : G_("ISO C++ forbids decrementing an enum"));
7161 else
7162 return error_mark_node;
7165 /* Compute the increment. */
7167 if (TYPE_PTR_P (argtype))
7169 tree type = complete_type (TREE_TYPE (argtype));
7171 if (!COMPLETE_OR_VOID_TYPE_P (type))
7173 if (complain & tf_error)
7174 error_at (location, ((code == PREINCREMENT_EXPR
7175 || code == POSTINCREMENT_EXPR))
7176 ? G_("cannot increment a pointer to incomplete "
7177 "type %qT")
7178 : G_("cannot decrement a pointer to incomplete "
7179 "type %qT"),
7180 TREE_TYPE (argtype));
7181 else
7182 return error_mark_node;
7184 else if (!TYPE_PTROB_P (argtype))
7186 if (complain & tf_error)
7187 pedwarn (location, OPT_Wpointer_arith,
7188 (code == PREINCREMENT_EXPR
7189 || code == POSTINCREMENT_EXPR)
7190 ? G_("ISO C++ forbids incrementing a pointer "
7191 "of type %qT")
7192 : G_("ISO C++ forbids decrementing a pointer "
7193 "of type %qT"),
7194 argtype);
7195 else
7196 return error_mark_node;
7198 else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7199 TREE_TYPE (argtype),
7200 !(complain & tf_error)))
7201 return error_mark_node;
7203 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7205 else
7206 inc = VECTOR_TYPE_P (argtype)
7207 ? build_one_cst (argtype)
7208 : integer_one_node;
7210 inc = cp_convert (argtype, inc, complain);
7212 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7213 need to ask Objective-C to build the increment or decrement
7214 expression for it. */
7215 if (objc_is_property_ref (arg))
7216 return objc_build_incr_expr_for_property_ref (input_location, code,
7217 arg, inc);
7219 /* Complain about anything else that is not a true lvalue. */
7220 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7221 || code == POSTINCREMENT_EXPR)
7222 ? lv_increment : lv_decrement),
7223 complain))
7224 return error_mark_node;
7226 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7227 prefix ++ and -- expressions of volatile-qualified arithmetic
7228 and pointer types are deprecated." */
7229 if (TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7230 warning_at (location, OPT_Wvolatile,
7231 "%qs expression of %<volatile%>-qualified type is "
7232 "deprecated",
7233 ((code == PREINCREMENT_EXPR
7234 || code == POSTINCREMENT_EXPR)
7235 ? "++" : "--"));
7237 /* Forbid using -- or ++ in C++17 on `bool'. */
7238 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7240 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7242 if (complain & tf_error)
7243 error_at (location,
7244 "use of an operand of type %qT in %<operator--%> "
7245 "is forbidden", boolean_type_node);
7246 return error_mark_node;
7248 else
7250 if (cxx_dialect >= cxx17)
7252 if (complain & tf_error)
7253 error_at (location,
7254 "use of an operand of type %qT in "
7255 "%<operator++%> is forbidden in C++17",
7256 boolean_type_node);
7257 return error_mark_node;
7259 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7260 else
7261 warning_at (location, OPT_Wdeprecated,
7262 "use of an operand of type %qT "
7263 "in %<operator++%> is deprecated",
7264 boolean_type_node);
7266 val = boolean_increment (code, arg);
7268 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7269 /* An rvalue has no cv-qualifiers. */
7270 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7271 else
7272 val = build2 (code, TREE_TYPE (arg), arg, inc);
7274 TREE_SIDE_EFFECTS (val) = 1;
7275 return val;
7278 case ADDR_EXPR:
7279 /* Note that this operation never does default_conversion
7280 regardless of NOCONVERT. */
7281 return cp_build_addr_expr (arg, complain);
7283 default:
7284 break;
7287 if (!errstring)
7289 if (argtype == 0)
7290 argtype = TREE_TYPE (arg);
7291 return build1 (code, argtype, arg);
7294 if (complain & tf_error)
7295 error_at (location, "%s", errstring);
7296 return error_mark_node;
7299 /* Hook for the c-common bits that build a unary op. */
7300 tree
7301 build_unary_op (location_t /*location*/,
7302 enum tree_code code, tree xarg, bool noconvert)
7304 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7307 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7308 so that it is a valid lvalue even for GENERIC by replacing
7309 (lhs = rhs) with ((lhs = rhs), lhs)
7310 (--lhs) with ((--lhs), lhs)
7311 (++lhs) with ((++lhs), lhs)
7312 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7313 that it can be evaluated multiple times. */
7315 tree
7316 genericize_compound_lvalue (tree lvalue)
7318 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7319 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7320 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7321 TREE_OPERAND (lvalue, 1));
7322 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7323 lvalue, TREE_OPERAND (lvalue, 0));
7326 /* Apply unary lvalue-demanding operator CODE to the expression ARG
7327 for certain kinds of expressions which are not really lvalues
7328 but which we can accept as lvalues.
7330 If ARG is not a kind of expression we can handle, return
7331 NULL_TREE. */
7333 tree
7334 unary_complex_lvalue (enum tree_code code, tree arg)
7336 /* Inside a template, making these kinds of adjustments is
7337 pointless; we are only concerned with the type of the
7338 expression. */
7339 if (processing_template_decl)
7340 return NULL_TREE;
7342 /* Handle (a, b) used as an "lvalue". */
7343 if (TREE_CODE (arg) == COMPOUND_EXPR)
7345 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7346 tf_warning_or_error);
7347 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7348 TREE_OPERAND (arg, 0), real_result);
7351 /* Handle (a ? b : c) used as an "lvalue". */
7352 if (TREE_CODE (arg) == COND_EXPR
7353 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7354 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7356 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7357 if (TREE_CODE (arg) == MODIFY_EXPR
7358 || TREE_CODE (arg) == PREINCREMENT_EXPR
7359 || TREE_CODE (arg) == PREDECREMENT_EXPR)
7360 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7362 if (code != ADDR_EXPR)
7363 return NULL_TREE;
7365 /* Handle (a = b) used as an "lvalue" for `&'. */
7366 if (TREE_CODE (arg) == MODIFY_EXPR
7367 || TREE_CODE (arg) == INIT_EXPR)
7369 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7370 tf_warning_or_error);
7371 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7372 arg, real_result);
7373 suppress_warning (arg /* What warning? */);
7374 return arg;
7377 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7378 || TREE_CODE (arg) == OFFSET_REF)
7379 return NULL_TREE;
7381 /* We permit compiler to make function calls returning
7382 objects of aggregate type look like lvalues. */
7384 tree targ = arg;
7386 if (TREE_CODE (targ) == SAVE_EXPR)
7387 targ = TREE_OPERAND (targ, 0);
7389 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7391 if (TREE_CODE (arg) == SAVE_EXPR)
7392 targ = arg;
7393 else
7394 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7395 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7398 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
7399 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
7400 TREE_OPERAND (targ, 0), current_function_decl, NULL);
7403 /* Don't let anything else be handled specially. */
7404 return NULL_TREE;
7407 /* Mark EXP saying that we need to be able to take the
7408 address of it; it should not be allocated in a register.
7409 Value is true if successful. ARRAY_REF_P is true if this
7410 is for ARRAY_REF construction - in that case we don't want
7411 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7412 it is fine to use ARRAY_REFs for vector subscripts on vector
7413 register variables.
7415 C++: we do not allow `current_class_ptr' to be addressable. */
7417 bool
7418 cxx_mark_addressable (tree exp, bool array_ref_p)
7420 tree x = exp;
7422 while (1)
7423 switch (TREE_CODE (x))
7425 case VIEW_CONVERT_EXPR:
7426 if (array_ref_p
7427 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7428 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
7429 return true;
7430 x = TREE_OPERAND (x, 0);
7431 break;
7433 case COMPONENT_REF:
7434 if (bitfield_p (x))
7435 error ("attempt to take address of bit-field");
7436 /* FALLTHRU */
7437 case ADDR_EXPR:
7438 case ARRAY_REF:
7439 case REALPART_EXPR:
7440 case IMAGPART_EXPR:
7441 x = TREE_OPERAND (x, 0);
7442 break;
7444 case PARM_DECL:
7445 if (x == current_class_ptr)
7447 error ("cannot take the address of %<this%>, which is an rvalue expression");
7448 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
7449 return true;
7451 /* Fall through. */
7453 case VAR_DECL:
7454 /* Caller should not be trying to mark initialized
7455 constant fields addressable. */
7456 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
7457 || DECL_IN_AGGR_P (x) == 0
7458 || TREE_STATIC (x)
7459 || DECL_EXTERNAL (x));
7460 /* Fall through. */
7462 case RESULT_DECL:
7463 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
7464 && !DECL_ARTIFICIAL (x))
7466 if (VAR_P (x) && DECL_HARD_REGISTER (x))
7468 error
7469 ("address of explicit register variable %qD requested", x);
7470 return false;
7472 else if (extra_warnings)
7473 warning
7474 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
7476 TREE_ADDRESSABLE (x) = 1;
7477 return true;
7479 case CONST_DECL:
7480 case FUNCTION_DECL:
7481 TREE_ADDRESSABLE (x) = 1;
7482 return true;
7484 case CONSTRUCTOR:
7485 TREE_ADDRESSABLE (x) = 1;
7486 return true;
7488 case TARGET_EXPR:
7489 TREE_ADDRESSABLE (x) = 1;
7490 cxx_mark_addressable (TREE_OPERAND (x, 0));
7491 return true;
7493 default:
7494 return true;
7498 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7500 tree
7501 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
7502 tsubst_flags_t complain)
7504 tree orig_ifexp = ifexp;
7505 tree orig_op1 = op1;
7506 tree orig_op2 = op2;
7507 tree expr;
7509 if (processing_template_decl)
7511 /* The standard says that the expression is type-dependent if
7512 IFEXP is type-dependent, even though the eventual type of the
7513 expression doesn't dependent on IFEXP. */
7514 if (type_dependent_expression_p (ifexp)
7515 /* As a GNU extension, the middle operand may be omitted. */
7516 || (op1 && type_dependent_expression_p (op1))
7517 || type_dependent_expression_p (op2))
7518 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
7519 ifexp = build_non_dependent_expr (ifexp);
7520 if (op1)
7521 op1 = build_non_dependent_expr (op1);
7522 op2 = build_non_dependent_expr (op2);
7525 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
7526 if (processing_template_decl && expr != error_mark_node)
7528 tree min = build_min_non_dep (COND_EXPR, expr,
7529 orig_ifexp, orig_op1, orig_op2);
7530 expr = convert_from_reference (min);
7532 return expr;
7535 /* Given a list of expressions, return a compound expression
7536 that performs them all and returns the value of the last of them. */
7538 tree
7539 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
7540 tsubst_flags_t complain)
7542 tree expr = TREE_VALUE (list);
7544 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7545 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
7547 if (complain & tf_error)
7548 pedwarn (cp_expr_loc_or_input_loc (expr), 0,
7549 "list-initializer for non-class type must not "
7550 "be parenthesized");
7551 else
7552 return error_mark_node;
7555 if (TREE_CHAIN (list))
7557 if (complain & tf_error)
7558 switch (exp)
7560 case ELK_INIT:
7561 permerror (input_location, "expression list treated as compound "
7562 "expression in initializer");
7563 break;
7564 case ELK_MEM_INIT:
7565 permerror (input_location, "expression list treated as compound "
7566 "expression in mem-initializer");
7567 break;
7568 case ELK_FUNC_CAST:
7569 permerror (input_location, "expression list treated as compound "
7570 "expression in functional cast");
7571 break;
7572 default:
7573 gcc_unreachable ();
7575 else
7576 return error_mark_node;
7578 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
7579 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
7580 expr, TREE_VALUE (list), NULL_TREE,
7581 complain);
7584 return expr;
7587 /* Like build_x_compound_expr_from_list, but using a VEC. */
7589 tree
7590 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
7591 tsubst_flags_t complain)
7593 if (vec_safe_is_empty (vec))
7594 return NULL_TREE;
7595 else if (vec->length () == 1)
7596 return (*vec)[0];
7597 else
7599 tree expr;
7600 unsigned int ix;
7601 tree t;
7603 if (msg != NULL)
7605 if (complain & tf_error)
7606 permerror (input_location,
7607 "%s expression list treated as compound expression",
7608 msg);
7609 else
7610 return error_mark_node;
7613 expr = (*vec)[0];
7614 for (ix = 1; vec->iterate (ix, &t); ++ix)
7615 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
7616 t, NULL_TREE, complain);
7618 return expr;
7622 /* Handle overloading of the ',' operator when needed. */
7624 tree
7625 build_x_compound_expr (location_t loc, tree op1, tree op2,
7626 tree lookups, tsubst_flags_t complain)
7628 tree result;
7629 tree orig_op1 = op1;
7630 tree orig_op2 = op2;
7631 tree overload = NULL_TREE;
7633 if (processing_template_decl)
7635 if (type_dependent_expression_p (op1)
7636 || type_dependent_expression_p (op2))
7638 result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
7639 TREE_TYPE (result)
7640 = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
7641 return result;
7643 op1 = build_non_dependent_expr (op1);
7644 op2 = build_non_dependent_expr (op2);
7647 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
7648 NULL_TREE, lookups, &overload, complain);
7649 if (!result)
7650 result = cp_build_compound_expr (op1, op2, complain);
7652 if (processing_template_decl && result != error_mark_node)
7654 if (overload != NULL_TREE)
7655 return (build_min_non_dep_op_overload
7656 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
7658 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
7661 return result;
7664 /* Like cp_build_compound_expr, but for the c-common bits. */
7666 tree
7667 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
7669 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
7672 /* Build a compound expression. */
7674 tree
7675 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
7677 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
7679 if (lhs == error_mark_node || rhs == error_mark_node)
7680 return error_mark_node;
7682 if (TREE_CODE (rhs) == TARGET_EXPR)
7684 /* If the rhs is a TARGET_EXPR, then build the compound
7685 expression inside the target_expr's initializer. This
7686 helps the compiler to eliminate unnecessary temporaries. */
7687 tree init = TREE_OPERAND (rhs, 1);
7689 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
7690 TREE_OPERAND (rhs, 1) = init;
7692 return rhs;
7695 if (type_unknown_p (rhs))
7697 if (complain & tf_error)
7698 error_at (cp_expr_loc_or_input_loc (rhs),
7699 "no context to resolve type of %qE", rhs);
7700 return error_mark_node;
7703 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
7706 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
7707 casts away constness. CAST gives the type of cast. Returns true
7708 if the cast is ill-formed, false if it is well-formed.
7710 ??? This function warns for casting away any qualifier not just
7711 const. We would like to specify exactly what qualifiers are casted
7712 away.
7715 static bool
7716 check_for_casting_away_constness (location_t loc, tree src_type,
7717 tree dest_type, enum tree_code cast,
7718 tsubst_flags_t complain)
7720 /* C-style casts are allowed to cast away constness. With
7721 WARN_CAST_QUAL, we still want to issue a warning. */
7722 if (cast == CAST_EXPR && !warn_cast_qual)
7723 return false;
7725 if (!casts_away_constness (src_type, dest_type, complain))
7726 return false;
7728 switch (cast)
7730 case CAST_EXPR:
7731 if (complain & tf_warning)
7732 warning_at (loc, OPT_Wcast_qual,
7733 "cast from type %qT to type %qT casts away qualifiers",
7734 src_type, dest_type);
7735 return false;
7737 case STATIC_CAST_EXPR:
7738 if (complain & tf_error)
7739 error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
7740 "away qualifiers",
7741 src_type, dest_type);
7742 return true;
7744 case REINTERPRET_CAST_EXPR:
7745 if (complain & tf_error)
7746 error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
7747 "casts away qualifiers",
7748 src_type, dest_type);
7749 return true;
7751 default:
7752 gcc_unreachable();
7756 /* Warns if the cast from expression EXPR to type TYPE is useless. */
7757 void
7758 maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
7759 tsubst_flags_t complain)
7761 if (warn_useless_cast
7762 && complain & tf_warning)
7764 if ((TYPE_REF_P (type)
7765 && (TYPE_REF_IS_RVALUE (type)
7766 ? xvalue_p (expr) : lvalue_p (expr))
7767 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
7768 || same_type_p (TREE_TYPE (expr), type))
7769 warning_at (loc, OPT_Wuseless_cast,
7770 "useless cast to type %q#T", type);
7774 /* Warns if the cast ignores cv-qualifiers on TYPE. */
7775 static void
7776 maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
7777 tsubst_flags_t complain)
7779 if (warn_ignored_qualifiers
7780 && complain & tf_warning
7781 && !CLASS_TYPE_P (type)
7782 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
7783 warning_at (loc, OPT_Wignored_qualifiers,
7784 "type qualifiers ignored on cast result type");
7787 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
7788 (another pointer-to-member type in the same hierarchy) and return
7789 the converted expression. If ALLOW_INVERSE_P is permitted, a
7790 pointer-to-derived may be converted to pointer-to-base; otherwise,
7791 only the other direction is permitted. If C_CAST_P is true, this
7792 conversion is taking place as part of a C-style cast. */
7794 tree
7795 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
7796 bool c_cast_p, tsubst_flags_t complain)
7798 if (same_type_p (type, TREE_TYPE (expr)))
7799 return expr;
7801 if (TYPE_PTRDATAMEM_P (type))
7803 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
7804 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
7805 tree delta = (get_delta_difference
7806 (obase, nbase,
7807 allow_inverse_p, c_cast_p, complain));
7809 if (delta == error_mark_node)
7810 return error_mark_node;
7812 if (!same_type_p (obase, nbase))
7814 if (TREE_CODE (expr) == PTRMEM_CST)
7815 expr = cplus_expand_constant (expr);
7817 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
7818 build_int_cst (TREE_TYPE (expr), -1),
7819 complain);
7820 tree op1 = build_nop (ptrdiff_type_node, expr);
7821 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
7822 complain);
7824 expr = fold_build3_loc (input_location,
7825 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
7828 return build_nop (type, expr);
7830 else
7831 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
7832 allow_inverse_p, c_cast_p, complain);
7835 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
7836 this static_cast is being attempted as one of the possible casts
7837 allowed by a C-style cast. (In that case, accessibility of base
7838 classes is not considered, and it is OK to cast away
7839 constness.) Return the result of the cast. *VALID_P is set to
7840 indicate whether or not the cast was valid. */
7842 static tree
7843 build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
7844 bool *valid_p, tsubst_flags_t complain)
7846 tree intype;
7847 tree result;
7848 cp_lvalue_kind clk;
7850 /* Assume the cast is valid. */
7851 *valid_p = true;
7853 intype = unlowered_expr_type (expr);
7855 /* Save casted types in the function's used types hash table. */
7856 used_types_insert (type);
7858 /* A prvalue of non-class type is cv-unqualified. */
7859 if (!CLASS_TYPE_P (type))
7860 type = cv_unqualified (type);
7862 /* [expr.static.cast]
7864 An lvalue of type "cv1 B", where B is a class type, can be cast
7865 to type "reference to cv2 D", where D is a class derived (clause
7866 _class.derived_) from B, if a valid standard conversion from
7867 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7868 same cv-qualification as, or greater cv-qualification than, cv1,
7869 and B is not a virtual base class of D. */
7870 /* We check this case before checking the validity of "TYPE t =
7871 EXPR;" below because for this case:
7873 struct B {};
7874 struct D : public B { D(const B&); };
7875 extern B& b;
7876 void f() { static_cast<const D&>(b); }
7878 we want to avoid constructing a new D. The standard is not
7879 completely clear about this issue, but our interpretation is
7880 consistent with other compilers. */
7881 if (TYPE_REF_P (type)
7882 && CLASS_TYPE_P (TREE_TYPE (type))
7883 && CLASS_TYPE_P (intype)
7884 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
7885 && DERIVED_FROM_P (intype, TREE_TYPE (type))
7886 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
7887 build_pointer_type (TYPE_MAIN_VARIANT
7888 (TREE_TYPE (type))),
7889 complain)
7890 && (c_cast_p
7891 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7893 tree base;
7895 if (processing_template_decl)
7896 return expr;
7898 /* There is a standard conversion from "D*" to "B*" even if "B"
7899 is ambiguous or inaccessible. If this is really a
7900 static_cast, then we check both for inaccessibility and
7901 ambiguity. However, if this is a static_cast being performed
7902 because the user wrote a C-style cast, then accessibility is
7903 not considered. */
7904 base = lookup_base (TREE_TYPE (type), intype,
7905 c_cast_p ? ba_unique : ba_check,
7906 NULL, complain);
7907 expr = cp_build_addr_expr (expr, complain);
7909 if (sanitize_flags_p (SANITIZE_VPTR))
7911 tree ubsan_check
7912 = cp_ubsan_maybe_instrument_downcast (loc, type,
7913 intype, expr);
7914 if (ubsan_check)
7915 expr = ubsan_check;
7918 /* Convert from "B*" to "D*". This function will check that "B"
7919 is not a virtual base of "D". Even if we don't have a guarantee
7920 that expr is NULL, if the static_cast is to a reference type,
7921 it is UB if it would be NULL, so omit the non-NULL check. */
7922 expr = build_base_path (MINUS_EXPR, expr, base,
7923 /*nonnull=*/flag_delete_null_pointer_checks,
7924 complain);
7926 /* Convert the pointer to a reference -- but then remember that
7927 there are no expressions with reference type in C++.
7929 We call rvalue so that there's an actual tree code
7930 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7931 is a variable with the same type, the conversion would get folded
7932 away, leaving just the variable and causing lvalue_kind to give
7933 the wrong answer. */
7934 expr = cp_fold_convert (type, expr);
7936 /* When -fsanitize=null, make sure to diagnose reference binding to
7937 NULL even when the reference is converted to pointer later on. */
7938 if (sanitize_flags_p (SANITIZE_NULL)
7939 && TREE_CODE (expr) == COND_EXPR
7940 && TREE_OPERAND (expr, 2)
7941 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
7942 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
7943 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
7945 return convert_from_reference (rvalue (expr));
7948 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7949 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7950 if (TYPE_REF_P (type)
7951 && TYPE_REF_IS_RVALUE (type)
7952 && (clk = real_lvalue_p (expr))
7953 && reference_compatible_p (TREE_TYPE (type), intype)
7954 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7956 if (processing_template_decl)
7957 return expr;
7958 if (clk == clk_ordinary)
7960 /* Handle the (non-bit-field) lvalue case here by casting to
7961 lvalue reference and then changing it to an rvalue reference.
7962 Casting an xvalue to rvalue reference will be handled by the
7963 main code path. */
7964 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7965 result = (perform_direct_initialization_if_possible
7966 (lref, expr, c_cast_p, complain));
7967 result = build1 (NON_LVALUE_EXPR, type, result);
7968 return convert_from_reference (result);
7970 else
7971 /* For a bit-field or packed field, bind to a temporary. */
7972 expr = rvalue (expr);
7975 /* Resolve overloaded address here rather than once in
7976 implicit_conversion and again in the inverse code below. */
7977 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7979 expr = instantiate_type (type, expr, complain);
7980 intype = TREE_TYPE (expr);
7983 /* [expr.static.cast]
7985 Any expression can be explicitly converted to type cv void. */
7986 if (VOID_TYPE_P (type))
7987 return convert_to_void (expr, ICV_CAST, complain);
7989 /* [class.abstract]
7990 An abstract class shall not be used ... as the type of an explicit
7991 conversion. */
7992 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7993 return error_mark_node;
7995 /* [expr.static.cast]
7997 An expression e can be explicitly converted to a type T using a
7998 static_cast of the form static_cast<T>(e) if the declaration T
7999 t(e);" is well-formed, for some invented temporary variable
8000 t. */
8001 result = perform_direct_initialization_if_possible (type, expr,
8002 c_cast_p, complain);
8003 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8004 which initialize the first element of the aggregate. We need to handle
8005 the array case specifically. */
8006 if (result == NULL_TREE
8007 && cxx_dialect >= cxx20
8008 && TREE_CODE (type) == ARRAY_TYPE)
8010 /* Create { EXPR } and perform direct-initialization from it. */
8011 tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8012 CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8013 CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8014 result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8015 complain);
8017 if (result)
8019 if (processing_template_decl)
8020 return expr;
8022 result = convert_from_reference (result);
8024 /* [expr.static.cast]
8026 If T is a reference type, the result is an lvalue; otherwise,
8027 the result is an rvalue. */
8028 if (!TYPE_REF_P (type))
8030 result = rvalue (result);
8032 if (result == expr && SCALAR_TYPE_P (type))
8033 /* Leave some record of the cast. */
8034 result = build_nop (type, expr);
8036 return result;
8039 /* [expr.static.cast]
8041 The inverse of any standard conversion sequence (clause _conv_),
8042 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8043 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8044 (_conv.bool_) conversions, can be performed explicitly using
8045 static_cast subject to the restriction that the explicit
8046 conversion does not cast away constness (_expr.const.cast_), and
8047 the following additional rules for specific cases: */
8048 /* For reference, the conversions not excluded are: integral
8049 promotions, floating-point promotion, integral conversions,
8050 floating-point conversions, floating-integral conversions,
8051 pointer conversions, and pointer to member conversions. */
8052 /* DR 128
8054 A value of integral _or enumeration_ type can be explicitly
8055 converted to an enumeration type. */
8056 /* The effect of all that is that any conversion between any two
8057 types which are integral, floating, or enumeration types can be
8058 performed. */
8059 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8060 || SCALAR_FLOAT_TYPE_P (type))
8061 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8062 || SCALAR_FLOAT_TYPE_P (intype)))
8064 if (processing_template_decl)
8065 return expr;
8066 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8069 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8070 && CLASS_TYPE_P (TREE_TYPE (type))
8071 && CLASS_TYPE_P (TREE_TYPE (intype))
8072 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8073 (TREE_TYPE (intype))),
8074 build_pointer_type (TYPE_MAIN_VARIANT
8075 (TREE_TYPE (type))),
8076 complain))
8078 tree base;
8080 if (processing_template_decl)
8081 return expr;
8083 if (!c_cast_p
8084 && check_for_casting_away_constness (loc, intype, type,
8085 STATIC_CAST_EXPR,
8086 complain))
8087 return error_mark_node;
8088 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8089 c_cast_p ? ba_unique : ba_check,
8090 NULL, complain);
8091 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8092 complain);
8094 if (sanitize_flags_p (SANITIZE_VPTR))
8096 tree ubsan_check
8097 = cp_ubsan_maybe_instrument_downcast (loc, type,
8098 intype, expr);
8099 if (ubsan_check)
8100 expr = ubsan_check;
8103 return cp_fold_convert (type, expr);
8106 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8107 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8109 tree c1;
8110 tree c2;
8111 tree t1;
8112 tree t2;
8114 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8115 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8117 if (TYPE_PTRDATAMEM_P (type))
8119 t1 = (build_ptrmem_type
8120 (c1,
8121 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8122 t2 = (build_ptrmem_type
8123 (c2,
8124 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8126 else
8128 t1 = intype;
8129 t2 = type;
8131 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8133 if (!c_cast_p
8134 && check_for_casting_away_constness (loc, intype, type,
8135 STATIC_CAST_EXPR,
8136 complain))
8137 return error_mark_node;
8138 if (processing_template_decl)
8139 return expr;
8140 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8141 c_cast_p, complain);
8145 /* [expr.static.cast]
8147 An rvalue of type "pointer to cv void" can be explicitly
8148 converted to a pointer to object type. A value of type pointer
8149 to object converted to "pointer to cv void" and back to the
8150 original pointer type will have its original value. */
8151 if (TYPE_PTR_P (intype)
8152 && VOID_TYPE_P (TREE_TYPE (intype))
8153 && TYPE_PTROB_P (type))
8155 if (!c_cast_p
8156 && check_for_casting_away_constness (loc, intype, type,
8157 STATIC_CAST_EXPR,
8158 complain))
8159 return error_mark_node;
8160 if (processing_template_decl)
8161 return expr;
8162 return build_nop (type, expr);
8165 *valid_p = false;
8166 return error_mark_node;
8169 /* Return an expression representing static_cast<TYPE>(EXPR). */
8171 tree
8172 build_static_cast (location_t loc, tree type, tree oexpr,
8173 tsubst_flags_t complain)
8175 tree expr = oexpr;
8176 tree result;
8177 bool valid_p;
8179 if (type == error_mark_node || expr == error_mark_node)
8180 return error_mark_node;
8182 bool dependent = (dependent_type_p (type)
8183 || type_dependent_expression_p (expr));
8184 if (dependent)
8186 tmpl:
8187 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8188 /* We don't know if it will or will not have side effects. */
8189 TREE_SIDE_EFFECTS (expr) = 1;
8190 result = convert_from_reference (expr);
8191 protected_set_expr_location (result, loc);
8192 return result;
8194 else if (processing_template_decl)
8195 expr = build_non_dependent_expr (expr);
8197 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8198 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8199 if (!TYPE_REF_P (type)
8200 && TREE_CODE (expr) == NOP_EXPR
8201 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8202 expr = TREE_OPERAND (expr, 0);
8204 result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8205 &valid_p, complain);
8206 if (valid_p)
8208 if (result != error_mark_node)
8210 maybe_warn_about_useless_cast (loc, type, expr, complain);
8211 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8213 if (processing_template_decl)
8214 goto tmpl;
8215 protected_set_expr_location (result, loc);
8216 return result;
8219 if (complain & tf_error)
8221 error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8222 TREE_TYPE (expr), type);
8223 if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8224 && CLASS_TYPE_P (TREE_TYPE (type))
8225 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8226 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8227 "class type %qT is incomplete", TREE_TYPE (type));
8228 tree expr_type = TREE_TYPE (expr);
8229 if (TYPE_PTR_P (expr_type))
8230 expr_type = TREE_TYPE (expr_type);
8231 if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8232 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8233 "class type %qT is incomplete", expr_type);
8235 return error_mark_node;
8238 /* EXPR is an expression with member function or pointer-to-member
8239 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8240 not permitted by ISO C++, but we accept it in some modes. If we
8241 are not in one of those modes, issue a diagnostic. Return the
8242 converted expression. */
8244 tree
8245 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8247 tree intype;
8248 tree decl;
8250 intype = TREE_TYPE (expr);
8251 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8252 || TREE_CODE (intype) == METHOD_TYPE);
8254 if (!(complain & tf_warning_or_error))
8255 return error_mark_node;
8257 location_t loc = cp_expr_loc_or_input_loc (expr);
8259 if (pedantic || warn_pmf2ptr)
8260 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8261 "converting from %qH to %qI", intype, type);
8263 STRIP_ANY_LOCATION_WRAPPER (expr);
8265 if (TREE_CODE (intype) == METHOD_TYPE)
8266 expr = build_addr_func (expr, complain);
8267 else if (TREE_CODE (expr) == PTRMEM_CST)
8268 expr = build_address (PTRMEM_CST_MEMBER (expr));
8269 else
8271 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8272 decl = build_address (decl);
8273 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8276 if (expr == error_mark_node)
8277 return error_mark_node;
8279 expr = build_nop (type, expr);
8280 SET_EXPR_LOCATION (expr, loc);
8281 return expr;
8284 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8285 constexpr evaluation knows to reject it. */
8287 static tree
8288 build_nop_reinterpret (tree type, tree expr)
8290 tree ret = build_nop (type, expr);
8291 if (ret != expr)
8292 REINTERPRET_CAST_P (ret) = true;
8293 return ret;
8296 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8297 If C_CAST_P is true, this reinterpret cast is being done as part of
8298 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8299 indicate whether or not reinterpret_cast was valid. */
8301 static tree
8302 build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8303 bool c_cast_p, bool *valid_p,
8304 tsubst_flags_t complain)
8306 tree intype;
8308 /* Assume the cast is invalid. */
8309 if (valid_p)
8310 *valid_p = true;
8312 if (type == error_mark_node || error_operand_p (expr))
8313 return error_mark_node;
8315 intype = TREE_TYPE (expr);
8317 /* Save casted types in the function's used types hash table. */
8318 used_types_insert (type);
8320 /* A prvalue of non-class type is cv-unqualified. */
8321 if (!CLASS_TYPE_P (type))
8322 type = cv_unqualified (type);
8324 /* [expr.reinterpret.cast]
8325 A glvalue of type T1, designating an object x, can be cast to the type
8326 "reference to T2" if an expression of type "pointer to T1" can be
8327 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8328 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8329 of type "pointer to T1". No temporary is created, no copy is made, and no
8330 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8331 if (TYPE_REF_P (type))
8333 if (!glvalue_p (expr))
8335 if (complain & tf_error)
8336 error_at (loc, "invalid cast of a prvalue expression of type "
8337 "%qT to type %qT",
8338 intype, type);
8339 return error_mark_node;
8342 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8343 "B" are related class types; the reinterpret_cast does not
8344 adjust the pointer. */
8345 if (TYPE_PTR_P (intype)
8346 && (complain & tf_warning)
8347 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8348 COMPARE_BASE | COMPARE_DERIVED)))
8349 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8350 intype, type);
8352 expr = cp_build_addr_expr (expr, complain);
8354 if (warn_strict_aliasing > 2)
8355 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8357 if (expr != error_mark_node)
8358 expr = build_reinterpret_cast_1
8359 (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8360 valid_p, complain);
8361 if (expr != error_mark_node)
8362 /* cp_build_indirect_ref isn't right for rvalue refs. */
8363 expr = convert_from_reference (fold_convert (type, expr));
8364 return expr;
8367 /* As a G++ extension, we consider conversions from member
8368 functions, and pointers to member functions to
8369 pointer-to-function and pointer-to-void types. If
8370 -Wno-pmf-conversions has not been specified,
8371 convert_member_func_to_ptr will issue an error message. */
8372 if ((TYPE_PTRMEMFUNC_P (intype)
8373 || TREE_CODE (intype) == METHOD_TYPE)
8374 && TYPE_PTR_P (type)
8375 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
8376 || VOID_TYPE_P (TREE_TYPE (type))))
8377 return convert_member_func_to_ptr (type, expr, complain);
8379 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8380 array-to-pointer, and function-to-pointer conversions are
8381 performed. */
8382 expr = decay_conversion (expr, complain);
8384 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8385 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8386 if (TREE_CODE (expr) == NOP_EXPR
8387 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8388 expr = TREE_OPERAND (expr, 0);
8390 if (error_operand_p (expr))
8391 return error_mark_node;
8393 intype = TREE_TYPE (expr);
8395 /* [expr.reinterpret.cast]
8396 A pointer can be converted to any integral type large enough to
8397 hold it. ... A value of type std::nullptr_t can be converted to
8398 an integral type; the conversion has the same meaning and
8399 validity as a conversion of (void*)0 to the integral type. */
8400 if (CP_INTEGRAL_TYPE_P (type)
8401 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8403 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
8405 if (complain & tf_error)
8406 permerror (loc, "cast from %qH to %qI loses precision",
8407 intype, type);
8408 else
8409 return error_mark_node;
8411 if (NULLPTR_TYPE_P (intype))
8412 return build_int_cst (type, 0);
8414 /* [expr.reinterpret.cast]
8415 A value of integral or enumeration type can be explicitly
8416 converted to a pointer. */
8417 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
8418 /* OK */
8420 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8421 || TYPE_PTR_OR_PTRMEM_P (type))
8422 && same_type_p (type, intype))
8423 /* DR 799 */
8424 return rvalue (expr);
8425 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
8427 if ((complain & tf_warning)
8428 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
8429 TREE_TYPE (intype)))
8430 warning_at (loc, OPT_Wcast_function_type,
8431 "cast between incompatible function types"
8432 " from %qH to %qI", intype, type);
8433 return build_nop_reinterpret (type, expr);
8435 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
8437 if ((complain & tf_warning)
8438 && !cxx_safe_function_type_cast_p
8439 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
8440 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
8441 warning_at (loc, OPT_Wcast_function_type,
8442 "cast between incompatible pointer to member types"
8443 " from %qH to %qI", intype, type);
8444 return build_nop_reinterpret (type, expr);
8446 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8447 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
8449 if (!c_cast_p
8450 && check_for_casting_away_constness (loc, intype, type,
8451 REINTERPRET_CAST_EXPR,
8452 complain))
8453 return error_mark_node;
8454 /* Warn about possible alignment problems. */
8455 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8456 && (complain & tf_warning)
8457 && !VOID_TYPE_P (type)
8458 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
8459 && COMPLETE_TYPE_P (TREE_TYPE (type))
8460 && COMPLETE_TYPE_P (TREE_TYPE (intype))
8461 && min_align_of_type (TREE_TYPE (type))
8462 > min_align_of_type (TREE_TYPE (intype)))
8463 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8464 "increases required alignment of target type",
8465 intype, type);
8467 if (warn_strict_aliasing <= 2)
8468 /* strict_aliasing_warning STRIP_NOPs its expr. */
8469 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8471 return build_nop_reinterpret (type, expr);
8473 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
8474 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
8476 if (complain & tf_warning)
8477 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8478 object pointer type or vice versa is conditionally-supported." */
8479 warning_at (loc, OPT_Wconditionally_supported,
8480 "casting between pointer-to-function and "
8481 "pointer-to-object is conditionally-supported");
8482 return build_nop_reinterpret (type, expr);
8484 else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
8485 return convert_to_vector (type, rvalue (expr));
8486 else if (gnu_vector_type_p (intype)
8487 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8488 return convert_to_integer_nofold (type, expr);
8489 else
8491 if (valid_p)
8492 *valid_p = false;
8493 if (complain & tf_error)
8494 error_at (loc, "invalid cast from type %qT to type %qT",
8495 intype, type);
8496 return error_mark_node;
8499 expr = cp_convert (type, expr, complain);
8500 if (TREE_CODE (expr) == NOP_EXPR)
8501 /* Mark any nop_expr that created as a reintepret_cast. */
8502 REINTERPRET_CAST_P (expr) = true;
8503 return expr;
8506 tree
8507 build_reinterpret_cast (location_t loc, tree type, tree expr,
8508 tsubst_flags_t complain)
8510 tree r;
8512 if (type == error_mark_node || expr == error_mark_node)
8513 return error_mark_node;
8515 if (processing_template_decl)
8517 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
8519 if (!TREE_SIDE_EFFECTS (t)
8520 && type_dependent_expression_p (expr))
8521 /* There might turn out to be side effects inside expr. */
8522 TREE_SIDE_EFFECTS (t) = 1;
8523 r = convert_from_reference (t);
8524 protected_set_expr_location (r, loc);
8525 return r;
8528 r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8529 /*valid_p=*/NULL, complain);
8530 if (r != error_mark_node)
8532 maybe_warn_about_useless_cast (loc, type, expr, complain);
8533 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8535 protected_set_expr_location (r, loc);
8536 return r;
8539 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
8540 return an appropriate expression. Otherwise, return
8541 error_mark_node. If the cast is not valid, and COMPLAIN is true,
8542 then a diagnostic will be issued. If VALID_P is non-NULL, we are
8543 performing a C-style cast, its value upon return will indicate
8544 whether or not the conversion succeeded. */
8546 static tree
8547 build_const_cast_1 (location_t loc, tree dst_type, tree expr,
8548 tsubst_flags_t complain, bool *valid_p)
8550 tree src_type;
8551 tree reference_type;
8553 /* Callers are responsible for handling error_mark_node as a
8554 destination type. */
8555 gcc_assert (dst_type != error_mark_node);
8556 /* In a template, callers should be building syntactic
8557 representations of casts, not using this machinery. */
8558 gcc_assert (!processing_template_decl);
8560 /* Assume the conversion is invalid. */
8561 if (valid_p)
8562 *valid_p = false;
8564 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
8566 if (complain & tf_error)
8567 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8568 "which is not a pointer, reference, "
8569 "nor a pointer-to-data-member type", dst_type);
8570 return error_mark_node;
8573 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
8575 if (complain & tf_error)
8576 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8577 "which is a pointer or reference to a function type",
8578 dst_type);
8579 return error_mark_node;
8582 /* A prvalue of non-class type is cv-unqualified. */
8583 dst_type = cv_unqualified (dst_type);
8585 /* Save casted types in the function's used types hash table. */
8586 used_types_insert (dst_type);
8588 src_type = TREE_TYPE (expr);
8589 /* Expressions do not really have reference types. */
8590 if (TYPE_REF_P (src_type))
8591 src_type = TREE_TYPE (src_type);
8593 /* [expr.const.cast]
8595 For two object types T1 and T2, if a pointer to T1 can be explicitly
8596 converted to the type "pointer to T2" using a const_cast, then the
8597 following conversions can also be made:
8599 -- an lvalue of type T1 can be explicitly converted to an lvalue of
8600 type T2 using the cast const_cast<T2&>;
8602 -- a glvalue of type T1 can be explicitly converted to an xvalue of
8603 type T2 using the cast const_cast<T2&&>; and
8605 -- if T1 is a class type, a prvalue of type T1 can be explicitly
8606 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
8608 if (TYPE_REF_P (dst_type))
8610 reference_type = dst_type;
8611 if (!TYPE_REF_IS_RVALUE (dst_type)
8612 ? lvalue_p (expr)
8613 : obvalue_p (expr))
8614 /* OK. */;
8615 else
8617 if (complain & tf_error)
8618 error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
8619 "to type %qT",
8620 src_type, dst_type);
8621 return error_mark_node;
8623 dst_type = build_pointer_type (TREE_TYPE (dst_type));
8624 src_type = build_pointer_type (src_type);
8626 else
8628 reference_type = NULL_TREE;
8629 /* If the destination type is not a reference type, the
8630 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8631 conversions are performed. */
8632 src_type = type_decays_to (src_type);
8633 if (src_type == error_mark_node)
8634 return error_mark_node;
8637 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
8639 if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
8641 if (valid_p)
8643 *valid_p = true;
8644 /* This cast is actually a C-style cast. Issue a warning if
8645 the user is making a potentially unsafe cast. */
8646 check_for_casting_away_constness (loc, src_type, dst_type,
8647 CAST_EXPR, complain);
8648 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
8649 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8650 && (complain & tf_warning)
8651 && min_align_of_type (TREE_TYPE (dst_type))
8652 > min_align_of_type (TREE_TYPE (src_type)))
8653 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8654 "increases required alignment of target type",
8655 src_type, dst_type);
8657 if (reference_type)
8659 expr = cp_build_addr_expr (expr, complain);
8660 if (expr == error_mark_node)
8661 return error_mark_node;
8662 expr = build_nop (reference_type, expr);
8663 return convert_from_reference (expr);
8665 else
8667 expr = decay_conversion (expr, complain);
8668 if (expr == error_mark_node)
8669 return error_mark_node;
8671 /* build_c_cast puts on a NOP_EXPR to make the result not an
8672 lvalue. Strip such NOP_EXPRs if VALUE is being used in
8673 non-lvalue context. */
8674 if (TREE_CODE (expr) == NOP_EXPR
8675 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8676 expr = TREE_OPERAND (expr, 0);
8677 return build_nop (dst_type, expr);
8680 else if (valid_p
8681 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
8682 TREE_TYPE (src_type)))
8683 check_for_casting_away_constness (loc, src_type, dst_type,
8684 CAST_EXPR, complain);
8687 if (complain & tf_error)
8688 error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
8689 src_type, dst_type);
8690 return error_mark_node;
8693 tree
8694 build_const_cast (location_t loc, tree type, tree expr,
8695 tsubst_flags_t complain)
8697 tree r;
8699 if (type == error_mark_node || error_operand_p (expr))
8700 return error_mark_node;
8702 if (processing_template_decl)
8704 tree t = build_min (CONST_CAST_EXPR, type, expr);
8706 if (!TREE_SIDE_EFFECTS (t)
8707 && type_dependent_expression_p (expr))
8708 /* There might turn out to be side effects inside expr. */
8709 TREE_SIDE_EFFECTS (t) = 1;
8710 r = convert_from_reference (t);
8711 protected_set_expr_location (r, loc);
8712 return r;
8715 r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
8716 if (r != error_mark_node)
8718 maybe_warn_about_useless_cast (loc, type, expr, complain);
8719 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8721 protected_set_expr_location (r, loc);
8722 return r;
8725 /* Like cp_build_c_cast, but for the c-common bits. */
8727 tree
8728 build_c_cast (location_t loc, tree type, tree expr)
8730 return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8733 /* Like the "build_c_cast" used for c-common, but using cp_expr to
8734 preserve location information even for tree nodes that don't
8735 support it. */
8737 cp_expr
8738 build_c_cast (location_t loc, tree type, cp_expr expr)
8740 cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8741 result.set_location (loc);
8742 return result;
8745 /* Build an expression representing an explicit C-style cast to type
8746 TYPE of expression EXPR. */
8748 tree
8749 cp_build_c_cast (location_t loc, tree type, tree expr,
8750 tsubst_flags_t complain)
8752 tree value = expr;
8753 tree result;
8754 bool valid_p;
8756 if (type == error_mark_node || error_operand_p (expr))
8757 return error_mark_node;
8759 if (processing_template_decl)
8761 tree t = build_min (CAST_EXPR, type,
8762 tree_cons (NULL_TREE, value, NULL_TREE));
8763 /* We don't know if it will or will not have side effects. */
8764 TREE_SIDE_EFFECTS (t) = 1;
8765 return convert_from_reference (t);
8768 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
8769 'Class') should always be retained, because this information aids
8770 in method lookup. */
8771 if (objc_is_object_ptr (type)
8772 && objc_is_object_ptr (TREE_TYPE (expr)))
8773 return build_nop (type, expr);
8775 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8776 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8777 if (!TYPE_REF_P (type)
8778 && TREE_CODE (value) == NOP_EXPR
8779 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
8780 value = TREE_OPERAND (value, 0);
8782 if (TREE_CODE (type) == ARRAY_TYPE)
8784 /* Allow casting from T1* to T2[] because Cfront allows it.
8785 NIHCL uses it. It is not valid ISO C++ however. */
8786 if (TYPE_PTR_P (TREE_TYPE (expr)))
8788 if (complain & tf_error)
8789 permerror (loc, "ISO C++ forbids casting to an array type %qT",
8790 type);
8791 else
8792 return error_mark_node;
8793 type = build_pointer_type (TREE_TYPE (type));
8795 else
8797 if (complain & tf_error)
8798 error_at (loc, "ISO C++ forbids casting to an array type %qT",
8799 type);
8800 return error_mark_node;
8804 if (FUNC_OR_METHOD_TYPE_P (type))
8806 if (complain & tf_error)
8807 error_at (loc, "invalid cast to function type %qT", type);
8808 return error_mark_node;
8811 if (TYPE_PTR_P (type)
8812 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
8813 /* Casting to an integer of smaller size is an error detected elsewhere. */
8814 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
8815 /* Don't warn about converting any constant. */
8816 && !TREE_CONSTANT (value))
8817 warning_at (loc, OPT_Wint_to_pointer_cast,
8818 "cast to pointer from integer of different size");
8820 /* A C-style cast can be a const_cast. */
8821 result = build_const_cast_1 (loc, type, value, complain & tf_warning,
8822 &valid_p);
8823 if (valid_p)
8825 if (result != error_mark_node)
8827 maybe_warn_about_useless_cast (loc, type, value, complain);
8828 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8830 return result;
8833 /* Or a static cast. */
8834 result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
8835 &valid_p, complain);
8836 /* Or a reinterpret_cast. */
8837 if (!valid_p)
8838 result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
8839 &valid_p, complain);
8840 /* The static_cast or reinterpret_cast may be followed by a
8841 const_cast. */
8842 if (valid_p
8843 /* A valid cast may result in errors if, for example, a
8844 conversion to an ambiguous base class is required. */
8845 && !error_operand_p (result))
8847 tree result_type;
8849 maybe_warn_about_useless_cast (loc, type, value, complain);
8850 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8852 /* Non-class rvalues always have cv-unqualified type. */
8853 if (!CLASS_TYPE_P (type))
8854 type = TYPE_MAIN_VARIANT (type);
8855 result_type = TREE_TYPE (result);
8856 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
8857 result_type = TYPE_MAIN_VARIANT (result_type);
8858 /* If the type of RESULT does not match TYPE, perform a
8859 const_cast to make it match. If the static_cast or
8860 reinterpret_cast succeeded, we will differ by at most
8861 cv-qualification, so the follow-on const_cast is guaranteed
8862 to succeed. */
8863 if (!same_type_p (non_reference (type), non_reference (result_type)))
8865 result = build_const_cast_1 (loc, type, result, false, &valid_p);
8866 gcc_assert (valid_p);
8868 return result;
8871 return error_mark_node;
8874 /* For use from the C common bits. */
8875 tree
8876 build_modify_expr (location_t location,
8877 tree lhs, tree /*lhs_origtype*/,
8878 enum tree_code modifycode,
8879 location_t /*rhs_location*/, tree rhs,
8880 tree /*rhs_origtype*/)
8882 return cp_build_modify_expr (location, lhs, modifycode, rhs,
8883 tf_warning_or_error);
8886 /* Build an assignment expression of lvalue LHS from value RHS.
8887 MODIFYCODE is the code for a binary operator that we use
8888 to combine the old value of LHS with RHS to get the new value.
8889 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
8891 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
8893 tree
8894 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8895 tree rhs, tsubst_flags_t complain)
8897 lhs = mark_lvalue_use_nonread (lhs);
8899 tree result = NULL_TREE;
8900 tree newrhs = rhs;
8901 tree lhstype = TREE_TYPE (lhs);
8902 tree olhs = lhs;
8903 tree olhstype = lhstype;
8904 bool plain_assign = (modifycode == NOP_EXPR);
8905 bool compound_side_effects_p = false;
8906 tree preeval = NULL_TREE;
8908 /* Avoid duplicate error messages from operands that had errors. */
8909 if (error_operand_p (lhs) || error_operand_p (rhs))
8910 return error_mark_node;
8912 while (TREE_CODE (lhs) == COMPOUND_EXPR)
8914 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
8915 compound_side_effects_p = true;
8916 lhs = TREE_OPERAND (lhs, 1);
8919 /* Handle control structure constructs used as "lvalues". Note that we
8920 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8921 switch (TREE_CODE (lhs))
8923 /* Handle --foo = 5; as these are valid constructs in C++. */
8924 case PREDECREMENT_EXPR:
8925 case PREINCREMENT_EXPR:
8926 if (compound_side_effects_p)
8927 newrhs = rhs = stabilize_expr (rhs, &preeval);
8928 lhs = genericize_compound_lvalue (lhs);
8929 maybe_add_compound:
8930 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8931 and looked through the COMPOUND_EXPRs, readd them now around
8932 the resulting lhs. */
8933 if (TREE_CODE (olhs) == COMPOUND_EXPR)
8935 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
8936 tree *ptr = &TREE_OPERAND (lhs, 1);
8937 for (olhs = TREE_OPERAND (olhs, 1);
8938 TREE_CODE (olhs) == COMPOUND_EXPR;
8939 olhs = TREE_OPERAND (olhs, 1))
8941 *ptr = build2 (COMPOUND_EXPR, lhstype,
8942 TREE_OPERAND (olhs, 0), *ptr);
8943 ptr = &TREE_OPERAND (*ptr, 1);
8946 break;
8948 case MODIFY_EXPR:
8949 if (compound_side_effects_p)
8950 newrhs = rhs = stabilize_expr (rhs, &preeval);
8951 lhs = genericize_compound_lvalue (lhs);
8952 goto maybe_add_compound;
8954 case MIN_EXPR:
8955 case MAX_EXPR:
8956 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8957 when neither operand has side-effects. */
8958 if (!lvalue_or_else (lhs, lv_assign, complain))
8959 return error_mark_node;
8961 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
8962 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
8964 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
8965 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
8966 boolean_type_node,
8967 TREE_OPERAND (lhs, 0),
8968 TREE_OPERAND (lhs, 1)),
8969 TREE_OPERAND (lhs, 0),
8970 TREE_OPERAND (lhs, 1));
8971 gcc_fallthrough ();
8973 /* Handle (a ? b : c) used as an "lvalue". */
8974 case COND_EXPR:
8976 /* Produce (a ? (b = rhs) : (c = rhs))
8977 except that the RHS goes through a save-expr
8978 so the code to compute it is only emitted once. */
8979 if (VOID_TYPE_P (TREE_TYPE (rhs)))
8981 if (complain & tf_error)
8982 error_at (cp_expr_loc_or_loc (rhs, loc),
8983 "void value not ignored as it ought to be");
8984 return error_mark_node;
8987 rhs = stabilize_expr (rhs, &preeval);
8989 /* Check this here to avoid odd errors when trying to convert
8990 a throw to the type of the COND_EXPR. */
8991 if (!lvalue_or_else (lhs, lv_assign, complain))
8992 return error_mark_node;
8994 tree op1 = TREE_OPERAND (lhs, 1);
8995 if (TREE_CODE (op1) != THROW_EXPR)
8996 op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
8997 /* When sanitizing undefined behavior, even when rhs doesn't need
8998 stabilization at this point, the sanitization might add extra
8999 SAVE_EXPRs in there and so make sure there is no tree sharing
9000 in the rhs, otherwise those SAVE_EXPRs will have initialization
9001 only in one of the two branches. */
9002 if (sanitize_flags_p (SANITIZE_UNDEFINED
9003 | SANITIZE_UNDEFINED_NONDEFAULT))
9004 rhs = unshare_expr (rhs);
9005 tree op2 = TREE_OPERAND (lhs, 2);
9006 if (TREE_CODE (op2) != THROW_EXPR)
9007 op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9008 tree cond = build_conditional_expr (input_location,
9009 TREE_OPERAND (lhs, 0), op1, op2,
9010 complain);
9012 if (cond == error_mark_node)
9013 return cond;
9014 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9015 and looked through the COMPOUND_EXPRs, readd them now around
9016 the resulting cond before adding the preevaluated rhs. */
9017 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9019 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9020 TREE_OPERAND (olhs, 0), cond);
9021 tree *ptr = &TREE_OPERAND (cond, 1);
9022 for (olhs = TREE_OPERAND (olhs, 1);
9023 TREE_CODE (olhs) == COMPOUND_EXPR;
9024 olhs = TREE_OPERAND (olhs, 1))
9026 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9027 TREE_OPERAND (olhs, 0), *ptr);
9028 ptr = &TREE_OPERAND (*ptr, 1);
9031 /* Make sure the code to compute the rhs comes out
9032 before the split. */
9033 result = cond;
9034 goto ret;
9037 default:
9038 lhs = olhs;
9039 break;
9042 if (modifycode == INIT_EXPR)
9044 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9045 /* Do the default thing. */;
9046 else if (TREE_CODE (rhs) == CONSTRUCTOR)
9048 /* Compound literal. */
9049 if (! same_type_p (TREE_TYPE (rhs), lhstype))
9050 /* Call convert to generate an error; see PR 11063. */
9051 rhs = convert (lhstype, rhs);
9052 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
9053 TREE_SIDE_EFFECTS (result) = 1;
9054 goto ret;
9056 else if (! MAYBE_CLASS_TYPE_P (lhstype))
9057 /* Do the default thing. */;
9058 else
9060 releasing_vec rhs_vec = make_tree_vector_single (rhs);
9061 result = build_special_member_call (lhs, complete_ctor_identifier,
9062 &rhs_vec, lhstype, LOOKUP_NORMAL,
9063 complain);
9064 if (result == NULL_TREE)
9065 return error_mark_node;
9066 goto ret;
9069 else
9071 lhs = require_complete_type_sfinae (lhs, complain);
9072 if (lhs == error_mark_node)
9073 return error_mark_node;
9075 if (modifycode == NOP_EXPR)
9077 if (c_dialect_objc ())
9079 result = objc_maybe_build_modify_expr (lhs, rhs);
9080 if (result)
9081 goto ret;
9084 /* `operator=' is not an inheritable operator. */
9085 if (! MAYBE_CLASS_TYPE_P (lhstype))
9086 /* Do the default thing. */;
9087 else
9089 result = build_new_op (input_location, MODIFY_EXPR,
9090 LOOKUP_NORMAL, lhs, rhs,
9091 make_node (NOP_EXPR), NULL_TREE,
9092 /*overload=*/NULL, complain);
9093 if (result == NULL_TREE)
9094 return error_mark_node;
9095 goto ret;
9097 lhstype = olhstype;
9099 else
9101 tree init = NULL_TREE;
9103 /* A binary op has been requested. Combine the old LHS
9104 value with the RHS producing the value we should actually
9105 store into the LHS. */
9106 gcc_assert (!((TYPE_REF_P (lhstype)
9107 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9108 || MAYBE_CLASS_TYPE_P (lhstype)));
9110 /* An expression of the form E1 op= E2. [expr.ass] says:
9111 "Such expressions are deprecated if E1 has volatile-qualified
9112 type." We warn here rather than in cp_genericize_r because
9113 for compound assignments we are supposed to warn even if the
9114 assignment is a discarded-value expression. */
9115 if (TREE_THIS_VOLATILE (lhs) || CP_TYPE_VOLATILE_P (lhstype))
9116 warning_at (loc, OPT_Wvolatile,
9117 "compound assignment with %<volatile%>-qualified left "
9118 "operand is deprecated");
9119 /* Preevaluate the RHS to make sure its evaluation is complete
9120 before the lvalue-to-rvalue conversion of the LHS:
9122 [expr.ass] With respect to an indeterminately-sequenced
9123 function call, the operation of a compound assignment is a
9124 single evaluation. [ Note: Therefore, a function call shall
9125 not intervene between the lvalue-to-rvalue conversion and the
9126 side effect associated with any single compound assignment
9127 operator. -- end note ] */
9128 lhs = cp_stabilize_reference (lhs);
9129 rhs = decay_conversion (rhs, complain);
9130 if (rhs == error_mark_node)
9131 return error_mark_node;
9132 rhs = stabilize_expr (rhs, &init);
9133 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9134 if (newrhs == error_mark_node)
9136 if (complain & tf_error)
9137 inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9138 modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9139 return error_mark_node;
9142 if (init)
9143 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9145 /* Now it looks like a plain assignment. */
9146 modifycode = NOP_EXPR;
9147 if (c_dialect_objc ())
9149 result = objc_maybe_build_modify_expr (lhs, newrhs);
9150 if (result)
9151 goto ret;
9154 gcc_assert (!TYPE_REF_P (lhstype));
9155 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9158 /* The left-hand side must be an lvalue. */
9159 if (!lvalue_or_else (lhs, lv_assign, complain))
9160 return error_mark_node;
9162 /* Warn about modifying something that is `const'. Don't warn if
9163 this is initialization. */
9164 if (modifycode != INIT_EXPR
9165 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9166 /* Functions are not modifiable, even though they are
9167 lvalues. */
9168 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9169 /* If it's an aggregate and any field is const, then it is
9170 effectively const. */
9171 || (CLASS_TYPE_P (lhstype)
9172 && C_TYPE_FIELDS_READONLY (lhstype))))
9174 if (complain & tf_error)
9175 cxx_readonly_error (loc, lhs, lv_assign);
9176 return error_mark_node;
9179 /* If storing into a structure or union member, it may have been given a
9180 lowered bitfield type. We need to convert to the declared type first,
9181 so retrieve it now. */
9183 olhstype = unlowered_expr_type (lhs);
9185 /* Convert new value to destination type. */
9187 if (TREE_CODE (lhstype) == ARRAY_TYPE)
9189 int from_array;
9191 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9193 if (modifycode != INIT_EXPR)
9195 if (complain & tf_error)
9196 error_at (loc,
9197 "assigning to an array from an initializer list");
9198 return error_mark_node;
9200 if (check_array_initializer (lhs, lhstype, newrhs))
9201 return error_mark_node;
9202 newrhs = digest_init (lhstype, newrhs, complain);
9203 if (newrhs == error_mark_node)
9204 return error_mark_node;
9207 /* C++11 8.5/17: "If the destination type is an array of characters,
9208 an array of char16_t, an array of char32_t, or an array of wchar_t,
9209 and the initializer is a string literal...". */
9210 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9211 == STRING_CST)
9212 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9213 && modifycode == INIT_EXPR)
9215 newrhs = digest_init (lhstype, newrhs, complain);
9216 if (newrhs == error_mark_node)
9217 return error_mark_node;
9220 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9221 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9223 if (complain & tf_error)
9224 error_at (loc, "incompatible types in assignment of %qT to %qT",
9225 TREE_TYPE (rhs), lhstype);
9226 return error_mark_node;
9229 /* Allow array assignment in compiler-generated code. */
9230 else if (!current_function_decl
9231 || !DECL_DEFAULTED_FN (current_function_decl))
9233 /* This routine is used for both initialization and assignment.
9234 Make sure the diagnostic message differentiates the context. */
9235 if (complain & tf_error)
9237 if (modifycode == INIT_EXPR)
9238 error_at (loc, "array used as initializer");
9239 else
9240 error_at (loc, "invalid array assignment");
9242 return error_mark_node;
9245 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9246 ? 1 + (modifycode != INIT_EXPR): 0;
9247 result = build_vec_init (lhs, NULL_TREE, newrhs,
9248 /*explicit_value_init_p=*/false,
9249 from_array, complain);
9250 goto ret;
9253 if (modifycode == INIT_EXPR)
9254 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9255 LOOKUP_ONLYCONVERTING. */
9256 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9257 ICR_INIT, NULL_TREE, 0,
9258 complain | tf_no_cleanup);
9259 else
9260 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9261 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9263 if (!same_type_p (lhstype, olhstype))
9264 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9266 if (modifycode != INIT_EXPR)
9268 if (TREE_CODE (newrhs) == CALL_EXPR
9269 && TYPE_NEEDS_CONSTRUCTING (lhstype))
9270 newrhs = build_cplus_new (lhstype, newrhs, complain);
9272 /* Can't initialize directly from a TARGET_EXPR, since that would
9273 cause the lhs to be constructed twice, and possibly result in
9274 accidental self-initialization. So we force the TARGET_EXPR to be
9275 expanded without a target. */
9276 if (TREE_CODE (newrhs) == TARGET_EXPR)
9277 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9278 TREE_OPERAND (newrhs, 0));
9281 if (newrhs == error_mark_node)
9282 return error_mark_node;
9284 if (c_dialect_objc () && flag_objc_gc)
9286 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9288 if (result)
9289 goto ret;
9292 result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9293 lhstype, lhs, newrhs);
9295 TREE_SIDE_EFFECTS (result) = 1;
9296 if (!plain_assign)
9297 suppress_warning (result, OPT_Wparentheses);
9299 ret:
9300 if (preeval)
9301 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
9302 return result;
9305 cp_expr
9306 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9307 tree rhs, tree lookups, tsubst_flags_t complain)
9309 tree orig_lhs = lhs;
9310 tree orig_rhs = rhs;
9311 tree overload = NULL_TREE;
9313 if (lhs == error_mark_node || rhs == error_mark_node)
9314 return cp_expr (error_mark_node, loc);
9316 if (processing_template_decl)
9318 if (modifycode == NOP_EXPR
9319 || type_dependent_expression_p (lhs)
9320 || type_dependent_expression_p (rhs))
9322 tree op = build_min_nt_loc (loc, modifycode, NULL_TREE, NULL_TREE);
9323 tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
9324 if (modifycode != NOP_EXPR)
9325 TREE_TYPE (rval)
9326 = build_dependent_operator_type (lookups, modifycode, true);
9327 return rval;
9330 lhs = build_non_dependent_expr (lhs);
9331 rhs = build_non_dependent_expr (rhs);
9334 if (modifycode != NOP_EXPR)
9336 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
9337 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
9338 lhs, rhs, op, lookups, &overload, complain);
9339 if (rval)
9341 if (rval == error_mark_node)
9342 return rval;
9343 suppress_warning (rval /* What warning? */);
9344 if (processing_template_decl)
9346 if (overload != NULL_TREE)
9347 return (build_min_non_dep_op_overload
9348 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
9350 return (build_min_non_dep
9351 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
9353 return rval;
9356 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
9359 /* Helper function for get_delta_difference which assumes FROM is a base
9360 class of TO. Returns a delta for the conversion of pointer-to-member
9361 of FROM to pointer-to-member of TO. If the conversion is invalid and
9362 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9363 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9364 If C_CAST_P is true, this conversion is taking place as part of a
9365 C-style cast. */
9367 static tree
9368 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
9369 tsubst_flags_t complain)
9371 tree binfo;
9372 base_kind kind;
9374 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
9375 &kind, complain);
9377 if (binfo == error_mark_node)
9379 if (!(complain & tf_error))
9380 return error_mark_node;
9382 inform (input_location, " in pointer to member function conversion");
9383 return size_zero_node;
9385 else if (binfo)
9387 if (kind != bk_via_virtual)
9388 return BINFO_OFFSET (binfo);
9389 else
9390 /* FROM is a virtual base class of TO. Issue an error or warning
9391 depending on whether or not this is a reinterpret cast. */
9393 if (!(complain & tf_error))
9394 return error_mark_node;
9396 error ("pointer to member conversion via virtual base %qT",
9397 BINFO_TYPE (binfo_from_vbase (binfo)));
9399 return size_zero_node;
9402 else
9403 return NULL_TREE;
9406 /* Get difference in deltas for different pointer to member function
9407 types. If the conversion is invalid and tf_error is not set in
9408 COMPLAIN, returns error_mark_node, otherwise returns an integer
9409 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9410 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9411 conversions as well. If C_CAST_P is true this conversion is taking
9412 place as part of a C-style cast.
9414 Note that the naming of FROM and TO is kind of backwards; the return
9415 value is what we add to a TO in order to get a FROM. They are named
9416 this way because we call this function to find out how to convert from
9417 a pointer to member of FROM to a pointer to member of TO. */
9419 static tree
9420 get_delta_difference (tree from, tree to,
9421 bool allow_inverse_p,
9422 bool c_cast_p, tsubst_flags_t complain)
9424 tree result;
9426 if (same_type_ignoring_top_level_qualifiers_p (from, to))
9427 /* Pointer to member of incomplete class is permitted*/
9428 result = size_zero_node;
9429 else
9430 result = get_delta_difference_1 (from, to, c_cast_p, complain);
9432 if (result == error_mark_node)
9433 return error_mark_node;
9435 if (!result)
9437 if (!allow_inverse_p)
9439 if (!(complain & tf_error))
9440 return error_mark_node;
9442 error_not_base_type (from, to);
9443 inform (input_location, " in pointer to member conversion");
9444 result = size_zero_node;
9446 else
9448 result = get_delta_difference_1 (to, from, c_cast_p, complain);
9450 if (result == error_mark_node)
9451 return error_mark_node;
9453 if (result)
9454 result = size_diffop_loc (input_location,
9455 size_zero_node, result);
9456 else
9458 if (!(complain & tf_error))
9459 return error_mark_node;
9461 error_not_base_type (from, to);
9462 inform (input_location, " in pointer to member conversion");
9463 result = size_zero_node;
9468 return convert_to_integer (ptrdiff_type_node, result);
9471 /* Return a constructor for the pointer-to-member-function TYPE using
9472 the other components as specified. */
9474 tree
9475 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
9477 tree u = NULL_TREE;
9478 tree delta_field;
9479 tree pfn_field;
9480 vec<constructor_elt, va_gc> *v;
9482 /* Pull the FIELD_DECLs out of the type. */
9483 pfn_field = TYPE_FIELDS (type);
9484 delta_field = DECL_CHAIN (pfn_field);
9486 /* Make sure DELTA has the type we want. */
9487 delta = convert_and_check (input_location, delta_type_node, delta);
9489 /* Convert to the correct target type if necessary. */
9490 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
9492 /* Finish creating the initializer. */
9493 vec_alloc (v, 2);
9494 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
9495 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
9496 u = build_constructor (type, v);
9497 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
9498 TREE_STATIC (u) = (TREE_CONSTANT (u)
9499 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
9500 != NULL_TREE)
9501 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
9502 != NULL_TREE));
9503 return u;
9506 /* Build a constructor for a pointer to member function. It can be
9507 used to initialize global variables, local variable, or used
9508 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
9509 want to be.
9511 If FORCE is nonzero, then force this conversion, even if
9512 we would rather not do it. Usually set when using an explicit
9513 cast. A C-style cast is being processed iff C_CAST_P is true.
9515 Return error_mark_node, if something goes wrong. */
9517 tree
9518 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
9519 tsubst_flags_t complain)
9521 tree fn;
9522 tree pfn_type;
9523 tree to_type;
9525 if (error_operand_p (pfn))
9526 return error_mark_node;
9528 pfn_type = TREE_TYPE (pfn);
9529 to_type = build_ptrmemfunc_type (type);
9531 /* Handle multiple conversions of pointer to member functions. */
9532 if (TYPE_PTRMEMFUNC_P (pfn_type))
9534 tree delta = NULL_TREE;
9535 tree npfn = NULL_TREE;
9536 tree n;
9538 if (!force
9539 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
9540 LOOKUP_NORMAL, complain))
9542 if (complain & tf_error)
9543 error ("invalid conversion to type %qT from type %qT",
9544 to_type, pfn_type);
9545 else
9546 return error_mark_node;
9549 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
9550 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
9551 force,
9552 c_cast_p, complain);
9553 if (n == error_mark_node)
9554 return error_mark_node;
9556 /* We don't have to do any conversion to convert a
9557 pointer-to-member to its own type. But, we don't want to
9558 just return a PTRMEM_CST if there's an explicit cast; that
9559 cast should make the expression an invalid template argument. */
9560 if (TREE_CODE (pfn) != PTRMEM_CST)
9562 if (same_type_p (to_type, pfn_type))
9563 return pfn;
9564 else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
9565 return build_reinterpret_cast (input_location, to_type, pfn,
9566 complain);
9569 if (TREE_SIDE_EFFECTS (pfn))
9570 pfn = save_expr (pfn);
9572 /* Obtain the function pointer and the current DELTA. */
9573 if (TREE_CODE (pfn) == PTRMEM_CST)
9574 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
9575 else
9577 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
9578 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
9581 /* Just adjust the DELTA field. */
9582 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9583 (TREE_TYPE (delta), ptrdiff_type_node));
9584 if (!integer_zerop (n))
9586 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
9587 n = cp_build_binary_op (input_location,
9588 LSHIFT_EXPR, n, integer_one_node,
9589 complain);
9590 delta = cp_build_binary_op (input_location,
9591 PLUS_EXPR, delta, n, complain);
9593 return build_ptrmemfunc1 (to_type, delta, npfn);
9596 /* Handle null pointer to member function conversions. */
9597 if (null_ptr_cst_p (pfn))
9599 pfn = cp_build_c_cast (input_location, type, pfn, complain);
9600 return build_ptrmemfunc1 (to_type,
9601 integer_zero_node,
9602 pfn);
9605 if (type_unknown_p (pfn))
9606 return instantiate_type (type, pfn, complain);
9608 fn = TREE_OPERAND (pfn, 0);
9609 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9610 /* In a template, we will have preserved the
9611 OFFSET_REF. */
9612 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
9613 return make_ptrmem_cst (to_type, fn);
9616 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
9617 given by CST.
9619 ??? There is no consistency as to the types returned for the above
9620 values. Some code acts as if it were a sizetype and some as if it were
9621 integer_type_node. */
9623 void
9624 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
9626 tree type = TREE_TYPE (cst);
9627 tree fn = PTRMEM_CST_MEMBER (cst);
9628 tree ptr_class, fn_class;
9630 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9632 /* The class that the function belongs to. */
9633 fn_class = DECL_CONTEXT (fn);
9635 /* The class that we're creating a pointer to member of. */
9636 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
9638 /* First, calculate the adjustment to the function's class. */
9639 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
9640 /*c_cast_p=*/0, tf_warning_or_error);
9642 if (!DECL_VIRTUAL_P (fn))
9644 tree t = build_addr_func (fn, tf_warning_or_error);
9645 if (TREE_CODE (t) == ADDR_EXPR)
9646 SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
9647 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
9649 else
9651 /* If we're dealing with a virtual function, we have to adjust 'this'
9652 again, to point to the base which provides the vtable entry for
9653 fn; the call will do the opposite adjustment. */
9654 tree orig_class = DECL_CONTEXT (fn);
9655 tree binfo = binfo_or_else (orig_class, fn_class);
9656 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9657 *delta, BINFO_OFFSET (binfo));
9659 /* We set PFN to the vtable offset at which the function can be
9660 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
9661 case delta is shifted left, and then incremented). */
9662 *pfn = DECL_VINDEX (fn);
9663 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
9664 TYPE_SIZE_UNIT (vtable_entry_type));
9666 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
9668 case ptrmemfunc_vbit_in_pfn:
9669 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
9670 integer_one_node);
9671 break;
9673 case ptrmemfunc_vbit_in_delta:
9674 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
9675 *delta, integer_one_node);
9676 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9677 *delta, integer_one_node);
9678 break;
9680 default:
9681 gcc_unreachable ();
9684 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
9688 /* Return an expression for PFN from the pointer-to-member function
9689 given by T. */
9691 static tree
9692 pfn_from_ptrmemfunc (tree t)
9694 if (TREE_CODE (t) == PTRMEM_CST)
9696 tree delta;
9697 tree pfn;
9699 expand_ptrmemfunc_cst (t, &delta, &pfn);
9700 if (pfn)
9701 return pfn;
9704 return build_ptrmemfunc_access_expr (t, pfn_identifier);
9707 /* Return an expression for DELTA from the pointer-to-member function
9708 given by T. */
9710 static tree
9711 delta_from_ptrmemfunc (tree t)
9713 if (TREE_CODE (t) == PTRMEM_CST)
9715 tree delta;
9716 tree pfn;
9718 expand_ptrmemfunc_cst (t, &delta, &pfn);
9719 if (delta)
9720 return delta;
9723 return build_ptrmemfunc_access_expr (t, delta_identifier);
9726 /* Convert value RHS to type TYPE as preparation for an assignment to
9727 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
9728 implicit conversion is. If FNDECL is non-NULL, we are doing the
9729 conversion in order to pass the PARMNUMth argument of FNDECL.
9730 If FNDECL is NULL, we are doing the conversion in function pointer
9731 argument passing, conversion in initialization, etc. */
9733 static tree
9734 convert_for_assignment (tree type, tree rhs,
9735 impl_conv_rhs errtype, tree fndecl, int parmnum,
9736 tsubst_flags_t complain, int flags)
9738 tree rhstype;
9739 enum tree_code coder;
9741 location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
9742 bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
9743 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
9744 but preserve location wrappers. */
9745 if (TREE_CODE (rhs) == NON_LVALUE_EXPR
9746 && !location_wrapper_p (rhs))
9747 rhs = TREE_OPERAND (rhs, 0);
9749 /* Handle [dcl.init.list] direct-list-initialization from
9750 single element of enumeration with a fixed underlying type. */
9751 if (is_direct_enum_init (type, rhs))
9753 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
9754 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
9756 warning_sentinel w (warn_useless_cast);
9757 warning_sentinel w2 (warn_ignored_qualifiers);
9758 rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
9760 else
9761 rhs = error_mark_node;
9764 rhstype = TREE_TYPE (rhs);
9765 coder = TREE_CODE (rhstype);
9767 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
9768 && vector_types_convertible_p (type, rhstype, true))
9770 rhs = mark_rvalue_use (rhs);
9771 return convert (type, rhs);
9774 if (rhs == error_mark_node || rhstype == error_mark_node)
9775 return error_mark_node;
9776 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
9777 return error_mark_node;
9779 /* The RHS of an assignment cannot have void type. */
9780 if (coder == VOID_TYPE)
9782 if (complain & tf_error)
9783 error_at (rhs_loc, "void value not ignored as it ought to be");
9784 return error_mark_node;
9787 if (c_dialect_objc ())
9789 int parmno;
9790 tree selector;
9791 tree rname = fndecl;
9793 switch (errtype)
9795 case ICR_ASSIGN:
9796 parmno = -1;
9797 break;
9798 case ICR_INIT:
9799 parmno = -2;
9800 break;
9801 default:
9802 selector = objc_message_selector ();
9803 parmno = parmnum;
9804 if (selector && parmno > 1)
9806 rname = selector;
9807 parmno -= 1;
9811 if (objc_compare_types (type, rhstype, parmno, rname))
9813 rhs = mark_rvalue_use (rhs);
9814 return convert (type, rhs);
9818 /* [expr.ass]
9820 The expression is implicitly converted (clause _conv_) to the
9821 cv-unqualified type of the left operand.
9823 We allow bad conversions here because by the time we get to this point
9824 we are committed to doing the conversion. If we end up doing a bad
9825 conversion, convert_like will complain. */
9826 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
9828 /* When -Wno-pmf-conversions is use, we just silently allow
9829 conversions from pointers-to-members to plain pointers. If
9830 the conversion doesn't work, cp_convert will complain. */
9831 if (!warn_pmf2ptr
9832 && TYPE_PTR_P (type)
9833 && TYPE_PTRMEMFUNC_P (rhstype))
9834 rhs = cp_convert (strip_top_quals (type), rhs, complain);
9835 else
9837 if (complain & tf_error)
9839 /* If the right-hand side has unknown type, then it is an
9840 overloaded function. Call instantiate_type to get error
9841 messages. */
9842 if (rhstype == unknown_type_node)
9844 tree r = instantiate_type (type, rhs, tf_warning_or_error);
9845 /* -fpermissive might allow this; recurse. */
9846 if (!seen_error ())
9847 return convert_for_assignment (type, r, errtype, fndecl,
9848 parmnum, complain, flags);
9850 else if (fndecl)
9851 complain_about_bad_argument (rhs_loc,
9852 rhstype, type,
9853 fndecl, parmnum);
9854 else
9856 range_label_for_type_mismatch label (rhstype, type);
9857 gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
9858 switch (errtype)
9860 case ICR_DEFAULT_ARGUMENT:
9861 error_at (&richloc,
9862 "cannot convert %qH to %qI in default argument",
9863 rhstype, type);
9864 break;
9865 case ICR_ARGPASS:
9866 error_at (&richloc,
9867 "cannot convert %qH to %qI in argument passing",
9868 rhstype, type);
9869 break;
9870 case ICR_CONVERTING:
9871 error_at (&richloc, "cannot convert %qH to %qI",
9872 rhstype, type);
9873 break;
9874 case ICR_INIT:
9875 error_at (&richloc,
9876 "cannot convert %qH to %qI in initialization",
9877 rhstype, type);
9878 break;
9879 case ICR_RETURN:
9880 error_at (&richloc, "cannot convert %qH to %qI in return",
9881 rhstype, type);
9882 break;
9883 case ICR_ASSIGN:
9884 error_at (&richloc,
9885 "cannot convert %qH to %qI in assignment",
9886 rhstype, type);
9887 break;
9888 default:
9889 gcc_unreachable();
9892 if (TYPE_PTR_P (rhstype)
9893 && TYPE_PTR_P (type)
9894 && CLASS_TYPE_P (TREE_TYPE (rhstype))
9895 && CLASS_TYPE_P (TREE_TYPE (type))
9896 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
9897 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
9898 (TREE_TYPE (rhstype))),
9899 "class type %qT is incomplete", TREE_TYPE (rhstype));
9901 return error_mark_node;
9904 if (warn_suggest_attribute_format)
9906 const enum tree_code codel = TREE_CODE (type);
9907 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9908 && coder == codel
9909 && check_missing_format_attribute (type, rhstype)
9910 && (complain & tf_warning))
9911 switch (errtype)
9913 case ICR_ARGPASS:
9914 case ICR_DEFAULT_ARGUMENT:
9915 if (fndecl)
9916 warning (OPT_Wsuggest_attribute_format,
9917 "parameter %qP of %qD might be a candidate "
9918 "for a format attribute", parmnum, fndecl);
9919 else
9920 warning (OPT_Wsuggest_attribute_format,
9921 "parameter might be a candidate "
9922 "for a format attribute");
9923 break;
9924 case ICR_CONVERTING:
9925 warning (OPT_Wsuggest_attribute_format,
9926 "target of conversion might be a candidate "
9927 "for a format attribute");
9928 break;
9929 case ICR_INIT:
9930 warning (OPT_Wsuggest_attribute_format,
9931 "target of initialization might be a candidate "
9932 "for a format attribute");
9933 break;
9934 case ICR_RETURN:
9935 warning (OPT_Wsuggest_attribute_format,
9936 "return type might be a candidate "
9937 "for a format attribute");
9938 break;
9939 case ICR_ASSIGN:
9940 warning (OPT_Wsuggest_attribute_format,
9941 "left-hand side of assignment might be a candidate "
9942 "for a format attribute");
9943 break;
9944 default:
9945 gcc_unreachable();
9949 /* If -Wparentheses, warn about a = b = c when a has type bool and b
9950 does not. */
9951 if (warn_parentheses
9952 && TREE_CODE (type) == BOOLEAN_TYPE
9953 && TREE_CODE (rhs) == MODIFY_EXPR
9954 && !warning_suppressed_p (rhs, OPT_Wparentheses)
9955 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
9956 && (complain & tf_warning)
9957 && warning_at (rhs_loc, OPT_Wparentheses,
9958 "suggest parentheses around assignment used as "
9959 "truth value"))
9960 suppress_warning (rhs, OPT_Wparentheses);
9962 if (complain & tf_warning)
9963 warn_for_address_or_pointer_of_packed_member (type, rhs);
9965 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
9966 complain, flags);
9969 /* Convert RHS to be of type TYPE.
9970 If EXP is nonzero, it is the target of the initialization.
9971 ERRTYPE indicates what kind of error the implicit conversion is.
9973 Two major differences between the behavior of
9974 `convert_for_assignment' and `convert_for_initialization'
9975 are that references are bashed in the former, while
9976 copied in the latter, and aggregates are assigned in
9977 the former (operator=) while initialized in the
9978 latter (X(X&)).
9980 If using constructor make sure no conversion operator exists, if one does
9981 exist, an ambiguity exists. */
9983 tree
9984 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
9985 impl_conv_rhs errtype, tree fndecl, int parmnum,
9986 tsubst_flags_t complain)
9988 enum tree_code codel = TREE_CODE (type);
9989 tree rhstype;
9990 enum tree_code coder;
9992 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9993 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9994 if (TREE_CODE (rhs) == NOP_EXPR
9995 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
9996 && codel != REFERENCE_TYPE)
9997 rhs = TREE_OPERAND (rhs, 0);
9999 if (type == error_mark_node
10000 || rhs == error_mark_node
10001 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10002 return error_mark_node;
10004 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10006 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10007 && TREE_CODE (type) != ARRAY_TYPE
10008 && (!TYPE_REF_P (type)
10009 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10010 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10011 && !TYPE_REFFN_P (type))
10012 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10013 rhs = decay_conversion (rhs, complain);
10015 rhstype = TREE_TYPE (rhs);
10016 coder = TREE_CODE (rhstype);
10018 if (coder == ERROR_MARK)
10019 return error_mark_node;
10021 /* We accept references to incomplete types, so we can
10022 return here before checking if RHS is of complete type. */
10024 if (codel == REFERENCE_TYPE)
10026 auto_diagnostic_group d;
10027 /* This should eventually happen in convert_arguments. */
10028 int savew = 0, savee = 0;
10030 if (fndecl)
10031 savew = warningcount + werrorcount, savee = errorcount;
10032 rhs = initialize_reference (type, rhs, flags, complain);
10034 if (fndecl
10035 && (warningcount + werrorcount > savew || errorcount > savee))
10036 inform (get_fndecl_argument_location (fndecl, parmnum),
10037 "in passing argument %P of %qD", parmnum, fndecl);
10038 return rhs;
10041 if (exp != 0)
10042 exp = require_complete_type_sfinae (exp, complain);
10043 if (exp == error_mark_node)
10044 return error_mark_node;
10046 type = complete_type (type);
10048 if (DIRECT_INIT_EXPR_P (type, rhs))
10049 /* Don't try to do copy-initialization if we already have
10050 direct-initialization. */
10051 return rhs;
10053 if (MAYBE_CLASS_TYPE_P (type))
10054 return perform_implicit_conversion_flags (type, rhs, complain, flags);
10056 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10057 complain, flags);
10060 /* If RETVAL is the address of, or a reference to, a local variable or
10061 temporary give an appropriate warning and return true. */
10063 static bool
10064 maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10066 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10067 tree whats_returned = fold_for_warn (retval);
10068 if (!loc)
10069 loc = cp_expr_loc_or_input_loc (retval);
10071 for (;;)
10073 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10074 whats_returned = TREE_OPERAND (whats_returned, 1);
10075 else if (CONVERT_EXPR_P (whats_returned)
10076 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10077 whats_returned = TREE_OPERAND (whats_returned, 0);
10078 else
10079 break;
10082 if (TREE_CODE (whats_returned) == TARGET_EXPR
10083 && is_std_init_list (TREE_TYPE (whats_returned)))
10085 tree init = TARGET_EXPR_INITIAL (whats_returned);
10086 if (TREE_CODE (init) == CONSTRUCTOR)
10087 /* Pull out the array address. */
10088 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10089 else if (TREE_CODE (init) == INDIRECT_REF)
10090 /* The source of a trivial copy looks like *(T*)&var. */
10091 whats_returned = TREE_OPERAND (init, 0);
10092 else
10093 return false;
10094 STRIP_NOPS (whats_returned);
10097 /* As a special case, we handle a call to std::move or std::forward. */
10098 if (TREE_CODE (whats_returned) == CALL_EXPR
10099 && (is_std_move_p (whats_returned)
10100 || is_std_forward_p (whats_returned)))
10102 tree arg = CALL_EXPR_ARG (whats_returned, 0);
10103 return maybe_warn_about_returning_address_of_local (arg, loc);
10106 if (TREE_CODE (whats_returned) != ADDR_EXPR)
10107 return false;
10108 whats_returned = TREE_OPERAND (whats_returned, 0);
10110 while (TREE_CODE (whats_returned) == COMPONENT_REF
10111 || TREE_CODE (whats_returned) == ARRAY_REF)
10112 whats_returned = TREE_OPERAND (whats_returned, 0);
10114 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10115 || TREE_CODE (whats_returned) == TARGET_EXPR)
10117 if (TYPE_REF_P (valtype))
10118 warning_at (loc, OPT_Wreturn_local_addr,
10119 "returning reference to temporary");
10120 else if (is_std_init_list (valtype))
10121 warning_at (loc, OPT_Winit_list_lifetime,
10122 "returning temporary %<initializer_list%> does not extend "
10123 "the lifetime of the underlying array");
10124 return true;
10127 STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10129 if (DECL_P (whats_returned)
10130 && DECL_NAME (whats_returned)
10131 && DECL_FUNCTION_SCOPE_P (whats_returned)
10132 && !is_capture_proxy (whats_returned)
10133 && !(TREE_STATIC (whats_returned)
10134 || TREE_PUBLIC (whats_returned)))
10136 if (VAR_P (whats_returned)
10137 && DECL_DECOMPOSITION_P (whats_returned)
10138 && DECL_DECOMP_BASE (whats_returned)
10139 && DECL_HAS_VALUE_EXPR_P (whats_returned))
10141 /* When returning address of a structured binding, if the structured
10142 binding is not a reference, continue normally, if it is a
10143 reference, recurse on the initializer of the structured
10144 binding. */
10145 tree base = DECL_DECOMP_BASE (whats_returned);
10146 if (TYPE_REF_P (TREE_TYPE (base)))
10148 if (tree init = DECL_INITIAL (base))
10149 return maybe_warn_about_returning_address_of_local (init, loc);
10150 else
10151 return false;
10154 bool w = false;
10155 auto_diagnostic_group d;
10156 if (TYPE_REF_P (valtype))
10157 w = warning_at (loc, OPT_Wreturn_local_addr,
10158 "reference to local variable %qD returned",
10159 whats_returned);
10160 else if (is_std_init_list (valtype))
10161 w = warning_at (loc, OPT_Winit_list_lifetime,
10162 "returning local %<initializer_list%> variable %qD "
10163 "does not extend the lifetime of the underlying array",
10164 whats_returned);
10165 else if (POINTER_TYPE_P (valtype)
10166 && TREE_CODE (whats_returned) == LABEL_DECL)
10167 w = warning_at (loc, OPT_Wreturn_local_addr,
10168 "address of label %qD returned",
10169 whats_returned);
10170 else if (POINTER_TYPE_P (valtype))
10171 w = warning_at (loc, OPT_Wreturn_local_addr,
10172 "address of local variable %qD returned",
10173 whats_returned);
10174 if (w)
10175 inform (DECL_SOURCE_LOCATION (whats_returned),
10176 "declared here");
10177 return true;
10180 return false;
10183 /* Returns true if DECL is in the std namespace. */
10185 bool
10186 decl_in_std_namespace_p (tree decl)
10188 while (decl)
10190 decl = decl_namespace_context (decl);
10191 if (DECL_NAMESPACE_STD_P (decl))
10192 return true;
10193 /* Allow inline namespaces inside of std namespace, e.g. with
10194 --enable-symvers=gnu-versioned-namespace std::forward would be
10195 actually std::_8::forward. */
10196 if (!DECL_NAMESPACE_INLINE_P (decl))
10197 return false;
10198 decl = CP_DECL_CONTEXT (decl);
10200 return false;
10203 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10205 static bool
10206 is_std_forward_p (tree fn)
10208 /* std::forward only takes one argument. */
10209 if (call_expr_nargs (fn) != 1)
10210 return false;
10212 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10213 if (!decl_in_std_namespace_p (fndecl))
10214 return false;
10216 tree name = DECL_NAME (fndecl);
10217 return name && id_equal (name, "forward");
10220 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10222 static bool
10223 is_std_move_p (tree fn)
10225 /* std::move only takes one argument. */
10226 if (call_expr_nargs (fn) != 1)
10227 return false;
10229 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10230 if (!decl_in_std_namespace_p (fndecl))
10231 return false;
10233 tree name = DECL_NAME (fndecl);
10234 return name && id_equal (name, "move");
10237 /* Returns true if RETVAL is a good candidate for the NRVO as per
10238 [class.copy.elision]. FUNCTYPE is the type the function is declared
10239 to return. */
10241 static bool
10242 can_do_nrvo_p (tree retval, tree functype)
10244 if (functype == error_mark_node)
10245 return false;
10246 if (retval)
10247 STRIP_ANY_LOCATION_WRAPPER (retval);
10248 tree result = DECL_RESULT (current_function_decl);
10249 return (retval != NULL_TREE
10250 && !processing_template_decl
10251 /* Must be a local, automatic variable. */
10252 && VAR_P (retval)
10253 && DECL_CONTEXT (retval) == current_function_decl
10254 && !TREE_STATIC (retval)
10255 /* And not a lambda or anonymous union proxy. */
10256 && !DECL_HAS_VALUE_EXPR_P (retval)
10257 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10258 /* The cv-unqualified type of the returned value must be the
10259 same as the cv-unqualified return type of the
10260 function. */
10261 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
10262 (TYPE_MAIN_VARIANT (functype)))
10263 /* And the returned value must be non-volatile. */
10264 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10267 /* If we should treat RETVAL, an expression being returned, as if it were
10268 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10269 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10270 context (rather than throw). */
10272 tree
10273 treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10275 if (cxx_dialect == cxx98)
10276 return NULL_TREE;
10278 tree retval = expr;
10279 STRIP_ANY_LOCATION_WRAPPER (retval);
10280 if (REFERENCE_REF_P (retval))
10281 retval = TREE_OPERAND (retval, 0);
10283 /* An implicitly movable entity is a variable of automatic storage duration
10284 that is either a non-volatile object or (C++20) an rvalue reference to a
10285 non-volatile object type. */
10286 if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
10287 || TREE_CODE (retval) == PARM_DECL)
10288 && !TREE_STATIC (retval)
10289 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
10290 && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
10291 || (cxx_dialect >= cxx20
10292 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
10293 return NULL_TREE;
10295 /* If the expression in a return or co_return statement is a (possibly
10296 parenthesized) id-expression that names an implicitly movable entity
10297 declared in the body or parameter-declaration-clause of the innermost
10298 enclosing function or lambda-expression, */
10299 if (DECL_CONTEXT (retval) != current_function_decl)
10300 return NULL_TREE;
10301 if (return_p)
10302 return set_implicit_rvalue_p (move (expr));
10304 /* if the operand of a throw-expression is a (possibly parenthesized)
10305 id-expression that names an implicitly movable entity whose scope does not
10306 extend beyond the compound-statement of the innermost try-block or
10307 function-try-block (if any) whose compound-statement or ctor-initializer
10308 encloses the throw-expression, */
10310 /* C++20 added move on throw of parms. */
10311 if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
10312 return NULL_TREE;
10314 for (cp_binding_level *b = current_binding_level;
10315 ; b = b->level_chain)
10317 for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
10318 if (decl == retval)
10319 return set_implicit_rvalue_p (move (expr));
10320 if (b->kind == sk_function_parms || b->kind == sk_try)
10321 return NULL_TREE;
10325 /* Warn about wrong usage of std::move in a return statement. RETVAL
10326 is the expression we are returning; FUNCTYPE is the type the function
10327 is declared to return. */
10329 static void
10330 maybe_warn_pessimizing_move (tree retval, tree functype)
10332 if (!(warn_pessimizing_move || warn_redundant_move))
10333 return;
10335 location_t loc = cp_expr_loc_or_input_loc (retval);
10337 /* C++98 doesn't know move. */
10338 if (cxx_dialect < cxx11)
10339 return;
10341 /* Wait until instantiation time, since we can't gauge if we should do
10342 the NRVO until then. */
10343 if (processing_template_decl)
10344 return;
10346 /* This is only interesting for class types. */
10347 if (!CLASS_TYPE_P (functype))
10348 return;
10350 /* We're looking for *std::move<T&> ((T &) &arg). */
10351 if (REFERENCE_REF_P (retval)
10352 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
10354 tree fn = TREE_OPERAND (retval, 0);
10355 if (is_std_move_p (fn))
10357 tree arg = CALL_EXPR_ARG (fn, 0);
10358 tree moved;
10359 if (TREE_CODE (arg) != NOP_EXPR)
10360 return;
10361 arg = TREE_OPERAND (arg, 0);
10362 if (TREE_CODE (arg) != ADDR_EXPR)
10363 return;
10364 arg = TREE_OPERAND (arg, 0);
10365 arg = convert_from_reference (arg);
10366 /* Warn if we could do copy elision were it not for the move. */
10367 if (can_do_nrvo_p (arg, functype))
10369 auto_diagnostic_group d;
10370 if (warning_at (loc, OPT_Wpessimizing_move,
10371 "moving a local object in a return statement "
10372 "prevents copy elision"))
10373 inform (loc, "remove %<std::move%> call");
10375 /* Warn if the move is redundant. It is redundant when we would
10376 do maybe-rvalue overload resolution even without std::move. */
10377 else if (warn_redundant_move
10378 && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
10380 /* Make sure that the overload resolution would actually succeed
10381 if we removed the std::move call. */
10382 tree t = convert_for_initialization (NULL_TREE, functype,
10383 moved,
10384 (LOOKUP_NORMAL
10385 | LOOKUP_ONLYCONVERTING
10386 | LOOKUP_PREFER_RVALUE),
10387 ICR_RETURN, NULL_TREE, 0,
10388 tf_none);
10389 /* If this worked, implicit rvalue would work, so the call to
10390 std::move is redundant. */
10391 if (t != error_mark_node)
10393 auto_diagnostic_group d;
10394 if (warning_at (loc, OPT_Wredundant_move,
10395 "redundant move in return statement"))
10396 inform (loc, "remove %<std::move%> call");
10403 /* Check that returning RETVAL from the current function is valid.
10404 Return an expression explicitly showing all conversions required to
10405 change RETVAL into the function return type, and to assign it to
10406 the DECL_RESULT for the function. Set *NO_WARNING to true if
10407 code reaches end of non-void function warning shouldn't be issued
10408 on this RETURN_EXPR. */
10410 tree
10411 check_return_expr (tree retval, bool *no_warning)
10413 tree result;
10414 /* The type actually returned by the function. */
10415 tree valtype;
10416 /* The type the function is declared to return, or void if
10417 the declared type is incomplete. */
10418 tree functype;
10419 int fn_returns_value_p;
10420 location_t loc = cp_expr_loc_or_input_loc (retval);
10422 *no_warning = false;
10424 /* A `volatile' function is one that isn't supposed to return, ever.
10425 (This is a G++ extension, used to get better code for functions
10426 that call the `volatile' function.) */
10427 if (TREE_THIS_VOLATILE (current_function_decl))
10428 warning (0, "function declared %<noreturn%> has a %<return%> statement");
10430 /* Check for various simple errors. */
10431 if (DECL_DESTRUCTOR_P (current_function_decl))
10433 if (retval)
10434 error_at (loc, "returning a value from a destructor");
10435 return NULL_TREE;
10437 else if (DECL_CONSTRUCTOR_P (current_function_decl))
10439 if (in_function_try_handler)
10440 /* If a return statement appears in a handler of the
10441 function-try-block of a constructor, the program is ill-formed. */
10442 error ("cannot return from a handler of a function-try-block of a constructor");
10443 else if (retval)
10444 /* You can't return a value from a constructor. */
10445 error_at (loc, "returning a value from a constructor");
10446 return NULL_TREE;
10449 const tree saved_retval = retval;
10451 if (processing_template_decl)
10453 current_function_returns_value = 1;
10455 if (check_for_bare_parameter_packs (retval))
10456 return error_mark_node;
10458 /* If one of the types might be void, we can't tell whether we're
10459 returning a value. */
10460 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
10461 && !FNDECL_USED_AUTO (current_function_decl))
10462 || (retval != NULL_TREE
10463 && (TREE_TYPE (retval) == NULL_TREE
10464 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
10465 goto dependent;
10468 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
10470 /* Deduce auto return type from a return statement. */
10471 if (FNDECL_USED_AUTO (current_function_decl))
10473 tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
10474 tree auto_node;
10475 tree type;
10477 if (!retval && !is_auto (pattern))
10479 /* Give a helpful error message. */
10480 error ("return-statement with no value, in function returning %qT",
10481 pattern);
10482 inform (input_location, "only plain %<auto%> return type can be "
10483 "deduced to %<void%>");
10484 type = error_mark_node;
10486 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
10488 error ("returning initializer list");
10489 type = error_mark_node;
10491 else
10493 if (!retval)
10494 retval = void_node;
10495 auto_node = type_uses_auto (pattern);
10496 type = do_auto_deduction (pattern, retval, auto_node,
10497 tf_warning_or_error, adc_return_type);
10500 if (type == error_mark_node)
10501 /* Leave it. */;
10502 else if (functype == pattern)
10503 apply_deduced_return_type (current_function_decl, type);
10504 else if (!same_type_p (type, functype))
10506 if (LAMBDA_FUNCTION_P (current_function_decl))
10507 error_at (loc, "inconsistent types %qT and %qT deduced for "
10508 "lambda return type", functype, type);
10509 else
10510 error_at (loc, "inconsistent deduction for auto return type: "
10511 "%qT and then %qT", functype, type);
10513 functype = type;
10516 result = DECL_RESULT (current_function_decl);
10517 valtype = TREE_TYPE (result);
10518 gcc_assert (valtype != NULL_TREE);
10519 fn_returns_value_p = !VOID_TYPE_P (valtype);
10521 /* Check for a return statement with no return value in a function
10522 that's supposed to return a value. */
10523 if (!retval && fn_returns_value_p)
10525 if (functype != error_mark_node)
10526 permerror (input_location, "return-statement with no value, in "
10527 "function returning %qT", valtype);
10528 /* Remember that this function did return. */
10529 current_function_returns_value = 1;
10530 /* And signal caller that TREE_NO_WARNING should be set on the
10531 RETURN_EXPR to avoid control reaches end of non-void function
10532 warnings in tree-cfg.c. */
10533 *no_warning = true;
10535 /* Check for a return statement with a value in a function that
10536 isn't supposed to return a value. */
10537 else if (retval && !fn_returns_value_p)
10539 if (VOID_TYPE_P (TREE_TYPE (retval)))
10540 /* You can return a `void' value from a function of `void'
10541 type. In that case, we have to evaluate the expression for
10542 its side-effects. */
10543 finish_expr_stmt (retval);
10544 else if (retval != error_mark_node)
10545 permerror (loc, "return-statement with a value, in function "
10546 "returning %qT", valtype);
10547 current_function_returns_null = 1;
10549 /* There's really no value to return, after all. */
10550 return NULL_TREE;
10552 else if (!retval)
10553 /* Remember that this function can sometimes return without a
10554 value. */
10555 current_function_returns_null = 1;
10556 else
10557 /* Remember that this function did return a value. */
10558 current_function_returns_value = 1;
10560 /* Check for erroneous operands -- but after giving ourselves a
10561 chance to provide an error about returning a value from a void
10562 function. */
10563 if (error_operand_p (retval))
10565 current_function_return_value = error_mark_node;
10566 return error_mark_node;
10569 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
10570 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
10571 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
10572 && ! flag_check_new
10573 && retval && null_ptr_cst_p (retval))
10574 warning (0, "%<operator new%> must not return NULL unless it is "
10575 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
10577 /* Effective C++ rule 15. See also start_function. */
10578 if (warn_ecpp
10579 && DECL_NAME (current_function_decl) == assign_op_identifier
10580 && !type_dependent_expression_p (retval))
10582 bool warn = true;
10584 /* The function return type must be a reference to the current
10585 class. */
10586 if (TYPE_REF_P (valtype)
10587 && same_type_ignoring_top_level_qualifiers_p
10588 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
10590 /* Returning '*this' is obviously OK. */
10591 if (retval == current_class_ref)
10592 warn = false;
10593 /* If we are calling a function whose return type is the same of
10594 the current class reference, it is ok. */
10595 else if (INDIRECT_REF_P (retval)
10596 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
10597 warn = false;
10600 if (warn)
10601 warning_at (loc, OPT_Weffc__,
10602 "%<operator=%> should return a reference to %<*this%>");
10605 if (dependent_type_p (functype)
10606 || type_dependent_expression_p (retval))
10608 dependent:
10609 /* We should not have changed the return value. */
10610 gcc_assert (retval == saved_retval);
10611 /* We don't know if this is an lvalue or rvalue use, but
10612 either way we can mark it as read. */
10613 mark_exp_read (retval);
10614 return retval;
10617 /* The fabled Named Return Value optimization, as per [class.copy]/15:
10619 [...] For a function with a class return type, if the expression
10620 in the return statement is the name of a local object, and the cv-
10621 unqualified type of the local object is the same as the function
10622 return type, an implementation is permitted to omit creating the tem-
10623 porary object to hold the function return value [...]
10625 So, if this is a value-returning function that always returns the same
10626 local variable, remember it.
10628 It might be nice to be more flexible, and choose the first suitable
10629 variable even if the function sometimes returns something else, but
10630 then we run the risk of clobbering the variable we chose if the other
10631 returned expression uses the chosen variable somehow. And people expect
10632 this restriction, anyway. (jason 2000-11-19)
10634 See finish_function and finalize_nrv for the rest of this optimization. */
10635 tree bare_retval = NULL_TREE;
10636 if (retval)
10638 retval = maybe_undo_parenthesized_ref (retval);
10639 bare_retval = tree_strip_any_location_wrapper (retval);
10642 bool named_return_value_okay_p = can_do_nrvo_p (bare_retval, functype);
10643 if (fn_returns_value_p && flag_elide_constructors)
10645 if (named_return_value_okay_p
10646 && (current_function_return_value == NULL_TREE
10647 || current_function_return_value == bare_retval))
10648 current_function_return_value = bare_retval;
10649 else
10650 current_function_return_value = error_mark_node;
10653 /* We don't need to do any conversions when there's nothing being
10654 returned. */
10655 if (!retval)
10656 return NULL_TREE;
10658 if (!named_return_value_okay_p)
10659 maybe_warn_pessimizing_move (retval, functype);
10661 /* Do any required conversions. */
10662 if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
10663 /* No conversions are required. */
10665 else
10667 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
10669 /* The functype's return type will have been set to void, if it
10670 was an incomplete type. Just treat this as 'return;' */
10671 if (VOID_TYPE_P (functype))
10672 return error_mark_node;
10674 if (processing_template_decl)
10675 retval = build_non_dependent_expr (retval);
10677 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
10678 treated as an rvalue for the purposes of overload resolution to
10679 favor move constructors over copy constructors.
10681 Note that these conditions are similar to, but not as strict as,
10682 the conditions for the named return value optimization. */
10683 bool converted = false;
10684 tree moved;
10685 /* This is only interesting for class type. */
10686 if (CLASS_TYPE_P (functype)
10687 && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
10689 if (cxx_dialect < cxx20)
10691 moved = convert_for_initialization
10692 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
10693 ICR_RETURN, NULL_TREE, 0, tf_none);
10694 if (moved != error_mark_node)
10696 retval = moved;
10697 converted = true;
10700 else
10701 /* In C++20 we just treat the return value as an rvalue that
10702 can bind to lvalue refs. */
10703 retval = moved;
10706 /* The call in a (lambda) thunk needs no conversions. */
10707 if (TREE_CODE (retval) == CALL_EXPR
10708 && call_from_lambda_thunk_p (retval))
10709 converted = true;
10711 /* First convert the value to the function's return type, then
10712 to the type of return value's location to handle the
10713 case that functype is smaller than the valtype. */
10714 if (!converted)
10715 retval = convert_for_initialization
10716 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
10717 tf_warning_or_error);
10718 retval = convert (valtype, retval);
10720 /* If the conversion failed, treat this just like `return;'. */
10721 if (retval == error_mark_node)
10722 return retval;
10723 /* We can't initialize a register from a AGGR_INIT_EXPR. */
10724 else if (! cfun->returns_struct
10725 && TREE_CODE (retval) == TARGET_EXPR
10726 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
10727 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
10728 TREE_OPERAND (retval, 0));
10729 else if (!processing_template_decl
10730 && maybe_warn_about_returning_address_of_local (retval, loc)
10731 && INDIRECT_TYPE_P (valtype))
10732 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
10733 build_zero_cst (TREE_TYPE (retval)));
10736 if (processing_template_decl)
10737 return saved_retval;
10739 /* Actually copy the value returned into the appropriate location. */
10740 if (retval && retval != result)
10741 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
10743 if (tree set = maybe_set_retval_sentinel ())
10744 retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
10746 return retval;
10750 /* Returns nonzero if the pointer-type FROM can be converted to the
10751 pointer-type TO via a qualification conversion. If CONSTP is -1,
10752 then we return nonzero if the pointers are similar, and the
10753 cv-qualification signature of FROM is a proper subset of that of TO.
10755 If CONSTP is positive, then all outer pointers have been
10756 const-qualified. */
10758 static bool
10759 comp_ptr_ttypes_real (tree to, tree from, int constp)
10761 bool to_more_cv_qualified = false;
10762 bool is_opaque_pointer = false;
10764 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10766 if (TREE_CODE (to) != TREE_CODE (from))
10767 return false;
10769 if (TREE_CODE (from) == OFFSET_TYPE
10770 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
10771 TYPE_OFFSET_BASETYPE (to)))
10772 return false;
10774 /* Const and volatile mean something different for function and
10775 array types, so the usual checks are not appropriate. We'll
10776 check the array type elements in further iterations. */
10777 if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
10779 if (!at_least_as_qualified_p (to, from))
10780 return false;
10782 if (!at_least_as_qualified_p (from, to))
10784 if (constp == 0)
10785 return false;
10786 to_more_cv_qualified = true;
10789 if (constp > 0)
10790 constp &= TYPE_READONLY (to);
10793 if (VECTOR_TYPE_P (to))
10794 is_opaque_pointer = vector_targets_convertible_p (to, from);
10796 /* P0388R4 allows a conversion from int[N] to int[] but not the
10797 other way round. When both arrays have bounds but they do
10798 not match, then no conversion is possible. */
10799 if (TREE_CODE (to) == ARRAY_TYPE
10800 && !comp_array_types (to, from, bounds_first, /*strict=*/false))
10801 return false;
10803 if (!TYPE_PTR_P (to)
10804 && !TYPE_PTRDATAMEM_P (to)
10805 /* CWG 330 says we need to look through arrays. */
10806 && TREE_CODE (to) != ARRAY_TYPE)
10807 return ((constp >= 0 || to_more_cv_qualified)
10808 && (is_opaque_pointer
10809 || same_type_ignoring_top_level_qualifiers_p (to, from)));
10813 /* When comparing, say, char ** to char const **, this function takes
10814 the 'char *' and 'char const *'. Do not pass non-pointer/reference
10815 types to this function. */
10818 comp_ptr_ttypes (tree to, tree from)
10820 return comp_ptr_ttypes_real (to, from, 1);
10823 /* Returns true iff FNTYPE is a non-class type that involves
10824 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
10825 if a parameter type is ill-formed. */
10827 bool
10828 error_type_p (const_tree type)
10830 tree t;
10832 switch (TREE_CODE (type))
10834 case ERROR_MARK:
10835 return true;
10837 case POINTER_TYPE:
10838 case REFERENCE_TYPE:
10839 case OFFSET_TYPE:
10840 return error_type_p (TREE_TYPE (type));
10842 case FUNCTION_TYPE:
10843 case METHOD_TYPE:
10844 if (error_type_p (TREE_TYPE (type)))
10845 return true;
10846 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
10847 if (error_type_p (TREE_VALUE (t)))
10848 return true;
10849 return false;
10851 case RECORD_TYPE:
10852 if (TYPE_PTRMEMFUNC_P (type))
10853 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
10854 return false;
10856 default:
10857 return false;
10861 /* Returns true if to and from are (possibly multi-level) pointers to the same
10862 type or inheritance-related types, regardless of cv-quals. */
10864 bool
10865 ptr_reasonably_similar (const_tree to, const_tree from)
10867 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10869 /* Any target type is similar enough to void. */
10870 if (VOID_TYPE_P (to))
10871 return !error_type_p (from);
10872 if (VOID_TYPE_P (from))
10873 return !error_type_p (to);
10875 if (TREE_CODE (to) != TREE_CODE (from))
10876 return false;
10878 if (TREE_CODE (from) == OFFSET_TYPE
10879 && comptypes (TYPE_OFFSET_BASETYPE (to),
10880 TYPE_OFFSET_BASETYPE (from),
10881 COMPARE_BASE | COMPARE_DERIVED))
10882 continue;
10884 if (VECTOR_TYPE_P (to)
10885 && vector_types_convertible_p (to, from, false))
10886 return true;
10888 if (TREE_CODE (to) == INTEGER_TYPE
10889 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
10890 return true;
10892 if (TREE_CODE (to) == FUNCTION_TYPE)
10893 return !error_type_p (to) && !error_type_p (from);
10895 if (!TYPE_PTR_P (to))
10897 /* When either type is incomplete avoid DERIVED_FROM_P,
10898 which may call complete_type (c++/57942). */
10899 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
10900 return comptypes
10901 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
10902 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
10907 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
10908 pointer-to-member types) are the same, ignoring cv-qualification at
10909 all levels. CB says how we should behave when comparing array bounds. */
10911 bool
10912 comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
10914 bool is_opaque_pointer = false;
10916 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10918 if (TREE_CODE (to) != TREE_CODE (from))
10919 return false;
10921 if (TREE_CODE (from) == OFFSET_TYPE
10922 && same_type_p (TYPE_OFFSET_BASETYPE (from),
10923 TYPE_OFFSET_BASETYPE (to)))
10924 continue;
10926 if (VECTOR_TYPE_P (to))
10927 is_opaque_pointer = vector_targets_convertible_p (to, from);
10929 if (TREE_CODE (to) == ARRAY_TYPE
10930 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
10931 we must fail. */
10932 && !comp_array_types (to, from, cb, /*strict=*/false))
10933 return false;
10935 /* CWG 330 says we need to look through arrays. */
10936 if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
10937 return (is_opaque_pointer
10938 || same_type_ignoring_top_level_qualifiers_p (to, from));
10942 /* Returns the type qualifiers for this type, including the qualifiers on the
10943 elements for an array type. */
10946 cp_type_quals (const_tree type)
10948 int quals;
10949 /* This CONST_CAST is okay because strip_array_types returns its
10950 argument unmodified and we assign it to a const_tree. */
10951 type = strip_array_types (CONST_CAST_TREE (type));
10952 if (type == error_mark_node
10953 /* Quals on a FUNCTION_TYPE are memfn quals. */
10954 || TREE_CODE (type) == FUNCTION_TYPE)
10955 return TYPE_UNQUALIFIED;
10956 quals = TYPE_QUALS (type);
10957 /* METHOD and REFERENCE_TYPEs should never have quals. */
10958 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
10959 && !TYPE_REF_P (type))
10960 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
10961 == TYPE_UNQUALIFIED));
10962 return quals;
10965 /* Returns the function-ref-qualifier for TYPE */
10967 cp_ref_qualifier
10968 type_memfn_rqual (const_tree type)
10970 gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
10972 if (!FUNCTION_REF_QUALIFIED (type))
10973 return REF_QUAL_NONE;
10974 else if (FUNCTION_RVALUE_QUALIFIED (type))
10975 return REF_QUAL_RVALUE;
10976 else
10977 return REF_QUAL_LVALUE;
10980 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
10981 METHOD_TYPE. */
10984 type_memfn_quals (const_tree type)
10986 if (TREE_CODE (type) == FUNCTION_TYPE)
10987 return TYPE_QUALS (type);
10988 else if (TREE_CODE (type) == METHOD_TYPE)
10989 return cp_type_quals (class_of_this_parm (type));
10990 else
10991 gcc_unreachable ();
10994 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
10995 MEMFN_QUALS and its ref-qualifier to RQUAL. */
10997 tree
10998 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11000 /* Could handle METHOD_TYPE here if necessary. */
11001 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11002 if (TYPE_QUALS (type) == memfn_quals
11003 && type_memfn_rqual (type) == rqual)
11004 return type;
11006 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11007 complex. */
11008 tree result = build_qualified_type (type, memfn_quals);
11009 return build_ref_qualified_type (result, rqual);
11012 /* Returns nonzero if TYPE is const or volatile. */
11014 bool
11015 cv_qualified_p (const_tree type)
11017 int quals = cp_type_quals (type);
11018 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11021 /* Returns nonzero if the TYPE contains a mutable member. */
11023 bool
11024 cp_has_mutable_p (const_tree type)
11026 /* This CONST_CAST is okay because strip_array_types returns its
11027 argument unmodified and we assign it to a const_tree. */
11028 type = strip_array_types (CONST_CAST_TREE(type));
11030 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11033 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11034 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11035 approximation. In particular, consider:
11037 int f();
11038 struct S { int i; };
11039 const S s = { f(); }
11041 Here, we will make "s" as TREE_READONLY (because it is declared
11042 "const") -- only to reverse ourselves upon seeing that the
11043 initializer is non-constant. */
11045 void
11046 cp_apply_type_quals_to_decl (int type_quals, tree decl)
11048 tree type = TREE_TYPE (decl);
11050 if (type == error_mark_node)
11051 return;
11053 if (TREE_CODE (decl) == TYPE_DECL)
11054 return;
11056 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11057 && type_quals != TYPE_UNQUALIFIED));
11059 /* Avoid setting TREE_READONLY incorrectly. */
11060 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11061 constructor can produce constant init, so rely on cp_finish_decl to
11062 clear TREE_READONLY if the variable has non-constant init. */
11064 /* If the type has (or might have) a mutable component, that component
11065 might be modified. */
11066 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11067 type_quals &= ~TYPE_QUAL_CONST;
11069 c_apply_type_quals_to_decl (type_quals, decl);
11072 /* Subroutine of casts_away_constness. Make T1 and T2 point at
11073 exemplar types such that casting T1 to T2 is casting away constness
11074 if and only if there is no implicit conversion from T1 to T2. */
11076 static void
11077 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11079 int quals1;
11080 int quals2;
11082 /* [expr.const.cast]
11084 For multi-level pointer to members and multi-level mixed pointers
11085 and pointers to members (conv.qual), the "member" aspect of a
11086 pointer to member level is ignored when determining if a const
11087 cv-qualifier has been cast away. */
11088 /* [expr.const.cast]
11090 For two pointer types:
11092 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11093 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11094 K is min(N,M)
11096 casting from X1 to X2 casts away constness if, for a non-pointer
11097 type T there does not exist an implicit conversion (clause
11098 _conv_) from:
11100 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11104 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11105 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11106 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11108 *t1 = cp_build_qualified_type (void_type_node,
11109 cp_type_quals (*t1));
11110 *t2 = cp_build_qualified_type (void_type_node,
11111 cp_type_quals (*t2));
11112 return;
11115 quals1 = cp_type_quals (*t1);
11116 quals2 = cp_type_quals (*t2);
11118 if (TYPE_PTRDATAMEM_P (*t1))
11119 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11120 else
11121 *t1 = TREE_TYPE (*t1);
11122 if (TYPE_PTRDATAMEM_P (*t2))
11123 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11124 else
11125 *t2 = TREE_TYPE (*t2);
11127 casts_away_constness_r (t1, t2, complain);
11128 *t1 = build_pointer_type (*t1);
11129 *t2 = build_pointer_type (*t2);
11130 *t1 = cp_build_qualified_type (*t1, quals1);
11131 *t2 = cp_build_qualified_type (*t2, quals2);
11134 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11135 constness.
11137 ??? This function returns non-zero if casting away qualifiers not
11138 just const. We would like to return to the caller exactly which
11139 qualifiers are casted away to give more accurate diagnostics.
11142 static bool
11143 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
11145 if (TYPE_REF_P (t2))
11147 /* [expr.const.cast]
11149 Casting from an lvalue of type T1 to an lvalue of type T2
11150 using a reference cast casts away constness if a cast from an
11151 rvalue of type "pointer to T1" to the type "pointer to T2"
11152 casts away constness. */
11153 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
11154 return casts_away_constness (build_pointer_type (t1),
11155 build_pointer_type (TREE_TYPE (t2)),
11156 complain);
11159 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
11160 /* [expr.const.cast]
11162 Casting from an rvalue of type "pointer to data member of X
11163 of type T1" to the type "pointer to data member of Y of type
11164 T2" casts away constness if a cast from an rvalue of type
11165 "pointer to T1" to the type "pointer to T2" casts away
11166 constness. */
11167 return casts_away_constness
11168 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
11169 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
11170 complain);
11172 /* Casting away constness is only something that makes sense for
11173 pointer or reference types. */
11174 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
11175 return false;
11177 /* Top-level qualifiers don't matter. */
11178 t1 = TYPE_MAIN_VARIANT (t1);
11179 t2 = TYPE_MAIN_VARIANT (t2);
11180 casts_away_constness_r (&t1, &t2, complain);
11181 if (!can_convert (t2, t1, complain))
11182 return true;
11184 return false;
11187 /* If T is a REFERENCE_TYPE return the type to which T refers.
11188 Otherwise, return T itself. */
11190 tree
11191 non_reference (tree t)
11193 if (t && TYPE_REF_P (t))
11194 t = TREE_TYPE (t);
11195 return t;
11199 /* Return nonzero if REF is an lvalue valid for this language;
11200 otherwise, print an error message and return zero. USE says
11201 how the lvalue is being used and so selects the error message. */
11204 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
11206 cp_lvalue_kind kind = lvalue_kind (ref);
11208 if (kind == clk_none)
11210 if (complain & tf_error)
11211 lvalue_error (cp_expr_loc_or_input_loc (ref), use);
11212 return 0;
11214 else if (kind & (clk_rvalueref|clk_class))
11216 if (!(complain & tf_error))
11217 return 0;
11218 /* Make this a permerror because we used to accept it. */
11219 permerror (cp_expr_loc_or_input_loc (ref),
11220 "using rvalue as lvalue");
11222 return 1;
11225 /* Return true if a user-defined literal operator is a raw operator. */
11227 bool
11228 check_raw_literal_operator (const_tree decl)
11230 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11231 tree argtype;
11232 int arity;
11233 bool maybe_raw_p = false;
11235 /* Count the number and type of arguments and check for ellipsis. */
11236 for (argtype = argtypes, arity = 0;
11237 argtype && argtype != void_list_node;
11238 ++arity, argtype = TREE_CHAIN (argtype))
11240 tree t = TREE_VALUE (argtype);
11242 if (same_type_p (t, const_string_type_node))
11243 maybe_raw_p = true;
11245 if (!argtype)
11246 return false; /* Found ellipsis. */
11248 if (!maybe_raw_p || arity != 1)
11249 return false;
11251 return true;
11255 /* Return true if a user-defined literal operator has one of the allowed
11256 argument types. */
11258 bool
11259 check_literal_operator_args (const_tree decl,
11260 bool *long_long_unsigned_p, bool *long_double_p)
11262 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11264 *long_long_unsigned_p = false;
11265 *long_double_p = false;
11266 if (processing_template_decl || processing_specialization)
11267 return argtypes == void_list_node;
11268 else
11270 tree argtype;
11271 int arity;
11272 int max_arity = 2;
11274 /* Count the number and type of arguments and check for ellipsis. */
11275 for (argtype = argtypes, arity = 0;
11276 argtype && argtype != void_list_node;
11277 argtype = TREE_CHAIN (argtype))
11279 tree t = TREE_VALUE (argtype);
11280 ++arity;
11282 if (TYPE_PTR_P (t))
11284 bool maybe_raw_p = false;
11285 t = TREE_TYPE (t);
11286 if (cp_type_quals (t) != TYPE_QUAL_CONST)
11287 return false;
11288 t = TYPE_MAIN_VARIANT (t);
11289 if ((maybe_raw_p = same_type_p (t, char_type_node))
11290 || same_type_p (t, wchar_type_node)
11291 || same_type_p (t, char8_type_node)
11292 || same_type_p (t, char16_type_node)
11293 || same_type_p (t, char32_type_node))
11295 argtype = TREE_CHAIN (argtype);
11296 if (!argtype)
11297 return false;
11298 t = TREE_VALUE (argtype);
11299 if (maybe_raw_p && argtype == void_list_node)
11300 return true;
11301 else if (same_type_p (t, size_type_node))
11303 ++arity;
11304 continue;
11306 else
11307 return false;
11310 else if (same_type_p (t, long_long_unsigned_type_node))
11312 max_arity = 1;
11313 *long_long_unsigned_p = true;
11315 else if (same_type_p (t, long_double_type_node))
11317 max_arity = 1;
11318 *long_double_p = true;
11320 else if (same_type_p (t, char_type_node))
11321 max_arity = 1;
11322 else if (same_type_p (t, wchar_type_node))
11323 max_arity = 1;
11324 else if (same_type_p (t, char8_type_node))
11325 max_arity = 1;
11326 else if (same_type_p (t, char16_type_node))
11327 max_arity = 1;
11328 else if (same_type_p (t, char32_type_node))
11329 max_arity = 1;
11330 else
11331 return false;
11333 if (!argtype)
11334 return false; /* Found ellipsis. */
11336 if (arity != max_arity)
11337 return false;
11339 return true;
11343 /* Always returns false since unlike C90, C++ has no concept of implicit
11344 function declarations. */
11346 bool
11347 c_decl_implicit (const_tree)
11349 return false;