Daily bump.
[official-gcc.git] / gcc / cp / typeck.cc
blob717eb63eb9850b12dcb9b05079f638e24e352973
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2024 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 "c-family/c-type-mismatch.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 (tree value,
75 tsubst_flags_t complain /* = tf_warning_or_error */)
77 tree type;
79 if (processing_template_decl || value == error_mark_node)
80 return value;
82 if (TREE_CODE (value) == OVERLOAD)
83 type = unknown_type_node;
84 else
85 type = TREE_TYPE (value);
87 if (type == error_mark_node)
88 return error_mark_node;
90 /* First, detect a valid value with a complete type. */
91 if (COMPLETE_TYPE_P (type))
92 return value;
94 if (complete_type_or_maybe_complain (type, value, complain))
95 return value;
96 else
97 return error_mark_node;
100 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
101 a template instantiation, do the instantiation. Returns TYPE,
102 whether or not it could be completed, unless something goes
103 horribly wrong, in which case the error_mark_node is returned. */
105 tree
106 complete_type (tree type)
108 if (type == NULL_TREE)
109 /* Rather than crash, we return something sure to cause an error
110 at some point. */
111 return error_mark_node;
113 if (type == error_mark_node || COMPLETE_TYPE_P (type))
115 else if (TREE_CODE (type) == ARRAY_TYPE)
117 tree t = complete_type (TREE_TYPE (type));
118 unsigned int needs_constructing, has_nontrivial_dtor;
119 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
120 layout_type (type);
121 needs_constructing
122 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 has_nontrivial_dtor
124 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
125 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
127 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
128 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
131 else if (CLASS_TYPE_P (type))
133 if (modules_p ())
134 /* TYPE could be a class member we've not loaded the definition of. */
135 lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
137 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 instantiate_class_template (TYPE_MAIN_VARIANT (type));
141 return type;
144 /* Like complete_type, but issue an error if the TYPE cannot be completed.
145 VALUE is used for informative diagnostics.
146 Returns NULL_TREE if the type cannot be made complete. */
148 tree
149 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
151 type = complete_type (type);
152 if (type == error_mark_node)
153 /* We already issued an error. */
154 return NULL_TREE;
155 else if (!COMPLETE_TYPE_P (type))
157 if (complain & tf_error)
158 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
159 note_failed_type_completion_for_satisfaction (type);
160 return NULL_TREE;
162 else
163 return type;
166 tree
167 complete_type_or_else (tree type, tree value)
169 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
173 /* Return the common type of two parameter lists.
174 We assume that comptypes has already been done and returned 1;
175 if that isn't so, this may crash.
177 As an optimization, free the space we allocate if the parameter
178 lists are already common. */
180 static tree
181 commonparms (tree p1, tree p2)
183 tree oldargs = p1, newargs, n;
184 int i, len;
185 int any_change = 0;
187 len = list_length (p1);
188 newargs = tree_last (p1);
190 if (newargs == void_list_node)
191 i = 1;
192 else
194 i = 0;
195 newargs = 0;
198 for (; i < len; i++)
199 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
201 n = newargs;
203 for (i = 0; p1;
204 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
206 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
208 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
209 any_change = 1;
211 else if (! TREE_PURPOSE (p1))
213 if (TREE_PURPOSE (p2))
215 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
216 any_change = 1;
219 else
221 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
222 any_change = 1;
223 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
225 if (TREE_VALUE (p1) != TREE_VALUE (p2))
227 any_change = 1;
228 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
230 else
231 TREE_VALUE (n) = TREE_VALUE (p1);
233 if (! any_change)
234 return oldargs;
236 return newargs;
239 /* Given a type, perhaps copied for a typedef,
240 find the "original" version of it. */
241 static tree
242 original_type (tree t)
244 int quals = cp_type_quals (t);
245 while (t != error_mark_node
246 && TYPE_NAME (t) != NULL_TREE)
248 tree x = TYPE_NAME (t);
249 if (TREE_CODE (x) != TYPE_DECL)
250 break;
251 x = DECL_ORIGINAL_TYPE (x);
252 if (x == NULL_TREE)
253 break;
254 t = x;
256 return cp_build_qualified_type (t, quals);
259 /* Merge the attributes of type OTHER_TYPE into the attributes of type TYPE
260 and return a variant of TYPE with the merged attributes. */
262 static tree
263 merge_type_attributes_from (tree type, tree other_type)
265 tree attrs = targetm.merge_type_attributes (type, other_type);
266 attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
267 return cp_build_type_attribute_variant (type, attrs);
270 /* Compare floating point conversion ranks and subranks of T1 and T2
271 types. If T1 and T2 have unordered conversion ranks, return 3.
272 If T1 has greater conversion rank than T2, return 2.
273 If T2 has greater conversion rank than T1, return -2.
274 If T1 has equal conversion rank as T2, return -1, 0 or 1 depending
275 on if T1 has smaller, equal or greater conversion subrank than
276 T2. */
279 cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
281 tree mv1 = TYPE_MAIN_VARIANT (t1);
282 tree mv2 = TYPE_MAIN_VARIANT (t2);
283 int extended1 = 0;
284 int extended2 = 0;
286 if (mv1 == mv2)
287 return 0;
289 for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
291 if (mv1 == FLOATN_NX_TYPE_NODE (i))
292 extended1 = i + 1;
293 if (mv2 == FLOATN_NX_TYPE_NODE (i))
294 extended2 = i + 1;
296 if (mv1 == bfloat16_type_node)
297 extended1 = true;
298 if (mv2 == bfloat16_type_node)
299 extended2 = true;
300 if (extended2 && !extended1)
302 int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
303 return ret == 3 ? 3 : -ret;
306 const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
307 const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
308 gcc_assert (fmt1->b == 2 && fmt2->b == 2);
309 /* For {ibm,mips}_extended_format formats, the type has variable
310 precision up to ~2150 bits when the first double is around maximum
311 representable double and second double is subnormal minimum.
312 So, e.g. for __ibm128 vs. std::float128_t, they have unordered
313 ranks. */
314 int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
315 ? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
316 int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
317 ? fmt2->emax - fmt2->emin + fmt2->p - 1 : fmt2->p);
318 /* The rank of a floating point type T is greater than the rank of
319 any floating-point type whose set of values is a proper subset
320 of the set of values of T. */
321 if ((p1 > p2 && fmt1->emax >= fmt2->emax)
322 || (p1 == p2 && fmt1->emax > fmt2->emax))
323 return 2;
324 if ((p1 < p2 && fmt1->emax <= fmt2->emax)
325 || (p1 == p2 && fmt1->emax < fmt2->emax))
326 return -2;
327 if ((p1 > p2 && fmt1->emax < fmt2->emax)
328 || (p1 < p2 && fmt1->emax > fmt2->emax))
329 return 3;
330 if (!extended1 && !extended2)
332 /* The rank of long double is greater than the rank of double, which
333 is greater than the rank of float. */
334 if (t1 == long_double_type_node)
335 return 2;
336 else if (t2 == long_double_type_node)
337 return -2;
338 if (t1 == double_type_node)
339 return 2;
340 else if (t2 == double_type_node)
341 return -2;
342 if (t1 == float_type_node)
343 return 2;
344 else if (t2 == float_type_node)
345 return -2;
346 return 0;
348 /* Two extended floating-point types with the same set of values have equal
349 ranks. */
350 if (extended1 && extended2)
352 if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES))
354 /* Prefer higher extendedN value. */
355 if (extended1 > extended2)
356 return 1;
357 else if (extended1 < extended2)
358 return -1;
359 else
360 return 0;
362 else if (extended1 <= NUM_FLOATN_TYPES)
363 /* Prefer _FloatN type over _FloatMx type. */
364 return 1;
365 else if (extended2 <= NUM_FLOATN_TYPES)
366 return -1;
367 else
368 return 0;
371 /* gcc_assert (extended1 && !extended2); */
372 tree *p;
373 int cnt = 0;
374 for (p = &float_type_node; p <= &long_double_type_node; ++p)
376 const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
377 gcc_assert (fmt3->b == 2);
378 int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
379 ? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
380 if (p1 == p3 && fmt1->emax == fmt3->emax)
381 ++cnt;
383 /* An extended floating-point type with the same set of values
384 as exactly one cv-unqualified standard floating-point type
385 has a rank equal to the rank of that standard floating-point
386 type.
388 An extended floating-point type with the same set of values
389 as more than one cv-unqualified standard floating-point type
390 has a rank equal to the rank of double.
392 Thus, if the latter is true and t2 is long double, t2
393 has higher rank. */
394 if (cnt > 1 && mv2 == long_double_type_node)
395 return -2;
396 /* And similarly if t2 is float, t2 has lower rank. */
397 if (cnt > 1 && mv2 == float_type_node)
398 return 2;
399 /* Otherwise, they have equal rank, but extended types
400 (other than std::bfloat16_t) have higher subrank.
401 std::bfloat16_t shouldn't have equal rank to any standard
402 floating point type. */
403 return 1;
406 /* Return the common type for two arithmetic types T1 and T2 under the
407 usual arithmetic conversions. The default conversions have already
408 been applied, and enumerated types converted to their compatible
409 integer types. */
411 static tree
412 cp_common_type (tree t1, tree t2)
414 enum tree_code code1 = TREE_CODE (t1);
415 enum tree_code code2 = TREE_CODE (t2);
416 tree attributes;
417 int i;
420 /* In what follows, we slightly generalize the rules given in [expr] so
421 as to deal with `long long' and `complex'. First, merge the
422 attributes. */
423 attributes = (*targetm.merge_type_attributes) (t1, t2);
425 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
427 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
428 return build_type_attribute_variant (t1, attributes);
429 else
430 return NULL_TREE;
433 /* FIXME: Attributes. */
434 gcc_assert (ARITHMETIC_TYPE_P (t1)
435 || VECTOR_TYPE_P (t1)
436 || UNSCOPED_ENUM_P (t1));
437 gcc_assert (ARITHMETIC_TYPE_P (t2)
438 || VECTOR_TYPE_P (t2)
439 || UNSCOPED_ENUM_P (t2));
441 /* If one type is complex, form the common type of the non-complex
442 components, then make that complex. Use T1 or T2 if it is the
443 required type. */
444 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
446 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
447 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
448 tree subtype
449 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
451 if (subtype == error_mark_node)
452 return subtype;
453 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
454 return build_type_attribute_variant (t1, attributes);
455 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
456 return build_type_attribute_variant (t2, attributes);
457 else
458 return build_type_attribute_variant (build_complex_type (subtype),
459 attributes);
462 if (code1 == VECTOR_TYPE)
464 /* When we get here we should have two vectors of the same size.
465 Just prefer the unsigned one if present. */
466 if (TYPE_UNSIGNED (t1))
467 return merge_type_attributes_from (t1, t2);
468 else
469 return merge_type_attributes_from (t2, t1);
472 /* If only one is real, use it as the result. */
473 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
474 return build_type_attribute_variant (t1, attributes);
475 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
476 return build_type_attribute_variant (t2, attributes);
478 if (code1 == REAL_TYPE
479 && (extended_float_type_p (t1) || extended_float_type_p (t2)))
481 tree mv1 = TYPE_MAIN_VARIANT (t1);
482 tree mv2 = TYPE_MAIN_VARIANT (t2);
483 if (mv1 == mv2)
484 return build_type_attribute_variant (t1, attributes);
486 int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
487 if (cmpret == 3)
488 return error_mark_node;
489 else if (cmpret >= 0)
490 return build_type_attribute_variant (t1, attributes);
491 else
492 return build_type_attribute_variant (t2, attributes);
495 /* Both real or both integers; use the one with greater precision. */
496 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
497 return build_type_attribute_variant (t1, attributes);
498 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
499 return build_type_attribute_variant (t2, attributes);
501 /* The types are the same; no need to do anything fancy. */
502 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
503 return build_type_attribute_variant (t1, attributes);
505 if (code1 != REAL_TYPE)
507 /* If one is unsigned long long, then convert the other to unsigned
508 long long. */
509 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
510 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
511 return build_type_attribute_variant (long_long_unsigned_type_node,
512 attributes);
513 /* If one is a long long, and the other is an unsigned long, and
514 long long can represent all the values of an unsigned long, then
515 convert to a long long. Otherwise, convert to an unsigned long
516 long. Otherwise, if either operand is long long, convert the
517 other to long long.
519 Since we're here, we know the TYPE_PRECISION is the same;
520 therefore converting to long long cannot represent all the values
521 of an unsigned long, so we choose unsigned long long in that
522 case. */
523 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
524 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
526 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
527 ? long_long_unsigned_type_node
528 : long_long_integer_type_node);
529 return build_type_attribute_variant (t, attributes);
532 /* Go through the same procedure, but for longs. */
533 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
534 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
535 return build_type_attribute_variant (long_unsigned_type_node,
536 attributes);
537 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
538 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
540 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
541 ? long_unsigned_type_node : long_integer_type_node);
542 return build_type_attribute_variant (t, attributes);
545 /* For __intN types, either the type is __int128 (and is lower
546 priority than the types checked above, but higher than other
547 128-bit types) or it's known to not be the same size as other
548 types (enforced in toplev.cc). Prefer the unsigned type. */
549 for (i = 0; i < NUM_INT_N_ENTS; i ++)
551 if (int_n_enabled_p [i]
552 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
553 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
554 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
555 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
557 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
558 ? int_n_trees[i].unsigned_type
559 : int_n_trees[i].signed_type);
560 return build_type_attribute_variant (t, attributes);
564 /* Otherwise prefer the unsigned one. */
565 if (TYPE_UNSIGNED (t1))
566 return build_type_attribute_variant (t1, attributes);
567 else
568 return build_type_attribute_variant (t2, attributes);
570 else
572 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
573 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
574 return build_type_attribute_variant (long_double_type_node,
575 attributes);
576 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
577 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
578 return build_type_attribute_variant (double_type_node,
579 attributes);
580 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
581 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
582 return build_type_attribute_variant (float_type_node,
583 attributes);
585 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
586 the standard C++ floating-point types. Logic earlier in this
587 function has already eliminated the possibility that
588 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
589 compelling reason to choose one or the other. */
590 return build_type_attribute_variant (t1, attributes);
594 /* T1 and T2 are arithmetic or enumeration types. Return the type
595 that will result from the "usual arithmetic conversions" on T1 and
596 T2 as described in [expr]. */
598 tree
599 type_after_usual_arithmetic_conversions (tree t1, tree t2)
601 gcc_assert (ARITHMETIC_TYPE_P (t1)
602 || VECTOR_TYPE_P (t1)
603 || UNSCOPED_ENUM_P (t1));
604 gcc_assert (ARITHMETIC_TYPE_P (t2)
605 || VECTOR_TYPE_P (t2)
606 || UNSCOPED_ENUM_P (t2));
608 /* Perform the integral promotions. We do not promote real types here. */
609 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
610 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
612 t1 = type_promotes_to (t1);
613 t2 = type_promotes_to (t2);
616 return cp_common_type (t1, t2);
619 static void
620 composite_pointer_error (const op_location_t &location,
621 diagnostic_t kind, tree t1, tree t2,
622 composite_pointer_operation operation)
624 switch (operation)
626 case CPO_COMPARISON:
627 emit_diagnostic (kind, location, 0,
628 "comparison between "
629 "distinct pointer types %qT and %qT lacks a cast",
630 t1, t2);
631 break;
632 case CPO_CONVERSION:
633 emit_diagnostic (kind, location, 0,
634 "conversion between "
635 "distinct pointer types %qT and %qT lacks a cast",
636 t1, t2);
637 break;
638 case CPO_CONDITIONAL_EXPR:
639 emit_diagnostic (kind, location, 0,
640 "conditional expression between "
641 "distinct pointer types %qT and %qT lacks a cast",
642 t1, t2);
643 break;
644 default:
645 gcc_unreachable ();
649 /* Subroutine of composite_pointer_type to implement the recursive
650 case. See that function for documentation of the parameters. And ADD_CONST
651 is used to track adding "const" where needed. */
653 static tree
654 composite_pointer_type_r (const op_location_t &location,
655 tree t1, tree t2, bool *add_const,
656 composite_pointer_operation operation,
657 tsubst_flags_t complain)
659 tree pointee1;
660 tree pointee2;
661 tree result_type;
662 tree attributes;
664 /* Determine the types pointed to by T1 and T2. */
665 if (TYPE_PTR_P (t1))
667 pointee1 = TREE_TYPE (t1);
668 pointee2 = TREE_TYPE (t2);
670 else
672 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
673 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
676 /* [expr.type]
678 If T1 and T2 are similar types, the result is the cv-combined type of
679 T1 and T2. */
680 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
681 result_type = pointee1;
682 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
683 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
685 result_type = composite_pointer_type_r (location, pointee1, pointee2,
686 add_const, operation, complain);
687 if (result_type == error_mark_node)
688 return error_mark_node;
690 else
692 if (complain & tf_error)
693 composite_pointer_error (location, DK_PERMERROR,
694 t1, t2, operation);
695 else
696 return error_mark_node;
697 result_type = void_type_node;
699 const int q1 = cp_type_quals (pointee1);
700 const int q2 = cp_type_quals (pointee2);
701 const int quals = q1 | q2;
702 result_type = cp_build_qualified_type (result_type,
703 (quals | (*add_const
704 ? TYPE_QUAL_CONST
705 : TYPE_UNQUALIFIED)));
706 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
707 the TLQ). The reason is that both T1 and T2 can then be converted to the
708 cv-combined type of T1 and T2. */
709 if (quals != q1 || quals != q2)
710 *add_const = true;
711 /* If the original types were pointers to members, so is the
712 result. */
713 if (TYPE_PTRMEM_P (t1))
715 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
716 TYPE_PTRMEM_CLASS_TYPE (t2)))
718 if (complain & tf_error)
719 composite_pointer_error (location, DK_PERMERROR,
720 t1, t2, operation);
721 else
722 return error_mark_node;
724 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
725 result_type);
727 else
728 result_type = build_pointer_type (result_type);
730 /* Merge the attributes. */
731 attributes = (*targetm.merge_type_attributes) (t1, t2);
732 return build_type_attribute_variant (result_type, attributes);
735 /* Return the composite pointer type (see [expr.type]) for T1 and T2.
736 ARG1 and ARG2 are the values with those types. The OPERATION is to
737 describe the operation between the pointer types,
738 in case an error occurs.
740 This routine also implements the computation of a common type for
741 pointers-to-members as per [expr.eq]. */
743 tree
744 composite_pointer_type (const op_location_t &location,
745 tree t1, tree t2, tree arg1, tree arg2,
746 composite_pointer_operation operation,
747 tsubst_flags_t complain)
749 tree class1;
750 tree class2;
752 /* [expr.type]
754 If one operand is a null pointer constant, the composite pointer
755 type is the type of the other operand. */
756 if (null_ptr_cst_p (arg1))
757 return t2;
758 if (null_ptr_cst_p (arg2))
759 return t1;
761 /* We have:
763 [expr.type]
765 If one of the operands has type "pointer to cv1 void", then
766 the other has type "pointer to cv2 T", and the composite pointer
767 type is "pointer to cv12 void", where cv12 is the union of cv1
768 and cv2.
770 If either type is a pointer to void, make sure it is T1. */
771 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
772 std::swap (t1, t2);
774 /* Now, if T1 is a pointer to void, merge the qualifiers. */
775 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
777 tree attributes;
778 tree result_type;
780 if (TYPE_PTRFN_P (t2))
782 if (complain & tf_error)
784 switch (operation)
786 case CPO_COMPARISON:
787 pedwarn (location, OPT_Wpedantic,
788 "ISO C++ forbids comparison between pointer "
789 "of type %<void *%> and pointer-to-function");
790 break;
791 case CPO_CONVERSION:
792 pedwarn (location, OPT_Wpedantic,
793 "ISO C++ forbids conversion between pointer "
794 "of type %<void *%> and pointer-to-function");
795 break;
796 case CPO_CONDITIONAL_EXPR:
797 pedwarn (location, OPT_Wpedantic,
798 "ISO C++ forbids conditional expression between "
799 "pointer of type %<void *%> and "
800 "pointer-to-function");
801 break;
802 default:
803 gcc_unreachable ();
806 else
807 return error_mark_node;
809 result_type
810 = cp_build_qualified_type (void_type_node,
811 (cp_type_quals (TREE_TYPE (t1))
812 | cp_type_quals (TREE_TYPE (t2))));
813 result_type = build_pointer_type (result_type);
814 /* Merge the attributes. */
815 attributes = (*targetm.merge_type_attributes) (t1, t2);
816 return build_type_attribute_variant (result_type, attributes);
819 if (c_dialect_objc () && TYPE_PTR_P (t1)
820 && TYPE_PTR_P (t2))
822 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
823 return objc_common_type (t1, t2);
826 /* if T1 or T2 is "pointer to noexcept function" and the other type is
827 "pointer to function", where the function types are otherwise the same,
828 "pointer to function" */
829 if (fnptr_conv_p (t1, t2))
830 return t1;
831 if (fnptr_conv_p (t2, t1))
832 return t2;
834 /* [expr.eq] permits the application of a pointer conversion to
835 bring the pointers to a common type. */
836 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
837 && CLASS_TYPE_P (TREE_TYPE (t1))
838 && CLASS_TYPE_P (TREE_TYPE (t2))
839 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
840 TREE_TYPE (t2)))
842 class1 = TREE_TYPE (t1);
843 class2 = TREE_TYPE (t2);
845 if (DERIVED_FROM_P (class1, class2))
846 t2 = (build_pointer_type
847 (cp_build_qualified_type (class1, cp_type_quals (class2))));
848 else if (DERIVED_FROM_P (class2, class1))
849 t1 = (build_pointer_type
850 (cp_build_qualified_type (class2, cp_type_quals (class1))));
851 else
853 if (complain & tf_error)
854 composite_pointer_error (location, DK_ERROR, t1, t2, operation);
855 return error_mark_node;
858 /* [expr.eq] permits the application of a pointer-to-member
859 conversion to change the class type of one of the types. */
860 else if (TYPE_PTRMEM_P (t1)
861 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
862 TYPE_PTRMEM_CLASS_TYPE (t2)))
864 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
865 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
867 if (DERIVED_FROM_P (class1, class2))
868 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
869 else if (DERIVED_FROM_P (class2, class1))
870 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
871 else
873 if (complain & tf_error)
874 switch (operation)
876 case CPO_COMPARISON:
877 error_at (location, "comparison between distinct "
878 "pointer-to-member types %qT and %qT lacks a cast",
879 t1, t2);
880 break;
881 case CPO_CONVERSION:
882 error_at (location, "conversion between distinct "
883 "pointer-to-member types %qT and %qT lacks a cast",
884 t1, t2);
885 break;
886 case CPO_CONDITIONAL_EXPR:
887 error_at (location, "conditional expression between distinct "
888 "pointer-to-member types %qT and %qT lacks a cast",
889 t1, t2);
890 break;
891 default:
892 gcc_unreachable ();
894 return error_mark_node;
898 bool add_const = false;
899 return composite_pointer_type_r (location, t1, t2, &add_const, operation,
900 complain);
903 /* Return the merged type of two types.
904 We assume that comptypes has already been done and returned 1;
905 if that isn't so, this may crash.
907 This just combines attributes and default arguments; any other
908 differences would cause the two types to compare unalike. */
910 tree
911 merge_types (tree t1, tree t2)
913 enum tree_code code1;
914 enum tree_code code2;
915 tree attributes;
917 /* Save time if the two types are the same. */
918 if (t1 == t2)
919 return t1;
920 if (original_type (t1) == original_type (t2))
921 return t1;
923 /* If one type is nonsense, use the other. */
924 if (t1 == error_mark_node)
925 return t2;
926 if (t2 == error_mark_node)
927 return t1;
929 /* Handle merging an auto redeclaration with a previous deduced
930 return type. */
931 if (is_auto (t1))
932 return t2;
934 /* Merge the attributes. */
935 attributes = (*targetm.merge_type_attributes) (t1, t2);
937 if (TYPE_PTRMEMFUNC_P (t1))
938 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
939 if (TYPE_PTRMEMFUNC_P (t2))
940 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
942 code1 = TREE_CODE (t1);
943 code2 = TREE_CODE (t2);
944 if (code1 != code2)
946 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
947 if (code1 == TYPENAME_TYPE)
949 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
950 code1 = TREE_CODE (t1);
952 else
954 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
955 code2 = TREE_CODE (t2);
959 switch (code1)
961 case POINTER_TYPE:
962 case REFERENCE_TYPE:
963 /* For two pointers, do this recursively on the target type. */
965 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
966 int quals = cp_type_quals (t1);
968 if (code1 == POINTER_TYPE)
970 t1 = build_pointer_type (target);
971 if (TREE_CODE (target) == METHOD_TYPE)
972 t1 = build_ptrmemfunc_type (t1);
974 else
975 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
976 t1 = build_type_attribute_variant (t1, attributes);
977 t1 = cp_build_qualified_type (t1, quals);
979 return t1;
982 case OFFSET_TYPE:
984 int quals;
985 tree pointee;
986 quals = cp_type_quals (t1);
987 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
988 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
989 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
990 pointee);
991 t1 = cp_build_qualified_type (t1, quals);
992 break;
995 case ARRAY_TYPE:
997 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
998 /* Save space: see if the result is identical to one of the args. */
999 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
1000 return build_type_attribute_variant (t1, attributes);
1001 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
1002 return build_type_attribute_variant (t2, attributes);
1003 /* Merge the element types, and have a size if either arg has one. */
1004 t1 = build_cplus_array_type
1005 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
1006 break;
1009 case FUNCTION_TYPE:
1010 /* Function types: prefer the one that specified arg types.
1011 If both do, merge the arg types. Also merge the return types. */
1013 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
1014 tree p1 = TYPE_ARG_TYPES (t1);
1015 tree p2 = TYPE_ARG_TYPES (t2);
1016 tree parms;
1018 /* Save space: see if the result is identical to one of the args. */
1019 if (valtype == TREE_TYPE (t1) && ! p2)
1020 return cp_build_type_attribute_variant (t1, attributes);
1021 if (valtype == TREE_TYPE (t2) && ! p1)
1022 return cp_build_type_attribute_variant (t2, attributes);
1024 /* Simple way if one arg fails to specify argument types. */
1025 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
1026 parms = p2;
1027 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
1028 parms = p1;
1029 else
1030 parms = commonparms (p1, p2);
1032 cp_cv_quals quals = type_memfn_quals (t1);
1033 cp_ref_qualifier rqual = type_memfn_rqual (t1);
1034 gcc_assert (quals == type_memfn_quals (t2));
1035 gcc_assert (rqual == type_memfn_rqual (t2));
1037 tree rval = build_function_type (valtype, parms);
1038 rval = apply_memfn_quals (rval, quals);
1039 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1040 TYPE_RAISES_EXCEPTIONS (t2));
1041 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1042 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
1043 break;
1046 case METHOD_TYPE:
1048 /* Get this value the long way, since TYPE_METHOD_BASETYPE
1049 is just the main variant of this. */
1050 tree basetype = class_of_this_parm (t2);
1051 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
1052 TYPE_RAISES_EXCEPTIONS (t2));
1053 cp_ref_qualifier rqual = type_memfn_rqual (t1);
1054 tree t3;
1055 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
1057 /* If this was a member function type, get back to the
1058 original type of type member function (i.e., without
1059 the class instance variable up front. */
1060 t1 = build_function_type (TREE_TYPE (t1),
1061 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
1062 t2 = build_function_type (TREE_TYPE (t2),
1063 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
1064 t3 = merge_types (t1, t2);
1065 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
1066 TYPE_ARG_TYPES (t3));
1067 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
1068 break;
1071 case TYPENAME_TYPE:
1072 /* There is no need to merge attributes into a TYPENAME_TYPE.
1073 When the type is instantiated it will have whatever
1074 attributes result from the instantiation. */
1075 return t1;
1077 default:;
1078 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1079 return t1;
1080 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1081 return t2;
1082 break;
1085 return cp_build_type_attribute_variant (t1, attributes);
1088 /* Return the ARRAY_TYPE type without its domain. */
1090 tree
1091 strip_array_domain (tree type)
1093 tree t2;
1094 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1095 if (TYPE_DOMAIN (type) == NULL_TREE)
1096 return type;
1097 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
1098 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
1101 /* Wrapper around cp_common_type that is used by c-common.cc and other
1102 front end optimizations that remove promotions.
1104 Return the common type for two arithmetic types T1 and T2 under the
1105 usual arithmetic conversions. The default conversions have already
1106 been applied, and enumerated types converted to their compatible
1107 integer types. */
1109 tree
1110 common_type (tree t1, tree t2)
1112 /* If one type is nonsense, use the other */
1113 if (t1 == error_mark_node)
1114 return t2;
1115 if (t2 == error_mark_node)
1116 return t1;
1118 return cp_common_type (t1, t2);
1121 /* Return the common type of two pointer types T1 and T2. This is the
1122 type for the result of most arithmetic operations if the operands
1123 have the given two types.
1125 We assume that comp_target_types has already been done and returned
1126 nonzero; if that isn't so, this may crash. */
1128 tree
1129 common_pointer_type (tree t1, tree t2)
1131 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
1132 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
1133 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
1135 return composite_pointer_type (input_location, t1, t2,
1136 error_mark_node, error_mark_node,
1137 CPO_CONVERSION, tf_warning_or_error);
1140 /* Compare two exception specifier types for exactness or subsetness, if
1141 allowed. Returns false for mismatch, true for match (same, or
1142 derived and !exact).
1144 [except.spec] "If a class X ... objects of class X or any class publicly
1145 and unambiguously derived from X. Similarly, if a pointer type Y * ...
1146 exceptions of type Y * or that are pointers to any type publicly and
1147 unambiguously derived from Y. Otherwise a function only allows exceptions
1148 that have the same type ..."
1149 This does not mention cv qualifiers and is different to what throw
1150 [except.throw] and catch [except.catch] will do. They will ignore the
1151 top level cv qualifiers, and allow qualifiers in the pointer to class
1152 example.
1154 We implement the letter of the standard. */
1156 static bool
1157 comp_except_types (tree a, tree b, bool exact)
1159 if (same_type_p (a, b))
1160 return true;
1161 else if (!exact)
1163 if (cp_type_quals (a) || cp_type_quals (b))
1164 return false;
1166 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1168 a = TREE_TYPE (a);
1169 b = TREE_TYPE (b);
1170 if (cp_type_quals (a) || cp_type_quals (b))
1171 return false;
1174 if (TREE_CODE (a) != RECORD_TYPE
1175 || TREE_CODE (b) != RECORD_TYPE)
1176 return false;
1178 if (publicly_uniquely_derived_p (a, b))
1179 return true;
1181 return false;
1184 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1185 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1186 If EXACT is ce_type, the C++17 type compatibility rules apply.
1187 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1188 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1189 are unordered, but we've already filtered out duplicates. Most lists will
1190 be in order, we should try to make use of that. */
1192 bool
1193 comp_except_specs (const_tree t1, const_tree t2, int exact)
1195 const_tree probe;
1196 const_tree base;
1197 int length = 0;
1199 if (t1 == t2)
1200 return true;
1202 /* First handle noexcept. */
1203 if (exact < ce_exact)
1205 if (exact == ce_type
1206 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1207 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1208 return true;
1210 /* noexcept(false) is compatible with no exception-specification,
1211 and less strict than any spec. */
1212 if (t1 == noexcept_false_spec)
1213 return t2 == NULL_TREE || exact == ce_derived;
1214 /* Even a derived noexcept(false) is compatible with no
1215 exception-specification. */
1216 if (t2 == noexcept_false_spec)
1217 return t1 == NULL_TREE;
1219 /* Otherwise, if we aren't looking for an exact match, noexcept is
1220 equivalent to throw(). */
1221 if (t1 == noexcept_true_spec)
1222 t1 = empty_except_spec;
1223 if (t2 == noexcept_true_spec)
1224 t2 = empty_except_spec;
1227 /* If any noexcept is left, it is only comparable to itself;
1228 either we're looking for an exact match or we're redeclaring a
1229 template with dependent noexcept. */
1230 if ((t1 && TREE_PURPOSE (t1))
1231 || (t2 && TREE_PURPOSE (t2)))
1232 return (t1 && t2
1233 && (exact == ce_exact
1234 ? TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
1235 : cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))));
1237 if (t1 == NULL_TREE) /* T1 is ... */
1238 return t2 == NULL_TREE || exact == ce_derived;
1239 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1240 return t2 != NULL_TREE && !TREE_VALUE (t2);
1241 if (t2 == NULL_TREE) /* T2 is ... */
1242 return false;
1243 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1244 return exact == ce_derived;
1246 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1247 Count how many we find, to determine exactness. For exact matching and
1248 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1249 O(nm). */
1250 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1252 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1254 tree a = TREE_VALUE (probe);
1255 tree b = TREE_VALUE (t2);
1257 if (comp_except_types (a, b, exact))
1259 if (probe == base && exact > ce_derived)
1260 base = TREE_CHAIN (probe);
1261 length++;
1262 break;
1265 if (probe == NULL_TREE)
1266 return false;
1268 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1271 /* Compare the array types T1 and T2. CB says how we should behave when
1272 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1273 bounds_either says than any array can be [], bounds_first means that
1274 onlt T1 can be an array with unknown bounds. STRICT is true if
1275 qualifiers must match when comparing the types of the array elements. */
1277 static bool
1278 comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
1279 bool strict)
1281 tree d1;
1282 tree d2;
1283 tree max1, max2;
1285 if (t1 == t2)
1286 return true;
1288 /* The type of the array elements must be the same. */
1289 if (strict
1290 ? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1291 : !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1292 return false;
1294 d1 = TYPE_DOMAIN (t1);
1295 d2 = TYPE_DOMAIN (t2);
1297 if (d1 == d2)
1298 return true;
1300 /* If one of the arrays is dimensionless, and the other has a
1301 dimension, they are of different types. However, it is valid to
1302 write:
1304 extern int a[];
1305 int a[3];
1307 by [basic.link]:
1309 declarations for an array object can specify
1310 array types that differ by the presence or absence of a major
1311 array bound (_dcl.array_). */
1312 if (!d1 && d2)
1313 return cb >= bounds_either;
1314 else if (d1 && !d2)
1315 return cb == bounds_either;
1317 /* Check that the dimensions are the same. */
1319 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1320 return false;
1321 max1 = TYPE_MAX_VALUE (d1);
1322 max2 = TYPE_MAX_VALUE (d2);
1324 if (!cp_tree_equal (max1, max2))
1325 return false;
1327 return true;
1330 /* Compare the relative position of T1 and T2 into their respective
1331 template parameter list.
1332 T1 and T2 must be template parameter types.
1333 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1335 static bool
1336 comp_template_parms_position (tree t1, tree t2)
1338 tree index1, index2;
1339 gcc_assert (t1 && t2
1340 && TREE_CODE (t1) == TREE_CODE (t2)
1341 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1342 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1343 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1345 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1346 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1348 /* Then compare their relative position. */
1349 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1350 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1351 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1352 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1353 return false;
1355 /* In C++14 we can end up comparing 'auto' to a normal template
1356 parameter. Don't confuse them. */
1357 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1358 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1360 return true;
1363 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1365 static bool
1366 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1368 t1 = TYPE_MAIN_VARIANT (t1);
1369 t2 = TYPE_MAIN_VARIANT (t2);
1371 if (TYPE_PTR_P (t1)
1372 && TYPE_PTR_P (t2))
1373 return true;
1375 /* The signedness of the parameter matters only when an integral
1376 type smaller than int is promoted to int, otherwise only the
1377 precision of the parameter matters.
1378 This check should make sure that the callee does not see
1379 undefined values in argument registers. */
1380 if (INTEGRAL_TYPE_P (t1)
1381 && INTEGRAL_TYPE_P (t2)
1382 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1383 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1384 || !targetm.calls.promote_prototypes (NULL_TREE)
1385 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1386 return true;
1388 return same_type_p (t1, t2);
1391 /* Check if a type cast between two function types can be considered safe. */
1393 static bool
1394 cxx_safe_function_type_cast_p (tree t1, tree t2)
1396 if (TREE_TYPE (t1) == void_type_node &&
1397 TYPE_ARG_TYPES (t1) == void_list_node)
1398 return true;
1400 if (TREE_TYPE (t2) == void_type_node &&
1401 TYPE_ARG_TYPES (t2) == void_list_node)
1402 return true;
1404 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1405 return false;
1407 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1408 t1 && t2;
1409 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1410 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1411 return false;
1413 return true;
1416 /* Subroutine in comptypes. */
1418 static bool
1419 structural_comptypes (tree t1, tree t2, int strict)
1421 /* Both should be types that are not obviously the same. */
1422 gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
1424 /* Suppress typename resolution under spec_hasher::equal in place of calling
1425 push_to_top_level there. */
1426 if (!comparing_specializations)
1428 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1429 current instantiation. */
1430 if (TREE_CODE (t1) == TYPENAME_TYPE)
1431 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1433 if (TREE_CODE (t2) == TYPENAME_TYPE)
1434 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1437 if (TYPE_PTRMEMFUNC_P (t1))
1438 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1439 if (TYPE_PTRMEMFUNC_P (t2))
1440 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1442 /* Different classes of types can't be compatible. */
1443 if (TREE_CODE (t1) != TREE_CODE (t2))
1444 return false;
1446 /* Qualifiers must match. For array types, we will check when we
1447 recur on the array element types. */
1448 if (TREE_CODE (t1) != ARRAY_TYPE
1449 && cp_type_quals (t1) != cp_type_quals (t2))
1450 return false;
1451 if (TREE_CODE (t1) == FUNCTION_TYPE
1452 && type_memfn_quals (t1) != type_memfn_quals (t2))
1453 return false;
1454 /* Need to check this before TYPE_MAIN_VARIANT.
1455 FIXME function qualifiers should really change the main variant. */
1456 if (FUNC_OR_METHOD_TYPE_P (t1))
1458 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1459 return false;
1460 if (flag_noexcept_type
1461 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1462 TYPE_RAISES_EXCEPTIONS (t2),
1463 ce_type))
1464 return false;
1467 /* Allow for two different type nodes which have essentially the same
1468 definition. Note that we already checked for equality of the type
1469 qualifiers (just above). */
1470 if (TREE_CODE (t1) != ARRAY_TYPE
1471 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1472 goto check_alias;
1474 /* Compare the types. Return false on known not-same. Break on not
1475 known. Never return true from this switch -- you'll break
1476 specialization comparison. */
1477 switch (TREE_CODE (t1))
1479 case VOID_TYPE:
1480 case BOOLEAN_TYPE:
1481 /* All void and bool types are the same. */
1482 break;
1484 case OPAQUE_TYPE:
1485 case INTEGER_TYPE:
1486 case FIXED_POINT_TYPE:
1487 case REAL_TYPE:
1488 /* With these nodes, we can't determine type equivalence by
1489 looking at what is stored in the nodes themselves, because
1490 two nodes might have different TYPE_MAIN_VARIANTs but still
1491 represent the same type. For example, wchar_t and int could
1492 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1493 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1494 and are distinct types. On the other hand, int and the
1495 following typedef
1497 typedef int INT __attribute((may_alias));
1499 have identical properties, different TYPE_MAIN_VARIANTs, but
1500 represent the same type. The canonical type system keeps
1501 track of equivalence in this case, so we fall back on it. */
1502 if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1503 return false;
1505 /* We don't need or want the attribute comparison. */
1506 goto check_alias;
1508 case TEMPLATE_TEMPLATE_PARM:
1509 case BOUND_TEMPLATE_TEMPLATE_PARM:
1510 if (!comp_template_parms_position (t1, t2))
1511 return false;
1512 if (!comp_template_parms
1513 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1514 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1515 return false;
1516 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1517 break;
1518 /* Don't check inheritance. */
1519 strict = COMPARE_STRICT;
1520 /* Fall through. */
1522 case RECORD_TYPE:
1523 case UNION_TYPE:
1524 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1525 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1526 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1527 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1528 break;
1530 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1531 break;
1532 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1533 break;
1535 return false;
1537 case OFFSET_TYPE:
1538 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1539 strict & ~COMPARE_REDECLARATION))
1540 return false;
1541 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1542 return false;
1543 break;
1545 case REFERENCE_TYPE:
1546 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1547 return false;
1548 /* fall through to checks for pointer types */
1549 gcc_fallthrough ();
1551 case POINTER_TYPE:
1552 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1553 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1554 return false;
1555 break;
1557 case METHOD_TYPE:
1558 case FUNCTION_TYPE:
1559 /* Exception specs and memfn_rquals were checked above. */
1560 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1561 return false;
1562 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1563 return false;
1564 break;
1566 case ARRAY_TYPE:
1567 /* Target types must match incl. qualifiers. */
1568 if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
1569 ? bounds_either : bounds_none),
1570 /*strict=*/true))
1571 return false;
1572 break;
1574 case TEMPLATE_TYPE_PARM:
1575 /* If T1 and T2 don't have the same relative position in their
1576 template parameters set, they can't be equal. */
1577 if (!comp_template_parms_position (t1, t2))
1578 return false;
1579 /* If T1 and T2 don't represent the same class template deduction,
1580 they aren't equal. */
1581 if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1),
1582 CLASS_PLACEHOLDER_TEMPLATE (t2)))
1583 return false;
1584 /* Constrained 'auto's are distinct from parms that don't have the same
1585 constraints. */
1586 if (!equivalent_placeholder_constraints (t1, t2))
1587 return false;
1588 break;
1590 case TYPENAME_TYPE:
1591 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1592 TYPENAME_TYPE_FULLNAME (t2)))
1593 return false;
1594 /* Qualifiers don't matter on scopes. */
1595 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1596 TYPE_CONTEXT (t2)))
1597 return false;
1598 break;
1600 case UNBOUND_CLASS_TEMPLATE:
1601 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1602 return false;
1603 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1604 return false;
1605 break;
1607 case COMPLEX_TYPE:
1608 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1609 return false;
1610 break;
1612 case VECTOR_TYPE:
1613 if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
1614 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1615 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1616 return false;
1617 break;
1619 case TYPE_PACK_EXPANSION:
1620 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1621 PACK_EXPANSION_PATTERN (t2))
1622 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1623 PACK_EXPANSION_EXTRA_ARGS (t2)));
1625 case DECLTYPE_TYPE:
1626 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1627 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
1628 return false;
1629 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1630 return false;
1631 if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1632 return false;
1633 if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
1634 return false;
1635 break;
1637 case TRAIT_TYPE:
1638 if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
1639 return false;
1640 if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
1641 || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
1642 return false;
1643 break;
1645 case TYPEOF_TYPE:
1646 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
1647 return false;
1648 break;
1650 default:
1651 return false;
1654 /* If we get here, we know that from a target independent POV the
1655 types are the same. Make sure the target attributes are also
1656 the same. */
1657 if (!comp_type_attributes (t1, t2))
1658 return false;
1660 check_alias:
1661 if (comparing_dependent_aliases)
1663 /* Don't treat an alias template specialization with dependent
1664 arguments as equivalent to its underlying type when used as a
1665 template argument; we need them to be distinct so that we
1666 substitute into the specialization arguments at instantiation
1667 time. And aliases can't be equivalent without being ==, so
1668 we don't need to look any deeper. */
1669 ++processing_template_decl;
1670 tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
1671 tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
1672 --processing_template_decl;
1673 if ((dep1 || dep2) && dep1 != dep2)
1674 return false;
1677 return true;
1680 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1681 is a bitwise-or of the COMPARE_* flags. */
1683 bool
1684 comptypes (tree t1, tree t2, int strict)
1686 gcc_checking_assert (t1 && t2);
1688 /* TYPE_ARGUMENT_PACKS are not really types. */
1689 gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
1690 && TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
1692 if (t1 == t2)
1693 return true;
1695 /* Suppress errors caused by previously reported errors. */
1696 if (t1 == error_mark_node || t2 == error_mark_node)
1697 return false;
1699 if (strict == COMPARE_STRICT)
1701 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1702 /* At least one of the types requires structural equality, so
1703 perform a deep check. */
1704 return structural_comptypes (t1, t2, strict);
1706 if (flag_checking && param_use_canonical_types)
1708 bool result = structural_comptypes (t1, t2, strict);
1710 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1711 /* The two types are structurally equivalent, but their
1712 canonical types were different. This is a failure of the
1713 canonical type propagation code.*/
1714 internal_error
1715 ("canonical types differ for identical types %qT and %qT",
1716 t1, t2);
1717 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1718 /* Two types are structurally different, but the canonical
1719 types are the same. This means we were over-eager in
1720 assigning canonical types. */
1721 internal_error
1722 ("same canonical type node for different types %qT and %qT",
1723 t1, t2);
1725 return result;
1727 if (!flag_checking && param_use_canonical_types)
1728 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1729 else
1730 return structural_comptypes (t1, t2, strict);
1732 else if (strict == COMPARE_STRUCTURAL)
1733 return structural_comptypes (t1, t2, COMPARE_STRICT);
1734 else
1735 return structural_comptypes (t1, t2, strict);
1738 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1739 top-level qualifiers. */
1741 bool
1742 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1744 if (type1 == error_mark_node || type2 == error_mark_node)
1745 return false;
1746 if (type1 == type2)
1747 return true;
1749 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1750 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1751 return same_type_p (type1, type2);
1754 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1756 bool
1757 similar_type_p (tree type1, tree type2)
1759 if (type1 == error_mark_node || type2 == error_mark_node)
1760 return false;
1762 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1763 * they are the same type; or
1764 * they are both pointers, and the pointed-to types are similar; or
1765 * they are both pointers to member of the same class, and the types of
1766 the pointed-to members are similar; or
1767 * they are both arrays of the same size or both arrays of unknown bound,
1768 and the array element types are similar. */
1770 if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
1771 return true;
1773 if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
1774 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
1775 || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
1776 return comp_ptr_ttypes_const (type1, type2, bounds_either);
1778 return false;
1781 /* Helper function for layout_compatible_type_p and
1782 is_corresponding_member_aggr. Advance to next members (NULL if
1783 no further ones) and return true if those members are still part of
1784 the common initial sequence. */
1786 bool
1787 next_common_initial_sequence (tree &memb1, tree &memb2)
1789 while (memb1)
1791 if (TREE_CODE (memb1) != FIELD_DECL
1792 || (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
1794 memb1 = DECL_CHAIN (memb1);
1795 continue;
1797 if (DECL_FIELD_IS_BASE (memb1))
1799 memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
1800 continue;
1802 break;
1804 while (memb2)
1806 if (TREE_CODE (memb2) != FIELD_DECL
1807 || (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
1809 memb2 = DECL_CHAIN (memb2);
1810 continue;
1812 if (DECL_FIELD_IS_BASE (memb2))
1814 memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
1815 continue;
1817 break;
1819 if (memb1 == NULL_TREE && memb2 == NULL_TREE)
1820 return true;
1821 if (memb1 == NULL_TREE || memb2 == NULL_TREE)
1822 return false;
1823 if (DECL_BIT_FIELD_TYPE (memb1))
1825 if (!DECL_BIT_FIELD_TYPE (memb2))
1826 return false;
1827 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
1828 DECL_BIT_FIELD_TYPE (memb2)))
1829 return false;
1830 if (TYPE_PRECISION (TREE_TYPE (memb1))
1831 != TYPE_PRECISION (TREE_TYPE (memb2)))
1832 return false;
1834 else if (DECL_BIT_FIELD_TYPE (memb2))
1835 return false;
1836 else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
1837 return false;
1838 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
1839 != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
1840 return false;
1841 if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
1842 return false;
1843 if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
1844 return false;
1845 return true;
1848 /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1850 bool
1851 layout_compatible_type_p (tree type1, tree type2)
1853 if (type1 == error_mark_node || type2 == error_mark_node)
1854 return false;
1855 if (type1 == type2)
1856 return true;
1857 if (TREE_CODE (type1) != TREE_CODE (type2))
1858 return false;
1860 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1861 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1863 if (TREE_CODE (type1) == ENUMERAL_TYPE)
1864 return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
1865 && same_type_p (finish_underlying_type (type1),
1866 finish_underlying_type (type2)));
1868 if (CLASS_TYPE_P (type1)
1869 && std_layout_type_p (type1)
1870 && std_layout_type_p (type2)
1871 && tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
1873 tree field1 = TYPE_FIELDS (type1);
1874 tree field2 = TYPE_FIELDS (type2);
1875 if (TREE_CODE (type1) == RECORD_TYPE)
1877 while (1)
1879 if (!next_common_initial_sequence (field1, field2))
1880 return false;
1881 if (field1 == NULL_TREE)
1882 return true;
1883 field1 = DECL_CHAIN (field1);
1884 field2 = DECL_CHAIN (field2);
1887 /* Otherwise both types must be union types.
1888 The standard says:
1889 "Two standard-layout unions are layout-compatible if they have
1890 the same number of non-static data members and corresponding
1891 non-static data members (in any order) have layout-compatible
1892 types."
1893 but the code anticipates that bitfield vs. non-bitfield,
1894 different bitfield widths or presence/absence of
1895 [[no_unique_address]] should be checked as well. */
1896 auto_vec<tree, 16> vec;
1897 unsigned int count = 0;
1898 for (; field1; field1 = DECL_CHAIN (field1))
1899 if (TREE_CODE (field1) == FIELD_DECL)
1900 count++;
1901 for (; field2; field2 = DECL_CHAIN (field2))
1902 if (TREE_CODE (field2) == FIELD_DECL)
1903 vec.safe_push (field2);
1904 /* Discussions on core lean towards treating multiple union fields
1905 of the same type as the same field, so this might need changing
1906 in the future. */
1907 if (count != vec.length ())
1908 return false;
1909 for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
1911 if (TREE_CODE (field1) != FIELD_DECL)
1912 continue;
1913 unsigned int j;
1914 tree t1 = DECL_BIT_FIELD_TYPE (field1);
1915 if (t1 == NULL_TREE)
1916 t1 = TREE_TYPE (field1);
1917 FOR_EACH_VEC_ELT (vec, j, field2)
1919 tree t2 = DECL_BIT_FIELD_TYPE (field2);
1920 if (t2 == NULL_TREE)
1921 t2 = TREE_TYPE (field2);
1922 if (DECL_BIT_FIELD_TYPE (field1))
1924 if (!DECL_BIT_FIELD_TYPE (field2))
1925 continue;
1926 if (TYPE_PRECISION (TREE_TYPE (field1))
1927 != TYPE_PRECISION (TREE_TYPE (field2)))
1928 continue;
1930 else if (DECL_BIT_FIELD_TYPE (field2))
1931 continue;
1932 if (!layout_compatible_type_p (t1, t2))
1933 continue;
1934 if ((!lookup_attribute ("no_unique_address",
1935 DECL_ATTRIBUTES (field1)))
1936 != !lookup_attribute ("no_unique_address",
1937 DECL_ATTRIBUTES (field2)))
1938 continue;
1939 break;
1941 if (j == vec.length ())
1942 return false;
1943 vec.unordered_remove (j);
1945 return true;
1948 return same_type_p (type1, type2);
1951 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1953 bool
1954 at_least_as_qualified_p (const_tree type1, const_tree type2)
1956 int q1 = cp_type_quals (type1);
1957 int q2 = cp_type_quals (type2);
1959 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1960 return (q1 & q2) == q2;
1963 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1964 more cv-qualified that TYPE1, and 0 otherwise. */
1967 comp_cv_qualification (int q1, int q2)
1969 if (q1 == q2)
1970 return 0;
1972 if ((q1 & q2) == q2)
1973 return 1;
1974 else if ((q1 & q2) == q1)
1975 return -1;
1977 return 0;
1981 comp_cv_qualification (const_tree type1, const_tree type2)
1983 int q1 = cp_type_quals (type1);
1984 int q2 = cp_type_quals (type2);
1985 return comp_cv_qualification (q1, q2);
1988 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1989 subset of the cv-qualification signature of TYPE2, and the types
1990 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1993 comp_cv_qual_signature (tree type1, tree type2)
1995 if (comp_ptr_ttypes_real (type2, type1, -1))
1996 return 1;
1997 else if (comp_ptr_ttypes_real (type1, type2, -1))
1998 return -1;
1999 else
2000 return 0;
2003 /* Subroutines of `comptypes'. */
2005 /* Return true if two parameter type lists PARMS1 and PARMS2 are
2006 equivalent in the sense that functions with those parameter types
2007 can have equivalent types. The two lists must be equivalent,
2008 element by element. */
2010 bool
2011 compparms (const_tree parms1, const_tree parms2)
2013 const_tree t1, t2;
2015 /* An unspecified parmlist matches any specified parmlist
2016 whose argument types don't need default promotions. */
2018 for (t1 = parms1, t2 = parms2;
2019 t1 || t2;
2020 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2022 /* If one parmlist is shorter than the other,
2023 they fail to match. */
2024 if (!t1 || !t2)
2025 return false;
2026 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
2027 return false;
2029 return true;
2033 /* Process a sizeof or alignof expression where the operand is a type.
2034 STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2035 or GNU (preferred alignment) semantics; it is ignored if OP is
2036 SIZEOF_EXPR. */
2038 tree
2039 cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
2040 bool std_alignof, bool complain)
2042 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2043 if (type == error_mark_node)
2044 return error_mark_node;
2046 type = non_reference (type);
2047 if (TREE_CODE (type) == METHOD_TYPE)
2049 if (complain)
2051 pedwarn (loc, OPT_Wpointer_arith,
2052 "invalid application of %qs to a member function",
2053 OVL_OP_INFO (false, op)->name);
2054 return size_one_node;
2056 else
2057 return error_mark_node;
2059 else if (VOID_TYPE_P (type) && std_alignof)
2061 if (complain)
2062 error_at (loc, "invalid application of %qs to a void type",
2063 OVL_OP_INFO (false, op)->name);
2064 return error_mark_node;
2067 bool dependent_p = dependent_type_p (type);
2068 if (!dependent_p)
2069 complete_type (type);
2070 if (dependent_p
2071 /* VLA types will have a non-constant size. In the body of an
2072 uninstantiated template, we don't need to try to compute the
2073 value, because the sizeof expression is not an integral
2074 constant expression in that case. And, if we do try to
2075 compute the value, we'll likely end up with SAVE_EXPRs, which
2076 the template substitution machinery does not expect to see. */
2077 || (processing_template_decl
2078 && COMPLETE_TYPE_P (type)
2079 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2081 tree value = build_min (op, size_type_node, type);
2082 TREE_READONLY (value) = 1;
2083 if (op == ALIGNOF_EXPR && std_alignof)
2084 ALIGNOF_EXPR_STD_P (value) = true;
2085 SET_EXPR_LOCATION (value, loc);
2086 return value;
2089 return c_sizeof_or_alignof_type (loc, complete_type (type),
2090 op == SIZEOF_EXPR, std_alignof,
2091 complain);
2094 /* Return the size of the type, without producing any warnings for
2095 types whose size cannot be taken. This routine should be used only
2096 in some other routine that has already produced a diagnostic about
2097 using the size of such a type. */
2098 tree
2099 cxx_sizeof_nowarn (tree type)
2101 if (TREE_CODE (type) == FUNCTION_TYPE
2102 || VOID_TYPE_P (type)
2103 || TREE_CODE (type) == ERROR_MARK)
2104 return size_one_node;
2105 else if (!COMPLETE_TYPE_P (type))
2106 return size_zero_node;
2107 else
2108 return cxx_sizeof_or_alignof_type (input_location, type,
2109 SIZEOF_EXPR, false, false);
2112 /* Process a sizeof expression where the operand is an expression. */
2114 static tree
2115 cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
2117 if (e == error_mark_node)
2118 return error_mark_node;
2120 if (instantiation_dependent_uneval_expression_p (e))
2122 e = build_min (SIZEOF_EXPR, size_type_node, e);
2123 TREE_SIDE_EFFECTS (e) = 0;
2124 TREE_READONLY (e) = 1;
2125 SET_EXPR_LOCATION (e, loc);
2127 return e;
2130 location_t e_loc = cp_expr_loc_or_loc (e, loc);
2131 STRIP_ANY_LOCATION_WRAPPER (e);
2133 /* To get the size of a static data member declared as an array of
2134 unknown bound, we need to instantiate it. */
2135 if (VAR_P (e)
2136 && VAR_HAD_UNKNOWN_BOUND (e)
2137 && DECL_TEMPLATE_INSTANTIATION (e))
2138 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
2140 if (TREE_CODE (e) == PARM_DECL
2141 && DECL_ARRAY_PARAMETER_P (e)
2142 && (complain & tf_warning))
2144 auto_diagnostic_group d;
2145 if (warning_at (e_loc, OPT_Wsizeof_array_argument,
2146 "%<sizeof%> on array function parameter %qE "
2147 "will return size of %qT", e, TREE_TYPE (e)))
2148 inform (DECL_SOURCE_LOCATION (e), "declared here");
2151 e = mark_type_use (e);
2153 if (bitfield_p (e))
2155 if (complain & tf_error)
2156 error_at (e_loc,
2157 "invalid application of %<sizeof%> to a bit-field");
2158 else
2159 return error_mark_node;
2160 e = char_type_node;
2162 else if (is_overloaded_fn (e))
2164 if (complain & tf_error)
2165 permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
2166 "an expression of function type");
2167 else
2168 return error_mark_node;
2169 e = char_type_node;
2171 else if (type_unknown_p (e))
2173 if (complain & tf_error)
2174 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2175 else
2176 return error_mark_node;
2177 e = char_type_node;
2179 else
2180 e = TREE_TYPE (e);
2182 return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
2183 complain & tf_error);
2186 /* Implement the __alignof keyword: Return the minimum required
2187 alignment of E, measured in bytes. For VAR_DECL's and
2188 FIELD_DECL's return DECL_ALIGN (which can be set from an
2189 "aligned" __attribute__ specification). STD_ALIGNOF acts
2190 like in cxx_sizeof_or_alignof_type. */
2192 static tree
2193 cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
2194 tsubst_flags_t complain)
2196 tree t;
2198 if (e == error_mark_node)
2199 return error_mark_node;
2201 if (processing_template_decl)
2203 e = build_min (ALIGNOF_EXPR, size_type_node, e);
2204 TREE_SIDE_EFFECTS (e) = 0;
2205 TREE_READONLY (e) = 1;
2206 SET_EXPR_LOCATION (e, loc);
2207 ALIGNOF_EXPR_STD_P (e) = std_alignof;
2209 return e;
2212 location_t e_loc = cp_expr_loc_or_loc (e, loc);
2213 STRIP_ANY_LOCATION_WRAPPER (e);
2215 e = mark_type_use (e);
2217 if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
2218 !(complain & tf_error)))
2220 if (!(complain & tf_error))
2221 return error_mark_node;
2222 t = size_one_node;
2224 else if (VAR_P (e))
2225 t = size_int (DECL_ALIGN_UNIT (e));
2226 else if (bitfield_p (e))
2228 if (complain & tf_error)
2229 error_at (e_loc,
2230 "invalid application of %<__alignof%> to a bit-field");
2231 else
2232 return error_mark_node;
2233 t = size_one_node;
2235 else if (TREE_CODE (e) == COMPONENT_REF
2236 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
2237 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
2238 else if (is_overloaded_fn (e))
2240 if (complain & tf_error)
2241 permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
2242 "an expression of function type");
2243 else
2244 return error_mark_node;
2245 if (TREE_CODE (e) == FUNCTION_DECL)
2246 t = size_int (DECL_ALIGN_UNIT (e));
2247 else
2248 t = size_one_node;
2250 else if (type_unknown_p (e))
2252 if (complain & tf_error)
2253 cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
2254 else
2255 return error_mark_node;
2256 t = size_one_node;
2258 else
2259 return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
2260 ALIGNOF_EXPR, std_alignof,
2261 complain & tf_error);
2263 return fold_convert_loc (loc, size_type_node, t);
2266 /* Process a sizeof or alignof expression E with code OP where the operand
2267 is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2269 tree
2270 cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
2271 bool std_alignof, bool complain)
2273 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
2274 if (op == SIZEOF_EXPR)
2275 return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
2276 else
2277 return cxx_alignof_expr (loc, e, std_alignof,
2278 complain? tf_warning_or_error : tf_none);
2281 /* Build a representation of an expression 'alignas(E).' Return the
2282 folded integer value of E if it is an integral constant expression
2283 that resolves to a valid alignment. If E depends on a template
2284 parameter, return a syntactic representation tree of kind
2285 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2286 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2288 tree
2289 cxx_alignas_expr (tree e)
2291 if (e == NULL_TREE || e == error_mark_node
2292 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
2293 return e;
2295 if (TYPE_P (e))
2296 /* [dcl.align]/3:
2298 When the alignment-specifier is of the form
2299 alignas(type-id), it shall have the same effect as
2300 alignas(alignof(type-id)). */
2302 return cxx_sizeof_or_alignof_type (input_location,
2303 e, ALIGNOF_EXPR,
2304 /*std_alignof=*/true,
2305 /*complain=*/true);
2307 /* If we reach this point, it means the alignas expression if of
2308 the form "alignas(assignment-expression)", so we should follow
2309 what is stated by [dcl.align]/2. */
2311 if (value_dependent_expression_p (e))
2312 /* Leave value-dependent expression alone for now. */
2313 return e;
2315 e = instantiate_non_dependent_expr (e);
2316 e = mark_rvalue_use (e);
2318 /* [dcl.align]/2 says:
2320 the assignment-expression shall be an integral constant
2321 expression. */
2323 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
2325 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
2326 return error_mark_node;
2329 return cxx_constant_value (e);
2333 /* EXPR is being used in a context that is not a function call.
2334 Enforce:
2336 [expr.ref]
2338 The expression can be used only as the left-hand operand of a
2339 member function call.
2341 [expr.mptr.operator]
2343 If the result of .* or ->* is a function, then that result can be
2344 used only as the operand for the function call operator ().
2346 by issuing an error message if appropriate. Returns true iff EXPR
2347 violates these rules. */
2349 bool
2350 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
2352 if (expr == NULL_TREE)
2353 return false;
2354 /* Don't enforce this in MS mode. */
2355 if (flag_ms_extensions)
2356 return false;
2357 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
2358 expr = get_first_fn (expr);
2359 if (TREE_TYPE (expr)
2360 && DECL_IOBJ_MEMBER_FUNCTION_P (expr))
2362 if (complain & tf_error)
2364 if (DECL_P (expr))
2366 error_at (loc, "invalid use of non-static member function %qD",
2367 expr);
2368 inform (DECL_SOURCE_LOCATION (expr), "declared here");
2370 else
2371 error_at (loc, "invalid use of non-static member function of "
2372 "type %qT", TREE_TYPE (expr));
2374 return true;
2376 return false;
2379 /* If EXP is a reference to a bit-field, and the type of EXP does not
2380 match the declared type of the bit-field, return the declared type
2381 of the bit-field. Otherwise, return NULL_TREE. */
2383 tree
2384 is_bitfield_expr_with_lowered_type (const_tree exp)
2386 switch (TREE_CODE (exp))
2388 case COND_EXPR:
2389 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
2390 ? TREE_OPERAND (exp, 1)
2391 : TREE_OPERAND (exp, 0)))
2392 return NULL_TREE;
2393 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
2395 case COMPOUND_EXPR:
2396 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
2398 case MODIFY_EXPR:
2399 case SAVE_EXPR:
2400 case UNARY_PLUS_EXPR:
2401 case PREDECREMENT_EXPR:
2402 case PREINCREMENT_EXPR:
2403 case POSTDECREMENT_EXPR:
2404 case POSTINCREMENT_EXPR:
2405 case NEGATE_EXPR:
2406 case NON_LVALUE_EXPR:
2407 case BIT_NOT_EXPR:
2408 case CLEANUP_POINT_EXPR:
2409 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2411 case COMPONENT_REF:
2413 tree field;
2415 field = TREE_OPERAND (exp, 1);
2416 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
2417 return NULL_TREE;
2418 if (same_type_ignoring_top_level_qualifiers_p
2419 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
2420 return NULL_TREE;
2421 return DECL_BIT_FIELD_TYPE (field);
2424 case VAR_DECL:
2425 if (DECL_HAS_VALUE_EXPR_P (exp))
2426 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2427 (CONST_CAST_TREE (exp)));
2428 return NULL_TREE;
2430 case VIEW_CONVERT_EXPR:
2431 if (location_wrapper_p (exp))
2432 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
2433 else
2434 return NULL_TREE;
2436 default:
2437 return NULL_TREE;
2441 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2442 bitfield with a lowered type, the type of EXP is returned, rather
2443 than NULL_TREE. */
2445 tree
2446 unlowered_expr_type (const_tree exp)
2448 tree type;
2449 tree etype = TREE_TYPE (exp);
2451 type = is_bitfield_expr_with_lowered_type (exp);
2452 if (type)
2453 type = cp_build_qualified_type (type, cp_type_quals (etype));
2454 else
2455 type = etype;
2457 return type;
2460 /* Perform the conversions in [expr] that apply when an lvalue appears
2461 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2462 function-to-pointer conversions. In addition, bitfield references are
2463 converted to their declared types. Note that this function does not perform
2464 the lvalue-to-rvalue conversion for class types. If you need that conversion
2465 for class types, then you probably need to use force_rvalue.
2467 Although the returned value is being used as an rvalue, this
2468 function does not wrap the returned expression in a
2469 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2470 that the return value is no longer an lvalue. */
2472 tree
2473 decay_conversion (tree exp,
2474 tsubst_flags_t complain,
2475 bool reject_builtin /* = true */)
2477 tree type;
2478 enum tree_code code;
2479 location_t loc = cp_expr_loc_or_input_loc (exp);
2481 type = TREE_TYPE (exp);
2482 if (type == error_mark_node)
2483 return error_mark_node;
2485 exp = resolve_nondeduced_context_or_error (exp, complain);
2487 code = TREE_CODE (type);
2489 if (error_operand_p (exp))
2490 return error_mark_node;
2492 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2494 mark_rvalue_use (exp, loc, reject_builtin);
2495 return nullptr_node;
2498 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2499 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2500 if (code == VOID_TYPE)
2502 if (complain & tf_error)
2503 error_at (loc, "void value not ignored as it ought to be");
2504 return error_mark_node;
2506 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2507 return error_mark_node;
2508 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2510 exp = mark_lvalue_use (exp);
2511 if (reject_builtin && reject_gcc_builtin (exp, loc))
2512 return error_mark_node;
2513 return cp_build_addr_expr (exp, complain);
2515 if (code == ARRAY_TYPE)
2517 tree adr;
2518 tree ptrtype;
2520 exp = mark_lvalue_use (exp);
2522 if (INDIRECT_REF_P (exp))
2523 return build_nop (build_pointer_type (TREE_TYPE (type)),
2524 TREE_OPERAND (exp, 0));
2526 if (TREE_CODE (exp) == COMPOUND_EXPR)
2528 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2529 if (op1 == error_mark_node)
2530 return error_mark_node;
2531 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2532 TREE_OPERAND (exp, 0), op1);
2535 if (!obvalue_p (exp)
2536 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2538 if (complain & tf_error)
2539 error_at (loc, "invalid use of non-lvalue array");
2540 return error_mark_node;
2543 ptrtype = build_pointer_type (TREE_TYPE (type));
2545 if (VAR_P (exp))
2547 if (!cxx_mark_addressable (exp))
2548 return error_mark_node;
2549 adr = build_nop (ptrtype, build_address (exp));
2550 return adr;
2552 /* This way is better for a COMPONENT_REF since it can
2553 simplify the offset for a component. */
2554 adr = cp_build_addr_expr (exp, complain);
2555 return cp_convert (ptrtype, adr, complain);
2558 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2559 exp = mark_rvalue_use (exp, loc, reject_builtin);
2561 /* If a bitfield is used in a context where integral promotion
2562 applies, then the caller is expected to have used
2563 default_conversion. That function promotes bitfields correctly
2564 before calling this function. At this point, if we have a
2565 bitfield referenced, we may assume that is not subject to
2566 promotion, and that, therefore, the type of the resulting rvalue
2567 is the declared type of the bitfield. */
2568 exp = convert_bitfield_to_declared_type (exp);
2570 /* We do not call rvalue() here because we do not want to wrap EXP
2571 in a NON_LVALUE_EXPR. */
2573 /* [basic.lval]
2575 Non-class rvalues always have cv-unqualified types. */
2576 type = TREE_TYPE (exp);
2577 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2578 exp = build_nop (cv_unqualified (type), exp);
2580 if (!complete_type_or_maybe_complain (type, exp, complain))
2581 return error_mark_node;
2583 return exp;
2586 /* Perform preparatory conversions, as part of the "usual arithmetic
2587 conversions". In particular, as per [expr]:
2589 Whenever an lvalue expression appears as an operand of an
2590 operator that expects the rvalue for that operand, the
2591 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2592 standard conversions are applied to convert the expression to an
2593 rvalue.
2595 In addition, we perform integral promotions here, as those are
2596 applied to both operands to a binary operator before determining
2597 what additional conversions should apply. */
2599 static tree
2600 cp_default_conversion (tree exp, tsubst_flags_t complain)
2602 /* Check for target-specific promotions. */
2603 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2604 if (promoted_type)
2605 exp = cp_convert (promoted_type, exp, complain);
2606 /* Perform the integral promotions first so that bitfield
2607 expressions (which may promote to "int", even if the bitfield is
2608 declared "unsigned") are promoted correctly. */
2609 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2610 exp = cp_perform_integral_promotions (exp, complain);
2611 /* Perform the other conversions. */
2612 exp = decay_conversion (exp, complain);
2614 return exp;
2617 /* C version. */
2619 tree
2620 default_conversion (tree exp)
2622 return cp_default_conversion (exp, tf_warning_or_error);
2625 /* EXPR is an expression with an integral or enumeration type.
2626 Perform the integral promotions in [conv.prom], and return the
2627 converted value. */
2629 tree
2630 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2632 tree type;
2633 tree promoted_type;
2635 expr = mark_rvalue_use (expr);
2636 if (error_operand_p (expr))
2637 return error_mark_node;
2639 type = TREE_TYPE (expr);
2641 /* [conv.prom]
2643 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2644 of type int if int can represent all the values of the bit-field;
2645 otherwise, it can be converted to unsigned int if unsigned int can
2646 represent all the values of the bit-field. If the bit-field is larger yet,
2647 no integral promotion applies to it. If the bit-field has an enumerated
2648 type, it is treated as any other value of that type for promotion
2649 purposes. */
2650 tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
2651 if (bitfield_type
2652 && (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
2653 || TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
2654 type = bitfield_type;
2656 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2657 /* Scoped enums don't promote. */
2658 if (SCOPED_ENUM_P (type))
2659 return expr;
2660 promoted_type = type_promotes_to (type);
2661 if (type != promoted_type)
2662 expr = cp_convert (promoted_type, expr, complain);
2663 else if (bitfield_type && bitfield_type != type)
2664 /* Prevent decay_conversion from converting to bitfield_type. */
2665 expr = build_nop (type, expr);
2666 return expr;
2669 /* C version. */
2671 tree
2672 perform_integral_promotions (tree expr)
2674 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2677 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2678 decay_conversion to one. */
2681 string_conv_p (const_tree totype, const_tree exp, int warn)
2683 tree t;
2685 if (!TYPE_PTR_P (totype))
2686 return 0;
2688 t = TREE_TYPE (totype);
2689 if (!same_type_p (t, char_type_node)
2690 && !same_type_p (t, char8_type_node)
2691 && !same_type_p (t, char16_type_node)
2692 && !same_type_p (t, char32_type_node)
2693 && !same_type_p (t, wchar_type_node))
2694 return 0;
2696 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2698 STRIP_ANY_LOCATION_WRAPPER (exp);
2700 if (TREE_CODE (exp) == STRING_CST)
2702 /* Make sure that we don't try to convert between char and wide chars. */
2703 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2704 return 0;
2706 else
2708 /* Is this a string constant which has decayed to 'const char *'? */
2709 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2710 if (!same_type_p (TREE_TYPE (exp), t))
2711 return 0;
2712 STRIP_NOPS (exp);
2713 if (TREE_CODE (exp) != ADDR_EXPR
2714 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2715 return 0;
2717 if (warn)
2719 if (cxx_dialect >= cxx11)
2720 pedwarn (loc, OPT_Wwrite_strings,
2721 "ISO C++ forbids converting a string constant to %qT",
2722 totype);
2723 else
2724 warning_at (loc, OPT_Wwrite_strings,
2725 "deprecated conversion from string constant to %qT",
2726 totype);
2729 return 1;
2732 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2733 can, for example, use as an lvalue. This code used to be in
2734 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2735 expressions, where we're dealing with aggregates. But now it's again only
2736 called from unary_complex_lvalue. The case (in particular) that led to
2737 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2738 get it there. */
2740 static tree
2741 rationalize_conditional_expr (enum tree_code code, tree t,
2742 tsubst_flags_t complain)
2744 location_t loc = cp_expr_loc_or_input_loc (t);
2746 /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2747 the first operand is always the one to be used if both operands
2748 are equal, so we know what conditional expression this used to be. */
2749 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2751 tree op0 = TREE_OPERAND (t, 0);
2752 tree op1 = TREE_OPERAND (t, 1);
2754 /* The following code is incorrect if either operand side-effects. */
2755 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2756 && !TREE_SIDE_EFFECTS (op1));
2757 return
2758 build_conditional_expr (loc,
2759 build_x_binary_op (loc,
2760 (TREE_CODE (t) == MIN_EXPR
2761 ? LE_EXPR : GE_EXPR),
2762 op0, TREE_CODE (op0),
2763 op1, TREE_CODE (op1),
2764 NULL_TREE,
2765 /*overload=*/NULL,
2766 complain),
2767 cp_build_unary_op (code, op0, false, complain),
2768 cp_build_unary_op (code, op1, false, complain),
2769 complain);
2772 tree op1 = TREE_OPERAND (t, 1);
2773 if (TREE_CODE (op1) != THROW_EXPR)
2774 op1 = cp_build_unary_op (code, op1, false, complain);
2775 tree op2 = TREE_OPERAND (t, 2);
2776 if (TREE_CODE (op2) != THROW_EXPR)
2777 op2 = cp_build_unary_op (code, op2, false, complain);
2779 return
2780 build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
2783 /* Given the TYPE of an anonymous union field inside T, return the
2784 FIELD_DECL for the field. If not found return NULL_TREE. Because
2785 anonymous unions can nest, we must also search all anonymous unions
2786 that are directly reachable. */
2788 tree
2789 lookup_anon_field (tree, tree type)
2791 tree field;
2793 type = TYPE_MAIN_VARIANT (type);
2794 field = ANON_AGGR_TYPE_FIELD (type);
2795 gcc_assert (field);
2796 return field;
2799 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2800 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2801 non-NULL, it indicates the path to the base used to name MEMBER.
2802 If PRESERVE_REFERENCE is true, the expression returned will have
2803 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2804 returned will have the type referred to by the reference.
2806 This function does not perform access control; that is either done
2807 earlier by the parser when the name of MEMBER is resolved to MEMBER
2808 itself, or later when overload resolution selects one of the
2809 functions indicated by MEMBER. */
2811 tree
2812 build_class_member_access_expr (cp_expr object, tree member,
2813 tree access_path, bool preserve_reference,
2814 tsubst_flags_t complain)
2816 tree object_type;
2817 tree member_scope;
2818 tree result = NULL_TREE;
2819 tree using_decl = NULL_TREE;
2821 if (error_operand_p (object) || error_operand_p (member))
2822 return error_mark_node;
2824 gcc_assert (DECL_P (member) || BASELINK_P (member));
2826 /* [expr.ref]
2828 The type of the first expression shall be "class object" (of a
2829 complete type). */
2830 object_type = TREE_TYPE (object);
2831 if (!currently_open_class (object_type)
2832 && !complete_type_or_maybe_complain (object_type, object, complain))
2833 return error_mark_node;
2834 if (!CLASS_TYPE_P (object_type))
2836 if (complain & tf_error)
2838 if (INDIRECT_TYPE_P (object_type)
2839 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2840 error ("request for member %qD in %qE, which is of pointer "
2841 "type %qT (maybe you meant to use %<->%> ?)",
2842 member, object.get_value (), object_type);
2843 else
2844 error ("request for member %qD in %qE, which is of non-class "
2845 "type %qT", member, object.get_value (), object_type);
2847 return error_mark_node;
2850 /* The standard does not seem to actually say that MEMBER must be a
2851 member of OBJECT_TYPE. However, that is clearly what is
2852 intended. */
2853 if (DECL_P (member))
2855 member_scope = DECL_CLASS_CONTEXT (member);
2856 if (!mark_used (member, complain) && !(complain & tf_error))
2857 return error_mark_node;
2859 if (TREE_UNAVAILABLE (member))
2860 error_unavailable_use (member, NULL_TREE);
2861 else if (TREE_DEPRECATED (member))
2862 warn_deprecated_use (member, NULL_TREE);
2864 else
2865 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2866 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2867 presently be the anonymous union. Go outwards until we find a
2868 type related to OBJECT_TYPE. */
2869 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2870 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2871 object_type))
2872 member_scope = TYPE_CONTEXT (member_scope);
2873 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2875 if (complain & tf_error)
2877 if (TREE_CODE (member) == FIELD_DECL)
2878 error ("invalid use of non-static data member %qE", member);
2879 else
2880 error ("%qD is not a member of %qT", member, object_type);
2882 return error_mark_node;
2885 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2886 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2887 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2888 if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
2890 temp = cp_build_fold_indirect_ref (temp);
2891 if (!lvalue_p (object) && lvalue_p (temp))
2892 /* Preserve rvalueness. */
2893 temp = move (temp);
2894 object = temp;
2897 /* In [expr.ref], there is an explicit list of the valid choices for
2898 MEMBER. We check for each of those cases here. */
2899 if (VAR_P (member))
2901 /* A static data member. */
2902 result = member;
2903 mark_exp_read (object);
2905 if (tree wrap = maybe_get_tls_wrapper_call (result))
2906 /* Replace an evaluated use of the thread_local variable with
2907 a call to its wrapper. */
2908 result = wrap;
2910 /* If OBJECT has side-effects, they are supposed to occur. */
2911 if (TREE_SIDE_EFFECTS (object))
2912 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2914 else if (TREE_CODE (member) == FIELD_DECL)
2916 /* A non-static data member. */
2917 bool null_object_p;
2918 int type_quals;
2919 tree member_type;
2921 if (INDIRECT_REF_P (object))
2922 null_object_p =
2923 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2924 else
2925 null_object_p = false;
2927 /* Convert OBJECT to the type of MEMBER. */
2928 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2929 TYPE_MAIN_VARIANT (member_scope)))
2931 tree binfo;
2932 base_kind kind;
2934 /* We didn't complain above about a currently open class, but now we
2935 must: we don't know how to refer to a base member before layout is
2936 complete. But still don't complain in a template. */
2937 if (!cp_unevaluated_operand
2938 && !dependent_type_p (object_type)
2939 && !complete_type_or_maybe_complain (object_type, object,
2940 complain))
2941 return error_mark_node;
2943 binfo = lookup_base (access_path ? access_path : object_type,
2944 member_scope, ba_unique, &kind, complain);
2945 if (binfo == error_mark_node)
2946 return error_mark_node;
2948 /* It is invalid to try to get to a virtual base of a
2949 NULL object. The most common cause is invalid use of
2950 offsetof macro. */
2951 if (null_object_p && kind == bk_via_virtual)
2953 if (complain & tf_error)
2955 error ("invalid access to non-static data member %qD in "
2956 "virtual base of NULL object", member);
2958 return error_mark_node;
2961 /* Convert to the base. */
2962 object = build_base_path (PLUS_EXPR, object, binfo,
2963 /*nonnull=*/1, complain);
2964 /* If we found the base successfully then we should be able
2965 to convert to it successfully. */
2966 gcc_assert (object != error_mark_node);
2969 /* If MEMBER is from an anonymous aggregate, we have converted
2970 OBJECT so that it refers to the class containing the
2971 anonymous union. Generate a reference to the anonymous union
2972 itself, and recur to find MEMBER. */
2973 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2974 /* When this code is called from build_field_call, the
2975 object already has the type of the anonymous union.
2976 That is because the COMPONENT_REF was already
2977 constructed, and was then disassembled before calling
2978 build_field_call. After the function-call code is
2979 cleaned up, this waste can be eliminated. */
2980 && (!same_type_ignoring_top_level_qualifiers_p
2981 (TREE_TYPE (object), DECL_CONTEXT (member))))
2983 tree anonymous_union;
2985 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2986 DECL_CONTEXT (member));
2987 object = build_class_member_access_expr (object,
2988 anonymous_union,
2989 /*access_path=*/NULL_TREE,
2990 preserve_reference,
2991 complain);
2994 /* Compute the type of the field, as described in [expr.ref]. */
2995 type_quals = TYPE_UNQUALIFIED;
2996 member_type = TREE_TYPE (member);
2997 if (!TYPE_REF_P (member_type))
2999 type_quals = (cp_type_quals (member_type)
3000 | cp_type_quals (object_type));
3002 /* A field is const (volatile) if the enclosing object, or the
3003 field itself, is const (volatile). But, a mutable field is
3004 not const, even within a const object. */
3005 if (DECL_MUTABLE_P (member))
3006 type_quals &= ~TYPE_QUAL_CONST;
3007 member_type = cp_build_qualified_type (member_type, type_quals);
3010 result = build3_loc (input_location, COMPONENT_REF, member_type,
3011 object, member, NULL_TREE);
3013 /* Mark the expression const or volatile, as appropriate. Even
3014 though we've dealt with the type above, we still have to mark the
3015 expression itself. */
3016 if (type_quals & TYPE_QUAL_CONST)
3017 TREE_READONLY (result) = 1;
3018 if (type_quals & TYPE_QUAL_VOLATILE)
3019 TREE_THIS_VOLATILE (result) = 1;
3021 else if (BASELINK_P (member))
3023 /* The member is a (possibly overloaded) member function. */
3024 tree functions;
3025 tree type;
3027 /* If the MEMBER is exactly one static member function, then we
3028 know the type of the expression. Otherwise, we must wait
3029 until overload resolution has been performed. */
3030 functions = BASELINK_FUNCTIONS (member);
3031 if (TREE_CODE (functions) == OVERLOAD && OVL_SINGLE_P (functions))
3032 functions = OVL_FIRST (functions);
3033 if (TREE_CODE (functions) == FUNCTION_DECL
3034 && DECL_STATIC_FUNCTION_P (functions))
3035 type = TREE_TYPE (functions);
3036 else
3037 type = unknown_type_node;
3038 /* Note that we do not convert OBJECT to the BASELINK_BINFO
3039 base. That will happen when the function is called. */
3040 result = build3_loc (input_location, COMPONENT_REF, type, object, member,
3041 NULL_TREE);
3043 else if (TREE_CODE (member) == CONST_DECL)
3045 /* The member is an enumerator. */
3046 result = member;
3047 /* If OBJECT has side-effects, they are supposed to occur. */
3048 if (TREE_SIDE_EFFECTS (object))
3049 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
3050 object, result);
3052 else if ((using_decl = strip_using_decl (member)) != member)
3053 result = build_class_member_access_expr (object,
3054 using_decl,
3055 access_path, preserve_reference,
3056 complain);
3057 else
3059 if (complain & tf_error)
3060 error ("invalid use of %qD", member);
3061 return error_mark_node;
3064 if (!preserve_reference)
3065 /* [expr.ref]
3067 If E2 is declared to have type "reference to T", then ... the
3068 type of E1.E2 is T. */
3069 result = convert_from_reference (result);
3071 return result;
3074 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3075 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3077 tree
3078 lookup_destructor (tree object, tree scope, tree dtor_name,
3079 tsubst_flags_t complain)
3081 tree object_type = TREE_TYPE (object);
3082 tree dtor_type = TREE_OPERAND (dtor_name, 0);
3083 tree expr;
3085 /* We've already complained about this destructor. */
3086 if (dtor_type == error_mark_node)
3087 return error_mark_node;
3089 if (scope && !check_dtor_name (scope, dtor_type))
3091 if (complain & tf_error)
3092 error ("qualified type %qT does not match destructor name ~%qT",
3093 scope, dtor_type);
3094 return error_mark_node;
3096 if (is_auto (dtor_type))
3097 dtor_type = object_type;
3098 else if (identifier_p (dtor_type))
3100 /* In a template, names we can't find a match for are still accepted
3101 destructor names, and we check them here. */
3102 if (check_dtor_name (object_type, dtor_type))
3103 dtor_type = object_type;
3104 else
3106 if (complain & tf_error)
3107 error ("object type %qT does not match destructor name ~%qT",
3108 object_type, dtor_type);
3109 return error_mark_node;
3113 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
3115 if (complain & tf_error)
3116 error ("the type being destroyed is %qT, but the destructor "
3117 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
3118 return error_mark_node;
3120 expr = lookup_member (dtor_type, complete_dtor_identifier,
3121 /*protect=*/1, /*want_type=*/false,
3122 tf_warning_or_error);
3123 if (!expr)
3125 if (complain & tf_error)
3126 cxx_incomplete_type_error (dtor_name, dtor_type);
3127 return error_mark_node;
3129 expr = (adjust_result_of_qualified_name_lookup
3130 (expr, dtor_type, object_type));
3131 if (scope == NULL_TREE)
3132 /* We need to call adjust_result_of_qualified_name_lookup in case the
3133 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3134 that we still get virtual function binding. */
3135 BASELINK_QUALIFIED_P (expr) = false;
3136 return expr;
3139 /* An expression of the form "A::template B" has been resolved to
3140 DECL. Issue a diagnostic if B is not a template or template
3141 specialization. */
3143 void
3144 check_template_keyword (tree decl)
3146 /* The standard says:
3148 [temp.names]
3150 If a name prefixed by the keyword template is not a member
3151 template, the program is ill-formed.
3153 DR 228 removed the restriction that the template be a member
3154 template.
3156 DR 96, if accepted would add the further restriction that explicit
3157 template arguments must be provided if the template keyword is
3158 used, but, as of 2005-10-16, that DR is still in "drafting". If
3159 this DR is accepted, then the semantic checks here can be
3160 simplified, as the entity named must in fact be a template
3161 specialization, rather than, as at present, a set of overloaded
3162 functions containing at least one template function. */
3163 if (TREE_CODE (decl) != TEMPLATE_DECL
3164 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
3166 if (VAR_P (decl))
3168 if (DECL_USE_TEMPLATE (decl)
3169 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
3171 else
3172 permerror (input_location, "%qD is not a template", decl);
3174 else if (!is_overloaded_fn (decl))
3175 permerror (input_location, "%qD is not a template", decl);
3176 else
3178 bool found = false;
3180 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
3181 !found && iter; ++iter)
3183 tree fn = *iter;
3184 if (TREE_CODE (fn) == TEMPLATE_DECL
3185 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
3186 || (TREE_CODE (fn) == FUNCTION_DECL
3187 && DECL_USE_TEMPLATE (fn)
3188 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
3189 found = true;
3191 if (!found)
3192 permerror (input_location, "%qD is not a template", decl);
3197 /* Record that an access failure occurred on BASETYPE_PATH attempting
3198 to access DECL, where DIAG_DECL should be used for diagnostics. */
3200 void
3201 access_failure_info::record_access_failure (tree basetype_path,
3202 tree decl, tree diag_decl)
3204 m_was_inaccessible = true;
3205 m_basetype_path = basetype_path;
3206 m_decl = decl;
3207 m_diag_decl = diag_decl;
3210 /* If an access failure was recorded, then attempt to locate an
3211 accessor function for the pertinent field.
3212 Otherwise, return NULL_TREE. */
3214 tree
3215 access_failure_info::get_any_accessor (bool const_p) const
3217 if (!was_inaccessible_p ())
3218 return NULL_TREE;
3220 tree accessor
3221 = locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
3222 if (!accessor)
3223 return NULL_TREE;
3225 /* The accessor must itself be accessible for it to be a reasonable
3226 suggestion. */
3227 if (!accessible_p (m_basetype_path, accessor, true))
3228 return NULL_TREE;
3230 return accessor;
3233 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3234 replacing the primary location in RICHLOC with "accessor()". */
3236 void
3237 access_failure_info::add_fixit_hint (rich_location *richloc,
3238 tree accessor_decl)
3240 pretty_printer pp;
3241 pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
3242 pp_string (&pp, "()");
3243 richloc->add_fixit_replace (pp_formatted_text (&pp));
3246 /* If an access failure was recorded, then attempt to locate an
3247 accessor function for the pertinent field, and if one is
3248 available, add a note and fix-it hint suggesting using it. */
3250 void
3251 access_failure_info::maybe_suggest_accessor (bool const_p) const
3253 tree accessor = get_any_accessor (const_p);
3254 if (accessor == NULL_TREE)
3255 return;
3256 rich_location richloc (line_table, input_location);
3257 add_fixit_hint (&richloc, accessor);
3258 inform (&richloc, "field %q#D can be accessed via %q#D",
3259 m_diag_decl, accessor);
3262 /* Subroutine of finish_class_member_access_expr.
3263 Issue an error about NAME not being a member of ACCESS_PATH (or
3264 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3265 names. */
3267 static void
3268 complain_about_unrecognized_member (tree access_path, tree name,
3269 tree object_type)
3271 /* Attempt to provide a hint about misspelled names. */
3272 tree guessed_id = lookup_member_fuzzy (access_path, name,
3273 /*want_type=*/false);
3274 if (guessed_id == NULL_TREE)
3276 /* No hint. */
3277 error ("%q#T has no member named %qE",
3278 TREE_CODE (access_path) == TREE_BINFO
3279 ? TREE_TYPE (access_path) : object_type, name);
3280 return;
3283 location_t bogus_component_loc = input_location;
3284 gcc_rich_location rich_loc (bogus_component_loc);
3286 /* Check that the guessed name is accessible along access_path. */
3287 access_failure_info afi;
3288 lookup_member (access_path, guessed_id, /*protect=*/1,
3289 /*want_type=*/false, /*complain=*/false,
3290 &afi);
3291 if (afi.was_inaccessible_p ())
3293 tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
3294 if (accessor)
3296 /* The guessed name isn't directly accessible, but can be accessed
3297 via an accessor member function. */
3298 afi.add_fixit_hint (&rich_loc, accessor);
3299 error_at (&rich_loc,
3300 "%q#T has no member named %qE;"
3301 " did you mean %q#D? (accessible via %q#D)",
3302 TREE_CODE (access_path) == TREE_BINFO
3303 ? TREE_TYPE (access_path) : object_type,
3304 name, afi.get_diag_decl (), accessor);
3306 else
3308 /* The guessed name isn't directly accessible, and no accessor
3309 member function could be found. */
3310 error_at (&rich_loc,
3311 "%q#T has no member named %qE;"
3312 " did you mean %q#D? (not accessible from this context)",
3313 TREE_CODE (access_path) == TREE_BINFO
3314 ? TREE_TYPE (access_path) : object_type,
3315 name, afi.get_diag_decl ());
3316 complain_about_access (afi.get_decl (), afi.get_diag_decl (),
3317 afi.get_diag_decl (), false, ak_none);
3320 else
3322 /* The guessed name is directly accessible; suggest it. */
3323 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
3324 guessed_id);
3325 error_at (&rich_loc,
3326 "%q#T has no member named %qE;"
3327 " did you mean %qE?",
3328 TREE_CODE (access_path) == TREE_BINFO
3329 ? TREE_TYPE (access_path) : object_type,
3330 name, guessed_id);
3334 /* This function is called by the parser to process a class member
3335 access expression of the form OBJECT.NAME. NAME is a node used by
3336 the parser to represent a name; it is not yet a DECL. It may,
3337 however, be a BASELINK where the BASELINK_FUNCTIONS is a
3338 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3339 there is no reason to do the lookup twice, so the parser keeps the
3340 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3341 be a template via the use of the "A::template B" syntax. */
3343 tree
3344 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
3345 tsubst_flags_t complain)
3347 tree expr;
3348 tree object_type;
3349 tree member;
3350 tree access_path = NULL_TREE;
3351 tree orig_object = object;
3352 tree orig_name = name;
3354 if (object == error_mark_node || name == error_mark_node)
3355 return error_mark_node;
3357 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3358 if (!objc_is_public (object, name))
3359 return error_mark_node;
3361 object_type = TREE_TYPE (object);
3363 if (processing_template_decl)
3365 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3366 type_dependent_object_expression_p (object)
3367 /* If NAME is "f<args>", where either 'f' or 'args' is
3368 dependent, then the expression is dependent. */
3369 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
3370 && dependent_template_id_p (TREE_OPERAND (name, 0),
3371 TREE_OPERAND (name, 1)))
3372 /* If NAME is "T::X" where "T" is dependent, then the
3373 expression is dependent. */
3374 || (TREE_CODE (name) == SCOPE_REF
3375 && TYPE_P (TREE_OPERAND (name, 0))
3376 && dependent_scope_p (TREE_OPERAND (name, 0)))
3377 /* If NAME is operator T where "T" is dependent, we can't
3378 lookup until we instantiate the T. */
3379 || (TREE_CODE (name) == IDENTIFIER_NODE
3380 && IDENTIFIER_CONV_OP_P (name)
3381 && dependent_type_p (TREE_TYPE (name))))
3383 dependent:
3384 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
3385 orig_object, orig_name, NULL_TREE);
3388 else if (c_dialect_objc ()
3389 && identifier_p (name)
3390 && (expr = objc_maybe_build_component_ref (object, name)))
3391 return expr;
3393 /* [expr.ref]
3395 The type of the first expression shall be "class object" (of a
3396 complete type). */
3397 if (!currently_open_class (object_type)
3398 && !complete_type_or_maybe_complain (object_type, object, complain))
3399 return error_mark_node;
3400 if (!CLASS_TYPE_P (object_type))
3402 if (complain & tf_error)
3404 if (INDIRECT_TYPE_P (object_type)
3405 && CLASS_TYPE_P (TREE_TYPE (object_type)))
3406 error ("request for member %qD in %qE, which is of pointer "
3407 "type %qT (maybe you meant to use %<->%> ?)",
3408 name, object.get_value (), object_type);
3409 else
3410 error ("request for member %qD in %qE, which is of non-class "
3411 "type %qT", name, object.get_value (), object_type);
3413 return error_mark_node;
3416 if (BASELINK_P (name))
3417 /* A member function that has already been looked up. */
3418 member = name;
3419 else
3421 bool is_template_id = false;
3422 tree template_args = NULL_TREE;
3423 tree scope = NULL_TREE;
3425 access_path = object_type;
3427 if (TREE_CODE (name) == SCOPE_REF)
3429 /* A qualified name. The qualifying class or namespace `S'
3430 has already been looked up; it is either a TYPE or a
3431 NAMESPACE_DECL. */
3432 scope = TREE_OPERAND (name, 0);
3433 name = TREE_OPERAND (name, 1);
3435 /* If SCOPE is a namespace, then the qualified name does not
3436 name a member of OBJECT_TYPE. */
3437 if (TREE_CODE (scope) == NAMESPACE_DECL)
3439 if (complain & tf_error)
3440 error ("%<%D::%D%> is not a member of %qT",
3441 scope, name, object_type);
3442 return error_mark_node;
3446 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3448 is_template_id = true;
3449 template_args = TREE_OPERAND (name, 1);
3450 name = TREE_OPERAND (name, 0);
3452 if (!identifier_p (name))
3453 name = OVL_NAME (name);
3456 if (scope)
3458 if (TREE_CODE (scope) == ENUMERAL_TYPE)
3460 gcc_assert (!is_template_id);
3461 /* Looking up a member enumerator (c++/56793). */
3462 if (!TYPE_CLASS_SCOPE_P (scope)
3463 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
3465 if (complain & tf_error)
3466 error ("%<%D::%D%> is not a member of %qT",
3467 scope, name, object_type);
3468 return error_mark_node;
3470 tree val = lookup_enumerator (scope, name);
3471 if (!val)
3473 if (complain & tf_error)
3474 error ("%qD is not a member of %qD",
3475 name, scope);
3476 return error_mark_node;
3479 if (TREE_SIDE_EFFECTS (object))
3480 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
3481 return val;
3484 gcc_assert (CLASS_TYPE_P (scope));
3485 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
3487 if (constructor_name_p (name, scope))
3489 if (complain & tf_error)
3490 error ("cannot call constructor %<%T::%D%> directly",
3491 scope, name);
3492 return error_mark_node;
3495 /* NAME may refer to a static data member, in which case there is
3496 one copy of the data member that is shared by all the objects of
3497 the class. So NAME can be unambiguously referred to even if
3498 there are multiple indirect base classes containing NAME. */
3499 const base_access ba = [scope, name] ()
3501 if (identifier_p (name))
3503 tree m = lookup_member (scope, name, /*protect=*/0,
3504 /*want_type=*/false, tf_none);
3505 if (!m || shared_member_p (m))
3506 return ba_any;
3508 return ba_check;
3509 } ();
3511 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3512 access_path = lookup_base (object_type, scope, ba, NULL, complain);
3513 if (access_path == error_mark_node)
3514 return error_mark_node;
3515 if (!access_path)
3517 if (any_dependent_bases_p (object_type))
3518 goto dependent;
3519 if (complain & tf_error)
3520 error ("%qT is not a base of %qT", scope, object_type);
3521 return error_mark_node;
3525 if (TREE_CODE (name) == BIT_NOT_EXPR)
3527 if (dependent_type_p (object_type))
3528 /* The destructor isn't declared yet. */
3529 goto dependent;
3530 member = lookup_destructor (object, scope, name, complain);
3532 else
3534 /* Look up the member. */
3535 access_failure_info afi;
3536 if (processing_template_decl)
3537 /* Even though this class member access expression is at this
3538 point not dependent, the member itself may be dependent, and
3539 we must not potentially push a access check for a dependent
3540 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3541 ahead of time here; we're going to redo this member lookup at
3542 instantiation time anyway. */
3543 push_deferring_access_checks (dk_no_check);
3544 member = lookup_member (access_path, name, /*protect=*/1,
3545 /*want_type=*/false, complain,
3546 &afi);
3547 if (processing_template_decl)
3548 pop_deferring_access_checks ();
3549 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
3550 if (member == NULL_TREE)
3552 if (dependent_type_p (object_type))
3553 /* Try again at instantiation time. */
3554 goto dependent;
3555 if (complain & tf_error)
3556 complain_about_unrecognized_member (access_path, name,
3557 object_type);
3558 return error_mark_node;
3560 if (member == error_mark_node)
3561 return error_mark_node;
3562 if (DECL_P (member)
3563 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
3564 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3565 wrong, so don't use it. */
3566 goto dependent;
3567 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
3568 goto dependent;
3571 if (is_template_id)
3573 tree templ = member;
3575 if (BASELINK_P (templ))
3576 member = lookup_template_function (templ, template_args);
3577 else if (variable_template_p (templ))
3578 member = (lookup_and_finish_template_variable
3579 (templ, template_args, complain));
3580 else
3582 if (complain & tf_error)
3583 error ("%qD is not a member template function", name);
3584 return error_mark_node;
3589 if (TREE_UNAVAILABLE (member))
3590 error_unavailable_use (member, NULL_TREE);
3591 else if (TREE_DEPRECATED (member))
3592 warn_deprecated_use (member, NULL_TREE);
3594 if (template_p)
3595 check_template_keyword (member);
3597 expr = build_class_member_access_expr (object, member, access_path,
3598 /*preserve_reference=*/false,
3599 complain);
3600 if (processing_template_decl && expr != error_mark_node)
3602 if (BASELINK_P (member))
3604 if (TREE_CODE (orig_name) == SCOPE_REF)
3605 BASELINK_QUALIFIED_P (member) = 1;
3606 orig_name = member;
3608 return build_min_non_dep (COMPONENT_REF, expr,
3609 orig_object, orig_name,
3610 NULL_TREE);
3613 return expr;
3616 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3617 type. */
3619 tree
3620 build_simple_component_ref (tree object, tree member)
3622 tree type = cp_build_qualified_type (TREE_TYPE (member),
3623 cp_type_quals (TREE_TYPE (object)));
3624 return build3_loc (input_location,
3625 COMPONENT_REF, type,
3626 object, member, NULL_TREE);
3629 /* Return an expression for the MEMBER_NAME field in the internal
3630 representation of PTRMEM, a pointer-to-member function. (Each
3631 pointer-to-member function type gets its own RECORD_TYPE so it is
3632 more convenient to access the fields by name than by FIELD_DECL.)
3633 This routine converts the NAME to a FIELD_DECL and then creates the
3634 node for the complete expression. */
3636 tree
3637 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3639 tree ptrmem_type;
3640 tree member;
3642 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3644 for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
3645 if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
3646 return e.value;
3647 gcc_unreachable ();
3650 /* This code is a stripped down version of
3651 build_class_member_access_expr. It does not work to use that
3652 routine directly because it expects the object to be of class
3653 type. */
3654 ptrmem_type = TREE_TYPE (ptrmem);
3655 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3656 for (member = TYPE_FIELDS (ptrmem_type); member;
3657 member = DECL_CHAIN (member))
3658 if (DECL_NAME (member) == member_name)
3659 break;
3660 return build_simple_component_ref (ptrmem, member);
3663 /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3664 and for any other relevant operator. */
3666 static tree
3667 op_unqualified_lookup (tree_code code, bool is_assign)
3669 tree lookups = NULL_TREE;
3671 if (cxx_dialect >= cxx20 && !is_assign)
3673 if (code == NE_EXPR)
3675 /* != can get rewritten in terms of ==. */
3676 tree fnname = ovl_op_identifier (false, EQ_EXPR);
3677 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3678 lookups = tree_cons (fnname, fns, lookups);
3680 else if (code == GT_EXPR || code == LE_EXPR
3681 || code == LT_EXPR || code == GE_EXPR)
3683 /* These can get rewritten in terms of <=>. */
3684 tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
3685 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3686 lookups = tree_cons (fnname, fns, lookups);
3690 tree fnname = ovl_op_identifier (is_assign, code);
3691 if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
3692 lookups = tree_cons (fnname, fns, lookups);
3694 if (lookups)
3695 return lookups;
3696 else
3697 return build_tree_list (NULL_TREE, NULL_TREE);
3700 /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3701 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3702 name lookup for the given operator. */
3704 tree
3705 build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
3707 if (lookups)
3708 /* We're partially instantiating a dependent operator expression, and
3709 LOOKUPS is the result of phase 1 name lookup that we performed
3710 earlier at template definition time, so just reuse the corresponding
3711 DEPENDENT_OPERATOR_TYPE. */
3712 return TREE_TYPE (lookups);
3714 /* Otherwise we're processing a dependent operator expression at template
3715 definition time, so perform phase 1 name lookup now. */
3716 lookups = op_unqualified_lookup (code, is_assign);
3718 tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
3719 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
3720 TREE_TYPE (lookups) = type;
3721 return type;
3724 /* Given an expression PTR for a pointer, return an expression
3725 for the value pointed to.
3726 ERRORSTRING is the name of the operator to appear in error messages.
3728 This function may need to overload OPERATOR_FNNAME.
3729 Must also handle REFERENCE_TYPEs for C++. */
3731 tree
3732 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3733 tree lookups, tsubst_flags_t complain)
3735 tree orig_expr = expr;
3736 tree rval;
3737 tree overload = NULL_TREE;
3739 if (processing_template_decl)
3741 /* Retain the type if we know the operand is a pointer. */
3742 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3744 if (expr == current_class_ptr
3745 || (TREE_CODE (expr) == NOP_EXPR
3746 && TREE_OPERAND (expr, 0) == current_class_ptr
3747 && (same_type_ignoring_top_level_qualifiers_p
3748 (TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
3749 return current_class_ref;
3750 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3752 if (type_dependent_expression_p (expr))
3754 expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
3755 TREE_TYPE (expr)
3756 = build_dependent_operator_type (lookups, INDIRECT_REF, false);
3757 return expr;
3761 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3762 NULL_TREE, NULL_TREE, lookups,
3763 &overload, complain);
3764 if (!rval)
3765 rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
3767 if (processing_template_decl && rval != error_mark_node)
3769 if (overload != NULL_TREE)
3770 return (build_min_non_dep_op_overload
3771 (INDIRECT_REF, rval, overload, orig_expr));
3773 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3775 else
3776 return rval;
3779 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3780 types or expressions. */
3782 static bool
3783 cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
3785 if (processing_template_decl)
3787 tree e = expr;
3788 STRIP_NOPS (e);
3789 if (dependent_type_p (type) || type_dependent_expression_p (e))
3790 return false;
3792 return strict_aliasing_warning (loc, type, expr);
3795 /* The implementation of the above, and of indirection implied by other
3796 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3798 static tree
3799 cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
3800 tsubst_flags_t complain, bool do_fold)
3802 tree pointer, type;
3804 /* RO_NULL should only be used with the folding entry points below, not
3805 cp_build_indirect_ref. */
3806 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3808 if (ptr == current_class_ptr
3809 || (TREE_CODE (ptr) == NOP_EXPR
3810 && TREE_OPERAND (ptr, 0) == current_class_ptr
3811 && (same_type_ignoring_top_level_qualifiers_p
3812 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3813 return current_class_ref;
3815 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3816 ? ptr : decay_conversion (ptr, complain));
3817 if (pointer == error_mark_node)
3818 return error_mark_node;
3820 type = TREE_TYPE (pointer);
3822 if (INDIRECT_TYPE_P (type))
3824 /* [expr.unary.op]
3826 If the type of the expression is "pointer to T," the type
3827 of the result is "T." */
3828 tree t = TREE_TYPE (type);
3830 if ((CONVERT_EXPR_P (ptr)
3831 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3832 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3834 /* If a warning is issued, mark it to avoid duplicates from
3835 the backend. This only needs to be done at
3836 warn_strict_aliasing > 2. */
3837 if (warn_strict_aliasing > 2
3838 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
3839 type, TREE_OPERAND (ptr, 0)))
3840 suppress_warning (ptr, OPT_Wstrict_aliasing);
3843 if (VOID_TYPE_P (t))
3845 /* A pointer to incomplete type (other than cv void) can be
3846 dereferenced [expr.unary.op]/1 */
3847 if (complain & tf_error)
3848 error_at (loc, "%qT is not a pointer-to-object type", type);
3849 return error_mark_node;
3851 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3852 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3853 /* The POINTER was something like `&x'. We simplify `*&x' to
3854 `x'. */
3855 return TREE_OPERAND (pointer, 0);
3856 else
3858 tree ref = build1 (INDIRECT_REF, t, pointer);
3860 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3861 so that we get the proper error message if the result is used
3862 to assign to. Also, &* is supposed to be a no-op. */
3863 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3864 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3865 TREE_SIDE_EFFECTS (ref)
3866 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3867 return ref;
3870 else if (!(complain & tf_error))
3871 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3873 /* `pointer' won't be an error_mark_node if we were given a
3874 pointer to member, so it's cool to check for this here. */
3875 else if (TYPE_PTRMEM_P (type))
3876 switch (errorstring)
3878 case RO_ARRAY_INDEXING:
3879 error_at (loc,
3880 "invalid use of array indexing on pointer to member");
3881 break;
3882 case RO_UNARY_STAR:
3883 error_at (loc, "invalid use of unary %<*%> on pointer to member");
3884 break;
3885 case RO_IMPLICIT_CONVERSION:
3886 error_at (loc, "invalid use of implicit conversion on pointer "
3887 "to member");
3888 break;
3889 case RO_ARROW_STAR:
3890 error_at (loc, "left hand operand of %<->*%> must be a pointer to "
3891 "class, but is a pointer to member of type %qT", type);
3892 break;
3893 default:
3894 gcc_unreachable ();
3896 else if (pointer != error_mark_node)
3897 invalid_indirection_error (loc, type, errorstring);
3899 return error_mark_node;
3902 /* Entry point used by c-common, which expects folding. */
3904 tree
3905 build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
3907 return cp_build_indirect_ref_1 (loc, ptr, errorstring,
3908 tf_warning_or_error, true);
3911 /* Entry point used by internal indirection needs that don't correspond to any
3912 syntactic construct. */
3914 tree
3915 cp_build_fold_indirect_ref (tree pointer)
3917 return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
3918 tf_warning_or_error, true);
3921 /* Entry point used by indirection needs that correspond to some syntactic
3922 construct. */
3924 tree
3925 cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
3926 tsubst_flags_t complain)
3928 return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
3931 /* This handles expressions of the form "a[i]", which denotes
3932 an array reference.
3934 This is logically equivalent in C to *(a+i), but we may do it differently.
3935 If A is a variable or a member, we generate a primitive ARRAY_REF.
3936 This avoids forcing the array out of registers, and can work on
3937 arrays that are not lvalues (for example, members of structures returned
3938 by functions).
3940 If INDEX is of some user-defined type, it must be converted to
3941 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3942 will inherit the type of the array, which will be some pointer type.
3944 LOC is the location to use in building the array reference. */
3946 tree
3947 cp_build_array_ref (location_t loc, tree array, tree idx,
3948 tsubst_flags_t complain)
3950 tree first = NULL_TREE;
3951 tree ret;
3953 if (idx == 0)
3955 if (complain & tf_error)
3956 error_at (loc, "subscript missing in array reference");
3957 return error_mark_node;
3960 if (TREE_TYPE (array) == error_mark_node
3961 || TREE_TYPE (idx) == error_mark_node)
3962 return error_mark_node;
3964 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3965 inside it. */
3966 switch (TREE_CODE (array))
3968 case COMPOUND_EXPR:
3970 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3971 complain);
3972 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3973 TREE_OPERAND (array, 0), value);
3974 SET_EXPR_LOCATION (ret, loc);
3975 return ret;
3978 case COND_EXPR:
3979 ret = build_conditional_expr
3980 (loc, TREE_OPERAND (array, 0),
3981 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3982 complain),
3983 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3984 complain),
3985 complain);
3986 protected_set_expr_location (ret, loc);
3987 return ret;
3989 default:
3990 break;
3993 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3995 /* 0[array] */
3996 if (TREE_CODE (TREE_TYPE (idx)) == ARRAY_TYPE)
3998 std::swap (array, idx);
3999 if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (array))
4000 idx = first = save_expr (idx);
4003 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
4005 tree rval, type;
4007 warn_array_subscript_with_type_char (loc, idx);
4009 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
4011 if (complain & tf_error)
4012 error_at (loc, "array subscript is not an integer");
4013 return error_mark_node;
4016 /* Apply integral promotions *after* noticing character types.
4017 (It is unclear why we do these promotions -- the standard
4018 does not say that we should. In fact, the natural thing would
4019 seem to be to convert IDX to ptrdiff_t; we're performing
4020 pointer arithmetic.) */
4021 idx = cp_perform_integral_promotions (idx, complain);
4023 idx = maybe_fold_non_dependent_expr (idx, complain);
4025 /* An array that is indexed by a non-constant
4026 cannot be stored in a register; we must be able to do
4027 address arithmetic on its address.
4028 Likewise an array of elements of variable size. */
4029 if (TREE_CODE (idx) != INTEGER_CST
4030 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
4031 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
4032 != INTEGER_CST)))
4034 if (!cxx_mark_addressable (array, true))
4035 return error_mark_node;
4038 /* An array that is indexed by a constant value which is not within
4039 the array bounds cannot be stored in a register either; because we
4040 would get a crash in store_bit_field/extract_bit_field when trying
4041 to access a non-existent part of the register. */
4042 if (TREE_CODE (idx) == INTEGER_CST
4043 && TYPE_DOMAIN (TREE_TYPE (array))
4044 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
4046 if (!cxx_mark_addressable (array))
4047 return error_mark_node;
4050 /* Note in C++ it is valid to subscript a `register' array, since
4051 it is valid to take the address of something with that
4052 storage specification. */
4053 if (extra_warnings)
4055 tree foo = array;
4056 while (TREE_CODE (foo) == COMPONENT_REF)
4057 foo = TREE_OPERAND (foo, 0);
4058 if (VAR_P (foo) && DECL_REGISTER (foo)
4059 && (complain & tf_warning))
4060 warning_at (loc, OPT_Wextra,
4061 "subscripting array declared %<register%>");
4064 type = TREE_TYPE (TREE_TYPE (array));
4065 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
4066 /* Array ref is const/volatile if the array elements are
4067 or if the array is.. */
4068 TREE_READONLY (rval)
4069 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
4070 TREE_SIDE_EFFECTS (rval)
4071 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
4072 TREE_THIS_VOLATILE (rval)
4073 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
4074 ret = require_complete_type (rval, complain);
4075 protected_set_expr_location (ret, loc);
4076 if (non_lvalue)
4077 ret = non_lvalue_loc (loc, ret);
4078 if (first)
4079 ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4080 return ret;
4084 tree ar = cp_default_conversion (array, complain);
4085 tree ind = cp_default_conversion (idx, complain);
4087 if (!first && flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
4088 ar = first = save_expr (ar);
4090 /* Put the integer in IND to simplify error checking. */
4091 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
4092 std::swap (ar, ind);
4094 if (ar == error_mark_node || ind == error_mark_node)
4095 return error_mark_node;
4097 if (!TYPE_PTR_P (TREE_TYPE (ar)))
4099 if (complain & tf_error)
4100 error_at (loc, "subscripted value is neither array nor pointer");
4101 return error_mark_node;
4103 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
4105 if (complain & tf_error)
4106 error_at (loc, "array subscript is not an integer");
4107 return error_mark_node;
4110 warn_array_subscript_with_type_char (loc, idx);
4112 ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
4113 if (first)
4114 ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
4115 ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
4116 protected_set_expr_location (ret, loc);
4117 if (non_lvalue)
4118 ret = non_lvalue_loc (loc, ret);
4119 return ret;
4123 /* Entry point for Obj-C++. */
4125 tree
4126 build_array_ref (location_t loc, tree array, tree idx)
4128 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
4131 /* Resolve a pointer to member function. INSTANCE is the object
4132 instance to use, if the member points to a virtual member.
4134 This used to avoid checking for virtual functions if basetype
4135 has no virtual functions, according to an earlier ANSI draft.
4136 With the final ISO C++ rules, such an optimization is
4137 incorrect: A pointer to a derived member can be static_cast
4138 to pointer-to-base-member, as long as the dynamic object
4139 later has the right member. So now we only do this optimization
4140 when we know the dynamic type of the object. */
4142 tree
4143 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
4144 tsubst_flags_t complain)
4146 if (TREE_CODE (function) == OFFSET_REF)
4147 function = TREE_OPERAND (function, 1);
4149 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
4151 tree idx, delta, e1, e2, e3, vtbl;
4152 bool nonvirtual;
4153 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
4154 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
4156 tree instance_ptr = *instance_ptrptr;
4157 tree instance_save_expr = 0;
4158 if (instance_ptr == error_mark_node)
4160 if (TREE_CODE (function) == PTRMEM_CST)
4162 /* Extracting the function address from a pmf is only
4163 allowed with -Wno-pmf-conversions. It only works for
4164 pmf constants. */
4165 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
4166 e1 = convert (fntype, e1);
4167 return e1;
4169 else
4171 if (complain & tf_error)
4172 error ("object missing in use of %qE", function);
4173 return error_mark_node;
4177 /* True if we know that the dynamic type of the object doesn't have
4178 virtual functions, so we can assume the PFN field is a pointer. */
4179 nonvirtual = (COMPLETE_TYPE_P (basetype)
4180 && !TYPE_POLYMORPHIC_P (basetype)
4181 && resolves_to_fixed_type_p (instance_ptr, 0));
4183 /* If we don't really have an object (i.e. in an ill-formed
4184 conversion from PMF to pointer), we can't resolve virtual
4185 functions anyway. */
4186 if (!nonvirtual && is_dummy_object (instance_ptr))
4187 nonvirtual = true;
4189 if (TREE_SIDE_EFFECTS (instance_ptr))
4190 instance_ptr = instance_save_expr = save_expr (instance_ptr);
4192 if (TREE_SIDE_EFFECTS (function))
4193 function = save_expr (function);
4195 /* Start by extracting all the information from the PMF itself. */
4196 e3 = pfn_from_ptrmemfunc (function);
4197 delta = delta_from_ptrmemfunc (function);
4198 idx = build1 (NOP_EXPR, vtable_index_type, e3);
4199 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
4201 int flag_sanitize_save;
4202 case ptrmemfunc_vbit_in_pfn:
4203 e1 = cp_build_binary_op (input_location,
4204 BIT_AND_EXPR, idx, integer_one_node,
4205 complain);
4206 idx = cp_build_binary_op (input_location,
4207 MINUS_EXPR, idx, integer_one_node,
4208 complain);
4209 if (idx == error_mark_node)
4210 return error_mark_node;
4211 break;
4213 case ptrmemfunc_vbit_in_delta:
4214 e1 = cp_build_binary_op (input_location,
4215 BIT_AND_EXPR, delta, integer_one_node,
4216 complain);
4217 /* Don't instrument the RSHIFT_EXPR we're about to create because
4218 we're going to use DELTA number of times, and that wouldn't play
4219 well with SAVE_EXPRs therein. */
4220 flag_sanitize_save = flag_sanitize;
4221 flag_sanitize = 0;
4222 delta = cp_build_binary_op (input_location,
4223 RSHIFT_EXPR, delta, integer_one_node,
4224 complain);
4225 flag_sanitize = flag_sanitize_save;
4226 if (delta == error_mark_node)
4227 return error_mark_node;
4228 break;
4230 default:
4231 gcc_unreachable ();
4234 if (e1 == error_mark_node)
4235 return error_mark_node;
4237 /* Convert down to the right base before using the instance. A
4238 special case is that in a pointer to member of class C, C may
4239 be incomplete. In that case, the function will of course be
4240 a member of C, and no conversion is required. In fact,
4241 lookup_base will fail in that case, because incomplete
4242 classes do not have BINFOs. */
4243 if (!same_type_ignoring_top_level_qualifiers_p
4244 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
4246 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
4247 basetype, ba_check, NULL, complain);
4248 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
4249 1, complain);
4250 if (instance_ptr == error_mark_node)
4251 return error_mark_node;
4253 /* ...and then the delta in the PMF. */
4254 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
4256 /* Hand back the adjusted 'this' argument to our caller. */
4257 *instance_ptrptr = instance_ptr;
4259 if (nonvirtual)
4260 /* Now just return the pointer. */
4261 return e3;
4263 /* Next extract the vtable pointer from the object. */
4264 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
4265 instance_ptr);
4266 vtbl = cp_build_fold_indirect_ref (vtbl);
4267 if (vtbl == error_mark_node)
4268 return error_mark_node;
4270 /* Finally, extract the function pointer from the vtable. */
4271 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
4272 e2 = cp_build_fold_indirect_ref (e2);
4273 if (e2 == error_mark_node)
4274 return error_mark_node;
4275 TREE_CONSTANT (e2) = 1;
4277 /* When using function descriptors, the address of the
4278 vtable entry is treated as a function pointer. */
4279 if (TARGET_VTABLE_USES_DESCRIPTORS)
4280 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
4281 cp_build_addr_expr (e2, complain));
4283 e2 = fold_convert (TREE_TYPE (e3), e2);
4284 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
4285 if (e1 == error_mark_node)
4286 return error_mark_node;
4288 /* Make sure this doesn't get evaluated first inside one of the
4289 branches of the COND_EXPR. */
4290 if (instance_save_expr)
4291 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
4292 instance_save_expr, e1);
4294 function = e1;
4296 return function;
4299 /* Used by the C-common bits. */
4300 tree
4301 build_function_call (location_t /*loc*/,
4302 tree function, tree params)
4304 return cp_build_function_call (function, params, tf_warning_or_error);
4307 /* Used by the C-common bits. */
4308 tree
4309 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
4310 tree function, vec<tree, va_gc> *params,
4311 vec<tree, va_gc> * /*origtypes*/, tree orig_function)
4313 vec<tree, va_gc> *orig_params = params;
4314 tree ret = cp_build_function_call_vec (function, &params,
4315 tf_warning_or_error, orig_function);
4317 /* cp_build_function_call_vec can reallocate PARAMS by adding
4318 default arguments. That should never happen here. Verify
4319 that. */
4320 gcc_assert (params == orig_params);
4322 return ret;
4325 /* Build a function call using a tree list of arguments. */
4327 static tree
4328 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
4330 tree ret;
4332 releasing_vec vec;
4333 for (; params != NULL_TREE; params = TREE_CHAIN (params))
4334 vec_safe_push (vec, TREE_VALUE (params));
4335 ret = cp_build_function_call_vec (function, &vec, complain);
4336 return ret;
4339 /* Build a function call using varargs. */
4341 tree
4342 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
4344 va_list args;
4345 tree ret, t;
4347 releasing_vec vec;
4348 va_start (args, complain);
4349 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
4350 vec_safe_push (vec, t);
4351 va_end (args);
4352 ret = cp_build_function_call_vec (function, &vec, complain);
4353 return ret;
4356 /* Build a function call using a vector of arguments.
4357 If FUNCTION is the result of resolving an overloaded target built-in,
4358 ORIG_FNDECL is the original function decl, otherwise it is null.
4359 PARAMS may be NULL if there are no parameters. This changes the
4360 contents of PARAMS. */
4362 tree
4363 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
4364 tsubst_flags_t complain, tree orig_fndecl)
4366 tree fntype, fndecl;
4367 int is_method;
4368 tree original = function;
4369 int nargs;
4370 tree *argarray;
4371 tree parm_types;
4372 vec<tree, va_gc> *allocated = NULL;
4373 tree ret;
4375 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4376 expressions, like those used for ObjC messenger dispatches. */
4377 if (params != NULL && !vec_safe_is_empty (*params))
4378 function = objc_rewrite_function_call (function, (**params)[0]);
4380 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4381 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4382 if (TREE_CODE (function) == NOP_EXPR
4383 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
4384 function = TREE_OPERAND (function, 0);
4386 if (TREE_CODE (function) == FUNCTION_DECL)
4388 if (!mark_used (function, complain))
4389 return error_mark_node;
4390 fndecl = function;
4392 /* Convert anything with function type to a pointer-to-function. */
4393 if (DECL_MAIN_P (function))
4395 if (complain & tf_error)
4396 pedwarn (input_location, OPT_Wpedantic,
4397 "ISO C++ forbids calling %<::main%> from within program");
4398 else
4399 return error_mark_node;
4401 function = build_addr_func (function, complain);
4403 else
4405 fndecl = NULL_TREE;
4407 function = build_addr_func (function, complain);
4410 if (function == error_mark_node)
4411 return error_mark_node;
4413 fntype = TREE_TYPE (function);
4415 if (TYPE_PTRMEMFUNC_P (fntype))
4417 if (complain & tf_error)
4418 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4419 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4420 original, original);
4421 return error_mark_node;
4424 is_method = (TYPE_PTR_P (fntype)
4425 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
4427 if (!(TYPE_PTRFN_P (fntype)
4428 || is_method
4429 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
4431 if (complain & tf_error)
4433 if (!flag_diagnostics_show_caret)
4434 error_at (input_location,
4435 "%qE cannot be used as a function", original);
4436 else if (DECL_P (original))
4437 error_at (input_location,
4438 "%qD cannot be used as a function", original);
4439 else
4440 error_at (input_location,
4441 "expression cannot be used as a function");
4444 return error_mark_node;
4447 /* fntype now gets the type of function pointed to. */
4448 fntype = TREE_TYPE (fntype);
4449 parm_types = TYPE_ARG_TYPES (fntype);
4451 if (params == NULL)
4453 allocated = make_tree_vector ();
4454 params = &allocated;
4457 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
4458 complain);
4459 if (nargs < 0)
4460 return error_mark_node;
4462 argarray = (*params)->address ();
4464 /* Check for errors in format strings and inappropriately
4465 null parameters. */
4466 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
4467 nargs, argarray, NULL);
4469 ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
4471 if (warned_p)
4473 tree c = extract_call_expr (ret);
4474 if (TREE_CODE (c) == CALL_EXPR)
4475 suppress_warning (c, OPT_Wnonnull);
4478 if (allocated != NULL)
4479 release_tree_vector (allocated);
4481 return ret;
4484 /* Subroutine of convert_arguments.
4485 Print an error message about a wrong number of arguments. */
4487 static void
4488 error_args_num (location_t loc, tree fndecl, bool too_many_p)
4490 if (fndecl)
4492 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
4494 if (DECL_NAME (fndecl) == NULL_TREE
4495 || (DECL_NAME (fndecl)
4496 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
4497 error_at (loc,
4498 too_many_p
4499 ? G_("too many arguments to constructor %q#D")
4500 : G_("too few arguments to constructor %q#D"),
4501 fndecl);
4502 else
4503 error_at (loc,
4504 too_many_p
4505 ? G_("too many arguments to member function %q#D")
4506 : G_("too few arguments to member function %q#D"),
4507 fndecl);
4509 else
4510 error_at (loc,
4511 too_many_p
4512 ? G_("too many arguments to function %q#D")
4513 : G_("too few arguments to function %q#D"),
4514 fndecl);
4515 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
4516 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
4518 else
4520 if (c_dialect_objc () && objc_message_selector ())
4521 error_at (loc,
4522 too_many_p
4523 ? G_("too many arguments to method %q#D")
4524 : G_("too few arguments to method %q#D"),
4525 objc_message_selector ());
4526 else
4527 error_at (loc, too_many_p ? G_("too many arguments to function")
4528 : G_("too few arguments to function"));
4532 /* Convert the actual parameter expressions in the list VALUES to the
4533 types in the list TYPELIST. The converted expressions are stored
4534 back in the VALUES vector.
4535 If parmdecls is exhausted, or when an element has NULL as its type,
4536 perform the default conversions.
4538 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4540 This is also where warnings about wrong number of args are generated.
4542 Returns the actual number of arguments processed (which might be less
4543 than the length of the vector), or -1 on error.
4545 In C++, unspecified trailing parameters can be filled in with their
4546 default arguments, if such were specified. Do so here. */
4548 static int
4549 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
4550 int flags, tsubst_flags_t complain)
4552 tree typetail;
4553 unsigned int i;
4555 /* Argument passing is always copy-initialization. */
4556 flags |= LOOKUP_ONLYCONVERTING;
4558 for (i = 0, typetail = typelist;
4559 i < vec_safe_length (*values);
4560 i++)
4562 tree type = typetail ? TREE_VALUE (typetail) : 0;
4563 tree val = (**values)[i];
4565 if (val == error_mark_node || type == error_mark_node)
4566 return -1;
4568 if (type == void_type_node)
4570 if (complain & tf_error)
4572 error_args_num (input_location, fndecl, /*too_many_p=*/true);
4573 return i;
4575 else
4576 return -1;
4579 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4580 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4581 if (TREE_CODE (val) == NOP_EXPR
4582 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
4583 && (type == 0 || !TYPE_REF_P (type)))
4584 val = TREE_OPERAND (val, 0);
4586 if (type == 0 || !TYPE_REF_P (type))
4588 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
4589 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
4590 val = decay_conversion (val, complain);
4593 if (val == error_mark_node)
4594 return -1;
4596 if (type != 0)
4598 /* Formal parm type is specified by a function prototype. */
4599 tree parmval;
4601 if (!COMPLETE_TYPE_P (complete_type (type)))
4603 if (complain & tf_error)
4605 location_t loc = EXPR_LOC_OR_LOC (val, input_location);
4606 if (fndecl)
4608 auto_diagnostic_group d;
4609 error_at (loc,
4610 "parameter %P of %qD has incomplete type %qT",
4611 i, fndecl, type);
4612 inform (get_fndecl_argument_location (fndecl, i),
4613 " declared here");
4615 else
4616 error_at (loc, "parameter %P has incomplete type %qT", i,
4617 type);
4619 parmval = error_mark_node;
4621 else
4623 parmval = convert_for_initialization
4624 (NULL_TREE, type, val, flags,
4625 ICR_ARGPASS, fndecl, i, complain);
4626 parmval = convert_for_arg_passing (type, parmval, complain);
4629 if (parmval == error_mark_node)
4630 return -1;
4632 (**values)[i] = parmval;
4634 else
4636 int magic = fndecl ? magic_varargs_p (fndecl) : 0;
4637 if (magic)
4639 /* Don't truncate excess precision to the semantic type. */
4640 if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
4641 val = TREE_OPERAND (val, 0);
4642 /* Don't do ellipsis conversion for __built_in_constant_p
4643 as this will result in spurious errors for non-trivial
4644 types. */
4645 val = require_complete_type (val, complain);
4647 else
4648 val = convert_arg_to_ellipsis (val, complain);
4650 (**values)[i] = val;
4653 if (typetail)
4654 typetail = TREE_CHAIN (typetail);
4657 if (typetail != 0 && typetail != void_list_node)
4659 /* See if there are default arguments that can be used. Because
4660 we hold default arguments in the FUNCTION_TYPE (which is so
4661 wrong), we can see default parameters here from deduced
4662 contexts (and via typeof) for indirect function calls.
4663 Fortunately we know whether we have a function decl to
4664 provide default arguments in a language conformant
4665 manner. */
4666 if (fndecl && TREE_PURPOSE (typetail)
4667 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
4669 for (; typetail != void_list_node; ++i)
4671 /* After DR777, with explicit template args we can end up with a
4672 default argument followed by no default argument. */
4673 if (!TREE_PURPOSE (typetail))
4674 break;
4675 tree parmval
4676 = convert_default_arg (TREE_VALUE (typetail),
4677 TREE_PURPOSE (typetail),
4678 fndecl, i, complain);
4680 if (parmval == error_mark_node)
4681 return -1;
4683 vec_safe_push (*values, parmval);
4684 typetail = TREE_CHAIN (typetail);
4685 /* ends with `...'. */
4686 if (typetail == NULL_TREE)
4687 break;
4691 if (typetail && typetail != void_list_node)
4693 if (complain & tf_error)
4694 error_args_num (input_location, fndecl, /*too_many_p=*/false);
4695 return -1;
4699 return (int) i;
4702 /* Build a binary-operation expression, after performing default
4703 conversions on the operands. CODE is the kind of expression to
4704 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4705 are the tree codes which correspond to ARG1 and ARG2 when issuing
4706 warnings about possibly misplaced parentheses. They may differ
4707 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4708 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4709 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4710 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4711 ARG2_CODE as ERROR_MARK. */
4713 tree
4714 build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
4715 enum tree_code arg1_code, tree arg2,
4716 enum tree_code arg2_code, tree lookups,
4717 tree *overload_p, tsubst_flags_t complain)
4719 tree orig_arg1;
4720 tree orig_arg2;
4721 tree expr;
4722 tree overload = NULL_TREE;
4724 orig_arg1 = arg1;
4725 orig_arg2 = arg2;
4727 if (processing_template_decl)
4729 if (type_dependent_expression_p (arg1)
4730 || type_dependent_expression_p (arg2))
4732 expr = build_min_nt_loc (loc, code, arg1, arg2);
4733 TREE_TYPE (expr)
4734 = build_dependent_operator_type (lookups, code, false);
4735 return expr;
4739 if (code == DOTSTAR_EXPR)
4740 expr = build_m_component_ref (arg1, arg2, complain);
4741 else
4742 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4743 lookups, &overload, complain);
4745 if (overload_p != NULL)
4746 *overload_p = overload;
4748 /* Check for cases such as x+y<<z which users are likely to
4749 misinterpret. But don't warn about obj << x + y, since that is a
4750 common idiom for I/O. */
4751 if (warn_parentheses
4752 && (complain & tf_warning)
4753 && !processing_template_decl
4754 && !error_operand_p (arg1)
4755 && !error_operand_p (arg2)
4756 && (code != LSHIFT_EXPR
4757 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4758 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4759 arg2_code, orig_arg2);
4761 if (processing_template_decl && expr != error_mark_node)
4763 if (overload != NULL_TREE)
4764 return (build_min_non_dep_op_overload
4765 (code, expr, overload, orig_arg1, orig_arg2));
4767 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4770 return expr;
4773 /* Build and return an ARRAY_REF expression. */
4775 tree
4776 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4777 tsubst_flags_t complain)
4779 tree orig_arg1 = arg1;
4780 tree orig_arg2 = arg2;
4781 tree expr;
4782 tree overload = NULL_TREE;
4784 if (processing_template_decl)
4786 if (type_dependent_expression_p (arg1)
4787 || type_dependent_expression_p (arg2))
4788 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4789 NULL_TREE, NULL_TREE);
4792 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4793 NULL_TREE, NULL_TREE, &overload, complain);
4795 if (processing_template_decl && expr != error_mark_node)
4797 if (overload != NULL_TREE)
4798 return (build_min_non_dep_op_overload
4799 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4801 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4802 NULL_TREE, NULL_TREE);
4804 return expr;
4807 /* Build an OpenMP array section reference, creating an exact type for the
4808 resulting expression based on the element type and bounds if possible. If
4809 we have variable bounds, create an incomplete array type for the result
4810 instead. */
4812 tree
4813 build_omp_array_section (location_t loc, tree array_expr, tree index,
4814 tree length)
4816 tree type = TREE_TYPE (array_expr);
4817 gcc_assert (type);
4818 type = non_reference (type);
4820 tree sectype, eltype = TREE_TYPE (type);
4822 /* It's not an array or pointer type. Just reuse the type of the
4823 original expression as the type of the array section (an error will be
4824 raised anyway, later). */
4825 if (eltype == NULL_TREE)
4826 sectype = TREE_TYPE (array_expr);
4827 else
4829 tree idxtype = NULL_TREE;
4831 /* If we know the integer bounds, create an index type with exact
4832 low/high (or zero/length) bounds. Otherwise, create an incomplete
4833 array type. (This mostly only affects diagnostics.) */
4834 if (index != NULL_TREE
4835 && length != NULL_TREE
4836 && TREE_CODE (index) == INTEGER_CST
4837 && TREE_CODE (length) == INTEGER_CST)
4839 tree low = fold_convert (sizetype, index);
4840 tree high = fold_convert (sizetype, length);
4841 high = size_binop (PLUS_EXPR, low, high);
4842 high = size_binop (MINUS_EXPR, high, size_one_node);
4843 idxtype = build_range_type (sizetype, low, high);
4845 else if ((index == NULL_TREE || integer_zerop (index))
4846 && length != NULL_TREE
4847 && TREE_CODE (length) == INTEGER_CST)
4848 idxtype = build_index_type (length);
4850 sectype = build_array_type (eltype, idxtype);
4853 return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array_expr, index,
4854 length);
4857 /* Return whether OP is an expression of enum type cast to integer
4858 type. In C++ even unsigned enum types are cast to signed integer
4859 types. We do not want to issue warnings about comparisons between
4860 signed and unsigned types when one of the types is an enum type.
4861 Those warnings are always false positives in practice. */
4863 static bool
4864 enum_cast_to_int (tree op)
4866 if (CONVERT_EXPR_P (op)
4867 && TREE_TYPE (op) == integer_type_node
4868 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4869 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4870 return true;
4872 /* The cast may have been pushed into a COND_EXPR. */
4873 if (TREE_CODE (op) == COND_EXPR)
4874 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4875 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4877 return false;
4880 /* For the c-common bits. */
4881 tree
4882 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4883 bool /*convert_p*/)
4885 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4888 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4889 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4891 static tree
4892 build_vec_cmp (tree_code code, tree type,
4893 tree arg0, tree arg1)
4895 tree zero_vec = build_zero_cst (type);
4896 tree minus_one_vec = build_minus_one_cst (type);
4897 tree cmp_type = truth_type_for (TREE_TYPE (arg0));
4898 tree cmp = build2 (code, cmp_type, arg0, arg1);
4899 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4902 /* Possibly warn about an address never being NULL. */
4904 static void
4905 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4907 /* Prevent warnings issued for macro expansion. */
4908 if (!warn_address
4909 || (complain & tf_warning) == 0
4910 || c_inhibit_evaluation_warnings != 0
4911 || from_macro_expansion_at (location)
4912 || warning_suppressed_p (op, OPT_Waddress))
4913 return;
4915 tree cop = fold_for_warn (op);
4917 if (TREE_CODE (cop) == NON_LVALUE_EXPR)
4918 /* Unwrap the expression for C++ 98. */
4919 cop = TREE_OPERAND (cop, 0);
4921 if (TREE_CODE (cop) == PTRMEM_CST)
4923 /* The address of a nonstatic data member is never null. */
4924 warning_at (location, OPT_Waddress,
4925 "the address %qE will never be NULL",
4926 cop);
4927 return;
4930 if (TREE_CODE (cop) == NOP_EXPR)
4932 /* Allow casts to intptr_t to suppress the warning. */
4933 tree type = TREE_TYPE (cop);
4934 if (TREE_CODE (type) == INTEGER_TYPE)
4935 return;
4937 STRIP_NOPS (cop);
4940 bool warned = false;
4941 if (TREE_CODE (cop) == ADDR_EXPR)
4943 cop = TREE_OPERAND (cop, 0);
4945 /* Set to true in the loop below if OP dereferences its operand.
4946 In such a case the ultimate target need not be a decl for
4947 the null [in]equality test to be necessarily constant. */
4948 bool deref = false;
4950 /* Get the outermost array or object, or member. */
4951 while (handled_component_p (cop))
4953 if (TREE_CODE (cop) == COMPONENT_REF)
4955 /* Get the member (its address is never null). */
4956 cop = TREE_OPERAND (cop, 1);
4957 break;
4960 /* Get the outer array/object to refer to in the warning. */
4961 cop = TREE_OPERAND (cop, 0);
4962 deref = true;
4965 if ((!deref && !decl_with_nonnull_addr_p (cop))
4966 || from_macro_expansion_at (location)
4967 || warning_suppressed_p (cop, OPT_Waddress))
4968 return;
4970 warned = warning_at (location, OPT_Waddress,
4971 "the address of %qD will never be NULL", cop);
4972 op = cop;
4974 else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
4976 /* Adding zero to the null pointer is well-defined in C++. When
4977 the offset is unknown (i.e., not a constant) warn anyway since
4978 it's less likely that the pointer operand is null than not. */
4979 tree off = TREE_OPERAND (cop, 1);
4980 if (!integer_zerop (off)
4981 && !warning_suppressed_p (cop, OPT_Waddress))
4983 tree base = TREE_OPERAND (cop, 0);
4984 STRIP_NOPS (base);
4985 if (TYPE_REF_P (TREE_TYPE (base)))
4986 warning_at (location, OPT_Waddress, "the compiler can assume that "
4987 "the address of %qE will never be NULL", base);
4988 else
4989 warning_at (location, OPT_Waddress, "comparing the result of "
4990 "pointer addition %qE and NULL", cop);
4992 return;
4994 else if (CONVERT_EXPR_P (op)
4995 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4997 STRIP_NOPS (op);
4999 if (TREE_CODE (op) == COMPONENT_REF)
5000 op = TREE_OPERAND (op, 1);
5002 if (DECL_P (op))
5003 warned = warning_at (location, OPT_Waddress,
5004 "the compiler can assume that the address of "
5005 "%qD will never be NULL", op);
5008 if (warned && DECL_P (op))
5009 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
5012 /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5013 the other operand is of a different enumeration type or a floating-point
5014 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5015 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5016 and LOC is the location for the whole binary expression.
5017 For C++26 this is ill-formed rather than deprecated.
5018 Return true for SFINAE errors.
5019 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5021 static bool
5022 do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
5023 tree type1, tsubst_flags_t complain)
5025 if (TREE_CODE (type0) == ENUMERAL_TYPE
5026 && TREE_CODE (type1) == ENUMERAL_TYPE
5027 && TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
5029 if (cxx_dialect >= cxx26)
5031 if ((complain & tf_warning_or_error) == 0)
5032 return true;
5034 else if ((complain & tf_warning) == 0)
5035 return false;
5036 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5037 Otherwise, warn if -Wenum-conversion is on. */
5038 enum opt_code opt;
5039 if (warn_deprecated_enum_enum_conv)
5040 opt = OPT_Wdeprecated_enum_enum_conversion;
5041 else if (warn_enum_conversion)
5042 opt = OPT_Wenum_conversion;
5043 else
5044 return false;
5046 switch (code)
5048 case GT_EXPR:
5049 case LT_EXPR:
5050 case GE_EXPR:
5051 case LE_EXPR:
5052 case EQ_EXPR:
5053 case NE_EXPR:
5054 /* Comparisons are handled by -Wenum-compare. */
5055 return false;
5056 case SPACESHIP_EXPR:
5057 /* This is invalid, don't warn. */
5058 return false;
5059 case BIT_AND_EXPR:
5060 case BIT_IOR_EXPR:
5061 case BIT_XOR_EXPR:
5062 if (cxx_dialect >= cxx26)
5063 pedwarn (loc, opt, "bitwise operation between different "
5064 "enumeration types %qT and %qT", type0, type1);
5065 else
5066 warning_at (loc, opt, "bitwise operation between different "
5067 "enumeration types %qT and %qT is deprecated",
5068 type0, type1);
5069 return false;
5070 default:
5071 if (cxx_dialect >= cxx26)
5072 pedwarn (loc, opt, "arithmetic between different enumeration "
5073 "types %qT and %qT", type0, type1);
5074 else
5075 warning_at (loc, opt, "arithmetic between different enumeration "
5076 "types %qT and %qT is deprecated", type0, type1);
5077 return false;
5080 else if ((TREE_CODE (type0) == ENUMERAL_TYPE
5081 && SCALAR_FLOAT_TYPE_P (type1))
5082 || (SCALAR_FLOAT_TYPE_P (type0)
5083 && TREE_CODE (type1) == ENUMERAL_TYPE))
5085 if (cxx_dialect >= cxx26)
5087 if ((complain & tf_warning_or_error) == 0)
5088 return true;
5090 else if ((complain & tf_warning) == 0)
5091 return false;
5092 const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
5093 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5094 Otherwise, warn if -Wenum-conversion is on. */
5095 enum opt_code opt;
5096 if (warn_deprecated_enum_float_conv)
5097 opt = OPT_Wdeprecated_enum_float_conversion;
5098 else if (warn_enum_conversion)
5099 opt = OPT_Wenum_conversion;
5100 else
5101 return false;
5103 switch (code)
5105 case GT_EXPR:
5106 case LT_EXPR:
5107 case GE_EXPR:
5108 case LE_EXPR:
5109 case EQ_EXPR:
5110 case NE_EXPR:
5111 if (enum_first_p && cxx_dialect >= cxx26)
5112 pedwarn (loc, opt, "comparison of enumeration type %qT with "
5113 "floating-point type %qT", type0, type1);
5114 else if (cxx_dialect >= cxx26)
5115 pedwarn (loc, opt, "comparison of floating-point type %qT "
5116 "with enumeration type %qT", type0, type1);
5117 else if (enum_first_p)
5118 warning_at (loc, opt, "comparison of enumeration type %qT with "
5119 "floating-point type %qT is deprecated",
5120 type0, type1);
5121 else
5122 warning_at (loc, opt, "comparison of floating-point type %qT "
5123 "with enumeration type %qT is deprecated",
5124 type0, type1);
5125 return false;
5126 case SPACESHIP_EXPR:
5127 /* This is invalid, don't warn. */
5128 return false;
5129 default:
5130 if (enum_first_p && cxx_dialect >= cxx26)
5131 pedwarn (loc, opt, "arithmetic between enumeration type %qT "
5132 "and floating-point type %qT", type0, type1);
5133 else if (cxx_dialect >= cxx26)
5134 pedwarn (loc, opt, "arithmetic between floating-point type %qT "
5135 "and enumeration type %qT", type0, type1);
5136 else if (enum_first_p)
5137 warning_at (loc, opt, "arithmetic between enumeration type %qT "
5138 "and floating-point type %qT is deprecated",
5139 type0, type1);
5140 else
5141 warning_at (loc, opt, "arithmetic between floating-point type %qT "
5142 "and enumeration type %qT is deprecated",
5143 type0, type1);
5144 return false;
5147 return false;
5150 /* Build a binary-operation expression without default conversions.
5151 CODE is the kind of expression to build.
5152 LOCATION is the location_t of the operator in the source code.
5153 This function differs from `build' in several ways:
5154 the data type of the result is computed and recorded in it,
5155 warnings are generated if arg data types are invalid,
5156 special handling for addition and subtraction of pointers is known,
5157 and some optimization is done (operations on narrow ints
5158 are done in the narrower type when that gives the same result).
5159 Constant folding is also done before the result is returned.
5161 Note that the operands will never have enumeral types
5162 because either they have just had the default conversions performed
5163 or they have both just been converted to some other type in which
5164 the arithmetic is to be done.
5166 C++: must do special pointer arithmetic when implementing
5167 multiple inheritance, and deal with pointer to member functions. */
5169 tree
5170 cp_build_binary_op (const op_location_t &location,
5171 enum tree_code code, tree orig_op0, tree orig_op1,
5172 tsubst_flags_t complain)
5174 tree op0, op1;
5175 enum tree_code code0, code1;
5176 tree type0, type1, orig_type0, orig_type1;
5177 const char *invalid_op_diag;
5179 /* Expression code to give to the expression when it is built.
5180 Normally this is CODE, which is what the caller asked for,
5181 but in some special cases we change it. */
5182 enum tree_code resultcode = code;
5184 /* Data type in which the computation is to be performed.
5185 In the simplest cases this is the common type of the arguments. */
5186 tree result_type = NULL_TREE;
5188 /* When the computation is in excess precision, the type of the
5189 final EXCESS_PRECISION_EXPR. */
5190 tree semantic_result_type = NULL;
5192 /* Nonzero means operands have already been type-converted
5193 in whatever way is necessary.
5194 Zero means they need to be converted to RESULT_TYPE. */
5195 int converted = 0;
5197 /* Nonzero means create the expression with this type, rather than
5198 RESULT_TYPE. */
5199 tree build_type = 0;
5201 /* Nonzero means after finally constructing the expression
5202 convert it to this type. */
5203 tree final_type = 0;
5205 tree result;
5207 /* Nonzero if this is an operation like MIN or MAX which can
5208 safely be computed in short if both args are promoted shorts.
5209 Also implies COMMON.
5210 -1 indicates a bitwise operation; this makes a difference
5211 in the exact conditions for when it is safe to do the operation
5212 in a narrower mode. */
5213 int shorten = 0;
5215 /* Nonzero if this is a comparison operation;
5216 if both args are promoted shorts, compare the original shorts.
5217 Also implies COMMON. */
5218 int short_compare = 0;
5220 /* Nonzero if this is a right-shift operation, which can be computed on the
5221 original short and then promoted if the operand is a promoted short. */
5222 int short_shift = 0;
5224 /* Nonzero means set RESULT_TYPE to the common type of the args. */
5225 int common = 0;
5227 /* True if both operands have arithmetic type. */
5228 bool arithmetic_types_p;
5230 /* Remember whether we're doing / or %. */
5231 bool doing_div_or_mod = false;
5233 /* Remember whether we're doing << or >>. */
5234 bool doing_shift = false;
5236 /* Tree holding instrumentation expression. */
5237 tree instrument_expr = NULL_TREE;
5239 /* True means this is an arithmetic operation that may need excess
5240 precision. */
5241 bool may_need_excess_precision;
5243 /* Apply default conversions. */
5244 op0 = resolve_nondeduced_context (orig_op0, complain);
5245 op1 = resolve_nondeduced_context (orig_op1, complain);
5247 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
5248 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
5249 || code == TRUTH_XOR_EXPR)
5251 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5252 op0 = decay_conversion (op0, complain);
5253 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5254 op1 = decay_conversion (op1, complain);
5256 else
5258 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
5259 op0 = cp_default_conversion (op0, complain);
5260 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
5261 op1 = cp_default_conversion (op1, complain);
5264 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5265 STRIP_TYPE_NOPS (op0);
5266 STRIP_TYPE_NOPS (op1);
5268 /* DTRT if one side is an overloaded function, but complain about it. */
5269 if (type_unknown_p (op0))
5271 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
5272 if (t != error_mark_node)
5274 if (complain & tf_error)
5275 permerror (location,
5276 "assuming cast to type %qT from overloaded function",
5277 TREE_TYPE (t));
5278 op0 = t;
5281 if (type_unknown_p (op1))
5283 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
5284 if (t != error_mark_node)
5286 if (complain & tf_error)
5287 permerror (location,
5288 "assuming cast to type %qT from overloaded function",
5289 TREE_TYPE (t));
5290 op1 = t;
5294 orig_type0 = type0 = TREE_TYPE (op0);
5295 orig_type1 = type1 = TREE_TYPE (op1);
5296 tree non_ep_op0 = op0;
5297 tree non_ep_op1 = op1;
5299 /* The expression codes of the data types of the arguments tell us
5300 whether the arguments are integers, floating, pointers, etc. */
5301 code0 = TREE_CODE (type0);
5302 code1 = TREE_CODE (type1);
5304 /* If an error was already reported for one of the arguments,
5305 avoid reporting another error. */
5306 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
5307 return error_mark_node;
5309 if ((invalid_op_diag
5310 = targetm.invalid_binary_op (code, type0, type1)))
5312 if (complain & tf_error)
5314 if (code0 == REAL_TYPE
5315 && code1 == REAL_TYPE
5316 && (extended_float_type_p (type0)
5317 || extended_float_type_p (type1))
5318 && cp_compare_floating_point_conversion_ranks (type0,
5319 type1) == 3)
5321 rich_location richloc (line_table, location);
5322 binary_op_error (&richloc, code, type0, type1);
5324 else
5325 error (invalid_op_diag);
5327 return error_mark_node;
5330 switch (code)
5332 case PLUS_EXPR:
5333 case MINUS_EXPR:
5334 case MULT_EXPR:
5335 case TRUNC_DIV_EXPR:
5336 case CEIL_DIV_EXPR:
5337 case FLOOR_DIV_EXPR:
5338 case ROUND_DIV_EXPR:
5339 case EXACT_DIV_EXPR:
5340 may_need_excess_precision = true;
5341 break;
5342 case EQ_EXPR:
5343 case NE_EXPR:
5344 case LE_EXPR:
5345 case GE_EXPR:
5346 case LT_EXPR:
5347 case GT_EXPR:
5348 case SPACESHIP_EXPR:
5349 /* Excess precision for implicit conversions of integers to
5350 floating point. */
5351 may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
5352 || ANY_INTEGRAL_TYPE_P (type1));
5353 break;
5354 default:
5355 may_need_excess_precision = false;
5356 break;
5358 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
5360 op0 = TREE_OPERAND (op0, 0);
5361 type0 = TREE_TYPE (op0);
5363 else if (may_need_excess_precision
5364 && (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
5365 if (tree eptype = excess_precision_type (type0))
5367 type0 = eptype;
5368 op0 = convert (eptype, op0);
5370 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5372 op1 = TREE_OPERAND (op1, 0);
5373 type1 = TREE_TYPE (op1);
5375 else if (may_need_excess_precision
5376 && (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
5377 if (tree eptype = excess_precision_type (type1))
5379 type1 = eptype;
5380 op1 = convert (eptype, op1);
5383 /* Issue warnings about peculiar, but valid, uses of NULL. */
5384 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
5385 /* It's reasonable to use pointer values as operands of &&
5386 and ||, so NULL is no exception. */
5387 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
5388 && ( /* Both are NULL (or 0) and the operation was not a
5389 comparison or a pointer subtraction. */
5390 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
5391 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
5392 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5393 || (!null_ptr_cst_p (orig_op0)
5394 && !TYPE_PTR_OR_PTRMEM_P (type0))
5395 || (!null_ptr_cst_p (orig_op1)
5396 && !TYPE_PTR_OR_PTRMEM_P (type1)))
5397 && (complain & tf_warning))
5399 location_t loc =
5400 expansion_point_location_if_in_system_header (input_location);
5402 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
5405 /* In case when one of the operands of the binary operation is
5406 a vector and another is a scalar -- convert scalar to vector. */
5407 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
5408 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
5410 enum stv_conv convert_flag
5411 = scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
5412 complain & tf_error);
5414 switch (convert_flag)
5416 case stv_error:
5417 return error_mark_node;
5418 case stv_firstarg:
5420 op0 = convert (TREE_TYPE (type1), op0);
5421 op0 = save_expr (op0);
5422 op0 = build_vector_from_val (type1, op0);
5423 orig_type0 = type0 = TREE_TYPE (op0);
5424 code0 = TREE_CODE (type0);
5425 converted = 1;
5426 break;
5428 case stv_secondarg:
5430 op1 = convert (TREE_TYPE (type0), op1);
5431 op1 = save_expr (op1);
5432 op1 = build_vector_from_val (type0, op1);
5433 orig_type1 = type1 = TREE_TYPE (op1);
5434 code1 = TREE_CODE (type1);
5435 converted = 1;
5436 break;
5438 default:
5439 break;
5443 switch (code)
5445 case MINUS_EXPR:
5446 /* Subtraction of two similar pointers.
5447 We must subtract them as integers, then divide by object size. */
5448 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
5449 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5450 TREE_TYPE (type1)))
5452 result = pointer_diff (location, op0, op1,
5453 common_pointer_type (type0, type1), complain,
5454 &instrument_expr);
5455 if (instrument_expr != NULL)
5456 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5457 instrument_expr, result);
5459 return result;
5461 /* In all other cases except pointer - int, the usual arithmetic
5462 rules apply. */
5463 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
5465 common = 1;
5466 break;
5468 /* The pointer - int case is just like pointer + int; fall
5469 through. */
5470 gcc_fallthrough ();
5471 case PLUS_EXPR:
5472 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5473 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
5475 tree ptr_operand;
5476 tree int_operand;
5477 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
5478 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
5479 if (processing_template_decl)
5481 result_type = TREE_TYPE (ptr_operand);
5482 break;
5484 return cp_pointer_int_sum (location, code,
5485 ptr_operand,
5486 int_operand,
5487 complain);
5489 common = 1;
5490 break;
5492 case MULT_EXPR:
5493 common = 1;
5494 break;
5496 case TRUNC_DIV_EXPR:
5497 case CEIL_DIV_EXPR:
5498 case FLOOR_DIV_EXPR:
5499 case ROUND_DIV_EXPR:
5500 case EXACT_DIV_EXPR:
5501 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
5503 tree type0 = TREE_OPERAND (op0, 0);
5504 tree type1 = TREE_OPERAND (op1, 0);
5505 tree first_arg = tree_strip_any_location_wrapper (type0);
5506 if (!TYPE_P (type0))
5507 type0 = TREE_TYPE (type0);
5508 if (!TYPE_P (type1))
5509 type1 = TREE_TYPE (type1);
5510 if (type0
5511 && type1
5512 && INDIRECT_TYPE_P (type0)
5513 && same_type_p (TREE_TYPE (type0), type1))
5515 if (!(TREE_CODE (first_arg) == PARM_DECL
5516 && DECL_ARRAY_PARAMETER_P (first_arg)
5517 && warn_sizeof_array_argument)
5518 && (complain & tf_warning))
5520 auto_diagnostic_group d;
5521 if (warning_at (location, OPT_Wsizeof_pointer_div,
5522 "division %<sizeof (%T) / sizeof (%T)%> does "
5523 "not compute the number of array elements",
5524 type0, type1))
5525 if (DECL_P (first_arg))
5526 inform (DECL_SOURCE_LOCATION (first_arg),
5527 "first %<sizeof%> operand was declared here");
5530 else if (!dependent_type_p (type0)
5531 && !dependent_type_p (type1)
5532 && TREE_CODE (type0) == ARRAY_TYPE
5533 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
5534 /* Set by finish_parenthesized_expr. */
5535 && !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
5536 && (complain & tf_warning))
5537 maybe_warn_sizeof_array_div (location, first_arg, type0,
5538 op1, non_reference (type1));
5541 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5542 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5543 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5544 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
5546 enum tree_code tcode0 = code0, tcode1 = code1;
5547 doing_div_or_mod = true;
5548 warn_for_div_by_zero (location, fold_for_warn (op1));
5550 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
5551 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
5552 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
5553 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
5555 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
5556 resultcode = RDIV_EXPR;
5557 else
5559 /* When dividing two signed integers, we have to promote to int.
5560 unless we divide by a constant != -1. Note that default
5561 conversion will have been performed on the operands at this
5562 point, so we have to dig out the original type to find out if
5563 it was unsigned. */
5564 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5565 shorten = may_shorten_divmod (op0, stripped_op1);
5568 common = 1;
5570 break;
5572 case BIT_AND_EXPR:
5573 case BIT_IOR_EXPR:
5574 case BIT_XOR_EXPR:
5575 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5576 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5577 && !VECTOR_FLOAT_TYPE_P (type0)
5578 && !VECTOR_FLOAT_TYPE_P (type1)))
5579 shorten = -1;
5580 break;
5582 case TRUNC_MOD_EXPR:
5583 case FLOOR_MOD_EXPR:
5584 doing_div_or_mod = true;
5585 warn_for_div_by_zero (location, fold_for_warn (op1));
5587 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
5588 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5589 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
5590 common = 1;
5591 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5593 /* Although it would be tempting to shorten always here, that loses
5594 on some targets, since the modulo instruction is undefined if the
5595 quotient can't be represented in the computation mode. We shorten
5596 only if unsigned or if dividing by something we know != -1. */
5597 tree stripped_op1 = tree_strip_any_location_wrapper (op1);
5598 shorten = may_shorten_divmod (op0, stripped_op1);
5599 common = 1;
5601 break;
5603 case TRUTH_ANDIF_EXPR:
5604 case TRUTH_ORIF_EXPR:
5605 case TRUTH_AND_EXPR:
5606 case TRUTH_OR_EXPR:
5607 if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
5609 if (!COMPARISON_CLASS_P (op1))
5610 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5611 build_zero_cst (type1), complain);
5612 if (code == TRUTH_ANDIF_EXPR)
5614 tree z = build_zero_cst (TREE_TYPE (op1));
5615 return build_conditional_expr (location, op0, op1, z, complain);
5617 else if (code == TRUTH_ORIF_EXPR)
5619 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
5620 return build_conditional_expr (location, op0, m1, op1, complain);
5622 else
5623 gcc_unreachable ();
5625 if (gnu_vector_type_p (type0)
5626 && (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
5628 if (!COMPARISON_CLASS_P (op0))
5629 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
5630 build_zero_cst (type0), complain);
5631 if (!VECTOR_TYPE_P (type1))
5633 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
5634 tree z = build_zero_cst (TREE_TYPE (op0));
5635 op1 = build_conditional_expr (location, op1, m1, z, complain);
5637 else if (!COMPARISON_CLASS_P (op1))
5638 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
5639 build_zero_cst (type1), complain);
5641 if (code == TRUTH_ANDIF_EXPR)
5642 code = BIT_AND_EXPR;
5643 else if (code == TRUTH_ORIF_EXPR)
5644 code = BIT_IOR_EXPR;
5645 else
5646 gcc_unreachable ();
5648 return cp_build_binary_op (location, code, op0, op1, complain);
5651 result_type = boolean_type_node;
5652 break;
5654 /* Shift operations: result has same type as first operand;
5655 always convert second operand to int.
5656 Also set SHORT_SHIFT if shifting rightward. */
5658 case RSHIFT_EXPR:
5659 if (gnu_vector_type_p (type0)
5660 && code1 == INTEGER_TYPE
5661 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5663 result_type = type0;
5664 converted = 1;
5666 else if (gnu_vector_type_p (type0)
5667 && gnu_vector_type_p (type1)
5668 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5669 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5670 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5671 TYPE_VECTOR_SUBPARTS (type1)))
5673 result_type = type0;
5674 converted = 1;
5676 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5678 tree const_op1 = fold_for_warn (op1);
5679 if (TREE_CODE (const_op1) != INTEGER_CST)
5680 const_op1 = op1;
5681 result_type = type0;
5682 doing_shift = true;
5683 if (TREE_CODE (const_op1) == INTEGER_CST)
5685 if (tree_int_cst_lt (const_op1, integer_zero_node))
5687 if ((complain & tf_warning)
5688 && c_inhibit_evaluation_warnings == 0)
5689 warning_at (location, OPT_Wshift_count_negative,
5690 "right shift count is negative");
5692 else
5694 if (!integer_zerop (const_op1))
5695 short_shift = 1;
5697 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
5698 && (complain & tf_warning)
5699 && c_inhibit_evaluation_warnings == 0)
5700 warning_at (location, OPT_Wshift_count_overflow,
5701 "right shift count >= width of type");
5704 /* Avoid converting op1 to result_type later. */
5705 converted = 1;
5707 break;
5709 case LSHIFT_EXPR:
5710 if (gnu_vector_type_p (type0)
5711 && code1 == INTEGER_TYPE
5712 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
5714 result_type = type0;
5715 converted = 1;
5717 else if (gnu_vector_type_p (type0)
5718 && gnu_vector_type_p (type1)
5719 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
5720 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
5721 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
5722 TYPE_VECTOR_SUBPARTS (type1)))
5724 result_type = type0;
5725 converted = 1;
5727 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
5729 tree const_op0 = fold_for_warn (op0);
5730 if (TREE_CODE (const_op0) != INTEGER_CST)
5731 const_op0 = op0;
5732 tree const_op1 = fold_for_warn (op1);
5733 if (TREE_CODE (const_op1) != INTEGER_CST)
5734 const_op1 = op1;
5735 result_type = type0;
5736 doing_shift = true;
5737 if (TREE_CODE (const_op0) == INTEGER_CST
5738 && tree_int_cst_sgn (const_op0) < 0
5739 && !TYPE_OVERFLOW_WRAPS (type0)
5740 && (complain & tf_warning)
5741 && c_inhibit_evaluation_warnings == 0)
5742 warning_at (location, OPT_Wshift_negative_value,
5743 "left shift of negative value");
5744 if (TREE_CODE (const_op1) == INTEGER_CST)
5746 if (tree_int_cst_lt (const_op1, integer_zero_node))
5748 if ((complain & tf_warning)
5749 && c_inhibit_evaluation_warnings == 0)
5750 warning_at (location, OPT_Wshift_count_negative,
5751 "left shift count is negative");
5753 else if (compare_tree_int (const_op1,
5754 TYPE_PRECISION (type0)) >= 0)
5756 if ((complain & tf_warning)
5757 && c_inhibit_evaluation_warnings == 0)
5758 warning_at (location, OPT_Wshift_count_overflow,
5759 "left shift count >= width of type");
5761 else if (TREE_CODE (const_op0) == INTEGER_CST
5762 && (complain & tf_warning))
5763 maybe_warn_shift_overflow (location, const_op0, const_op1);
5765 /* Avoid converting op1 to result_type later. */
5766 converted = 1;
5768 break;
5770 case EQ_EXPR:
5771 case NE_EXPR:
5772 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
5773 goto vector_compare;
5774 if ((complain & tf_warning)
5775 && c_inhibit_evaluation_warnings == 0
5776 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
5777 warning_at (location, OPT_Wfloat_equal,
5778 "comparing floating-point with %<==%> "
5779 "or %<!=%> is unsafe");
5780 if (complain & tf_warning)
5782 tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
5783 tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
5784 if ((TREE_CODE (stripped_orig_op0) == STRING_CST
5785 && !integer_zerop (cp_fully_fold (op1)))
5786 || (TREE_CODE (stripped_orig_op1) == STRING_CST
5787 && !integer_zerop (cp_fully_fold (op0))))
5788 warning_at (location, OPT_Waddress,
5789 "comparison with string literal results in "
5790 "unspecified behavior");
5791 else if (warn_array_compare
5792 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
5793 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
5794 do_warn_array_compare (location, code, stripped_orig_op0,
5795 stripped_orig_op1);
5798 build_type = boolean_type_node;
5799 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5800 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
5801 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5802 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
5803 short_compare = 1;
5804 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
5805 && null_ptr_cst_p (orig_op1))
5806 /* Handle, eg, (void*)0 (c++/43906), and more. */
5807 || (code0 == POINTER_TYPE
5808 && TYPE_PTR_P (type1) && integer_zerop (op1)))
5810 if (TYPE_PTR_P (type1))
5811 result_type = composite_pointer_type (location,
5812 type0, type1, op0, op1,
5813 CPO_COMPARISON, complain);
5814 else
5815 result_type = type0;
5817 if (char_type_p (TREE_TYPE (orig_op1)))
5819 auto_diagnostic_group d;
5820 if (warning_at (location, OPT_Wpointer_compare,
5821 "comparison between pointer and zero character "
5822 "constant"))
5823 inform (location,
5824 "did you mean to dereference the pointer?");
5826 warn_for_null_address (location, op0, complain);
5828 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
5829 && null_ptr_cst_p (orig_op0))
5830 /* Handle, eg, (void*)0 (c++/43906), and more. */
5831 || (code1 == POINTER_TYPE
5832 && TYPE_PTR_P (type0) && integer_zerop (op0)))
5834 if (TYPE_PTR_P (type0))
5835 result_type = composite_pointer_type (location,
5836 type0, type1, op0, op1,
5837 CPO_COMPARISON, complain);
5838 else
5839 result_type = type1;
5841 if (char_type_p (TREE_TYPE (orig_op0)))
5843 auto_diagnostic_group d;
5844 if (warning_at (location, OPT_Wpointer_compare,
5845 "comparison between pointer and zero character "
5846 "constant"))
5847 inform (location,
5848 "did you mean to dereference the pointer?");
5850 warn_for_null_address (location, op1, complain);
5852 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5853 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
5854 result_type = composite_pointer_type (location,
5855 type0, type1, op0, op1,
5856 CPO_COMPARISON, complain);
5857 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5858 /* One of the operands must be of nullptr_t type. */
5859 result_type = TREE_TYPE (nullptr_node);
5860 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5862 result_type = type0;
5863 if (complain & tf_error)
5864 permerror (location, "ISO C++ forbids comparison between "
5865 "pointer and integer");
5866 else
5867 return error_mark_node;
5869 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5871 result_type = type1;
5872 if (complain & tf_error)
5873 permerror (location, "ISO C++ forbids comparison between "
5874 "pointer and integer");
5875 else
5876 return error_mark_node;
5878 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
5880 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5881 == ptrmemfunc_vbit_in_delta)
5883 tree pfn0, delta0, e1, e2;
5885 if (TREE_SIDE_EFFECTS (op0))
5886 op0 = cp_save_expr (op0);
5888 pfn0 = pfn_from_ptrmemfunc (op0);
5889 delta0 = delta_from_ptrmemfunc (op0);
5891 /* If we will warn below about a null-address compare
5892 involving the orig_op0 ptrmemfunc, we'd likely also
5893 warn about the pfn0's null-address compare, and
5894 that would be redundant, so suppress it. */
5895 warning_sentinel ws (warn_address);
5896 e1 = cp_build_binary_op (location,
5897 EQ_EXPR,
5898 pfn0,
5899 build_zero_cst (TREE_TYPE (pfn0)),
5900 complain);
5902 e2 = cp_build_binary_op (location,
5903 BIT_AND_EXPR,
5904 delta0,
5905 integer_one_node,
5906 complain);
5908 if (complain & tf_warning)
5909 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
5911 e2 = cp_build_binary_op (location,
5912 EQ_EXPR, e2, integer_zero_node,
5913 complain);
5914 op0 = cp_build_binary_op (location,
5915 TRUTH_ANDIF_EXPR, e1, e2,
5916 complain);
5917 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
5919 else
5921 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
5922 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
5924 result_type = TREE_TYPE (op0);
5926 warn_for_null_address (location, orig_op0, complain);
5928 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
5929 return cp_build_binary_op (location, code, op1, op0, complain);
5930 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
5932 tree type;
5933 /* E will be the final comparison. */
5934 tree e;
5935 /* E1 and E2 are for scratch. */
5936 tree e1;
5937 tree e2;
5938 tree pfn0;
5939 tree pfn1;
5940 tree delta0;
5941 tree delta1;
5943 type = composite_pointer_type (location, type0, type1, op0, op1,
5944 CPO_COMPARISON, complain);
5946 if (!same_type_p (TREE_TYPE (op0), type))
5947 op0 = cp_convert_and_check (type, op0, complain);
5948 if (!same_type_p (TREE_TYPE (op1), type))
5949 op1 = cp_convert_and_check (type, op1, complain);
5951 if (op0 == error_mark_node || op1 == error_mark_node)
5952 return error_mark_node;
5954 if (TREE_SIDE_EFFECTS (op0))
5955 op0 = save_expr (op0);
5956 if (TREE_SIDE_EFFECTS (op1))
5957 op1 = save_expr (op1);
5959 pfn0 = pfn_from_ptrmemfunc (op0);
5960 pfn0 = cp_fully_fold (pfn0);
5961 /* Avoid -Waddress warnings (c++/64877). */
5962 if (TREE_CODE (pfn0) == ADDR_EXPR)
5963 suppress_warning (pfn0, OPT_Waddress);
5964 pfn1 = pfn_from_ptrmemfunc (op1);
5965 pfn1 = cp_fully_fold (pfn1);
5966 delta0 = delta_from_ptrmemfunc (op0);
5967 delta1 = delta_from_ptrmemfunc (op1);
5968 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5969 == ptrmemfunc_vbit_in_delta)
5971 /* We generate:
5973 (op0.pfn == op1.pfn
5974 && ((op0.delta == op1.delta)
5975 || (!op0.pfn && op0.delta & 1 == 0
5976 && op1.delta & 1 == 0))
5978 The reason for the `!op0.pfn' bit is that a NULL
5979 pointer-to-member is any member with a zero PFN and
5980 LSB of the DELTA field is 0. */
5982 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
5983 delta0,
5984 integer_one_node,
5985 complain);
5986 e1 = cp_build_binary_op (location,
5987 EQ_EXPR, e1, integer_zero_node,
5988 complain);
5989 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
5990 delta1,
5991 integer_one_node,
5992 complain);
5993 e2 = cp_build_binary_op (location,
5994 EQ_EXPR, e2, integer_zero_node,
5995 complain);
5996 e1 = cp_build_binary_op (location,
5997 TRUTH_ANDIF_EXPR, e2, e1,
5998 complain);
5999 e2 = cp_build_binary_op (location, EQ_EXPR,
6000 pfn0,
6001 build_zero_cst (TREE_TYPE (pfn0)),
6002 complain);
6003 e2 = cp_build_binary_op (location,
6004 TRUTH_ANDIF_EXPR, e2, e1, complain);
6005 e1 = cp_build_binary_op (location,
6006 EQ_EXPR, delta0, delta1, complain);
6007 e1 = cp_build_binary_op (location,
6008 TRUTH_ORIF_EXPR, e1, e2, complain);
6010 else
6012 /* We generate:
6014 (op0.pfn == op1.pfn
6015 && (!op0.pfn || op0.delta == op1.delta))
6017 The reason for the `!op0.pfn' bit is that a NULL
6018 pointer-to-member is any member with a zero PFN; the
6019 DELTA field is unspecified. */
6021 e1 = cp_build_binary_op (location,
6022 EQ_EXPR, delta0, delta1, complain);
6023 e2 = cp_build_binary_op (location,
6024 EQ_EXPR,
6025 pfn0,
6026 build_zero_cst (TREE_TYPE (pfn0)),
6027 complain);
6028 e1 = cp_build_binary_op (location,
6029 TRUTH_ORIF_EXPR, e1, e2, complain);
6031 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
6032 e = cp_build_binary_op (location,
6033 TRUTH_ANDIF_EXPR, e2, e1, complain);
6034 if (code == EQ_EXPR)
6035 return e;
6036 return cp_build_binary_op (location,
6037 EQ_EXPR, e, integer_zero_node, complain);
6039 else
6041 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
6042 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
6043 type1));
6044 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
6045 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
6046 type0));
6049 break;
6051 case MAX_EXPR:
6052 case MIN_EXPR:
6053 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6054 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6055 shorten = 1;
6056 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6057 result_type = composite_pointer_type (location,
6058 type0, type1, op0, op1,
6059 CPO_COMPARISON, complain);
6060 break;
6062 case LE_EXPR:
6063 case GE_EXPR:
6064 case LT_EXPR:
6065 case GT_EXPR:
6066 case SPACESHIP_EXPR:
6067 if (TREE_CODE (orig_op0) == STRING_CST
6068 || TREE_CODE (orig_op1) == STRING_CST)
6070 if (complain & tf_warning)
6071 warning_at (location, OPT_Waddress,
6072 "comparison with string literal results "
6073 "in unspecified behavior");
6075 else if (warn_array_compare
6076 && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
6077 && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
6078 && code != SPACESHIP_EXPR
6079 && (complain & tf_warning))
6080 do_warn_array_compare (location, code,
6081 tree_strip_any_location_wrapper (orig_op0),
6082 tree_strip_any_location_wrapper (orig_op1));
6084 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6086 vector_compare:
6087 tree intt;
6088 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
6089 TREE_TYPE (type1))
6090 && !vector_types_compatible_elements_p (type0, type1))
6092 if (complain & tf_error)
6094 error_at (location, "comparing vectors with different "
6095 "element types");
6096 inform (location, "operand types are %qT and %qT",
6097 type0, type1);
6099 return error_mark_node;
6102 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
6103 TYPE_VECTOR_SUBPARTS (type1)))
6105 if (complain & tf_error)
6107 error_at (location, "comparing vectors with different "
6108 "number of elements");
6109 inform (location, "operand types are %qT and %qT",
6110 type0, type1);
6112 return error_mark_node;
6115 /* It's not precisely specified how the usual arithmetic
6116 conversions apply to the vector types. Here, we use
6117 the unsigned type if one of the operands is signed and
6118 the other one is unsigned. */
6119 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
6121 if (!TYPE_UNSIGNED (type0))
6122 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
6123 else
6124 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
6125 warning_at (location, OPT_Wsign_compare, "comparison between "
6126 "types %qT and %qT", type0, type1);
6129 if (resultcode == SPACESHIP_EXPR)
6131 if (complain & tf_error)
6132 sorry_at (location, "three-way comparison of vectors");
6133 return error_mark_node;
6136 /* Always construct signed integer vector type. */
6137 intt = c_common_type_for_size
6138 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
6139 if (!intt)
6141 if (complain & tf_error)
6142 error_at (location, "could not find an integer type "
6143 "of the same size as %qT", TREE_TYPE (type0));
6144 return error_mark_node;
6146 result_type = build_opaque_vector_type (intt,
6147 TYPE_VECTOR_SUBPARTS (type0));
6148 return build_vec_cmp (resultcode, result_type, op0, op1);
6150 build_type = boolean_type_node;
6151 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6152 || code0 == ENUMERAL_TYPE)
6153 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6154 || code1 == ENUMERAL_TYPE))
6155 short_compare = 1;
6156 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6157 result_type = composite_pointer_type (location,
6158 type0, type1, op0, op1,
6159 CPO_COMPARISON, complain);
6160 else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6161 || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6162 || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
6164 /* Core Issue 1512 made this ill-formed. */
6165 if (complain & tf_error)
6166 error_at (location, "ordered comparison of pointer with "
6167 "integer zero (%qT and %qT)", type0, type1);
6168 return error_mark_node;
6170 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6172 result_type = type0;
6173 if (complain & tf_error)
6174 permerror (location, "ISO C++ forbids comparison between "
6175 "pointer and integer");
6176 else
6177 return error_mark_node;
6179 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6181 result_type = type1;
6182 if (complain & tf_error)
6183 permerror (location, "ISO C++ forbids comparison between "
6184 "pointer and integer");
6185 else
6186 return error_mark_node;
6189 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
6190 && !processing_template_decl
6191 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
6193 op0 = save_expr (op0);
6194 op1 = save_expr (op1);
6196 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
6197 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
6200 break;
6202 case UNORDERED_EXPR:
6203 case ORDERED_EXPR:
6204 case UNLT_EXPR:
6205 case UNLE_EXPR:
6206 case UNGT_EXPR:
6207 case UNGE_EXPR:
6208 case UNEQ_EXPR:
6209 build_type = integer_type_node;
6210 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6212 if (complain & tf_error)
6213 error ("unordered comparison on non-floating-point argument");
6214 return error_mark_node;
6216 common = 1;
6217 break;
6219 default:
6220 break;
6223 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6224 || code0 == ENUMERAL_TYPE)
6225 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6226 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
6227 arithmetic_types_p = 1;
6228 else
6230 arithmetic_types_p = 0;
6231 /* Vector arithmetic is only allowed when both sides are vectors. */
6232 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
6234 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
6235 || !vector_types_compatible_elements_p (type0, type1))
6237 if (complain & tf_error)
6239 /* "location" already embeds the locations of the
6240 operands, so we don't need to add them separately
6241 to richloc. */
6242 rich_location richloc (line_table, location);
6243 binary_op_error (&richloc, code, type0, type1);
6245 return error_mark_node;
6247 arithmetic_types_p = 1;
6250 /* Determine the RESULT_TYPE, if it is not already known. */
6251 if (!result_type
6252 && arithmetic_types_p
6253 && (shorten || common || short_compare))
6255 result_type = cp_common_type (type0, type1);
6256 if (result_type == error_mark_node)
6258 tree t1 = type0;
6259 tree t2 = type1;
6260 if (TREE_CODE (t1) == COMPLEX_TYPE)
6261 t1 = TREE_TYPE (t1);
6262 if (TREE_CODE (t2) == COMPLEX_TYPE)
6263 t2 = TREE_TYPE (t2);
6264 gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6265 && TREE_CODE (t2) == REAL_TYPE
6266 && (extended_float_type_p (t1)
6267 || extended_float_type_p (t2))
6268 && cp_compare_floating_point_conversion_ranks
6269 (t1, t2) == 3);
6270 if (complain & tf_error)
6272 rich_location richloc (line_table, location);
6273 binary_op_error (&richloc, code, type0, type1);
6275 return error_mark_node;
6277 if (complain & tf_warning)
6278 do_warn_double_promotion (result_type, type0, type1,
6279 "implicit conversion from %qH to %qI "
6280 "to match other operand of binary "
6281 "expression", location);
6282 if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
6283 TREE_TYPE (orig_op1), complain))
6284 return error_mark_node;
6286 if (may_need_excess_precision
6287 && (orig_type0 != type0 || orig_type1 != type1)
6288 && build_type == NULL_TREE
6289 && result_type)
6291 gcc_assert (common);
6292 semantic_result_type = cp_common_type (orig_type0, orig_type1);
6293 if (semantic_result_type == error_mark_node)
6295 tree t1 = orig_type0;
6296 tree t2 = orig_type1;
6297 if (TREE_CODE (t1) == COMPLEX_TYPE)
6298 t1 = TREE_TYPE (t1);
6299 if (TREE_CODE (t2) == COMPLEX_TYPE)
6300 t2 = TREE_TYPE (t2);
6301 gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
6302 && TREE_CODE (t2) == REAL_TYPE
6303 && (extended_float_type_p (t1)
6304 || extended_float_type_p (t2))
6305 && cp_compare_floating_point_conversion_ranks
6306 (t1, t2) == 3);
6307 if (complain & tf_error)
6309 rich_location richloc (line_table, location);
6310 binary_op_error (&richloc, code, type0, type1);
6312 return error_mark_node;
6316 if (code == SPACESHIP_EXPR)
6318 iloc_sentinel s (location);
6320 tree orig_type0 = TREE_TYPE (orig_op0);
6321 tree_code orig_code0 = TREE_CODE (orig_type0);
6322 tree orig_type1 = TREE_TYPE (orig_op1);
6323 tree_code orig_code1 = TREE_CODE (orig_type1);
6324 if (!result_type || result_type == error_mark_node)
6325 /* Nope. */
6326 result_type = NULL_TREE;
6327 else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
6328 /* "If one of the operands is of type bool and the other is not, the
6329 program is ill-formed." */
6330 result_type = NULL_TREE;
6331 else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
6332 && code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
6333 /* We only do array/function-to-pointer conversion if "at least one of
6334 the operands is of pointer type". */
6335 result_type = NULL_TREE;
6336 else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
6337 /* <=> no longer supports equality relations. */
6338 result_type = NULL_TREE;
6339 else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
6340 && !(same_type_ignoring_top_level_qualifiers_p
6341 (orig_type0, orig_type1)))
6342 /* "If both operands have arithmetic types, or one operand has integral
6343 type and the other operand has unscoped enumeration type, the usual
6344 arithmetic conversions are applied to the operands." So we don't do
6345 arithmetic conversions if the operands both have enumeral type. */
6346 result_type = NULL_TREE;
6347 else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
6348 || (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
6349 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6350 [where one is of enumeration type and the other is of a different
6351 enumeration type or a floating-point type] are ill-formed. */
6352 result_type = NULL_TREE;
6354 if (result_type)
6356 build_type = spaceship_type (result_type, complain);
6357 if (build_type == error_mark_node)
6358 return error_mark_node;
6361 if (result_type && arithmetic_types_p)
6363 /* If a narrowing conversion is required, other than from an integral
6364 type to a floating point type, the program is ill-formed. */
6365 bool ok = true;
6366 if (TREE_CODE (result_type) == REAL_TYPE
6367 && CP_INTEGRAL_TYPE_P (orig_type0))
6368 /* OK */;
6369 else if (!check_narrowing (result_type, orig_op0, complain))
6370 ok = false;
6371 if (TREE_CODE (result_type) == REAL_TYPE
6372 && CP_INTEGRAL_TYPE_P (orig_type1))
6373 /* OK */;
6374 else if (!check_narrowing (result_type, orig_op1, complain))
6375 ok = false;
6376 if (!ok && !(complain & tf_error))
6377 return error_mark_node;
6381 if (!result_type)
6383 if (complain & tf_error)
6385 binary_op_rich_location richloc (location,
6386 orig_op0, orig_op1, true);
6387 error_at (&richloc,
6388 "invalid operands of types %qT and %qT to binary %qO",
6389 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
6391 return error_mark_node;
6394 /* If we're in a template, the only thing we need to know is the
6395 RESULT_TYPE. */
6396 if (processing_template_decl)
6398 /* Since the middle-end checks the type when doing a build2, we
6399 need to build the tree in pieces. This built tree will never
6400 get out of the front-end as we replace it when instantiating
6401 the template. */
6402 tree tmp = build2 (resultcode,
6403 build_type ? build_type : result_type,
6404 NULL_TREE, op1);
6405 TREE_OPERAND (tmp, 0) = op0;
6406 if (semantic_result_type)
6407 tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
6408 return tmp;
6411 /* Remember the original type; RESULT_TYPE might be changed later on
6412 by shorten_binary_op. */
6413 tree orig_type = result_type;
6415 if (arithmetic_types_p)
6417 bool first_complex = (code0 == COMPLEX_TYPE);
6418 bool second_complex = (code1 == COMPLEX_TYPE);
6419 int none_complex = (!first_complex && !second_complex);
6421 /* Adapted from patch for c/24581. */
6422 if (first_complex != second_complex
6423 && (code == PLUS_EXPR
6424 || code == MINUS_EXPR
6425 || code == MULT_EXPR
6426 || (code == TRUNC_DIV_EXPR && first_complex))
6427 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
6428 && flag_signed_zeros)
6430 /* An operation on mixed real/complex operands must be
6431 handled specially, but the language-independent code can
6432 more easily optimize the plain complex arithmetic if
6433 -fno-signed-zeros. */
6434 tree real_type = TREE_TYPE (result_type);
6435 tree real, imag;
6436 if (first_complex)
6438 if (TREE_TYPE (op0) != result_type)
6439 op0 = cp_convert_and_check (result_type, op0, complain);
6440 if (TREE_TYPE (op1) != real_type)
6441 op1 = cp_convert_and_check (real_type, op1, complain);
6443 else
6445 if (TREE_TYPE (op0) != real_type)
6446 op0 = cp_convert_and_check (real_type, op0, complain);
6447 if (TREE_TYPE (op1) != result_type)
6448 op1 = cp_convert_and_check (result_type, op1, complain);
6450 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
6451 return error_mark_node;
6452 if (first_complex)
6454 op0 = save_expr (op0);
6455 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
6456 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
6457 switch (code)
6459 case MULT_EXPR:
6460 case TRUNC_DIV_EXPR:
6461 op1 = save_expr (op1);
6462 imag = build2 (resultcode, real_type, imag, op1);
6463 /* Fall through. */
6464 case PLUS_EXPR:
6465 case MINUS_EXPR:
6466 real = build2 (resultcode, real_type, real, op1);
6467 break;
6468 default:
6469 gcc_unreachable();
6472 else
6474 op1 = save_expr (op1);
6475 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
6476 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
6477 switch (code)
6479 case MULT_EXPR:
6480 op0 = save_expr (op0);
6481 imag = build2 (resultcode, real_type, op0, imag);
6482 /* Fall through. */
6483 case PLUS_EXPR:
6484 real = build2 (resultcode, real_type, op0, real);
6485 break;
6486 case MINUS_EXPR:
6487 real = build2 (resultcode, real_type, op0, real);
6488 imag = build1 (NEGATE_EXPR, real_type, imag);
6489 break;
6490 default:
6491 gcc_unreachable();
6494 result = build2 (COMPLEX_EXPR, result_type, real, imag);
6495 if (semantic_result_type)
6496 result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6497 result);
6498 return result;
6501 /* For certain operations (which identify themselves by shorten != 0)
6502 if both args were extended from the same smaller type,
6503 do the arithmetic in that type and then extend.
6505 shorten !=0 and !=1 indicates a bitwise operation.
6506 For them, this optimization is safe only if
6507 both args are zero-extended or both are sign-extended.
6508 Otherwise, we might change the result.
6509 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6510 but calculated in (unsigned short) it would be (unsigned short)-1. */
6512 if (shorten && none_complex)
6514 final_type = result_type;
6515 result_type = shorten_binary_op (result_type, op0, op1,
6516 shorten == -1);
6519 /* Shifts can be shortened if shifting right. */
6521 if (short_shift)
6523 int unsigned_arg;
6524 tree arg0 = get_narrower (op0, &unsigned_arg);
6525 /* We're not really warning here but when we set short_shift we
6526 used fold_for_warn to fold the operand. */
6527 tree const_op1 = fold_for_warn (op1);
6529 final_type = result_type;
6531 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6532 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
6534 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6535 && tree_int_cst_sgn (const_op1) > 0
6536 /* We can shorten only if the shift count is less than the
6537 number of bits in the smaller type size. */
6538 && compare_tree_int (const_op1,
6539 TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6540 /* We cannot drop an unsigned shift after sign-extension. */
6541 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
6543 /* Do an unsigned shift if the operand was zero-extended. */
6544 result_type
6545 = c_common_signed_or_unsigned_type (unsigned_arg,
6546 TREE_TYPE (arg0));
6547 /* Convert value-to-be-shifted to that type. */
6548 if (TREE_TYPE (op0) != result_type)
6549 op0 = convert (result_type, op0);
6550 converted = 1;
6554 /* Comparison operations are shortened too but differently.
6555 They identify themselves by setting short_compare = 1. */
6557 if (short_compare)
6559 /* We call shorten_compare only for diagnostics. */
6560 tree xop0 = fold_simple (op0);
6561 tree xop1 = fold_simple (op1);
6562 tree xresult_type = result_type;
6563 enum tree_code xresultcode = resultcode;
6564 shorten_compare (location, &xop0, &xop1, &xresult_type,
6565 &xresultcode);
6568 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
6569 && warn_sign_compare
6570 /* Do not warn until the template is instantiated; we cannot
6571 bound the ranges of the arguments until that point. */
6572 && !processing_template_decl
6573 && (complain & tf_warning)
6574 && c_inhibit_evaluation_warnings == 0
6575 /* Even unsigned enum types promote to signed int. We don't
6576 want to issue -Wsign-compare warnings for this case. */
6577 && !enum_cast_to_int (orig_op0)
6578 && !enum_cast_to_int (orig_op1))
6580 warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
6581 result_type, resultcode);
6585 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6586 Then the expression will be built.
6587 It will be given type FINAL_TYPE if that is nonzero;
6588 otherwise, it will be given type RESULT_TYPE. */
6589 if (! converted)
6591 warning_sentinel w (warn_sign_conversion, short_compare);
6592 if (!same_type_p (TREE_TYPE (op0), result_type))
6593 op0 = cp_convert_and_check (result_type, op0, complain);
6594 if (!same_type_p (TREE_TYPE (op1), result_type))
6595 op1 = cp_convert_and_check (result_type, op1, complain);
6597 if (op0 == error_mark_node || op1 == error_mark_node)
6598 return error_mark_node;
6601 if (build_type == NULL_TREE)
6602 build_type = result_type;
6604 if (doing_shift
6605 && flag_strong_eval_order == 2
6606 && TREE_SIDE_EFFECTS (op1)
6607 && !processing_template_decl)
6609 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6610 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6611 op0 = cp_save_expr (op0);
6612 instrument_expr = op0;
6615 if (sanitize_flags_p ((SANITIZE_SHIFT
6616 | SANITIZE_DIVIDE
6617 | SANITIZE_FLOAT_DIVIDE
6618 | SANITIZE_SI_OVERFLOW))
6619 && current_function_decl != NULL_TREE
6620 && !processing_template_decl
6621 && (doing_div_or_mod || doing_shift))
6623 /* OP0 and/or OP1 might have side-effects. */
6624 op0 = cp_save_expr (op0);
6625 op1 = cp_save_expr (op1);
6626 op0 = fold_non_dependent_expr (op0, complain);
6627 op1 = fold_non_dependent_expr (op1, complain);
6628 tree instrument_expr1 = NULL_TREE;
6629 if (doing_div_or_mod
6630 && sanitize_flags_p (SANITIZE_DIVIDE
6631 | SANITIZE_FLOAT_DIVIDE
6632 | SANITIZE_SI_OVERFLOW))
6634 /* For diagnostics we want to use the promoted types without
6635 shorten_binary_op. So convert the arguments to the
6636 original result_type. */
6637 tree cop0 = op0;
6638 tree cop1 = op1;
6639 if (TREE_TYPE (cop0) != orig_type)
6640 cop0 = cp_convert (orig_type, op0, complain);
6641 if (TREE_TYPE (cop1) != orig_type)
6642 cop1 = cp_convert (orig_type, op1, complain);
6643 instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
6645 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
6646 instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
6647 if (instrument_expr != NULL)
6648 instrument_expr = add_stmt_to_compound (instrument_expr,
6649 instrument_expr1);
6650 else
6651 instrument_expr = instrument_expr1;
6654 result = build2_loc (location, resultcode, build_type, op0, op1);
6655 if (final_type != 0)
6656 result = cp_convert (final_type, result, complain);
6658 if (instrument_expr != NULL)
6659 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
6660 instrument_expr, result);
6662 if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
6663 result = get_target_expr (result, complain);
6665 if (semantic_result_type)
6666 result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
6668 if (!c_inhibit_evaluation_warnings)
6670 if (!processing_template_decl)
6672 op0 = cp_fully_fold (op0);
6673 /* Only consider the second argument if the first isn't overflowed. */
6674 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
6675 return result;
6676 op1 = cp_fully_fold (op1);
6677 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
6678 return result;
6680 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
6681 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
6682 return result;
6684 tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
6685 if (TREE_OVERFLOW_P (result_ovl))
6686 overflow_warning (location, result_ovl);
6689 return result;
6692 /* Build a VEC_PERM_EXPR.
6693 This is a simple wrapper for c_build_vec_perm_expr. */
6694 tree
6695 build_x_vec_perm_expr (location_t loc,
6696 tree arg0, tree arg1, tree arg2,
6697 tsubst_flags_t complain)
6699 tree orig_arg0 = arg0;
6700 tree orig_arg1 = arg1;
6701 tree orig_arg2 = arg2;
6702 if (processing_template_decl)
6704 if (type_dependent_expression_p (arg0)
6705 || type_dependent_expression_p (arg1)
6706 || type_dependent_expression_p (arg2))
6707 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
6709 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
6710 if (processing_template_decl && exp != error_mark_node)
6711 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
6712 orig_arg1, orig_arg2);
6713 return exp;
6716 /* Build a VEC_PERM_EXPR.
6717 This is a simple wrapper for c_build_shufflevector. */
6718 tree
6719 build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
6720 tsubst_flags_t complain)
6722 tree arg0 = (*args)[0];
6723 tree arg1 = (*args)[1];
6724 if (processing_template_decl)
6726 for (unsigned i = 0; i < args->length (); ++i)
6727 if (i <= 1
6728 ? type_dependent_expression_p ((*args)[i])
6729 : instantiation_dependent_expression_p ((*args)[i]))
6731 tree exp = build_min_nt_call_vec (NULL, args);
6732 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6733 return exp;
6736 auto_vec<tree, 16> mask;
6737 for (unsigned i = 2; i < args->length (); ++i)
6739 tree idx = fold_non_dependent_expr ((*args)[i], complain);
6740 mask.safe_push (idx);
6742 tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
6743 if (processing_template_decl && exp != error_mark_node)
6745 exp = build_min_non_dep_call_vec (exp, NULL, args);
6746 CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
6748 return exp;
6751 /* Return a tree for the sum or difference (RESULTCODE says which)
6752 of pointer PTROP and integer INTOP. */
6754 static tree
6755 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
6756 tree intop, tsubst_flags_t complain)
6758 tree res_type = TREE_TYPE (ptrop);
6760 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6761 in certain circumstance (when it's valid to do so). So we need
6762 to make sure it's complete. We don't need to check here, if we
6763 can actually complete it at all, as those checks will be done in
6764 pointer_int_sum() anyway. */
6765 complete_type (TREE_TYPE (res_type));
6767 return pointer_int_sum (loc, resultcode, ptrop,
6768 intop, complain & tf_warning_or_error);
6771 /* Return a tree for the difference of pointers OP0 and OP1.
6772 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6773 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6775 static tree
6776 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
6777 tsubst_flags_t complain, tree *instrument_expr)
6779 tree result, inttype;
6780 tree restype = ptrdiff_type_node;
6781 tree target_type = TREE_TYPE (ptrtype);
6783 if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
6784 return error_mark_node;
6786 if (VOID_TYPE_P (target_type))
6788 if (complain & tf_error)
6789 permerror (loc, "ISO C++ forbids using pointer of "
6790 "type %<void *%> in subtraction");
6791 else
6792 return error_mark_node;
6794 if (TREE_CODE (target_type) == FUNCTION_TYPE)
6796 if (complain & tf_error)
6797 permerror (loc, "ISO C++ forbids using pointer to "
6798 "a function in subtraction");
6799 else
6800 return error_mark_node;
6802 if (TREE_CODE (target_type) == METHOD_TYPE)
6804 if (complain & tf_error)
6805 permerror (loc, "ISO C++ forbids using pointer to "
6806 "a method in subtraction");
6807 else
6808 return error_mark_node;
6810 else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
6811 TREE_TYPE (TREE_TYPE (op0)),
6812 !(complain & tf_error))
6813 || !verify_type_context (loc, TCTX_POINTER_ARITH,
6814 TREE_TYPE (TREE_TYPE (op1)),
6815 !(complain & tf_error)))
6816 return error_mark_node;
6818 /* Determine integer type result of the subtraction. This will usually
6819 be the same as the result type (ptrdiff_t), but may need to be a wider
6820 type if pointers for the address space are wider than ptrdiff_t. */
6821 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
6822 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6823 else
6824 inttype = restype;
6826 if (!processing_template_decl
6827 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
6829 op0 = save_expr (op0);
6830 op1 = save_expr (op1);
6832 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
6833 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
6836 /* First do the subtraction, then build the divide operator
6837 and only convert at the very end.
6838 Do not do default conversions in case restype is a short type. */
6840 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6841 pointers. If some platform cannot provide that, or has a larger
6842 ptrdiff_type to support differences larger than half the address
6843 space, cast the pointers to some larger integer type and do the
6844 computations in that type. */
6845 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
6846 op0 = cp_build_binary_op (loc,
6847 MINUS_EXPR,
6848 cp_convert (inttype, op0, complain),
6849 cp_convert (inttype, op1, complain),
6850 complain);
6851 else
6852 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
6854 /* This generates an error if op1 is a pointer to an incomplete type. */
6855 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
6857 if (complain & tf_error)
6858 error_at (loc, "invalid use of a pointer to an incomplete type in "
6859 "pointer arithmetic");
6860 else
6861 return error_mark_node;
6864 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
6866 if (complain & tf_error)
6867 error_at (loc, "arithmetic on pointer to an empty aggregate");
6868 else
6869 return error_mark_node;
6872 op1 = (TYPE_PTROB_P (ptrtype)
6873 ? size_in_bytes_loc (loc, target_type)
6874 : integer_one_node);
6876 /* Do the division. */
6878 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
6879 cp_convert (inttype, op1, complain));
6880 return cp_convert (restype, result, complain);
6883 /* Construct and perhaps optimize a tree representation
6884 for a unary operation. CODE, a tree_code, specifies the operation
6885 and XARG is the operand. */
6887 tree
6888 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
6889 tree lookups, tsubst_flags_t complain)
6891 tree orig_expr = xarg;
6892 tree exp;
6893 int ptrmem = 0;
6894 tree overload = NULL_TREE;
6896 if (processing_template_decl)
6898 if (type_dependent_expression_p (xarg))
6900 tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
6901 TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
6902 return e;
6906 exp = NULL_TREE;
6908 /* [expr.unary.op] says:
6910 The address of an object of incomplete type can be taken.
6912 (And is just the ordinary address operator, not an overloaded
6913 "operator &".) However, if the type is a template
6914 specialization, we must complete the type at this point so that
6915 an overloaded "operator &" will be available if required. */
6916 if (code == ADDR_EXPR
6917 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
6918 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
6919 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
6920 || (TREE_CODE (xarg) == OFFSET_REF)))
6921 /* Don't look for a function. */;
6922 else
6923 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
6924 NULL_TREE, lookups, &overload, complain);
6926 if (!exp && code == ADDR_EXPR)
6928 if (is_overloaded_fn (xarg))
6930 tree fn = get_first_fn (xarg);
6931 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
6933 if (complain & tf_error)
6934 error_at (loc, DECL_CONSTRUCTOR_P (fn)
6935 ? G_("taking address of constructor %qD")
6936 : G_("taking address of destructor %qD"),
6937 fn);
6938 return error_mark_node;
6942 /* A pointer to member-function can be formed only by saying
6943 &X::mf. */
6944 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
6945 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
6947 if (TREE_CODE (xarg) != OFFSET_REF
6948 || !TYPE_P (TREE_OPERAND (xarg, 0)))
6950 if (complain & tf_error)
6952 error_at (loc, "invalid use of %qE to form a "
6953 "pointer-to-member-function", xarg.get_value ());
6954 if (TREE_CODE (xarg) != OFFSET_REF)
6955 inform (loc, " a qualified-id is required");
6957 return error_mark_node;
6959 else
6961 if (complain & tf_error)
6962 error_at (loc, "parentheses around %qE cannot be used to "
6963 "form a pointer-to-member-function",
6964 xarg.get_value ());
6965 else
6966 return error_mark_node;
6967 PTRMEM_OK_P (xarg) = 1;
6971 if (TREE_CODE (xarg) == OFFSET_REF)
6973 ptrmem = PTRMEM_OK_P (xarg);
6975 if (!ptrmem && !flag_ms_extensions
6976 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
6978 /* A single non-static member, make sure we don't allow a
6979 pointer-to-member. */
6980 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
6981 TREE_OPERAND (xarg, 0),
6982 ovl_make (TREE_OPERAND (xarg, 1)));
6983 PTRMEM_OK_P (xarg) = ptrmem;
6987 exp = cp_build_addr_expr_strict (xarg, complain);
6989 if (TREE_CODE (exp) == PTRMEM_CST)
6990 PTRMEM_CST_LOCATION (exp) = loc;
6991 else
6992 protected_set_expr_location (exp, loc);
6995 if (processing_template_decl && exp != error_mark_node)
6997 if (overload != NULL_TREE)
6998 return (build_min_non_dep_op_overload
6999 (code, exp, overload, orig_expr, integer_zero_node));
7001 exp = build_min_non_dep (code, exp, orig_expr,
7002 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
7004 if (TREE_CODE (exp) == ADDR_EXPR)
7005 PTRMEM_OK_P (exp) = ptrmem;
7006 return exp;
7009 /* Construct and perhaps optimize a tree representation
7010 for __builtin_addressof operation. ARG specifies the operand. */
7012 tree
7013 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
7015 tree orig_expr = arg;
7017 if (processing_template_decl)
7019 if (type_dependent_expression_p (arg))
7020 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
7023 tree exp = cp_build_addr_expr_strict (arg, complain);
7025 if (processing_template_decl && exp != error_mark_node)
7026 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
7027 return exp;
7030 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7031 constants, where a null value is represented by an INTEGER_CST of
7032 -1. */
7034 tree
7035 cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
7037 tree type = TREE_TYPE (expr);
7038 location_t loc = cp_expr_loc_or_input_loc (expr);
7039 if (TYPE_PTR_OR_PTRMEM_P (type)
7040 /* Avoid ICE on invalid use of non-static member function. */
7041 || TREE_CODE (expr) == FUNCTION_DECL)
7042 return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
7043 else
7044 return c_common_truthvalue_conversion (loc, expr);
7047 /* Returns EXPR contextually converted to bool. */
7049 tree
7050 contextual_conv_bool (tree expr, tsubst_flags_t complain)
7052 return perform_implicit_conversion_flags (boolean_type_node, expr,
7053 complain, LOOKUP_NORMAL);
7056 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7057 is a low-level function; most callers should use maybe_convert_cond. */
7059 tree
7060 condition_conversion (tree expr)
7062 tree t = contextual_conv_bool (expr, tf_warning_or_error);
7063 if (!processing_template_decl)
7064 t = fold_build_cleanup_point_expr (boolean_type_node, t);
7065 return t;
7068 /* Returns the address of T. This function will fold away
7069 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7070 most places should use cp_build_addr_expr instead. */
7072 tree
7073 build_address (tree t)
7075 if (error_operand_p (t) || !cxx_mark_addressable (t))
7076 return error_mark_node;
7077 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
7078 || processing_template_decl);
7079 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
7080 if (TREE_CODE (t) != ADDR_EXPR)
7081 t = rvalue (t);
7082 return t;
7085 /* Return a NOP_EXPR converting EXPR to TYPE. */
7087 tree
7088 build_nop (tree type, tree expr)
7090 if (type == error_mark_node || error_operand_p (expr))
7091 return expr;
7092 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
7095 /* Take the address of ARG, whatever that means under C++ semantics.
7096 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7097 and class rvalues as well.
7099 Nothing should call this function directly; instead, callers should use
7100 cp_build_addr_expr or cp_build_addr_expr_strict. */
7102 static tree
7103 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
7105 tree argtype;
7106 tree val;
7108 if (!arg || error_operand_p (arg))
7109 return error_mark_node;
7111 arg = mark_lvalue_use (arg);
7112 if (error_operand_p (arg))
7113 return error_mark_node;
7115 argtype = lvalue_type (arg);
7116 location_t loc = cp_expr_loc_or_input_loc (arg);
7118 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
7120 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
7121 && !really_overloaded_fn (arg))
7123 /* They're trying to take the address of a unique non-static
7124 member function. This is ill-formed (except in MS-land),
7125 but let's try to DTRT.
7126 Note: We only handle unique functions here because we don't
7127 want to complain if there's a static overload; non-unique
7128 cases will be handled by instantiate_type. But we need to
7129 handle this case here to allow casts on the resulting PMF.
7130 We could defer this in non-MS mode, but it's easier to give
7131 a useful error here. */
7133 /* Inside constant member functions, the `this' pointer
7134 contains an extra const qualifier. TYPE_MAIN_VARIANT
7135 is used here to remove this const from the diagnostics
7136 and the created OFFSET_REF. */
7137 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
7138 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
7139 if (!mark_used (fn, complain) && !(complain & tf_error))
7140 return error_mark_node;
7141 /* Until microsoft headers are known to incorrectly take the address of
7142 unqualified xobj member functions we should not support this
7143 extension.
7144 See comment in class.cc:resolve_address_of_overloaded_function for
7145 the extended reasoning. */
7146 if (!flag_ms_extensions || DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7148 auto_diagnostic_group d;
7149 tree name = DECL_NAME (fn);
7150 if (!(complain & tf_error))
7151 return error_mark_node;
7152 else if (current_class_type
7153 && TREE_OPERAND (arg, 0) == current_class_ref)
7154 /* An expression like &memfn. */
7155 if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7156 permerror (loc,
7157 "ISO C++ forbids taking the address of an unqualified"
7158 " or parenthesized non-static member function to form"
7159 " a pointer to member function. Say %<&%T::%D%>",
7160 base, name);
7161 else
7162 error_at (loc,
7163 "ISO C++ forbids taking the address of an unqualified"
7164 " or parenthesized non-static member function to form"
7165 " a pointer to explicit object member function");
7166 else
7167 if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7168 permerror (loc,
7169 "ISO C++ forbids taking the address of a bound member"
7170 " function to form a pointer to member function."
7171 " Say %<&%T::%D%>",
7172 base, name);
7173 else
7174 error_at (loc,
7175 "ISO C++ forbids taking the address of a bound member"
7176 " function to form a pointer to explicit object member"
7177 " function");
7178 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
7179 inform (loc,
7180 "a pointer to explicit object member function can only be "
7181 "formed with %<&%T::%D%>", base, name);
7183 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
7186 /* Uninstantiated types are all functions. Taking the
7187 address of a function is a no-op, so just return the
7188 argument. */
7189 if (type_unknown_p (arg))
7190 return build1 (ADDR_EXPR, unknown_type_node, arg);
7192 if (TREE_CODE (arg) == OFFSET_REF)
7193 /* We want a pointer to member; bypass all the code for actually taking
7194 the address of something. */
7195 goto offset_ref;
7197 /* Anything not already handled and not a true memory reference
7198 is an error. */
7199 if (!FUNC_OR_METHOD_TYPE_P (argtype))
7201 cp_lvalue_kind kind = lvalue_kind (arg);
7202 if (kind == clk_none)
7204 if (complain & tf_error)
7205 lvalue_error (loc, lv_addressof);
7206 return error_mark_node;
7208 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
7210 if (!(complain & tf_error))
7211 return error_mark_node;
7212 /* Make this a permerror because we used to accept it. */
7213 permerror (loc, "taking address of rvalue");
7217 if (TYPE_REF_P (argtype))
7219 tree type = build_pointer_type (TREE_TYPE (argtype));
7220 arg = build1 (CONVERT_EXPR, type, arg);
7221 return arg;
7223 else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
7225 /* ARM $3.4 */
7226 /* Apparently a lot of autoconf scripts for C++ packages do this,
7227 so only complain if -Wpedantic. */
7228 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
7229 pedwarn (loc, OPT_Wpedantic,
7230 "ISO C++ forbids taking address of function %<::main%>");
7231 else if (flag_pedantic_errors)
7232 return error_mark_node;
7235 /* Let &* cancel out to simplify resulting code. */
7236 if (INDIRECT_REF_P (arg))
7238 arg = TREE_OPERAND (arg, 0);
7239 if (TYPE_REF_P (TREE_TYPE (arg)))
7241 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
7242 arg = build1 (CONVERT_EXPR, type, arg);
7244 else
7245 /* Don't let this be an lvalue. */
7246 arg = rvalue (arg);
7247 return arg;
7250 /* Handle complex lvalues (when permitted)
7251 by reduction to simpler cases. */
7252 val = unary_complex_lvalue (ADDR_EXPR, arg);
7253 if (val != 0)
7254 return val;
7256 switch (TREE_CODE (arg))
7258 CASE_CONVERT:
7259 case FLOAT_EXPR:
7260 case FIX_TRUNC_EXPR:
7261 /* We should have handled this above in the lvalue_kind check. */
7262 gcc_unreachable ();
7263 break;
7265 case BASELINK:
7266 arg = BASELINK_FUNCTIONS (arg);
7267 /* Fall through. */
7269 case OVERLOAD:
7270 arg = OVL_FIRST (arg);
7271 break;
7273 case OFFSET_REF:
7274 offset_ref:
7275 /* Turn a reference to a non-static data member into a
7276 pointer-to-member. */
7278 tree type;
7279 tree t;
7281 gcc_assert (PTRMEM_OK_P (arg));
7283 t = TREE_OPERAND (arg, 1);
7284 if (TYPE_REF_P (TREE_TYPE (t)))
7286 if (complain & tf_error)
7287 error_at (loc,
7288 "cannot create pointer to reference member %qD", t);
7289 return error_mark_node;
7292 /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7293 if (TREE_CODE (t) == FUNCTION_DECL
7294 && !DECL_PURE_VIRTUAL_P (t)
7295 && !mark_used (t, complain) && !(complain & tf_error))
7296 return error_mark_node;
7298 /* Pull out the function_decl for a single xobj member function, and
7299 let the rest of this function handle it. This is similar to how
7300 static member functions are handled in the BASELINK case above. */
7301 if (DECL_XOBJ_MEMBER_FUNCTION_P (t))
7303 arg = t;
7304 break;
7307 type = build_ptrmem_type (context_for_name_lookup (t),
7308 TREE_TYPE (t));
7309 t = make_ptrmem_cst (type, t);
7310 return t;
7313 default:
7314 break;
7317 if (argtype != error_mark_node)
7318 argtype = build_pointer_type (argtype);
7320 if (bitfield_p (arg))
7322 if (complain & tf_error)
7323 error_at (loc, "attempt to take address of bit-field");
7324 return error_mark_node;
7327 /* In a template, we are processing a non-dependent expression
7328 so we can just form an ADDR_EXPR with the correct type. */
7329 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
7331 if (!mark_single_function (arg, complain))
7332 return error_mark_node;
7333 val = build_address (arg);
7334 if (TREE_CODE (arg) == OFFSET_REF)
7335 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
7337 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
7339 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
7341 if (TREE_CODE (fn) == OVERLOAD && OVL_SINGLE_P (fn))
7342 fn = OVL_FIRST (fn);
7344 /* We can only get here with a single static member
7345 function. */
7346 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7347 && DECL_STATIC_FUNCTION_P (fn));
7348 if (!mark_used (fn, complain) && !(complain & tf_error))
7349 return error_mark_node;
7350 val = build_address (fn);
7351 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
7352 /* Do not lose object's side effects. */
7353 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
7354 TREE_OPERAND (arg, 0), val);
7356 else
7358 tree object = TREE_OPERAND (arg, 0);
7359 tree field = TREE_OPERAND (arg, 1);
7360 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7361 (TREE_TYPE (object), decl_type_context (field)));
7362 val = build_address (arg);
7365 if (TYPE_PTR_P (argtype)
7366 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
7368 build_ptrmemfunc_type (argtype);
7369 val = build_ptrmemfunc (argtype, val, 0,
7370 /*c_cast_p=*/false,
7371 complain);
7374 /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7375 if (TREE_CODE (val) == ADDR_EXPR
7376 && TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
7377 SET_EXPR_LOCATION (val, input_location);
7379 return val;
7382 /* Take the address of ARG if it has one, even if it's an rvalue. */
7384 tree
7385 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
7387 return cp_build_addr_expr_1 (arg, 0, complain);
7390 /* Take the address of ARG, but only if it's an lvalue. */
7392 static tree
7393 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
7395 return cp_build_addr_expr_1 (arg, 1, complain);
7398 /* C++: Must handle pointers to members.
7400 Perhaps type instantiation should be extended to handle conversion
7401 from aggregates to types we don't yet know we want? (Or are those
7402 cases typically errors which should be reported?)
7404 NOCONVERT suppresses the default promotions (such as from short to int). */
7406 tree
7407 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
7408 tsubst_flags_t complain)
7410 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7411 tree arg = xarg;
7412 location_t location = cp_expr_loc_or_input_loc (arg);
7413 tree argtype = 0;
7414 tree eptype = NULL_TREE;
7415 const char *errstring = NULL;
7416 tree val;
7417 const char *invalid_op_diag;
7419 if (!arg || error_operand_p (arg))
7420 return error_mark_node;
7422 arg = resolve_nondeduced_context (arg, complain);
7424 if ((invalid_op_diag
7425 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
7426 ? CONVERT_EXPR
7427 : code),
7428 TREE_TYPE (arg))))
7430 if (complain & tf_error)
7431 error (invalid_op_diag);
7432 return error_mark_node;
7435 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
7437 eptype = TREE_TYPE (arg);
7438 arg = TREE_OPERAND (arg, 0);
7441 switch (code)
7443 case UNARY_PLUS_EXPR:
7444 case NEGATE_EXPR:
7446 int flags = WANT_ARITH | WANT_ENUM;
7447 /* Unary plus (but not unary minus) is allowed on pointers. */
7448 if (code == UNARY_PLUS_EXPR)
7449 flags |= WANT_POINTER;
7450 arg = build_expr_type_conversion (flags, arg, true);
7451 if (!arg)
7452 errstring = (code == NEGATE_EXPR
7453 ? _("wrong type argument to unary minus")
7454 : _("wrong type argument to unary plus"));
7455 else
7457 if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7458 arg = cp_perform_integral_promotions (arg, complain);
7460 /* Make sure the result is not an lvalue: a unary plus or minus
7461 expression is always a rvalue. */
7462 arg = rvalue (arg);
7465 break;
7467 case BIT_NOT_EXPR:
7468 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7470 code = CONJ_EXPR;
7471 if (!noconvert)
7473 arg = cp_default_conversion (arg, complain);
7474 if (arg == error_mark_node)
7475 return error_mark_node;
7478 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
7479 | WANT_VECTOR_OR_COMPLEX,
7480 arg, true)))
7481 errstring = _("wrong type argument to bit-complement");
7482 else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
7484 /* Warn if the expression has boolean value. */
7485 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
7486 && (complain & tf_warning)
7487 && warning_at (location, OPT_Wbool_operation,
7488 "%<~%> on an expression of type %<bool%>"))
7489 inform (location, "did you mean to use logical not (%<!%>)?");
7490 arg = cp_perform_integral_promotions (arg, complain);
7492 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
7493 arg = mark_rvalue_use (arg);
7494 break;
7496 case ABS_EXPR:
7497 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7498 errstring = _("wrong type argument to abs");
7499 else if (!noconvert)
7501 arg = cp_default_conversion (arg, complain);
7502 if (arg == error_mark_node)
7503 return error_mark_node;
7505 break;
7507 case CONJ_EXPR:
7508 /* Conjugating a real value is a no-op, but allow it anyway. */
7509 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
7510 errstring = _("wrong type argument to conjugation");
7511 else if (!noconvert)
7513 arg = cp_default_conversion (arg, complain);
7514 if (arg == error_mark_node)
7515 return error_mark_node;
7517 break;
7519 case TRUTH_NOT_EXPR:
7520 if (gnu_vector_type_p (TREE_TYPE (arg)))
7521 return cp_build_binary_op (input_location, EQ_EXPR, arg,
7522 build_zero_cst (TREE_TYPE (arg)), complain);
7523 arg = perform_implicit_conversion (boolean_type_node, arg,
7524 complain);
7525 if (arg != error_mark_node)
7527 if (processing_template_decl)
7528 return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
7529 val = invert_truthvalue_loc (location, arg);
7530 if (obvalue_p (val))
7531 val = non_lvalue_loc (location, val);
7532 return val;
7534 errstring = _("in argument to unary !");
7535 break;
7537 case NOP_EXPR:
7538 break;
7540 case REALPART_EXPR:
7541 case IMAGPART_EXPR:
7542 val = build_real_imag_expr (input_location, code, arg);
7543 if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
7544 val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
7545 TREE_TYPE (eptype), val);
7546 return val;
7548 case PREINCREMENT_EXPR:
7549 case POSTINCREMENT_EXPR:
7550 case PREDECREMENT_EXPR:
7551 case POSTDECREMENT_EXPR:
7552 /* Handle complex lvalues (when permitted)
7553 by reduction to simpler cases. */
7555 val = unary_complex_lvalue (code, arg);
7556 if (val != 0)
7557 goto return_build_unary_op;
7559 arg = mark_lvalue_use (arg);
7561 /* Increment or decrement the real part of the value,
7562 and don't change the imaginary part. */
7563 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
7565 tree real, imag;
7567 arg = cp_stabilize_reference (arg);
7568 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
7569 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
7570 real = cp_build_unary_op (code, real, true, complain);
7571 if (real == error_mark_node || imag == error_mark_node)
7572 return error_mark_node;
7573 val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
7574 goto return_build_unary_op;
7577 /* Report invalid types. */
7579 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
7580 arg, true)))
7582 if (code == PREINCREMENT_EXPR)
7583 errstring = _("no pre-increment operator for type");
7584 else if (code == POSTINCREMENT_EXPR)
7585 errstring = _("no post-increment operator for type");
7586 else if (code == PREDECREMENT_EXPR)
7587 errstring = _("no pre-decrement operator for type");
7588 else
7589 errstring = _("no post-decrement operator for type");
7590 break;
7592 else if (arg == error_mark_node)
7593 return error_mark_node;
7595 /* Report something read-only. */
7597 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
7598 || TREE_READONLY (arg))
7600 if (complain & tf_error)
7601 cxx_readonly_error (location, arg,
7602 ((code == PREINCREMENT_EXPR
7603 || code == POSTINCREMENT_EXPR)
7604 ? lv_increment : lv_decrement));
7605 else
7606 return error_mark_node;
7610 tree inc;
7611 tree declared_type = unlowered_expr_type (arg);
7613 argtype = TREE_TYPE (arg);
7615 /* ARM $5.2.5 last annotation says this should be forbidden. */
7616 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
7618 if (complain & tf_error)
7619 permerror (location, (code == PREINCREMENT_EXPR
7620 || code == POSTINCREMENT_EXPR)
7621 ? G_("ISO C++ forbids incrementing an enum")
7622 : G_("ISO C++ forbids decrementing an enum"));
7623 else
7624 return error_mark_node;
7627 /* Compute the increment. */
7629 if (TYPE_PTR_P (argtype))
7631 tree type = complete_type (TREE_TYPE (argtype));
7633 if (!COMPLETE_OR_VOID_TYPE_P (type))
7635 if (complain & tf_error)
7636 error_at (location, ((code == PREINCREMENT_EXPR
7637 || code == POSTINCREMENT_EXPR))
7638 ? G_("cannot increment a pointer to incomplete "
7639 "type %qT")
7640 : G_("cannot decrement a pointer to incomplete "
7641 "type %qT"),
7642 TREE_TYPE (argtype));
7643 else
7644 return error_mark_node;
7646 else if (!TYPE_PTROB_P (argtype))
7648 if (complain & tf_error)
7649 pedwarn (location, OPT_Wpointer_arith,
7650 (code == PREINCREMENT_EXPR
7651 || code == POSTINCREMENT_EXPR)
7652 ? G_("ISO C++ forbids incrementing a pointer "
7653 "of type %qT")
7654 : G_("ISO C++ forbids decrementing a pointer "
7655 "of type %qT"),
7656 argtype);
7657 else
7658 return error_mark_node;
7660 else if (!verify_type_context (location, TCTX_POINTER_ARITH,
7661 TREE_TYPE (argtype),
7662 !(complain & tf_error)))
7663 return error_mark_node;
7665 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
7667 else
7668 inc = VECTOR_TYPE_P (argtype)
7669 ? build_one_cst (argtype)
7670 : integer_one_node;
7672 inc = cp_convert (argtype, inc, complain);
7674 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7675 need to ask Objective-C to build the increment or decrement
7676 expression for it. */
7677 if (objc_is_property_ref (arg))
7678 return objc_build_incr_expr_for_property_ref (input_location, code,
7679 arg, inc);
7681 /* Complain about anything else that is not a true lvalue. */
7682 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
7683 || code == POSTINCREMENT_EXPR)
7684 ? lv_increment : lv_decrement),
7685 complain))
7686 return error_mark_node;
7688 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7689 prefix ++ and -- expressions of volatile-qualified arithmetic
7690 and pointer types are deprecated." */
7691 if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
7692 && (complain & tf_warning))
7693 warning_at (location, OPT_Wvolatile,
7694 "%qs expression of %<volatile%>-qualified type is "
7695 "deprecated",
7696 ((code == PREINCREMENT_EXPR
7697 || code == POSTINCREMENT_EXPR)
7698 ? "++" : "--"));
7700 /* Forbid using -- or ++ in C++17 on `bool'. */
7701 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
7703 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
7705 if (complain & tf_error)
7706 error_at (location,
7707 "use of an operand of type %qT in %<operator--%> "
7708 "is forbidden", boolean_type_node);
7709 return error_mark_node;
7711 else
7713 if (cxx_dialect >= cxx17)
7715 if (complain & tf_error)
7716 error_at (location,
7717 "use of an operand of type %qT in "
7718 "%<operator++%> is forbidden in C++17",
7719 boolean_type_node);
7720 return error_mark_node;
7722 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7723 else if (complain & tf_warning)
7724 warning_at (location, OPT_Wdeprecated,
7725 "use of an operand of type %qT "
7726 "in %<operator++%> is deprecated",
7727 boolean_type_node);
7729 val = boolean_increment (code, arg);
7731 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
7732 /* An rvalue has no cv-qualifiers. */
7733 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
7734 else
7735 val = build2 (code, TREE_TYPE (arg), arg, inc);
7737 TREE_SIDE_EFFECTS (val) = 1;
7738 goto return_build_unary_op;
7741 case ADDR_EXPR:
7742 /* Note that this operation never does default_conversion
7743 regardless of NOCONVERT. */
7744 return cp_build_addr_expr (arg, complain);
7746 default:
7747 break;
7750 if (!errstring)
7752 if (argtype == 0)
7753 argtype = TREE_TYPE (arg);
7754 val = build1 (code, argtype, arg);
7755 return_build_unary_op:
7756 if (eptype)
7757 val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
7758 return val;
7761 if (complain & tf_error)
7762 error_at (location, "%s", errstring);
7763 return error_mark_node;
7766 /* Hook for the c-common bits that build a unary op. */
7767 tree
7768 build_unary_op (location_t /*location*/,
7769 enum tree_code code, tree xarg, bool noconvert)
7771 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
7774 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7775 so that it is a valid lvalue even for GENERIC by replacing
7776 (lhs = rhs) with ((lhs = rhs), lhs)
7777 (--lhs) with ((--lhs), lhs)
7778 (++lhs) with ((++lhs), lhs)
7779 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7780 that it can be evaluated multiple times. */
7782 tree
7783 genericize_compound_lvalue (tree lvalue)
7785 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
7786 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
7787 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
7788 TREE_OPERAND (lvalue, 1));
7789 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
7790 lvalue, TREE_OPERAND (lvalue, 0));
7793 /* Apply unary lvalue-demanding operator CODE to the expression ARG
7794 for certain kinds of expressions which are not really lvalues
7795 but which we can accept as lvalues.
7797 If ARG is not a kind of expression we can handle, return
7798 NULL_TREE. */
7800 tree
7801 unary_complex_lvalue (enum tree_code code, tree arg)
7803 /* Inside a template, making these kinds of adjustments is
7804 pointless; we are only concerned with the type of the
7805 expression. */
7806 if (processing_template_decl)
7807 return NULL_TREE;
7809 /* Handle (a, b) used as an "lvalue". */
7810 if (TREE_CODE (arg) == COMPOUND_EXPR)
7812 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
7813 tf_warning_or_error);
7814 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7815 TREE_OPERAND (arg, 0), real_result);
7818 /* Handle (a ? b : c) used as an "lvalue". */
7819 if (TREE_CODE (arg) == COND_EXPR
7820 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
7821 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
7823 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7824 if (TREE_CODE (arg) == MODIFY_EXPR
7825 || TREE_CODE (arg) == PREINCREMENT_EXPR
7826 || TREE_CODE (arg) == PREDECREMENT_EXPR)
7827 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
7829 if (code != ADDR_EXPR)
7830 return NULL_TREE;
7832 /* Handle (a = b) used as an "lvalue" for `&'. */
7833 if (TREE_CODE (arg) == MODIFY_EXPR
7834 || TREE_CODE (arg) == INIT_EXPR)
7836 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
7837 tf_warning_or_error);
7838 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
7839 arg, real_result);
7840 suppress_warning (arg /* What warning? */);
7841 return arg;
7844 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
7845 || TREE_CODE (arg) == OFFSET_REF)
7846 return NULL_TREE;
7848 /* We permit compiler to make function calls returning
7849 objects of aggregate type look like lvalues. */
7851 tree targ = arg;
7853 if (TREE_CODE (targ) == SAVE_EXPR)
7854 targ = TREE_OPERAND (targ, 0);
7856 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
7858 if (TREE_CODE (arg) == SAVE_EXPR)
7859 targ = arg;
7860 else
7861 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
7862 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
7865 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
7866 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
7867 TREE_OPERAND (targ, 0), current_function_decl, NULL);
7870 /* Don't let anything else be handled specially. */
7871 return NULL_TREE;
7874 /* Mark EXP saying that we need to be able to take the
7875 address of it; it should not be allocated in a register.
7876 Value is true if successful. ARRAY_REF_P is true if this
7877 is for ARRAY_REF construction - in that case we don't want
7878 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7879 it is fine to use ARRAY_REFs for vector subscripts on vector
7880 register variables.
7882 C++: we do not allow `current_class_ptr' to be addressable. */
7884 bool
7885 cxx_mark_addressable (tree exp, bool array_ref_p)
7887 tree x = exp;
7889 while (1)
7890 switch (TREE_CODE (x))
7892 case VIEW_CONVERT_EXPR:
7893 if (array_ref_p
7894 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7895 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
7896 return true;
7897 x = TREE_OPERAND (x, 0);
7898 break;
7900 case COMPONENT_REF:
7901 if (bitfield_p (x))
7902 error ("attempt to take address of bit-field");
7903 /* FALLTHRU */
7904 case ADDR_EXPR:
7905 case ARRAY_REF:
7906 case REALPART_EXPR:
7907 case IMAGPART_EXPR:
7908 x = TREE_OPERAND (x, 0);
7909 break;
7911 case PARM_DECL:
7912 if (x == current_class_ptr)
7914 error ("cannot take the address of %<this%>, which is an rvalue expression");
7915 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
7916 return true;
7918 /* Fall through. */
7920 case VAR_DECL:
7921 /* Caller should not be trying to mark initialized
7922 constant fields addressable. */
7923 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
7924 || DECL_IN_AGGR_P (x) == 0
7925 || TREE_STATIC (x)
7926 || DECL_EXTERNAL (x));
7927 /* Fall through. */
7929 case RESULT_DECL:
7930 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
7931 && !DECL_ARTIFICIAL (x))
7933 if (VAR_P (x) && DECL_HARD_REGISTER (x))
7935 error
7936 ("address of explicit register variable %qD requested", x);
7937 return false;
7939 else if (extra_warnings)
7940 warning
7941 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
7943 TREE_ADDRESSABLE (x) = 1;
7944 return true;
7946 case CONST_DECL:
7947 case FUNCTION_DECL:
7948 TREE_ADDRESSABLE (x) = 1;
7949 return true;
7951 case CONSTRUCTOR:
7952 TREE_ADDRESSABLE (x) = 1;
7953 return true;
7955 case TARGET_EXPR:
7956 TREE_ADDRESSABLE (x) = 1;
7957 cxx_mark_addressable (TREE_OPERAND (x, 0));
7958 return true;
7960 default:
7961 return true;
7965 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7967 tree
7968 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
7969 tsubst_flags_t complain)
7971 tree orig_ifexp = ifexp;
7972 tree orig_op1 = op1;
7973 tree orig_op2 = op2;
7974 tree expr;
7976 if (processing_template_decl)
7978 /* The standard says that the expression is type-dependent if
7979 IFEXP is type-dependent, even though the eventual type of the
7980 expression doesn't dependent on IFEXP. */
7981 if (type_dependent_expression_p (ifexp)
7982 /* As a GNU extension, the middle operand may be omitted. */
7983 || (op1 && type_dependent_expression_p (op1))
7984 || type_dependent_expression_p (op2))
7985 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
7988 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
7989 if (processing_template_decl && expr != error_mark_node)
7991 tree min = build_min_non_dep (COND_EXPR, expr,
7992 orig_ifexp, orig_op1, orig_op2);
7993 expr = convert_from_reference (min);
7995 return expr;
7998 /* Given a list of expressions, return a compound expression
7999 that performs them all and returns the value of the last of them. */
8001 tree
8002 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
8003 tsubst_flags_t complain)
8005 tree expr = TREE_VALUE (list);
8007 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8008 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
8010 if (complain & tf_error)
8011 pedwarn (cp_expr_loc_or_input_loc (expr), 0,
8012 "list-initializer for non-class type must not "
8013 "be parenthesized");
8014 else
8015 return error_mark_node;
8018 if (TREE_CHAIN (list))
8020 if (complain & tf_error)
8021 switch (exp)
8023 case ELK_INIT:
8024 permerror (input_location, "expression list treated as compound "
8025 "expression in initializer");
8026 break;
8027 case ELK_MEM_INIT:
8028 permerror (input_location, "expression list treated as compound "
8029 "expression in mem-initializer");
8030 break;
8031 case ELK_FUNC_CAST:
8032 permerror (input_location, "expression list treated as compound "
8033 "expression in functional cast");
8034 break;
8035 default:
8036 gcc_unreachable ();
8038 else
8039 return error_mark_node;
8041 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
8042 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
8043 expr, TREE_VALUE (list), NULL_TREE,
8044 complain);
8047 return expr;
8050 /* Like build_x_compound_expr_from_list, but using a VEC. */
8052 tree
8053 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
8054 tsubst_flags_t complain)
8056 if (vec_safe_is_empty (vec))
8057 return NULL_TREE;
8058 else if (vec->length () == 1)
8059 return (*vec)[0];
8060 else
8062 tree expr;
8063 unsigned int ix;
8064 tree t;
8066 if (msg != NULL)
8068 if (complain & tf_error)
8069 permerror (input_location,
8070 "%s expression list treated as compound expression",
8071 msg);
8072 else
8073 return error_mark_node;
8076 expr = (*vec)[0];
8077 for (ix = 1; vec->iterate (ix, &t); ++ix)
8078 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
8079 t, NULL_TREE, complain);
8081 return expr;
8085 /* Handle overloading of the ',' operator when needed. */
8087 tree
8088 build_x_compound_expr (location_t loc, tree op1, tree op2,
8089 tree lookups, tsubst_flags_t complain)
8091 tree result;
8092 tree orig_op1 = op1;
8093 tree orig_op2 = op2;
8094 tree overload = NULL_TREE;
8096 if (processing_template_decl)
8098 if (type_dependent_expression_p (op1)
8099 || type_dependent_expression_p (op2))
8101 result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
8102 TREE_TYPE (result)
8103 = build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
8104 return result;
8108 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
8109 NULL_TREE, lookups, &overload, complain);
8110 if (!result)
8111 result = cp_build_compound_expr (op1, op2, complain);
8113 if (processing_template_decl && result != error_mark_node)
8115 if (overload != NULL_TREE)
8116 return (build_min_non_dep_op_overload
8117 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
8119 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
8122 return result;
8125 /* Like cp_build_compound_expr, but for the c-common bits. */
8127 tree
8128 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
8130 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
8133 /* Build a compound expression. */
8135 tree
8136 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8138 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
8140 if (lhs == error_mark_node || rhs == error_mark_node)
8141 return error_mark_node;
8143 if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
8144 lhs = TREE_OPERAND (lhs, 0);
8145 tree eptype = NULL_TREE;
8146 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8148 eptype = TREE_TYPE (rhs);
8149 rhs = TREE_OPERAND (rhs, 0);
8152 if (TREE_CODE (rhs) == TARGET_EXPR)
8154 /* If the rhs is a TARGET_EXPR, then build the compound
8155 expression inside the target_expr's initializer. This
8156 helps the compiler to eliminate unnecessary temporaries. */
8157 tree init = TREE_OPERAND (rhs, 1);
8159 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
8160 TREE_OPERAND (rhs, 1) = init;
8162 if (eptype)
8163 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
8164 return rhs;
8167 if (type_unknown_p (rhs))
8169 if (complain & tf_error)
8170 error_at (cp_expr_loc_or_input_loc (rhs),
8171 "no context to resolve type of %qE", rhs);
8172 return error_mark_node;
8175 tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8176 if (eptype)
8177 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
8178 return ret;
8181 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8182 casts away constness. CAST gives the type of cast. Returns true
8183 if the cast is ill-formed, false if it is well-formed.
8185 ??? This function warns for casting away any qualifier not just
8186 const. We would like to specify exactly what qualifiers are casted
8187 away.
8190 static bool
8191 check_for_casting_away_constness (location_t loc, tree src_type,
8192 tree dest_type, enum tree_code cast,
8193 tsubst_flags_t complain)
8195 /* C-style casts are allowed to cast away constness. With
8196 WARN_CAST_QUAL, we still want to issue a warning. */
8197 if (cast == CAST_EXPR && !warn_cast_qual)
8198 return false;
8200 if (!casts_away_constness (src_type, dest_type, complain))
8201 return false;
8203 switch (cast)
8205 case CAST_EXPR:
8206 if (complain & tf_warning)
8207 warning_at (loc, OPT_Wcast_qual,
8208 "cast from type %qT to type %qT casts away qualifiers",
8209 src_type, dest_type);
8210 return false;
8212 case STATIC_CAST_EXPR:
8213 if (complain & tf_error)
8214 error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
8215 "away qualifiers",
8216 src_type, dest_type);
8217 return true;
8219 case REINTERPRET_CAST_EXPR:
8220 if (complain & tf_error)
8221 error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
8222 "casts away qualifiers",
8223 src_type, dest_type);
8224 return true;
8226 default:
8227 gcc_unreachable();
8231 /* Warns if the cast from expression EXPR to type TYPE is useless. */
8232 void
8233 maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
8234 tsubst_flags_t complain)
8236 if (warn_useless_cast
8237 && complain & tf_warning)
8239 if (TYPE_REF_P (type)
8240 ? ((TYPE_REF_IS_RVALUE (type)
8241 ? xvalue_p (expr) : lvalue_p (expr))
8242 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
8243 /* Don't warn when converting a class object to a non-reference type,
8244 because that's a common way to create a temporary. */
8245 : (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
8246 warning_at (loc, OPT_Wuseless_cast,
8247 "useless cast to type %q#T", type);
8251 /* Warns if the cast ignores cv-qualifiers on TYPE. */
8252 static void
8253 maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
8254 tsubst_flags_t complain)
8256 if (warn_ignored_qualifiers
8257 && complain & tf_warning
8258 && !CLASS_TYPE_P (type)
8259 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
8260 warning_at (loc, OPT_Wignored_qualifiers,
8261 "type qualifiers ignored on cast result type");
8264 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8265 (another pointer-to-member type in the same hierarchy) and return
8266 the converted expression. If ALLOW_INVERSE_P is permitted, a
8267 pointer-to-derived may be converted to pointer-to-base; otherwise,
8268 only the other direction is permitted. If C_CAST_P is true, this
8269 conversion is taking place as part of a C-style cast. */
8271 tree
8272 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
8273 bool c_cast_p, tsubst_flags_t complain)
8275 if (same_type_p (type, TREE_TYPE (expr)))
8276 return expr;
8278 if (TYPE_PTRDATAMEM_P (type))
8280 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
8281 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
8282 tree delta = (get_delta_difference
8283 (obase, nbase,
8284 allow_inverse_p, c_cast_p, complain));
8286 if (delta == error_mark_node)
8287 return error_mark_node;
8289 if (!same_type_p (obase, nbase))
8291 if (TREE_CODE (expr) == PTRMEM_CST)
8292 expr = cplus_expand_constant (expr);
8294 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
8295 build_int_cst (TREE_TYPE (expr), -1),
8296 complain);
8297 tree op1 = build_nop (ptrdiff_type_node, expr);
8298 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
8299 complain);
8301 expr = fold_build3_loc (input_location,
8302 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
8305 return build_nop (type, expr);
8307 else
8308 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
8309 allow_inverse_p, c_cast_p, complain);
8312 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8313 this static_cast is being attempted as one of the possible casts
8314 allowed by a C-style cast. (In that case, accessibility of base
8315 classes is not considered, and it is OK to cast away
8316 constness.) Return the result of the cast. *VALID_P is set to
8317 indicate whether or not the cast was valid. */
8319 static tree
8320 build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
8321 bool *valid_p, tsubst_flags_t complain)
8323 tree intype;
8324 tree result;
8325 cp_lvalue_kind clk;
8327 /* Assume the cast is valid. */
8328 *valid_p = true;
8330 intype = unlowered_expr_type (expr);
8332 /* Save casted types in the function's used types hash table. */
8333 used_types_insert (type);
8335 /* A prvalue of non-class type is cv-unqualified. */
8336 if (!CLASS_TYPE_P (type))
8337 type = cv_unqualified (type);
8339 /* [expr.static.cast]
8341 An lvalue of type "cv1 B", where B is a class type, can be cast
8342 to type "reference to cv2 D", where D is a class derived (clause
8343 _class.derived_) from B, if a valid standard conversion from
8344 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8345 same cv-qualification as, or greater cv-qualification than, cv1,
8346 and B is not a virtual base class of D. */
8347 /* We check this case before checking the validity of "TYPE t =
8348 EXPR;" below because for this case:
8350 struct B {};
8351 struct D : public B { D(const B&); };
8352 extern B& b;
8353 void f() { static_cast<const D&>(b); }
8355 we want to avoid constructing a new D. The standard is not
8356 completely clear about this issue, but our interpretation is
8357 consistent with other compilers. */
8358 if (TYPE_REF_P (type)
8359 && CLASS_TYPE_P (TREE_TYPE (type))
8360 && CLASS_TYPE_P (intype)
8361 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
8362 && DERIVED_FROM_P (intype, TREE_TYPE (type))
8363 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
8364 build_pointer_type (TYPE_MAIN_VARIANT
8365 (TREE_TYPE (type))),
8366 complain)
8367 && (c_cast_p
8368 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8370 tree base;
8372 if (processing_template_decl)
8373 return expr;
8375 /* There is a standard conversion from "D*" to "B*" even if "B"
8376 is ambiguous or inaccessible. If this is really a
8377 static_cast, then we check both for inaccessibility and
8378 ambiguity. However, if this is a static_cast being performed
8379 because the user wrote a C-style cast, then accessibility is
8380 not considered. */
8381 base = lookup_base (TREE_TYPE (type), intype,
8382 c_cast_p ? ba_unique : ba_check,
8383 NULL, complain);
8384 expr = cp_build_addr_expr (expr, complain);
8386 if (sanitize_flags_p (SANITIZE_VPTR))
8388 tree ubsan_check
8389 = cp_ubsan_maybe_instrument_downcast (loc, type,
8390 intype, expr);
8391 if (ubsan_check)
8392 expr = ubsan_check;
8395 /* Convert from "B*" to "D*". This function will check that "B"
8396 is not a virtual base of "D". Even if we don't have a guarantee
8397 that expr is NULL, if the static_cast is to a reference type,
8398 it is UB if it would be NULL, so omit the non-NULL check. */
8399 expr = build_base_path (MINUS_EXPR, expr, base,
8400 /*nonnull=*/flag_delete_null_pointer_checks,
8401 complain);
8403 /* Convert the pointer to a reference -- but then remember that
8404 there are no expressions with reference type in C++.
8406 We call rvalue so that there's an actual tree code
8407 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8408 is a variable with the same type, the conversion would get folded
8409 away, leaving just the variable and causing lvalue_kind to give
8410 the wrong answer. */
8411 expr = cp_fold_convert (type, expr);
8413 /* When -fsanitize=null, make sure to diagnose reference binding to
8414 NULL even when the reference is converted to pointer later on. */
8415 if (sanitize_flags_p (SANITIZE_NULL)
8416 && TREE_CODE (expr) == COND_EXPR
8417 && TREE_OPERAND (expr, 2)
8418 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
8419 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
8420 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
8422 return convert_from_reference (rvalue (expr));
8425 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8426 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8427 if (TYPE_REF_P (type)
8428 && TYPE_REF_IS_RVALUE (type)
8429 && (clk = real_lvalue_p (expr))
8430 && reference_compatible_p (TREE_TYPE (type), intype)
8431 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
8433 if (processing_template_decl)
8434 return expr;
8435 if (clk == clk_ordinary)
8437 /* Handle the (non-bit-field) lvalue case here by casting to
8438 lvalue reference and then changing it to an rvalue reference.
8439 Casting an xvalue to rvalue reference will be handled by the
8440 main code path. */
8441 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
8442 result = (perform_direct_initialization_if_possible
8443 (lref, expr, c_cast_p, complain));
8444 result = build1 (NON_LVALUE_EXPR, type, result);
8445 return convert_from_reference (result);
8447 else
8448 /* For a bit-field or packed field, bind to a temporary. */
8449 expr = rvalue (expr);
8452 /* Resolve overloaded address here rather than once in
8453 implicit_conversion and again in the inverse code below. */
8454 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
8456 expr = instantiate_type (type, expr, complain);
8457 intype = TREE_TYPE (expr);
8460 /* [expr.static.cast]
8462 Any expression can be explicitly converted to type cv void. */
8463 if (VOID_TYPE_P (type))
8465 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8466 expr = TREE_OPERAND (expr, 0);
8467 return convert_to_void (expr, ICV_CAST, complain);
8470 /* [class.abstract]
8471 An abstract class shall not be used ... as the type of an explicit
8472 conversion. */
8473 if (abstract_virtuals_error (ACU_CAST, type, complain))
8474 return error_mark_node;
8476 /* [expr.static.cast]
8478 An expression e can be explicitly converted to a type T using a
8479 static_cast of the form static_cast<T>(e) if the declaration T
8480 t(e);" is well-formed, for some invented temporary variable
8481 t. */
8482 result = perform_direct_initialization_if_possible (type, expr,
8483 c_cast_p, complain);
8484 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8485 which initialize the first element of the aggregate. We need to handle
8486 the array case specifically. */
8487 if (result == NULL_TREE
8488 && cxx_dialect >= cxx20
8489 && TREE_CODE (type) == ARRAY_TYPE)
8491 /* Create { EXPR } and perform direct-initialization from it. */
8492 tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
8493 CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
8494 CONSTRUCTOR_IS_PAREN_INIT (e) = true;
8495 result = perform_direct_initialization_if_possible (type, e, c_cast_p,
8496 complain);
8498 if (result)
8500 if (processing_template_decl)
8501 return expr;
8503 result = convert_from_reference (result);
8505 /* [expr.static.cast]
8507 If T is a reference type, the result is an lvalue; otherwise,
8508 the result is an rvalue. */
8509 if (!TYPE_REF_P (type))
8511 result = rvalue (result);
8513 if (result == expr && SCALAR_TYPE_P (type))
8514 /* Leave some record of the cast. */
8515 result = build_nop (type, expr);
8517 return result;
8520 /* [expr.static.cast]
8522 The inverse of any standard conversion sequence (clause _conv_),
8523 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8524 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8525 (_conv.bool_) conversions, can be performed explicitly using
8526 static_cast subject to the restriction that the explicit
8527 conversion does not cast away constness (_expr.const.cast_), and
8528 the following additional rules for specific cases: */
8529 /* For reference, the conversions not excluded are: integral
8530 promotions, floating-point promotion, integral conversions,
8531 floating-point conversions, floating-integral conversions,
8532 pointer conversions, and pointer to member conversions. */
8533 /* DR 128
8535 A value of integral _or enumeration_ type can be explicitly
8536 converted to an enumeration type. */
8537 /* The effect of all that is that any conversion between any two
8538 types which are integral, floating, or enumeration types can be
8539 performed. */
8540 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8541 || SCALAR_FLOAT_TYPE_P (type))
8542 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
8543 || SCALAR_FLOAT_TYPE_P (intype)))
8545 if (processing_template_decl)
8546 return expr;
8547 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8548 expr = TREE_OPERAND (expr, 0);
8549 /* [expr.static.cast]: "If the value is not a bit-field, the result
8550 refers to the object or the specified base class subobject thereof;
8551 otherwise, the lvalue-to-rvalue conversion is applied to the
8552 bit-field and the resulting prvalue is used as the operand of the
8553 static_cast." There are no prvalue bit-fields; the l-to-r conversion
8554 will give us an object of the underlying type of the bit-field. */
8555 expr = decay_conversion (expr, complain);
8556 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
8559 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
8560 && CLASS_TYPE_P (TREE_TYPE (type))
8561 && CLASS_TYPE_P (TREE_TYPE (intype))
8562 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8563 (TREE_TYPE (intype))),
8564 build_pointer_type (TYPE_MAIN_VARIANT
8565 (TREE_TYPE (type))),
8566 complain))
8568 tree base;
8570 if (processing_template_decl)
8571 return expr;
8573 if (!c_cast_p
8574 && check_for_casting_away_constness (loc, intype, type,
8575 STATIC_CAST_EXPR,
8576 complain))
8577 return error_mark_node;
8578 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
8579 c_cast_p ? ba_unique : ba_check,
8580 NULL, complain);
8581 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
8582 complain);
8584 if (sanitize_flags_p (SANITIZE_VPTR))
8586 tree ubsan_check
8587 = cp_ubsan_maybe_instrument_downcast (loc, type,
8588 intype, expr);
8589 if (ubsan_check)
8590 expr = ubsan_check;
8593 return cp_fold_convert (type, expr);
8596 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8597 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
8599 tree c1;
8600 tree c2;
8601 tree t1;
8602 tree t2;
8604 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
8605 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
8607 if (TYPE_PTRDATAMEM_P (type))
8609 t1 = (build_ptrmem_type
8610 (c1,
8611 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
8612 t2 = (build_ptrmem_type
8613 (c2,
8614 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
8616 else
8618 t1 = intype;
8619 t2 = type;
8621 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
8623 if (!c_cast_p
8624 && check_for_casting_away_constness (loc, intype, type,
8625 STATIC_CAST_EXPR,
8626 complain))
8627 return error_mark_node;
8628 if (processing_template_decl)
8629 return expr;
8630 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
8631 c_cast_p, complain);
8635 /* [expr.static.cast]
8637 An rvalue of type "pointer to cv void" can be explicitly
8638 converted to a pointer to object type. A value of type pointer
8639 to object converted to "pointer to cv void" and back to the
8640 original pointer type will have its original value. */
8641 if (TYPE_PTR_P (intype)
8642 && VOID_TYPE_P (TREE_TYPE (intype))
8643 && TYPE_PTROB_P (type))
8645 if (!c_cast_p
8646 && check_for_casting_away_constness (loc, intype, type,
8647 STATIC_CAST_EXPR,
8648 complain))
8649 return error_mark_node;
8650 if (processing_template_decl)
8651 return expr;
8652 return build_nop (type, expr);
8655 *valid_p = false;
8656 return error_mark_node;
8659 /* Return an expression representing static_cast<TYPE>(EXPR). */
8661 tree
8662 build_static_cast (location_t loc, tree type, tree oexpr,
8663 tsubst_flags_t complain)
8665 tree expr = oexpr;
8666 tree result;
8667 bool valid_p;
8669 if (type == error_mark_node || expr == error_mark_node)
8670 return error_mark_node;
8672 bool dependent = (dependent_type_p (type)
8673 || type_dependent_expression_p (expr));
8674 if (dependent)
8676 tmpl:
8677 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
8678 /* We don't know if it will or will not have side effects. */
8679 TREE_SIDE_EFFECTS (expr) = 1;
8680 result = convert_from_reference (expr);
8681 protected_set_expr_location (result, loc);
8682 return result;
8685 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8686 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8687 if (!TYPE_REF_P (type)
8688 && TREE_CODE (expr) == NOP_EXPR
8689 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8690 expr = TREE_OPERAND (expr, 0);
8692 result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
8693 &valid_p, complain);
8694 if (valid_p)
8696 if (result != error_mark_node)
8698 maybe_warn_about_useless_cast (loc, type, expr, complain);
8699 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
8701 if (processing_template_decl)
8702 goto tmpl;
8703 protected_set_expr_location (result, loc);
8704 return result;
8707 if (complain & tf_error)
8709 error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
8710 TREE_TYPE (expr), type);
8711 if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
8712 && CLASS_TYPE_P (TREE_TYPE (type))
8713 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
8714 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
8715 "class type %qT is incomplete", TREE_TYPE (type));
8716 tree expr_type = TREE_TYPE (expr);
8717 if (TYPE_PTR_P (expr_type))
8718 expr_type = TREE_TYPE (expr_type);
8719 if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
8720 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
8721 "class type %qT is incomplete", expr_type);
8723 return error_mark_node;
8726 /* EXPR is an expression with member function or pointer-to-member
8727 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8728 not permitted by ISO C++, but we accept it in some modes. If we
8729 are not in one of those modes, issue a diagnostic. Return the
8730 converted expression. */
8732 tree
8733 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
8735 tree intype;
8736 tree decl;
8738 intype = TREE_TYPE (expr);
8739 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
8740 || TREE_CODE (intype) == METHOD_TYPE);
8742 if (!(complain & tf_warning_or_error))
8743 return error_mark_node;
8745 location_t loc = cp_expr_loc_or_input_loc (expr);
8747 if (pedantic || warn_pmf2ptr)
8748 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
8749 "converting from %qH to %qI", intype, type);
8751 STRIP_ANY_LOCATION_WRAPPER (expr);
8753 if (TREE_CODE (intype) == METHOD_TYPE)
8754 expr = build_addr_func (expr, complain);
8755 else if (TREE_CODE (expr) == PTRMEM_CST)
8756 expr = build_address (PTRMEM_CST_MEMBER (expr));
8757 else
8759 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
8760 decl = build_address (decl);
8761 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
8764 if (expr == error_mark_node)
8765 return error_mark_node;
8767 expr = build_nop (type, expr);
8768 SET_EXPR_LOCATION (expr, loc);
8769 return expr;
8772 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8773 constexpr evaluation knows to reject it. */
8775 static tree
8776 build_nop_reinterpret (tree type, tree expr)
8778 tree ret = build_nop (type, expr);
8779 if (ret != expr)
8780 REINTERPRET_CAST_P (ret) = true;
8781 return ret;
8784 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8785 If C_CAST_P is true, this reinterpret cast is being done as part of
8786 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8787 indicate whether or not reinterpret_cast was valid. */
8789 static tree
8790 build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
8791 bool c_cast_p, bool *valid_p,
8792 tsubst_flags_t complain)
8794 tree intype;
8796 /* Assume the cast is invalid. */
8797 if (valid_p)
8798 *valid_p = true;
8800 if (type == error_mark_node || error_operand_p (expr))
8801 return error_mark_node;
8803 intype = TREE_TYPE (expr);
8805 /* Save casted types in the function's used types hash table. */
8806 used_types_insert (type);
8808 /* A prvalue of non-class type is cv-unqualified. */
8809 if (!CLASS_TYPE_P (type))
8810 type = cv_unqualified (type);
8812 /* [expr.reinterpret.cast]
8813 A glvalue of type T1, designating an object x, can be cast to the type
8814 "reference to T2" if an expression of type "pointer to T1" can be
8815 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8816 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8817 of type "pointer to T1". No temporary is created, no copy is made, and no
8818 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8819 if (TYPE_REF_P (type))
8821 if (!glvalue_p (expr))
8823 if (complain & tf_error)
8824 error_at (loc, "invalid cast of a prvalue expression of type "
8825 "%qT to type %qT",
8826 intype, type);
8827 return error_mark_node;
8830 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8831 "B" are related class types; the reinterpret_cast does not
8832 adjust the pointer. */
8833 if (TYPE_PTR_P (intype)
8834 && (complain & tf_warning)
8835 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
8836 COMPARE_BASE | COMPARE_DERIVED)))
8837 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
8838 intype, type);
8840 expr = cp_build_addr_expr (expr, complain);
8842 if (warn_strict_aliasing > 2)
8843 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8845 if (expr != error_mark_node)
8846 expr = build_reinterpret_cast_1
8847 (loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
8848 valid_p, complain);
8849 if (expr != error_mark_node)
8850 /* cp_build_indirect_ref isn't right for rvalue refs. */
8851 expr = convert_from_reference (fold_convert (type, expr));
8852 return expr;
8855 /* As a G++ extension, we consider conversions from member
8856 functions, and pointers to member functions to
8857 pointer-to-function and pointer-to-void types. If
8858 -Wno-pmf-conversions has not been specified,
8859 convert_member_func_to_ptr will issue an error message. */
8860 if ((TYPE_PTRMEMFUNC_P (intype)
8861 || TREE_CODE (intype) == METHOD_TYPE)
8862 && TYPE_PTR_P (type)
8863 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
8864 || VOID_TYPE_P (TREE_TYPE (type))))
8865 return convert_member_func_to_ptr (type, expr, complain);
8867 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8868 array-to-pointer, and function-to-pointer conversions are
8869 performed. */
8870 expr = decay_conversion (expr, complain);
8872 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8873 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8874 if (TREE_CODE (expr) == NOP_EXPR
8875 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
8876 expr = TREE_OPERAND (expr, 0);
8878 if (error_operand_p (expr))
8879 return error_mark_node;
8881 intype = TREE_TYPE (expr);
8883 /* [expr.reinterpret.cast]
8884 A pointer can be converted to any integral type large enough to
8885 hold it. ... A value of type std::nullptr_t can be converted to
8886 an integral type; the conversion has the same meaning and
8887 validity as a conversion of (void*)0 to the integral type. */
8888 if (CP_INTEGRAL_TYPE_P (type)
8889 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8891 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
8893 if (complain & tf_error)
8894 permerror (loc, "cast from %qH to %qI loses precision",
8895 intype, type);
8896 else
8897 return error_mark_node;
8899 if (NULLPTR_TYPE_P (intype))
8900 return build_int_cst (type, 0);
8902 /* [expr.reinterpret.cast]
8903 A value of integral or enumeration type can be explicitly
8904 converted to a pointer. */
8905 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
8906 /* OK */
8908 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
8909 || TYPE_PTR_OR_PTRMEM_P (type))
8910 && same_type_p (type, intype))
8911 /* DR 799 */
8912 return rvalue (expr);
8913 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
8915 if ((complain & tf_warning)
8916 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
8917 TREE_TYPE (intype)))
8918 warning_at (loc, OPT_Wcast_function_type,
8919 "cast between incompatible function types"
8920 " from %qH to %qI", intype, type);
8921 return build_nop_reinterpret (type, expr);
8923 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
8925 if ((complain & tf_warning)
8926 && !cxx_safe_function_type_cast_p
8927 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
8928 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
8929 warning_at (loc, OPT_Wcast_function_type,
8930 "cast between incompatible pointer to member types"
8931 " from %qH to %qI", intype, type);
8932 return build_nop_reinterpret (type, expr);
8934 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
8935 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
8937 if (!c_cast_p
8938 && check_for_casting_away_constness (loc, intype, type,
8939 REINTERPRET_CAST_EXPR,
8940 complain))
8941 return error_mark_node;
8942 /* Warn about possible alignment problems. */
8943 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
8944 && (complain & tf_warning)
8945 && !VOID_TYPE_P (type)
8946 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
8947 && COMPLETE_TYPE_P (TREE_TYPE (type))
8948 && COMPLETE_TYPE_P (TREE_TYPE (intype))
8949 && min_align_of_type (TREE_TYPE (type))
8950 > min_align_of_type (TREE_TYPE (intype)))
8951 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
8952 "increases required alignment of target type",
8953 intype, type);
8955 if (warn_strict_aliasing <= 2)
8956 /* strict_aliasing_warning STRIP_NOPs its expr. */
8957 cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
8959 return build_nop_reinterpret (type, expr);
8961 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
8962 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
8964 if (complain & tf_warning)
8965 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8966 object pointer type or vice versa is conditionally-supported." */
8967 warning_at (loc, OPT_Wconditionally_supported,
8968 "casting between pointer-to-function and "
8969 "pointer-to-object is conditionally-supported");
8970 return build_nop_reinterpret (type, expr);
8972 else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
8973 return convert_to_vector (type, rvalue (expr));
8974 else if (gnu_vector_type_p (intype)
8975 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8976 return convert_to_integer_nofold (type, expr);
8977 else
8979 if (valid_p)
8980 *valid_p = false;
8981 if (complain & tf_error)
8982 error_at (loc, "invalid cast from type %qT to type %qT",
8983 intype, type);
8984 return error_mark_node;
8987 expr = cp_convert (type, expr, complain);
8988 if (TREE_CODE (expr) == NOP_EXPR)
8989 /* Mark any nop_expr that created as a reintepret_cast. */
8990 REINTERPRET_CAST_P (expr) = true;
8991 return expr;
8994 tree
8995 build_reinterpret_cast (location_t loc, tree type, tree expr,
8996 tsubst_flags_t complain)
8998 tree r;
9000 if (type == error_mark_node || expr == error_mark_node)
9001 return error_mark_node;
9003 if (processing_template_decl)
9005 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
9007 if (!TREE_SIDE_EFFECTS (t)
9008 && type_dependent_expression_p (expr))
9009 /* There might turn out to be side effects inside expr. */
9010 TREE_SIDE_EFFECTS (t) = 1;
9011 r = convert_from_reference (t);
9012 protected_set_expr_location (r, loc);
9013 return r;
9016 r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
9017 /*valid_p=*/NULL, complain);
9018 if (r != error_mark_node)
9020 maybe_warn_about_useless_cast (loc, type, expr, complain);
9021 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9023 protected_set_expr_location (r, loc);
9024 return r;
9027 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9028 return an appropriate expression. Otherwise, return
9029 error_mark_node. If the cast is not valid, and COMPLAIN is true,
9030 then a diagnostic will be issued. If VALID_P is non-NULL, we are
9031 performing a C-style cast, its value upon return will indicate
9032 whether or not the conversion succeeded. */
9034 static tree
9035 build_const_cast_1 (location_t loc, tree dst_type, tree expr,
9036 tsubst_flags_t complain, bool *valid_p)
9038 tree src_type;
9039 tree reference_type;
9041 /* Callers are responsible for handling error_mark_node as a
9042 destination type. */
9043 gcc_assert (dst_type != error_mark_node);
9044 /* In a template, callers should be building syntactic
9045 representations of casts, not using this machinery. */
9046 gcc_assert (!processing_template_decl);
9048 /* Assume the conversion is invalid. */
9049 if (valid_p)
9050 *valid_p = false;
9052 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
9054 if (complain & tf_error)
9055 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9056 "which is not a pointer, reference, "
9057 "nor a pointer-to-data-member type", dst_type);
9058 return error_mark_node;
9061 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
9063 if (complain & tf_error)
9064 error_at (loc, "invalid use of %<const_cast%> with type %qT, "
9065 "which is a pointer or reference to a function type",
9066 dst_type);
9067 return error_mark_node;
9070 /* A prvalue of non-class type is cv-unqualified. */
9071 dst_type = cv_unqualified (dst_type);
9073 /* Save casted types in the function's used types hash table. */
9074 used_types_insert (dst_type);
9076 src_type = TREE_TYPE (expr);
9077 /* Expressions do not really have reference types. */
9078 if (TYPE_REF_P (src_type))
9079 src_type = TREE_TYPE (src_type);
9081 /* [expr.const.cast]
9083 For two object types T1 and T2, if a pointer to T1 can be explicitly
9084 converted to the type "pointer to T2" using a const_cast, then the
9085 following conversions can also be made:
9087 -- an lvalue of type T1 can be explicitly converted to an lvalue of
9088 type T2 using the cast const_cast<T2&>;
9090 -- a glvalue of type T1 can be explicitly converted to an xvalue of
9091 type T2 using the cast const_cast<T2&&>; and
9093 -- if T1 is a class type, a prvalue of type T1 can be explicitly
9094 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9096 if (TYPE_REF_P (dst_type))
9098 reference_type = dst_type;
9099 if (!TYPE_REF_IS_RVALUE (dst_type)
9100 ? lvalue_p (expr)
9101 : obvalue_p (expr))
9102 /* OK. */;
9103 else
9105 if (complain & tf_error)
9106 error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
9107 "to type %qT",
9108 src_type, dst_type);
9109 return error_mark_node;
9111 dst_type = build_pointer_type (TREE_TYPE (dst_type));
9112 src_type = build_pointer_type (src_type);
9114 else
9116 reference_type = NULL_TREE;
9117 /* If the destination type is not a reference type, the
9118 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9119 conversions are performed. */
9120 src_type = type_decays_to (src_type);
9121 if (src_type == error_mark_node)
9122 return error_mark_node;
9125 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
9127 if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
9129 if (valid_p)
9131 *valid_p = true;
9132 /* This cast is actually a C-style cast. Issue a warning if
9133 the user is making a potentially unsafe cast. */
9134 check_for_casting_away_constness (loc, src_type, dst_type,
9135 CAST_EXPR, complain);
9136 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9137 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
9138 && (complain & tf_warning)
9139 && min_align_of_type (TREE_TYPE (dst_type))
9140 > min_align_of_type (TREE_TYPE (src_type)))
9141 warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
9142 "increases required alignment of target type",
9143 src_type, dst_type);
9145 if (reference_type)
9147 expr = cp_build_addr_expr (expr, complain);
9148 if (expr == error_mark_node)
9149 return error_mark_node;
9150 expr = build_nop (reference_type, expr);
9151 return convert_from_reference (expr);
9153 else
9155 expr = decay_conversion (expr, complain);
9156 if (expr == error_mark_node)
9157 return error_mark_node;
9159 /* build_c_cast puts on a NOP_EXPR to make the result not an
9160 lvalue. Strip such NOP_EXPRs if VALUE is being used in
9161 non-lvalue context. */
9162 if (TREE_CODE (expr) == NOP_EXPR
9163 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
9164 expr = TREE_OPERAND (expr, 0);
9165 return build_nop (dst_type, expr);
9168 else if (valid_p
9169 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
9170 TREE_TYPE (src_type)))
9171 check_for_casting_away_constness (loc, src_type, dst_type,
9172 CAST_EXPR, complain);
9175 if (complain & tf_error)
9176 error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
9177 src_type, dst_type);
9178 return error_mark_node;
9181 tree
9182 build_const_cast (location_t loc, tree type, tree expr,
9183 tsubst_flags_t complain)
9185 tree r;
9187 if (type == error_mark_node || error_operand_p (expr))
9188 return error_mark_node;
9190 if (processing_template_decl)
9192 tree t = build_min (CONST_CAST_EXPR, type, expr);
9194 if (!TREE_SIDE_EFFECTS (t)
9195 && type_dependent_expression_p (expr))
9196 /* There might turn out to be side effects inside expr. */
9197 TREE_SIDE_EFFECTS (t) = 1;
9198 r = convert_from_reference (t);
9199 protected_set_expr_location (r, loc);
9200 return r;
9203 r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
9204 if (r != error_mark_node)
9206 maybe_warn_about_useless_cast (loc, type, expr, complain);
9207 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9209 protected_set_expr_location (r, loc);
9210 return r;
9213 /* Like cp_build_c_cast, but for the c-common bits. */
9215 tree
9216 build_c_cast (location_t loc, tree type, tree expr)
9218 return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9221 /* Like the "build_c_cast" used for c-common, but using cp_expr to
9222 preserve location information even for tree nodes that don't
9223 support it. */
9225 cp_expr
9226 build_c_cast (location_t loc, tree type, cp_expr expr)
9228 cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
9229 result.set_location (loc);
9230 return result;
9233 /* Build an expression representing an explicit C-style cast to type
9234 TYPE of expression EXPR. */
9236 tree
9237 cp_build_c_cast (location_t loc, tree type, tree expr,
9238 tsubst_flags_t complain)
9240 tree value = expr;
9241 tree result;
9242 bool valid_p;
9244 if (type == error_mark_node || error_operand_p (expr))
9245 return error_mark_node;
9247 if (processing_template_decl)
9249 tree t = build_min (CAST_EXPR, type,
9250 tree_cons (NULL_TREE, value, NULL_TREE));
9251 /* We don't know if it will or will not have side effects. */
9252 TREE_SIDE_EFFECTS (t) = 1;
9253 return convert_from_reference (t);
9256 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9257 'Class') should always be retained, because this information aids
9258 in method lookup. */
9259 if (objc_is_object_ptr (type)
9260 && objc_is_object_ptr (TREE_TYPE (expr)))
9261 return build_nop (type, expr);
9263 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9264 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9265 if (!TYPE_REF_P (type)
9266 && TREE_CODE (value) == NOP_EXPR
9267 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
9268 value = TREE_OPERAND (value, 0);
9270 if (TREE_CODE (type) == ARRAY_TYPE)
9272 /* Allow casting from T1* to T2[] because Cfront allows it.
9273 NIHCL uses it. It is not valid ISO C++ however. */
9274 if (TYPE_PTR_P (TREE_TYPE (expr)))
9276 if (complain & tf_error)
9277 permerror (loc, "ISO C++ forbids casting to an array type %qT",
9278 type);
9279 else
9280 return error_mark_node;
9281 type = build_pointer_type (TREE_TYPE (type));
9283 else
9285 if (complain & tf_error)
9286 error_at (loc, "ISO C++ forbids casting to an array type %qT",
9287 type);
9288 return error_mark_node;
9292 if (FUNC_OR_METHOD_TYPE_P (type))
9294 if (complain & tf_error)
9295 error_at (loc, "invalid cast to function type %qT", type);
9296 return error_mark_node;
9299 if (TYPE_PTR_P (type)
9300 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
9301 /* Casting to an integer of smaller size is an error detected elsewhere. */
9302 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
9303 /* Don't warn about converting any constant. */
9304 && !TREE_CONSTANT (value))
9305 warning_at (loc, OPT_Wint_to_pointer_cast,
9306 "cast to pointer from integer of different size");
9308 /* A C-style cast can be a const_cast. */
9309 result = build_const_cast_1 (loc, type, value, complain & tf_warning,
9310 &valid_p);
9311 if (valid_p)
9313 if (result != error_mark_node)
9315 maybe_warn_about_useless_cast (loc, type, value, complain);
9316 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9318 else if (complain & tf_error)
9319 build_const_cast_1 (loc, type, value, tf_error, &valid_p);
9320 return result;
9323 /* Or a static cast. */
9324 result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
9325 &valid_p, complain);
9326 /* Or a reinterpret_cast. */
9327 if (!valid_p)
9328 result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
9329 &valid_p, complain);
9330 /* The static_cast or reinterpret_cast may be followed by a
9331 const_cast. */
9332 if (valid_p
9333 /* A valid cast may result in errors if, for example, a
9334 conversion to an ambiguous base class is required. */
9335 && !error_operand_p (result))
9337 tree result_type;
9339 maybe_warn_about_useless_cast (loc, type, value, complain);
9340 maybe_warn_about_cast_ignoring_quals (loc, type, complain);
9342 /* Non-class rvalues always have cv-unqualified type. */
9343 if (!CLASS_TYPE_P (type))
9344 type = TYPE_MAIN_VARIANT (type);
9345 result_type = TREE_TYPE (result);
9346 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
9347 result_type = TYPE_MAIN_VARIANT (result_type);
9348 /* If the type of RESULT does not match TYPE, perform a
9349 const_cast to make it match. If the static_cast or
9350 reinterpret_cast succeeded, we will differ by at most
9351 cv-qualification, so the follow-on const_cast is guaranteed
9352 to succeed. */
9353 if (!same_type_p (non_reference (type), non_reference (result_type)))
9355 result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
9356 gcc_assert (valid_p);
9358 return result;
9361 return error_mark_node;
9364 /* Warn when a value is moved to itself with std::move. LHS is the target,
9365 RHS may be the std::move call, and LOC is the location of the whole
9366 assignment. Return true if we warned. */
9368 bool
9369 maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
9371 if (!warn_self_move)
9372 return false;
9374 /* C++98 doesn't know move. */
9375 if (cxx_dialect < cxx11)
9376 return false;
9378 if (processing_template_decl)
9379 return false;
9381 if (!REFERENCE_REF_P (rhs)
9382 || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
9383 return false;
9384 tree fn = TREE_OPERAND (rhs, 0);
9385 if (!is_std_move_p (fn))
9386 return false;
9388 /* Just a little helper to strip * and various NOPs. */
9389 auto extract_op = [] (tree &op) {
9390 STRIP_NOPS (op);
9391 while (INDIRECT_REF_P (op))
9392 op = TREE_OPERAND (op, 0);
9393 op = maybe_undo_parenthesized_ref (op);
9394 STRIP_ANY_LOCATION_WRAPPER (op);
9397 tree arg = CALL_EXPR_ARG (fn, 0);
9398 extract_op (arg);
9399 if (TREE_CODE (arg) == ADDR_EXPR)
9400 arg = TREE_OPERAND (arg, 0);
9401 tree type = TREE_TYPE (lhs);
9402 tree orig_lhs = lhs;
9403 extract_op (lhs);
9404 if (cp_tree_equal (lhs, arg)
9405 /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9406 || (TREE_CODE (lhs) == FIELD_DECL
9407 && TREE_CODE (arg) == COMPONENT_REF
9408 && cp_tree_equal (TREE_OPERAND (arg, 0), current_class_ref)
9409 && TREE_OPERAND (arg, 1) == lhs))
9410 if (warning_at (loc, OPT_Wself_move,
9411 "moving %qE of type %qT to itself", orig_lhs, type))
9412 return true;
9414 return false;
9417 /* For use from the C common bits. */
9418 tree
9419 build_modify_expr (location_t location,
9420 tree lhs, tree /*lhs_origtype*/,
9421 enum tree_code modifycode,
9422 location_t /*rhs_location*/, tree rhs,
9423 tree /*rhs_origtype*/)
9425 return cp_build_modify_expr (location, lhs, modifycode, rhs,
9426 tf_warning_or_error);
9429 /* Build an assignment expression of lvalue LHS from value RHS.
9430 MODIFYCODE is the code for a binary operator that we use
9431 to combine the old value of LHS with RHS to get the new value.
9432 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9434 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9436 tree
9437 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9438 tree rhs, tsubst_flags_t complain)
9440 lhs = mark_lvalue_use_nonread (lhs);
9442 tree result = NULL_TREE;
9443 tree newrhs = rhs;
9444 tree lhstype = TREE_TYPE (lhs);
9445 tree olhs = lhs;
9446 tree olhstype = lhstype;
9447 bool plain_assign = (modifycode == NOP_EXPR);
9448 bool compound_side_effects_p = false;
9449 tree preeval = NULL_TREE;
9451 /* Avoid duplicate error messages from operands that had errors. */
9452 if (error_operand_p (lhs) || error_operand_p (rhs))
9453 return error_mark_node;
9455 while (TREE_CODE (lhs) == COMPOUND_EXPR)
9457 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
9458 compound_side_effects_p = true;
9459 lhs = TREE_OPERAND (lhs, 1);
9462 /* Handle control structure constructs used as "lvalues". Note that we
9463 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9464 switch (TREE_CODE (lhs))
9466 /* Handle --foo = 5; as these are valid constructs in C++. */
9467 case PREDECREMENT_EXPR:
9468 case PREINCREMENT_EXPR:
9469 if (compound_side_effects_p)
9470 newrhs = rhs = stabilize_expr (rhs, &preeval);
9471 lhs = genericize_compound_lvalue (lhs);
9472 maybe_add_compound:
9473 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9474 and looked through the COMPOUND_EXPRs, readd them now around
9475 the resulting lhs. */
9476 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9478 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
9479 tree *ptr = &TREE_OPERAND (lhs, 1);
9480 for (olhs = TREE_OPERAND (olhs, 1);
9481 TREE_CODE (olhs) == COMPOUND_EXPR;
9482 olhs = TREE_OPERAND (olhs, 1))
9484 *ptr = build2 (COMPOUND_EXPR, lhstype,
9485 TREE_OPERAND (olhs, 0), *ptr);
9486 ptr = &TREE_OPERAND (*ptr, 1);
9489 break;
9491 case MODIFY_EXPR:
9492 if (compound_side_effects_p)
9493 newrhs = rhs = stabilize_expr (rhs, &preeval);
9494 lhs = genericize_compound_lvalue (lhs);
9495 goto maybe_add_compound;
9497 case MIN_EXPR:
9498 case MAX_EXPR:
9499 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9500 when neither operand has side-effects. */
9501 if (!lvalue_or_else (lhs, lv_assign, complain))
9502 return error_mark_node;
9504 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
9505 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
9507 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
9508 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
9509 boolean_type_node,
9510 TREE_OPERAND (lhs, 0),
9511 TREE_OPERAND (lhs, 1)),
9512 TREE_OPERAND (lhs, 0),
9513 TREE_OPERAND (lhs, 1));
9514 gcc_fallthrough ();
9516 /* Handle (a ? b : c) used as an "lvalue". */
9517 case COND_EXPR:
9519 /* Produce (a ? (b = rhs) : (c = rhs))
9520 except that the RHS goes through a save-expr
9521 so the code to compute it is only emitted once. */
9522 if (VOID_TYPE_P (TREE_TYPE (rhs)))
9524 if (complain & tf_error)
9525 error_at (cp_expr_loc_or_loc (rhs, loc),
9526 "void value not ignored as it ought to be");
9527 return error_mark_node;
9530 rhs = stabilize_expr (rhs, &preeval);
9532 /* Check this here to avoid odd errors when trying to convert
9533 a throw to the type of the COND_EXPR. */
9534 if (!lvalue_or_else (lhs, lv_assign, complain))
9535 return error_mark_node;
9537 tree op1 = TREE_OPERAND (lhs, 1);
9538 if (TREE_CODE (op1) != THROW_EXPR)
9539 op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
9540 /* When sanitizing undefined behavior, even when rhs doesn't need
9541 stabilization at this point, the sanitization might add extra
9542 SAVE_EXPRs in there and so make sure there is no tree sharing
9543 in the rhs, otherwise those SAVE_EXPRs will have initialization
9544 only in one of the two branches. */
9545 if (sanitize_flags_p (SANITIZE_UNDEFINED
9546 | SANITIZE_UNDEFINED_NONDEFAULT))
9547 rhs = unshare_expr (rhs);
9548 tree op2 = TREE_OPERAND (lhs, 2);
9549 if (TREE_CODE (op2) != THROW_EXPR)
9550 op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
9551 tree cond = build_conditional_expr (input_location,
9552 TREE_OPERAND (lhs, 0), op1, op2,
9553 complain);
9555 if (cond == error_mark_node)
9556 return cond;
9557 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9558 and looked through the COMPOUND_EXPRs, readd them now around
9559 the resulting cond before adding the preevaluated rhs. */
9560 if (TREE_CODE (olhs) == COMPOUND_EXPR)
9562 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9563 TREE_OPERAND (olhs, 0), cond);
9564 tree *ptr = &TREE_OPERAND (cond, 1);
9565 for (olhs = TREE_OPERAND (olhs, 1);
9566 TREE_CODE (olhs) == COMPOUND_EXPR;
9567 olhs = TREE_OPERAND (olhs, 1))
9569 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
9570 TREE_OPERAND (olhs, 0), *ptr);
9571 ptr = &TREE_OPERAND (*ptr, 1);
9574 /* Make sure the code to compute the rhs comes out
9575 before the split. */
9576 result = cond;
9577 goto ret;
9580 default:
9581 lhs = olhs;
9582 break;
9585 if (modifycode == INIT_EXPR)
9587 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9588 /* Do the default thing. */;
9589 else if (TREE_CODE (rhs) == CONSTRUCTOR)
9591 /* Compound literal. */
9592 if (! same_type_p (TREE_TYPE (rhs), lhstype))
9593 /* Call convert to generate an error; see PR 11063. */
9594 rhs = convert (lhstype, rhs);
9595 result = cp_build_init_expr (lhs, rhs);
9596 TREE_SIDE_EFFECTS (result) = 1;
9597 goto ret;
9599 else if (! MAYBE_CLASS_TYPE_P (lhstype))
9600 /* Do the default thing. */;
9601 else
9603 releasing_vec rhs_vec = make_tree_vector_single (rhs);
9604 result = build_special_member_call (lhs, complete_ctor_identifier,
9605 &rhs_vec, lhstype, LOOKUP_NORMAL,
9606 complain);
9607 if (result == NULL_TREE)
9608 return error_mark_node;
9609 goto ret;
9612 else
9614 lhs = require_complete_type (lhs, complain);
9615 if (lhs == error_mark_node)
9616 return error_mark_node;
9618 if (modifycode == NOP_EXPR)
9620 maybe_warn_self_move (loc, lhs, rhs);
9622 if (c_dialect_objc ())
9624 result = objc_maybe_build_modify_expr (lhs, rhs);
9625 if (result)
9626 goto ret;
9629 /* `operator=' is not an inheritable operator. */
9630 if (! MAYBE_CLASS_TYPE_P (lhstype))
9631 /* Do the default thing. */;
9632 else
9634 result = build_new_op (input_location, MODIFY_EXPR,
9635 LOOKUP_NORMAL, lhs, rhs,
9636 make_node (NOP_EXPR), NULL_TREE,
9637 /*overload=*/NULL, complain);
9638 if (result == NULL_TREE)
9639 return error_mark_node;
9640 goto ret;
9642 lhstype = olhstype;
9644 else
9646 tree init = NULL_TREE;
9648 /* A binary op has been requested. Combine the old LHS
9649 value with the RHS producing the value we should actually
9650 store into the LHS. */
9651 gcc_assert (!((TYPE_REF_P (lhstype)
9652 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
9653 || MAYBE_CLASS_TYPE_P (lhstype)));
9655 /* Preevaluate the RHS to make sure its evaluation is complete
9656 before the lvalue-to-rvalue conversion of the LHS:
9658 [expr.ass] With respect to an indeterminately-sequenced
9659 function call, the operation of a compound assignment is a
9660 single evaluation. [ Note: Therefore, a function call shall
9661 not intervene between the lvalue-to-rvalue conversion and the
9662 side effect associated with any single compound assignment
9663 operator. -- end note ] */
9664 lhs = cp_stabilize_reference (lhs);
9665 rhs = decay_conversion (rhs, complain);
9666 if (rhs == error_mark_node)
9667 return error_mark_node;
9668 rhs = stabilize_expr (rhs, &init);
9669 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
9670 if (newrhs == error_mark_node)
9672 if (complain & tf_error)
9673 inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
9674 modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
9675 return error_mark_node;
9678 if (init)
9679 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
9681 /* Now it looks like a plain assignment. */
9682 modifycode = NOP_EXPR;
9683 if (c_dialect_objc ())
9685 result = objc_maybe_build_modify_expr (lhs, newrhs);
9686 if (result)
9687 goto ret;
9690 gcc_assert (!TYPE_REF_P (lhstype));
9691 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
9694 /* The left-hand side must be an lvalue. */
9695 if (!lvalue_or_else (lhs, lv_assign, complain))
9696 return error_mark_node;
9698 /* Warn about modifying something that is `const'. Don't warn if
9699 this is initialization. */
9700 if (modifycode != INIT_EXPR
9701 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
9702 /* Functions are not modifiable, even though they are
9703 lvalues. */
9704 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
9705 /* If it's an aggregate and any field is const, then it is
9706 effectively const. */
9707 || (CLASS_TYPE_P (lhstype)
9708 && C_TYPE_FIELDS_READONLY (lhstype))))
9710 if (complain & tf_error)
9711 cxx_readonly_error (loc, lhs, lv_assign);
9712 return error_mark_node;
9715 /* If storing into a structure or union member, it may have been given a
9716 lowered bitfield type. We need to convert to the declared type first,
9717 so retrieve it now. */
9719 olhstype = unlowered_expr_type (lhs);
9721 /* Convert new value to destination type. */
9723 if (TREE_CODE (lhstype) == ARRAY_TYPE)
9725 int from_array;
9727 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
9729 if (modifycode != INIT_EXPR)
9731 if (complain & tf_error)
9732 error_at (loc,
9733 "assigning to an array from an initializer list");
9734 return error_mark_node;
9736 if (check_array_initializer (lhs, lhstype, newrhs))
9737 return error_mark_node;
9738 newrhs = digest_init (lhstype, newrhs, complain);
9739 if (newrhs == error_mark_node)
9740 return error_mark_node;
9743 /* C++11 8.5/17: "If the destination type is an array of characters,
9744 an array of char16_t, an array of char32_t, or an array of wchar_t,
9745 and the initializer is a string literal...". */
9746 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
9747 == STRING_CST)
9748 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
9749 && modifycode == INIT_EXPR)
9751 newrhs = digest_init (lhstype, newrhs, complain);
9752 if (newrhs == error_mark_node)
9753 return error_mark_node;
9756 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
9757 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
9759 if (complain & tf_error)
9760 error_at (loc, "incompatible types in assignment of %qT to %qT",
9761 TREE_TYPE (rhs), lhstype);
9762 return error_mark_node;
9765 /* Allow array assignment in compiler-generated code. */
9766 else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
9767 /* OK, used by coroutines (co-await-initlist1.C). */;
9768 else if (!current_function_decl
9769 || !DECL_DEFAULTED_FN (current_function_decl))
9771 /* This routine is used for both initialization and assignment.
9772 Make sure the diagnostic message differentiates the context. */
9773 if (complain & tf_error)
9775 if (modifycode == INIT_EXPR)
9776 error_at (loc, "array used as initializer");
9777 else
9778 error_at (loc, "invalid array assignment");
9780 return error_mark_node;
9783 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
9784 ? 1 + (modifycode != INIT_EXPR): 0;
9785 result = build_vec_init (lhs, NULL_TREE, newrhs,
9786 /*explicit_value_init_p=*/false,
9787 from_array, complain);
9788 goto ret;
9791 if (modifycode == INIT_EXPR)
9792 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9793 LOOKUP_ONLYCONVERTING. */
9794 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
9795 ICR_INIT, NULL_TREE, 0,
9796 complain | tf_no_cleanup);
9797 else
9798 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
9799 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
9801 if (!same_type_p (lhstype, olhstype))
9802 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
9804 if (modifycode != INIT_EXPR)
9806 if (TREE_CODE (newrhs) == CALL_EXPR
9807 && TYPE_NEEDS_CONSTRUCTING (lhstype))
9808 newrhs = build_cplus_new (lhstype, newrhs, complain);
9810 /* Can't initialize directly from a TARGET_EXPR, since that would
9811 cause the lhs to be constructed twice, and possibly result in
9812 accidental self-initialization. So we force the TARGET_EXPR to be
9813 expanded without a target. */
9814 if (TREE_CODE (newrhs) == TARGET_EXPR)
9815 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
9816 TREE_OPERAND (newrhs, 0));
9819 if (newrhs == error_mark_node)
9820 return error_mark_node;
9822 if (c_dialect_objc () && flag_objc_gc)
9824 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
9826 if (result)
9827 goto ret;
9830 result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
9831 lhstype, lhs, newrhs);
9832 if (modifycode == INIT_EXPR)
9833 set_target_expr_eliding (newrhs);
9835 TREE_SIDE_EFFECTS (result) = 1;
9836 if (!plain_assign)
9837 suppress_warning (result, OPT_Wparentheses);
9839 ret:
9840 if (preeval)
9841 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
9842 return result;
9845 cp_expr
9846 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
9847 tree rhs, tree lookups, tsubst_flags_t complain)
9849 tree orig_lhs = lhs;
9850 tree orig_rhs = rhs;
9851 tree overload = NULL_TREE;
9853 if (lhs == error_mark_node || rhs == error_mark_node)
9854 return cp_expr (error_mark_node, loc);
9856 tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
9858 if (processing_template_decl)
9860 if (type_dependent_expression_p (lhs)
9861 || type_dependent_expression_p (rhs))
9863 tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
9864 if (modifycode != NOP_EXPR)
9865 TREE_TYPE (rval)
9866 = build_dependent_operator_type (lookups, modifycode, true);
9867 return rval;
9871 tree rval;
9872 if (modifycode == NOP_EXPR)
9873 rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
9874 else
9875 rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
9876 lhs, rhs, op, lookups, &overload, complain);
9877 if (rval == error_mark_node)
9878 return error_mark_node;
9879 if (processing_template_decl)
9881 if (overload != NULL_TREE)
9882 return (build_min_non_dep_op_overload
9883 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
9885 return (build_min_non_dep
9886 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
9888 return rval;
9891 /* Helper function for get_delta_difference which assumes FROM is a base
9892 class of TO. Returns a delta for the conversion of pointer-to-member
9893 of FROM to pointer-to-member of TO. If the conversion is invalid and
9894 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9895 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9896 If C_CAST_P is true, this conversion is taking place as part of a
9897 C-style cast. */
9899 static tree
9900 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
9901 tsubst_flags_t complain)
9903 tree binfo;
9904 base_kind kind;
9906 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
9907 &kind, complain);
9909 if (binfo == error_mark_node)
9911 if (!(complain & tf_error))
9912 return error_mark_node;
9914 inform (input_location, " in pointer to member function conversion");
9915 return size_zero_node;
9917 else if (binfo)
9919 if (kind != bk_via_virtual)
9920 return BINFO_OFFSET (binfo);
9921 else
9922 /* FROM is a virtual base class of TO. Issue an error or warning
9923 depending on whether or not this is a reinterpret cast. */
9925 if (!(complain & tf_error))
9926 return error_mark_node;
9928 error ("pointer to member conversion via virtual base %qT",
9929 BINFO_TYPE (binfo_from_vbase (binfo)));
9931 return size_zero_node;
9934 else
9935 return NULL_TREE;
9938 /* Get difference in deltas for different pointer to member function
9939 types. If the conversion is invalid and tf_error is not set in
9940 COMPLAIN, returns error_mark_node, otherwise returns an integer
9941 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9942 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9943 conversions as well. If C_CAST_P is true this conversion is taking
9944 place as part of a C-style cast.
9946 Note that the naming of FROM and TO is kind of backwards; the return
9947 value is what we add to a TO in order to get a FROM. They are named
9948 this way because we call this function to find out how to convert from
9949 a pointer to member of FROM to a pointer to member of TO. */
9951 static tree
9952 get_delta_difference (tree from, tree to,
9953 bool allow_inverse_p,
9954 bool c_cast_p, tsubst_flags_t complain)
9956 tree result;
9958 if (same_type_ignoring_top_level_qualifiers_p (from, to))
9959 /* Pointer to member of incomplete class is permitted*/
9960 result = size_zero_node;
9961 else
9962 result = get_delta_difference_1 (from, to, c_cast_p, complain);
9964 if (result == error_mark_node)
9965 return error_mark_node;
9967 if (!result)
9969 if (!allow_inverse_p)
9971 if (!(complain & tf_error))
9972 return error_mark_node;
9974 error_not_base_type (from, to);
9975 inform (input_location, " in pointer to member conversion");
9976 result = size_zero_node;
9978 else
9980 result = get_delta_difference_1 (to, from, c_cast_p, complain);
9982 if (result == error_mark_node)
9983 return error_mark_node;
9985 if (result)
9986 result = size_diffop_loc (input_location,
9987 size_zero_node, result);
9988 else
9990 if (!(complain & tf_error))
9991 return error_mark_node;
9993 error_not_base_type (from, to);
9994 inform (input_location, " in pointer to member conversion");
9995 result = size_zero_node;
10000 return convert_to_integer (ptrdiff_type_node, result);
10003 /* Return a constructor for the pointer-to-member-function TYPE using
10004 the other components as specified. */
10006 tree
10007 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
10009 tree u = NULL_TREE;
10010 tree delta_field;
10011 tree pfn_field;
10012 vec<constructor_elt, va_gc> *v;
10014 /* Pull the FIELD_DECLs out of the type. */
10015 pfn_field = TYPE_FIELDS (type);
10016 delta_field = DECL_CHAIN (pfn_field);
10018 /* Make sure DELTA has the type we want. */
10019 delta = convert_and_check (input_location, delta_type_node, delta);
10021 /* Convert to the correct target type if necessary. */
10022 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
10024 /* Finish creating the initializer. */
10025 vec_alloc (v, 2);
10026 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
10027 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
10028 u = build_constructor (type, v);
10029 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
10030 TREE_STATIC (u) = (TREE_CONSTANT (u)
10031 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
10032 != NULL_TREE)
10033 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
10034 != NULL_TREE));
10035 return u;
10038 /* Build a constructor for a pointer to member function. It can be
10039 used to initialize global variables, local variable, or used
10040 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10041 want to be.
10043 If FORCE is nonzero, then force this conversion, even if
10044 we would rather not do it. Usually set when using an explicit
10045 cast. A C-style cast is being processed iff C_CAST_P is true.
10047 Return error_mark_node, if something goes wrong. */
10049 tree
10050 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
10051 tsubst_flags_t complain)
10053 tree fn;
10054 tree pfn_type;
10055 tree to_type;
10057 if (error_operand_p (pfn))
10058 return error_mark_node;
10060 pfn_type = TREE_TYPE (pfn);
10061 to_type = build_ptrmemfunc_type (type);
10063 /* Handle multiple conversions of pointer to member functions. */
10064 if (TYPE_PTRMEMFUNC_P (pfn_type))
10066 tree delta = NULL_TREE;
10067 tree npfn = NULL_TREE;
10068 tree n;
10070 if (!force
10071 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
10072 LOOKUP_NORMAL, complain))
10074 if (complain & tf_error)
10075 error ("invalid conversion to type %qT from type %qT",
10076 to_type, pfn_type);
10077 else
10078 return error_mark_node;
10081 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
10082 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
10083 force,
10084 c_cast_p, complain);
10085 if (n == error_mark_node)
10086 return error_mark_node;
10088 STRIP_ANY_LOCATION_WRAPPER (pfn);
10090 /* We don't have to do any conversion to convert a
10091 pointer-to-member to its own type. But, we don't want to
10092 just return a PTRMEM_CST if there's an explicit cast; that
10093 cast should make the expression an invalid template argument. */
10094 if (TREE_CODE (pfn) != PTRMEM_CST
10095 && same_type_p (to_type, pfn_type))
10096 return pfn;
10098 if (TREE_SIDE_EFFECTS (pfn))
10099 pfn = save_expr (pfn);
10101 /* Obtain the function pointer and the current DELTA. */
10102 if (TREE_CODE (pfn) == PTRMEM_CST)
10103 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
10104 else
10106 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
10107 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
10110 /* Just adjust the DELTA field. */
10111 gcc_assert (same_type_ignoring_top_level_qualifiers_p
10112 (TREE_TYPE (delta), ptrdiff_type_node));
10113 if (!integer_zerop (n))
10115 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
10116 n = cp_build_binary_op (input_location,
10117 LSHIFT_EXPR, n, integer_one_node,
10118 complain);
10119 delta = cp_build_binary_op (input_location,
10120 PLUS_EXPR, delta, n, complain);
10122 return build_ptrmemfunc1 (to_type, delta, npfn);
10125 /* Handle null pointer to member function conversions. */
10126 if (null_ptr_cst_p (pfn))
10128 pfn = cp_build_c_cast (input_location,
10129 TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
10130 pfn, complain);
10131 return build_ptrmemfunc1 (to_type,
10132 integer_zero_node,
10133 pfn);
10136 if (type_unknown_p (pfn))
10137 return instantiate_type (type, pfn, complain);
10139 fn = TREE_OPERAND (pfn, 0);
10140 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
10141 /* In a template, we will have preserved the
10142 OFFSET_REF. */
10143 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
10144 return make_ptrmem_cst (to_type, fn);
10147 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10148 given by CST.
10150 ??? There is no consistency as to the types returned for the above
10151 values. Some code acts as if it were a sizetype and some as if it were
10152 integer_type_node. */
10154 void
10155 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
10157 tree type = TREE_TYPE (cst);
10158 tree fn = PTRMEM_CST_MEMBER (cst);
10159 tree ptr_class, fn_class;
10161 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10163 /* The class that the function belongs to. */
10164 fn_class = DECL_CONTEXT (fn);
10166 /* The class that we're creating a pointer to member of. */
10167 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
10169 /* First, calculate the adjustment to the function's class. */
10170 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
10171 /*c_cast_p=*/0, tf_warning_or_error);
10173 if (!DECL_VIRTUAL_P (fn))
10175 tree t = build_addr_func (fn, tf_warning_or_error);
10176 if (TREE_CODE (t) == ADDR_EXPR)
10177 SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
10178 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
10180 else
10182 /* If we're dealing with a virtual function, we have to adjust 'this'
10183 again, to point to the base which provides the vtable entry for
10184 fn; the call will do the opposite adjustment. */
10185 tree orig_class = DECL_CONTEXT (fn);
10186 tree binfo = binfo_or_else (orig_class, fn_class);
10187 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10188 *delta, BINFO_OFFSET (binfo));
10190 /* We set PFN to the vtable offset at which the function can be
10191 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10192 case delta is shifted left, and then incremented). */
10193 *pfn = DECL_VINDEX (fn);
10194 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
10195 TYPE_SIZE_UNIT (vtable_entry_type));
10197 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
10199 case ptrmemfunc_vbit_in_pfn:
10200 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
10201 integer_one_node);
10202 break;
10204 case ptrmemfunc_vbit_in_delta:
10205 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
10206 *delta, integer_one_node);
10207 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
10208 *delta, integer_one_node);
10209 break;
10211 default:
10212 gcc_unreachable ();
10215 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
10219 /* Return an expression for PFN from the pointer-to-member function
10220 given by T. */
10222 static tree
10223 pfn_from_ptrmemfunc (tree t)
10225 if (TREE_CODE (t) == PTRMEM_CST)
10227 tree delta;
10228 tree pfn;
10230 expand_ptrmemfunc_cst (t, &delta, &pfn);
10231 if (pfn)
10232 return pfn;
10235 return build_ptrmemfunc_access_expr (t, pfn_identifier);
10238 /* Return an expression for DELTA from the pointer-to-member function
10239 given by T. */
10241 static tree
10242 delta_from_ptrmemfunc (tree t)
10244 if (TREE_CODE (t) == PTRMEM_CST)
10246 tree delta;
10247 tree pfn;
10249 expand_ptrmemfunc_cst (t, &delta, &pfn);
10250 if (delta)
10251 return delta;
10254 return build_ptrmemfunc_access_expr (t, delta_identifier);
10257 /* Convert value RHS to type TYPE as preparation for an assignment to
10258 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10259 implicit conversion is. If FNDECL is non-NULL, we are doing the
10260 conversion in order to pass the PARMNUMth argument of FNDECL.
10261 If FNDECL is NULL, we are doing the conversion in function pointer
10262 argument passing, conversion in initialization, etc. */
10264 static tree
10265 convert_for_assignment (tree type, tree rhs,
10266 impl_conv_rhs errtype, tree fndecl, int parmnum,
10267 tsubst_flags_t complain, int flags)
10269 tree rhstype;
10270 enum tree_code coder;
10272 location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
10273 bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
10274 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10275 but preserve location wrappers. */
10276 if (TREE_CODE (rhs) == NON_LVALUE_EXPR
10277 && !location_wrapper_p (rhs))
10278 rhs = TREE_OPERAND (rhs, 0);
10280 /* Handle [dcl.init.list] direct-list-initialization from
10281 single element of enumeration with a fixed underlying type. */
10282 if (is_direct_enum_init (type, rhs))
10284 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
10285 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
10287 warning_sentinel w (warn_useless_cast);
10288 warning_sentinel w2 (warn_ignored_qualifiers);
10289 rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
10291 else
10292 rhs = error_mark_node;
10295 rhstype = TREE_TYPE (rhs);
10296 coder = TREE_CODE (rhstype);
10298 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
10299 && vector_types_convertible_p (type, rhstype, true))
10301 rhs = mark_rvalue_use (rhs);
10302 return convert (type, rhs);
10305 if (rhs == error_mark_node || rhstype == error_mark_node)
10306 return error_mark_node;
10307 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
10308 return error_mark_node;
10310 /* The RHS of an assignment cannot have void type. */
10311 if (coder == VOID_TYPE)
10313 if (complain & tf_error)
10314 error_at (rhs_loc, "void value not ignored as it ought to be");
10315 return error_mark_node;
10318 if (c_dialect_objc ())
10320 int parmno;
10321 tree selector;
10322 tree rname = fndecl;
10324 switch (errtype)
10326 case ICR_ASSIGN:
10327 parmno = -1;
10328 break;
10329 case ICR_INIT:
10330 parmno = -2;
10331 break;
10332 default:
10333 selector = objc_message_selector ();
10334 parmno = parmnum;
10335 if (selector && parmno > 1)
10337 rname = selector;
10338 parmno -= 1;
10342 if (objc_compare_types (type, rhstype, parmno, rname))
10344 rhs = mark_rvalue_use (rhs);
10345 return convert (type, rhs);
10349 /* [expr.ass]
10351 The expression is implicitly converted (clause _conv_) to the
10352 cv-unqualified type of the left operand.
10354 We allow bad conversions here because by the time we get to this point
10355 we are committed to doing the conversion. If we end up doing a bad
10356 conversion, convert_like will complain. */
10357 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
10359 /* When -Wno-pmf-conversions is use, we just silently allow
10360 conversions from pointers-to-members to plain pointers. If
10361 the conversion doesn't work, cp_convert will complain. */
10362 if (!warn_pmf2ptr
10363 && TYPE_PTR_P (type)
10364 && TYPE_PTRMEMFUNC_P (rhstype))
10365 rhs = cp_convert (strip_top_quals (type), rhs, complain);
10366 else
10368 if (complain & tf_error)
10370 /* If the right-hand side has unknown type, then it is an
10371 overloaded function. Call instantiate_type to get error
10372 messages. */
10373 if (rhstype == unknown_type_node)
10375 tree r = instantiate_type (type, rhs, tf_warning_or_error);
10376 /* -fpermissive might allow this; recurse. */
10377 if (!seen_error ())
10378 return convert_for_assignment (type, r, errtype, fndecl,
10379 parmnum, complain, flags);
10381 else if (fndecl)
10382 complain_about_bad_argument (rhs_loc,
10383 rhstype, type,
10384 fndecl, parmnum);
10385 else
10387 range_label_for_type_mismatch label (rhstype, type);
10388 gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
10389 auto_diagnostic_group d;
10391 switch (errtype)
10393 case ICR_DEFAULT_ARGUMENT:
10394 error_at (&richloc,
10395 "cannot convert %qH to %qI in default argument",
10396 rhstype, type);
10397 break;
10398 case ICR_ARGPASS:
10399 error_at (&richloc,
10400 "cannot convert %qH to %qI in argument passing",
10401 rhstype, type);
10402 break;
10403 case ICR_CONVERTING:
10404 error_at (&richloc, "cannot convert %qH to %qI",
10405 rhstype, type);
10406 break;
10407 case ICR_INIT:
10408 error_at (&richloc,
10409 "cannot convert %qH to %qI in initialization",
10410 rhstype, type);
10411 break;
10412 case ICR_RETURN:
10413 error_at (&richloc, "cannot convert %qH to %qI in return",
10414 rhstype, type);
10415 break;
10416 case ICR_ASSIGN:
10417 error_at (&richloc,
10418 "cannot convert %qH to %qI in assignment",
10419 rhstype, type);
10420 break;
10421 default:
10422 gcc_unreachable();
10426 /* See if we can be more helpful. */
10427 maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
10429 if (TYPE_PTR_P (rhstype)
10430 && TYPE_PTR_P (type)
10431 && CLASS_TYPE_P (TREE_TYPE (rhstype))
10432 && CLASS_TYPE_P (TREE_TYPE (type))
10433 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
10434 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10435 (TREE_TYPE (rhstype))),
10436 "class type %qT is incomplete", TREE_TYPE (rhstype));
10438 return error_mark_node;
10441 if (warn_suggest_attribute_format)
10443 const enum tree_code codel = TREE_CODE (type);
10444 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
10445 && coder == codel
10446 && check_missing_format_attribute (type, rhstype)
10447 && (complain & tf_warning))
10448 switch (errtype)
10450 case ICR_ARGPASS:
10451 case ICR_DEFAULT_ARGUMENT:
10452 if (fndecl)
10453 warning (OPT_Wsuggest_attribute_format,
10454 "parameter %qP of %qD might be a candidate "
10455 "for a format attribute", parmnum, fndecl);
10456 else
10457 warning (OPT_Wsuggest_attribute_format,
10458 "parameter might be a candidate "
10459 "for a format attribute");
10460 break;
10461 case ICR_CONVERTING:
10462 warning (OPT_Wsuggest_attribute_format,
10463 "target of conversion might be a candidate "
10464 "for a format attribute");
10465 break;
10466 case ICR_INIT:
10467 warning (OPT_Wsuggest_attribute_format,
10468 "target of initialization might be a candidate "
10469 "for a format attribute");
10470 break;
10471 case ICR_RETURN:
10472 warning (OPT_Wsuggest_attribute_format,
10473 "return type might be a candidate "
10474 "for a format attribute");
10475 break;
10476 case ICR_ASSIGN:
10477 warning (OPT_Wsuggest_attribute_format,
10478 "left-hand side of assignment might be a candidate "
10479 "for a format attribute");
10480 break;
10481 default:
10482 gcc_unreachable();
10486 if (TREE_CODE (type) == BOOLEAN_TYPE)
10487 maybe_warn_unparenthesized_assignment (rhs, /*nested_p=*/true, complain);
10489 if (complain & tf_warning)
10490 warn_for_address_of_packed_member (type, rhs);
10492 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
10493 complain, flags);
10496 /* Convert RHS to be of type TYPE.
10497 If EXP is nonzero, it is the target of the initialization.
10498 ERRTYPE indicates what kind of error the implicit conversion is.
10500 Two major differences between the behavior of
10501 `convert_for_assignment' and `convert_for_initialization'
10502 are that references are bashed in the former, while
10503 copied in the latter, and aggregates are assigned in
10504 the former (operator=) while initialized in the
10505 latter (X(X&)).
10507 If using constructor make sure no conversion operator exists, if one does
10508 exist, an ambiguity exists. */
10510 tree
10511 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
10512 impl_conv_rhs errtype, tree fndecl, int parmnum,
10513 tsubst_flags_t complain)
10515 enum tree_code codel = TREE_CODE (type);
10516 tree rhstype;
10517 enum tree_code coder;
10519 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10520 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10521 if (TREE_CODE (rhs) == NOP_EXPR
10522 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
10523 && codel != REFERENCE_TYPE)
10524 rhs = TREE_OPERAND (rhs, 0);
10526 if (type == error_mark_node
10527 || rhs == error_mark_node
10528 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
10529 return error_mark_node;
10531 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
10533 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
10534 && TREE_CODE (type) != ARRAY_TYPE
10535 && (!TYPE_REF_P (type)
10536 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
10537 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
10538 && !TYPE_REFFN_P (type))
10539 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
10540 rhs = decay_conversion (rhs, complain);
10542 rhstype = TREE_TYPE (rhs);
10543 coder = TREE_CODE (rhstype);
10545 if (coder == ERROR_MARK)
10546 return error_mark_node;
10548 /* We accept references to incomplete types, so we can
10549 return here before checking if RHS is of complete type. */
10551 if (codel == REFERENCE_TYPE)
10553 auto_diagnostic_group d;
10554 /* This should eventually happen in convert_arguments. */
10555 int savew = 0, savee = 0;
10557 if (fndecl)
10558 savew = warningcount + werrorcount, savee = errorcount;
10559 rhs = initialize_reference (type, rhs, flags, complain);
10561 if (fndecl
10562 && (warningcount + werrorcount > savew || errorcount > savee))
10563 inform (get_fndecl_argument_location (fndecl, parmnum),
10564 "in passing argument %P of %qD", parmnum, fndecl);
10565 return rhs;
10568 if (exp != 0)
10569 exp = require_complete_type (exp, complain);
10570 if (exp == error_mark_node)
10571 return error_mark_node;
10573 type = complete_type (type);
10575 if (DIRECT_INIT_EXPR_P (type, rhs))
10576 /* Don't try to do copy-initialization if we already have
10577 direct-initialization. */
10578 return rhs;
10580 if (MAYBE_CLASS_TYPE_P (type))
10581 return perform_implicit_conversion_flags (type, rhs, complain, flags);
10583 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
10584 complain, flags);
10587 /* If RETVAL is the address of, or a reference to, a local variable or
10588 temporary give an appropriate warning and return true. */
10590 static bool
10591 maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
10593 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
10594 tree whats_returned = fold_for_warn (retval);
10595 if (!loc)
10596 loc = cp_expr_loc_or_input_loc (retval);
10598 for (;;)
10600 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
10601 whats_returned = TREE_OPERAND (whats_returned, 1);
10602 else if (CONVERT_EXPR_P (whats_returned)
10603 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
10604 whats_returned = TREE_OPERAND (whats_returned, 0);
10605 else
10606 break;
10609 if (TREE_CODE (whats_returned) == TARGET_EXPR
10610 && is_std_init_list (TREE_TYPE (whats_returned)))
10612 tree init = TARGET_EXPR_INITIAL (whats_returned);
10613 if (TREE_CODE (init) == CONSTRUCTOR)
10614 /* Pull out the array address. */
10615 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
10616 else if (TREE_CODE (init) == INDIRECT_REF)
10617 /* The source of a trivial copy looks like *(T*)&var. */
10618 whats_returned = TREE_OPERAND (init, 0);
10619 else
10620 return false;
10621 STRIP_NOPS (whats_returned);
10624 /* As a special case, we handle a call to std::move or std::forward. */
10625 if (TREE_CODE (whats_returned) == CALL_EXPR
10626 && (is_std_move_p (whats_returned)
10627 || is_std_forward_p (whats_returned)))
10629 tree arg = CALL_EXPR_ARG (whats_returned, 0);
10630 return maybe_warn_about_returning_address_of_local (arg, loc);
10633 if (TREE_CODE (whats_returned) != ADDR_EXPR)
10634 return false;
10635 whats_returned = TREE_OPERAND (whats_returned, 0);
10637 while (TREE_CODE (whats_returned) == COMPONENT_REF
10638 || TREE_CODE (whats_returned) == ARRAY_REF)
10639 whats_returned = TREE_OPERAND (whats_returned, 0);
10641 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
10642 || TREE_CODE (whats_returned) == TARGET_EXPR)
10644 if (TYPE_REF_P (valtype))
10645 /* P2748 made this an error in C++26. */
10646 emit_diagnostic (cxx_dialect >= cxx26 ? DK_PERMERROR : DK_WARNING,
10647 loc, OPT_Wreturn_local_addr,
10648 "returning reference to temporary");
10649 else if (TYPE_PTR_P (valtype))
10650 warning_at (loc, OPT_Wreturn_local_addr,
10651 "returning pointer to temporary");
10652 else if (is_std_init_list (valtype))
10653 warning_at (loc, OPT_Winit_list_lifetime,
10654 "returning temporary %<initializer_list%> does not extend "
10655 "the lifetime of the underlying array");
10656 return true;
10659 STRIP_ANY_LOCATION_WRAPPER (whats_returned);
10661 if (DECL_P (whats_returned)
10662 && DECL_NAME (whats_returned)
10663 && DECL_FUNCTION_SCOPE_P (whats_returned)
10664 && !is_capture_proxy (whats_returned)
10665 && !(TREE_STATIC (whats_returned)
10666 || TREE_PUBLIC (whats_returned)))
10668 if (DECL_DECOMPOSITION_P (whats_returned)
10669 && DECL_DECOMP_BASE (whats_returned)
10670 && DECL_HAS_VALUE_EXPR_P (whats_returned))
10672 /* When returning address of a structured binding, if the structured
10673 binding is not a reference, continue normally, if it is a
10674 reference, recurse on the initializer of the structured
10675 binding. */
10676 tree base = DECL_DECOMP_BASE (whats_returned);
10677 if (TYPE_REF_P (TREE_TYPE (base)))
10679 if (tree init = DECL_INITIAL (base))
10680 return maybe_warn_about_returning_address_of_local (init, loc);
10681 else
10682 return false;
10685 bool w = false;
10686 auto_diagnostic_group d;
10687 if (TYPE_REF_P (valtype))
10688 w = warning_at (loc, OPT_Wreturn_local_addr,
10689 "reference to local variable %qD returned",
10690 whats_returned);
10691 else if (is_std_init_list (valtype))
10692 w = warning_at (loc, OPT_Winit_list_lifetime,
10693 "returning local %<initializer_list%> variable %qD "
10694 "does not extend the lifetime of the underlying array",
10695 whats_returned);
10696 else if (POINTER_TYPE_P (valtype)
10697 && TREE_CODE (whats_returned) == LABEL_DECL)
10698 w = warning_at (loc, OPT_Wreturn_local_addr,
10699 "address of label %qD returned",
10700 whats_returned);
10701 else if (POINTER_TYPE_P (valtype))
10702 w = warning_at (loc, OPT_Wreturn_local_addr,
10703 "address of local variable %qD returned",
10704 whats_returned);
10705 if (w)
10706 inform (DECL_SOURCE_LOCATION (whats_returned),
10707 "declared here");
10708 return true;
10711 return false;
10714 /* Returns true if DECL is in the std namespace. */
10716 bool
10717 decl_in_std_namespace_p (tree decl)
10719 while (decl)
10721 decl = decl_namespace_context (decl);
10722 if (DECL_NAMESPACE_STD_P (decl))
10723 return true;
10724 /* Allow inline namespaces inside of std namespace, e.g. with
10725 --enable-symvers=gnu-versioned-namespace std::forward would be
10726 actually std::_8::forward. */
10727 if (!DECL_NAMESPACE_INLINE_P (decl))
10728 return false;
10729 decl = CP_DECL_CONTEXT (decl);
10731 return false;
10734 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10736 static bool
10737 is_std_forward_p (tree fn)
10739 /* std::forward only takes one argument. */
10740 if (call_expr_nargs (fn) != 1)
10741 return false;
10743 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10744 if (!decl_in_std_namespace_p (fndecl))
10745 return false;
10747 tree name = DECL_NAME (fndecl);
10748 return name && id_equal (name, "forward");
10751 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10753 static bool
10754 is_std_move_p (tree fn)
10756 /* std::move only takes one argument. */
10757 if (call_expr_nargs (fn) != 1)
10758 return false;
10760 tree fndecl = cp_get_callee_fndecl_nofold (fn);
10761 if (!decl_in_std_namespace_p (fndecl))
10762 return false;
10764 tree name = DECL_NAME (fndecl);
10765 return name && id_equal (name, "move");
10768 /* Returns true if RETVAL is a good candidate for the NRVO as per
10769 [class.copy.elision]. FUNCTYPE is the type the function is declared
10770 to return. */
10772 static bool
10773 can_do_nrvo_p (tree retval, tree functype)
10775 if (functype == error_mark_node)
10776 return false;
10777 if (retval)
10778 STRIP_ANY_LOCATION_WRAPPER (retval);
10779 tree result = DECL_RESULT (current_function_decl);
10780 return (retval != NULL_TREE
10781 && !processing_template_decl
10782 /* Must be a local, automatic variable. */
10783 && VAR_P (retval)
10784 && DECL_CONTEXT (retval) == current_function_decl
10785 && !TREE_STATIC (retval)
10786 /* And not a lambda or anonymous union proxy. */
10787 && !DECL_HAS_VALUE_EXPR_P (retval)
10788 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
10789 /* The cv-unqualified type of the returned value must be the
10790 same as the cv-unqualified return type of the
10791 function. */
10792 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10793 TYPE_MAIN_VARIANT (functype))
10794 /* And the returned value must be non-volatile. */
10795 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10798 /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10799 would otherwise return in memory. */
10801 static bool
10802 want_nrvo_p (tree retval, tree functype)
10804 return (can_do_nrvo_p (retval, functype)
10805 && aggregate_value_p (functype, current_function_decl));
10808 /* Like can_do_nrvo_p, but we check if we're trying to move a class
10809 prvalue. */
10811 static bool
10812 can_elide_copy_prvalue_p (tree retval, tree functype)
10814 if (functype == error_mark_node)
10815 return false;
10816 if (retval)
10817 STRIP_ANY_LOCATION_WRAPPER (retval);
10818 return (retval != NULL_TREE
10819 && !glvalue_p (retval)
10820 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
10821 TYPE_MAIN_VARIANT (functype))
10822 && !TYPE_VOLATILE (TREE_TYPE (retval)));
10825 /* If we should treat RETVAL, an expression being returned, as if it were
10826 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10827 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10828 context (rather than throw). */
10830 tree
10831 treat_lvalue_as_rvalue_p (tree expr, bool return_p)
10833 if (cxx_dialect == cxx98)
10834 return NULL_TREE;
10836 tree retval = expr;
10837 STRIP_ANY_LOCATION_WRAPPER (retval);
10838 if (REFERENCE_REF_P (retval))
10839 retval = TREE_OPERAND (retval, 0);
10841 /* An implicitly movable entity is a variable of automatic storage duration
10842 that is either a non-volatile object or (C++20) an rvalue reference to a
10843 non-volatile object type. */
10844 if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
10845 || TREE_CODE (retval) == PARM_DECL)
10846 && !TREE_STATIC (retval)
10847 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
10848 && (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
10849 || (cxx_dialect >= cxx20
10850 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
10851 return NULL_TREE;
10853 /* If the expression in a return or co_return statement is a (possibly
10854 parenthesized) id-expression that names an implicitly movable entity
10855 declared in the body or parameter-declaration-clause of the innermost
10856 enclosing function or lambda-expression, */
10857 if (return_p)
10859 if (DECL_CONTEXT (retval) != current_function_decl)
10860 return NULL_TREE;
10861 expr = move (expr);
10862 if (expr == error_mark_node)
10863 return NULL_TREE;
10864 return set_implicit_rvalue_p (expr);
10867 /* if the id-expression (possibly parenthesized) is the operand of
10868 a throw-expression, and names an implicitly movable entity that belongs
10869 to a scope that does not contain the compound-statement of the innermost
10870 lambda-expression, try-block, or function-try-block (if any) whose
10871 compound-statement or ctor-initializer contains the throw-expression. */
10873 /* C++20 added move on throw of parms. */
10874 if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
10875 return NULL_TREE;
10877 /* We don't check for lambda-expression here, because we should not get past
10878 the DECL_HAS_VALUE_EXPR_P check above. */
10879 for (cp_binding_level *b = current_binding_level;
10880 b->kind != sk_namespace; b = b->level_chain)
10882 for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
10883 if (decl == retval)
10884 return set_implicit_rvalue_p (move (expr));
10885 if (b->kind == sk_try)
10886 return NULL_TREE;
10889 return set_implicit_rvalue_p (move (expr));
10892 /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10893 is true). EXPR is the std::move expression; TYPE is the type of the object
10894 being initialized. */
10896 void
10897 maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
10899 if (!(warn_pessimizing_move || warn_redundant_move))
10900 return;
10902 const location_t loc = cp_expr_loc_or_input_loc (expr);
10904 /* C++98 doesn't know move. */
10905 if (cxx_dialect < cxx11)
10906 return;
10908 /* Wait until instantiation time, since we can't gauge if we should do
10909 the NRVO until then. */
10910 if (processing_template_decl)
10911 return;
10913 /* This is only interesting for class types. */
10914 if (!CLASS_TYPE_P (type))
10915 return;
10917 bool wrapped_p = false;
10918 /* A a = std::move (A()); */
10919 if (TREE_CODE (expr) == TREE_LIST)
10921 if (list_length (expr) == 1)
10923 expr = TREE_VALUE (expr);
10924 wrapped_p = true;
10926 else
10927 return;
10929 /* A a = {std::move (A())};
10930 A a{std::move (A())}; */
10931 else if (TREE_CODE (expr) == CONSTRUCTOR)
10933 if (CONSTRUCTOR_NELTS (expr) == 1)
10935 expr = CONSTRUCTOR_ELT (expr, 0)->value;
10936 wrapped_p = true;
10938 else
10939 return;
10942 /* First, check if this is a call to std::move. */
10943 if (!REFERENCE_REF_P (expr)
10944 || TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
10945 return;
10946 tree fn = TREE_OPERAND (expr, 0);
10947 if (!is_std_move_p (fn))
10948 return;
10949 tree arg = CALL_EXPR_ARG (fn, 0);
10950 if (TREE_CODE (arg) != NOP_EXPR)
10951 return;
10952 /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
10953 and implicitly-movable warnings. */
10954 if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
10956 arg = TREE_OPERAND (arg, 0);
10957 arg = TREE_OPERAND (arg, 0);
10958 arg = convert_from_reference (arg);
10959 if (can_elide_copy_prvalue_p (arg, type))
10961 auto_diagnostic_group d;
10962 if (warning_at (loc, OPT_Wpessimizing_move,
10963 "moving a temporary object prevents copy elision"))
10964 inform (loc, "remove %<std::move%> call");
10966 /* The rest of the warnings is only relevant for when we are returning
10967 from a function. */
10968 if (!return_p)
10969 return;
10971 tree moved;
10972 /* Warn if we could do copy elision were it not for the move. */
10973 if (can_do_nrvo_p (arg, type))
10975 auto_diagnostic_group d;
10976 if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
10977 && warning_at (loc, OPT_Wpessimizing_move,
10978 "moving a local object in a return statement "
10979 "prevents copy elision"))
10980 inform (loc, "remove %<std::move%> call");
10982 /* Warn if the move is redundant. It is redundant when we would
10983 do maybe-rvalue overload resolution even without std::move. */
10984 else if (warn_redundant_move
10985 /* This doesn't apply for return {std::move (t)};. */
10986 && !wrapped_p
10987 && !warning_suppressed_p (expr, OPT_Wredundant_move)
10988 && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
10990 /* Make sure that overload resolution would actually succeed
10991 if we removed the std::move call. */
10992 tree t = convert_for_initialization (NULL_TREE, type,
10993 moved,
10994 (LOOKUP_NORMAL
10995 | LOOKUP_ONLYCONVERTING),
10996 ICR_RETURN, NULL_TREE, 0,
10997 tf_none);
10998 /* If this worked, implicit rvalue would work, so the call to
10999 std::move is redundant. */
11000 if (t != error_mark_node)
11002 auto_diagnostic_group d;
11003 if (warning_at (loc, OPT_Wredundant_move,
11004 "redundant move in return statement"))
11005 inform (loc, "remove %<std::move%> call");
11009 /* Also try to warn about redundant std::move in code such as
11010 T f (const T& t)
11012 return std::move(t);
11014 for which EXPR will be something like
11015 *std::move<const T&> ((const struct T &) (const struct T *) t)
11016 and where the std::move does nothing if T does not have a T(const T&&)
11017 constructor, because the argument is const. It will not use T(T&&)
11018 because that would mean losing the const. */
11019 else if (warn_redundant_move
11020 && !warning_suppressed_p (expr, OPT_Wredundant_move)
11021 && TYPE_REF_P (TREE_TYPE (arg))
11022 && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
11024 tree rtype = TREE_TYPE (TREE_TYPE (arg));
11025 if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
11026 return;
11027 /* Check for the unlikely case there's T(const T&&) (we don't care if
11028 it's deleted). */
11029 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
11030 if (move_fn_p (fn))
11032 tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
11033 if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
11034 return;
11036 auto_diagnostic_group d;
11037 if (return_p
11038 ? warning_at (loc, OPT_Wredundant_move,
11039 "redundant move in return statement")
11040 : warning_at (loc, OPT_Wredundant_move,
11041 "redundant move in initialization"))
11042 inform (loc, "remove %<std::move%> call");
11046 /* Check that returning RETVAL from the current function is valid.
11047 Return an expression explicitly showing all conversions required to
11048 change RETVAL into the function return type, and to assign it to
11049 the DECL_RESULT for the function. Set *NO_WARNING to true if
11050 code reaches end of non-void function warning shouldn't be issued
11051 on this RETURN_EXPR. Set *DANGLING to true if code returns the
11052 address of a local variable. */
11054 tree
11055 check_return_expr (tree retval, bool *no_warning, bool *dangling)
11057 tree result;
11058 /* The type actually returned by the function. */
11059 tree valtype;
11060 /* The type the function is declared to return, or void if
11061 the declared type is incomplete. */
11062 tree functype;
11063 int fn_returns_value_p;
11064 location_t loc = cp_expr_loc_or_input_loc (retval);
11066 *no_warning = false;
11067 *dangling = false;
11069 /* A `volatile' function is one that isn't supposed to return, ever.
11070 (This is a G++ extension, used to get better code for functions
11071 that call the `volatile' function.) */
11072 if (TREE_THIS_VOLATILE (current_function_decl))
11073 warning (0, "function declared %<noreturn%> has a %<return%> statement");
11075 /* Check for various simple errors. */
11076 if (DECL_DESTRUCTOR_P (current_function_decl))
11078 if (retval)
11079 error_at (loc, "returning a value from a destructor");
11081 if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11082 retval = current_class_ptr;
11083 else
11084 return NULL_TREE;
11086 else if (DECL_CONSTRUCTOR_P (current_function_decl))
11088 if (in_function_try_handler)
11089 /* If a return statement appears in a handler of the
11090 function-try-block of a constructor, the program is ill-formed. */
11091 error ("cannot return from a handler of a function-try-block of a constructor");
11092 else if (retval)
11093 /* You can't return a value from a constructor. */
11094 error_at (loc, "returning a value from a constructor");
11096 if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
11097 retval = current_class_ptr;
11098 else
11099 return NULL_TREE;
11102 const tree saved_retval = retval;
11104 if (processing_template_decl)
11106 current_function_returns_value = 1;
11108 if (check_for_bare_parameter_packs (retval))
11109 return error_mark_node;
11111 /* If one of the types might be void, we can't tell whether we're
11112 returning a value. */
11113 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
11114 && !FNDECL_USED_AUTO (current_function_decl))
11115 || (retval != NULL_TREE
11116 && (TREE_TYPE (retval) == NULL_TREE
11117 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
11118 goto dependent;
11121 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
11123 /* Deduce auto return type from a return statement. */
11124 if (FNDECL_USED_AUTO (current_function_decl))
11126 tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
11127 tree auto_node;
11128 tree type;
11130 if (!retval && !is_auto (pattern))
11132 /* Give a helpful error message. */
11133 error ("return-statement with no value, in function returning %qT",
11134 pattern);
11135 inform (input_location, "only plain %<auto%> return type can be "
11136 "deduced to %<void%>");
11137 type = error_mark_node;
11139 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
11141 error ("returning initializer list");
11142 type = error_mark_node;
11144 else
11146 if (!retval)
11147 retval = void_node;
11148 auto_node = type_uses_auto (pattern);
11149 type = do_auto_deduction (pattern, retval, auto_node,
11150 tf_warning_or_error, adc_return_type);
11153 if (type == error_mark_node)
11154 /* Leave it. */;
11155 else if (functype == pattern)
11156 apply_deduced_return_type (current_function_decl, type);
11157 else if (!same_type_p (type, functype))
11159 if (LAMBDA_FUNCTION_P (current_function_decl))
11160 error_at (loc, "inconsistent types %qT and %qT deduced for "
11161 "lambda return type", functype, type);
11162 else
11163 error_at (loc, "inconsistent deduction for auto return type: "
11164 "%qT and then %qT", functype, type);
11166 functype = type;
11169 result = DECL_RESULT (current_function_decl);
11170 valtype = TREE_TYPE (result);
11171 gcc_assert (valtype != NULL_TREE);
11172 fn_returns_value_p = !VOID_TYPE_P (valtype);
11174 /* Check for a return statement with no return value in a function
11175 that's supposed to return a value. */
11176 if (!retval && fn_returns_value_p)
11178 if (functype != error_mark_node)
11179 permerror (input_location, "return-statement with no value, in "
11180 "function returning %qT", valtype);
11181 /* Remember that this function did return. */
11182 current_function_returns_value = 1;
11183 /* And signal caller that TREE_NO_WARNING should be set on the
11184 RETURN_EXPR to avoid control reaches end of non-void function
11185 warnings in tree-cfg.cc. */
11186 *no_warning = true;
11188 /* Check for a return statement with a value in a function that
11189 isn't supposed to return a value. */
11190 else if (retval && !fn_returns_value_p)
11192 if (VOID_TYPE_P (TREE_TYPE (retval)))
11193 /* You can return a `void' value from a function of `void'
11194 type. In that case, we have to evaluate the expression for
11195 its side-effects. */
11196 finish_expr_stmt (retval);
11197 else if (retval != error_mark_node)
11198 permerror (loc, "return-statement with a value, in function "
11199 "returning %qT", valtype);
11200 current_function_returns_null = 1;
11202 /* There's really no value to return, after all. */
11203 return NULL_TREE;
11205 else if (!retval)
11206 /* Remember that this function can sometimes return without a
11207 value. */
11208 current_function_returns_null = 1;
11209 else
11210 /* Remember that this function did return a value. */
11211 current_function_returns_value = 1;
11213 /* Check for erroneous operands -- but after giving ourselves a
11214 chance to provide an error about returning a value from a void
11215 function. */
11216 if (error_operand_p (retval))
11218 current_function_return_value = error_mark_node;
11219 return error_mark_node;
11222 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11223 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
11224 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
11225 && ! flag_check_new
11226 && retval && null_ptr_cst_p (retval))
11227 warning (0, "%<operator new%> must not return NULL unless it is "
11228 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11230 /* Effective C++ rule 15. See also start_function. */
11231 if (warn_ecpp
11232 && DECL_NAME (current_function_decl) == assign_op_identifier
11233 && !type_dependent_expression_p (retval))
11235 bool warn = true;
11237 /* The function return type must be a reference to the current
11238 class. */
11239 if (TYPE_REF_P (valtype)
11240 && same_type_ignoring_top_level_qualifiers_p
11241 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
11243 /* Returning '*this' is obviously OK. */
11244 if (retval == current_class_ref)
11245 warn = false;
11246 /* If we are calling a function whose return type is the same of
11247 the current class reference, it is ok. */
11248 else if (INDIRECT_REF_P (retval)
11249 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
11250 warn = false;
11253 if (warn)
11254 warning_at (loc, OPT_Weffc__,
11255 "%<operator=%> should return a reference to %<*this%>");
11258 if (dependent_type_p (functype)
11259 || type_dependent_expression_p (retval))
11261 dependent:
11262 /* We should not have changed the return value. */
11263 gcc_assert (retval == saved_retval);
11264 /* We don't know if this is an lvalue or rvalue use, but
11265 either way we can mark it as read. */
11266 mark_exp_read (retval);
11267 return retval;
11270 /* The fabled Named Return Value optimization, as per [class.copy]/15:
11272 [...] For a function with a class return type, if the expression
11273 in the return statement is the name of a local object, and the cv-
11274 unqualified type of the local object is the same as the function
11275 return type, an implementation is permitted to omit creating the tem-
11276 porary object to hold the function return value [...]
11278 So, if this is a value-returning function that always returns the same
11279 local variable, remember it.
11281 We choose the first suitable variable even if the function sometimes
11282 returns something else, but only if the variable is out of scope at the
11283 other return sites, or else we run the risk of clobbering the variable we
11284 chose if the other returned expression uses the chosen variable somehow.
11286 We don't currently do this if the first return is a non-variable, as it
11287 would be complicated to determine whether an NRV selected later was in
11288 scope at the point of the earlier return. We also don't currently support
11289 multiple variables with non-overlapping scopes (53637).
11291 See finish_function and finalize_nrv for the rest of this optimization. */
11292 tree bare_retval = NULL_TREE;
11293 if (retval)
11295 retval = maybe_undo_parenthesized_ref (retval);
11296 bare_retval = tree_strip_any_location_wrapper (retval);
11299 bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
11300 if (fn_returns_value_p && flag_elide_constructors
11301 && current_function_return_value != bare_retval)
11303 if (named_return_value_okay_p
11304 && current_function_return_value == NULL_TREE)
11305 current_function_return_value = bare_retval;
11306 else if (current_function_return_value
11307 && VAR_P (current_function_return_value)
11308 && DECL_NAME (current_function_return_value)
11309 && !decl_in_scope_p (current_function_return_value))
11311 /* The earlier NRV is out of scope at this point, so it's safe to
11312 leave it alone; the current return can't refer to it. */;
11313 if (named_return_value_okay_p
11314 && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11316 warning (OPT_Wnrvo, "not eliding copy on return from %qD",
11317 bare_retval);
11318 suppress_warning (current_function_decl, OPT_Wnrvo);
11321 else
11323 if ((named_return_value_okay_p
11324 || (current_function_return_value
11325 && current_function_return_value != error_mark_node))
11326 && !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
11328 warning (OPT_Wnrvo, "not eliding copy on return in %qD",
11329 current_function_decl);
11330 suppress_warning (current_function_decl, OPT_Wnrvo);
11332 current_function_return_value = error_mark_node;
11336 /* We don't need to do any conversions when there's nothing being
11337 returned. */
11338 if (!retval)
11339 return NULL_TREE;
11341 if (!named_return_value_okay_p)
11342 maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
11344 /* Do any required conversions. */
11345 if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
11346 /* No conversions are required. */
11348 else
11350 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
11352 /* The functype's return type will have been set to void, if it
11353 was an incomplete type. Just treat this as 'return;' */
11354 if (VOID_TYPE_P (functype))
11355 return error_mark_node;
11357 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11358 treated as an rvalue for the purposes of overload resolution to
11359 favor move constructors over copy constructors.
11361 Note that these conditions are similar to, but not as strict as,
11362 the conditions for the named return value optimization. */
11363 bool converted = false;
11364 tree moved;
11365 /* Until C++23, this was only interesting for class type, but in C++23,
11366 we should do the below when we're converting rom/to a class/reference
11367 (a non-scalar type). */
11368 if ((cxx_dialect < cxx23
11369 ? CLASS_TYPE_P (functype)
11370 : !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
11371 && (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
11372 /* In C++20 and earlier we treat the return value as an rvalue
11373 that can bind to lvalue refs. In C++23, such an expression is just
11374 an xvalue (see reference_binding). */
11375 retval = moved;
11377 /* The call in a (lambda) thunk needs no conversions. */
11378 if (TREE_CODE (retval) == CALL_EXPR
11379 && call_from_lambda_thunk_p (retval))
11380 converted = true;
11382 /* First convert the value to the function's return type, then
11383 to the type of return value's location to handle the
11384 case that functype is smaller than the valtype. */
11385 if (!converted)
11386 retval = convert_for_initialization
11387 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
11388 tf_warning_or_error);
11389 retval = convert (valtype, retval);
11391 /* If the conversion failed, treat this just like `return;'. */
11392 if (retval == error_mark_node)
11393 return retval;
11394 /* We can't initialize a register from a AGGR_INIT_EXPR. */
11395 else if (! cfun->returns_struct
11396 && TREE_CODE (retval) == TARGET_EXPR
11397 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
11398 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
11399 TREE_OPERAND (retval, 0));
11400 else if (!processing_template_decl
11401 && maybe_warn_about_returning_address_of_local (retval, loc)
11402 && INDIRECT_TYPE_P (valtype))
11403 *dangling = true;
11406 /* A naive attempt to reduce the number of -Wdangling-reference false
11407 positives: if we know that this function can return a variable with
11408 static storage duration rather than one of its parameters, suppress
11409 the warning. */
11410 if (warn_dangling_reference
11411 && TYPE_REF_P (functype)
11412 && bare_retval
11413 && VAR_P (bare_retval)
11414 && TREE_STATIC (bare_retval))
11415 suppress_warning (current_function_decl, OPT_Wdangling_reference);
11417 if (processing_template_decl)
11418 return saved_retval;
11420 /* Actually copy the value returned into the appropriate location. */
11421 if (retval && retval != result)
11423 /* If there's a postcondition for a scalar return value, wrap
11424 retval in a call to the postcondition function. */
11425 if (tree post = apply_postcondition_to_return (retval))
11426 retval = post;
11427 retval = cp_build_init_expr (result, retval);
11430 if (current_function_return_value == bare_retval)
11431 INIT_EXPR_NRV_P (retval) = true;
11433 if (tree set = maybe_set_retval_sentinel ())
11434 retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
11436 /* If there's a postcondition for an aggregate return value, call the
11437 postcondition function after the return object is initialized. */
11438 if (tree post = apply_postcondition_to_return (result))
11439 retval = build2 (COMPOUND_EXPR, void_type_node, retval, post);
11441 return retval;
11445 /* Returns nonzero if the pointer-type FROM can be converted to the
11446 pointer-type TO via a qualification conversion. If CONSTP is -1,
11447 then we return nonzero if the pointers are similar, and the
11448 cv-qualification signature of FROM is a proper subset of that of TO.
11450 If CONSTP is positive, then all outer pointers have been
11451 const-qualified. */
11453 static bool
11454 comp_ptr_ttypes_real (tree to, tree from, int constp)
11456 bool to_more_cv_qualified = false;
11457 bool is_opaque_pointer = false;
11459 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11461 if (TREE_CODE (to) != TREE_CODE (from))
11462 return false;
11464 if (TREE_CODE (from) == OFFSET_TYPE
11465 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
11466 TYPE_OFFSET_BASETYPE (to)))
11467 return false;
11469 /* Const and volatile mean something different for function and
11470 array types, so the usual checks are not appropriate. We'll
11471 check the array type elements in further iterations. */
11472 if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11474 if (!at_least_as_qualified_p (to, from))
11475 return false;
11477 if (!at_least_as_qualified_p (from, to))
11479 if (constp == 0)
11480 return false;
11481 to_more_cv_qualified = true;
11484 if (constp > 0)
11485 constp &= TYPE_READONLY (to);
11488 if (VECTOR_TYPE_P (to))
11489 is_opaque_pointer = vector_targets_convertible_p (to, from);
11491 /* P0388R4 allows a conversion from int[N] to int[] but not the
11492 other way round. When both arrays have bounds but they do
11493 not match, then no conversion is possible. */
11494 if (TREE_CODE (to) == ARRAY_TYPE
11495 && !comp_array_types (to, from, bounds_first, /*strict=*/false))
11496 return false;
11498 if (!TYPE_PTR_P (to)
11499 && !TYPE_PTRDATAMEM_P (to)
11500 /* CWG 330 says we need to look through arrays. */
11501 && TREE_CODE (to) != ARRAY_TYPE)
11502 return ((constp >= 0 || to_more_cv_qualified)
11503 && (is_opaque_pointer
11504 || same_type_ignoring_top_level_qualifiers_p (to, from)));
11508 /* When comparing, say, char ** to char const **, this function takes
11509 the 'char *' and 'char const *'. Do not pass non-pointer/reference
11510 types to this function. */
11513 comp_ptr_ttypes (tree to, tree from)
11515 return comp_ptr_ttypes_real (to, from, 1);
11518 /* Returns true iff FNTYPE is a non-class type that involves
11519 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11520 if a parameter type is ill-formed. */
11522 bool
11523 error_type_p (const_tree type)
11525 tree t;
11527 switch (TREE_CODE (type))
11529 case ERROR_MARK:
11530 return true;
11532 case POINTER_TYPE:
11533 case REFERENCE_TYPE:
11534 case OFFSET_TYPE:
11535 return error_type_p (TREE_TYPE (type));
11537 case FUNCTION_TYPE:
11538 case METHOD_TYPE:
11539 if (error_type_p (TREE_TYPE (type)))
11540 return true;
11541 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
11542 if (error_type_p (TREE_VALUE (t)))
11543 return true;
11544 return false;
11546 case RECORD_TYPE:
11547 if (TYPE_PTRMEMFUNC_P (type))
11548 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
11549 return false;
11551 default:
11552 return false;
11556 /* Returns true if to and from are (possibly multi-level) pointers to the same
11557 type or inheritance-related types, regardless of cv-quals. */
11559 bool
11560 ptr_reasonably_similar (const_tree to, const_tree from)
11562 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11564 /* Any target type is similar enough to void. */
11565 if (VOID_TYPE_P (to))
11566 return !error_type_p (from);
11567 if (VOID_TYPE_P (from))
11568 return !error_type_p (to);
11570 if (TREE_CODE (to) != TREE_CODE (from))
11571 return false;
11573 if (TREE_CODE (from) == OFFSET_TYPE
11574 && comptypes (TYPE_OFFSET_BASETYPE (to),
11575 TYPE_OFFSET_BASETYPE (from),
11576 COMPARE_BASE | COMPARE_DERIVED))
11577 continue;
11579 if (VECTOR_TYPE_P (to)
11580 && vector_types_convertible_p (to, from, false))
11581 return true;
11583 if (TREE_CODE (to) == INTEGER_TYPE
11584 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
11585 return true;
11587 if (TREE_CODE (to) == FUNCTION_TYPE)
11588 return !error_type_p (to) && !error_type_p (from);
11590 if (!TYPE_PTR_P (to))
11592 /* When either type is incomplete avoid DERIVED_FROM_P,
11593 which may call complete_type (c++/57942). */
11594 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
11595 return comptypes
11596 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
11597 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
11602 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11603 pointer-to-member types) are the same, ignoring cv-qualification at
11604 all levels. CB says how we should behave when comparing array bounds. */
11606 bool
11607 comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
11609 bool is_opaque_pointer = false;
11611 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
11613 if (TREE_CODE (to) != TREE_CODE (from))
11614 return false;
11616 if (TREE_CODE (from) == OFFSET_TYPE
11617 && same_type_p (TYPE_OFFSET_BASETYPE (from),
11618 TYPE_OFFSET_BASETYPE (to)))
11619 continue;
11621 if (VECTOR_TYPE_P (to))
11622 is_opaque_pointer = vector_targets_convertible_p (to, from);
11624 if (TREE_CODE (to) == ARRAY_TYPE
11625 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11626 we must fail. */
11627 && !comp_array_types (to, from, cb, /*strict=*/false))
11628 return false;
11630 /* CWG 330 says we need to look through arrays. */
11631 if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
11632 return (is_opaque_pointer
11633 || same_type_ignoring_top_level_qualifiers_p (to, from));
11637 /* Returns the type qualifiers for this type, including the qualifiers on the
11638 elements for an array type. */
11641 cp_type_quals (const_tree type)
11643 int quals;
11644 /* This CONST_CAST is okay because strip_array_types returns its
11645 argument unmodified and we assign it to a const_tree. */
11646 type = strip_array_types (CONST_CAST_TREE (type));
11647 if (type == error_mark_node
11648 /* Quals on a FUNCTION_TYPE are memfn quals. */
11649 || TREE_CODE (type) == FUNCTION_TYPE)
11650 return TYPE_UNQUALIFIED;
11651 quals = TYPE_QUALS (type);
11652 /* METHOD and REFERENCE_TYPEs should never have quals. */
11653 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
11654 && !TYPE_REF_P (type))
11655 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
11656 == TYPE_UNQUALIFIED));
11657 return quals;
11660 /* Returns the function-ref-qualifier for TYPE */
11662 cp_ref_qualifier
11663 type_memfn_rqual (const_tree type)
11665 gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
11667 if (!FUNCTION_REF_QUALIFIED (type))
11668 return REF_QUAL_NONE;
11669 else if (FUNCTION_RVALUE_QUALIFIED (type))
11670 return REF_QUAL_RVALUE;
11671 else
11672 return REF_QUAL_LVALUE;
11675 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11676 METHOD_TYPE. */
11679 type_memfn_quals (const_tree type)
11681 if (TREE_CODE (type) == FUNCTION_TYPE)
11682 return TYPE_QUALS (type);
11683 else if (TREE_CODE (type) == METHOD_TYPE)
11684 return cp_type_quals (class_of_this_parm (type));
11685 else
11686 gcc_unreachable ();
11689 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11690 MEMFN_QUALS and its ref-qualifier to RQUAL. */
11692 tree
11693 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
11695 /* Could handle METHOD_TYPE here if necessary. */
11696 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
11697 if (TYPE_QUALS (type) == memfn_quals
11698 && type_memfn_rqual (type) == rqual)
11699 return type;
11701 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11702 complex. */
11703 tree result = build_qualified_type (type, memfn_quals);
11704 return build_ref_qualified_type (result, rqual);
11707 /* Returns nonzero if TYPE is const or volatile. */
11709 bool
11710 cv_qualified_p (const_tree type)
11712 int quals = cp_type_quals (type);
11713 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
11716 /* Returns nonzero if the TYPE contains a mutable member. */
11718 bool
11719 cp_has_mutable_p (const_tree type)
11721 /* This CONST_CAST is okay because strip_array_types returns its
11722 argument unmodified and we assign it to a const_tree. */
11723 type = strip_array_types (CONST_CAST_TREE(type));
11725 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
11728 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11729 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11730 approximation. In particular, consider:
11732 int f();
11733 struct S { int i; };
11734 const S s = { f(); }
11736 Here, we will make "s" as TREE_READONLY (because it is declared
11737 "const") -- only to reverse ourselves upon seeing that the
11738 initializer is non-constant. */
11740 void
11741 cp_apply_type_quals_to_decl (int type_quals, tree decl)
11743 tree type = TREE_TYPE (decl);
11745 if (type == error_mark_node)
11746 return;
11748 if (TREE_CODE (decl) == TYPE_DECL)
11749 return;
11751 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
11752 && type_quals != TYPE_UNQUALIFIED));
11754 /* Avoid setting TREE_READONLY incorrectly. */
11755 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11756 constructor can produce constant init, so rely on cp_finish_decl to
11757 clear TREE_READONLY if the variable has non-constant init. */
11759 /* If the type has (or might have) a mutable component, that component
11760 might be modified. */
11761 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
11762 type_quals &= ~TYPE_QUAL_CONST;
11764 c_apply_type_quals_to_decl (type_quals, decl);
11767 /* Subroutine of casts_away_constness. Make T1 and T2 point at
11768 exemplar types such that casting T1 to T2 is casting away constness
11769 if and only if there is no implicit conversion from T1 to T2. */
11771 static void
11772 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
11774 int quals1;
11775 int quals2;
11777 /* [expr.const.cast]
11779 For multi-level pointer to members and multi-level mixed pointers
11780 and pointers to members (conv.qual), the "member" aspect of a
11781 pointer to member level is ignored when determining if a const
11782 cv-qualifier has been cast away. */
11783 /* [expr.const.cast]
11785 For two pointer types:
11787 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11788 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11789 K is min(N,M)
11791 casting from X1 to X2 casts away constness if, for a non-pointer
11792 type T there does not exist an implicit conversion (clause
11793 _conv_) from:
11795 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11799 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11800 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
11801 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
11803 *t1 = cp_build_qualified_type (void_type_node,
11804 cp_type_quals (*t1));
11805 *t2 = cp_build_qualified_type (void_type_node,
11806 cp_type_quals (*t2));
11807 return;
11810 quals1 = cp_type_quals (*t1);
11811 quals2 = cp_type_quals (*t2);
11813 if (TYPE_PTRDATAMEM_P (*t1))
11814 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
11815 else
11816 *t1 = TREE_TYPE (*t1);
11817 if (TYPE_PTRDATAMEM_P (*t2))
11818 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
11819 else
11820 *t2 = TREE_TYPE (*t2);
11822 casts_away_constness_r (t1, t2, complain);
11823 *t1 = build_pointer_type (*t1);
11824 *t2 = build_pointer_type (*t2);
11825 *t1 = cp_build_qualified_type (*t1, quals1);
11826 *t2 = cp_build_qualified_type (*t2, quals2);
11829 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11830 constness.
11832 ??? This function returns non-zero if casting away qualifiers not
11833 just const. We would like to return to the caller exactly which
11834 qualifiers are casted away to give more accurate diagnostics.
11837 static bool
11838 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
11840 if (TYPE_REF_P (t2))
11842 /* [expr.const.cast]
11844 Casting from an lvalue of type T1 to an lvalue of type T2
11845 using a reference cast casts away constness if a cast from an
11846 rvalue of type "pointer to T1" to the type "pointer to T2"
11847 casts away constness. */
11848 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
11849 return casts_away_constness (build_pointer_type (t1),
11850 build_pointer_type (TREE_TYPE (t2)),
11851 complain);
11854 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
11855 /* [expr.const.cast]
11857 Casting from an rvalue of type "pointer to data member of X
11858 of type T1" to the type "pointer to data member of Y of type
11859 T2" casts away constness if a cast from an rvalue of type
11860 "pointer to T1" to the type "pointer to T2" casts away
11861 constness. */
11862 return casts_away_constness
11863 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
11864 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
11865 complain);
11867 /* Casting away constness is only something that makes sense for
11868 pointer or reference types. */
11869 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
11870 return false;
11872 /* Top-level qualifiers don't matter. */
11873 t1 = TYPE_MAIN_VARIANT (t1);
11874 t2 = TYPE_MAIN_VARIANT (t2);
11875 casts_away_constness_r (&t1, &t2, complain);
11876 if (!can_convert (t2, t1, complain))
11877 return true;
11879 return false;
11882 /* If T is a REFERENCE_TYPE return the type to which T refers.
11883 Otherwise, return T itself. */
11885 tree
11886 non_reference (tree t)
11888 if (t && TYPE_REF_P (t))
11889 t = TREE_TYPE (t);
11890 return t;
11894 /* Return nonzero if REF is an lvalue valid for this language;
11895 otherwise, print an error message and return zero. USE says
11896 how the lvalue is being used and so selects the error message. */
11899 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
11901 cp_lvalue_kind kind = lvalue_kind (ref);
11903 if (kind == clk_none)
11905 if (complain & tf_error)
11906 lvalue_error (cp_expr_loc_or_input_loc (ref), use);
11907 return 0;
11909 else if (kind & (clk_rvalueref|clk_class))
11911 if (!(complain & tf_error))
11912 return 0;
11913 /* Make this a permerror because we used to accept it. */
11914 permerror (cp_expr_loc_or_input_loc (ref),
11915 "using rvalue as lvalue");
11917 return 1;
11920 /* Return true if a user-defined literal operator is a raw operator. */
11922 bool
11923 check_raw_literal_operator (const_tree decl)
11925 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11926 tree argtype;
11927 int arity;
11928 bool maybe_raw_p = false;
11930 /* Count the number and type of arguments and check for ellipsis. */
11931 for (argtype = argtypes, arity = 0;
11932 argtype && argtype != void_list_node;
11933 ++arity, argtype = TREE_CHAIN (argtype))
11935 tree t = TREE_VALUE (argtype);
11937 if (same_type_p (t, const_string_type_node))
11938 maybe_raw_p = true;
11940 if (!argtype)
11941 return false; /* Found ellipsis. */
11943 if (!maybe_raw_p || arity != 1)
11944 return false;
11946 return true;
11950 /* Return true if a user-defined literal operator has one of the allowed
11951 argument types. */
11953 bool
11954 check_literal_operator_args (const_tree decl,
11955 bool *long_long_unsigned_p, bool *long_double_p)
11957 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
11959 *long_long_unsigned_p = false;
11960 *long_double_p = false;
11961 if (processing_template_decl || processing_specialization)
11962 return argtypes == void_list_node;
11963 else
11965 tree argtype;
11966 int arity;
11967 int max_arity = 2;
11969 /* Count the number and type of arguments and check for ellipsis. */
11970 for (argtype = argtypes, arity = 0;
11971 argtype && argtype != void_list_node;
11972 argtype = TREE_CHAIN (argtype))
11974 tree t = TREE_VALUE (argtype);
11975 ++arity;
11977 if (TYPE_PTR_P (t))
11979 bool maybe_raw_p = false;
11980 t = TREE_TYPE (t);
11981 if (cp_type_quals (t) != TYPE_QUAL_CONST)
11982 return false;
11983 t = TYPE_MAIN_VARIANT (t);
11984 if ((maybe_raw_p = same_type_p (t, char_type_node))
11985 || same_type_p (t, wchar_type_node)
11986 || same_type_p (t, char8_type_node)
11987 || same_type_p (t, char16_type_node)
11988 || same_type_p (t, char32_type_node))
11990 argtype = TREE_CHAIN (argtype);
11991 if (!argtype)
11992 return false;
11993 t = TREE_VALUE (argtype);
11994 if (maybe_raw_p && argtype == void_list_node)
11995 return true;
11996 else if (same_type_p (t, size_type_node))
11998 ++arity;
11999 continue;
12001 else
12002 return false;
12005 else if (same_type_p (t, long_long_unsigned_type_node))
12007 max_arity = 1;
12008 *long_long_unsigned_p = true;
12010 else if (same_type_p (t, long_double_type_node))
12012 max_arity = 1;
12013 *long_double_p = true;
12015 else if (same_type_p (t, char_type_node))
12016 max_arity = 1;
12017 else if (same_type_p (t, wchar_type_node))
12018 max_arity = 1;
12019 else if (same_type_p (t, char8_type_node))
12020 max_arity = 1;
12021 else if (same_type_p (t, char16_type_node))
12022 max_arity = 1;
12023 else if (same_type_p (t, char32_type_node))
12024 max_arity = 1;
12025 else
12026 return false;
12028 if (!argtype)
12029 return false; /* Found ellipsis. */
12031 if (arity != max_arity)
12032 return false;
12034 return true;
12038 /* Always returns false since unlike C90, C++ has no concept of implicit
12039 function declarations. */
12041 bool
12042 c_decl_implicit (const_tree)
12044 return false;