c++: tweak to (*(fn))() patch [PR104618]
[official-gcc.git] / gcc / cp / typeck.cc
blobea6a485bbbf65055872340857a3173239ce4a8f0
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2022 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.cc). 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.cc 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.cc 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. */
2729 if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2731 temp = cp_build_fold_indirect_ref (temp);
2732 if (!lvalue_p (object) && lvalue_p (temp))
2733 /* Preserve rvalueness. */
2734 temp = move (temp);
2735 object = temp;
2738 /* In [expr.ref], there is an explicit list of the valid choices for
2739 MEMBER. We check for each of those cases here. */
2740 if (VAR_P (member))
2742 /* A static data member. */
2743 result = member;
2744 mark_exp_read (object);
2746 if (tree wrap = maybe_get_tls_wrapper_call (result))
2747 /* Replace an evaluated use of the thread_local variable with
2748 a call to its wrapper. */
2749 result = wrap;
2751 /* If OBJECT has side-effects, they are supposed to occur. */
2752 if (TREE_SIDE_EFFECTS (object))
2753 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2755 else if (TREE_CODE (member) == FIELD_DECL)
2757 /* A non-static data member. */
2758 bool null_object_p;
2759 int type_quals;
2760 tree member_type;
2762 if (INDIRECT_REF_P (object))
2763 null_object_p =
2764 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2765 else
2766 null_object_p = false;
2768 /* Convert OBJECT to the type of MEMBER. */
2769 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2770 TYPE_MAIN_VARIANT (member_scope)))
2772 tree binfo;
2773 base_kind kind;
2775 /* We didn't complain above about a currently open class, but now we
2776 must: we don't know how to refer to a base member before layout is
2777 complete. But still don't complain in a template. */
2778 if (!cp_unevaluated_operand
2779 && !dependent_type_p (object_type)
2780 && !complete_type_or_maybe_complain (object_type, object,
2781 complain))
2782 return error_mark_node;
2784 binfo = lookup_base (access_path ? access_path : object_type,
2785 member_scope, ba_unique, &kind, complain);
2786 if (binfo == error_mark_node)
2787 return error_mark_node;
2789 /* It is invalid to try to get to a virtual base of a
2790 NULL object. The most common cause is invalid use of
2791 offsetof macro. */
2792 if (null_object_p && kind == bk_via_virtual)
2794 if (complain & tf_error)
2796 error ("invalid access to non-static data member %qD in "
2797 "virtual base of NULL object", member);
2799 return error_mark_node;
2802 /* Convert to the base. */
2803 object = build_base_path (PLUS_EXPR, object, binfo,
2804 /*nonnull=*/1, complain);
2805 /* If we found the base successfully then we should be able
2806 to convert to it successfully. */
2807 gcc_assert (object != error_mark_node);
2810 /* If MEMBER is from an anonymous aggregate, we have converted
2811 OBJECT so that it refers to the class containing the
2812 anonymous union. Generate a reference to the anonymous union
2813 itself, and recur to find MEMBER. */
2814 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2815 /* When this code is called from build_field_call, the
2816 object already has the type of the anonymous union.
2817 That is because the COMPONENT_REF was already
2818 constructed, and was then disassembled before calling
2819 build_field_call. After the function-call code is
2820 cleaned up, this waste can be eliminated. */
2821 && (!same_type_ignoring_top_level_qualifiers_p
2822 (TREE_TYPE (object), DECL_CONTEXT (member))))
2824 tree anonymous_union;
2826 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2827 DECL_CONTEXT (member));
2828 object = build_class_member_access_expr (object,
2829 anonymous_union,
2830 /*access_path=*/NULL_TREE,
2831 preserve_reference,
2832 complain);
2835 /* Compute the type of the field, as described in [expr.ref]. */
2836 type_quals = TYPE_UNQUALIFIED;
2837 member_type = TREE_TYPE (member);
2838 if (!TYPE_REF_P (member_type))
2840 type_quals = (cp_type_quals (member_type)
2841 | cp_type_quals (object_type));
2843 /* A field is const (volatile) if the enclosing object, or the
2844 field itself, is const (volatile). But, a mutable field is
2845 not const, even within a const object. */
2846 if (DECL_MUTABLE_P (member))
2847 type_quals &= ~TYPE_QUAL_CONST;
2848 member_type = cp_build_qualified_type (member_type, type_quals);
2851 result = build3_loc (input_location, COMPONENT_REF, member_type,
2852 object, member, NULL_TREE);
2854 /* Mark the expression const or volatile, as appropriate. Even
2855 though we've dealt with the type above, we still have to mark the
2856 expression itself. */
2857 if (type_quals & TYPE_QUAL_CONST)
2858 TREE_READONLY (result) = 1;
2859 if (type_quals & TYPE_QUAL_VOLATILE)
2860 TREE_THIS_VOLATILE (result) = 1;
2862 else if (BASELINK_P (member))
2864 /* The member is a (possibly overloaded) member function. */
2865 tree functions;
2866 tree type;
2868 /* If the MEMBER is exactly one static member function, then we
2869 know the type of the expression. Otherwise, we must wait
2870 until overload resolution has been performed. */
2871 functions = BASELINK_FUNCTIONS (member);
2872 if (TREE_CODE (functions) == FUNCTION_DECL
2873 && DECL_STATIC_FUNCTION_P (functions))
2874 type = TREE_TYPE (functions);
2875 else
2876 type = unknown_type_node;
2877 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2878 base. That will happen when the function is called. */
2879 result = build3_loc (input_location, COMPONENT_REF, type, object, member,
2880 NULL_TREE);
2882 else if (TREE_CODE (member) == CONST_DECL)
2884 /* The member is an enumerator. */
2885 result = member;
2886 /* If OBJECT has side-effects, they are supposed to occur. */
2887 if (TREE_SIDE_EFFECTS (object))
2888 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2889 object, result);
2891 else if ((using_decl = strip_using_decl (member)) != member)
2892 result = build_class_member_access_expr (object,
2893 using_decl,
2894 access_path, preserve_reference,
2895 complain);
2896 else
2898 if (complain & tf_error)
2899 error ("invalid use of %qD", member);
2900 return error_mark_node;
2903 if (!preserve_reference)
2904 /* [expr.ref]
2906 If E2 is declared to have type "reference to T", then ... the
2907 type of E1.E2 is T. */
2908 result = convert_from_reference (result);
2910 return result;
2913 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2914 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2916 tree
2917 lookup_destructor (tree object, tree scope, tree dtor_name,
2918 tsubst_flags_t complain)
2920 tree object_type = TREE_TYPE (object);
2921 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2922 tree expr;
2924 /* We've already complained about this destructor. */
2925 if (dtor_type == error_mark_node)
2926 return error_mark_node;
2928 if (scope && !check_dtor_name (scope, dtor_type))
2930 if (complain & tf_error)
2931 error ("qualified type %qT does not match destructor name ~%qT",
2932 scope, dtor_type);
2933 return error_mark_node;
2935 if (is_auto (dtor_type))
2936 dtor_type = object_type;
2937 else if (identifier_p (dtor_type))
2939 /* In a template, names we can't find a match for are still accepted
2940 destructor names, and we check them here. */
2941 if (check_dtor_name (object_type, dtor_type))
2942 dtor_type = object_type;
2943 else
2945 if (complain & tf_error)
2946 error ("object type %qT does not match destructor name ~%qT",
2947 object_type, dtor_type);
2948 return error_mark_node;
2952 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2954 if (complain & tf_error)
2955 error ("the type being destroyed is %qT, but the destructor "
2956 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2957 return error_mark_node;
2959 expr = lookup_member (dtor_type, complete_dtor_identifier,
2960 /*protect=*/1, /*want_type=*/false,
2961 tf_warning_or_error);
2962 if (!expr)
2964 if (complain & tf_error)
2965 cxx_incomplete_type_error (dtor_name, dtor_type);
2966 return error_mark_node;
2968 expr = (adjust_result_of_qualified_name_lookup
2969 (expr, dtor_type, object_type));
2970 if (scope == NULL_TREE)
2971 /* We need to call adjust_result_of_qualified_name_lookup in case the
2972 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2973 that we still get virtual function binding. */
2974 BASELINK_QUALIFIED_P (expr) = false;
2975 return expr;
2978 /* An expression of the form "A::template B" has been resolved to
2979 DECL. Issue a diagnostic if B is not a template or template
2980 specialization. */
2982 void
2983 check_template_keyword (tree decl)
2985 /* The standard says:
2987 [temp.names]
2989 If a name prefixed by the keyword template is not a member
2990 template, the program is ill-formed.
2992 DR 228 removed the restriction that the template be a member
2993 template.
2995 DR 96, if accepted would add the further restriction that explicit
2996 template arguments must be provided if the template keyword is
2997 used, but, as of 2005-10-16, that DR is still in "drafting". If
2998 this DR is accepted, then the semantic checks here can be
2999 simplified, as the entity named must in fact be a template
3000 specialization, rather than, as at present, a set of overloaded
3001 functions containing at least one template function. */
3002 if (TREE_CODE (decl) != TEMPLATE_DECL
3003 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3005 if (VAR_P (decl))
3007 if (DECL_USE_TEMPLATE (decl)
3008 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3010 else
3011 permerror (input_location, "%qD is not a template", decl);
3013 else if (!is_overloaded_fn (decl))
3014 permerror (input_location, "%qD is not a template", decl);
3015 else
3017 bool found = false;
3019 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3020 !found && iter; ++iter)
3022 tree fn = *iter;
3023 if (TREE_CODE (fn) == TEMPLATE_DECL
3024 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3025 || (TREE_CODE (fn) == FUNCTION_DECL
3026 && DECL_USE_TEMPLATE (fn)
3027 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3028 found = true;
3030 if (!found)
3031 permerror (input_location, "%qD is not a template", decl);
3036 /* Record that an access failure occurred on BASETYPE_PATH attempting
3037 to access DECL, where DIAG_DECL should be used for diagnostics. */
3039 void
3040 access_failure_info::record_access_failure (tree basetype_path,
3041 tree decl, tree diag_decl)
3043 m_was_inaccessible = true;
3044 m_basetype_path = basetype_path;
3045 m_decl = decl;
3046 m_diag_decl = diag_decl;
3049 /* If an access failure was recorded, then attempt to locate an
3050 accessor function for the pertinent field.
3051 Otherwise, return NULL_TREE. */
3053 tree
3054 access_failure_info::get_any_accessor (bool const_p) const
3056 if (!was_inaccessible_p ())
3057 return NULL_TREE;
3059 tree accessor
3060 = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3061 if (!accessor)
3062 return NULL_TREE;
3064 /* The accessor must itself be accessible for it to be a reasonable
3065 suggestion. */
3066 if (!accessible_p (m_basetype_path, accessor, true))
3067 return NULL_TREE;
3069 return accessor;
3072 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3073 replacing the primary location in RICHLOC with "accessor()". */
3075 void
3076 access_failure_info::add_fixit_hint (rich_location *richloc,
3077 tree accessor_decl)
3079 pretty_printer pp;
3080 pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3081 pp_string (&pp, "()");
3082 richloc->add_fixit_replace (pp_formatted_text (&pp));
3085 /* If an access failure was recorded, then attempt to locate an
3086 accessor function for the pertinent field, and if one is
3087 available, add a note and fix-it hint suggesting using it. */
3089 void
3090 access_failure_info::maybe_suggest_accessor (bool const_p) const
3092 tree accessor = get_any_accessor (const_p);
3093 if (accessor == NULL_TREE)
3094 return;
3095 rich_location richloc (line_table, input_location);
3096 add_fixit_hint (&richloc, accessor);
3097 inform (&richloc, "field %q#D can be accessed via %q#D",
3098 m_diag_decl, accessor);
3101 /* Subroutine of finish_class_member_access_expr.
3102 Issue an error about NAME not being a member of ACCESS_PATH (or
3103 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3104 names. */
3106 static void
3107 complain_about_unrecognized_member (tree access_path, tree name,
3108 tree object_type)
3110 /* Attempt to provide a hint about misspelled names. */
3111 tree guessed_id = lookup_member_fuzzy (access_path, name,
3112 /*want_type=*/false);
3113 if (guessed_id == NULL_TREE)
3115 /* No hint. */
3116 error ("%q#T has no member named %qE",
3117 TREE_CODE (access_path) == TREE_BINFO
3118 ? TREE_TYPE (access_path) : object_type, name);
3119 return;
3122 location_t bogus_component_loc = input_location;
3123 gcc_rich_location rich_loc (bogus_component_loc);
3125 /* Check that the guessed name is accessible along access_path. */
3126 access_failure_info afi;
3127 lookup_member (access_path, guessed_id, /*protect=*/1,
3128 /*want_type=*/false, /*complain=*/false,
3129 &afi);
3130 if (afi.was_inaccessible_p ())
3132 tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3133 if (accessor)
3135 /* The guessed name isn't directly accessible, but can be accessed
3136 via an accessor member function. */
3137 afi.add_fixit_hint (&rich_loc, accessor);
3138 error_at (&rich_loc,
3139 "%q#T has no member named %qE;"
3140 " did you mean %q#D? (accessible via %q#D)",
3141 TREE_CODE (access_path) == TREE_BINFO
3142 ? TREE_TYPE (access_path) : object_type,
3143 name, afi.get_diag_decl (), accessor);
3145 else
3147 /* The guessed name isn't directly accessible, and no accessor
3148 member function could be found. */
3149 error_at (&rich_loc,
3150 "%q#T has no member named %qE;"
3151 " did you mean %q#D? (not accessible from this context)",
3152 TREE_CODE (access_path) == TREE_BINFO
3153 ? TREE_TYPE (access_path) : object_type,
3154 name, afi.get_diag_decl ());
3155 complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3156 afi.get_diag_decl (), false, ak_none);
3159 else
3161 /* The guessed name is directly accessible; suggest it. */
3162 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3163 guessed_id);
3164 error_at (&rich_loc,
3165 "%q#T has no member named %qE;"
3166 " did you mean %qE?",
3167 TREE_CODE (access_path) == TREE_BINFO
3168 ? TREE_TYPE (access_path) : object_type,
3169 name, guessed_id);
3173 /* This function is called by the parser to process a class member
3174 access expression of the form OBJECT.NAME. NAME is a node used by
3175 the parser to represent a name; it is not yet a DECL. It may,
3176 however, be a BASELINK where the BASELINK_FUNCTIONS is a
3177 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3178 there is no reason to do the lookup twice, so the parser keeps the
3179 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3180 be a template via the use of the "A::template B" syntax. */
3182 tree
3183 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3184 tsubst_flags_t complain)
3186 tree expr;
3187 tree object_type;
3188 tree member;
3189 tree access_path = NULL_TREE;
3190 tree orig_object = object;
3191 tree orig_name = name;
3193 if (object == error_mark_node || name == error_mark_node)
3194 return error_mark_node;
3196 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3197 if (!objc_is_public (object, name))
3198 return error_mark_node;
3200 object_type = TREE_TYPE (object);
3202 if (processing_template_decl)
3204 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3205 type_dependent_object_expression_p (object)
3206 /* If NAME is "f<args>", where either 'f' or 'args' is
3207 dependent, then the expression is dependent. */
3208 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3209 && dependent_template_id_p (TREE_OPERAND (name, 0),
3210 TREE_OPERAND (name, 1)))
3211 /* If NAME is "T::X" where "T" is dependent, then the
3212 expression is dependent. */
3213 || (TREE_CODE (name) == SCOPE_REF
3214 && TYPE_P (TREE_OPERAND (name, 0))
3215 && dependent_scope_p (TREE_OPERAND (name, 0)))
3216 /* If NAME is operator T where "T" is dependent, we can't
3217 lookup until we instantiate the T. */
3218 || (TREE_CODE (name) == IDENTIFIER_NODE
3219 && IDENTIFIER_CONV_OP_P (name)
3220 && dependent_type_p (TREE_TYPE (name))))
3222 dependent:
3223 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3224 orig_object, orig_name, NULL_TREE);
3226 object = build_non_dependent_expr (object);
3228 else if (c_dialect_objc ()
3229 && identifier_p (name)
3230 && (expr = objc_maybe_build_component_ref (object, name)))
3231 return expr;
3233 /* [expr.ref]
3235 The type of the first expression shall be "class object" (of a
3236 complete type). */
3237 if (!currently_open_class (object_type)
3238 && !complete_type_or_maybe_complain (object_type, object, complain))
3239 return error_mark_node;
3240 if (!CLASS_TYPE_P (object_type))
3242 if (complain & tf_error)
3244 if (INDIRECT_TYPE_P (object_type)
3245 && CLASS_TYPE_P (TREE_TYPE (object_type)))
3246 error ("request for member %qD in %qE, which is of pointer "
3247 "type %qT (maybe you meant to use %<->%> ?)",
3248 name, object.get_value (), object_type);
3249 else
3250 error ("request for member %qD in %qE, which is of non-class "
3251 "type %qT", name, object.get_value (), object_type);
3253 return error_mark_node;
3256 if (BASELINK_P (name))
3257 /* A member function that has already been looked up. */
3258 member = name;
3259 else
3261 bool is_template_id = false;
3262 tree template_args = NULL_TREE;
3263 tree scope = NULL_TREE;
3265 access_path = object_type;
3267 if (TREE_CODE (name) == SCOPE_REF)
3269 /* A qualified name. The qualifying class or namespace `S'
3270 has already been looked up; it is either a TYPE or a
3271 NAMESPACE_DECL. */
3272 scope = TREE_OPERAND (name, 0);
3273 name = TREE_OPERAND (name, 1);
3275 /* If SCOPE is a namespace, then the qualified name does not
3276 name a member of OBJECT_TYPE. */
3277 if (TREE_CODE (scope) == NAMESPACE_DECL)
3279 if (complain & tf_error)
3280 error ("%<%D::%D%> is not a member of %qT",
3281 scope, name, object_type);
3282 return error_mark_node;
3286 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3288 is_template_id = true;
3289 template_args = TREE_OPERAND (name, 1);
3290 name = TREE_OPERAND (name, 0);
3292 if (!identifier_p (name))
3293 name = OVL_NAME (name);
3296 if (scope)
3298 if (TREE_CODE (scope) == ENUMERAL_TYPE)
3300 gcc_assert (!is_template_id);
3301 /* Looking up a member enumerator (c++/56793). */
3302 if (!TYPE_CLASS_SCOPE_P (scope)
3303 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3305 if (complain & tf_error)
3306 error ("%<%D::%D%> is not a member of %qT",
3307 scope, name, object_type);
3308 return error_mark_node;
3310 tree val = lookup_enumerator (scope, name);
3311 if (!val)
3313 if (complain & tf_error)
3314 error ("%qD is not a member of %qD",
3315 name, scope);
3316 return error_mark_node;
3319 if (TREE_SIDE_EFFECTS (object))
3320 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3321 return val;
3324 gcc_assert (CLASS_TYPE_P (scope));
3325 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3327 if (constructor_name_p (name, scope))
3329 if (complain & tf_error)
3330 error ("cannot call constructor %<%T::%D%> directly",
3331 scope, name);
3332 return error_mark_node;
3335 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3336 access_path = lookup_base (object_type, scope, ba_check,
3337 NULL, complain);
3338 if (access_path == error_mark_node)
3339 return error_mark_node;
3340 if (!access_path)
3342 if (any_dependent_bases_p (object_type))
3343 goto dependent;
3344 if (complain & tf_error)
3345 error ("%qT is not a base of %qT", scope, object_type);
3346 return error_mark_node;
3350 if (TREE_CODE (name) == BIT_NOT_EXPR)
3352 if (dependent_type_p (object_type))
3353 /* The destructor isn't declared yet. */
3354 goto dependent;
3355 member = lookup_destructor (object, scope, name, complain);
3357 else
3359 /* Look up the member. */
3360 access_failure_info afi;
3361 if (processing_template_decl)
3362 /* Even though this class member access expression is at this
3363 point not dependent, the member itself may be dependent, and
3364 we must not potentially push a access check for a dependent
3365 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3366 ahead of time here; we're going to redo this member lookup at
3367 instantiation time anyway. */
3368 push_deferring_access_checks (dk_no_check);
3369 member = lookup_member (access_path, name, /*protect=*/1,
3370 /*want_type=*/false, complain,
3371 &afi);
3372 if (processing_template_decl)
3373 pop_deferring_access_checks ();
3374 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3375 if (member == NULL_TREE)
3377 if (dependent_type_p (object_type))
3378 /* Try again at instantiation time. */
3379 goto dependent;
3380 if (complain & tf_error)
3381 complain_about_unrecognized_member (access_path, name,
3382 object_type);
3383 return error_mark_node;
3385 if (member == error_mark_node)
3386 return error_mark_node;
3387 if (DECL_P (member)
3388 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3389 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3390 wrong, so don't use it. */
3391 goto dependent;
3392 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3393 goto dependent;
3396 if (is_template_id)
3398 tree templ = member;
3400 if (BASELINK_P (templ))
3401 member = lookup_template_function (templ, template_args);
3402 else if (variable_template_p (templ))
3403 member = (lookup_and_finish_template_variable
3404 (templ, template_args, complain));
3405 else
3407 if (complain & tf_error)
3408 error ("%qD is not a member template function", name);
3409 return error_mark_node;
3414 if (TREE_UNAVAILABLE (member))
3415 error_unavailable_use (member, NULL_TREE);
3416 else if (TREE_DEPRECATED (member))
3417 warn_deprecated_use (member, NULL_TREE);
3419 if (template_p)
3420 check_template_keyword (member);
3422 expr = build_class_member_access_expr (object, member, access_path,
3423 /*preserve_reference=*/false,
3424 complain);
3425 if (processing_template_decl && expr != error_mark_node)
3427 if (BASELINK_P (member))
3429 if (TREE_CODE (orig_name) == SCOPE_REF)
3430 BASELINK_QUALIFIED_P (member) = 1;
3431 orig_name = member;
3433 return build_min_non_dep (COMPONENT_REF, expr,
3434 orig_object, orig_name,
3435 NULL_TREE);
3438 return expr;
3441 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3442 type. */
3444 tree
3445 build_simple_component_ref (tree object, tree member)
3447 tree type = cp_build_qualified_type (TREE_TYPE (member),
3448 cp_type_quals (TREE_TYPE (object)));
3449 return build3_loc (input_location,
3450 COMPONENT_REF, type,
3451 object, member, NULL_TREE);
3454 /* Return an expression for the MEMBER_NAME field in the internal
3455 representation of PTRMEM, a pointer-to-member function. (Each
3456 pointer-to-member function type gets its own RECORD_TYPE so it is
3457 more convenient to access the fields by name than by FIELD_DECL.)
3458 This routine converts the NAME to a FIELD_DECL and then creates the
3459 node for the complete expression. */
3461 tree
3462 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3464 tree ptrmem_type;
3465 tree member;
3467 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3469 for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3470 if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3471 return e.value;
3472 gcc_unreachable ();
3475 /* This code is a stripped down version of
3476 build_class_member_access_expr. It does not work to use that
3477 routine directly because it expects the object to be of class
3478 type. */
3479 ptrmem_type = TREE_TYPE (ptrmem);
3480 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3481 for (member = TYPE_FIELDS (ptrmem_type); member;
3482 member = DECL_CHAIN (member))
3483 if (DECL_NAME (member) == member_name)
3484 break;
3485 return build_simple_component_ref (ptrmem, member);
3488 /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3489 and for any other relevant operator. */
3491 static tree
3492 op_unqualified_lookup (tree_code code, bool is_assign)
3494 tree lookups = NULL_TREE;
3496 if (cxx_dialect >= cxx20 && !is_assign)
3498 if (code == NE_EXPR)
3500 /* != can get rewritten in terms of ==. */
3501 tree fnname = ovl_op_identifier (false, EQ_EXPR);
3502 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3503 lookups = tree_cons (fnname, fns, lookups);
3505 else if (code == GT_EXPR || code == LE_EXPR
3506 || code == LT_EXPR || code == GE_EXPR)
3508 /* These can get rewritten in terms of <=>. */
3509 tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3510 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3511 lookups = tree_cons (fnname, fns, lookups);
3515 tree fnname = ovl_op_identifier (is_assign, code);
3516 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3517 lookups = tree_cons (fnname, fns, lookups);
3519 if (lookups)
3520 return lookups;
3521 else
3522 return build_tree_list (NULL_TREE, NULL_TREE);
3525 /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3526 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3527 name lookup for the given operator. */
3529 tree
3530 build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3532 if (lookups)
3533 /* We're partially instantiating a dependent operator expression, and
3534 LOOKUPS is the result of phase 1 name lookup that we performed
3535 earlier at template definition time, so just reuse the corresponding
3536 DEPENDENT_OPERATOR_TYPE. */
3537 return TREE_TYPE (lookups);
3539 /* Otherwise we're processing a dependent operator expression at template
3540 definition time, so perform phase 1 name lookup now. */
3541 lookups = op_unqualified_lookup (code, is_assign);
3543 tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3544 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3545 TREE_TYPE (lookups) = type;
3546 return type;
3549 /* Given an expression PTR for a pointer, return an expression
3550 for the value pointed to.
3551 ERRORSTRING is the name of the operator to appear in error messages.
3553 This function may need to overload OPERATOR_FNNAME.
3554 Must also handle REFERENCE_TYPEs for C++. */
3556 tree
3557 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3558 tree lookups, tsubst_flags_t complain)
3560 tree orig_expr = expr;
3561 tree rval;
3562 tree overload = NULL_TREE;
3564 if (processing_template_decl)
3566 /* Retain the type if we know the operand is a pointer. */
3567 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3569 if (expr == current_class_ptr
3570 || (TREE_CODE (expr) == NOP_EXPR
3571 && TREE_OPERAND (expr, 0) == current_class_ptr
3572 && (same_type_ignoring_top_level_qualifiers_p
3573 (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3574 return current_class_ref;
3575 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3577 if (type_dependent_expression_p (expr))
3579 expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3580 TREE_TYPE (expr)
3581 = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3582 return expr;
3584 expr = build_non_dependent_expr (expr);
3587 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3588 NULL_TREE, NULL_TREE, lookups,
3589 &overload, complain);
3590 if (!rval)
3591 rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3593 if (processing_template_decl && rval != error_mark_node)
3595 if (overload != NULL_TREE)
3596 return (build_min_non_dep_op_overload
3597 (INDIRECT_REF, rval, overload, orig_expr));
3599 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3601 else
3602 return rval;
3605 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3606 types or expressions. */
3608 static bool
3609 cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3611 if (processing_template_decl)
3613 tree e = expr;
3614 STRIP_NOPS (e);
3615 if (dependent_type_p (type) || type_dependent_expression_p (e))
3616 return false;
3618 return strict_aliasing_warning (loc, type, expr);
3621 /* The implementation of the above, and of indirection implied by other
3622 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3624 static tree
3625 cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3626 tsubst_flags_t complain, bool do_fold)
3628 tree pointer, type;
3630 /* RO_NULL should only be used with the folding entry points below, not
3631 cp_build_indirect_ref. */
3632 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3634 if (ptr == current_class_ptr
3635 || (TREE_CODE (ptr) == NOP_EXPR
3636 && TREE_OPERAND (ptr, 0) == current_class_ptr
3637 && (same_type_ignoring_top_level_qualifiers_p
3638 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3639 return current_class_ref;
3641 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3642 ? ptr : decay_conversion (ptr, complain));
3643 if (pointer == error_mark_node)
3644 return error_mark_node;
3646 type = TREE_TYPE (pointer);
3648 if (INDIRECT_TYPE_P (type))
3650 /* [expr.unary.op]
3652 If the type of the expression is "pointer to T," the type
3653 of the result is "T." */
3654 tree t = TREE_TYPE (type);
3656 if ((CONVERT_EXPR_P (ptr)
3657 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3658 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3660 /* If a warning is issued, mark it to avoid duplicates from
3661 the backend. This only needs to be done at
3662 warn_strict_aliasing > 2. */
3663 if (warn_strict_aliasing > 2
3664 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3665 type, TREE_OPERAND (ptr, 0)))
3666 suppress_warning (ptr, OPT_Wstrict_aliasing);
3669 if (VOID_TYPE_P (t))
3671 /* A pointer to incomplete type (other than cv void) can be
3672 dereferenced [expr.unary.op]/1 */
3673 if (complain & tf_error)
3674 error_at (loc, "%qT is not a pointer-to-object type", type);
3675 return error_mark_node;
3677 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3678 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3679 /* The POINTER was something like `&x'. We simplify `*&x' to
3680 `x'. */
3681 return TREE_OPERAND (pointer, 0);
3682 else
3684 tree ref = build1 (INDIRECT_REF, t, pointer);
3686 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3687 so that we get the proper error message if the result is used
3688 to assign to. Also, &* is supposed to be a no-op. */
3689 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3690 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3691 TREE_SIDE_EFFECTS (ref)
3692 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3693 return ref;
3696 else if (!(complain & tf_error))
3697 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3699 /* `pointer' won't be an error_mark_node if we were given a
3700 pointer to member, so it's cool to check for this here. */
3701 else if (TYPE_PTRMEM_P (type))
3702 switch (errorstring)
3704 case RO_ARRAY_INDEXING:
3705 error_at (loc,
3706 "invalid use of array indexing on pointer to member");
3707 break;
3708 case RO_UNARY_STAR:
3709 error_at (loc, "invalid use of unary %<*%> on pointer to member");
3710 break;
3711 case RO_IMPLICIT_CONVERSION:
3712 error_at (loc, "invalid use of implicit conversion on pointer "
3713 "to member");
3714 break;
3715 case RO_ARROW_STAR:
3716 error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3717 "class, but is a pointer to member of type %qT", type);
3718 break;
3719 default:
3720 gcc_unreachable ();
3722 else if (pointer != error_mark_node)
3723 invalid_indirection_error (loc, type, errorstring);
3725 return error_mark_node;
3728 /* Entry point used by c-common, which expects folding. */
3730 tree
3731 build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3733 return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3734 tf_warning_or_error, true);
3737 /* Entry point used by internal indirection needs that don't correspond to any
3738 syntactic construct. */
3740 tree
3741 cp_build_fold_indirect_ref (tree pointer)
3743 return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3744 tf_warning_or_error, true);
3747 /* Entry point used by indirection needs that correspond to some syntactic
3748 construct. */
3750 tree
3751 cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3752 tsubst_flags_t complain)
3754 return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3757 /* This handles expressions of the form "a[i]", which denotes
3758 an array reference.
3760 This is logically equivalent in C to *(a+i), but we may do it differently.
3761 If A is a variable or a member, we generate a primitive ARRAY_REF.
3762 This avoids forcing the array out of registers, and can work on
3763 arrays that are not lvalues (for example, members of structures returned
3764 by functions).
3766 If INDEX is of some user-defined type, it must be converted to
3767 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3768 will inherit the type of the array, which will be some pointer type.
3770 LOC is the location to use in building the array reference. */
3772 tree
3773 cp_build_array_ref (location_t loc, tree array, tree idx,
3774 tsubst_flags_t complain)
3776 tree ret;
3778 if (idx == 0)
3780 if (complain & tf_error)
3781 error_at (loc, "subscript missing in array reference");
3782 return error_mark_node;
3785 if (TREE_TYPE (array) == error_mark_node
3786 || TREE_TYPE (idx) == error_mark_node)
3787 return error_mark_node;
3789 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3790 inside it. */
3791 switch (TREE_CODE (array))
3793 case COMPOUND_EXPR:
3795 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3796 complain);
3797 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3798 TREE_OPERAND (array, 0), value);
3799 SET_EXPR_LOCATION (ret, loc);
3800 return ret;
3803 case COND_EXPR:
3804 ret = build_conditional_expr
3805 (loc, TREE_OPERAND (array, 0),
3806 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3807 complain),
3808 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3809 complain),
3810 complain);
3811 protected_set_expr_location (ret, loc);
3812 return ret;
3814 default:
3815 break;
3818 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3820 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3822 tree rval, type;
3824 warn_array_subscript_with_type_char (loc, idx);
3826 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3828 if (complain & tf_error)
3829 error_at (loc, "array subscript is not an integer");
3830 return error_mark_node;
3833 /* Apply integral promotions *after* noticing character types.
3834 (It is unclear why we do these promotions -- the standard
3835 does not say that we should. In fact, the natural thing would
3836 seem to be to convert IDX to ptrdiff_t; we're performing
3837 pointer arithmetic.) */
3838 idx = cp_perform_integral_promotions (idx, complain);
3840 idx = maybe_fold_non_dependent_expr (idx, complain);
3842 /* An array that is indexed by a non-constant
3843 cannot be stored in a register; we must be able to do
3844 address arithmetic on its address.
3845 Likewise an array of elements of variable size. */
3846 if (TREE_CODE (idx) != INTEGER_CST
3847 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3848 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3849 != INTEGER_CST)))
3851 if (!cxx_mark_addressable (array, true))
3852 return error_mark_node;
3855 /* An array that is indexed by a constant value which is not within
3856 the array bounds cannot be stored in a register either; because we
3857 would get a crash in store_bit_field/extract_bit_field when trying
3858 to access a non-existent part of the register. */
3859 if (TREE_CODE (idx) == INTEGER_CST
3860 && TYPE_DOMAIN (TREE_TYPE (array))
3861 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3863 if (!cxx_mark_addressable (array))
3864 return error_mark_node;
3867 /* Note in C++ it is valid to subscript a `register' array, since
3868 it is valid to take the address of something with that
3869 storage specification. */
3870 if (extra_warnings)
3872 tree foo = array;
3873 while (TREE_CODE (foo) == COMPONENT_REF)
3874 foo = TREE_OPERAND (foo, 0);
3875 if (VAR_P (foo) && DECL_REGISTER (foo)
3876 && (complain & tf_warning))
3877 warning_at (loc, OPT_Wextra,
3878 "subscripting array declared %<register%>");
3881 type = TREE_TYPE (TREE_TYPE (array));
3882 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3883 /* Array ref is const/volatile if the array elements are
3884 or if the array is.. */
3885 TREE_READONLY (rval)
3886 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3887 TREE_SIDE_EFFECTS (rval)
3888 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3889 TREE_THIS_VOLATILE (rval)
3890 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3891 ret = require_complete_type_sfinae (rval, complain);
3892 protected_set_expr_location (ret, loc);
3893 if (non_lvalue)
3894 ret = non_lvalue_loc (loc, ret);
3895 return ret;
3899 tree ar = cp_default_conversion (array, complain);
3900 tree ind = cp_default_conversion (idx, complain);
3901 tree first = NULL_TREE;
3903 if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
3904 ar = first = save_expr (ar);
3906 /* Put the integer in IND to simplify error checking. */
3907 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3908 std::swap (ar, ind);
3910 if (ar == error_mark_node || ind == error_mark_node)
3911 return error_mark_node;
3913 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3915 if (complain & tf_error)
3916 error_at (loc, "subscripted value is neither array nor pointer");
3917 return error_mark_node;
3919 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3921 if (complain & tf_error)
3922 error_at (loc, "array subscript is not an integer");
3923 return error_mark_node;
3926 warn_array_subscript_with_type_char (loc, idx);
3928 ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
3929 if (first)
3930 ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
3931 ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
3932 protected_set_expr_location (ret, loc);
3933 if (non_lvalue)
3934 ret = non_lvalue_loc (loc, ret);
3935 return ret;
3939 /* Entry point for Obj-C++. */
3941 tree
3942 build_array_ref (location_t loc, tree array, tree idx)
3944 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3947 /* Resolve a pointer to member function. INSTANCE is the object
3948 instance to use, if the member points to a virtual member.
3950 This used to avoid checking for virtual functions if basetype
3951 has no virtual functions, according to an earlier ANSI draft.
3952 With the final ISO C++ rules, such an optimization is
3953 incorrect: A pointer to a derived member can be static_cast
3954 to pointer-to-base-member, as long as the dynamic object
3955 later has the right member. So now we only do this optimization
3956 when we know the dynamic type of the object. */
3958 tree
3959 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3960 tsubst_flags_t complain)
3962 if (TREE_CODE (function) == OFFSET_REF)
3963 function = TREE_OPERAND (function, 1);
3965 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3967 tree idx, delta, e1, e2, e3, vtbl;
3968 bool nonvirtual;
3969 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3970 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3972 tree instance_ptr = *instance_ptrptr;
3973 tree instance_save_expr = 0;
3974 if (instance_ptr == error_mark_node)
3976 if (TREE_CODE (function) == PTRMEM_CST)
3978 /* Extracting the function address from a pmf is only
3979 allowed with -Wno-pmf-conversions. It only works for
3980 pmf constants. */
3981 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3982 e1 = convert (fntype, e1);
3983 return e1;
3985 else
3987 if (complain & tf_error)
3988 error ("object missing in use of %qE", function);
3989 return error_mark_node;
3993 /* True if we know that the dynamic type of the object doesn't have
3994 virtual functions, so we can assume the PFN field is a pointer. */
3995 nonvirtual = (COMPLETE_TYPE_P (basetype)
3996 && !TYPE_POLYMORPHIC_P (basetype)
3997 && resolves_to_fixed_type_p (instance_ptr, 0));
3999 /* If we don't really have an object (i.e. in an ill-formed
4000 conversion from PMF to pointer), we can't resolve virtual
4001 functions anyway. */
4002 if (!nonvirtual && is_dummy_object (instance_ptr))
4003 nonvirtual = true;
4005 if (TREE_SIDE_EFFECTS (instance_ptr))
4006 instance_ptr = instance_save_expr = save_expr (instance_ptr);
4008 if (TREE_SIDE_EFFECTS (function))
4009 function = save_expr (function);
4011 /* Start by extracting all the information from the PMF itself. */
4012 e3 = pfn_from_ptrmemfunc (function);
4013 delta = delta_from_ptrmemfunc (function);
4014 idx = build1 (NOP_EXPR, vtable_index_type, e3);
4015 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4017 int flag_sanitize_save;
4018 case ptrmemfunc_vbit_in_pfn:
4019 e1 = cp_build_binary_op (input_location,
4020 BIT_AND_EXPR, idx, integer_one_node,
4021 complain);
4022 idx = cp_build_binary_op (input_location,
4023 MINUS_EXPR, idx, integer_one_node,
4024 complain);
4025 if (idx == error_mark_node)
4026 return error_mark_node;
4027 break;
4029 case ptrmemfunc_vbit_in_delta:
4030 e1 = cp_build_binary_op (input_location,
4031 BIT_AND_EXPR, delta, integer_one_node,
4032 complain);
4033 /* Don't instrument the RSHIFT_EXPR we're about to create because
4034 we're going to use DELTA number of times, and that wouldn't play
4035 well with SAVE_EXPRs therein. */
4036 flag_sanitize_save = flag_sanitize;
4037 flag_sanitize = 0;
4038 delta = cp_build_binary_op (input_location,
4039 RSHIFT_EXPR, delta, integer_one_node,
4040 complain);
4041 flag_sanitize = flag_sanitize_save;
4042 if (delta == error_mark_node)
4043 return error_mark_node;
4044 break;
4046 default:
4047 gcc_unreachable ();
4050 if (e1 == error_mark_node)
4051 return error_mark_node;
4053 /* Convert down to the right base before using the instance. A
4054 special case is that in a pointer to member of class C, C may
4055 be incomplete. In that case, the function will of course be
4056 a member of C, and no conversion is required. In fact,
4057 lookup_base will fail in that case, because incomplete
4058 classes do not have BINFOs. */
4059 if (!same_type_ignoring_top_level_qualifiers_p
4060 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4062 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4063 basetype, ba_check, NULL, complain);
4064 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4065 1, complain);
4066 if (instance_ptr == error_mark_node)
4067 return error_mark_node;
4069 /* ...and then the delta in the PMF. */
4070 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4072 /* Hand back the adjusted 'this' argument to our caller. */
4073 *instance_ptrptr = instance_ptr;
4075 if (nonvirtual)
4076 /* Now just return the pointer. */
4077 return e3;
4079 /* Next extract the vtable pointer from the object. */
4080 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4081 instance_ptr);
4082 vtbl = cp_build_fold_indirect_ref (vtbl);
4083 if (vtbl == error_mark_node)
4084 return error_mark_node;
4086 /* Finally, extract the function pointer from the vtable. */
4087 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4088 e2 = cp_build_fold_indirect_ref (e2);
4089 if (e2 == error_mark_node)
4090 return error_mark_node;
4091 TREE_CONSTANT (e2) = 1;
4093 /* When using function descriptors, the address of the
4094 vtable entry is treated as a function pointer. */
4095 if (TARGET_VTABLE_USES_DESCRIPTORS)
4096 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4097 cp_build_addr_expr (e2, complain));
4099 e2 = fold_convert (TREE_TYPE (e3), e2);
4100 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4101 if (e1 == error_mark_node)
4102 return error_mark_node;
4104 /* Make sure this doesn't get evaluated first inside one of the
4105 branches of the COND_EXPR. */
4106 if (instance_save_expr)
4107 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4108 instance_save_expr, e1);
4110 function = e1;
4112 return function;
4115 /* Used by the C-common bits. */
4116 tree
4117 build_function_call (location_t /*loc*/,
4118 tree function, tree params)
4120 return cp_build_function_call (function, params, tf_warning_or_error);
4123 /* Used by the C-common bits. */
4124 tree
4125 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4126 tree function, vec<tree, va_gc> *params,
4127 vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4129 vec<tree, va_gc> *orig_params = params;
4130 tree ret = cp_build_function_call_vec (function, &params,
4131 tf_warning_or_error, orig_function);
4133 /* cp_build_function_call_vec can reallocate PARAMS by adding
4134 default arguments. That should never happen here. Verify
4135 that. */
4136 gcc_assert (params == orig_params);
4138 return ret;
4141 /* Build a function call using a tree list of arguments. */
4143 static tree
4144 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4146 tree ret;
4148 releasing_vec vec;
4149 for (; params != NULL_TREE; params = TREE_CHAIN (params))
4150 vec_safe_push (vec, TREE_VALUE (params));
4151 ret = cp_build_function_call_vec (function, &vec, complain);
4152 return ret;
4155 /* Build a function call using varargs. */
4157 tree
4158 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4160 va_list args;
4161 tree ret, t;
4163 releasing_vec vec;
4164 va_start (args, complain);
4165 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4166 vec_safe_push (vec, t);
4167 va_end (args);
4168 ret = cp_build_function_call_vec (function, &vec, complain);
4169 return ret;
4172 /* Build a function call using a vector of arguments.
4173 If FUNCTION is the result of resolving an overloaded target built-in,
4174 ORIG_FNDECL is the original function decl, otherwise it is null.
4175 PARAMS may be NULL if there are no parameters. This changes the
4176 contents of PARAMS. */
4178 tree
4179 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4180 tsubst_flags_t complain, tree orig_fndecl)
4182 tree fntype, fndecl;
4183 int is_method;
4184 tree original = function;
4185 int nargs;
4186 tree *argarray;
4187 tree parm_types;
4188 vec<tree, va_gc> *allocated = NULL;
4189 tree ret;
4191 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4192 expressions, like those used for ObjC messenger dispatches. */
4193 if (params != NULL && !vec_safe_is_empty (*params))
4194 function = objc_rewrite_function_call (function, (**params)[0]);
4196 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4197 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4198 if (TREE_CODE (function) == NOP_EXPR
4199 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4200 function = TREE_OPERAND (function, 0);
4202 if (TREE_CODE (function) == FUNCTION_DECL)
4204 if (!mark_used (function, complain))
4205 return error_mark_node;
4206 fndecl = function;
4208 /* Convert anything with function type to a pointer-to-function. */
4209 if (DECL_MAIN_P (function))
4211 if (complain & tf_error)
4212 pedwarn (input_location, OPT_Wpedantic,
4213 "ISO C++ forbids calling %<::main%> from within program");
4214 else
4215 return error_mark_node;
4217 function = build_addr_func (function, complain);
4219 else
4221 fndecl = NULL_TREE;
4223 function = build_addr_func (function, complain);
4226 if (function == error_mark_node)
4227 return error_mark_node;
4229 fntype = TREE_TYPE (function);
4231 if (TYPE_PTRMEMFUNC_P (fntype))
4233 if (complain & tf_error)
4234 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4235 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4236 original, original);
4237 return error_mark_node;
4240 is_method = (TYPE_PTR_P (fntype)
4241 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4243 if (!(TYPE_PTRFN_P (fntype)
4244 || is_method
4245 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4247 if (complain & tf_error)
4249 if (!flag_diagnostics_show_caret)
4250 error_at (input_location,
4251 "%qE cannot be used as a function", original);
4252 else if (DECL_P (original))
4253 error_at (input_location,
4254 "%qD cannot be used as a function", original);
4255 else
4256 error_at (input_location,
4257 "expression cannot be used as a function");
4260 return error_mark_node;
4263 /* fntype now gets the type of function pointed to. */
4264 fntype = TREE_TYPE (fntype);
4265 parm_types = TYPE_ARG_TYPES (fntype);
4267 if (params == NULL)
4269 allocated = make_tree_vector ();
4270 params = &allocated;
4273 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4274 complain);
4275 if (nargs < 0)
4276 return error_mark_node;
4278 argarray = (*params)->address ();
4280 /* Check for errors in format strings and inappropriately
4281 null parameters. */
4282 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
4283 nargs, argarray, NULL);
4285 ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4287 if (warned_p)
4289 tree c = extract_call_expr (ret);
4290 if (TREE_CODE (c) == CALL_EXPR)
4291 suppress_warning (c, OPT_Wnonnull);
4294 if (allocated != NULL)
4295 release_tree_vector (allocated);
4297 return ret;
4300 /* Subroutine of convert_arguments.
4301 Print an error message about a wrong number of arguments. */
4303 static void
4304 error_args_num (location_t loc, tree fndecl, bool too_many_p)
4306 if (fndecl)
4308 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4310 if (DECL_NAME (fndecl) == NULL_TREE
4311 || (DECL_NAME (fndecl)
4312 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4313 error_at (loc,
4314 too_many_p
4315 ? G_("too many arguments to constructor %q#D")
4316 : G_("too few arguments to constructor %q#D"),
4317 fndecl);
4318 else
4319 error_at (loc,
4320 too_many_p
4321 ? G_("too many arguments to member function %q#D")
4322 : G_("too few arguments to member function %q#D"),
4323 fndecl);
4325 else
4326 error_at (loc,
4327 too_many_p
4328 ? G_("too many arguments to function %q#D")
4329 : G_("too few arguments to function %q#D"),
4330 fndecl);
4331 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4332 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4334 else
4336 if (c_dialect_objc () && objc_message_selector ())
4337 error_at (loc,
4338 too_many_p
4339 ? G_("too many arguments to method %q#D")
4340 : G_("too few arguments to method %q#D"),
4341 objc_message_selector ());
4342 else
4343 error_at (loc, too_many_p ? G_("too many arguments to function")
4344 : G_("too few arguments to function"));
4348 /* Convert the actual parameter expressions in the list VALUES to the
4349 types in the list TYPELIST. The converted expressions are stored
4350 back in the VALUES vector.
4351 If parmdecls is exhausted, or when an element has NULL as its type,
4352 perform the default conversions.
4354 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4356 This is also where warnings about wrong number of args are generated.
4358 Returns the actual number of arguments processed (which might be less
4359 than the length of the vector), or -1 on error.
4361 In C++, unspecified trailing parameters can be filled in with their
4362 default arguments, if such were specified. Do so here. */
4364 static int
4365 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4366 int flags, tsubst_flags_t complain)
4368 tree typetail;
4369 unsigned int i;
4371 /* Argument passing is always copy-initialization. */
4372 flags |= LOOKUP_ONLYCONVERTING;
4374 for (i = 0, typetail = typelist;
4375 i < vec_safe_length (*values);
4376 i++)
4378 tree type = typetail ? TREE_VALUE (typetail) : 0;
4379 tree val = (**values)[i];
4381 if (val == error_mark_node || type == error_mark_node)
4382 return -1;
4384 if (type == void_type_node)
4386 if (complain & tf_error)
4388 error_args_num (input_location, fndecl, /*too_many_p=*/true);
4389 return i;
4391 else
4392 return -1;
4395 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4396 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4397 if (TREE_CODE (val) == NOP_EXPR
4398 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4399 && (type == 0 || !TYPE_REF_P (type)))
4400 val = TREE_OPERAND (val, 0);
4402 if (type == 0 || !TYPE_REF_P (type))
4404 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4405 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4406 val = decay_conversion (val, complain);
4409 if (val == error_mark_node)
4410 return -1;
4412 if (type != 0)
4414 /* Formal parm type is specified by a function prototype. */
4415 tree parmval;
4417 if (!COMPLETE_TYPE_P (complete_type (type)))
4419 if (complain & tf_error)
4421 location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4422 if (fndecl)
4424 auto_diagnostic_group d;
4425 error_at (loc,
4426 "parameter %P of %qD has incomplete type %qT",
4427 i, fndecl, type);
4428 inform (get_fndecl_argument_location (fndecl, i),
4429 " declared here");
4431 else
4432 error_at (loc, "parameter %P has incomplete type %qT", i,
4433 type);
4435 parmval = error_mark_node;
4437 else
4439 parmval = convert_for_initialization
4440 (NULL_TREE, type, val, flags,
4441 ICR_ARGPASS, fndecl, i, complain);
4442 parmval = convert_for_arg_passing (type, parmval, complain);
4445 if (parmval == error_mark_node)
4446 return -1;
4448 (**values)[i] = parmval;
4450 else
4452 if (fndecl && magic_varargs_p (fndecl))
4453 /* Don't do ellipsis conversion for __built_in_constant_p
4454 as this will result in spurious errors for non-trivial
4455 types. */
4456 val = require_complete_type_sfinae (val, complain);
4457 else
4458 val = convert_arg_to_ellipsis (val, complain);
4460 (**values)[i] = val;
4463 if (typetail)
4464 typetail = TREE_CHAIN (typetail);
4467 if (typetail != 0 && typetail != void_list_node)
4469 /* See if there are default arguments that can be used. Because
4470 we hold default arguments in the FUNCTION_TYPE (which is so
4471 wrong), we can see default parameters here from deduced
4472 contexts (and via typeof) for indirect function calls.
4473 Fortunately we know whether we have a function decl to
4474 provide default arguments in a language conformant
4475 manner. */
4476 if (fndecl && TREE_PURPOSE (typetail)
4477 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4479 for (; typetail != void_list_node; ++i)
4481 /* After DR777, with explicit template args we can end up with a
4482 default argument followed by no default argument. */
4483 if (!TREE_PURPOSE (typetail))
4484 break;
4485 tree parmval
4486 = convert_default_arg (TREE_VALUE (typetail),
4487 TREE_PURPOSE (typetail),
4488 fndecl, i, complain);
4490 if (parmval == error_mark_node)
4491 return -1;
4493 vec_safe_push (*values, parmval);
4494 typetail = TREE_CHAIN (typetail);
4495 /* ends with `...'. */
4496 if (typetail == NULL_TREE)
4497 break;
4501 if (typetail && typetail != void_list_node)
4503 if (complain & tf_error)
4504 error_args_num (input_location, fndecl, /*too_many_p=*/false);
4505 return -1;
4509 return (int) i;
4512 /* Build a binary-operation expression, after performing default
4513 conversions on the operands. CODE is the kind of expression to
4514 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4515 are the tree codes which correspond to ARG1 and ARG2 when issuing
4516 warnings about possibly misplaced parentheses. They may differ
4517 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4518 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4519 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4520 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4521 ARG2_CODE as ERROR_MARK. */
4523 tree
4524 build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4525 enum tree_code arg1_code, tree arg2,
4526 enum tree_code arg2_code, tree lookups,
4527 tree *overload_p, tsubst_flags_t complain)
4529 tree orig_arg1;
4530 tree orig_arg2;
4531 tree expr;
4532 tree overload = NULL_TREE;
4534 orig_arg1 = arg1;
4535 orig_arg2 = arg2;
4537 if (processing_template_decl)
4539 if (type_dependent_expression_p (arg1)
4540 || type_dependent_expression_p (arg2))
4542 expr = build_min_nt_loc (loc, code, arg1, arg2);
4543 TREE_TYPE (expr)
4544 = build_dependent_operator_type (lookups, code, false);
4545 return expr;
4547 arg1 = build_non_dependent_expr (arg1);
4548 arg2 = build_non_dependent_expr (arg2);
4551 if (code == DOTSTAR_EXPR)
4552 expr = build_m_component_ref (arg1, arg2, complain);
4553 else
4554 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4555 lookups, &overload, complain);
4557 if (overload_p != NULL)
4558 *overload_p = overload;
4560 /* Check for cases such as x+y<<z which users are likely to
4561 misinterpret. But don't warn about obj << x + y, since that is a
4562 common idiom for I/O. */
4563 if (warn_parentheses
4564 && (complain & tf_warning)
4565 && !processing_template_decl
4566 && !error_operand_p (arg1)
4567 && !error_operand_p (arg2)
4568 && (code != LSHIFT_EXPR
4569 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4570 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4571 arg2_code, orig_arg2);
4573 if (processing_template_decl && expr != error_mark_node)
4575 if (overload != NULL_TREE)
4576 return (build_min_non_dep_op_overload
4577 (code, expr, overload, orig_arg1, orig_arg2));
4579 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4582 return expr;
4585 /* Build and return an ARRAY_REF expression. */
4587 tree
4588 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4589 tsubst_flags_t complain)
4591 tree orig_arg1 = arg1;
4592 tree orig_arg2 = arg2;
4593 tree expr;
4594 tree overload = NULL_TREE;
4596 if (processing_template_decl)
4598 if (type_dependent_expression_p (arg1)
4599 || type_dependent_expression_p (arg2))
4600 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4601 NULL_TREE, NULL_TREE);
4602 arg1 = build_non_dependent_expr (arg1);
4603 arg2 = build_non_dependent_expr (arg2);
4606 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4607 NULL_TREE, NULL_TREE, &overload, complain);
4609 if (processing_template_decl && expr != error_mark_node)
4611 if (overload != NULL_TREE)
4612 return (build_min_non_dep_op_overload
4613 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4615 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4616 NULL_TREE, NULL_TREE);
4618 return expr;
4621 /* Return whether OP is an expression of enum type cast to integer
4622 type. In C++ even unsigned enum types are cast to signed integer
4623 types. We do not want to issue warnings about comparisons between
4624 signed and unsigned types when one of the types is an enum type.
4625 Those warnings are always false positives in practice. */
4627 static bool
4628 enum_cast_to_int (tree op)
4630 if (CONVERT_EXPR_P (op)
4631 && TREE_TYPE (op) == integer_type_node
4632 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4633 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4634 return true;
4636 /* The cast may have been pushed into a COND_EXPR. */
4637 if (TREE_CODE (op) == COND_EXPR)
4638 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4639 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4641 return false;
4644 /* For the c-common bits. */
4645 tree
4646 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4647 bool /*convert_p*/)
4649 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4652 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4653 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4655 static tree
4656 build_vec_cmp (tree_code code, tree type,
4657 tree arg0, tree arg1)
4659 tree zero_vec = build_zero_cst (type);
4660 tree minus_one_vec = build_minus_one_cst (type);
4661 tree cmp_type = truth_type_for (type);
4662 tree cmp = build2 (code, cmp_type, arg0, arg1);
4663 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4666 /* Possibly warn about an address never being NULL. */
4668 static void
4669 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4671 /* Prevent warnings issued for macro expansion. */
4672 if (!warn_address
4673 || (complain & tf_warning) == 0
4674 || c_inhibit_evaluation_warnings != 0
4675 || from_macro_expansion_at (location)
4676 || warning_suppressed_p (op, OPT_Waddress))
4677 return;
4679 if (TREE_CODE (op) == NON_DEPENDENT_EXPR)
4680 op = TREE_OPERAND (op, 0);
4682 tree cop = fold_for_warn (op);
4684 if (TREE_CODE (cop) == NON_LVALUE_EXPR)
4685 /* Unwrap the expression for C++ 98. */
4686 cop = TREE_OPERAND (cop, 0);
4688 if (TREE_CODE (cop) == PTRMEM_CST)
4690 /* The address of a nonstatic data member is never null. */
4691 warning_at (location, OPT_Waddress,
4692 "the address %qE will never be NULL",
4693 cop);
4694 return;
4697 if (TREE_CODE (cop) == NOP_EXPR)
4699 /* Allow casts to intptr_t to suppress the warning. */
4700 tree type = TREE_TYPE (cop);
4701 if (TREE_CODE (type) == INTEGER_TYPE)
4702 return;
4704 STRIP_NOPS (cop);
4707 bool warned = false;
4708 if (TREE_CODE (cop) == ADDR_EXPR)
4710 cop = TREE_OPERAND (cop, 0);
4712 /* Set to true in the loop below if OP dereferences its operand.
4713 In such a case the ultimate target need not be a decl for
4714 the null [in]equality test to be necessarily constant. */
4715 bool deref = false;
4717 /* Get the outermost array or object, or member. */
4718 while (handled_component_p (cop))
4720 if (TREE_CODE (cop) == COMPONENT_REF)
4722 /* Get the member (its address is never null). */
4723 cop = TREE_OPERAND (cop, 1);
4724 break;
4727 /* Get the outer array/object to refer to in the warning. */
4728 cop = TREE_OPERAND (cop, 0);
4729 deref = true;
4732 if ((!deref && !decl_with_nonnull_addr_p (cop))
4733 || from_macro_expansion_at (location)
4734 || warning_suppressed_p (cop, OPT_Waddress))
4735 return;
4737 warned = warning_at (location, OPT_Waddress,
4738 "the address of %qD will never be NULL", cop);
4739 op = cop;
4741 else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
4743 /* Adding zero to the null pointer is well-defined in C++. When
4744 the offset is unknown (i.e., not a constant) warn anyway since
4745 it's less likely that the pointer operand is null than not. */
4746 tree off = TREE_OPERAND (cop, 1);
4747 if (!integer_zerop (off)
4748 && !warning_suppressed_p (cop, OPT_Waddress))
4749 warning_at (location, OPT_Waddress, "comparing the result of pointer "
4750 "addition %qE and NULL", cop);
4751 return;
4753 else if (CONVERT_EXPR_P (op)
4754 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4756 STRIP_NOPS (op);
4758 if (TREE_CODE (op) == COMPONENT_REF)
4759 op = TREE_OPERAND (op, 1);
4761 if (DECL_P (op))
4762 warned = warning_at (location, OPT_Waddress,
4763 "the compiler can assume that the address of "
4764 "%qD will never be NULL", op);
4767 if (warned && DECL_P (op))
4768 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
4771 /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
4772 the other operand is of a different enumeration type or a floating-point
4773 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
4774 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
4775 and LOC is the location for the whole binary expression.
4776 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
4778 static void
4779 do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
4780 tree type1)
4782 if (TREE_CODE (type0) == ENUMERAL_TYPE
4783 && TREE_CODE (type1) == ENUMERAL_TYPE
4784 && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
4786 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
4787 Otherwise, warn if -Wenum-conversion is on. */
4788 enum opt_code opt;
4789 if (warn_deprecated_enum_enum_conv)
4790 opt = OPT_Wdeprecated_enum_enum_conversion;
4791 else if (warn_enum_conversion)
4792 opt = OPT_Wenum_conversion;
4793 else
4794 return;
4796 switch (code)
4798 case GT_EXPR:
4799 case LT_EXPR:
4800 case GE_EXPR:
4801 case LE_EXPR:
4802 case EQ_EXPR:
4803 case NE_EXPR:
4804 /* Comparisons are handled by -Wenum-compare. */
4805 return;
4806 case SPACESHIP_EXPR:
4807 /* This is invalid, don't warn. */
4808 return;
4809 case BIT_AND_EXPR:
4810 case BIT_IOR_EXPR:
4811 case BIT_XOR_EXPR:
4812 warning_at (loc, opt, "bitwise operation between different "
4813 "enumeration types %qT and %qT is deprecated",
4814 type0, type1);
4815 return;
4816 default:
4817 warning_at (loc, opt, "arithmetic between different enumeration "
4818 "types %qT and %qT is deprecated", type0, type1);
4819 return;
4822 else if ((TREE_CODE (type0) == ENUMERAL_TYPE
4823 && TREE_CODE (type1) == REAL_TYPE)
4824 || (TREE_CODE (type0) == REAL_TYPE
4825 && TREE_CODE (type1) == ENUMERAL_TYPE))
4827 const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
4828 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
4829 Otherwise, warn if -Wenum-conversion is on. */
4830 enum opt_code opt;
4831 if (warn_deprecated_enum_float_conv)
4832 opt = OPT_Wdeprecated_enum_float_conversion;
4833 else if (warn_enum_conversion)
4834 opt = OPT_Wenum_conversion;
4835 else
4836 return;
4838 switch (code)
4840 case GT_EXPR:
4841 case LT_EXPR:
4842 case GE_EXPR:
4843 case LE_EXPR:
4844 case EQ_EXPR:
4845 case NE_EXPR:
4846 if (enum_first_p)
4847 warning_at (loc, opt, "comparison of enumeration type %qT with "
4848 "floating-point type %qT is deprecated",
4849 type0, type1);
4850 else
4851 warning_at (loc, opt, "comparison of floating-point type %qT "
4852 "with enumeration type %qT is deprecated",
4853 type0, type1);
4854 return;
4855 case SPACESHIP_EXPR:
4856 /* This is invalid, don't warn. */
4857 return;
4858 default:
4859 if (enum_first_p)
4860 warning_at (loc, opt, "arithmetic between enumeration type %qT "
4861 "and floating-point type %qT is deprecated",
4862 type0, type1);
4863 else
4864 warning_at (loc, opt, "arithmetic between floating-point type %qT "
4865 "and enumeration type %qT is deprecated",
4866 type0, type1);
4867 return;
4872 /* Build a binary-operation expression without default conversions.
4873 CODE is the kind of expression to build.
4874 LOCATION is the location_t of the operator in the source code.
4875 This function differs from `build' in several ways:
4876 the data type of the result is computed and recorded in it,
4877 warnings are generated if arg data types are invalid,
4878 special handling for addition and subtraction of pointers is known,
4879 and some optimization is done (operations on narrow ints
4880 are done in the narrower type when that gives the same result).
4881 Constant folding is also done before the result is returned.
4883 Note that the operands will never have enumeral types
4884 because either they have just had the default conversions performed
4885 or they have both just been converted to some other type in which
4886 the arithmetic is to be done.
4888 C++: must do special pointer arithmetic when implementing
4889 multiple inheritance, and deal with pointer to member functions. */
4891 tree
4892 cp_build_binary_op (const op_location_t &location,
4893 enum tree_code code, tree orig_op0, tree orig_op1,
4894 tsubst_flags_t complain)
4896 tree op0, op1;
4897 enum tree_code code0, code1;
4898 tree type0, type1;
4899 const char *invalid_op_diag;
4901 /* Expression code to give to the expression when it is built.
4902 Normally this is CODE, which is what the caller asked for,
4903 but in some special cases we change it. */
4904 enum tree_code resultcode = code;
4906 /* Data type in which the computation is to be performed.
4907 In the simplest cases this is the common type of the arguments. */
4908 tree result_type = NULL_TREE;
4910 /* Nonzero means operands have already been type-converted
4911 in whatever way is necessary.
4912 Zero means they need to be converted to RESULT_TYPE. */
4913 int converted = 0;
4915 /* Nonzero means create the expression with this type, rather than
4916 RESULT_TYPE. */
4917 tree build_type = 0;
4919 /* Nonzero means after finally constructing the expression
4920 convert it to this type. */
4921 tree final_type = 0;
4923 tree result, result_ovl;
4925 /* Nonzero if this is an operation like MIN or MAX which can
4926 safely be computed in short if both args are promoted shorts.
4927 Also implies COMMON.
4928 -1 indicates a bitwise operation; this makes a difference
4929 in the exact conditions for when it is safe to do the operation
4930 in a narrower mode. */
4931 int shorten = 0;
4933 /* Nonzero if this is a comparison operation;
4934 if both args are promoted shorts, compare the original shorts.
4935 Also implies COMMON. */
4936 int short_compare = 0;
4938 /* Nonzero if this is a right-shift operation, which can be computed on the
4939 original short and then promoted if the operand is a promoted short. */
4940 int short_shift = 0;
4942 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4943 int common = 0;
4945 /* True if both operands have arithmetic type. */
4946 bool arithmetic_types_p;
4948 /* Remember whether we're doing / or %. */
4949 bool doing_div_or_mod = false;
4951 /* Remember whether we're doing << or >>. */
4952 bool doing_shift = false;
4954 /* Tree holding instrumentation expression. */
4955 tree instrument_expr = NULL_TREE;
4957 /* Apply default conversions. */
4958 op0 = resolve_nondeduced_context (orig_op0, complain);
4959 op1 = resolve_nondeduced_context (orig_op1, complain);
4961 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4962 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4963 || code == TRUTH_XOR_EXPR)
4965 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4966 op0 = decay_conversion (op0, complain);
4967 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4968 op1 = decay_conversion (op1, complain);
4970 else
4972 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4973 op0 = cp_default_conversion (op0, complain);
4974 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4975 op1 = cp_default_conversion (op1, complain);
4978 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4979 STRIP_TYPE_NOPS (op0);
4980 STRIP_TYPE_NOPS (op1);
4982 /* DTRT if one side is an overloaded function, but complain about it. */
4983 if (type_unknown_p (op0))
4985 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4986 if (t != error_mark_node)
4988 if (complain & tf_error)
4989 permerror (location,
4990 "assuming cast to type %qT from overloaded function",
4991 TREE_TYPE (t));
4992 op0 = t;
4995 if (type_unknown_p (op1))
4997 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4998 if (t != error_mark_node)
5000 if (complain & tf_error)
5001 permerror (location,
5002 "assuming cast to type %qT from overloaded function",
5003 TREE_TYPE (t));
5004 op1 = t;
5008 type0 = TREE_TYPE (op0);
5009 type1 = TREE_TYPE (op1);
5011 /* The expression codes of the data types of the arguments tell us
5012 whether the arguments are integers, floating, pointers, etc. */
5013 code0 = TREE_CODE (type0);
5014 code1 = TREE_CODE (type1);
5016 /* If an error was already reported for one of the arguments,
5017 avoid reporting another error. */
5018 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5019 return error_mark_node;
5021 if ((invalid_op_diag
5022 = targetm.invalid_binary_op (code, type0, type1)))
5024 if (complain & tf_error)
5025 error (invalid_op_diag);
5026 return error_mark_node;
5029 /* Issue warnings about peculiar, but valid, uses of NULL. */
5030 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5031 /* It's reasonable to use pointer values as operands of &&
5032 and ||, so NULL is no exception. */
5033 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5034 && ( /* Both are NULL (or 0) and the operation was not a
5035 comparison or a pointer subtraction. */
5036 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5037 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5038 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5039 || (!null_ptr_cst_p (orig_op0)
5040 && !TYPE_PTR_OR_PTRMEM_P (type0))
5041 || (!null_ptr_cst_p (orig_op1)
5042 && !TYPE_PTR_OR_PTRMEM_P (type1)))
5043 && (complain & tf_warning))
5045 location_t loc =
5046 expansion_point_location_if_in_system_header (input_location);
5048 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5051 /* In case when one of the operands of the binary operation is
5052 a vector and another is a scalar -- convert scalar to vector. */
5053 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5054 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5056 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
5057 complain & tf_error);
5059 switch (convert_flag)
5061 case stv_error:
5062 return error_mark_node;
5063 case stv_firstarg:
5065 op0 = convert (TREE_TYPE (type1), op0);
5066 op0 = save_expr (op0);
5067 op0 = build_vector_from_val (type1, op0);
5068 type0 = TREE_TYPE (op0);
5069 code0 = TREE_CODE (type0);
5070 converted = 1;
5071 break;
5073 case stv_secondarg:
5075 op1 = convert (TREE_TYPE (type0), op1);
5076 op1 = save_expr (op1);
5077 op1 = build_vector_from_val (type0, op1);
5078 type1 = TREE_TYPE (op1);
5079 code1 = TREE_CODE (type1);
5080 converted = 1;
5081 break;
5083 default:
5084 break;
5088 switch (code)
5090 case MINUS_EXPR:
5091 /* Subtraction of two similar pointers.
5092 We must subtract them as integers, then divide by object size. */
5093 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5094 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5095 TREE_TYPE (type1)))
5097 result = pointer_diff (location, op0, op1,
5098 common_pointer_type (type0, type1), complain,
5099 &instrument_expr);
5100 if (instrument_expr != NULL)
5101 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5102 instrument_expr, result);
5104 return result;
5106 /* In all other cases except pointer - int, the usual arithmetic
5107 rules apply. */
5108 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5110 common = 1;
5111 break;
5113 /* The pointer - int case is just like pointer + int; fall
5114 through. */
5115 gcc_fallthrough ();
5116 case PLUS_EXPR:
5117 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5118 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5120 tree ptr_operand;
5121 tree int_operand;
5122 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5123 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5124 if (processing_template_decl)
5126 result_type = TREE_TYPE (ptr_operand);
5127 break;
5129 return cp_pointer_int_sum (location, code,
5130 ptr_operand,
5131 int_operand,
5132 complain);
5134 common = 1;
5135 break;
5137 case MULT_EXPR:
5138 common = 1;
5139 break;
5141 case TRUNC_DIV_EXPR:
5142 case CEIL_DIV_EXPR:
5143 case FLOOR_DIV_EXPR:
5144 case ROUND_DIV_EXPR:
5145 case EXACT_DIV_EXPR:
5146 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5148 tree type0 = TREE_OPERAND (op0, 0);
5149 tree type1 = TREE_OPERAND (op1, 0);
5150 tree first_arg = tree_strip_any_location_wrapper (type0);
5151 if (!TYPE_P (type0))
5152 type0 = TREE_TYPE (type0);
5153 if (!TYPE_P (type1))
5154 type1 = TREE_TYPE (type1);
5155 if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1))
5157 if (!(TREE_CODE (first_arg) == PARM_DECL
5158 && DECL_ARRAY_PARAMETER_P (first_arg)
5159 && warn_sizeof_array_argument)
5160 && (complain & tf_warning))
5162 auto_diagnostic_group d;
5163 if (warning_at (location, OPT_Wsizeof_pointer_div,
5164 "division %<sizeof (%T) / sizeof (%T)%> does "
5165 "not compute the number of array elements",
5166 type0, type1))
5167 if (DECL_P (first_arg))
5168 inform (DECL_SOURCE_LOCATION (first_arg),
5169 "first %<sizeof%> operand was declared here");
5172 else if (TREE_CODE (type0) == ARRAY_TYPE
5173 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5174 /* Set by finish_parenthesized_expr. */
5175 && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5176 && (complain & tf_warning))
5177 maybe_warn_sizeof_array_div (location, first_arg, type0,
5178 op1, non_reference (type1));
5181 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5182 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5183 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5184 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5186 enum tree_code tcode0 = code0, tcode1 = code1;
5187 doing_div_or_mod = true;
5188 warn_for_div_by_zero (location, fold_for_warn (op1));
5190 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5191 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5192 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5193 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5195 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5196 resultcode = RDIV_EXPR;
5197 else
5199 /* When dividing two signed integers, we have to promote to int.
5200 unless we divide by a constant != -1. Note that default
5201 conversion will have been performed on the operands at this
5202 point, so we have to dig out the original type to find out if
5203 it was unsigned. */
5204 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5205 shorten = ((TREE_CODE (op0) == NOP_EXPR
5206 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
5207 || (TREE_CODE (stripped_op1) == INTEGER_CST
5208 && ! integer_all_onesp (stripped_op1)));
5211 common = 1;
5213 break;
5215 case BIT_AND_EXPR:
5216 case BIT_IOR_EXPR:
5217 case BIT_XOR_EXPR:
5218 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5219 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5220 && !VECTOR_FLOAT_TYPE_P (type0)
5221 && !VECTOR_FLOAT_TYPE_P (type1)))
5222 shorten = -1;
5223 break;
5225 case TRUNC_MOD_EXPR:
5226 case FLOOR_MOD_EXPR:
5227 doing_div_or_mod = true;
5228 warn_for_div_by_zero (location, fold_for_warn (op1));
5230 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5231 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5232 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5233 common = 1;
5234 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5236 /* Although it would be tempting to shorten always here, that loses
5237 on some targets, since the modulo instruction is undefined if the
5238 quotient can't be represented in the computation mode. We shorten
5239 only if unsigned or if dividing by something we know != -1. */
5240 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5241 shorten = ((TREE_CODE (op0) == NOP_EXPR
5242 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
5243 || (TREE_CODE (stripped_op1) == INTEGER_CST
5244 && ! integer_all_onesp (stripped_op1)));
5245 common = 1;
5247 break;
5249 case TRUTH_ANDIF_EXPR:
5250 case TRUTH_ORIF_EXPR:
5251 case TRUTH_AND_EXPR:
5252 case TRUTH_OR_EXPR:
5253 if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5255 if (!COMPARISON_CLASS_P (op1))
5256 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5257 build_zero_cst (type1), complain);
5258 if (code == TRUTH_ANDIF_EXPR)
5260 tree z = build_zero_cst (TREE_TYPE (op1));
5261 return build_conditional_expr (location, op0, op1, z, complain);
5263 else if (code == TRUTH_ORIF_EXPR)
5265 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5266 return build_conditional_expr (location, op0, m1, op1, complain);
5268 else
5269 gcc_unreachable ();
5271 if (gnu_vector_type_p (type0)
5272 && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5274 if (!COMPARISON_CLASS_P (op0))
5275 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5276 build_zero_cst (type0), complain);
5277 if (!VECTOR_TYPE_P (type1))
5279 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5280 tree z = build_zero_cst (TREE_TYPE (op0));
5281 op1 = build_conditional_expr (location, op1, m1, z, complain);
5283 else if (!COMPARISON_CLASS_P (op1))
5284 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5285 build_zero_cst (type1), complain);
5287 if (code == TRUTH_ANDIF_EXPR)
5288 code = BIT_AND_EXPR;
5289 else if (code == TRUTH_ORIF_EXPR)
5290 code = BIT_IOR_EXPR;
5291 else
5292 gcc_unreachable ();
5294 return cp_build_binary_op (location, code, op0, op1, complain);
5297 result_type = boolean_type_node;
5298 break;
5300 /* Shift operations: result has same type as first operand;
5301 always convert second operand to int.
5302 Also set SHORT_SHIFT if shifting rightward. */
5304 case RSHIFT_EXPR:
5305 if (gnu_vector_type_p (type0)
5306 && code1 == INTEGER_TYPE
5307 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5309 result_type = type0;
5310 converted = 1;
5312 else if (gnu_vector_type_p (type0)
5313 && gnu_vector_type_p (type1)
5314 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5315 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5316 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5317 TYPE_VECTOR_SUBPARTS (type1)))
5319 result_type = type0;
5320 converted = 1;
5322 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5324 tree const_op1 = fold_for_warn (op1);
5325 if (TREE_CODE (const_op1) != INTEGER_CST)
5326 const_op1 = op1;
5327 result_type = type0;
5328 doing_shift = true;
5329 if (TREE_CODE (const_op1) == INTEGER_CST)
5331 if (tree_int_cst_lt (const_op1, integer_zero_node))
5333 if ((complain & tf_warning)
5334 && c_inhibit_evaluation_warnings == 0)
5335 warning_at (location, OPT_Wshift_count_negative,
5336 "right shift count is negative");
5338 else
5340 if (!integer_zerop (const_op1))
5341 short_shift = 1;
5343 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5344 && (complain & tf_warning)
5345 && c_inhibit_evaluation_warnings == 0)
5346 warning_at (location, OPT_Wshift_count_overflow,
5347 "right shift count >= width of type");
5350 /* Avoid converting op1 to result_type later. */
5351 converted = 1;
5353 break;
5355 case LSHIFT_EXPR:
5356 if (gnu_vector_type_p (type0)
5357 && code1 == INTEGER_TYPE
5358 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5360 result_type = type0;
5361 converted = 1;
5363 else if (gnu_vector_type_p (type0)
5364 && gnu_vector_type_p (type1)
5365 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5366 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5367 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5368 TYPE_VECTOR_SUBPARTS (type1)))
5370 result_type = type0;
5371 converted = 1;
5373 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5375 tree const_op0 = fold_for_warn (op0);
5376 if (TREE_CODE (const_op0) != INTEGER_CST)
5377 const_op0 = op0;
5378 tree const_op1 = fold_for_warn (op1);
5379 if (TREE_CODE (const_op1) != INTEGER_CST)
5380 const_op1 = op1;
5381 result_type = type0;
5382 doing_shift = true;
5383 if (TREE_CODE (const_op0) == INTEGER_CST
5384 && tree_int_cst_sgn (const_op0) < 0
5385 && (complain & tf_warning)
5386 && c_inhibit_evaluation_warnings == 0)
5387 warning_at (location, OPT_Wshift_negative_value,
5388 "left shift of negative value");
5389 if (TREE_CODE (const_op1) == INTEGER_CST)
5391 if (tree_int_cst_lt (const_op1, integer_zero_node))
5393 if ((complain & tf_warning)
5394 && c_inhibit_evaluation_warnings == 0)
5395 warning_at (location, OPT_Wshift_count_negative,
5396 "left shift count is negative");
5398 else if (compare_tree_int (const_op1,
5399 TYPE_PRECISION (type0)) >= 0)
5401 if ((complain & tf_warning)
5402 && c_inhibit_evaluation_warnings == 0)
5403 warning_at (location, OPT_Wshift_count_overflow,
5404 "left shift count >= width of type");
5406 else if (TREE_CODE (const_op0) == INTEGER_CST
5407 && (complain & tf_warning))
5408 maybe_warn_shift_overflow (location, const_op0, const_op1);
5410 /* Avoid converting op1 to result_type later. */
5411 converted = 1;
5413 break;
5415 case EQ_EXPR:
5416 case NE_EXPR:
5417 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5418 goto vector_compare;
5419 if ((complain & tf_warning)
5420 && c_inhibit_evaluation_warnings == 0
5421 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5422 warning_at (location, OPT_Wfloat_equal,
5423 "comparing floating-point with %<==%> "
5424 "or %<!=%> is unsafe");
5425 if (complain & tf_warning)
5427 tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5428 tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5429 if ((TREE_CODE (stripped_orig_op0) == STRING_CST
5430 && !integer_zerop (cp_fully_fold (op1)))
5431 || (TREE_CODE (stripped_orig_op1) == STRING_CST
5432 && !integer_zerop (cp_fully_fold (op0))))
5433 warning_at (location, OPT_Waddress,
5434 "comparison with string literal results in "
5435 "unspecified behavior");
5436 else if (warn_array_compare
5437 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5438 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5439 do_warn_array_compare (location, code, stripped_orig_op0,
5440 stripped_orig_op1);
5443 build_type = boolean_type_node;
5444 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5445 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5446 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5447 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5448 short_compare = 1;
5449 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5450 && null_ptr_cst_p (orig_op1))
5451 /* Handle, eg, (void*)0 (c++/43906), and more. */
5452 || (code0 == POINTER_TYPE
5453 && TYPE_PTR_P (type1) && integer_zerop (op1)))
5455 if (TYPE_PTR_P (type1))
5456 result_type = composite_pointer_type (location,
5457 type0, type1, op0, op1,
5458 CPO_COMPARISON, complain);
5459 else
5460 result_type = type0;
5462 if (char_type_p (TREE_TYPE (orig_op1)))
5464 auto_diagnostic_group d;
5465 if (warning_at (location, OPT_Wpointer_compare,
5466 "comparison between pointer and zero character "
5467 "constant"))
5468 inform (location,
5469 "did you mean to dereference the pointer?");
5471 warn_for_null_address (location, op0, complain);
5473 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5474 && null_ptr_cst_p (orig_op0))
5475 /* Handle, eg, (void*)0 (c++/43906), and more. */
5476 || (code1 == POINTER_TYPE
5477 && TYPE_PTR_P (type0) && integer_zerop (op0)))
5479 if (TYPE_PTR_P (type0))
5480 result_type = composite_pointer_type (location,
5481 type0, type1, op0, op1,
5482 CPO_COMPARISON, complain);
5483 else
5484 result_type = type1;
5486 if (char_type_p (TREE_TYPE (orig_op0)))
5488 auto_diagnostic_group d;
5489 if (warning_at (location, OPT_Wpointer_compare,
5490 "comparison between pointer and zero character "
5491 "constant"))
5492 inform (location,
5493 "did you mean to dereference the pointer?");
5495 warn_for_null_address (location, op1, complain);
5497 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5498 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5499 result_type = composite_pointer_type (location,
5500 type0, type1, op0, op1,
5501 CPO_COMPARISON, complain);
5502 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5503 /* One of the operands must be of nullptr_t type. */
5504 result_type = TREE_TYPE (nullptr_node);
5505 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5507 result_type = type0;
5508 if (complain & tf_error)
5509 permerror (location, "ISO C++ forbids comparison between "
5510 "pointer and integer");
5511 else
5512 return error_mark_node;
5514 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5516 result_type = type1;
5517 if (complain & tf_error)
5518 permerror (location, "ISO C++ forbids comparison between "
5519 "pointer and integer");
5520 else
5521 return error_mark_node;
5523 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5525 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5526 == ptrmemfunc_vbit_in_delta)
5528 tree pfn0, delta0, e1, e2;
5530 if (TREE_SIDE_EFFECTS (op0))
5531 op0 = cp_save_expr (op0);
5533 pfn0 = pfn_from_ptrmemfunc (op0);
5534 delta0 = delta_from_ptrmemfunc (op0);
5535 e1 = cp_build_binary_op (location,
5536 EQ_EXPR,
5537 pfn0,
5538 build_zero_cst (TREE_TYPE (pfn0)),
5539 complain);
5540 e2 = cp_build_binary_op (location,
5541 BIT_AND_EXPR,
5542 delta0,
5543 integer_one_node,
5544 complain);
5546 if (complain & tf_warning)
5547 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5549 e2 = cp_build_binary_op (location,
5550 EQ_EXPR, e2, integer_zero_node,
5551 complain);
5552 op0 = cp_build_binary_op (location,
5553 TRUTH_ANDIF_EXPR, e1, e2,
5554 complain);
5555 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5557 else
5559 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5560 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5562 result_type = TREE_TYPE (op0);
5564 warn_for_null_address (location, orig_op0, complain);
5566 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5567 return cp_build_binary_op (location, code, op1, op0, complain);
5568 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5570 tree type;
5571 /* E will be the final comparison. */
5572 tree e;
5573 /* E1 and E2 are for scratch. */
5574 tree e1;
5575 tree e2;
5576 tree pfn0;
5577 tree pfn1;
5578 tree delta0;
5579 tree delta1;
5581 type = composite_pointer_type (location, type0, type1, op0, op1,
5582 CPO_COMPARISON, complain);
5584 if (!same_type_p (TREE_TYPE (op0), type))
5585 op0 = cp_convert_and_check (type, op0, complain);
5586 if (!same_type_p (TREE_TYPE (op1), type))
5587 op1 = cp_convert_and_check (type, op1, complain);
5589 if (op0 == error_mark_node || op1 == error_mark_node)
5590 return error_mark_node;
5592 if (TREE_SIDE_EFFECTS (op0))
5593 op0 = save_expr (op0);
5594 if (TREE_SIDE_EFFECTS (op1))
5595 op1 = save_expr (op1);
5597 pfn0 = pfn_from_ptrmemfunc (op0);
5598 pfn0 = cp_fully_fold (pfn0);
5599 /* Avoid -Waddress warnings (c++/64877). */
5600 if (TREE_CODE (pfn0) == ADDR_EXPR)
5601 suppress_warning (pfn0, OPT_Waddress);
5602 pfn1 = pfn_from_ptrmemfunc (op1);
5603 pfn1 = cp_fully_fold (pfn1);
5604 delta0 = delta_from_ptrmemfunc (op0);
5605 delta1 = delta_from_ptrmemfunc (op1);
5606 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5607 == ptrmemfunc_vbit_in_delta)
5609 /* We generate:
5611 (op0.pfn == op1.pfn
5612 && ((op0.delta == op1.delta)
5613 || (!op0.pfn && op0.delta & 1 == 0
5614 && op1.delta & 1 == 0))
5616 The reason for the `!op0.pfn' bit is that a NULL
5617 pointer-to-member is any member with a zero PFN and
5618 LSB of the DELTA field is 0. */
5620 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
5621 delta0,
5622 integer_one_node,
5623 complain);
5624 e1 = cp_build_binary_op (location,
5625 EQ_EXPR, e1, integer_zero_node,
5626 complain);
5627 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
5628 delta1,
5629 integer_one_node,
5630 complain);
5631 e2 = cp_build_binary_op (location,
5632 EQ_EXPR, e2, integer_zero_node,
5633 complain);
5634 e1 = cp_build_binary_op (location,
5635 TRUTH_ANDIF_EXPR, e2, e1,
5636 complain);
5637 e2 = cp_build_binary_op (location, EQ_EXPR,
5638 pfn0,
5639 build_zero_cst (TREE_TYPE (pfn0)),
5640 complain);
5641 e2 = cp_build_binary_op (location,
5642 TRUTH_ANDIF_EXPR, e2, e1, complain);
5643 e1 = cp_build_binary_op (location,
5644 EQ_EXPR, delta0, delta1, complain);
5645 e1 = cp_build_binary_op (location,
5646 TRUTH_ORIF_EXPR, e1, e2, complain);
5648 else
5650 /* We generate:
5652 (op0.pfn == op1.pfn
5653 && (!op0.pfn || op0.delta == op1.delta))
5655 The reason for the `!op0.pfn' bit is that a NULL
5656 pointer-to-member is any member with a zero PFN; the
5657 DELTA field is unspecified. */
5659 e1 = cp_build_binary_op (location,
5660 EQ_EXPR, delta0, delta1, complain);
5661 e2 = cp_build_binary_op (location,
5662 EQ_EXPR,
5663 pfn0,
5664 build_zero_cst (TREE_TYPE (pfn0)),
5665 complain);
5666 e1 = cp_build_binary_op (location,
5667 TRUTH_ORIF_EXPR, e1, e2, complain);
5669 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
5670 e = cp_build_binary_op (location,
5671 TRUTH_ANDIF_EXPR, e2, e1, complain);
5672 if (code == EQ_EXPR)
5673 return e;
5674 return cp_build_binary_op (location,
5675 EQ_EXPR, e, integer_zero_node, complain);
5677 else
5679 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
5680 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
5681 type1));
5682 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
5683 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
5684 type0));
5687 break;
5689 case MAX_EXPR:
5690 case MIN_EXPR:
5691 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
5692 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
5693 shorten = 1;
5694 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5695 result_type = composite_pointer_type (location,
5696 type0, type1, op0, op1,
5697 CPO_COMPARISON, complain);
5698 break;
5700 case LE_EXPR:
5701 case GE_EXPR:
5702 case LT_EXPR:
5703 case GT_EXPR:
5704 case SPACESHIP_EXPR:
5705 if (TREE_CODE (orig_op0) == STRING_CST
5706 || TREE_CODE (orig_op1) == STRING_CST)
5708 if (complain & tf_warning)
5709 warning_at (location, OPT_Waddress,
5710 "comparison with string literal results "
5711 "in unspecified behavior");
5713 else if (warn_array_compare
5714 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5715 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
5716 && code != SPACESHIP_EXPR
5717 && (complain & tf_warning))
5718 do_warn_array_compare (location, code,
5719 tree_strip_any_location_wrapper (orig_op0),
5720 tree_strip_any_location_wrapper (orig_op1));
5722 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5724 vector_compare:
5725 tree intt;
5726 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5727 TREE_TYPE (type1))
5728 && !vector_types_compatible_elements_p (type0, type1))
5730 if (complain & tf_error)
5732 error_at (location, "comparing vectors with different "
5733 "element types");
5734 inform (location, "operand types are %qT and %qT",
5735 type0, type1);
5737 return error_mark_node;
5740 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5741 TYPE_VECTOR_SUBPARTS (type1)))
5743 if (complain & tf_error)
5745 error_at (location, "comparing vectors with different "
5746 "number of elements");
5747 inform (location, "operand types are %qT and %qT",
5748 type0, type1);
5750 return error_mark_node;
5753 /* It's not precisely specified how the usual arithmetic
5754 conversions apply to the vector types. Here, we use
5755 the unsigned type if one of the operands is signed and
5756 the other one is unsigned. */
5757 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5759 if (!TYPE_UNSIGNED (type0))
5760 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5761 else
5762 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5763 warning_at (location, OPT_Wsign_compare, "comparison between "
5764 "types %qT and %qT", type0, type1);
5767 if (resultcode == SPACESHIP_EXPR)
5769 if (complain & tf_error)
5770 sorry_at (location, "three-way comparison of vectors");
5771 return error_mark_node;
5774 /* Always construct signed integer vector type. */
5775 intt = c_common_type_for_size
5776 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5777 if (!intt)
5779 if (complain & tf_error)
5780 error_at (location, "could not find an integer type "
5781 "of the same size as %qT", TREE_TYPE (type0));
5782 return error_mark_node;
5784 result_type = build_opaque_vector_type (intt,
5785 TYPE_VECTOR_SUBPARTS (type0));
5786 return build_vec_cmp (resultcode, result_type, op0, op1);
5788 build_type = boolean_type_node;
5789 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5790 || code0 == ENUMERAL_TYPE)
5791 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5792 || code1 == ENUMERAL_TYPE))
5793 short_compare = 1;
5794 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5795 result_type = composite_pointer_type (location,
5796 type0, type1, op0, op1,
5797 CPO_COMPARISON, complain);
5798 else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5799 || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5800 || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
5802 /* Core Issue 1512 made this ill-formed. */
5803 if (complain & tf_error)
5804 error_at (location, "ordered comparison of pointer with "
5805 "integer zero (%qT and %qT)", type0, type1);
5806 return error_mark_node;
5808 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5810 result_type = type0;
5811 if (complain & tf_error)
5812 permerror (location, "ISO C++ forbids comparison between "
5813 "pointer and integer");
5814 else
5815 return error_mark_node;
5817 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5819 result_type = type1;
5820 if (complain & tf_error)
5821 permerror (location, "ISO C++ forbids comparison between "
5822 "pointer and integer");
5823 else
5824 return error_mark_node;
5827 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5828 && !processing_template_decl
5829 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5831 op0 = save_expr (op0);
5832 op1 = save_expr (op1);
5834 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5835 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5838 break;
5840 case UNORDERED_EXPR:
5841 case ORDERED_EXPR:
5842 case UNLT_EXPR:
5843 case UNLE_EXPR:
5844 case UNGT_EXPR:
5845 case UNGE_EXPR:
5846 case UNEQ_EXPR:
5847 build_type = integer_type_node;
5848 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5850 if (complain & tf_error)
5851 error ("unordered comparison on non-floating-point argument");
5852 return error_mark_node;
5854 common = 1;
5855 break;
5857 default:
5858 break;
5861 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5862 || code0 == ENUMERAL_TYPE)
5863 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5864 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5865 arithmetic_types_p = 1;
5866 else
5868 arithmetic_types_p = 0;
5869 /* Vector arithmetic is only allowed when both sides are vectors. */
5870 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5872 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5873 || !vector_types_compatible_elements_p (type0, type1))
5875 if (complain & tf_error)
5877 /* "location" already embeds the locations of the
5878 operands, so we don't need to add them separately
5879 to richloc. */
5880 rich_location richloc (line_table, location);
5881 binary_op_error (&richloc, code, type0, type1);
5883 return error_mark_node;
5885 arithmetic_types_p = 1;
5888 /* Determine the RESULT_TYPE, if it is not already known. */
5889 if (!result_type
5890 && arithmetic_types_p
5891 && (shorten || common || short_compare))
5893 result_type = cp_common_type (type0, type1);
5894 if (complain & tf_warning)
5896 do_warn_double_promotion (result_type, type0, type1,
5897 "implicit conversion from %qH to %qI "
5898 "to match other operand of binary "
5899 "expression",
5900 location);
5901 do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
5902 TREE_TYPE (orig_op1));
5906 if (code == SPACESHIP_EXPR)
5908 iloc_sentinel s (location);
5910 tree orig_type0 = TREE_TYPE (orig_op0);
5911 tree_code orig_code0 = TREE_CODE (orig_type0);
5912 tree orig_type1 = TREE_TYPE (orig_op1);
5913 tree_code orig_code1 = TREE_CODE (orig_type1);
5914 if (!result_type)
5915 /* Nope. */;
5916 else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
5917 /* "If one of the operands is of type bool and the other is not, the
5918 program is ill-formed." */
5919 result_type = NULL_TREE;
5920 else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
5921 && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
5922 /* We only do array/function-to-pointer conversion if "at least one of
5923 the operands is of pointer type". */
5924 result_type = NULL_TREE;
5925 else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
5926 /* <=> no longer supports equality relations. */
5927 result_type = NULL_TREE;
5928 else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
5929 && !(same_type_ignoring_top_level_qualifiers_p
5930 (orig_type0, orig_type1)))
5931 /* "If both operands have arithmetic types, or one operand has integral
5932 type and the other operand has unscoped enumeration type, the usual
5933 arithmetic conversions are applied to the operands." So we don't do
5934 arithmetic conversions if the operands both have enumeral type. */
5935 result_type = NULL_TREE;
5936 else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
5937 || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
5938 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
5939 [where one is of enumeration type and the other is of a different
5940 enumeration type or a floating-point type] are ill-formed. */
5941 result_type = NULL_TREE;
5943 if (result_type)
5945 build_type = spaceship_type (result_type, complain);
5946 if (build_type == error_mark_node)
5947 return error_mark_node;
5950 if (result_type && arithmetic_types_p)
5952 /* If a narrowing conversion is required, other than from an integral
5953 type to a floating point type, the program is ill-formed. */
5954 bool ok = true;
5955 if (TREE_CODE (result_type) == REAL_TYPE
5956 && CP_INTEGRAL_TYPE_P (orig_type0))
5957 /* OK */;
5958 else if (!check_narrowing (result_type, orig_op0, complain))
5959 ok = false;
5960 if (TREE_CODE (result_type) == REAL_TYPE
5961 && CP_INTEGRAL_TYPE_P (orig_type1))
5962 /* OK */;
5963 else if (!check_narrowing (result_type, orig_op1, complain))
5964 ok = false;
5965 if (!ok && !(complain & tf_error))
5966 return error_mark_node;
5970 if (!result_type)
5972 if (complain & tf_error)
5974 binary_op_rich_location richloc (location,
5975 orig_op0, orig_op1, true);
5976 error_at (&richloc,
5977 "invalid operands of types %qT and %qT to binary %qO",
5978 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5980 return error_mark_node;
5983 /* If we're in a template, the only thing we need to know is the
5984 RESULT_TYPE. */
5985 if (processing_template_decl)
5987 /* Since the middle-end checks the type when doing a build2, we
5988 need to build the tree in pieces. This built tree will never
5989 get out of the front-end as we replace it when instantiating
5990 the template. */
5991 tree tmp = build2 (resultcode,
5992 build_type ? build_type : result_type,
5993 NULL_TREE, op1);
5994 TREE_OPERAND (tmp, 0) = op0;
5995 return tmp;
5998 /* Remember the original type; RESULT_TYPE might be changed later on
5999 by shorten_binary_op. */
6000 tree orig_type = result_type;
6002 if (arithmetic_types_p)
6004 bool first_complex = (code0 == COMPLEX_TYPE);
6005 bool second_complex = (code1 == COMPLEX_TYPE);
6006 int none_complex = (!first_complex && !second_complex);
6008 /* Adapted from patch for c/24581. */
6009 if (first_complex != second_complex
6010 && (code == PLUS_EXPR
6011 || code == MINUS_EXPR
6012 || code == MULT_EXPR
6013 || (code == TRUNC_DIV_EXPR && first_complex))
6014 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6015 && flag_signed_zeros)
6017 /* An operation on mixed real/complex operands must be
6018 handled specially, but the language-independent code can
6019 more easily optimize the plain complex arithmetic if
6020 -fno-signed-zeros. */
6021 tree real_type = TREE_TYPE (result_type);
6022 tree real, imag;
6023 if (first_complex)
6025 if (TREE_TYPE (op0) != result_type)
6026 op0 = cp_convert_and_check (result_type, op0, complain);
6027 if (TREE_TYPE (op1) != real_type)
6028 op1 = cp_convert_and_check (real_type, op1, complain);
6030 else
6032 if (TREE_TYPE (op0) != real_type)
6033 op0 = cp_convert_and_check (real_type, op0, complain);
6034 if (TREE_TYPE (op1) != result_type)
6035 op1 = cp_convert_and_check (result_type, op1, complain);
6037 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6038 return error_mark_node;
6039 if (first_complex)
6041 op0 = save_expr (op0);
6042 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6043 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6044 switch (code)
6046 case MULT_EXPR:
6047 case TRUNC_DIV_EXPR:
6048 op1 = save_expr (op1);
6049 imag = build2 (resultcode, real_type, imag, op1);
6050 /* Fall through. */
6051 case PLUS_EXPR:
6052 case MINUS_EXPR:
6053 real = build2 (resultcode, real_type, real, op1);
6054 break;
6055 default:
6056 gcc_unreachable();
6059 else
6061 op1 = save_expr (op1);
6062 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6063 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6064 switch (code)
6066 case MULT_EXPR:
6067 op0 = save_expr (op0);
6068 imag = build2 (resultcode, real_type, op0, imag);
6069 /* Fall through. */
6070 case PLUS_EXPR:
6071 real = build2 (resultcode, real_type, op0, real);
6072 break;
6073 case MINUS_EXPR:
6074 real = build2 (resultcode, real_type, op0, real);
6075 imag = build1 (NEGATE_EXPR, real_type, imag);
6076 break;
6077 default:
6078 gcc_unreachable();
6081 result = build2 (COMPLEX_EXPR, result_type, real, imag);
6082 return result;
6085 /* For certain operations (which identify themselves by shorten != 0)
6086 if both args were extended from the same smaller type,
6087 do the arithmetic in that type and then extend.
6089 shorten !=0 and !=1 indicates a bitwise operation.
6090 For them, this optimization is safe only if
6091 both args are zero-extended or both are sign-extended.
6092 Otherwise, we might change the result.
6093 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6094 but calculated in (unsigned short) it would be (unsigned short)-1. */
6096 if (shorten && none_complex)
6098 final_type = result_type;
6099 result_type = shorten_binary_op (result_type, op0, op1,
6100 shorten == -1);
6103 /* Shifts can be shortened if shifting right. */
6105 if (short_shift)
6107 int unsigned_arg;
6108 tree arg0 = get_narrower (op0, &unsigned_arg);
6109 /* We're not really warning here but when we set short_shift we
6110 used fold_for_warn to fold the operand. */
6111 tree const_op1 = fold_for_warn (op1);
6113 final_type = result_type;
6115 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6116 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6118 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6119 && tree_int_cst_sgn (const_op1) > 0
6120 /* We can shorten only if the shift count is less than the
6121 number of bits in the smaller type size. */
6122 && compare_tree_int (const_op1,
6123 TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6124 /* We cannot drop an unsigned shift after sign-extension. */
6125 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6127 /* Do an unsigned shift if the operand was zero-extended. */
6128 result_type
6129 = c_common_signed_or_unsigned_type (unsigned_arg,
6130 TREE_TYPE (arg0));
6131 /* Convert value-to-be-shifted to that type. */
6132 if (TREE_TYPE (op0) != result_type)
6133 op0 = convert (result_type, op0);
6134 converted = 1;
6138 /* Comparison operations are shortened too but differently.
6139 They identify themselves by setting short_compare = 1. */
6141 if (short_compare)
6143 /* We call shorten_compare only for diagnostics. */
6144 tree xop0 = fold_simple (op0);
6145 tree xop1 = fold_simple (op1);
6146 tree xresult_type = result_type;
6147 enum tree_code xresultcode = resultcode;
6148 shorten_compare (location, &xop0, &xop1, &xresult_type,
6149 &xresultcode);
6152 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6153 && warn_sign_compare
6154 /* Do not warn until the template is instantiated; we cannot
6155 bound the ranges of the arguments until that point. */
6156 && !processing_template_decl
6157 && (complain & tf_warning)
6158 && c_inhibit_evaluation_warnings == 0
6159 /* Even unsigned enum types promote to signed int. We don't
6160 want to issue -Wsign-compare warnings for this case. */
6161 && !enum_cast_to_int (orig_op0)
6162 && !enum_cast_to_int (orig_op1))
6164 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6165 result_type, resultcode);
6169 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6170 Then the expression will be built.
6171 It will be given type FINAL_TYPE if that is nonzero;
6172 otherwise, it will be given type RESULT_TYPE. */
6173 if (! converted)
6175 warning_sentinel w (warn_sign_conversion, short_compare);
6176 if (!same_type_p (TREE_TYPE (op0), result_type))
6177 op0 = cp_convert_and_check (result_type, op0, complain);
6178 if (!same_type_p (TREE_TYPE (op1), result_type))
6179 op1 = cp_convert_and_check (result_type, op1, complain);
6181 if (op0 == error_mark_node || op1 == error_mark_node)
6182 return error_mark_node;
6185 if (build_type == NULL_TREE)
6186 build_type = result_type;
6188 if (doing_shift
6189 && flag_strong_eval_order == 2
6190 && TREE_SIDE_EFFECTS (op1)
6191 && !processing_template_decl)
6193 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6194 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6195 op0 = cp_save_expr (op0);
6196 instrument_expr = op0;
6199 if (sanitize_flags_p ((SANITIZE_SHIFT
6200 | SANITIZE_DIVIDE
6201 | SANITIZE_FLOAT_DIVIDE
6202 | SANITIZE_SI_OVERFLOW))
6203 && current_function_decl != NULL_TREE
6204 && !processing_template_decl
6205 && (doing_div_or_mod || doing_shift))
6207 /* OP0 and/or OP1 might have side-effects. */
6208 op0 = cp_save_expr (op0);
6209 op1 = cp_save_expr (op1);
6210 op0 = fold_non_dependent_expr (op0, complain);
6211 op1 = fold_non_dependent_expr (op1, complain);
6212 tree instrument_expr1 = NULL_TREE;
6213 if (doing_div_or_mod
6214 && sanitize_flags_p (SANITIZE_DIVIDE
6215 | SANITIZE_FLOAT_DIVIDE
6216 | SANITIZE_SI_OVERFLOW))
6218 /* For diagnostics we want to use the promoted types without
6219 shorten_binary_op. So convert the arguments to the
6220 original result_type. */
6221 tree cop0 = op0;
6222 tree cop1 = op1;
6223 if (TREE_TYPE (cop0) != orig_type)
6224 cop0 = cp_convert (orig_type, op0, complain);
6225 if (TREE_TYPE (cop1) != orig_type)
6226 cop1 = cp_convert (orig_type, op1, complain);
6227 instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6229 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6230 instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6231 if (instrument_expr != NULL)
6232 instrument_expr = add_stmt_to_compound (instrument_expr,
6233 instrument_expr1);
6234 else
6235 instrument_expr = instrument_expr1;
6238 result = build2_loc (location, resultcode, build_type, op0, op1);
6239 if (final_type != 0)
6240 result = cp_convert (final_type, result, complain);
6242 if (instrument_expr != NULL)
6243 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6244 instrument_expr, result);
6246 if (!processing_template_decl)
6248 if (resultcode == SPACESHIP_EXPR)
6249 result = get_target_expr_sfinae (result, complain);
6250 op0 = cp_fully_fold (op0);
6251 /* Only consider the second argument if the first isn't overflowed. */
6252 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6253 return result;
6254 op1 = cp_fully_fold (op1);
6255 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6256 return result;
6258 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6259 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6260 return result;
6262 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6263 if (TREE_OVERFLOW_P (result_ovl))
6264 overflow_warning (location, result_ovl);
6266 return result;
6269 /* Build a VEC_PERM_EXPR.
6270 This is a simple wrapper for c_build_vec_perm_expr. */
6271 tree
6272 build_x_vec_perm_expr (location_t loc,
6273 tree arg0, tree arg1, tree arg2,
6274 tsubst_flags_t complain)
6276 tree orig_arg0 = arg0;
6277 tree orig_arg1 = arg1;
6278 tree orig_arg2 = arg2;
6279 if (processing_template_decl)
6281 if (type_dependent_expression_p (arg0)
6282 || type_dependent_expression_p (arg1)
6283 || type_dependent_expression_p (arg2))
6284 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6285 arg0 = build_non_dependent_expr (arg0);
6286 if (arg1)
6287 arg1 = build_non_dependent_expr (arg1);
6288 arg2 = build_non_dependent_expr (arg2);
6290 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6291 if (processing_template_decl && exp != error_mark_node)
6292 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6293 orig_arg1, orig_arg2);
6294 return exp;
6297 /* Build a VEC_PERM_EXPR.
6298 This is a simple wrapper for c_build_shufflevector. */
6299 tree
6300 build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6301 tsubst_flags_t complain)
6303 tree arg0 = (*args)[0];
6304 tree arg1 = (*args)[1];
6305 if (processing_template_decl)
6307 for (unsigned i = 0; i < args->length (); ++i)
6308 if (type_dependent_expression_p ((*args)[i]))
6310 tree exp = build_min_nt_call_vec (NULL, args);
6311 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6312 return exp;
6314 arg0 = build_non_dependent_expr (arg0);
6315 arg1 = build_non_dependent_expr (arg1);
6316 /* ??? Nothing needed for the index arguments? */
6318 auto_vec<tree, 16> mask;
6319 for (unsigned i = 2; i < args->length (); ++i)
6321 tree idx = maybe_constant_value ((*args)[i]);
6322 mask.safe_push (idx);
6324 tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6325 if (processing_template_decl && exp != error_mark_node)
6327 exp = build_min_non_dep_call_vec (exp, NULL, args);
6328 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6330 return exp;
6333 /* Return a tree for the sum or difference (RESULTCODE says which)
6334 of pointer PTROP and integer INTOP. */
6336 static tree
6337 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6338 tree intop, tsubst_flags_t complain)
6340 tree res_type = TREE_TYPE (ptrop);
6342 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6343 in certain circumstance (when it's valid to do so). So we need
6344 to make sure it's complete. We don't need to check here, if we
6345 can actually complete it at all, as those checks will be done in
6346 pointer_int_sum() anyway. */
6347 complete_type (TREE_TYPE (res_type));
6349 return pointer_int_sum (loc, resultcode, ptrop,
6350 intop, complain & tf_warning_or_error);
6353 /* Return a tree for the difference of pointers OP0 and OP1.
6354 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6355 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6357 static tree
6358 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6359 tsubst_flags_t complain, tree *instrument_expr)
6361 tree result, inttype;
6362 tree restype = ptrdiff_type_node;
6363 tree target_type = TREE_TYPE (ptrtype);
6365 if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6366 return error_mark_node;
6368 if (VOID_TYPE_P (target_type))
6370 if (complain & tf_error)
6371 permerror (loc, "ISO C++ forbids using pointer of "
6372 "type %<void *%> in subtraction");
6373 else
6374 return error_mark_node;
6376 if (TREE_CODE (target_type) == FUNCTION_TYPE)
6378 if (complain & tf_error)
6379 permerror (loc, "ISO C++ forbids using pointer to "
6380 "a function in subtraction");
6381 else
6382 return error_mark_node;
6384 if (TREE_CODE (target_type) == METHOD_TYPE)
6386 if (complain & tf_error)
6387 permerror (loc, "ISO C++ forbids using pointer to "
6388 "a method in subtraction");
6389 else
6390 return error_mark_node;
6392 else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6393 TREE_TYPE (TREE_TYPE (op0)),
6394 !(complain & tf_error))
6395 || !verify_type_context (loc, TCTX_POINTER_ARITH,
6396 TREE_TYPE (TREE_TYPE (op1)),
6397 !(complain & tf_error)))
6398 return error_mark_node;
6400 /* Determine integer type result of the subtraction. This will usually
6401 be the same as the result type (ptrdiff_t), but may need to be a wider
6402 type if pointers for the address space are wider than ptrdiff_t. */
6403 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6404 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6405 else
6406 inttype = restype;
6408 if (!processing_template_decl
6409 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6411 op0 = save_expr (op0);
6412 op1 = save_expr (op1);
6414 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6415 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6418 /* First do the subtraction, then build the divide operator
6419 and only convert at the very end.
6420 Do not do default conversions in case restype is a short type. */
6422 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6423 pointers. If some platform cannot provide that, or has a larger
6424 ptrdiff_type to support differences larger than half the address
6425 space, cast the pointers to some larger integer type and do the
6426 computations in that type. */
6427 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6428 op0 = cp_build_binary_op (loc,
6429 MINUS_EXPR,
6430 cp_convert (inttype, op0, complain),
6431 cp_convert (inttype, op1, complain),
6432 complain);
6433 else
6434 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6436 /* This generates an error if op1 is a pointer to an incomplete type. */
6437 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6439 if (complain & tf_error)
6440 error_at (loc, "invalid use of a pointer to an incomplete type in "
6441 "pointer arithmetic");
6442 else
6443 return error_mark_node;
6446 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6448 if (complain & tf_error)
6449 error_at (loc, "arithmetic on pointer to an empty aggregate");
6450 else
6451 return error_mark_node;
6454 op1 = (TYPE_PTROB_P (ptrtype)
6455 ? size_in_bytes_loc (loc, target_type)
6456 : integer_one_node);
6458 /* Do the division. */
6460 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
6461 cp_convert (inttype, op1, complain));
6462 return cp_convert (restype, result, complain);
6465 /* Construct and perhaps optimize a tree representation
6466 for a unary operation. CODE, a tree_code, specifies the operation
6467 and XARG is the operand. */
6469 tree
6470 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
6471 tree lookups, tsubst_flags_t complain)
6473 tree orig_expr = xarg;
6474 tree exp;
6475 int ptrmem = 0;
6476 tree overload = NULL_TREE;
6478 if (processing_template_decl)
6480 if (type_dependent_expression_p (xarg))
6482 tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6483 TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
6484 return e;
6487 xarg = build_non_dependent_expr (xarg);
6490 exp = NULL_TREE;
6492 /* [expr.unary.op] says:
6494 The address of an object of incomplete type can be taken.
6496 (And is just the ordinary address operator, not an overloaded
6497 "operator &".) However, if the type is a template
6498 specialization, we must complete the type at this point so that
6499 an overloaded "operator &" will be available if required. */
6500 if (code == ADDR_EXPR
6501 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6502 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6503 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6504 || (TREE_CODE (xarg) == OFFSET_REF)))
6505 /* Don't look for a function. */;
6506 else
6507 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6508 NULL_TREE, lookups, &overload, complain);
6510 if (!exp && code == ADDR_EXPR)
6512 if (is_overloaded_fn (xarg))
6514 tree fn = get_first_fn (xarg);
6515 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
6517 if (complain & tf_error)
6518 error_at (loc, DECL_CONSTRUCTOR_P (fn)
6519 ? G_("taking address of constructor %qD")
6520 : G_("taking address of destructor %qD"),
6521 fn);
6522 return error_mark_node;
6526 /* A pointer to member-function can be formed only by saying
6527 &X::mf. */
6528 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
6529 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
6531 if (TREE_CODE (xarg) != OFFSET_REF
6532 || !TYPE_P (TREE_OPERAND (xarg, 0)))
6534 if (complain & tf_error)
6536 error_at (loc, "invalid use of %qE to form a "
6537 "pointer-to-member-function", xarg.get_value ());
6538 if (TREE_CODE (xarg) != OFFSET_REF)
6539 inform (loc, " a qualified-id is required");
6541 return error_mark_node;
6543 else
6545 if (complain & tf_error)
6546 error_at (loc, "parentheses around %qE cannot be used to "
6547 "form a pointer-to-member-function",
6548 xarg.get_value ());
6549 else
6550 return error_mark_node;
6551 PTRMEM_OK_P (xarg) = 1;
6555 if (TREE_CODE (xarg) == OFFSET_REF)
6557 ptrmem = PTRMEM_OK_P (xarg);
6559 if (!ptrmem && !flag_ms_extensions
6560 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
6562 /* A single non-static member, make sure we don't allow a
6563 pointer-to-member. */
6564 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
6565 TREE_OPERAND (xarg, 0),
6566 ovl_make (TREE_OPERAND (xarg, 1)));
6567 PTRMEM_OK_P (xarg) = ptrmem;
6571 exp = cp_build_addr_expr_strict (xarg, complain);
6573 if (TREE_CODE (exp) == PTRMEM_CST)
6574 PTRMEM_CST_LOCATION (exp) = loc;
6575 else
6576 protected_set_expr_location (exp, loc);
6579 if (processing_template_decl && exp != error_mark_node)
6581 if (overload != NULL_TREE)
6582 return (build_min_non_dep_op_overload
6583 (code, exp, overload, orig_expr, integer_zero_node));
6585 exp = build_min_non_dep (code, exp, orig_expr,
6586 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
6588 if (TREE_CODE (exp) == ADDR_EXPR)
6589 PTRMEM_OK_P (exp) = ptrmem;
6590 return exp;
6593 /* Construct and perhaps optimize a tree representation
6594 for __builtin_addressof operation. ARG specifies the operand. */
6596 tree
6597 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
6599 tree orig_expr = arg;
6601 if (processing_template_decl)
6603 if (type_dependent_expression_p (arg))
6604 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
6606 arg = build_non_dependent_expr (arg);
6609 tree exp = cp_build_addr_expr_strict (arg, complain);
6611 if (processing_template_decl && exp != error_mark_node)
6612 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
6613 return exp;
6616 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6617 constants, where a null value is represented by an INTEGER_CST of
6618 -1. */
6620 tree
6621 cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
6623 tree type = TREE_TYPE (expr);
6624 location_t loc = cp_expr_loc_or_input_loc (expr);
6625 if (TYPE_PTR_OR_PTRMEM_P (type)
6626 /* Avoid ICE on invalid use of non-static member function. */
6627 || TREE_CODE (expr) == FUNCTION_DECL)
6628 return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
6629 else
6630 return c_common_truthvalue_conversion (loc, expr);
6633 /* Returns EXPR contextually converted to bool. */
6635 tree
6636 contextual_conv_bool (tree expr, tsubst_flags_t complain)
6638 return perform_implicit_conversion_flags (boolean_type_node, expr,
6639 complain, LOOKUP_NORMAL);
6642 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
6643 is a low-level function; most callers should use maybe_convert_cond. */
6645 tree
6646 condition_conversion (tree expr)
6648 tree t = contextual_conv_bool (expr, tf_warning_or_error);
6649 if (!processing_template_decl)
6650 t = fold_build_cleanup_point_expr (boolean_type_node, t);
6651 return t;
6654 /* Returns the address of T. This function will fold away
6655 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
6656 most places should use cp_build_addr_expr instead. */
6658 tree
6659 build_address (tree t)
6661 if (error_operand_p (t) || !cxx_mark_addressable (t))
6662 return error_mark_node;
6663 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
6664 || processing_template_decl);
6665 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
6666 if (TREE_CODE (t) != ADDR_EXPR)
6667 t = rvalue (t);
6668 return t;
6671 /* Return a NOP_EXPR converting EXPR to TYPE. */
6673 tree
6674 build_nop (tree type, tree expr)
6676 if (type == error_mark_node || error_operand_p (expr))
6677 return expr;
6678 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
6681 /* Take the address of ARG, whatever that means under C++ semantics.
6682 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
6683 and class rvalues as well.
6685 Nothing should call this function directly; instead, callers should use
6686 cp_build_addr_expr or cp_build_addr_expr_strict. */
6688 static tree
6689 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
6691 tree argtype;
6692 tree val;
6694 if (!arg || error_operand_p (arg))
6695 return error_mark_node;
6697 arg = mark_lvalue_use (arg);
6698 if (error_operand_p (arg))
6699 return error_mark_node;
6701 argtype = lvalue_type (arg);
6702 location_t loc = cp_expr_loc_or_input_loc (arg);
6704 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
6706 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
6707 && !really_overloaded_fn (arg))
6709 /* They're trying to take the address of a unique non-static
6710 member function. This is ill-formed (except in MS-land),
6711 but let's try to DTRT.
6712 Note: We only handle unique functions here because we don't
6713 want to complain if there's a static overload; non-unique
6714 cases will be handled by instantiate_type. But we need to
6715 handle this case here to allow casts on the resulting PMF.
6716 We could defer this in non-MS mode, but it's easier to give
6717 a useful error here. */
6719 /* Inside constant member functions, the `this' pointer
6720 contains an extra const qualifier. TYPE_MAIN_VARIANT
6721 is used here to remove this const from the diagnostics
6722 and the created OFFSET_REF. */
6723 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
6724 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
6725 if (!mark_used (fn, complain) && !(complain & tf_error))
6726 return error_mark_node;
6728 if (! flag_ms_extensions)
6730 tree name = DECL_NAME (fn);
6731 if (!(complain & tf_error))
6732 return error_mark_node;
6733 else if (current_class_type
6734 && TREE_OPERAND (arg, 0) == current_class_ref)
6735 /* An expression like &memfn. */
6736 permerror (loc,
6737 "ISO C++ forbids taking the address of an unqualified"
6738 " or parenthesized non-static member function to form"
6739 " a pointer to member function. Say %<&%T::%D%>",
6740 base, name);
6741 else
6742 permerror (loc,
6743 "ISO C++ forbids taking the address of a bound member"
6744 " function to form a pointer to member function."
6745 " Say %<&%T::%D%>",
6746 base, name);
6748 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
6751 /* Uninstantiated types are all functions. Taking the
6752 address of a function is a no-op, so just return the
6753 argument. */
6754 if (type_unknown_p (arg))
6755 return build1 (ADDR_EXPR, unknown_type_node, arg);
6757 if (TREE_CODE (arg) == OFFSET_REF)
6758 /* We want a pointer to member; bypass all the code for actually taking
6759 the address of something. */
6760 goto offset_ref;
6762 /* Anything not already handled and not a true memory reference
6763 is an error. */
6764 if (!FUNC_OR_METHOD_TYPE_P (argtype))
6766 cp_lvalue_kind kind = lvalue_kind (arg);
6767 if (kind == clk_none)
6769 if (complain & tf_error)
6770 lvalue_error (loc, lv_addressof);
6771 return error_mark_node;
6773 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
6775 if (!(complain & tf_error))
6776 return error_mark_node;
6777 /* Make this a permerror because we used to accept it. */
6778 permerror (loc, "taking address of rvalue");
6782 if (TYPE_REF_P (argtype))
6784 tree type = build_pointer_type (TREE_TYPE (argtype));
6785 arg = build1 (CONVERT_EXPR, type, arg);
6786 return arg;
6788 else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
6790 /* ARM $3.4 */
6791 /* Apparently a lot of autoconf scripts for C++ packages do this,
6792 so only complain if -Wpedantic. */
6793 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
6794 pedwarn (loc, OPT_Wpedantic,
6795 "ISO C++ forbids taking address of function %<::main%>");
6796 else if (flag_pedantic_errors)
6797 return error_mark_node;
6800 /* Let &* cancel out to simplify resulting code. */
6801 if (INDIRECT_REF_P (arg))
6803 arg = TREE_OPERAND (arg, 0);
6804 if (TYPE_REF_P (TREE_TYPE (arg)))
6806 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
6807 arg = build1 (CONVERT_EXPR, type, arg);
6809 else
6810 /* Don't let this be an lvalue. */
6811 arg = rvalue (arg);
6812 return arg;
6815 /* Handle complex lvalues (when permitted)
6816 by reduction to simpler cases. */
6817 val = unary_complex_lvalue (ADDR_EXPR, arg);
6818 if (val != 0)
6819 return val;
6821 switch (TREE_CODE (arg))
6823 CASE_CONVERT:
6824 case FLOAT_EXPR:
6825 case FIX_TRUNC_EXPR:
6826 /* We should have handled this above in the lvalue_kind check. */
6827 gcc_unreachable ();
6828 break;
6830 case BASELINK:
6831 arg = BASELINK_FUNCTIONS (arg);
6832 /* Fall through. */
6834 case OVERLOAD:
6835 arg = OVL_FIRST (arg);
6836 break;
6838 case OFFSET_REF:
6839 offset_ref:
6840 /* Turn a reference to a non-static data member into a
6841 pointer-to-member. */
6843 tree type;
6844 tree t;
6846 gcc_assert (PTRMEM_OK_P (arg));
6848 t = TREE_OPERAND (arg, 1);
6849 if (TYPE_REF_P (TREE_TYPE (t)))
6851 if (complain & tf_error)
6852 error_at (loc,
6853 "cannot create pointer to reference member %qD", t);
6854 return error_mark_node;
6857 /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
6858 if (TREE_CODE (t) == FUNCTION_DECL
6859 && !DECL_PURE_VIRTUAL_P (t)
6860 && !mark_used (t, complain) && !(complain & tf_error))
6861 return error_mark_node;
6863 type = build_ptrmem_type (context_for_name_lookup (t),
6864 TREE_TYPE (t));
6865 t = make_ptrmem_cst (type, t);
6866 return t;
6869 default:
6870 break;
6873 if (argtype != error_mark_node)
6874 argtype = build_pointer_type (argtype);
6876 if (bitfield_p (arg))
6878 if (complain & tf_error)
6879 error_at (loc, "attempt to take address of bit-field");
6880 return error_mark_node;
6883 /* In a template, we are processing a non-dependent expression
6884 so we can just form an ADDR_EXPR with the correct type. */
6885 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
6887 if (!mark_single_function (arg, complain))
6888 return error_mark_node;
6889 val = build_address (arg);
6890 if (TREE_CODE (arg) == OFFSET_REF)
6891 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
6893 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
6895 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
6897 /* We can only get here with a single static member
6898 function. */
6899 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6900 && DECL_STATIC_FUNCTION_P (fn));
6901 if (!mark_used (fn, complain) && !(complain & tf_error))
6902 return error_mark_node;
6903 val = build_address (fn);
6904 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
6905 /* Do not lose object's side effects. */
6906 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
6907 TREE_OPERAND (arg, 0), val);
6909 else
6911 tree object = TREE_OPERAND (arg, 0);
6912 tree field = TREE_OPERAND (arg, 1);
6913 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6914 (TREE_TYPE (object), decl_type_context (field)));
6915 val = build_address (arg);
6918 if (TYPE_PTR_P (argtype)
6919 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6921 build_ptrmemfunc_type (argtype);
6922 val = build_ptrmemfunc (argtype, val, 0,
6923 /*c_cast_p=*/false,
6924 complain);
6927 /* For addresses of immediate functions ensure we have EXPR_LOCATION
6928 set for possible later diagnostics. */
6929 if (TREE_CODE (val) == ADDR_EXPR
6930 && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL
6931 && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (val, 0)))
6932 SET_EXPR_LOCATION (val, input_location);
6934 return val;
6937 /* Take the address of ARG if it has one, even if it's an rvalue. */
6939 tree
6940 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6942 return cp_build_addr_expr_1 (arg, 0, complain);
6945 /* Take the address of ARG, but only if it's an lvalue. */
6947 static tree
6948 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6950 return cp_build_addr_expr_1 (arg, 1, complain);
6953 /* C++: Must handle pointers to members.
6955 Perhaps type instantiation should be extended to handle conversion
6956 from aggregates to types we don't yet know we want? (Or are those
6957 cases typically errors which should be reported?)
6959 NOCONVERT suppresses the default promotions (such as from short to int). */
6961 tree
6962 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6963 tsubst_flags_t complain)
6965 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6966 tree arg = xarg;
6967 location_t location = cp_expr_loc_or_input_loc (arg);
6968 tree argtype = 0;
6969 const char *errstring = NULL;
6970 tree val;
6971 const char *invalid_op_diag;
6973 if (!arg || error_operand_p (arg))
6974 return error_mark_node;
6976 arg = resolve_nondeduced_context (arg, complain);
6978 if ((invalid_op_diag
6979 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6980 ? CONVERT_EXPR
6981 : code),
6982 TREE_TYPE (arg))))
6984 if (complain & tf_error)
6985 error (invalid_op_diag);
6986 return error_mark_node;
6989 switch (code)
6991 case UNARY_PLUS_EXPR:
6992 case NEGATE_EXPR:
6994 int flags = WANT_ARITH | WANT_ENUM;
6995 /* Unary plus (but not unary minus) is allowed on pointers. */
6996 if (code == UNARY_PLUS_EXPR)
6997 flags |= WANT_POINTER;
6998 arg = build_expr_type_conversion (flags, arg, true);
6999 if (!arg)
7000 errstring = (code == NEGATE_EXPR
7001 ? _("wrong type argument to unary minus")
7002 : _("wrong type argument to unary plus"));
7003 else
7005 if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7006 arg = cp_perform_integral_promotions (arg, complain);
7008 /* Make sure the result is not an lvalue: a unary plus or minus
7009 expression is always a rvalue. */
7010 arg = rvalue (arg);
7013 break;
7015 case BIT_NOT_EXPR:
7016 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7018 code = CONJ_EXPR;
7019 if (!noconvert)
7021 arg = cp_default_conversion (arg, complain);
7022 if (arg == error_mark_node)
7023 return error_mark_node;
7026 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7027 | WANT_VECTOR_OR_COMPLEX,
7028 arg, true)))
7029 errstring = _("wrong type argument to bit-complement");
7030 else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7032 /* Warn if the expression has boolean value. */
7033 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7034 && (complain & tf_warning)
7035 && warning_at (location, OPT_Wbool_operation,
7036 "%<~%> on an expression of type %<bool%>"))
7037 inform (location, "did you mean to use logical not (%<!%>)?");
7038 arg = cp_perform_integral_promotions (arg, complain);
7040 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7041 arg = mark_rvalue_use (arg);
7042 break;
7044 case ABS_EXPR:
7045 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7046 errstring = _("wrong type argument to abs");
7047 else if (!noconvert)
7049 arg = cp_default_conversion (arg, complain);
7050 if (arg == error_mark_node)
7051 return error_mark_node;
7053 break;
7055 case CONJ_EXPR:
7056 /* Conjugating a real value is a no-op, but allow it anyway. */
7057 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7058 errstring = _("wrong type argument to conjugation");
7059 else if (!noconvert)
7061 arg = cp_default_conversion (arg, complain);
7062 if (arg == error_mark_node)
7063 return error_mark_node;
7065 break;
7067 case TRUTH_NOT_EXPR:
7068 if (gnu_vector_type_p (TREE_TYPE (arg)))
7069 return cp_build_binary_op (input_location, EQ_EXPR, arg,
7070 build_zero_cst (TREE_TYPE (arg)), complain);
7071 arg = perform_implicit_conversion (boolean_type_node, arg,
7072 complain);
7073 val = invert_truthvalue_loc (location, arg);
7074 if (arg != error_mark_node)
7075 return val;
7076 errstring = _("in argument to unary !");
7077 break;
7079 case NOP_EXPR:
7080 break;
7082 case REALPART_EXPR:
7083 case IMAGPART_EXPR:
7084 arg = build_real_imag_expr (input_location, code, arg);
7085 return arg;
7087 case PREINCREMENT_EXPR:
7088 case POSTINCREMENT_EXPR:
7089 case PREDECREMENT_EXPR:
7090 case POSTDECREMENT_EXPR:
7091 /* Handle complex lvalues (when permitted)
7092 by reduction to simpler cases. */
7094 val = unary_complex_lvalue (code, arg);
7095 if (val != 0)
7096 return val;
7098 arg = mark_lvalue_use (arg);
7100 /* Increment or decrement the real part of the value,
7101 and don't change the imaginary part. */
7102 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7104 tree real, imag;
7106 arg = cp_stabilize_reference (arg);
7107 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7108 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7109 real = cp_build_unary_op (code, real, true, complain);
7110 if (real == error_mark_node || imag == error_mark_node)
7111 return error_mark_node;
7112 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
7113 real, imag);
7116 /* Report invalid types. */
7118 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7119 arg, true)))
7121 if (code == PREINCREMENT_EXPR)
7122 errstring = _("no pre-increment operator for type");
7123 else if (code == POSTINCREMENT_EXPR)
7124 errstring = _("no post-increment operator for type");
7125 else if (code == PREDECREMENT_EXPR)
7126 errstring = _("no pre-decrement operator for type");
7127 else
7128 errstring = _("no post-decrement operator for type");
7129 break;
7131 else if (arg == error_mark_node)
7132 return error_mark_node;
7134 /* Report something read-only. */
7136 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7137 || TREE_READONLY (arg))
7139 if (complain & tf_error)
7140 cxx_readonly_error (location, arg,
7141 ((code == PREINCREMENT_EXPR
7142 || code == POSTINCREMENT_EXPR)
7143 ? lv_increment : lv_decrement));
7144 else
7145 return error_mark_node;
7149 tree inc;
7150 tree declared_type = unlowered_expr_type (arg);
7152 argtype = TREE_TYPE (arg);
7154 /* ARM $5.2.5 last annotation says this should be forbidden. */
7155 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7157 if (complain & tf_error)
7158 permerror (location, (code == PREINCREMENT_EXPR
7159 || code == POSTINCREMENT_EXPR)
7160 ? G_("ISO C++ forbids incrementing an enum")
7161 : G_("ISO C++ forbids decrementing an enum"));
7162 else
7163 return error_mark_node;
7166 /* Compute the increment. */
7168 if (TYPE_PTR_P (argtype))
7170 tree type = complete_type (TREE_TYPE (argtype));
7172 if (!COMPLETE_OR_VOID_TYPE_P (type))
7174 if (complain & tf_error)
7175 error_at (location, ((code == PREINCREMENT_EXPR
7176 || code == POSTINCREMENT_EXPR))
7177 ? G_("cannot increment a pointer to incomplete "
7178 "type %qT")
7179 : G_("cannot decrement a pointer to incomplete "
7180 "type %qT"),
7181 TREE_TYPE (argtype));
7182 else
7183 return error_mark_node;
7185 else if (!TYPE_PTROB_P (argtype))
7187 if (complain & tf_error)
7188 pedwarn (location, OPT_Wpointer_arith,
7189 (code == PREINCREMENT_EXPR
7190 || code == POSTINCREMENT_EXPR)
7191 ? G_("ISO C++ forbids incrementing a pointer "
7192 "of type %qT")
7193 : G_("ISO C++ forbids decrementing a pointer "
7194 "of type %qT"),
7195 argtype);
7196 else
7197 return error_mark_node;
7199 else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7200 TREE_TYPE (argtype),
7201 !(complain & tf_error)))
7202 return error_mark_node;
7204 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7206 else
7207 inc = VECTOR_TYPE_P (argtype)
7208 ? build_one_cst (argtype)
7209 : integer_one_node;
7211 inc = cp_convert (argtype, inc, complain);
7213 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7214 need to ask Objective-C to build the increment or decrement
7215 expression for it. */
7216 if (objc_is_property_ref (arg))
7217 return objc_build_incr_expr_for_property_ref (input_location, code,
7218 arg, inc);
7220 /* Complain about anything else that is not a true lvalue. */
7221 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7222 || code == POSTINCREMENT_EXPR)
7223 ? lv_increment : lv_decrement),
7224 complain))
7225 return error_mark_node;
7227 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7228 prefix ++ and -- expressions of volatile-qualified arithmetic
7229 and pointer types are deprecated." */
7230 if (TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7231 warning_at (location, OPT_Wvolatile,
7232 "%qs expression of %<volatile%>-qualified type is "
7233 "deprecated",
7234 ((code == PREINCREMENT_EXPR
7235 || code == POSTINCREMENT_EXPR)
7236 ? "++" : "--"));
7238 /* Forbid using -- or ++ in C++17 on `bool'. */
7239 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7241 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7243 if (complain & tf_error)
7244 error_at (location,
7245 "use of an operand of type %qT in %<operator--%> "
7246 "is forbidden", boolean_type_node);
7247 return error_mark_node;
7249 else
7251 if (cxx_dialect >= cxx17)
7253 if (complain & tf_error)
7254 error_at (location,
7255 "use of an operand of type %qT in "
7256 "%<operator++%> is forbidden in C++17",
7257 boolean_type_node);
7258 return error_mark_node;
7260 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7261 else
7262 warning_at (location, OPT_Wdeprecated,
7263 "use of an operand of type %qT "
7264 "in %<operator++%> is deprecated",
7265 boolean_type_node);
7267 val = boolean_increment (code, arg);
7269 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7270 /* An rvalue has no cv-qualifiers. */
7271 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7272 else
7273 val = build2 (code, TREE_TYPE (arg), arg, inc);
7275 TREE_SIDE_EFFECTS (val) = 1;
7276 return val;
7279 case ADDR_EXPR:
7280 /* Note that this operation never does default_conversion
7281 regardless of NOCONVERT. */
7282 return cp_build_addr_expr (arg, complain);
7284 default:
7285 break;
7288 if (!errstring)
7290 if (argtype == 0)
7291 argtype = TREE_TYPE (arg);
7292 return build1 (code, argtype, arg);
7295 if (complain & tf_error)
7296 error_at (location, "%s", errstring);
7297 return error_mark_node;
7300 /* Hook for the c-common bits that build a unary op. */
7301 tree
7302 build_unary_op (location_t /*location*/,
7303 enum tree_code code, tree xarg, bool noconvert)
7305 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7308 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7309 so that it is a valid lvalue even for GENERIC by replacing
7310 (lhs = rhs) with ((lhs = rhs), lhs)
7311 (--lhs) with ((--lhs), lhs)
7312 (++lhs) with ((++lhs), lhs)
7313 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7314 that it can be evaluated multiple times. */
7316 tree
7317 genericize_compound_lvalue (tree lvalue)
7319 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7320 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7321 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7322 TREE_OPERAND (lvalue, 1));
7323 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7324 lvalue, TREE_OPERAND (lvalue, 0));
7327 /* Apply unary lvalue-demanding operator CODE to the expression ARG
7328 for certain kinds of expressions which are not really lvalues
7329 but which we can accept as lvalues.
7331 If ARG is not a kind of expression we can handle, return
7332 NULL_TREE. */
7334 tree
7335 unary_complex_lvalue (enum tree_code code, tree arg)
7337 /* Inside a template, making these kinds of adjustments is
7338 pointless; we are only concerned with the type of the
7339 expression. */
7340 if (processing_template_decl)
7341 return NULL_TREE;
7343 /* Handle (a, b) used as an "lvalue". */
7344 if (TREE_CODE (arg) == COMPOUND_EXPR)
7346 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7347 tf_warning_or_error);
7348 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7349 TREE_OPERAND (arg, 0), real_result);
7352 /* Handle (a ? b : c) used as an "lvalue". */
7353 if (TREE_CODE (arg) == COND_EXPR
7354 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7355 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7357 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7358 if (TREE_CODE (arg) == MODIFY_EXPR
7359 || TREE_CODE (arg) == PREINCREMENT_EXPR
7360 || TREE_CODE (arg) == PREDECREMENT_EXPR)
7361 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7363 if (code != ADDR_EXPR)
7364 return NULL_TREE;
7366 /* Handle (a = b) used as an "lvalue" for `&'. */
7367 if (TREE_CODE (arg) == MODIFY_EXPR
7368 || TREE_CODE (arg) == INIT_EXPR)
7370 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7371 tf_warning_or_error);
7372 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7373 arg, real_result);
7374 suppress_warning (arg /* What warning? */);
7375 return arg;
7378 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7379 || TREE_CODE (arg) == OFFSET_REF)
7380 return NULL_TREE;
7382 /* We permit compiler to make function calls returning
7383 objects of aggregate type look like lvalues. */
7385 tree targ = arg;
7387 if (TREE_CODE (targ) == SAVE_EXPR)
7388 targ = TREE_OPERAND (targ, 0);
7390 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7392 if (TREE_CODE (arg) == SAVE_EXPR)
7393 targ = arg;
7394 else
7395 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7396 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7399 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
7400 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
7401 TREE_OPERAND (targ, 0), current_function_decl, NULL);
7404 /* Don't let anything else be handled specially. */
7405 return NULL_TREE;
7408 /* Mark EXP saying that we need to be able to take the
7409 address of it; it should not be allocated in a register.
7410 Value is true if successful. ARRAY_REF_P is true if this
7411 is for ARRAY_REF construction - in that case we don't want
7412 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7413 it is fine to use ARRAY_REFs for vector subscripts on vector
7414 register variables.
7416 C++: we do not allow `current_class_ptr' to be addressable. */
7418 bool
7419 cxx_mark_addressable (tree exp, bool array_ref_p)
7421 tree x = exp;
7423 while (1)
7424 switch (TREE_CODE (x))
7426 case VIEW_CONVERT_EXPR:
7427 if (array_ref_p
7428 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7429 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
7430 return true;
7431 x = TREE_OPERAND (x, 0);
7432 break;
7434 case COMPONENT_REF:
7435 if (bitfield_p (x))
7436 error ("attempt to take address of bit-field");
7437 /* FALLTHRU */
7438 case ADDR_EXPR:
7439 case ARRAY_REF:
7440 case REALPART_EXPR:
7441 case IMAGPART_EXPR:
7442 x = TREE_OPERAND (x, 0);
7443 break;
7445 case PARM_DECL:
7446 if (x == current_class_ptr)
7448 error ("cannot take the address of %<this%>, which is an rvalue expression");
7449 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
7450 return true;
7452 /* Fall through. */
7454 case VAR_DECL:
7455 /* Caller should not be trying to mark initialized
7456 constant fields addressable. */
7457 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
7458 || DECL_IN_AGGR_P (x) == 0
7459 || TREE_STATIC (x)
7460 || DECL_EXTERNAL (x));
7461 /* Fall through. */
7463 case RESULT_DECL:
7464 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
7465 && !DECL_ARTIFICIAL (x))
7467 if (VAR_P (x) && DECL_HARD_REGISTER (x))
7469 error
7470 ("address of explicit register variable %qD requested", x);
7471 return false;
7473 else if (extra_warnings)
7474 warning
7475 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
7477 TREE_ADDRESSABLE (x) = 1;
7478 return true;
7480 case CONST_DECL:
7481 case FUNCTION_DECL:
7482 TREE_ADDRESSABLE (x) = 1;
7483 return true;
7485 case CONSTRUCTOR:
7486 TREE_ADDRESSABLE (x) = 1;
7487 return true;
7489 case TARGET_EXPR:
7490 TREE_ADDRESSABLE (x) = 1;
7491 cxx_mark_addressable (TREE_OPERAND (x, 0));
7492 return true;
7494 default:
7495 return true;
7499 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7501 tree
7502 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
7503 tsubst_flags_t complain)
7505 tree orig_ifexp = ifexp;
7506 tree orig_op1 = op1;
7507 tree orig_op2 = op2;
7508 tree expr;
7510 if (processing_template_decl)
7512 /* The standard says that the expression is type-dependent if
7513 IFEXP is type-dependent, even though the eventual type of the
7514 expression doesn't dependent on IFEXP. */
7515 if (type_dependent_expression_p (ifexp)
7516 /* As a GNU extension, the middle operand may be omitted. */
7517 || (op1 && type_dependent_expression_p (op1))
7518 || type_dependent_expression_p (op2))
7519 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
7520 ifexp = build_non_dependent_expr (ifexp);
7521 if (op1)
7522 op1 = build_non_dependent_expr (op1);
7523 op2 = build_non_dependent_expr (op2);
7526 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
7527 if (processing_template_decl && expr != error_mark_node)
7529 tree min = build_min_non_dep (COND_EXPR, expr,
7530 orig_ifexp, orig_op1, orig_op2);
7531 expr = convert_from_reference (min);
7533 return expr;
7536 /* Given a list of expressions, return a compound expression
7537 that performs them all and returns the value of the last of them. */
7539 tree
7540 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
7541 tsubst_flags_t complain)
7543 tree expr = TREE_VALUE (list);
7545 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
7546 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
7548 if (complain & tf_error)
7549 pedwarn (cp_expr_loc_or_input_loc (expr), 0,
7550 "list-initializer for non-class type must not "
7551 "be parenthesized");
7552 else
7553 return error_mark_node;
7556 if (TREE_CHAIN (list))
7558 if (complain & tf_error)
7559 switch (exp)
7561 case ELK_INIT:
7562 permerror (input_location, "expression list treated as compound "
7563 "expression in initializer");
7564 break;
7565 case ELK_MEM_INIT:
7566 permerror (input_location, "expression list treated as compound "
7567 "expression in mem-initializer");
7568 break;
7569 case ELK_FUNC_CAST:
7570 permerror (input_location, "expression list treated as compound "
7571 "expression in functional cast");
7572 break;
7573 default:
7574 gcc_unreachable ();
7576 else
7577 return error_mark_node;
7579 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
7580 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
7581 expr, TREE_VALUE (list), NULL_TREE,
7582 complain);
7585 return expr;
7588 /* Like build_x_compound_expr_from_list, but using a VEC. */
7590 tree
7591 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
7592 tsubst_flags_t complain)
7594 if (vec_safe_is_empty (vec))
7595 return NULL_TREE;
7596 else if (vec->length () == 1)
7597 return (*vec)[0];
7598 else
7600 tree expr;
7601 unsigned int ix;
7602 tree t;
7604 if (msg != NULL)
7606 if (complain & tf_error)
7607 permerror (input_location,
7608 "%s expression list treated as compound expression",
7609 msg);
7610 else
7611 return error_mark_node;
7614 expr = (*vec)[0];
7615 for (ix = 1; vec->iterate (ix, &t); ++ix)
7616 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
7617 t, NULL_TREE, complain);
7619 return expr;
7623 /* Handle overloading of the ',' operator when needed. */
7625 tree
7626 build_x_compound_expr (location_t loc, tree op1, tree op2,
7627 tree lookups, tsubst_flags_t complain)
7629 tree result;
7630 tree orig_op1 = op1;
7631 tree orig_op2 = op2;
7632 tree overload = NULL_TREE;
7634 if (processing_template_decl)
7636 if (type_dependent_expression_p (op1)
7637 || type_dependent_expression_p (op2))
7639 result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
7640 TREE_TYPE (result)
7641 = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
7642 return result;
7644 op1 = build_non_dependent_expr (op1);
7645 op2 = build_non_dependent_expr (op2);
7648 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
7649 NULL_TREE, lookups, &overload, complain);
7650 if (!result)
7651 result = cp_build_compound_expr (op1, op2, complain);
7653 if (processing_template_decl && result != error_mark_node)
7655 if (overload != NULL_TREE)
7656 return (build_min_non_dep_op_overload
7657 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
7659 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
7662 return result;
7665 /* Like cp_build_compound_expr, but for the c-common bits. */
7667 tree
7668 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
7670 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
7673 /* Build a compound expression. */
7675 tree
7676 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
7678 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
7680 if (lhs == error_mark_node || rhs == error_mark_node)
7681 return error_mark_node;
7683 if (TREE_CODE (rhs) == TARGET_EXPR)
7685 /* If the rhs is a TARGET_EXPR, then build the compound
7686 expression inside the target_expr's initializer. This
7687 helps the compiler to eliminate unnecessary temporaries. */
7688 tree init = TREE_OPERAND (rhs, 1);
7690 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
7691 TREE_OPERAND (rhs, 1) = init;
7693 return rhs;
7696 if (type_unknown_p (rhs))
7698 if (complain & tf_error)
7699 error_at (cp_expr_loc_or_input_loc (rhs),
7700 "no context to resolve type of %qE", rhs);
7701 return error_mark_node;
7704 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
7707 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
7708 casts away constness. CAST gives the type of cast. Returns true
7709 if the cast is ill-formed, false if it is well-formed.
7711 ??? This function warns for casting away any qualifier not just
7712 const. We would like to specify exactly what qualifiers are casted
7713 away.
7716 static bool
7717 check_for_casting_away_constness (location_t loc, tree src_type,
7718 tree dest_type, enum tree_code cast,
7719 tsubst_flags_t complain)
7721 /* C-style casts are allowed to cast away constness. With
7722 WARN_CAST_QUAL, we still want to issue a warning. */
7723 if (cast == CAST_EXPR && !warn_cast_qual)
7724 return false;
7726 if (!casts_away_constness (src_type, dest_type, complain))
7727 return false;
7729 switch (cast)
7731 case CAST_EXPR:
7732 if (complain & tf_warning)
7733 warning_at (loc, OPT_Wcast_qual,
7734 "cast from type %qT to type %qT casts away qualifiers",
7735 src_type, dest_type);
7736 return false;
7738 case STATIC_CAST_EXPR:
7739 if (complain & tf_error)
7740 error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
7741 "away qualifiers",
7742 src_type, dest_type);
7743 return true;
7745 case REINTERPRET_CAST_EXPR:
7746 if (complain & tf_error)
7747 error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
7748 "casts away qualifiers",
7749 src_type, dest_type);
7750 return true;
7752 default:
7753 gcc_unreachable();
7757 /* Warns if the cast from expression EXPR to type TYPE is useless. */
7758 void
7759 maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
7760 tsubst_flags_t complain)
7762 if (warn_useless_cast
7763 && complain & tf_warning)
7765 if ((TYPE_REF_P (type)
7766 && (TYPE_REF_IS_RVALUE (type)
7767 ? xvalue_p (expr) : lvalue_p (expr))
7768 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
7769 || same_type_p (TREE_TYPE (expr), type))
7770 warning_at (loc, OPT_Wuseless_cast,
7771 "useless cast to type %q#T", type);
7775 /* Warns if the cast ignores cv-qualifiers on TYPE. */
7776 static void
7777 maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
7778 tsubst_flags_t complain)
7780 if (warn_ignored_qualifiers
7781 && complain & tf_warning
7782 && !CLASS_TYPE_P (type)
7783 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
7784 warning_at (loc, OPT_Wignored_qualifiers,
7785 "type qualifiers ignored on cast result type");
7788 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
7789 (another pointer-to-member type in the same hierarchy) and return
7790 the converted expression. If ALLOW_INVERSE_P is permitted, a
7791 pointer-to-derived may be converted to pointer-to-base; otherwise,
7792 only the other direction is permitted. If C_CAST_P is true, this
7793 conversion is taking place as part of a C-style cast. */
7795 tree
7796 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
7797 bool c_cast_p, tsubst_flags_t complain)
7799 if (same_type_p (type, TREE_TYPE (expr)))
7800 return expr;
7802 if (TYPE_PTRDATAMEM_P (type))
7804 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
7805 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
7806 tree delta = (get_delta_difference
7807 (obase, nbase,
7808 allow_inverse_p, c_cast_p, complain));
7810 if (delta == error_mark_node)
7811 return error_mark_node;
7813 if (!same_type_p (obase, nbase))
7815 if (TREE_CODE (expr) == PTRMEM_CST)
7816 expr = cplus_expand_constant (expr);
7818 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
7819 build_int_cst (TREE_TYPE (expr), -1),
7820 complain);
7821 tree op1 = build_nop (ptrdiff_type_node, expr);
7822 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
7823 complain);
7825 expr = fold_build3_loc (input_location,
7826 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
7829 return build_nop (type, expr);
7831 else
7832 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
7833 allow_inverse_p, c_cast_p, complain);
7836 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
7837 this static_cast is being attempted as one of the possible casts
7838 allowed by a C-style cast. (In that case, accessibility of base
7839 classes is not considered, and it is OK to cast away
7840 constness.) Return the result of the cast. *VALID_P is set to
7841 indicate whether or not the cast was valid. */
7843 static tree
7844 build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
7845 bool *valid_p, tsubst_flags_t complain)
7847 tree intype;
7848 tree result;
7849 cp_lvalue_kind clk;
7851 /* Assume the cast is valid. */
7852 *valid_p = true;
7854 intype = unlowered_expr_type (expr);
7856 /* Save casted types in the function's used types hash table. */
7857 used_types_insert (type);
7859 /* A prvalue of non-class type is cv-unqualified. */
7860 if (!CLASS_TYPE_P (type))
7861 type = cv_unqualified (type);
7863 /* [expr.static.cast]
7865 An lvalue of type "cv1 B", where B is a class type, can be cast
7866 to type "reference to cv2 D", where D is a class derived (clause
7867 _class.derived_) from B, if a valid standard conversion from
7868 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7869 same cv-qualification as, or greater cv-qualification than, cv1,
7870 and B is not a virtual base class of D. */
7871 /* We check this case before checking the validity of "TYPE t =
7872 EXPR;" below because for this case:
7874 struct B {};
7875 struct D : public B { D(const B&); };
7876 extern B& b;
7877 void f() { static_cast<const D&>(b); }
7879 we want to avoid constructing a new D. The standard is not
7880 completely clear about this issue, but our interpretation is
7881 consistent with other compilers. */
7882 if (TYPE_REF_P (type)
7883 && CLASS_TYPE_P (TREE_TYPE (type))
7884 && CLASS_TYPE_P (intype)
7885 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
7886 && DERIVED_FROM_P (intype, TREE_TYPE (type))
7887 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
7888 build_pointer_type (TYPE_MAIN_VARIANT
7889 (TREE_TYPE (type))),
7890 complain)
7891 && (c_cast_p
7892 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7894 tree base;
7896 if (processing_template_decl)
7897 return expr;
7899 /* There is a standard conversion from "D*" to "B*" even if "B"
7900 is ambiguous or inaccessible. If this is really a
7901 static_cast, then we check both for inaccessibility and
7902 ambiguity. However, if this is a static_cast being performed
7903 because the user wrote a C-style cast, then accessibility is
7904 not considered. */
7905 base = lookup_base (TREE_TYPE (type), intype,
7906 c_cast_p ? ba_unique : ba_check,
7907 NULL, complain);
7908 expr = cp_build_addr_expr (expr, complain);
7910 if (sanitize_flags_p (SANITIZE_VPTR))
7912 tree ubsan_check
7913 = cp_ubsan_maybe_instrument_downcast (loc, type,
7914 intype, expr);
7915 if (ubsan_check)
7916 expr = ubsan_check;
7919 /* Convert from "B*" to "D*". This function will check that "B"
7920 is not a virtual base of "D". Even if we don't have a guarantee
7921 that expr is NULL, if the static_cast is to a reference type,
7922 it is UB if it would be NULL, so omit the non-NULL check. */
7923 expr = build_base_path (MINUS_EXPR, expr, base,
7924 /*nonnull=*/flag_delete_null_pointer_checks,
7925 complain);
7927 /* Convert the pointer to a reference -- but then remember that
7928 there are no expressions with reference type in C++.
7930 We call rvalue so that there's an actual tree code
7931 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7932 is a variable with the same type, the conversion would get folded
7933 away, leaving just the variable and causing lvalue_kind to give
7934 the wrong answer. */
7935 expr = cp_fold_convert (type, expr);
7937 /* When -fsanitize=null, make sure to diagnose reference binding to
7938 NULL even when the reference is converted to pointer later on. */
7939 if (sanitize_flags_p (SANITIZE_NULL)
7940 && TREE_CODE (expr) == COND_EXPR
7941 && TREE_OPERAND (expr, 2)
7942 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
7943 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
7944 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
7946 return convert_from_reference (rvalue (expr));
7949 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7950 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7951 if (TYPE_REF_P (type)
7952 && TYPE_REF_IS_RVALUE (type)
7953 && (clk = real_lvalue_p (expr))
7954 && reference_compatible_p (TREE_TYPE (type), intype)
7955 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7957 if (processing_template_decl)
7958 return expr;
7959 if (clk == clk_ordinary)
7961 /* Handle the (non-bit-field) lvalue case here by casting to
7962 lvalue reference and then changing it to an rvalue reference.
7963 Casting an xvalue to rvalue reference will be handled by the
7964 main code path. */
7965 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7966 result = (perform_direct_initialization_if_possible
7967 (lref, expr, c_cast_p, complain));
7968 result = build1 (NON_LVALUE_EXPR, type, result);
7969 return convert_from_reference (result);
7971 else
7972 /* For a bit-field or packed field, bind to a temporary. */
7973 expr = rvalue (expr);
7976 /* Resolve overloaded address here rather than once in
7977 implicit_conversion and again in the inverse code below. */
7978 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7980 expr = instantiate_type (type, expr, complain);
7981 intype = TREE_TYPE (expr);
7984 /* [expr.static.cast]
7986 Any expression can be explicitly converted to type cv void. */
7987 if (VOID_TYPE_P (type))
7988 return convert_to_void (expr, ICV_CAST, complain);
7990 /* [class.abstract]
7991 An abstract class shall not be used ... as the type of an explicit
7992 conversion. */
7993 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7994 return error_mark_node;
7996 /* [expr.static.cast]
7998 An expression e can be explicitly converted to a type T using a
7999 static_cast of the form static_cast<T>(e) if the declaration T
8000 t(e);" is well-formed, for some invented temporary variable
8001 t. */
8002 result = perform_direct_initialization_if_possible (type, expr,
8003 c_cast_p, complain);
8004 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8005 which initialize the first element of the aggregate. We need to handle
8006 the array case specifically. */
8007 if (result == NULL_TREE
8008 && cxx_dialect >= cxx20
8009 && TREE_CODE (type) == ARRAY_TYPE)
8011 /* Create { EXPR } and perform direct-initialization from it. */
8012 tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8013 CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8014 CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8015 result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8016 complain);
8018 if (result)
8020 if (processing_template_decl)
8021 return expr;
8023 result = convert_from_reference (result);
8025 /* [expr.static.cast]
8027 If T is a reference type, the result is an lvalue; otherwise,
8028 the result is an rvalue. */
8029 if (!TYPE_REF_P (type))
8031 result = rvalue (result);
8033 if (result == expr && SCALAR_TYPE_P (type))
8034 /* Leave some record of the cast. */
8035 result = build_nop (type, expr);
8037 return result;
8040 /* [expr.static.cast]
8042 The inverse of any standard conversion sequence (clause _conv_),
8043 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8044 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8045 (_conv.bool_) conversions, can be performed explicitly using
8046 static_cast subject to the restriction that the explicit
8047 conversion does not cast away constness (_expr.const.cast_), and
8048 the following additional rules for specific cases: */
8049 /* For reference, the conversions not excluded are: integral
8050 promotions, floating-point promotion, integral conversions,
8051 floating-point conversions, floating-integral conversions,
8052 pointer conversions, and pointer to member conversions. */
8053 /* DR 128
8055 A value of integral _or enumeration_ type can be explicitly
8056 converted to an enumeration type. */
8057 /* The effect of all that is that any conversion between any two
8058 types which are integral, floating, or enumeration types can be
8059 performed. */
8060 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8061 || SCALAR_FLOAT_TYPE_P (type))
8062 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8063 || SCALAR_FLOAT_TYPE_P (intype)))
8065 if (processing_template_decl)
8066 return expr;
8067 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8070 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8071 && CLASS_TYPE_P (TREE_TYPE (type))
8072 && CLASS_TYPE_P (TREE_TYPE (intype))
8073 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8074 (TREE_TYPE (intype))),
8075 build_pointer_type (TYPE_MAIN_VARIANT
8076 (TREE_TYPE (type))),
8077 complain))
8079 tree base;
8081 if (processing_template_decl)
8082 return expr;
8084 if (!c_cast_p
8085 && check_for_casting_away_constness (loc, intype, type,
8086 STATIC_CAST_EXPR,
8087 complain))
8088 return error_mark_node;
8089 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8090 c_cast_p ? ba_unique : ba_check,
8091 NULL, complain);
8092 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8093 complain);
8095 if (sanitize_flags_p (SANITIZE_VPTR))
8097 tree ubsan_check
8098 = cp_ubsan_maybe_instrument_downcast (loc, type,
8099 intype, expr);
8100 if (ubsan_check)
8101 expr = ubsan_check;
8104 return cp_fold_convert (type, expr);
8107 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8108 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8110 tree c1;
8111 tree c2;
8112 tree t1;
8113 tree t2;
8115 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8116 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8118 if (TYPE_PTRDATAMEM_P (type))
8120 t1 = (build_ptrmem_type
8121 (c1,
8122 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8123 t2 = (build_ptrmem_type
8124 (c2,
8125 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8127 else
8129 t1 = intype;
8130 t2 = type;
8132 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8134 if (!c_cast_p
8135 && check_for_casting_away_constness (loc, intype, type,
8136 STATIC_CAST_EXPR,
8137 complain))
8138 return error_mark_node;
8139 if (processing_template_decl)
8140 return expr;
8141 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8142 c_cast_p, complain);
8146 /* [expr.static.cast]
8148 An rvalue of type "pointer to cv void" can be explicitly
8149 converted to a pointer to object type. A value of type pointer
8150 to object converted to "pointer to cv void" and back to the
8151 original pointer type will have its original value. */
8152 if (TYPE_PTR_P (intype)
8153 && VOID_TYPE_P (TREE_TYPE (intype))
8154 && TYPE_PTROB_P (type))
8156 if (!c_cast_p
8157 && check_for_casting_away_constness (loc, intype, type,
8158 STATIC_CAST_EXPR,
8159 complain))
8160 return error_mark_node;
8161 if (processing_template_decl)
8162 return expr;
8163 return build_nop (type, expr);
8166 *valid_p = false;
8167 return error_mark_node;
8170 /* Return an expression representing static_cast<TYPE>(EXPR). */
8172 tree
8173 build_static_cast (location_t loc, tree type, tree oexpr,
8174 tsubst_flags_t complain)
8176 tree expr = oexpr;
8177 tree result;
8178 bool valid_p;
8180 if (type == error_mark_node || expr == error_mark_node)
8181 return error_mark_node;
8183 bool dependent = (dependent_type_p (type)
8184 || type_dependent_expression_p (expr));
8185 if (dependent)
8187 tmpl:
8188 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8189 /* We don't know if it will or will not have side effects. */
8190 TREE_SIDE_EFFECTS (expr) = 1;
8191 result = convert_from_reference (expr);
8192 protected_set_expr_location (result, loc);
8193 return result;
8195 else if (processing_template_decl)
8196 expr = build_non_dependent_expr (expr);
8198 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8199 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8200 if (!TYPE_REF_P (type)
8201 && TREE_CODE (expr) == NOP_EXPR
8202 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8203 expr = TREE_OPERAND (expr, 0);
8205 result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8206 &valid_p, complain);
8207 if (valid_p)
8209 if (result != error_mark_node)
8211 maybe_warn_about_useless_cast (loc, type, expr, complain);
8212 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8214 if (processing_template_decl)
8215 goto tmpl;
8216 protected_set_expr_location (result, loc);
8217 return result;
8220 if (complain & tf_error)
8222 error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8223 TREE_TYPE (expr), type);
8224 if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8225 && CLASS_TYPE_P (TREE_TYPE (type))
8226 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8227 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8228 "class type %qT is incomplete", TREE_TYPE (type));
8229 tree expr_type = TREE_TYPE (expr);
8230 if (TYPE_PTR_P (expr_type))
8231 expr_type = TREE_TYPE (expr_type);
8232 if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8233 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8234 "class type %qT is incomplete", expr_type);
8236 return error_mark_node;
8239 /* EXPR is an expression with member function or pointer-to-member
8240 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8241 not permitted by ISO C++, but we accept it in some modes. If we
8242 are not in one of those modes, issue a diagnostic. Return the
8243 converted expression. */
8245 tree
8246 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8248 tree intype;
8249 tree decl;
8251 intype = TREE_TYPE (expr);
8252 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8253 || TREE_CODE (intype) == METHOD_TYPE);
8255 if (!(complain & tf_warning_or_error))
8256 return error_mark_node;
8258 location_t loc = cp_expr_loc_or_input_loc (expr);
8260 if (pedantic || warn_pmf2ptr)
8261 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8262 "converting from %qH to %qI", intype, type);
8264 STRIP_ANY_LOCATION_WRAPPER (expr);
8266 if (TREE_CODE (intype) == METHOD_TYPE)
8267 expr = build_addr_func (expr, complain);
8268 else if (TREE_CODE (expr) == PTRMEM_CST)
8269 expr = build_address (PTRMEM_CST_MEMBER (expr));
8270 else
8272 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8273 decl = build_address (decl);
8274 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8277 if (expr == error_mark_node)
8278 return error_mark_node;
8280 expr = build_nop (type, expr);
8281 SET_EXPR_LOCATION (expr, loc);
8282 return expr;
8285 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8286 constexpr evaluation knows to reject it. */
8288 static tree
8289 build_nop_reinterpret (tree type, tree expr)
8291 tree ret = build_nop (type, expr);
8292 if (ret != expr)
8293 REINTERPRET_CAST_P (ret) = true;
8294 return ret;
8297 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8298 If C_CAST_P is true, this reinterpret cast is being done as part of
8299 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8300 indicate whether or not reinterpret_cast was valid. */
8302 static tree
8303 build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8304 bool c_cast_p, bool *valid_p,
8305 tsubst_flags_t complain)
8307 tree intype;
8309 /* Assume the cast is invalid. */
8310 if (valid_p)
8311 *valid_p = true;
8313 if (type == error_mark_node || error_operand_p (expr))
8314 return error_mark_node;
8316 intype = TREE_TYPE (expr);
8318 /* Save casted types in the function's used types hash table. */
8319 used_types_insert (type);
8321 /* A prvalue of non-class type is cv-unqualified. */
8322 if (!CLASS_TYPE_P (type))
8323 type = cv_unqualified (type);
8325 /* [expr.reinterpret.cast]
8326 A glvalue of type T1, designating an object x, can be cast to the type
8327 "reference to T2" if an expression of type "pointer to T1" can be
8328 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8329 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8330 of type "pointer to T1". No temporary is created, no copy is made, and no
8331 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8332 if (TYPE_REF_P (type))
8334 if (!glvalue_p (expr))
8336 if (complain & tf_error)
8337 error_at (loc, "invalid cast of a prvalue expression of type "
8338 "%qT to type %qT",
8339 intype, type);
8340 return error_mark_node;
8343 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8344 "B" are related class types; the reinterpret_cast does not
8345 adjust the pointer. */
8346 if (TYPE_PTR_P (intype)
8347 && (complain & tf_warning)
8348 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8349 COMPARE_BASE | COMPARE_DERIVED)))
8350 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8351 intype, type);
8353 expr = cp_build_addr_expr (expr, complain);
8355 if (warn_strict_aliasing > 2)
8356 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8358 if (expr != error_mark_node)
8359 expr = build_reinterpret_cast_1
8360 (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8361 valid_p, complain);
8362 if (expr != error_mark_node)
8363 /* cp_build_indirect_ref isn't right for rvalue refs. */
8364 expr = convert_from_reference (fold_convert (type, expr));
8365 return expr;
8368 /* As a G++ extension, we consider conversions from member
8369 functions, and pointers to member functions to
8370 pointer-to-function and pointer-to-void types. If
8371 -Wno-pmf-conversions has not been specified,
8372 convert_member_func_to_ptr will issue an error message. */
8373 if ((TYPE_PTRMEMFUNC_P (intype)
8374 || TREE_CODE (intype) == METHOD_TYPE)
8375 && TYPE_PTR_P (type)
8376 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
8377 || VOID_TYPE_P (TREE_TYPE (type))))
8378 return convert_member_func_to_ptr (type, expr, complain);
8380 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8381 array-to-pointer, and function-to-pointer conversions are
8382 performed. */
8383 expr = decay_conversion (expr, complain);
8385 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8386 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8387 if (TREE_CODE (expr) == NOP_EXPR
8388 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8389 expr = TREE_OPERAND (expr, 0);
8391 if (error_operand_p (expr))
8392 return error_mark_node;
8394 intype = TREE_TYPE (expr);
8396 /* [expr.reinterpret.cast]
8397 A pointer can be converted to any integral type large enough to
8398 hold it. ... A value of type std::nullptr_t can be converted to
8399 an integral type; the conversion has the same meaning and
8400 validity as a conversion of (void*)0 to the integral type. */
8401 if (CP_INTEGRAL_TYPE_P (type)
8402 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8404 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
8406 if (complain & tf_error)
8407 permerror (loc, "cast from %qH to %qI loses precision",
8408 intype, type);
8409 else
8410 return error_mark_node;
8412 if (NULLPTR_TYPE_P (intype))
8413 return build_int_cst (type, 0);
8415 /* [expr.reinterpret.cast]
8416 A value of integral or enumeration type can be explicitly
8417 converted to a pointer. */
8418 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
8419 /* OK */
8421 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8422 || TYPE_PTR_OR_PTRMEM_P (type))
8423 && same_type_p (type, intype))
8424 /* DR 799 */
8425 return rvalue (expr);
8426 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
8428 if ((complain & tf_warning)
8429 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
8430 TREE_TYPE (intype)))
8431 warning_at (loc, OPT_Wcast_function_type,
8432 "cast between incompatible function types"
8433 " from %qH to %qI", intype, type);
8434 return build_nop_reinterpret (type, expr);
8436 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
8438 if ((complain & tf_warning)
8439 && !cxx_safe_function_type_cast_p
8440 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
8441 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
8442 warning_at (loc, OPT_Wcast_function_type,
8443 "cast between incompatible pointer to member types"
8444 " from %qH to %qI", intype, type);
8445 return build_nop_reinterpret (type, expr);
8447 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8448 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
8450 if (!c_cast_p
8451 && check_for_casting_away_constness (loc, intype, type,
8452 REINTERPRET_CAST_EXPR,
8453 complain))
8454 return error_mark_node;
8455 /* Warn about possible alignment problems. */
8456 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8457 && (complain & tf_warning)
8458 && !VOID_TYPE_P (type)
8459 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
8460 && COMPLETE_TYPE_P (TREE_TYPE (type))
8461 && COMPLETE_TYPE_P (TREE_TYPE (intype))
8462 && min_align_of_type (TREE_TYPE (type))
8463 > min_align_of_type (TREE_TYPE (intype)))
8464 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8465 "increases required alignment of target type",
8466 intype, type);
8468 if (warn_strict_aliasing <= 2)
8469 /* strict_aliasing_warning STRIP_NOPs its expr. */
8470 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8472 return build_nop_reinterpret (type, expr);
8474 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
8475 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
8477 if (complain & tf_warning)
8478 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8479 object pointer type or vice versa is conditionally-supported." */
8480 warning_at (loc, OPT_Wconditionally_supported,
8481 "casting between pointer-to-function and "
8482 "pointer-to-object is conditionally-supported");
8483 return build_nop_reinterpret (type, expr);
8485 else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
8486 return convert_to_vector (type, rvalue (expr));
8487 else if (gnu_vector_type_p (intype)
8488 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8489 return convert_to_integer_nofold (type, expr);
8490 else
8492 if (valid_p)
8493 *valid_p = false;
8494 if (complain & tf_error)
8495 error_at (loc, "invalid cast from type %qT to type %qT",
8496 intype, type);
8497 return error_mark_node;
8500 expr = cp_convert (type, expr, complain);
8501 if (TREE_CODE (expr) == NOP_EXPR)
8502 /* Mark any nop_expr that created as a reintepret_cast. */
8503 REINTERPRET_CAST_P (expr) = true;
8504 return expr;
8507 tree
8508 build_reinterpret_cast (location_t loc, tree type, tree expr,
8509 tsubst_flags_t complain)
8511 tree r;
8513 if (type == error_mark_node || expr == error_mark_node)
8514 return error_mark_node;
8516 if (processing_template_decl)
8518 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
8520 if (!TREE_SIDE_EFFECTS (t)
8521 && type_dependent_expression_p (expr))
8522 /* There might turn out to be side effects inside expr. */
8523 TREE_SIDE_EFFECTS (t) = 1;
8524 r = convert_from_reference (t);
8525 protected_set_expr_location (r, loc);
8526 return r;
8529 r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8530 /*valid_p=*/NULL, complain);
8531 if (r != error_mark_node)
8533 maybe_warn_about_useless_cast (loc, type, expr, complain);
8534 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8536 protected_set_expr_location (r, loc);
8537 return r;
8540 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
8541 return an appropriate expression. Otherwise, return
8542 error_mark_node. If the cast is not valid, and COMPLAIN is true,
8543 then a diagnostic will be issued. If VALID_P is non-NULL, we are
8544 performing a C-style cast, its value upon return will indicate
8545 whether or not the conversion succeeded. */
8547 static tree
8548 build_const_cast_1 (location_t loc, tree dst_type, tree expr,
8549 tsubst_flags_t complain, bool *valid_p)
8551 tree src_type;
8552 tree reference_type;
8554 /* Callers are responsible for handling error_mark_node as a
8555 destination type. */
8556 gcc_assert (dst_type != error_mark_node);
8557 /* In a template, callers should be building syntactic
8558 representations of casts, not using this machinery. */
8559 gcc_assert (!processing_template_decl);
8561 /* Assume the conversion is invalid. */
8562 if (valid_p)
8563 *valid_p = false;
8565 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
8567 if (complain & tf_error)
8568 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8569 "which is not a pointer, reference, "
8570 "nor a pointer-to-data-member type", dst_type);
8571 return error_mark_node;
8574 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
8576 if (complain & tf_error)
8577 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
8578 "which is a pointer or reference to a function type",
8579 dst_type);
8580 return error_mark_node;
8583 /* A prvalue of non-class type is cv-unqualified. */
8584 dst_type = cv_unqualified (dst_type);
8586 /* Save casted types in the function's used types hash table. */
8587 used_types_insert (dst_type);
8589 src_type = TREE_TYPE (expr);
8590 /* Expressions do not really have reference types. */
8591 if (TYPE_REF_P (src_type))
8592 src_type = TREE_TYPE (src_type);
8594 /* [expr.const.cast]
8596 For two object types T1 and T2, if a pointer to T1 can be explicitly
8597 converted to the type "pointer to T2" using a const_cast, then the
8598 following conversions can also be made:
8600 -- an lvalue of type T1 can be explicitly converted to an lvalue of
8601 type T2 using the cast const_cast<T2&>;
8603 -- a glvalue of type T1 can be explicitly converted to an xvalue of
8604 type T2 using the cast const_cast<T2&&>; and
8606 -- if T1 is a class type, a prvalue of type T1 can be explicitly
8607 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
8609 if (TYPE_REF_P (dst_type))
8611 reference_type = dst_type;
8612 if (!TYPE_REF_IS_RVALUE (dst_type)
8613 ? lvalue_p (expr)
8614 : obvalue_p (expr))
8615 /* OK. */;
8616 else
8618 if (complain & tf_error)
8619 error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
8620 "to type %qT",
8621 src_type, dst_type);
8622 return error_mark_node;
8624 dst_type = build_pointer_type (TREE_TYPE (dst_type));
8625 src_type = build_pointer_type (src_type);
8627 else
8629 reference_type = NULL_TREE;
8630 /* If the destination type is not a reference type, the
8631 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8632 conversions are performed. */
8633 src_type = type_decays_to (src_type);
8634 if (src_type == error_mark_node)
8635 return error_mark_node;
8638 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
8640 if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
8642 if (valid_p)
8644 *valid_p = true;
8645 /* This cast is actually a C-style cast. Issue a warning if
8646 the user is making a potentially unsafe cast. */
8647 check_for_casting_away_constness (loc, src_type, dst_type,
8648 CAST_EXPR, complain);
8649 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
8650 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8651 && (complain & tf_warning)
8652 && min_align_of_type (TREE_TYPE (dst_type))
8653 > min_align_of_type (TREE_TYPE (src_type)))
8654 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8655 "increases required alignment of target type",
8656 src_type, dst_type);
8658 if (reference_type)
8660 expr = cp_build_addr_expr (expr, complain);
8661 if (expr == error_mark_node)
8662 return error_mark_node;
8663 expr = build_nop (reference_type, expr);
8664 return convert_from_reference (expr);
8666 else
8668 expr = decay_conversion (expr, complain);
8669 if (expr == error_mark_node)
8670 return error_mark_node;
8672 /* build_c_cast puts on a NOP_EXPR to make the result not an
8673 lvalue. Strip such NOP_EXPRs if VALUE is being used in
8674 non-lvalue context. */
8675 if (TREE_CODE (expr) == NOP_EXPR
8676 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8677 expr = TREE_OPERAND (expr, 0);
8678 return build_nop (dst_type, expr);
8681 else if (valid_p
8682 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
8683 TREE_TYPE (src_type)))
8684 check_for_casting_away_constness (loc, src_type, dst_type,
8685 CAST_EXPR, complain);
8688 if (complain & tf_error)
8689 error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
8690 src_type, dst_type);
8691 return error_mark_node;
8694 tree
8695 build_const_cast (location_t loc, tree type, tree expr,
8696 tsubst_flags_t complain)
8698 tree r;
8700 if (type == error_mark_node || error_operand_p (expr))
8701 return error_mark_node;
8703 if (processing_template_decl)
8705 tree t = build_min (CONST_CAST_EXPR, type, expr);
8707 if (!TREE_SIDE_EFFECTS (t)
8708 && type_dependent_expression_p (expr))
8709 /* There might turn out to be side effects inside expr. */
8710 TREE_SIDE_EFFECTS (t) = 1;
8711 r = convert_from_reference (t);
8712 protected_set_expr_location (r, loc);
8713 return r;
8716 r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
8717 if (r != error_mark_node)
8719 maybe_warn_about_useless_cast (loc, type, expr, complain);
8720 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8722 protected_set_expr_location (r, loc);
8723 return r;
8726 /* Like cp_build_c_cast, but for the c-common bits. */
8728 tree
8729 build_c_cast (location_t loc, tree type, tree expr)
8731 return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8734 /* Like the "build_c_cast" used for c-common, but using cp_expr to
8735 preserve location information even for tree nodes that don't
8736 support it. */
8738 cp_expr
8739 build_c_cast (location_t loc, tree type, cp_expr expr)
8741 cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
8742 result.set_location (loc);
8743 return result;
8746 /* Build an expression representing an explicit C-style cast to type
8747 TYPE of expression EXPR. */
8749 tree
8750 cp_build_c_cast (location_t loc, tree type, tree expr,
8751 tsubst_flags_t complain)
8753 tree value = expr;
8754 tree result;
8755 bool valid_p;
8757 if (type == error_mark_node || error_operand_p (expr))
8758 return error_mark_node;
8760 if (processing_template_decl)
8762 tree t = build_min (CAST_EXPR, type,
8763 tree_cons (NULL_TREE, value, NULL_TREE));
8764 /* We don't know if it will or will not have side effects. */
8765 TREE_SIDE_EFFECTS (t) = 1;
8766 return convert_from_reference (t);
8769 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
8770 'Class') should always be retained, because this information aids
8771 in method lookup. */
8772 if (objc_is_object_ptr (type)
8773 && objc_is_object_ptr (TREE_TYPE (expr)))
8774 return build_nop (type, expr);
8776 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8777 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8778 if (!TYPE_REF_P (type)
8779 && TREE_CODE (value) == NOP_EXPR
8780 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
8781 value = TREE_OPERAND (value, 0);
8783 if (TREE_CODE (type) == ARRAY_TYPE)
8785 /* Allow casting from T1* to T2[] because Cfront allows it.
8786 NIHCL uses it. It is not valid ISO C++ however. */
8787 if (TYPE_PTR_P (TREE_TYPE (expr)))
8789 if (complain & tf_error)
8790 permerror (loc, "ISO C++ forbids casting to an array type %qT",
8791 type);
8792 else
8793 return error_mark_node;
8794 type = build_pointer_type (TREE_TYPE (type));
8796 else
8798 if (complain & tf_error)
8799 error_at (loc, "ISO C++ forbids casting to an array type %qT",
8800 type);
8801 return error_mark_node;
8805 if (FUNC_OR_METHOD_TYPE_P (type))
8807 if (complain & tf_error)
8808 error_at (loc, "invalid cast to function type %qT", type);
8809 return error_mark_node;
8812 if (TYPE_PTR_P (type)
8813 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
8814 /* Casting to an integer of smaller size is an error detected elsewhere. */
8815 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
8816 /* Don't warn about converting any constant. */
8817 && !TREE_CONSTANT (value))
8818 warning_at (loc, OPT_Wint_to_pointer_cast,
8819 "cast to pointer from integer of different size");
8821 /* A C-style cast can be a const_cast. */
8822 result = build_const_cast_1 (loc, type, value, complain & tf_warning,
8823 &valid_p);
8824 if (valid_p)
8826 if (result != error_mark_node)
8828 maybe_warn_about_useless_cast (loc, type, value, complain);
8829 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8831 return result;
8834 /* Or a static cast. */
8835 result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
8836 &valid_p, complain);
8837 /* Or a reinterpret_cast. */
8838 if (!valid_p)
8839 result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
8840 &valid_p, complain);
8841 /* The static_cast or reinterpret_cast may be followed by a
8842 const_cast. */
8843 if (valid_p
8844 /* A valid cast may result in errors if, for example, a
8845 conversion to an ambiguous base class is required. */
8846 && !error_operand_p (result))
8848 tree result_type;
8850 maybe_warn_about_useless_cast (loc, type, value, complain);
8851 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8853 /* Non-class rvalues always have cv-unqualified type. */
8854 if (!CLASS_TYPE_P (type))
8855 type = TYPE_MAIN_VARIANT (type);
8856 result_type = TREE_TYPE (result);
8857 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
8858 result_type = TYPE_MAIN_VARIANT (result_type);
8859 /* If the type of RESULT does not match TYPE, perform a
8860 const_cast to make it match. If the static_cast or
8861 reinterpret_cast succeeded, we will differ by at most
8862 cv-qualification, so the follow-on const_cast is guaranteed
8863 to succeed. */
8864 if (!same_type_p (non_reference (type), non_reference (result_type)))
8866 result = build_const_cast_1 (loc, type, result, false, &valid_p);
8867 gcc_assert (valid_p);
8869 return result;
8872 return error_mark_node;
8875 /* For use from the C common bits. */
8876 tree
8877 build_modify_expr (location_t location,
8878 tree lhs, tree /*lhs_origtype*/,
8879 enum tree_code modifycode,
8880 location_t /*rhs_location*/, tree rhs,
8881 tree /*rhs_origtype*/)
8883 return cp_build_modify_expr (location, lhs, modifycode, rhs,
8884 tf_warning_or_error);
8887 /* Build an assignment expression of lvalue LHS from value RHS.
8888 MODIFYCODE is the code for a binary operator that we use
8889 to combine the old value of LHS with RHS to get the new value.
8890 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
8892 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
8894 tree
8895 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8896 tree rhs, tsubst_flags_t complain)
8898 lhs = mark_lvalue_use_nonread (lhs);
8900 tree result = NULL_TREE;
8901 tree newrhs = rhs;
8902 tree lhstype = TREE_TYPE (lhs);
8903 tree olhs = lhs;
8904 tree olhstype = lhstype;
8905 bool plain_assign = (modifycode == NOP_EXPR);
8906 bool compound_side_effects_p = false;
8907 tree preeval = NULL_TREE;
8909 /* Avoid duplicate error messages from operands that had errors. */
8910 if (error_operand_p (lhs) || error_operand_p (rhs))
8911 return error_mark_node;
8913 while (TREE_CODE (lhs) == COMPOUND_EXPR)
8915 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
8916 compound_side_effects_p = true;
8917 lhs = TREE_OPERAND (lhs, 1);
8920 /* Handle control structure constructs used as "lvalues". Note that we
8921 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8922 switch (TREE_CODE (lhs))
8924 /* Handle --foo = 5; as these are valid constructs in C++. */
8925 case PREDECREMENT_EXPR:
8926 case PREINCREMENT_EXPR:
8927 if (compound_side_effects_p)
8928 newrhs = rhs = stabilize_expr (rhs, &preeval);
8929 lhs = genericize_compound_lvalue (lhs);
8930 maybe_add_compound:
8931 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8932 and looked through the COMPOUND_EXPRs, readd them now around
8933 the resulting lhs. */
8934 if (TREE_CODE (olhs) == COMPOUND_EXPR)
8936 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
8937 tree *ptr = &TREE_OPERAND (lhs, 1);
8938 for (olhs = TREE_OPERAND (olhs, 1);
8939 TREE_CODE (olhs) == COMPOUND_EXPR;
8940 olhs = TREE_OPERAND (olhs, 1))
8942 *ptr = build2 (COMPOUND_EXPR, lhstype,
8943 TREE_OPERAND (olhs, 0), *ptr);
8944 ptr = &TREE_OPERAND (*ptr, 1);
8947 break;
8949 case MODIFY_EXPR:
8950 if (compound_side_effects_p)
8951 newrhs = rhs = stabilize_expr (rhs, &preeval);
8952 lhs = genericize_compound_lvalue (lhs);
8953 goto maybe_add_compound;
8955 case MIN_EXPR:
8956 case MAX_EXPR:
8957 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8958 when neither operand has side-effects. */
8959 if (!lvalue_or_else (lhs, lv_assign, complain))
8960 return error_mark_node;
8962 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
8963 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
8965 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
8966 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
8967 boolean_type_node,
8968 TREE_OPERAND (lhs, 0),
8969 TREE_OPERAND (lhs, 1)),
8970 TREE_OPERAND (lhs, 0),
8971 TREE_OPERAND (lhs, 1));
8972 gcc_fallthrough ();
8974 /* Handle (a ? b : c) used as an "lvalue". */
8975 case COND_EXPR:
8977 /* Produce (a ? (b = rhs) : (c = rhs))
8978 except that the RHS goes through a save-expr
8979 so the code to compute it is only emitted once. */
8980 if (VOID_TYPE_P (TREE_TYPE (rhs)))
8982 if (complain & tf_error)
8983 error_at (cp_expr_loc_or_loc (rhs, loc),
8984 "void value not ignored as it ought to be");
8985 return error_mark_node;
8988 rhs = stabilize_expr (rhs, &preeval);
8990 /* Check this here to avoid odd errors when trying to convert
8991 a throw to the type of the COND_EXPR. */
8992 if (!lvalue_or_else (lhs, lv_assign, complain))
8993 return error_mark_node;
8995 tree op1 = TREE_OPERAND (lhs, 1);
8996 if (TREE_CODE (op1) != THROW_EXPR)
8997 op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
8998 /* When sanitizing undefined behavior, even when rhs doesn't need
8999 stabilization at this point, the sanitization might add extra
9000 SAVE_EXPRs in there and so make sure there is no tree sharing
9001 in the rhs, otherwise those SAVE_EXPRs will have initialization
9002 only in one of the two branches. */
9003 if (sanitize_flags_p (SANITIZE_UNDEFINED
9004 | SANITIZE_UNDEFINED_NONDEFAULT))
9005 rhs = unshare_expr (rhs);
9006 tree op2 = TREE_OPERAND (lhs, 2);
9007 if (TREE_CODE (op2) != THROW_EXPR)
9008 op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9009 tree cond = build_conditional_expr (input_location,
9010 TREE_OPERAND (lhs, 0), op1, op2,
9011 complain);
9013 if (cond == error_mark_node)
9014 return cond;
9015 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9016 and looked through the COMPOUND_EXPRs, readd them now around
9017 the resulting cond before adding the preevaluated rhs. */
9018 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9020 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9021 TREE_OPERAND (olhs, 0), cond);
9022 tree *ptr = &TREE_OPERAND (cond, 1);
9023 for (olhs = TREE_OPERAND (olhs, 1);
9024 TREE_CODE (olhs) == COMPOUND_EXPR;
9025 olhs = TREE_OPERAND (olhs, 1))
9027 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9028 TREE_OPERAND (olhs, 0), *ptr);
9029 ptr = &TREE_OPERAND (*ptr, 1);
9032 /* Make sure the code to compute the rhs comes out
9033 before the split. */
9034 result = cond;
9035 goto ret;
9038 default:
9039 lhs = olhs;
9040 break;
9043 if (modifycode == INIT_EXPR)
9045 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9046 /* Do the default thing. */;
9047 else if (TREE_CODE (rhs) == CONSTRUCTOR)
9049 /* Compound literal. */
9050 if (! same_type_p (TREE_TYPE (rhs), lhstype))
9051 /* Call convert to generate an error; see PR 11063. */
9052 rhs = convert (lhstype, rhs);
9053 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
9054 TREE_SIDE_EFFECTS (result) = 1;
9055 goto ret;
9057 else if (! MAYBE_CLASS_TYPE_P (lhstype))
9058 /* Do the default thing. */;
9059 else
9061 releasing_vec rhs_vec = make_tree_vector_single (rhs);
9062 result = build_special_member_call (lhs, complete_ctor_identifier,
9063 &rhs_vec, lhstype, LOOKUP_NORMAL,
9064 complain);
9065 if (result == NULL_TREE)
9066 return error_mark_node;
9067 goto ret;
9070 else
9072 lhs = require_complete_type_sfinae (lhs, complain);
9073 if (lhs == error_mark_node)
9074 return error_mark_node;
9076 if (modifycode == NOP_EXPR)
9078 if (c_dialect_objc ())
9080 result = objc_maybe_build_modify_expr (lhs, rhs);
9081 if (result)
9082 goto ret;
9085 /* `operator=' is not an inheritable operator. */
9086 if (! MAYBE_CLASS_TYPE_P (lhstype))
9087 /* Do the default thing. */;
9088 else
9090 result = build_new_op (input_location, MODIFY_EXPR,
9091 LOOKUP_NORMAL, lhs, rhs,
9092 make_node (NOP_EXPR), NULL_TREE,
9093 /*overload=*/NULL, complain);
9094 if (result == NULL_TREE)
9095 return error_mark_node;
9096 goto ret;
9098 lhstype = olhstype;
9100 else
9102 tree init = NULL_TREE;
9104 /* A binary op has been requested. Combine the old LHS
9105 value with the RHS producing the value we should actually
9106 store into the LHS. */
9107 gcc_assert (!((TYPE_REF_P (lhstype)
9108 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9109 || MAYBE_CLASS_TYPE_P (lhstype)));
9111 /* An expression of the form E1 op= E2. [expr.ass] says:
9112 "Such expressions are deprecated if E1 has volatile-qualified
9113 type." We warn here rather than in cp_genericize_r because
9114 for compound assignments we are supposed to warn even if the
9115 assignment is a discarded-value expression. */
9116 if (TREE_THIS_VOLATILE (lhs) || CP_TYPE_VOLATILE_P (lhstype))
9117 warning_at (loc, OPT_Wvolatile,
9118 "compound assignment with %<volatile%>-qualified left "
9119 "operand is deprecated");
9120 /* Preevaluate the RHS to make sure its evaluation is complete
9121 before the lvalue-to-rvalue conversion of the LHS:
9123 [expr.ass] With respect to an indeterminately-sequenced
9124 function call, the operation of a compound assignment is a
9125 single evaluation. [ Note: Therefore, a function call shall
9126 not intervene between the lvalue-to-rvalue conversion and the
9127 side effect associated with any single compound assignment
9128 operator. -- end note ] */
9129 lhs = cp_stabilize_reference (lhs);
9130 rhs = decay_conversion (rhs, complain);
9131 if (rhs == error_mark_node)
9132 return error_mark_node;
9133 rhs = stabilize_expr (rhs, &init);
9134 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9135 if (newrhs == error_mark_node)
9137 if (complain & tf_error)
9138 inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9139 modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9140 return error_mark_node;
9143 if (init)
9144 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9146 /* Now it looks like a plain assignment. */
9147 modifycode = NOP_EXPR;
9148 if (c_dialect_objc ())
9150 result = objc_maybe_build_modify_expr (lhs, newrhs);
9151 if (result)
9152 goto ret;
9155 gcc_assert (!TYPE_REF_P (lhstype));
9156 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9159 /* The left-hand side must be an lvalue. */
9160 if (!lvalue_or_else (lhs, lv_assign, complain))
9161 return error_mark_node;
9163 /* Warn about modifying something that is `const'. Don't warn if
9164 this is initialization. */
9165 if (modifycode != INIT_EXPR
9166 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9167 /* Functions are not modifiable, even though they are
9168 lvalues. */
9169 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9170 /* If it's an aggregate and any field is const, then it is
9171 effectively const. */
9172 || (CLASS_TYPE_P (lhstype)
9173 && C_TYPE_FIELDS_READONLY (lhstype))))
9175 if (complain & tf_error)
9176 cxx_readonly_error (loc, lhs, lv_assign);
9177 return error_mark_node;
9180 /* If storing into a structure or union member, it may have been given a
9181 lowered bitfield type. We need to convert to the declared type first,
9182 so retrieve it now. */
9184 olhstype = unlowered_expr_type (lhs);
9186 /* Convert new value to destination type. */
9188 if (TREE_CODE (lhstype) == ARRAY_TYPE)
9190 int from_array;
9192 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9194 if (modifycode != INIT_EXPR)
9196 if (complain & tf_error)
9197 error_at (loc,
9198 "assigning to an array from an initializer list");
9199 return error_mark_node;
9201 if (check_array_initializer (lhs, lhstype, newrhs))
9202 return error_mark_node;
9203 newrhs = digest_init (lhstype, newrhs, complain);
9204 if (newrhs == error_mark_node)
9205 return error_mark_node;
9208 /* C++11 8.5/17: "If the destination type is an array of characters,
9209 an array of char16_t, an array of char32_t, or an array of wchar_t,
9210 and the initializer is a string literal...". */
9211 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9212 == STRING_CST)
9213 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9214 && modifycode == INIT_EXPR)
9216 newrhs = digest_init (lhstype, newrhs, complain);
9217 if (newrhs == error_mark_node)
9218 return error_mark_node;
9221 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9222 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9224 if (complain & tf_error)
9225 error_at (loc, "incompatible types in assignment of %qT to %qT",
9226 TREE_TYPE (rhs), lhstype);
9227 return error_mark_node;
9230 /* Allow array assignment in compiler-generated code. */
9231 else if (!current_function_decl
9232 || !DECL_DEFAULTED_FN (current_function_decl))
9234 /* This routine is used for both initialization and assignment.
9235 Make sure the diagnostic message differentiates the context. */
9236 if (complain & tf_error)
9238 if (modifycode == INIT_EXPR)
9239 error_at (loc, "array used as initializer");
9240 else
9241 error_at (loc, "invalid array assignment");
9243 return error_mark_node;
9246 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9247 ? 1 + (modifycode != INIT_EXPR): 0;
9248 result = build_vec_init (lhs, NULL_TREE, newrhs,
9249 /*explicit_value_init_p=*/false,
9250 from_array, complain);
9251 goto ret;
9254 if (modifycode == INIT_EXPR)
9255 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9256 LOOKUP_ONLYCONVERTING. */
9257 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9258 ICR_INIT, NULL_TREE, 0,
9259 complain | tf_no_cleanup);
9260 else
9261 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9262 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9264 if (!same_type_p (lhstype, olhstype))
9265 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9267 if (modifycode != INIT_EXPR)
9269 if (TREE_CODE (newrhs) == CALL_EXPR
9270 && TYPE_NEEDS_CONSTRUCTING (lhstype))
9271 newrhs = build_cplus_new (lhstype, newrhs, complain);
9273 /* Can't initialize directly from a TARGET_EXPR, since that would
9274 cause the lhs to be constructed twice, and possibly result in
9275 accidental self-initialization. So we force the TARGET_EXPR to be
9276 expanded without a target. */
9277 if (TREE_CODE (newrhs) == TARGET_EXPR)
9278 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9279 TREE_OPERAND (newrhs, 0));
9282 if (newrhs == error_mark_node)
9283 return error_mark_node;
9285 if (c_dialect_objc () && flag_objc_gc)
9287 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9289 if (result)
9290 goto ret;
9293 result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9294 lhstype, lhs, newrhs);
9296 TREE_SIDE_EFFECTS (result) = 1;
9297 if (!plain_assign)
9298 suppress_warning (result, OPT_Wparentheses);
9300 ret:
9301 if (preeval)
9302 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
9303 return result;
9306 cp_expr
9307 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9308 tree rhs, tree lookups, tsubst_flags_t complain)
9310 tree orig_lhs = lhs;
9311 tree orig_rhs = rhs;
9312 tree overload = NULL_TREE;
9314 if (lhs == error_mark_node || rhs == error_mark_node)
9315 return cp_expr (error_mark_node, loc);
9317 if (processing_template_decl)
9319 if (modifycode == NOP_EXPR
9320 || type_dependent_expression_p (lhs)
9321 || type_dependent_expression_p (rhs))
9323 tree op = build_min_nt_loc (loc, modifycode, NULL_TREE, NULL_TREE);
9324 tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
9325 if (modifycode != NOP_EXPR)
9326 TREE_TYPE (rval)
9327 = build_dependent_operator_type (lookups, modifycode, true);
9328 return rval;
9331 lhs = build_non_dependent_expr (lhs);
9332 rhs = build_non_dependent_expr (rhs);
9335 if (modifycode != NOP_EXPR)
9337 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
9338 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
9339 lhs, rhs, op, lookups, &overload, complain);
9340 if (rval)
9342 if (rval == error_mark_node)
9343 return rval;
9344 suppress_warning (rval /* What warning? */);
9345 if (processing_template_decl)
9347 if (overload != NULL_TREE)
9348 return (build_min_non_dep_op_overload
9349 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
9351 return (build_min_non_dep
9352 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
9354 return rval;
9357 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
9360 /* Helper function for get_delta_difference which assumes FROM is a base
9361 class of TO. Returns a delta for the conversion of pointer-to-member
9362 of FROM to pointer-to-member of TO. If the conversion is invalid and
9363 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9364 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9365 If C_CAST_P is true, this conversion is taking place as part of a
9366 C-style cast. */
9368 static tree
9369 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
9370 tsubst_flags_t complain)
9372 tree binfo;
9373 base_kind kind;
9375 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
9376 &kind, complain);
9378 if (binfo == error_mark_node)
9380 if (!(complain & tf_error))
9381 return error_mark_node;
9383 inform (input_location, " in pointer to member function conversion");
9384 return size_zero_node;
9386 else if (binfo)
9388 if (kind != bk_via_virtual)
9389 return BINFO_OFFSET (binfo);
9390 else
9391 /* FROM is a virtual base class of TO. Issue an error or warning
9392 depending on whether or not this is a reinterpret cast. */
9394 if (!(complain & tf_error))
9395 return error_mark_node;
9397 error ("pointer to member conversion via virtual base %qT",
9398 BINFO_TYPE (binfo_from_vbase (binfo)));
9400 return size_zero_node;
9403 else
9404 return NULL_TREE;
9407 /* Get difference in deltas for different pointer to member function
9408 types. If the conversion is invalid and tf_error is not set in
9409 COMPLAIN, returns error_mark_node, otherwise returns an integer
9410 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9411 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9412 conversions as well. If C_CAST_P is true this conversion is taking
9413 place as part of a C-style cast.
9415 Note that the naming of FROM and TO is kind of backwards; the return
9416 value is what we add to a TO in order to get a FROM. They are named
9417 this way because we call this function to find out how to convert from
9418 a pointer to member of FROM to a pointer to member of TO. */
9420 static tree
9421 get_delta_difference (tree from, tree to,
9422 bool allow_inverse_p,
9423 bool c_cast_p, tsubst_flags_t complain)
9425 tree result;
9427 if (same_type_ignoring_top_level_qualifiers_p (from, to))
9428 /* Pointer to member of incomplete class is permitted*/
9429 result = size_zero_node;
9430 else
9431 result = get_delta_difference_1 (from, to, c_cast_p, complain);
9433 if (result == error_mark_node)
9434 return error_mark_node;
9436 if (!result)
9438 if (!allow_inverse_p)
9440 if (!(complain & tf_error))
9441 return error_mark_node;
9443 error_not_base_type (from, to);
9444 inform (input_location, " in pointer to member conversion");
9445 result = size_zero_node;
9447 else
9449 result = get_delta_difference_1 (to, from, c_cast_p, complain);
9451 if (result == error_mark_node)
9452 return error_mark_node;
9454 if (result)
9455 result = size_diffop_loc (input_location,
9456 size_zero_node, result);
9457 else
9459 if (!(complain & tf_error))
9460 return error_mark_node;
9462 error_not_base_type (from, to);
9463 inform (input_location, " in pointer to member conversion");
9464 result = size_zero_node;
9469 return convert_to_integer (ptrdiff_type_node, result);
9472 /* Return a constructor for the pointer-to-member-function TYPE using
9473 the other components as specified. */
9475 tree
9476 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
9478 tree u = NULL_TREE;
9479 tree delta_field;
9480 tree pfn_field;
9481 vec<constructor_elt, va_gc> *v;
9483 /* Pull the FIELD_DECLs out of the type. */
9484 pfn_field = TYPE_FIELDS (type);
9485 delta_field = DECL_CHAIN (pfn_field);
9487 /* Make sure DELTA has the type we want. */
9488 delta = convert_and_check (input_location, delta_type_node, delta);
9490 /* Convert to the correct target type if necessary. */
9491 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
9493 /* Finish creating the initializer. */
9494 vec_alloc (v, 2);
9495 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
9496 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
9497 u = build_constructor (type, v);
9498 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
9499 TREE_STATIC (u) = (TREE_CONSTANT (u)
9500 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
9501 != NULL_TREE)
9502 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
9503 != NULL_TREE));
9504 return u;
9507 /* Build a constructor for a pointer to member function. It can be
9508 used to initialize global variables, local variable, or used
9509 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
9510 want to be.
9512 If FORCE is nonzero, then force this conversion, even if
9513 we would rather not do it. Usually set when using an explicit
9514 cast. A C-style cast is being processed iff C_CAST_P is true.
9516 Return error_mark_node, if something goes wrong. */
9518 tree
9519 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
9520 tsubst_flags_t complain)
9522 tree fn;
9523 tree pfn_type;
9524 tree to_type;
9526 if (error_operand_p (pfn))
9527 return error_mark_node;
9529 pfn_type = TREE_TYPE (pfn);
9530 to_type = build_ptrmemfunc_type (type);
9532 /* Handle multiple conversions of pointer to member functions. */
9533 if (TYPE_PTRMEMFUNC_P (pfn_type))
9535 tree delta = NULL_TREE;
9536 tree npfn = NULL_TREE;
9537 tree n;
9539 if (!force
9540 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
9541 LOOKUP_NORMAL, complain))
9543 if (complain & tf_error)
9544 error ("invalid conversion to type %qT from type %qT",
9545 to_type, pfn_type);
9546 else
9547 return error_mark_node;
9550 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
9551 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
9552 force,
9553 c_cast_p, complain);
9554 if (n == error_mark_node)
9555 return error_mark_node;
9557 /* We don't have to do any conversion to convert a
9558 pointer-to-member to its own type. But, we don't want to
9559 just return a PTRMEM_CST if there's an explicit cast; that
9560 cast should make the expression an invalid template argument. */
9561 if (TREE_CODE (pfn) != PTRMEM_CST)
9563 if (same_type_p (to_type, pfn_type))
9564 return pfn;
9565 else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
9566 return build_reinterpret_cast (input_location, to_type, pfn,
9567 complain);
9570 if (TREE_SIDE_EFFECTS (pfn))
9571 pfn = save_expr (pfn);
9573 /* Obtain the function pointer and the current DELTA. */
9574 if (TREE_CODE (pfn) == PTRMEM_CST)
9575 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
9576 else
9578 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
9579 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
9582 /* Just adjust the DELTA field. */
9583 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9584 (TREE_TYPE (delta), ptrdiff_type_node));
9585 if (!integer_zerop (n))
9587 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
9588 n = cp_build_binary_op (input_location,
9589 LSHIFT_EXPR, n, integer_one_node,
9590 complain);
9591 delta = cp_build_binary_op (input_location,
9592 PLUS_EXPR, delta, n, complain);
9594 return build_ptrmemfunc1 (to_type, delta, npfn);
9597 /* Handle null pointer to member function conversions. */
9598 if (null_ptr_cst_p (pfn))
9600 pfn = cp_build_c_cast (input_location,
9601 TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
9602 pfn, complain);
9603 return build_ptrmemfunc1 (to_type,
9604 integer_zero_node,
9605 pfn);
9608 if (type_unknown_p (pfn))
9609 return instantiate_type (type, pfn, complain);
9611 fn = TREE_OPERAND (pfn, 0);
9612 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9613 /* In a template, we will have preserved the
9614 OFFSET_REF. */
9615 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
9616 return make_ptrmem_cst (to_type, fn);
9619 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
9620 given by CST.
9622 ??? There is no consistency as to the types returned for the above
9623 values. Some code acts as if it were a sizetype and some as if it were
9624 integer_type_node. */
9626 void
9627 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
9629 tree type = TREE_TYPE (cst);
9630 tree fn = PTRMEM_CST_MEMBER (cst);
9631 tree ptr_class, fn_class;
9633 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9635 /* The class that the function belongs to. */
9636 fn_class = DECL_CONTEXT (fn);
9638 /* The class that we're creating a pointer to member of. */
9639 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
9641 /* First, calculate the adjustment to the function's class. */
9642 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
9643 /*c_cast_p=*/0, tf_warning_or_error);
9645 if (!DECL_VIRTUAL_P (fn))
9647 tree t = build_addr_func (fn, tf_warning_or_error);
9648 if (TREE_CODE (t) == ADDR_EXPR)
9649 SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
9650 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
9652 else
9654 /* If we're dealing with a virtual function, we have to adjust 'this'
9655 again, to point to the base which provides the vtable entry for
9656 fn; the call will do the opposite adjustment. */
9657 tree orig_class = DECL_CONTEXT (fn);
9658 tree binfo = binfo_or_else (orig_class, fn_class);
9659 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9660 *delta, BINFO_OFFSET (binfo));
9662 /* We set PFN to the vtable offset at which the function can be
9663 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
9664 case delta is shifted left, and then incremented). */
9665 *pfn = DECL_VINDEX (fn);
9666 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
9667 TYPE_SIZE_UNIT (vtable_entry_type));
9669 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
9671 case ptrmemfunc_vbit_in_pfn:
9672 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
9673 integer_one_node);
9674 break;
9676 case ptrmemfunc_vbit_in_delta:
9677 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
9678 *delta, integer_one_node);
9679 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
9680 *delta, integer_one_node);
9681 break;
9683 default:
9684 gcc_unreachable ();
9687 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
9691 /* Return an expression for PFN from the pointer-to-member function
9692 given by T. */
9694 static tree
9695 pfn_from_ptrmemfunc (tree t)
9697 if (TREE_CODE (t) == PTRMEM_CST)
9699 tree delta;
9700 tree pfn;
9702 expand_ptrmemfunc_cst (t, &delta, &pfn);
9703 if (pfn)
9704 return pfn;
9707 return build_ptrmemfunc_access_expr (t, pfn_identifier);
9710 /* Return an expression for DELTA from the pointer-to-member function
9711 given by T. */
9713 static tree
9714 delta_from_ptrmemfunc (tree t)
9716 if (TREE_CODE (t) == PTRMEM_CST)
9718 tree delta;
9719 tree pfn;
9721 expand_ptrmemfunc_cst (t, &delta, &pfn);
9722 if (delta)
9723 return delta;
9726 return build_ptrmemfunc_access_expr (t, delta_identifier);
9729 /* Convert value RHS to type TYPE as preparation for an assignment to
9730 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
9731 implicit conversion is. If FNDECL is non-NULL, we are doing the
9732 conversion in order to pass the PARMNUMth argument of FNDECL.
9733 If FNDECL is NULL, we are doing the conversion in function pointer
9734 argument passing, conversion in initialization, etc. */
9736 static tree
9737 convert_for_assignment (tree type, tree rhs,
9738 impl_conv_rhs errtype, tree fndecl, int parmnum,
9739 tsubst_flags_t complain, int flags)
9741 tree rhstype;
9742 enum tree_code coder;
9744 location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
9745 bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
9746 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
9747 but preserve location wrappers. */
9748 if (TREE_CODE (rhs) == NON_LVALUE_EXPR
9749 && !location_wrapper_p (rhs))
9750 rhs = TREE_OPERAND (rhs, 0);
9752 /* Handle [dcl.init.list] direct-list-initialization from
9753 single element of enumeration with a fixed underlying type. */
9754 if (is_direct_enum_init (type, rhs))
9756 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
9757 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
9759 warning_sentinel w (warn_useless_cast);
9760 warning_sentinel w2 (warn_ignored_qualifiers);
9761 rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
9763 else
9764 rhs = error_mark_node;
9767 rhstype = TREE_TYPE (rhs);
9768 coder = TREE_CODE (rhstype);
9770 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
9771 && vector_types_convertible_p (type, rhstype, true))
9773 rhs = mark_rvalue_use (rhs);
9774 return convert (type, rhs);
9777 if (rhs == error_mark_node || rhstype == error_mark_node)
9778 return error_mark_node;
9779 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
9780 return error_mark_node;
9782 /* The RHS of an assignment cannot have void type. */
9783 if (coder == VOID_TYPE)
9785 if (complain & tf_error)
9786 error_at (rhs_loc, "void value not ignored as it ought to be");
9787 return error_mark_node;
9790 if (c_dialect_objc ())
9792 int parmno;
9793 tree selector;
9794 tree rname = fndecl;
9796 switch (errtype)
9798 case ICR_ASSIGN:
9799 parmno = -1;
9800 break;
9801 case ICR_INIT:
9802 parmno = -2;
9803 break;
9804 default:
9805 selector = objc_message_selector ();
9806 parmno = parmnum;
9807 if (selector && parmno > 1)
9809 rname = selector;
9810 parmno -= 1;
9814 if (objc_compare_types (type, rhstype, parmno, rname))
9816 rhs = mark_rvalue_use (rhs);
9817 return convert (type, rhs);
9821 /* [expr.ass]
9823 The expression is implicitly converted (clause _conv_) to the
9824 cv-unqualified type of the left operand.
9826 We allow bad conversions here because by the time we get to this point
9827 we are committed to doing the conversion. If we end up doing a bad
9828 conversion, convert_like will complain. */
9829 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
9831 /* When -Wno-pmf-conversions is use, we just silently allow
9832 conversions from pointers-to-members to plain pointers. If
9833 the conversion doesn't work, cp_convert will complain. */
9834 if (!warn_pmf2ptr
9835 && TYPE_PTR_P (type)
9836 && TYPE_PTRMEMFUNC_P (rhstype))
9837 rhs = cp_convert (strip_top_quals (type), rhs, complain);
9838 else
9840 if (complain & tf_error)
9842 /* If the right-hand side has unknown type, then it is an
9843 overloaded function. Call instantiate_type to get error
9844 messages. */
9845 if (rhstype == unknown_type_node)
9847 tree r = instantiate_type (type, rhs, tf_warning_or_error);
9848 /* -fpermissive might allow this; recurse. */
9849 if (!seen_error ())
9850 return convert_for_assignment (type, r, errtype, fndecl,
9851 parmnum, complain, flags);
9853 else if (fndecl)
9854 complain_about_bad_argument (rhs_loc,
9855 rhstype, type,
9856 fndecl, parmnum);
9857 else
9859 range_label_for_type_mismatch label (rhstype, type);
9860 gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
9861 switch (errtype)
9863 case ICR_DEFAULT_ARGUMENT:
9864 error_at (&richloc,
9865 "cannot convert %qH to %qI in default argument",
9866 rhstype, type);
9867 break;
9868 case ICR_ARGPASS:
9869 error_at (&richloc,
9870 "cannot convert %qH to %qI in argument passing",
9871 rhstype, type);
9872 break;
9873 case ICR_CONVERTING:
9874 error_at (&richloc, "cannot convert %qH to %qI",
9875 rhstype, type);
9876 break;
9877 case ICR_INIT:
9878 error_at (&richloc,
9879 "cannot convert %qH to %qI in initialization",
9880 rhstype, type);
9881 break;
9882 case ICR_RETURN:
9883 error_at (&richloc, "cannot convert %qH to %qI in return",
9884 rhstype, type);
9885 break;
9886 case ICR_ASSIGN:
9887 error_at (&richloc,
9888 "cannot convert %qH to %qI in assignment",
9889 rhstype, type);
9890 break;
9891 default:
9892 gcc_unreachable();
9895 if (TYPE_PTR_P (rhstype)
9896 && TYPE_PTR_P (type)
9897 && CLASS_TYPE_P (TREE_TYPE (rhstype))
9898 && CLASS_TYPE_P (TREE_TYPE (type))
9899 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
9900 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
9901 (TREE_TYPE (rhstype))),
9902 "class type %qT is incomplete", TREE_TYPE (rhstype));
9904 return error_mark_node;
9907 if (warn_suggest_attribute_format)
9909 const enum tree_code codel = TREE_CODE (type);
9910 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9911 && coder == codel
9912 && check_missing_format_attribute (type, rhstype)
9913 && (complain & tf_warning))
9914 switch (errtype)
9916 case ICR_ARGPASS:
9917 case ICR_DEFAULT_ARGUMENT:
9918 if (fndecl)
9919 warning (OPT_Wsuggest_attribute_format,
9920 "parameter %qP of %qD might be a candidate "
9921 "for a format attribute", parmnum, fndecl);
9922 else
9923 warning (OPT_Wsuggest_attribute_format,
9924 "parameter might be a candidate "
9925 "for a format attribute");
9926 break;
9927 case ICR_CONVERTING:
9928 warning (OPT_Wsuggest_attribute_format,
9929 "target of conversion might be a candidate "
9930 "for a format attribute");
9931 break;
9932 case ICR_INIT:
9933 warning (OPT_Wsuggest_attribute_format,
9934 "target of initialization might be a candidate "
9935 "for a format attribute");
9936 break;
9937 case ICR_RETURN:
9938 warning (OPT_Wsuggest_attribute_format,
9939 "return type might be a candidate "
9940 "for a format attribute");
9941 break;
9942 case ICR_ASSIGN:
9943 warning (OPT_Wsuggest_attribute_format,
9944 "left-hand side of assignment might be a candidate "
9945 "for a format attribute");
9946 break;
9947 default:
9948 gcc_unreachable();
9952 /* If -Wparentheses, warn about a = b = c when a has type bool and b
9953 does not. */
9954 if (warn_parentheses
9955 && TREE_CODE (type) == BOOLEAN_TYPE
9956 && TREE_CODE (rhs) == MODIFY_EXPR
9957 && !warning_suppressed_p (rhs, OPT_Wparentheses)
9958 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
9959 && (complain & tf_warning)
9960 && warning_at (rhs_loc, OPT_Wparentheses,
9961 "suggest parentheses around assignment used as "
9962 "truth value"))
9963 suppress_warning (rhs, OPT_Wparentheses);
9965 if (complain & tf_warning)
9966 warn_for_address_or_pointer_of_packed_member (type, rhs);
9968 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
9969 complain, flags);
9972 /* Convert RHS to be of type TYPE.
9973 If EXP is nonzero, it is the target of the initialization.
9974 ERRTYPE indicates what kind of error the implicit conversion is.
9976 Two major differences between the behavior of
9977 `convert_for_assignment' and `convert_for_initialization'
9978 are that references are bashed in the former, while
9979 copied in the latter, and aggregates are assigned in
9980 the former (operator=) while initialized in the
9981 latter (X(X&)).
9983 If using constructor make sure no conversion operator exists, if one does
9984 exist, an ambiguity exists. */
9986 tree
9987 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
9988 impl_conv_rhs errtype, tree fndecl, int parmnum,
9989 tsubst_flags_t complain)
9991 enum tree_code codel = TREE_CODE (type);
9992 tree rhstype;
9993 enum tree_code coder;
9995 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9996 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9997 if (TREE_CODE (rhs) == NOP_EXPR
9998 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
9999 && codel != REFERENCE_TYPE)
10000 rhs = TREE_OPERAND (rhs, 0);
10002 if (type == error_mark_node
10003 || rhs == error_mark_node
10004 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10005 return error_mark_node;
10007 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10009 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10010 && TREE_CODE (type) != ARRAY_TYPE
10011 && (!TYPE_REF_P (type)
10012 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10013 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10014 && !TYPE_REFFN_P (type))
10015 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10016 rhs = decay_conversion (rhs, complain);
10018 rhstype = TREE_TYPE (rhs);
10019 coder = TREE_CODE (rhstype);
10021 if (coder == ERROR_MARK)
10022 return error_mark_node;
10024 /* We accept references to incomplete types, so we can
10025 return here before checking if RHS is of complete type. */
10027 if (codel == REFERENCE_TYPE)
10029 auto_diagnostic_group d;
10030 /* This should eventually happen in convert_arguments. */
10031 int savew = 0, savee = 0;
10033 if (fndecl)
10034 savew = warningcount + werrorcount, savee = errorcount;
10035 rhs = initialize_reference (type, rhs, flags, complain);
10037 if (fndecl
10038 && (warningcount + werrorcount > savew || errorcount > savee))
10039 inform (get_fndecl_argument_location (fndecl, parmnum),
10040 "in passing argument %P of %qD", parmnum, fndecl);
10041 return rhs;
10044 if (exp != 0)
10045 exp = require_complete_type_sfinae (exp, complain);
10046 if (exp == error_mark_node)
10047 return error_mark_node;
10049 type = complete_type (type);
10051 if (DIRECT_INIT_EXPR_P (type, rhs))
10052 /* Don't try to do copy-initialization if we already have
10053 direct-initialization. */
10054 return rhs;
10056 if (MAYBE_CLASS_TYPE_P (type))
10057 return perform_implicit_conversion_flags (type, rhs, complain, flags);
10059 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10060 complain, flags);
10063 /* If RETVAL is the address of, or a reference to, a local variable or
10064 temporary give an appropriate warning and return true. */
10066 static bool
10067 maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10069 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10070 tree whats_returned = fold_for_warn (retval);
10071 if (!loc)
10072 loc = cp_expr_loc_or_input_loc (retval);
10074 for (;;)
10076 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10077 whats_returned = TREE_OPERAND (whats_returned, 1);
10078 else if (CONVERT_EXPR_P (whats_returned)
10079 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10080 whats_returned = TREE_OPERAND (whats_returned, 0);
10081 else
10082 break;
10085 if (TREE_CODE (whats_returned) == TARGET_EXPR
10086 && is_std_init_list (TREE_TYPE (whats_returned)))
10088 tree init = TARGET_EXPR_INITIAL (whats_returned);
10089 if (TREE_CODE (init) == CONSTRUCTOR)
10090 /* Pull out the array address. */
10091 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10092 else if (TREE_CODE (init) == INDIRECT_REF)
10093 /* The source of a trivial copy looks like *(T*)&var. */
10094 whats_returned = TREE_OPERAND (init, 0);
10095 else
10096 return false;
10097 STRIP_NOPS (whats_returned);
10100 /* As a special case, we handle a call to std::move or std::forward. */
10101 if (TREE_CODE (whats_returned) == CALL_EXPR
10102 && (is_std_move_p (whats_returned)
10103 || is_std_forward_p (whats_returned)))
10105 tree arg = CALL_EXPR_ARG (whats_returned, 0);
10106 return maybe_warn_about_returning_address_of_local (arg, loc);
10109 if (TREE_CODE (whats_returned) != ADDR_EXPR)
10110 return false;
10111 whats_returned = TREE_OPERAND (whats_returned, 0);
10113 while (TREE_CODE (whats_returned) == COMPONENT_REF
10114 || TREE_CODE (whats_returned) == ARRAY_REF)
10115 whats_returned = TREE_OPERAND (whats_returned, 0);
10117 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10118 || TREE_CODE (whats_returned) == TARGET_EXPR)
10120 if (TYPE_REF_P (valtype))
10121 warning_at (loc, OPT_Wreturn_local_addr,
10122 "returning reference to temporary");
10123 else if (is_std_init_list (valtype))
10124 warning_at (loc, OPT_Winit_list_lifetime,
10125 "returning temporary %<initializer_list%> does not extend "
10126 "the lifetime of the underlying array");
10127 return true;
10130 STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10132 if (DECL_P (whats_returned)
10133 && DECL_NAME (whats_returned)
10134 && DECL_FUNCTION_SCOPE_P (whats_returned)
10135 && !is_capture_proxy (whats_returned)
10136 && !(TREE_STATIC (whats_returned)
10137 || TREE_PUBLIC (whats_returned)))
10139 if (VAR_P (whats_returned)
10140 && DECL_DECOMPOSITION_P (whats_returned)
10141 && DECL_DECOMP_BASE (whats_returned)
10142 && DECL_HAS_VALUE_EXPR_P (whats_returned))
10144 /* When returning address of a structured binding, if the structured
10145 binding is not a reference, continue normally, if it is a
10146 reference, recurse on the initializer of the structured
10147 binding. */
10148 tree base = DECL_DECOMP_BASE (whats_returned);
10149 if (TYPE_REF_P (TREE_TYPE (base)))
10151 if (tree init = DECL_INITIAL (base))
10152 return maybe_warn_about_returning_address_of_local (init, loc);
10153 else
10154 return false;
10157 bool w = false;
10158 auto_diagnostic_group d;
10159 if (TYPE_REF_P (valtype))
10160 w = warning_at (loc, OPT_Wreturn_local_addr,
10161 "reference to local variable %qD returned",
10162 whats_returned);
10163 else if (is_std_init_list (valtype))
10164 w = warning_at (loc, OPT_Winit_list_lifetime,
10165 "returning local %<initializer_list%> variable %qD "
10166 "does not extend the lifetime of the underlying array",
10167 whats_returned);
10168 else if (POINTER_TYPE_P (valtype)
10169 && TREE_CODE (whats_returned) == LABEL_DECL)
10170 w = warning_at (loc, OPT_Wreturn_local_addr,
10171 "address of label %qD returned",
10172 whats_returned);
10173 else if (POINTER_TYPE_P (valtype))
10174 w = warning_at (loc, OPT_Wreturn_local_addr,
10175 "address of local variable %qD returned",
10176 whats_returned);
10177 if (w)
10178 inform (DECL_SOURCE_LOCATION (whats_returned),
10179 "declared here");
10180 return true;
10183 return false;
10186 /* Returns true if DECL is in the std namespace. */
10188 bool
10189 decl_in_std_namespace_p (tree decl)
10191 while (decl)
10193 decl = decl_namespace_context (decl);
10194 if (DECL_NAMESPACE_STD_P (decl))
10195 return true;
10196 /* Allow inline namespaces inside of std namespace, e.g. with
10197 --enable-symvers=gnu-versioned-namespace std::forward would be
10198 actually std::_8::forward. */
10199 if (!DECL_NAMESPACE_INLINE_P (decl))
10200 return false;
10201 decl = CP_DECL_CONTEXT (decl);
10203 return false;
10206 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10208 static bool
10209 is_std_forward_p (tree fn)
10211 /* std::forward only takes one argument. */
10212 if (call_expr_nargs (fn) != 1)
10213 return false;
10215 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10216 if (!decl_in_std_namespace_p (fndecl))
10217 return false;
10219 tree name = DECL_NAME (fndecl);
10220 return name && id_equal (name, "forward");
10223 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10225 static bool
10226 is_std_move_p (tree fn)
10228 /* std::move only takes one argument. */
10229 if (call_expr_nargs (fn) != 1)
10230 return false;
10232 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10233 if (!decl_in_std_namespace_p (fndecl))
10234 return false;
10236 tree name = DECL_NAME (fndecl);
10237 return name && id_equal (name, "move");
10240 /* Returns true if RETVAL is a good candidate for the NRVO as per
10241 [class.copy.elision]. FUNCTYPE is the type the function is declared
10242 to return. */
10244 static bool
10245 can_do_nrvo_p (tree retval, tree functype)
10247 if (functype == error_mark_node)
10248 return false;
10249 if (retval)
10250 STRIP_ANY_LOCATION_WRAPPER (retval);
10251 tree result = DECL_RESULT (current_function_decl);
10252 return (retval != NULL_TREE
10253 && !processing_template_decl
10254 /* Must be a local, automatic variable. */
10255 && VAR_P (retval)
10256 && DECL_CONTEXT (retval) == current_function_decl
10257 && !TREE_STATIC (retval)
10258 /* And not a lambda or anonymous union proxy. */
10259 && !DECL_HAS_VALUE_EXPR_P (retval)
10260 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10261 /* The cv-unqualified type of the returned value must be the
10262 same as the cv-unqualified return type of the
10263 function. */
10264 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
10265 (TYPE_MAIN_VARIANT (functype)))
10266 /* And the returned value must be non-volatile. */
10267 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10270 /* If we should treat RETVAL, an expression being returned, as if it were
10271 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10272 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10273 context (rather than throw). */
10275 tree
10276 treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10278 if (cxx_dialect == cxx98)
10279 return NULL_TREE;
10281 tree retval = expr;
10282 STRIP_ANY_LOCATION_WRAPPER (retval);
10283 if (REFERENCE_REF_P (retval))
10284 retval = TREE_OPERAND (retval, 0);
10286 /* An implicitly movable entity is a variable of automatic storage duration
10287 that is either a non-volatile object or (C++20) an rvalue reference to a
10288 non-volatile object type. */
10289 if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
10290 || TREE_CODE (retval) == PARM_DECL)
10291 && !TREE_STATIC (retval)
10292 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
10293 && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
10294 || (cxx_dialect >= cxx20
10295 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
10296 return NULL_TREE;
10298 /* If the expression in a return or co_return statement is a (possibly
10299 parenthesized) id-expression that names an implicitly movable entity
10300 declared in the body or parameter-declaration-clause of the innermost
10301 enclosing function or lambda-expression, */
10302 if (DECL_CONTEXT (retval) != current_function_decl)
10303 return NULL_TREE;
10304 if (return_p)
10305 return set_implicit_rvalue_p (move (expr));
10307 /* if the operand of a throw-expression is a (possibly parenthesized)
10308 id-expression that names an implicitly movable entity whose scope does not
10309 extend beyond the compound-statement of the innermost try-block or
10310 function-try-block (if any) whose compound-statement or ctor-initializer
10311 encloses the throw-expression, */
10313 /* C++20 added move on throw of parms. */
10314 if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
10315 return NULL_TREE;
10317 for (cp_binding_level *b = current_binding_level;
10318 ; b = b->level_chain)
10320 for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
10321 if (decl == retval)
10322 return set_implicit_rvalue_p (move (expr));
10323 if (b->kind == sk_function_parms || b->kind == sk_try)
10324 return NULL_TREE;
10328 /* Warn about wrong usage of std::move in a return statement. RETVAL
10329 is the expression we are returning; FUNCTYPE is the type the function
10330 is declared to return. */
10332 static void
10333 maybe_warn_pessimizing_move (tree retval, tree functype)
10335 if (!(warn_pessimizing_move || warn_redundant_move))
10336 return;
10338 location_t loc = cp_expr_loc_or_input_loc (retval);
10340 /* C++98 doesn't know move. */
10341 if (cxx_dialect < cxx11)
10342 return;
10344 /* Wait until instantiation time, since we can't gauge if we should do
10345 the NRVO until then. */
10346 if (processing_template_decl)
10347 return;
10349 /* This is only interesting for class types. */
10350 if (!CLASS_TYPE_P (functype))
10351 return;
10353 /* We're looking for *std::move<T&> ((T &) &arg). */
10354 if (REFERENCE_REF_P (retval)
10355 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
10357 tree fn = TREE_OPERAND (retval, 0);
10358 if (is_std_move_p (fn))
10360 tree arg = CALL_EXPR_ARG (fn, 0);
10361 tree moved;
10362 if (TREE_CODE (arg) != NOP_EXPR)
10363 return;
10364 arg = TREE_OPERAND (arg, 0);
10365 if (TREE_CODE (arg) != ADDR_EXPR)
10366 return;
10367 arg = TREE_OPERAND (arg, 0);
10368 arg = convert_from_reference (arg);
10369 /* Warn if we could do copy elision were it not for the move. */
10370 if (can_do_nrvo_p (arg, functype))
10372 auto_diagnostic_group d;
10373 if (warning_at (loc, OPT_Wpessimizing_move,
10374 "moving a local object in a return statement "
10375 "prevents copy elision"))
10376 inform (loc, "remove %<std::move%> call");
10378 /* Warn if the move is redundant. It is redundant when we would
10379 do maybe-rvalue overload resolution even without std::move. */
10380 else if (warn_redundant_move
10381 && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
10383 /* Make sure that the overload resolution would actually succeed
10384 if we removed the std::move call. */
10385 tree t = convert_for_initialization (NULL_TREE, functype,
10386 moved,
10387 (LOOKUP_NORMAL
10388 | LOOKUP_ONLYCONVERTING
10389 | LOOKUP_PREFER_RVALUE),
10390 ICR_RETURN, NULL_TREE, 0,
10391 tf_none);
10392 /* If this worked, implicit rvalue would work, so the call to
10393 std::move is redundant. */
10394 if (t != error_mark_node)
10396 auto_diagnostic_group d;
10397 if (warning_at (loc, OPT_Wredundant_move,
10398 "redundant move in return statement"))
10399 inform (loc, "remove %<std::move%> call");
10406 /* Check that returning RETVAL from the current function is valid.
10407 Return an expression explicitly showing all conversions required to
10408 change RETVAL into the function return type, and to assign it to
10409 the DECL_RESULT for the function. Set *NO_WARNING to true if
10410 code reaches end of non-void function warning shouldn't be issued
10411 on this RETURN_EXPR. */
10413 tree
10414 check_return_expr (tree retval, bool *no_warning)
10416 tree result;
10417 /* The type actually returned by the function. */
10418 tree valtype;
10419 /* The type the function is declared to return, or void if
10420 the declared type is incomplete. */
10421 tree functype;
10422 int fn_returns_value_p;
10423 location_t loc = cp_expr_loc_or_input_loc (retval);
10425 *no_warning = false;
10427 /* A `volatile' function is one that isn't supposed to return, ever.
10428 (This is a G++ extension, used to get better code for functions
10429 that call the `volatile' function.) */
10430 if (TREE_THIS_VOLATILE (current_function_decl))
10431 warning (0, "function declared %<noreturn%> has a %<return%> statement");
10433 /* Check for various simple errors. */
10434 if (DECL_DESTRUCTOR_P (current_function_decl))
10436 if (retval)
10437 error_at (loc, "returning a value from a destructor");
10438 return NULL_TREE;
10440 else if (DECL_CONSTRUCTOR_P (current_function_decl))
10442 if (in_function_try_handler)
10443 /* If a return statement appears in a handler of the
10444 function-try-block of a constructor, the program is ill-formed. */
10445 error ("cannot return from a handler of a function-try-block of a constructor");
10446 else if (retval)
10447 /* You can't return a value from a constructor. */
10448 error_at (loc, "returning a value from a constructor");
10449 return NULL_TREE;
10452 const tree saved_retval = retval;
10454 if (processing_template_decl)
10456 current_function_returns_value = 1;
10458 if (check_for_bare_parameter_packs (retval))
10459 return error_mark_node;
10461 /* If one of the types might be void, we can't tell whether we're
10462 returning a value. */
10463 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
10464 && !FNDECL_USED_AUTO (current_function_decl))
10465 || (retval != NULL_TREE
10466 && (TREE_TYPE (retval) == NULL_TREE
10467 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
10468 goto dependent;
10471 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
10473 /* Deduce auto return type from a return statement. */
10474 if (FNDECL_USED_AUTO (current_function_decl))
10476 tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
10477 tree auto_node;
10478 tree type;
10480 if (!retval && !is_auto (pattern))
10482 /* Give a helpful error message. */
10483 error ("return-statement with no value, in function returning %qT",
10484 pattern);
10485 inform (input_location, "only plain %<auto%> return type can be "
10486 "deduced to %<void%>");
10487 type = error_mark_node;
10489 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
10491 error ("returning initializer list");
10492 type = error_mark_node;
10494 else
10496 if (!retval)
10497 retval = void_node;
10498 auto_node = type_uses_auto (pattern);
10499 type = do_auto_deduction (pattern, retval, auto_node,
10500 tf_warning_or_error, adc_return_type);
10503 if (type == error_mark_node)
10504 /* Leave it. */;
10505 else if (functype == pattern)
10506 apply_deduced_return_type (current_function_decl, type);
10507 else if (!same_type_p (type, functype))
10509 if (LAMBDA_FUNCTION_P (current_function_decl))
10510 error_at (loc, "inconsistent types %qT and %qT deduced for "
10511 "lambda return type", functype, type);
10512 else
10513 error_at (loc, "inconsistent deduction for auto return type: "
10514 "%qT and then %qT", functype, type);
10516 functype = type;
10519 result = DECL_RESULT (current_function_decl);
10520 valtype = TREE_TYPE (result);
10521 gcc_assert (valtype != NULL_TREE);
10522 fn_returns_value_p = !VOID_TYPE_P (valtype);
10524 /* Check for a return statement with no return value in a function
10525 that's supposed to return a value. */
10526 if (!retval && fn_returns_value_p)
10528 if (functype != error_mark_node)
10529 permerror (input_location, "return-statement with no value, in "
10530 "function returning %qT", valtype);
10531 /* Remember that this function did return. */
10532 current_function_returns_value = 1;
10533 /* And signal caller that TREE_NO_WARNING should be set on the
10534 RETURN_EXPR to avoid control reaches end of non-void function
10535 warnings in tree-cfg.cc. */
10536 *no_warning = true;
10538 /* Check for a return statement with a value in a function that
10539 isn't supposed to return a value. */
10540 else if (retval && !fn_returns_value_p)
10542 if (VOID_TYPE_P (TREE_TYPE (retval)))
10543 /* You can return a `void' value from a function of `void'
10544 type. In that case, we have to evaluate the expression for
10545 its side-effects. */
10546 finish_expr_stmt (retval);
10547 else if (retval != error_mark_node)
10548 permerror (loc, "return-statement with a value, in function "
10549 "returning %qT", valtype);
10550 current_function_returns_null = 1;
10552 /* There's really no value to return, after all. */
10553 return NULL_TREE;
10555 else if (!retval)
10556 /* Remember that this function can sometimes return without a
10557 value. */
10558 current_function_returns_null = 1;
10559 else
10560 /* Remember that this function did return a value. */
10561 current_function_returns_value = 1;
10563 /* Check for erroneous operands -- but after giving ourselves a
10564 chance to provide an error about returning a value from a void
10565 function. */
10566 if (error_operand_p (retval))
10568 current_function_return_value = error_mark_node;
10569 return error_mark_node;
10572 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
10573 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
10574 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
10575 && ! flag_check_new
10576 && retval && null_ptr_cst_p (retval))
10577 warning (0, "%<operator new%> must not return NULL unless it is "
10578 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
10580 /* Effective C++ rule 15. See also start_function. */
10581 if (warn_ecpp
10582 && DECL_NAME (current_function_decl) == assign_op_identifier
10583 && !type_dependent_expression_p (retval))
10585 bool warn = true;
10587 /* The function return type must be a reference to the current
10588 class. */
10589 if (TYPE_REF_P (valtype)
10590 && same_type_ignoring_top_level_qualifiers_p
10591 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
10593 /* Returning '*this' is obviously OK. */
10594 if (retval == current_class_ref)
10595 warn = false;
10596 /* If we are calling a function whose return type is the same of
10597 the current class reference, it is ok. */
10598 else if (INDIRECT_REF_P (retval)
10599 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
10600 warn = false;
10603 if (warn)
10604 warning_at (loc, OPT_Weffc__,
10605 "%<operator=%> should return a reference to %<*this%>");
10608 if (dependent_type_p (functype)
10609 || type_dependent_expression_p (retval))
10611 dependent:
10612 /* We should not have changed the return value. */
10613 gcc_assert (retval == saved_retval);
10614 /* We don't know if this is an lvalue or rvalue use, but
10615 either way we can mark it as read. */
10616 mark_exp_read (retval);
10617 return retval;
10620 /* The fabled Named Return Value optimization, as per [class.copy]/15:
10622 [...] For a function with a class return type, if the expression
10623 in the return statement is the name of a local object, and the cv-
10624 unqualified type of the local object is the same as the function
10625 return type, an implementation is permitted to omit creating the tem-
10626 porary object to hold the function return value [...]
10628 So, if this is a value-returning function that always returns the same
10629 local variable, remember it.
10631 It might be nice to be more flexible, and choose the first suitable
10632 variable even if the function sometimes returns something else, but
10633 then we run the risk of clobbering the variable we chose if the other
10634 returned expression uses the chosen variable somehow. And people expect
10635 this restriction, anyway. (jason 2000-11-19)
10637 See finish_function and finalize_nrv for the rest of this optimization. */
10638 tree bare_retval = NULL_TREE;
10639 if (retval)
10641 retval = maybe_undo_parenthesized_ref (retval);
10642 bare_retval = tree_strip_any_location_wrapper (retval);
10645 bool named_return_value_okay_p = can_do_nrvo_p (bare_retval, functype);
10646 if (fn_returns_value_p && flag_elide_constructors)
10648 if (named_return_value_okay_p
10649 && (current_function_return_value == NULL_TREE
10650 || current_function_return_value == bare_retval))
10651 current_function_return_value = bare_retval;
10652 else
10653 current_function_return_value = error_mark_node;
10656 /* We don't need to do any conversions when there's nothing being
10657 returned. */
10658 if (!retval)
10659 return NULL_TREE;
10661 if (!named_return_value_okay_p)
10662 maybe_warn_pessimizing_move (retval, functype);
10664 /* Do any required conversions. */
10665 if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
10666 /* No conversions are required. */
10668 else
10670 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
10672 /* The functype's return type will have been set to void, if it
10673 was an incomplete type. Just treat this as 'return;' */
10674 if (VOID_TYPE_P (functype))
10675 return error_mark_node;
10677 if (processing_template_decl)
10678 retval = build_non_dependent_expr (retval);
10680 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
10681 treated as an rvalue for the purposes of overload resolution to
10682 favor move constructors over copy constructors.
10684 Note that these conditions are similar to, but not as strict as,
10685 the conditions for the named return value optimization. */
10686 bool converted = false;
10687 tree moved;
10688 /* This is only interesting for class type. */
10689 if (CLASS_TYPE_P (functype)
10690 && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
10692 if (cxx_dialect < cxx20)
10694 moved = convert_for_initialization
10695 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
10696 ICR_RETURN, NULL_TREE, 0, tf_none);
10697 if (moved != error_mark_node)
10699 retval = moved;
10700 converted = true;
10703 else
10704 /* In C++20 we just treat the return value as an rvalue that
10705 can bind to lvalue refs. */
10706 retval = moved;
10709 /* The call in a (lambda) thunk needs no conversions. */
10710 if (TREE_CODE (retval) == CALL_EXPR
10711 && call_from_lambda_thunk_p (retval))
10712 converted = true;
10714 /* First convert the value to the function's return type, then
10715 to the type of return value's location to handle the
10716 case that functype is smaller than the valtype. */
10717 if (!converted)
10718 retval = convert_for_initialization
10719 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
10720 tf_warning_or_error);
10721 retval = convert (valtype, retval);
10723 /* If the conversion failed, treat this just like `return;'. */
10724 if (retval == error_mark_node)
10725 return retval;
10726 /* We can't initialize a register from a AGGR_INIT_EXPR. */
10727 else if (! cfun->returns_struct
10728 && TREE_CODE (retval) == TARGET_EXPR
10729 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
10730 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
10731 TREE_OPERAND (retval, 0));
10732 else if (!processing_template_decl
10733 && maybe_warn_about_returning_address_of_local (retval, loc)
10734 && INDIRECT_TYPE_P (valtype))
10735 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
10736 build_zero_cst (TREE_TYPE (retval)));
10739 if (processing_template_decl)
10740 return saved_retval;
10742 /* Actually copy the value returned into the appropriate location. */
10743 if (retval && retval != result)
10744 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
10746 if (tree set = maybe_set_retval_sentinel ())
10747 retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
10749 return retval;
10753 /* Returns nonzero if the pointer-type FROM can be converted to the
10754 pointer-type TO via a qualification conversion. If CONSTP is -1,
10755 then we return nonzero if the pointers are similar, and the
10756 cv-qualification signature of FROM is a proper subset of that of TO.
10758 If CONSTP is positive, then all outer pointers have been
10759 const-qualified. */
10761 static bool
10762 comp_ptr_ttypes_real (tree to, tree from, int constp)
10764 bool to_more_cv_qualified = false;
10765 bool is_opaque_pointer = false;
10767 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10769 if (TREE_CODE (to) != TREE_CODE (from))
10770 return false;
10772 if (TREE_CODE (from) == OFFSET_TYPE
10773 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
10774 TYPE_OFFSET_BASETYPE (to)))
10775 return false;
10777 /* Const and volatile mean something different for function and
10778 array types, so the usual checks are not appropriate. We'll
10779 check the array type elements in further iterations. */
10780 if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
10782 if (!at_least_as_qualified_p (to, from))
10783 return false;
10785 if (!at_least_as_qualified_p (from, to))
10787 if (constp == 0)
10788 return false;
10789 to_more_cv_qualified = true;
10792 if (constp > 0)
10793 constp &= TYPE_READONLY (to);
10796 if (VECTOR_TYPE_P (to))
10797 is_opaque_pointer = vector_targets_convertible_p (to, from);
10799 /* P0388R4 allows a conversion from int[N] to int[] but not the
10800 other way round. When both arrays have bounds but they do
10801 not match, then no conversion is possible. */
10802 if (TREE_CODE (to) == ARRAY_TYPE
10803 && !comp_array_types (to, from, bounds_first, /*strict=*/false))
10804 return false;
10806 if (!TYPE_PTR_P (to)
10807 && !TYPE_PTRDATAMEM_P (to)
10808 /* CWG 330 says we need to look through arrays. */
10809 && TREE_CODE (to) != ARRAY_TYPE)
10810 return ((constp >= 0 || to_more_cv_qualified)
10811 && (is_opaque_pointer
10812 || same_type_ignoring_top_level_qualifiers_p (to, from)));
10816 /* When comparing, say, char ** to char const **, this function takes
10817 the 'char *' and 'char const *'. Do not pass non-pointer/reference
10818 types to this function. */
10821 comp_ptr_ttypes (tree to, tree from)
10823 return comp_ptr_ttypes_real (to, from, 1);
10826 /* Returns true iff FNTYPE is a non-class type that involves
10827 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
10828 if a parameter type is ill-formed. */
10830 bool
10831 error_type_p (const_tree type)
10833 tree t;
10835 switch (TREE_CODE (type))
10837 case ERROR_MARK:
10838 return true;
10840 case POINTER_TYPE:
10841 case REFERENCE_TYPE:
10842 case OFFSET_TYPE:
10843 return error_type_p (TREE_TYPE (type));
10845 case FUNCTION_TYPE:
10846 case METHOD_TYPE:
10847 if (error_type_p (TREE_TYPE (type)))
10848 return true;
10849 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
10850 if (error_type_p (TREE_VALUE (t)))
10851 return true;
10852 return false;
10854 case RECORD_TYPE:
10855 if (TYPE_PTRMEMFUNC_P (type))
10856 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
10857 return false;
10859 default:
10860 return false;
10864 /* Returns true if to and from are (possibly multi-level) pointers to the same
10865 type or inheritance-related types, regardless of cv-quals. */
10867 bool
10868 ptr_reasonably_similar (const_tree to, const_tree from)
10870 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10872 /* Any target type is similar enough to void. */
10873 if (VOID_TYPE_P (to))
10874 return !error_type_p (from);
10875 if (VOID_TYPE_P (from))
10876 return !error_type_p (to);
10878 if (TREE_CODE (to) != TREE_CODE (from))
10879 return false;
10881 if (TREE_CODE (from) == OFFSET_TYPE
10882 && comptypes (TYPE_OFFSET_BASETYPE (to),
10883 TYPE_OFFSET_BASETYPE (from),
10884 COMPARE_BASE | COMPARE_DERIVED))
10885 continue;
10887 if (VECTOR_TYPE_P (to)
10888 && vector_types_convertible_p (to, from, false))
10889 return true;
10891 if (TREE_CODE (to) == INTEGER_TYPE
10892 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
10893 return true;
10895 if (TREE_CODE (to) == FUNCTION_TYPE)
10896 return !error_type_p (to) && !error_type_p (from);
10898 if (!TYPE_PTR_P (to))
10900 /* When either type is incomplete avoid DERIVED_FROM_P,
10901 which may call complete_type (c++/57942). */
10902 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
10903 return comptypes
10904 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
10905 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
10910 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
10911 pointer-to-member types) are the same, ignoring cv-qualification at
10912 all levels. CB says how we should behave when comparing array bounds. */
10914 bool
10915 comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
10917 bool is_opaque_pointer = false;
10919 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
10921 if (TREE_CODE (to) != TREE_CODE (from))
10922 return false;
10924 if (TREE_CODE (from) == OFFSET_TYPE
10925 && same_type_p (TYPE_OFFSET_BASETYPE (from),
10926 TYPE_OFFSET_BASETYPE (to)))
10927 continue;
10929 if (VECTOR_TYPE_P (to))
10930 is_opaque_pointer = vector_targets_convertible_p (to, from);
10932 if (TREE_CODE (to) == ARRAY_TYPE
10933 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
10934 we must fail. */
10935 && !comp_array_types (to, from, cb, /*strict=*/false))
10936 return false;
10938 /* CWG 330 says we need to look through arrays. */
10939 if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
10940 return (is_opaque_pointer
10941 || same_type_ignoring_top_level_qualifiers_p (to, from));
10945 /* Returns the type qualifiers for this type, including the qualifiers on the
10946 elements for an array type. */
10949 cp_type_quals (const_tree type)
10951 int quals;
10952 /* This CONST_CAST is okay because strip_array_types returns its
10953 argument unmodified and we assign it to a const_tree. */
10954 type = strip_array_types (CONST_CAST_TREE (type));
10955 if (type == error_mark_node
10956 /* Quals on a FUNCTION_TYPE are memfn quals. */
10957 || TREE_CODE (type) == FUNCTION_TYPE)
10958 return TYPE_UNQUALIFIED;
10959 quals = TYPE_QUALS (type);
10960 /* METHOD and REFERENCE_TYPEs should never have quals. */
10961 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
10962 && !TYPE_REF_P (type))
10963 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
10964 == TYPE_UNQUALIFIED));
10965 return quals;
10968 /* Returns the function-ref-qualifier for TYPE */
10970 cp_ref_qualifier
10971 type_memfn_rqual (const_tree type)
10973 gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
10975 if (!FUNCTION_REF_QUALIFIED (type))
10976 return REF_QUAL_NONE;
10977 else if (FUNCTION_RVALUE_QUALIFIED (type))
10978 return REF_QUAL_RVALUE;
10979 else
10980 return REF_QUAL_LVALUE;
10983 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
10984 METHOD_TYPE. */
10987 type_memfn_quals (const_tree type)
10989 if (TREE_CODE (type) == FUNCTION_TYPE)
10990 return TYPE_QUALS (type);
10991 else if (TREE_CODE (type) == METHOD_TYPE)
10992 return cp_type_quals (class_of_this_parm (type));
10993 else
10994 gcc_unreachable ();
10997 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
10998 MEMFN_QUALS and its ref-qualifier to RQUAL. */
11000 tree
11001 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11003 /* Could handle METHOD_TYPE here if necessary. */
11004 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11005 if (TYPE_QUALS (type) == memfn_quals
11006 && type_memfn_rqual (type) == rqual)
11007 return type;
11009 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11010 complex. */
11011 tree result = build_qualified_type (type, memfn_quals);
11012 return build_ref_qualified_type (result, rqual);
11015 /* Returns nonzero if TYPE is const or volatile. */
11017 bool
11018 cv_qualified_p (const_tree type)
11020 int quals = cp_type_quals (type);
11021 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11024 /* Returns nonzero if the TYPE contains a mutable member. */
11026 bool
11027 cp_has_mutable_p (const_tree type)
11029 /* This CONST_CAST is okay because strip_array_types returns its
11030 argument unmodified and we assign it to a const_tree. */
11031 type = strip_array_types (CONST_CAST_TREE(type));
11033 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11036 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11037 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11038 approximation. In particular, consider:
11040 int f();
11041 struct S { int i; };
11042 const S s = { f(); }
11044 Here, we will make "s" as TREE_READONLY (because it is declared
11045 "const") -- only to reverse ourselves upon seeing that the
11046 initializer is non-constant. */
11048 void
11049 cp_apply_type_quals_to_decl (int type_quals, tree decl)
11051 tree type = TREE_TYPE (decl);
11053 if (type == error_mark_node)
11054 return;
11056 if (TREE_CODE (decl) == TYPE_DECL)
11057 return;
11059 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11060 && type_quals != TYPE_UNQUALIFIED));
11062 /* Avoid setting TREE_READONLY incorrectly. */
11063 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11064 constructor can produce constant init, so rely on cp_finish_decl to
11065 clear TREE_READONLY if the variable has non-constant init. */
11067 /* If the type has (or might have) a mutable component, that component
11068 might be modified. */
11069 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11070 type_quals &= ~TYPE_QUAL_CONST;
11072 c_apply_type_quals_to_decl (type_quals, decl);
11075 /* Subroutine of casts_away_constness. Make T1 and T2 point at
11076 exemplar types such that casting T1 to T2 is casting away constness
11077 if and only if there is no implicit conversion from T1 to T2. */
11079 static void
11080 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11082 int quals1;
11083 int quals2;
11085 /* [expr.const.cast]
11087 For multi-level pointer to members and multi-level mixed pointers
11088 and pointers to members (conv.qual), the "member" aspect of a
11089 pointer to member level is ignored when determining if a const
11090 cv-qualifier has been cast away. */
11091 /* [expr.const.cast]
11093 For two pointer types:
11095 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11096 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11097 K is min(N,M)
11099 casting from X1 to X2 casts away constness if, for a non-pointer
11100 type T there does not exist an implicit conversion (clause
11101 _conv_) from:
11103 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11107 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11108 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11109 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11111 *t1 = cp_build_qualified_type (void_type_node,
11112 cp_type_quals (*t1));
11113 *t2 = cp_build_qualified_type (void_type_node,
11114 cp_type_quals (*t2));
11115 return;
11118 quals1 = cp_type_quals (*t1);
11119 quals2 = cp_type_quals (*t2);
11121 if (TYPE_PTRDATAMEM_P (*t1))
11122 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11123 else
11124 *t1 = TREE_TYPE (*t1);
11125 if (TYPE_PTRDATAMEM_P (*t2))
11126 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11127 else
11128 *t2 = TREE_TYPE (*t2);
11130 casts_away_constness_r (t1, t2, complain);
11131 *t1 = build_pointer_type (*t1);
11132 *t2 = build_pointer_type (*t2);
11133 *t1 = cp_build_qualified_type (*t1, quals1);
11134 *t2 = cp_build_qualified_type (*t2, quals2);
11137 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11138 constness.
11140 ??? This function returns non-zero if casting away qualifiers not
11141 just const. We would like to return to the caller exactly which
11142 qualifiers are casted away to give more accurate diagnostics.
11145 static bool
11146 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
11148 if (TYPE_REF_P (t2))
11150 /* [expr.const.cast]
11152 Casting from an lvalue of type T1 to an lvalue of type T2
11153 using a reference cast casts away constness if a cast from an
11154 rvalue of type "pointer to T1" to the type "pointer to T2"
11155 casts away constness. */
11156 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
11157 return casts_away_constness (build_pointer_type (t1),
11158 build_pointer_type (TREE_TYPE (t2)),
11159 complain);
11162 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
11163 /* [expr.const.cast]
11165 Casting from an rvalue of type "pointer to data member of X
11166 of type T1" to the type "pointer to data member of Y of type
11167 T2" casts away constness if a cast from an rvalue of type
11168 "pointer to T1" to the type "pointer to T2" casts away
11169 constness. */
11170 return casts_away_constness
11171 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
11172 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
11173 complain);
11175 /* Casting away constness is only something that makes sense for
11176 pointer or reference types. */
11177 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
11178 return false;
11180 /* Top-level qualifiers don't matter. */
11181 t1 = TYPE_MAIN_VARIANT (t1);
11182 t2 = TYPE_MAIN_VARIANT (t2);
11183 casts_away_constness_r (&t1, &t2, complain);
11184 if (!can_convert (t2, t1, complain))
11185 return true;
11187 return false;
11190 /* If T is a REFERENCE_TYPE return the type to which T refers.
11191 Otherwise, return T itself. */
11193 tree
11194 non_reference (tree t)
11196 if (t && TYPE_REF_P (t))
11197 t = TREE_TYPE (t);
11198 return t;
11202 /* Return nonzero if REF is an lvalue valid for this language;
11203 otherwise, print an error message and return zero. USE says
11204 how the lvalue is being used and so selects the error message. */
11207 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
11209 cp_lvalue_kind kind = lvalue_kind (ref);
11211 if (kind == clk_none)
11213 if (complain & tf_error)
11214 lvalue_error (cp_expr_loc_or_input_loc (ref), use);
11215 return 0;
11217 else if (kind & (clk_rvalueref|clk_class))
11219 if (!(complain & tf_error))
11220 return 0;
11221 /* Make this a permerror because we used to accept it. */
11222 permerror (cp_expr_loc_or_input_loc (ref),
11223 "using rvalue as lvalue");
11225 return 1;
11228 /* Return true if a user-defined literal operator is a raw operator. */
11230 bool
11231 check_raw_literal_operator (const_tree decl)
11233 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11234 tree argtype;
11235 int arity;
11236 bool maybe_raw_p = false;
11238 /* Count the number and type of arguments and check for ellipsis. */
11239 for (argtype = argtypes, arity = 0;
11240 argtype && argtype != void_list_node;
11241 ++arity, argtype = TREE_CHAIN (argtype))
11243 tree t = TREE_VALUE (argtype);
11245 if (same_type_p (t, const_string_type_node))
11246 maybe_raw_p = true;
11248 if (!argtype)
11249 return false; /* Found ellipsis. */
11251 if (!maybe_raw_p || arity != 1)
11252 return false;
11254 return true;
11258 /* Return true if a user-defined literal operator has one of the allowed
11259 argument types. */
11261 bool
11262 check_literal_operator_args (const_tree decl,
11263 bool *long_long_unsigned_p, bool *long_double_p)
11265 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11267 *long_long_unsigned_p = false;
11268 *long_double_p = false;
11269 if (processing_template_decl || processing_specialization)
11270 return argtypes == void_list_node;
11271 else
11273 tree argtype;
11274 int arity;
11275 int max_arity = 2;
11277 /* Count the number and type of arguments and check for ellipsis. */
11278 for (argtype = argtypes, arity = 0;
11279 argtype && argtype != void_list_node;
11280 argtype = TREE_CHAIN (argtype))
11282 tree t = TREE_VALUE (argtype);
11283 ++arity;
11285 if (TYPE_PTR_P (t))
11287 bool maybe_raw_p = false;
11288 t = TREE_TYPE (t);
11289 if (cp_type_quals (t) != TYPE_QUAL_CONST)
11290 return false;
11291 t = TYPE_MAIN_VARIANT (t);
11292 if ((maybe_raw_p = same_type_p (t, char_type_node))
11293 || same_type_p (t, wchar_type_node)
11294 || same_type_p (t, char8_type_node)
11295 || same_type_p (t, char16_type_node)
11296 || same_type_p (t, char32_type_node))
11298 argtype = TREE_CHAIN (argtype);
11299 if (!argtype)
11300 return false;
11301 t = TREE_VALUE (argtype);
11302 if (maybe_raw_p && argtype == void_list_node)
11303 return true;
11304 else if (same_type_p (t, size_type_node))
11306 ++arity;
11307 continue;
11309 else
11310 return false;
11313 else if (same_type_p (t, long_long_unsigned_type_node))
11315 max_arity = 1;
11316 *long_long_unsigned_p = true;
11318 else if (same_type_p (t, long_double_type_node))
11320 max_arity = 1;
11321 *long_double_p = true;
11323 else if (same_type_p (t, char_type_node))
11324 max_arity = 1;
11325 else if (same_type_p (t, wchar_type_node))
11326 max_arity = 1;
11327 else if (same_type_p (t, char8_type_node))
11328 max_arity = 1;
11329 else if (same_type_p (t, char16_type_node))
11330 max_arity = 1;
11331 else if (same_type_p (t, char32_type_node))
11332 max_arity = 1;
11333 else
11334 return false;
11336 if (!argtype)
11337 return false; /* Found ellipsis. */
11339 if (arity != max_arity)
11340 return false;
11342 return true;
11346 /* Always returns false since unlike C90, C++ has no concept of implicit
11347 function declarations. */
11349 bool
11350 c_decl_implicit (const_tree)
11352 return false;