C++: special-case single non-viable candidate (more PR c++/85110)
[official-gcc.git] / gcc / cp / typeck.c
blobe9932202b4684f91dea7b7f477e5fb09fd29e6d7
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2018 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 "params.h"
39 #include "gcc-rich-location.h"
40 #include "stringpool.h"
41 #include "attribs.h"
42 #include "asan.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 int 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, 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);
62 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
63 static void error_args_num (location_t, tree, bool);
64 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
65 tsubst_flags_t);
66 static bool is_std_move_p (tree);
67 static bool is_std_forward_p (tree);
69 /* Do `exp = require_complete_type (exp);' to make sure exp
70 does not have an incomplete type. (That includes void types.)
71 Returns error_mark_node if the VALUE does not have
72 complete type when this function returns. */
74 tree
75 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
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 tree
101 require_complete_type (tree value)
103 return require_complete_type_sfinae (value, tf_warning_or_error);
106 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
107 a template instantiation, do the instantiation. Returns TYPE,
108 whether or not it could be completed, unless something goes
109 horribly wrong, in which case the error_mark_node is returned. */
111 tree
112 complete_type (tree type)
114 if (type == NULL_TREE)
115 /* Rather than crash, we return something sure to cause an error
116 at some point. */
117 return error_mark_node;
119 if (type == error_mark_node || COMPLETE_TYPE_P (type))
121 else if (TREE_CODE (type) == ARRAY_TYPE)
123 tree t = complete_type (TREE_TYPE (type));
124 unsigned int needs_constructing, has_nontrivial_dtor;
125 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
126 layout_type (type);
127 needs_constructing
128 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
129 has_nontrivial_dtor
130 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
131 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
133 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
134 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
137 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
138 instantiate_class_template (TYPE_MAIN_VARIANT (type));
140 return type;
143 /* Like complete_type, but issue an error if the TYPE cannot be completed.
144 VALUE is used for informative diagnostics.
145 Returns NULL_TREE if the type cannot be made complete. */
147 tree
148 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
150 type = complete_type (type);
151 if (type == error_mark_node)
152 /* We already issued an error. */
153 return NULL_TREE;
154 else if (!COMPLETE_TYPE_P (type))
156 if (complain & tf_error)
157 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
158 return NULL_TREE;
160 else
161 return type;
164 tree
165 complete_type_or_else (tree type, tree value)
167 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
171 /* Return the common type of two parameter lists.
172 We assume that comptypes has already been done and returned 1;
173 if that isn't so, this may crash.
175 As an optimization, free the space we allocate if the parameter
176 lists are already common. */
178 static tree
179 commonparms (tree p1, tree p2)
181 tree oldargs = p1, newargs, n;
182 int i, len;
183 int any_change = 0;
185 len = list_length (p1);
186 newargs = tree_last (p1);
188 if (newargs == void_list_node)
189 i = 1;
190 else
192 i = 0;
193 newargs = 0;
196 for (; i < len; i++)
197 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
199 n = newargs;
201 for (i = 0; p1;
202 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
204 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
206 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
207 any_change = 1;
209 else if (! TREE_PURPOSE (p1))
211 if (TREE_PURPOSE (p2))
213 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
214 any_change = 1;
217 else
219 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
220 any_change = 1;
221 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
223 if (TREE_VALUE (p1) != TREE_VALUE (p2))
225 any_change = 1;
226 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
228 else
229 TREE_VALUE (n) = TREE_VALUE (p1);
231 if (! any_change)
232 return oldargs;
234 return newargs;
237 /* Given a type, perhaps copied for a typedef,
238 find the "original" version of it. */
239 static tree
240 original_type (tree t)
242 int quals = cp_type_quals (t);
243 while (t != error_mark_node
244 && TYPE_NAME (t) != NULL_TREE)
246 tree x = TYPE_NAME (t);
247 if (TREE_CODE (x) != TYPE_DECL)
248 break;
249 x = DECL_ORIGINAL_TYPE (x);
250 if (x == NULL_TREE)
251 break;
252 t = x;
254 return cp_build_qualified_type (t, quals);
257 /* Return the common type for two arithmetic types T1 and T2 under the
258 usual arithmetic conversions. The default conversions have already
259 been applied, and enumerated types converted to their compatible
260 integer types. */
262 static tree
263 cp_common_type (tree t1, tree t2)
265 enum tree_code code1 = TREE_CODE (t1);
266 enum tree_code code2 = TREE_CODE (t2);
267 tree attributes;
268 int i;
271 /* In what follows, we slightly generalize the rules given in [expr] so
272 as to deal with `long long' and `complex'. First, merge the
273 attributes. */
274 attributes = (*targetm.merge_type_attributes) (t1, t2);
276 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
278 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
279 return build_type_attribute_variant (t1, attributes);
280 else
281 return NULL_TREE;
284 /* FIXME: Attributes. */
285 gcc_assert (ARITHMETIC_TYPE_P (t1)
286 || VECTOR_TYPE_P (t1)
287 || UNSCOPED_ENUM_P (t1));
288 gcc_assert (ARITHMETIC_TYPE_P (t2)
289 || VECTOR_TYPE_P (t2)
290 || UNSCOPED_ENUM_P (t2));
292 /* If one type is complex, form the common type of the non-complex
293 components, then make that complex. Use T1 or T2 if it is the
294 required type. */
295 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
297 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
298 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
299 tree subtype
300 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
302 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
303 return build_type_attribute_variant (t1, attributes);
304 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
305 return build_type_attribute_variant (t2, attributes);
306 else
307 return build_type_attribute_variant (build_complex_type (subtype),
308 attributes);
311 if (code1 == VECTOR_TYPE)
313 /* When we get here we should have two vectors of the same size.
314 Just prefer the unsigned one if present. */
315 if (TYPE_UNSIGNED (t1))
316 return build_type_attribute_variant (t1, attributes);
317 else
318 return build_type_attribute_variant (t2, attributes);
321 /* If only one is real, use it as the result. */
322 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
323 return build_type_attribute_variant (t1, attributes);
324 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
325 return build_type_attribute_variant (t2, attributes);
327 /* Both real or both integers; use the one with greater precision. */
328 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
329 return build_type_attribute_variant (t1, attributes);
330 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
331 return build_type_attribute_variant (t2, attributes);
333 /* The types are the same; no need to do anything fancy. */
334 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
335 return build_type_attribute_variant (t1, attributes);
337 if (code1 != REAL_TYPE)
339 /* If one is unsigned long long, then convert the other to unsigned
340 long long. */
341 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
342 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
343 return build_type_attribute_variant (long_long_unsigned_type_node,
344 attributes);
345 /* If one is a long long, and the other is an unsigned long, and
346 long long can represent all the values of an unsigned long, then
347 convert to a long long. Otherwise, convert to an unsigned long
348 long. Otherwise, if either operand is long long, convert the
349 other to long long.
351 Since we're here, we know the TYPE_PRECISION is the same;
352 therefore converting to long long cannot represent all the values
353 of an unsigned long, so we choose unsigned long long in that
354 case. */
355 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
356 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
358 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
359 ? long_long_unsigned_type_node
360 : long_long_integer_type_node);
361 return build_type_attribute_variant (t, attributes);
364 /* Go through the same procedure, but for longs. */
365 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
366 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
367 return build_type_attribute_variant (long_unsigned_type_node,
368 attributes);
369 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
372 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
373 ? long_unsigned_type_node : long_integer_type_node);
374 return build_type_attribute_variant (t, attributes);
377 /* For __intN types, either the type is __int128 (and is lower
378 priority than the types checked above, but higher than other
379 128-bit types) or it's known to not be the same size as other
380 types (enforced in toplev.c). Prefer the unsigned type. */
381 for (i = 0; i < NUM_INT_N_ENTS; i ++)
383 if (int_n_enabled_p [i]
384 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
386 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
387 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
389 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
390 ? int_n_trees[i].unsigned_type
391 : int_n_trees[i].signed_type);
392 return build_type_attribute_variant (t, attributes);
396 /* Otherwise prefer the unsigned one. */
397 if (TYPE_UNSIGNED (t1))
398 return build_type_attribute_variant (t1, attributes);
399 else
400 return build_type_attribute_variant (t2, attributes);
402 else
404 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
406 return build_type_attribute_variant (long_double_type_node,
407 attributes);
408 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
409 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
410 return build_type_attribute_variant (double_type_node,
411 attributes);
412 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
413 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
414 return build_type_attribute_variant (float_type_node,
415 attributes);
417 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
418 the standard C++ floating-point types. Logic earlier in this
419 function has already eliminated the possibility that
420 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
421 compelling reason to choose one or the other. */
422 return build_type_attribute_variant (t1, attributes);
426 /* T1 and T2 are arithmetic or enumeration types. Return the type
427 that will result from the "usual arithmetic conversions" on T1 and
428 T2 as described in [expr]. */
430 tree
431 type_after_usual_arithmetic_conversions (tree t1, tree t2)
433 gcc_assert (ARITHMETIC_TYPE_P (t1)
434 || VECTOR_TYPE_P (t1)
435 || UNSCOPED_ENUM_P (t1));
436 gcc_assert (ARITHMETIC_TYPE_P (t2)
437 || VECTOR_TYPE_P (t2)
438 || UNSCOPED_ENUM_P (t2));
440 /* Perform the integral promotions. We do not promote real types here. */
441 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
442 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
444 t1 = type_promotes_to (t1);
445 t2 = type_promotes_to (t2);
448 return cp_common_type (t1, t2);
451 static void
452 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
453 composite_pointer_operation operation)
455 switch (operation)
457 case CPO_COMPARISON:
458 emit_diagnostic (kind, input_location, 0,
459 "comparison between "
460 "distinct pointer types %qT and %qT lacks a cast",
461 t1, t2);
462 break;
463 case CPO_CONVERSION:
464 emit_diagnostic (kind, input_location, 0,
465 "conversion between "
466 "distinct pointer types %qT and %qT lacks a cast",
467 t1, t2);
468 break;
469 case CPO_CONDITIONAL_EXPR:
470 emit_diagnostic (kind, input_location, 0,
471 "conditional expression between "
472 "distinct pointer types %qT and %qT lacks a cast",
473 t1, t2);
474 break;
475 default:
476 gcc_unreachable ();
480 /* Subroutine of composite_pointer_type to implement the recursive
481 case. See that function for documentation of the parameters. */
483 static tree
484 composite_pointer_type_r (tree t1, tree t2,
485 composite_pointer_operation operation,
486 tsubst_flags_t complain)
488 tree pointee1;
489 tree pointee2;
490 tree result_type;
491 tree attributes;
493 /* Determine the types pointed to by T1 and T2. */
494 if (TYPE_PTR_P (t1))
496 pointee1 = TREE_TYPE (t1);
497 pointee2 = TREE_TYPE (t2);
499 else
501 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
502 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
505 /* [expr.rel]
507 Otherwise, the composite pointer type is a pointer type
508 similar (_conv.qual_) to the type of one of the operands,
509 with a cv-qualification signature (_conv.qual_) that is the
510 union of the cv-qualification signatures of the operand
511 types. */
512 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
513 result_type = pointee1;
514 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
515 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
517 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
518 complain);
519 if (result_type == error_mark_node)
520 return error_mark_node;
522 else
524 if (complain & tf_error)
525 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
526 else
527 return error_mark_node;
528 result_type = void_type_node;
530 result_type = cp_build_qualified_type (result_type,
531 (cp_type_quals (pointee1)
532 | cp_type_quals (pointee2)));
533 /* If the original types were pointers to members, so is the
534 result. */
535 if (TYPE_PTRMEM_P (t1))
537 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
538 TYPE_PTRMEM_CLASS_TYPE (t2)))
540 if (complain & tf_error)
541 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
542 else
543 return error_mark_node;
545 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
546 result_type);
548 else
549 result_type = build_pointer_type (result_type);
551 /* Merge the attributes. */
552 attributes = (*targetm.merge_type_attributes) (t1, t2);
553 return build_type_attribute_variant (result_type, attributes);
556 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
557 ARG1 and ARG2 are the values with those types. The OPERATION is to
558 describe the operation between the pointer types,
559 in case an error occurs.
561 This routine also implements the computation of a common type for
562 pointers-to-members as per [expr.eq]. */
564 tree
565 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
566 composite_pointer_operation operation,
567 tsubst_flags_t complain)
569 tree class1;
570 tree class2;
572 /* [expr.rel]
574 If one operand is a null pointer constant, the composite pointer
575 type is the type of the other operand. */
576 if (null_ptr_cst_p (arg1))
577 return t2;
578 if (null_ptr_cst_p (arg2))
579 return t1;
581 /* We have:
583 [expr.rel]
585 If one of the operands has type "pointer to cv1 void*", then
586 the other has type "pointer to cv2T", and the composite pointer
587 type is "pointer to cv12 void", where cv12 is the union of cv1
588 and cv2.
590 If either type is a pointer to void, make sure it is T1. */
591 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
592 std::swap (t1, t2);
594 /* Now, if T1 is a pointer to void, merge the qualifiers. */
595 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
597 tree attributes;
598 tree result_type;
600 if (TYPE_PTRFN_P (t2))
602 if (complain & tf_error)
604 switch (operation)
606 case CPO_COMPARISON:
607 pedwarn (input_location, OPT_Wpedantic,
608 "ISO C++ forbids comparison between pointer "
609 "of type %<void *%> and pointer-to-function");
610 break;
611 case CPO_CONVERSION:
612 pedwarn (input_location, OPT_Wpedantic,
613 "ISO C++ forbids conversion between pointer "
614 "of type %<void *%> and pointer-to-function");
615 break;
616 case CPO_CONDITIONAL_EXPR:
617 pedwarn (input_location, OPT_Wpedantic,
618 "ISO C++ forbids conditional expression between "
619 "pointer of type %<void *%> and "
620 "pointer-to-function");
621 break;
622 default:
623 gcc_unreachable ();
626 else
627 return error_mark_node;
629 result_type
630 = cp_build_qualified_type (void_type_node,
631 (cp_type_quals (TREE_TYPE (t1))
632 | cp_type_quals (TREE_TYPE (t2))));
633 result_type = build_pointer_type (result_type);
634 /* Merge the attributes. */
635 attributes = (*targetm.merge_type_attributes) (t1, t2);
636 return build_type_attribute_variant (result_type, attributes);
639 if (c_dialect_objc () && TYPE_PTR_P (t1)
640 && TYPE_PTR_P (t2))
642 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
643 return objc_common_type (t1, t2);
646 /* if T1 or T2 is "pointer to noexcept function" and the other type is
647 "pointer to function", where the function types are otherwise the same,
648 "pointer to function" */
649 if (fnptr_conv_p (t1, t2))
650 return t1;
651 if (fnptr_conv_p (t2, t1))
652 return t2;
654 /* [expr.eq] permits the application of a pointer conversion to
655 bring the pointers to a common type. */
656 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
657 && CLASS_TYPE_P (TREE_TYPE (t1))
658 && CLASS_TYPE_P (TREE_TYPE (t2))
659 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
660 TREE_TYPE (t2)))
662 class1 = TREE_TYPE (t1);
663 class2 = TREE_TYPE (t2);
665 if (DERIVED_FROM_P (class1, class2))
666 t2 = (build_pointer_type
667 (cp_build_qualified_type (class1, cp_type_quals (class2))));
668 else if (DERIVED_FROM_P (class2, class1))
669 t1 = (build_pointer_type
670 (cp_build_qualified_type (class2, cp_type_quals (class1))));
671 else
673 if (complain & tf_error)
674 composite_pointer_error (DK_ERROR, t1, t2, operation);
675 return error_mark_node;
678 /* [expr.eq] permits the application of a pointer-to-member
679 conversion to change the class type of one of the types. */
680 else if (TYPE_PTRMEM_P (t1)
681 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
682 TYPE_PTRMEM_CLASS_TYPE (t2)))
684 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
685 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
687 if (DERIVED_FROM_P (class1, class2))
688 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
689 else if (DERIVED_FROM_P (class2, class1))
690 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
691 else
693 if (complain & tf_error)
694 switch (operation)
696 case CPO_COMPARISON:
697 error ("comparison between distinct "
698 "pointer-to-member types %qT and %qT lacks a cast",
699 t1, t2);
700 break;
701 case CPO_CONVERSION:
702 error ("conversion between distinct "
703 "pointer-to-member types %qT and %qT lacks a cast",
704 t1, t2);
705 break;
706 case CPO_CONDITIONAL_EXPR:
707 error ("conditional expression between distinct "
708 "pointer-to-member types %qT and %qT lacks a cast",
709 t1, t2);
710 break;
711 default:
712 gcc_unreachable ();
714 return error_mark_node;
718 return composite_pointer_type_r (t1, t2, operation, complain);
721 /* Return the merged type of two types.
722 We assume that comptypes has already been done and returned 1;
723 if that isn't so, this may crash.
725 This just combines attributes and default arguments; any other
726 differences would cause the two types to compare unalike. */
728 tree
729 merge_types (tree t1, tree t2)
731 enum tree_code code1;
732 enum tree_code code2;
733 tree attributes;
735 /* Save time if the two types are the same. */
736 if (t1 == t2)
737 return t1;
738 if (original_type (t1) == original_type (t2))
739 return t1;
741 /* If one type is nonsense, use the other. */
742 if (t1 == error_mark_node)
743 return t2;
744 if (t2 == error_mark_node)
745 return t1;
747 /* Handle merging an auto redeclaration with a previous deduced
748 return type. */
749 if (is_auto (t1))
750 return t2;
752 /* Merge the attributes. */
753 attributes = (*targetm.merge_type_attributes) (t1, t2);
755 if (TYPE_PTRMEMFUNC_P (t1))
756 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
757 if (TYPE_PTRMEMFUNC_P (t2))
758 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
760 code1 = TREE_CODE (t1);
761 code2 = TREE_CODE (t2);
762 if (code1 != code2)
764 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
765 if (code1 == TYPENAME_TYPE)
767 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
768 code1 = TREE_CODE (t1);
770 else
772 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
773 code2 = TREE_CODE (t2);
777 switch (code1)
779 case POINTER_TYPE:
780 case REFERENCE_TYPE:
781 /* For two pointers, do this recursively on the target type. */
783 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
784 int quals = cp_type_quals (t1);
786 if (code1 == POINTER_TYPE)
788 t1 = build_pointer_type (target);
789 if (TREE_CODE (target) == METHOD_TYPE)
790 t1 = build_ptrmemfunc_type (t1);
792 else
793 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
794 t1 = build_type_attribute_variant (t1, attributes);
795 t1 = cp_build_qualified_type (t1, quals);
797 return t1;
800 case OFFSET_TYPE:
802 int quals;
803 tree pointee;
804 quals = cp_type_quals (t1);
805 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
806 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
807 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
808 pointee);
809 t1 = cp_build_qualified_type (t1, quals);
810 break;
813 case ARRAY_TYPE:
815 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
816 /* Save space: see if the result is identical to one of the args. */
817 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
818 return build_type_attribute_variant (t1, attributes);
819 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
820 return build_type_attribute_variant (t2, attributes);
821 /* Merge the element types, and have a size if either arg has one. */
822 t1 = build_cplus_array_type
823 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
824 break;
827 case FUNCTION_TYPE:
828 /* Function types: prefer the one that specified arg types.
829 If both do, merge the arg types. Also merge the return types. */
831 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
832 tree p1 = TYPE_ARG_TYPES (t1);
833 tree p2 = TYPE_ARG_TYPES (t2);
834 tree parms;
836 /* Save space: see if the result is identical to one of the args. */
837 if (valtype == TREE_TYPE (t1) && ! p2)
838 return cp_build_type_attribute_variant (t1, attributes);
839 if (valtype == TREE_TYPE (t2) && ! p1)
840 return cp_build_type_attribute_variant (t2, attributes);
842 /* Simple way if one arg fails to specify argument types. */
843 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
844 parms = p2;
845 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
846 parms = p1;
847 else
848 parms = commonparms (p1, p2);
850 cp_cv_quals quals = type_memfn_quals (t1);
851 cp_ref_qualifier rqual = type_memfn_rqual (t1);
852 gcc_assert (quals == type_memfn_quals (t2));
853 gcc_assert (rqual == type_memfn_rqual (t2));
855 tree rval = build_function_type (valtype, parms);
856 rval = apply_memfn_quals (rval, quals);
857 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
858 TYPE_RAISES_EXCEPTIONS (t2));
859 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
860 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
861 break;
864 case METHOD_TYPE:
866 /* Get this value the long way, since TYPE_METHOD_BASETYPE
867 is just the main variant of this. */
868 tree basetype = class_of_this_parm (t2);
869 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
870 TYPE_RAISES_EXCEPTIONS (t2));
871 cp_ref_qualifier rqual = type_memfn_rqual (t1);
872 tree t3;
873 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
875 /* If this was a member function type, get back to the
876 original type of type member function (i.e., without
877 the class instance variable up front. */
878 t1 = build_function_type (TREE_TYPE (t1),
879 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
880 t2 = build_function_type (TREE_TYPE (t2),
881 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
882 t3 = merge_types (t1, t2);
883 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
884 TYPE_ARG_TYPES (t3));
885 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
886 break;
889 case TYPENAME_TYPE:
890 /* There is no need to merge attributes into a TYPENAME_TYPE.
891 When the type is instantiated it will have whatever
892 attributes result from the instantiation. */
893 return t1;
895 default:;
896 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
897 return t1;
898 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
899 return t2;
900 break;
903 return cp_build_type_attribute_variant (t1, attributes);
906 /* Return the ARRAY_TYPE type without its domain. */
908 tree
909 strip_array_domain (tree type)
911 tree t2;
912 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
913 if (TYPE_DOMAIN (type) == NULL_TREE)
914 return type;
915 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
916 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
919 /* Wrapper around cp_common_type that is used by c-common.c and other
920 front end optimizations that remove promotions.
922 Return the common type for two arithmetic types T1 and T2 under the
923 usual arithmetic conversions. The default conversions have already
924 been applied, and enumerated types converted to their compatible
925 integer types. */
927 tree
928 common_type (tree t1, tree t2)
930 /* If one type is nonsense, use the other */
931 if (t1 == error_mark_node)
932 return t2;
933 if (t2 == error_mark_node)
934 return t1;
936 return cp_common_type (t1, t2);
939 /* Return the common type of two pointer types T1 and T2. This is the
940 type for the result of most arithmetic operations if the operands
941 have the given two types.
943 We assume that comp_target_types has already been done and returned
944 nonzero; if that isn't so, this may crash. */
946 tree
947 common_pointer_type (tree t1, tree t2)
949 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
950 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
951 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
953 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
954 CPO_CONVERSION, tf_warning_or_error);
957 /* Compare two exception specifier types for exactness or subsetness, if
958 allowed. Returns false for mismatch, true for match (same, or
959 derived and !exact).
961 [except.spec] "If a class X ... objects of class X or any class publicly
962 and unambiguously derived from X. Similarly, if a pointer type Y * ...
963 exceptions of type Y * or that are pointers to any type publicly and
964 unambiguously derived from Y. Otherwise a function only allows exceptions
965 that have the same type ..."
966 This does not mention cv qualifiers and is different to what throw
967 [except.throw] and catch [except.catch] will do. They will ignore the
968 top level cv qualifiers, and allow qualifiers in the pointer to class
969 example.
971 We implement the letter of the standard. */
973 static bool
974 comp_except_types (tree a, tree b, bool exact)
976 if (same_type_p (a, b))
977 return true;
978 else if (!exact)
980 if (cp_type_quals (a) || cp_type_quals (b))
981 return false;
983 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
985 a = TREE_TYPE (a);
986 b = TREE_TYPE (b);
987 if (cp_type_quals (a) || cp_type_quals (b))
988 return false;
991 if (TREE_CODE (a) != RECORD_TYPE
992 || TREE_CODE (b) != RECORD_TYPE)
993 return false;
995 if (publicly_uniquely_derived_p (a, b))
996 return true;
998 return false;
1001 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1002 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1003 If EXACT is ce_type, the C++17 type compatibility rules apply.
1004 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1005 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1006 are unordered, but we've already filtered out duplicates. Most lists will
1007 be in order, we should try to make use of that. */
1009 bool
1010 comp_except_specs (const_tree t1, const_tree t2, int exact)
1012 const_tree probe;
1013 const_tree base;
1014 int length = 0;
1016 if (t1 == t2)
1017 return true;
1019 /* First handle noexcept. */
1020 if (exact < ce_exact)
1022 if (exact == ce_type
1023 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1024 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1025 return true;
1027 /* noexcept(false) is compatible with no exception-specification,
1028 and less strict than any spec. */
1029 if (t1 == noexcept_false_spec)
1030 return t2 == NULL_TREE || exact == ce_derived;
1031 /* Even a derived noexcept(false) is compatible with no
1032 exception-specification. */
1033 if (t2 == noexcept_false_spec)
1034 return t1 == NULL_TREE;
1036 /* Otherwise, if we aren't looking for an exact match, noexcept is
1037 equivalent to throw(). */
1038 if (t1 == noexcept_true_spec)
1039 t1 = empty_except_spec;
1040 if (t2 == noexcept_true_spec)
1041 t2 = empty_except_spec;
1044 /* If any noexcept is left, it is only comparable to itself;
1045 either we're looking for an exact match or we're redeclaring a
1046 template with dependent noexcept. */
1047 if ((t1 && TREE_PURPOSE (t1))
1048 || (t2 && TREE_PURPOSE (t2)))
1049 return (t1 && t2
1050 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1052 if (t1 == NULL_TREE) /* T1 is ... */
1053 return t2 == NULL_TREE || exact == ce_derived;
1054 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1055 return t2 != NULL_TREE && !TREE_VALUE (t2);
1056 if (t2 == NULL_TREE) /* T2 is ... */
1057 return false;
1058 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1059 return exact == ce_derived;
1061 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1062 Count how many we find, to determine exactness. For exact matching and
1063 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1064 O(nm). */
1065 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1067 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1069 tree a = TREE_VALUE (probe);
1070 tree b = TREE_VALUE (t2);
1072 if (comp_except_types (a, b, exact))
1074 if (probe == base && exact > ce_derived)
1075 base = TREE_CHAIN (probe);
1076 length++;
1077 break;
1080 if (probe == NULL_TREE)
1081 return false;
1083 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1086 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1087 [] can match [size]. */
1089 static bool
1090 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1092 tree d1;
1093 tree d2;
1094 tree max1, max2;
1096 if (t1 == t2)
1097 return true;
1099 /* The type of the array elements must be the same. */
1100 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1101 return false;
1103 d1 = TYPE_DOMAIN (t1);
1104 d2 = TYPE_DOMAIN (t2);
1106 if (d1 == d2)
1107 return true;
1109 /* If one of the arrays is dimensionless, and the other has a
1110 dimension, they are of different types. However, it is valid to
1111 write:
1113 extern int a[];
1114 int a[3];
1116 by [basic.link]:
1118 declarations for an array object can specify
1119 array types that differ by the presence or absence of a major
1120 array bound (_dcl.array_). */
1121 if (!d1 || !d2)
1122 return allow_redeclaration;
1124 /* Check that the dimensions are the same. */
1126 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1127 return false;
1128 max1 = TYPE_MAX_VALUE (d1);
1129 max2 = TYPE_MAX_VALUE (d2);
1131 if (!cp_tree_equal (max1, max2))
1132 return false;
1134 return true;
1137 /* Compare the relative position of T1 and T2 into their respective
1138 template parameter list.
1139 T1 and T2 must be template parameter types.
1140 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1142 static bool
1143 comp_template_parms_position (tree t1, tree t2)
1145 tree index1, index2;
1146 gcc_assert (t1 && t2
1147 && TREE_CODE (t1) == TREE_CODE (t2)
1148 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1149 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1150 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1152 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1153 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1155 /* Then compare their relative position. */
1156 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1157 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1158 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1159 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1160 return false;
1162 /* In C++14 we can end up comparing 'auto' to a normal template
1163 parameter. Don't confuse them. */
1164 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1165 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1167 return true;
1170 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1172 static bool
1173 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1175 t1 = TYPE_MAIN_VARIANT (t1);
1176 t2 = TYPE_MAIN_VARIANT (t2);
1178 if (TYPE_PTR_P (t1)
1179 && TYPE_PTR_P (t2))
1180 return true;
1182 /* The signedness of the parameter matters only when an integral
1183 type smaller than int is promoted to int, otherwise only the
1184 precision of the parameter matters.
1185 This check should make sure that the callee does not see
1186 undefined values in argument registers. */
1187 if (INTEGRAL_TYPE_P (t1)
1188 && INTEGRAL_TYPE_P (t2)
1189 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1190 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1191 || !targetm.calls.promote_prototypes (NULL_TREE)
1192 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1193 return true;
1195 return same_type_p (t1, t2);
1198 /* Check if a type cast between two function types can be considered safe. */
1200 static bool
1201 cxx_safe_function_type_cast_p (tree t1, tree t2)
1203 if (TREE_TYPE (t1) == void_type_node &&
1204 TYPE_ARG_TYPES (t1) == void_list_node)
1205 return true;
1207 if (TREE_TYPE (t2) == void_type_node &&
1208 TYPE_ARG_TYPES (t2) == void_list_node)
1209 return true;
1211 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1212 return false;
1214 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1215 t1 && t2;
1216 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1217 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1218 return false;
1220 return true;
1223 /* Subroutine in comptypes. */
1225 static bool
1226 structural_comptypes (tree t1, tree t2, int strict)
1228 if (t1 == t2)
1229 return true;
1231 /* Suppress errors caused by previously reported errors. */
1232 if (t1 == error_mark_node || t2 == error_mark_node)
1233 return false;
1235 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1237 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1238 current instantiation. */
1239 if (TREE_CODE (t1) == TYPENAME_TYPE)
1240 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1242 if (TREE_CODE (t2) == TYPENAME_TYPE)
1243 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1245 if (TYPE_PTRMEMFUNC_P (t1))
1246 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1247 if (TYPE_PTRMEMFUNC_P (t2))
1248 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1250 /* Different classes of types can't be compatible. */
1251 if (TREE_CODE (t1) != TREE_CODE (t2))
1252 return false;
1254 /* Qualifiers must match. For array types, we will check when we
1255 recur on the array element types. */
1256 if (TREE_CODE (t1) != ARRAY_TYPE
1257 && cp_type_quals (t1) != cp_type_quals (t2))
1258 return false;
1259 if (TREE_CODE (t1) == FUNCTION_TYPE
1260 && type_memfn_quals (t1) != type_memfn_quals (t2))
1261 return false;
1262 /* Need to check this before TYPE_MAIN_VARIANT.
1263 FIXME function qualifiers should really change the main variant. */
1264 if (TREE_CODE (t1) == FUNCTION_TYPE
1265 || TREE_CODE (t1) == METHOD_TYPE)
1267 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1268 return false;
1269 if (flag_noexcept_type
1270 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1271 TYPE_RAISES_EXCEPTIONS (t2),
1272 ce_type))
1273 return false;
1276 /* Allow for two different type nodes which have essentially the same
1277 definition. Note that we already checked for equality of the type
1278 qualifiers (just above). */
1280 if (TREE_CODE (t1) != ARRAY_TYPE
1281 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1282 return true;
1285 /* Compare the types. Break out if they could be the same. */
1286 switch (TREE_CODE (t1))
1288 case VOID_TYPE:
1289 case BOOLEAN_TYPE:
1290 /* All void and bool types are the same. */
1291 break;
1293 case INTEGER_TYPE:
1294 case FIXED_POINT_TYPE:
1295 case REAL_TYPE:
1296 /* With these nodes, we can't determine type equivalence by
1297 looking at what is stored in the nodes themselves, because
1298 two nodes might have different TYPE_MAIN_VARIANTs but still
1299 represent the same type. For example, wchar_t and int could
1300 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1301 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1302 and are distinct types. On the other hand, int and the
1303 following typedef
1305 typedef int INT __attribute((may_alias));
1307 have identical properties, different TYPE_MAIN_VARIANTs, but
1308 represent the same type. The canonical type system keeps
1309 track of equivalence in this case, so we fall back on it. */
1310 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1312 case TEMPLATE_TEMPLATE_PARM:
1313 case BOUND_TEMPLATE_TEMPLATE_PARM:
1314 if (!comp_template_parms_position (t1, t2))
1315 return false;
1316 if (!comp_template_parms
1317 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1318 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1319 return false;
1320 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1321 break;
1322 /* Don't check inheritance. */
1323 strict = COMPARE_STRICT;
1324 /* Fall through. */
1326 case RECORD_TYPE:
1327 case UNION_TYPE:
1328 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1329 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1330 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1331 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1332 break;
1334 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1335 break;
1336 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1337 break;
1339 return false;
1341 case OFFSET_TYPE:
1342 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1343 strict & ~COMPARE_REDECLARATION))
1344 return false;
1345 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1346 return false;
1347 break;
1349 case REFERENCE_TYPE:
1350 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1351 return false;
1352 /* fall through to checks for pointer types */
1353 gcc_fallthrough ();
1355 case POINTER_TYPE:
1356 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1357 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1358 return false;
1359 break;
1361 case METHOD_TYPE:
1362 case FUNCTION_TYPE:
1363 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1364 return false;
1365 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1366 return false;
1367 break;
1369 case ARRAY_TYPE:
1370 /* Target types must match incl. qualifiers. */
1371 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1372 return false;
1373 break;
1375 case TEMPLATE_TYPE_PARM:
1376 /* If T1 and T2 don't have the same relative position in their
1377 template parameters set, they can't be equal. */
1378 if (!comp_template_parms_position (t1, t2))
1379 return false;
1380 /* If T1 and T2 don't represent the same class template deduction,
1381 they aren't equal. */
1382 if (CLASS_PLACEHOLDER_TEMPLATE (t1)
1383 != CLASS_PLACEHOLDER_TEMPLATE (t2))
1384 return false;
1385 /* Constrained 'auto's are distinct from parms that don't have the same
1386 constraints. */
1387 if (!equivalent_placeholder_constraints (t1, t2))
1388 return false;
1389 break;
1391 case TYPENAME_TYPE:
1392 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1393 TYPENAME_TYPE_FULLNAME (t2)))
1394 return false;
1395 /* Qualifiers don't matter on scopes. */
1396 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1397 TYPE_CONTEXT (t2)))
1398 return false;
1399 break;
1401 case UNBOUND_CLASS_TEMPLATE:
1402 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1403 return false;
1404 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1405 return false;
1406 break;
1408 case COMPLEX_TYPE:
1409 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1410 return false;
1411 break;
1413 case VECTOR_TYPE:
1414 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1415 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1416 return false;
1417 break;
1419 case TYPE_PACK_EXPANSION:
1420 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1421 PACK_EXPANSION_PATTERN (t2))
1422 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1423 PACK_EXPANSION_EXTRA_ARGS (t2)));
1425 case DECLTYPE_TYPE:
1426 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1427 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1428 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1429 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1430 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1431 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1432 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1433 DECLTYPE_TYPE_EXPR (t2)))
1434 return false;
1435 break;
1437 case UNDERLYING_TYPE:
1438 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1439 UNDERLYING_TYPE_TYPE (t2));
1441 default:
1442 return false;
1445 /* If we get here, we know that from a target independent POV the
1446 types are the same. Make sure the target attributes are also
1447 the same. */
1448 return comp_type_attributes (t1, t2);
1451 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1452 is a bitwise-or of the COMPARE_* flags. */
1454 bool
1455 comptypes (tree t1, tree t2, int strict)
1457 if (strict == COMPARE_STRICT)
1459 if (t1 == t2)
1460 return true;
1462 if (t1 == error_mark_node || t2 == error_mark_node)
1463 return false;
1465 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1466 /* At least one of the types requires structural equality, so
1467 perform a deep check. */
1468 return structural_comptypes (t1, t2, strict);
1470 if (flag_checking && USE_CANONICAL_TYPES)
1472 bool result = structural_comptypes (t1, t2, strict);
1474 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1475 /* The two types are structurally equivalent, but their
1476 canonical types were different. This is a failure of the
1477 canonical type propagation code.*/
1478 internal_error
1479 ("canonical types differ for identical types %qT and %qT",
1480 t1, t2);
1481 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1482 /* Two types are structurally different, but the canonical
1483 types are the same. This means we were over-eager in
1484 assigning canonical types. */
1485 internal_error
1486 ("same canonical type node for different types %qT and %qT",
1487 t1, t2);
1489 return result;
1491 if (!flag_checking && USE_CANONICAL_TYPES)
1492 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1493 else
1494 return structural_comptypes (t1, t2, strict);
1496 else if (strict == COMPARE_STRUCTURAL)
1497 return structural_comptypes (t1, t2, COMPARE_STRICT);
1498 else
1499 return structural_comptypes (t1, t2, strict);
1502 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1503 top-level qualifiers. */
1505 bool
1506 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1508 if (type1 == error_mark_node || type2 == error_mark_node)
1509 return false;
1511 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1512 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1513 return same_type_p (type1, type2);
1516 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1518 bool
1519 at_least_as_qualified_p (const_tree type1, const_tree type2)
1521 int q1 = cp_type_quals (type1);
1522 int q2 = cp_type_quals (type2);
1524 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1525 return (q1 & q2) == q2;
1528 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1529 more cv-qualified that TYPE1, and 0 otherwise. */
1532 comp_cv_qualification (int q1, int q2)
1534 if (q1 == q2)
1535 return 0;
1537 if ((q1 & q2) == q2)
1538 return 1;
1539 else if ((q1 & q2) == q1)
1540 return -1;
1542 return 0;
1546 comp_cv_qualification (const_tree type1, const_tree type2)
1548 int q1 = cp_type_quals (type1);
1549 int q2 = cp_type_quals (type2);
1550 return comp_cv_qualification (q1, q2);
1553 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1554 subset of the cv-qualification signature of TYPE2, and the types
1555 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1558 comp_cv_qual_signature (tree type1, tree type2)
1560 if (comp_ptr_ttypes_real (type2, type1, -1))
1561 return 1;
1562 else if (comp_ptr_ttypes_real (type1, type2, -1))
1563 return -1;
1564 else
1565 return 0;
1568 /* Subroutines of `comptypes'. */
1570 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1571 equivalent in the sense that functions with those parameter types
1572 can have equivalent types. The two lists must be equivalent,
1573 element by element. */
1575 bool
1576 compparms (const_tree parms1, const_tree parms2)
1578 const_tree t1, t2;
1580 /* An unspecified parmlist matches any specified parmlist
1581 whose argument types don't need default promotions. */
1583 for (t1 = parms1, t2 = parms2;
1584 t1 || t2;
1585 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1587 /* If one parmlist is shorter than the other,
1588 they fail to match. */
1589 if (!t1 || !t2)
1590 return false;
1591 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1592 return false;
1594 return true;
1598 /* Process a sizeof or alignof expression where the operand is a
1599 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1600 or GNU (preferred alignment) semantics; it is ignored if op is
1601 SIZEOF_EXPR. */
1603 tree
1604 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool std_alignof,
1605 bool complain)
1607 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1608 if (type == error_mark_node)
1609 return error_mark_node;
1611 type = non_reference (type);
1612 if (TREE_CODE (type) == METHOD_TYPE)
1614 if (complain)
1616 pedwarn (input_location, OPT_Wpointer_arith,
1617 "invalid application of %qs to a member function",
1618 OVL_OP_INFO (false, op)->name);
1619 return size_one_node;
1621 else
1622 return error_mark_node;
1625 bool dependent_p = dependent_type_p (type);
1626 if (!dependent_p)
1627 complete_type (type);
1628 if (dependent_p
1629 /* VLA types will have a non-constant size. In the body of an
1630 uninstantiated template, we don't need to try to compute the
1631 value, because the sizeof expression is not an integral
1632 constant expression in that case. And, if we do try to
1633 compute the value, we'll likely end up with SAVE_EXPRs, which
1634 the template substitution machinery does not expect to see. */
1635 || (processing_template_decl
1636 && COMPLETE_TYPE_P (type)
1637 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1639 tree value = build_min (op, size_type_node, type);
1640 TREE_READONLY (value) = 1;
1641 if (op == ALIGNOF_EXPR && std_alignof)
1642 ALIGNOF_EXPR_STD_P (value) = true;
1643 return value;
1646 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1647 op == SIZEOF_EXPR, std_alignof,
1648 complain);
1651 /* Return the size of the type, without producing any warnings for
1652 types whose size cannot be taken. This routine should be used only
1653 in some other routine that has already produced a diagnostic about
1654 using the size of such a type. */
1655 tree
1656 cxx_sizeof_nowarn (tree type)
1658 if (TREE_CODE (type) == FUNCTION_TYPE
1659 || VOID_TYPE_P (type)
1660 || TREE_CODE (type) == ERROR_MARK)
1661 return size_one_node;
1662 else if (!COMPLETE_TYPE_P (type))
1663 return size_zero_node;
1664 else
1665 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false, false);
1668 /* Process a sizeof expression where the operand is an expression. */
1670 static tree
1671 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1673 if (e == error_mark_node)
1674 return error_mark_node;
1676 if (processing_template_decl)
1678 e = build_min (SIZEOF_EXPR, size_type_node, e);
1679 TREE_SIDE_EFFECTS (e) = 0;
1680 TREE_READONLY (e) = 1;
1682 return e;
1685 /* To get the size of a static data member declared as an array of
1686 unknown bound, we need to instantiate it. */
1687 if (VAR_P (e)
1688 && VAR_HAD_UNKNOWN_BOUND (e)
1689 && DECL_TEMPLATE_INSTANTIATION (e))
1690 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1692 if (TREE_CODE (e) == PARM_DECL
1693 && DECL_ARRAY_PARAMETER_P (e)
1694 && (complain & tf_warning))
1696 auto_diagnostic_group d;
1697 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1698 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1699 inform (DECL_SOURCE_LOCATION (e), "declared here");
1702 e = mark_type_use (e);
1704 if (bitfield_p (e))
1706 if (complain & tf_error)
1707 error ("invalid application of %<sizeof%> to a bit-field");
1708 else
1709 return error_mark_node;
1710 e = char_type_node;
1712 else if (is_overloaded_fn (e))
1714 if (complain & tf_error)
1715 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1716 "function type");
1717 else
1718 return error_mark_node;
1719 e = char_type_node;
1721 else if (type_unknown_p (e))
1723 if (complain & tf_error)
1724 cxx_incomplete_type_error (e, TREE_TYPE (e));
1725 else
1726 return error_mark_node;
1727 e = char_type_node;
1729 else
1730 e = TREE_TYPE (e);
1732 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, false, complain & tf_error);
1735 /* Implement the __alignof keyword: Return the minimum required
1736 alignment of E, measured in bytes. For VAR_DECL's and
1737 FIELD_DECL's return DECL_ALIGN (which can be set from an
1738 "aligned" __attribute__ specification). */
1740 static tree
1741 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1743 tree t;
1745 if (e == error_mark_node)
1746 return error_mark_node;
1748 if (processing_template_decl)
1750 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1751 TREE_SIDE_EFFECTS (e) = 0;
1752 TREE_READONLY (e) = 1;
1754 return e;
1757 e = mark_type_use (e);
1759 if (VAR_P (e))
1760 t = size_int (DECL_ALIGN_UNIT (e));
1761 else if (bitfield_p (e))
1763 if (complain & tf_error)
1764 error ("invalid application of %<__alignof%> to a bit-field");
1765 else
1766 return error_mark_node;
1767 t = size_one_node;
1769 else if (TREE_CODE (e) == COMPONENT_REF
1770 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1771 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1772 else if (is_overloaded_fn (e))
1774 if (complain & tf_error)
1775 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1776 "function type");
1777 else
1778 return error_mark_node;
1779 if (TREE_CODE (e) == FUNCTION_DECL)
1780 t = size_int (DECL_ALIGN_UNIT (e));
1781 else
1782 t = size_one_node;
1784 else if (type_unknown_p (e))
1786 if (complain & tf_error)
1787 cxx_incomplete_type_error (e, TREE_TYPE (e));
1788 else
1789 return error_mark_node;
1790 t = size_one_node;
1792 else
1793 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, false,
1794 complain & tf_error);
1796 return fold_convert (size_type_node, t);
1799 /* Process a sizeof or alignof expression E with code OP where the operand
1800 is an expression. */
1802 tree
1803 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1805 if (op == SIZEOF_EXPR)
1806 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1807 else
1808 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1811 /* Build a representation of an expression 'alignas(E).' Return the
1812 folded integer value of E if it is an integral constant expression
1813 that resolves to a valid alignment. If E depends on a template
1814 parameter, return a syntactic representation tree of kind
1815 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1816 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1818 tree
1819 cxx_alignas_expr (tree e)
1821 if (e == NULL_TREE || e == error_mark_node
1822 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1823 return e;
1825 if (TYPE_P (e))
1826 /* [dcl.align]/3:
1828 When the alignment-specifier is of the form
1829 alignas(type-id ), it shall have the same effect as
1830 alignas(alignof(type-id )). */
1832 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, true, false);
1834 /* If we reach this point, it means the alignas expression if of
1835 the form "alignas(assignment-expression)", so we should follow
1836 what is stated by [dcl.align]/2. */
1838 if (value_dependent_expression_p (e))
1839 /* Leave value-dependent expression alone for now. */
1840 return e;
1842 e = instantiate_non_dependent_expr (e);
1843 e = mark_rvalue_use (e);
1845 /* [dcl.align]/2 says:
1847 the assignment-expression shall be an integral constant
1848 expression. */
1850 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1852 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1853 return error_mark_node;
1856 return cxx_constant_value (e);
1860 /* EXPR is being used in a context that is not a function call.
1861 Enforce:
1863 [expr.ref]
1865 The expression can be used only as the left-hand operand of a
1866 member function call.
1868 [expr.mptr.operator]
1870 If the result of .* or ->* is a function, then that result can be
1871 used only as the operand for the function call operator ().
1873 by issuing an error message if appropriate. Returns true iff EXPR
1874 violates these rules. */
1876 bool
1877 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1879 if (expr == NULL_TREE)
1880 return false;
1881 /* Don't enforce this in MS mode. */
1882 if (flag_ms_extensions)
1883 return false;
1884 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1885 expr = get_first_fn (expr);
1886 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1888 if (complain & tf_error)
1890 if (DECL_P (expr))
1892 error_at (loc, "invalid use of non-static member function %qD",
1893 expr);
1894 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1896 else
1897 error_at (loc, "invalid use of non-static member function of "
1898 "type %qT", TREE_TYPE (expr));
1900 return true;
1902 return false;
1905 /* If EXP is a reference to a bitfield, and the type of EXP does not
1906 match the declared type of the bitfield, return the declared type
1907 of the bitfield. Otherwise, return NULL_TREE. */
1909 tree
1910 is_bitfield_expr_with_lowered_type (const_tree exp)
1912 switch (TREE_CODE (exp))
1914 case COND_EXPR:
1915 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1916 ? TREE_OPERAND (exp, 1)
1917 : TREE_OPERAND (exp, 0)))
1918 return NULL_TREE;
1919 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1921 case COMPOUND_EXPR:
1922 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1924 case MODIFY_EXPR:
1925 case SAVE_EXPR:
1926 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1928 case COMPONENT_REF:
1930 tree field;
1932 field = TREE_OPERAND (exp, 1);
1933 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1934 return NULL_TREE;
1935 if (same_type_ignoring_top_level_qualifiers_p
1936 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1937 return NULL_TREE;
1938 return DECL_BIT_FIELD_TYPE (field);
1941 case VAR_DECL:
1942 if (DECL_HAS_VALUE_EXPR_P (exp))
1943 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1944 (CONST_CAST_TREE (exp)));
1945 return NULL_TREE;
1947 CASE_CONVERT:
1948 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1949 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1950 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1951 /* Fallthrough. */
1953 default:
1954 return NULL_TREE;
1958 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1959 bitfield with a lowered type, the type of EXP is returned, rather
1960 than NULL_TREE. */
1962 tree
1963 unlowered_expr_type (const_tree exp)
1965 tree type;
1966 tree etype = TREE_TYPE (exp);
1968 type = is_bitfield_expr_with_lowered_type (exp);
1969 if (type)
1970 type = cp_build_qualified_type (type, cp_type_quals (etype));
1971 else
1972 type = etype;
1974 return type;
1977 /* Perform the conversions in [expr] that apply when an lvalue appears
1978 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1979 function-to-pointer conversions. In addition, bitfield references are
1980 converted to their declared types. Note that this function does not perform
1981 the lvalue-to-rvalue conversion for class types. If you need that conversion
1982 for class types, then you probably need to use force_rvalue.
1984 Although the returned value is being used as an rvalue, this
1985 function does not wrap the returned expression in a
1986 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1987 that the return value is no longer an lvalue. */
1989 tree
1990 decay_conversion (tree exp,
1991 tsubst_flags_t complain,
1992 bool reject_builtin /* = true */)
1994 tree type;
1995 enum tree_code code;
1996 location_t loc = cp_expr_loc_or_loc (exp, input_location);
1998 type = TREE_TYPE (exp);
1999 if (type == error_mark_node)
2000 return error_mark_node;
2002 exp = resolve_nondeduced_context (exp, complain);
2003 if (type_unknown_p (exp))
2005 if (complain & tf_error)
2006 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
2007 return error_mark_node;
2010 code = TREE_CODE (type);
2012 if (error_operand_p (exp))
2013 return error_mark_node;
2015 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2017 mark_rvalue_use (exp, loc, reject_builtin);
2018 return nullptr_node;
2021 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2022 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2023 if (code == VOID_TYPE)
2025 if (complain & tf_error)
2026 error_at (loc, "void value not ignored as it ought to be");
2027 return error_mark_node;
2029 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2030 return error_mark_node;
2031 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2033 exp = mark_lvalue_use (exp);
2034 if (reject_builtin && reject_gcc_builtin (exp, loc))
2035 return error_mark_node;
2036 return cp_build_addr_expr (exp, complain);
2038 if (code == ARRAY_TYPE)
2040 tree adr;
2041 tree ptrtype;
2043 exp = mark_lvalue_use (exp);
2045 if (INDIRECT_REF_P (exp))
2046 return build_nop (build_pointer_type (TREE_TYPE (type)),
2047 TREE_OPERAND (exp, 0));
2049 if (TREE_CODE (exp) == COMPOUND_EXPR)
2051 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2052 if (op1 == error_mark_node)
2053 return error_mark_node;
2054 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2055 TREE_OPERAND (exp, 0), op1);
2058 if (!obvalue_p (exp)
2059 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2061 if (complain & tf_error)
2062 error_at (loc, "invalid use of non-lvalue array");
2063 return error_mark_node;
2066 /* Don't let an array compound literal decay to a pointer. It can
2067 still be used to initialize an array or bind to a reference. */
2068 if (TREE_CODE (exp) == TARGET_EXPR)
2070 if (complain & tf_error)
2071 error_at (loc, "taking address of temporary array");
2072 return error_mark_node;
2075 ptrtype = build_pointer_type (TREE_TYPE (type));
2077 if (VAR_P (exp))
2079 if (!cxx_mark_addressable (exp))
2080 return error_mark_node;
2081 adr = build_nop (ptrtype, build_address (exp));
2082 return adr;
2084 /* This way is better for a COMPONENT_REF since it can
2085 simplify the offset for a component. */
2086 adr = cp_build_addr_expr (exp, complain);
2087 return cp_convert (ptrtype, adr, complain);
2090 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2091 exp = mark_rvalue_use (exp, loc, reject_builtin);
2093 /* If a bitfield is used in a context where integral promotion
2094 applies, then the caller is expected to have used
2095 default_conversion. That function promotes bitfields correctly
2096 before calling this function. At this point, if we have a
2097 bitfield referenced, we may assume that is not subject to
2098 promotion, and that, therefore, the type of the resulting rvalue
2099 is the declared type of the bitfield. */
2100 exp = convert_bitfield_to_declared_type (exp);
2102 /* We do not call rvalue() here because we do not want to wrap EXP
2103 in a NON_LVALUE_EXPR. */
2105 /* [basic.lval]
2107 Non-class rvalues always have cv-unqualified types. */
2108 type = TREE_TYPE (exp);
2109 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2110 exp = build_nop (cv_unqualified (type), exp);
2112 if (!complete_type_or_maybe_complain (type, exp, complain))
2113 return error_mark_node;
2115 return exp;
2118 /* Perform preparatory conversions, as part of the "usual arithmetic
2119 conversions". In particular, as per [expr]:
2121 Whenever an lvalue expression appears as an operand of an
2122 operator that expects the rvalue for that operand, the
2123 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2124 standard conversions are applied to convert the expression to an
2125 rvalue.
2127 In addition, we perform integral promotions here, as those are
2128 applied to both operands to a binary operator before determining
2129 what additional conversions should apply. */
2131 static tree
2132 cp_default_conversion (tree exp, tsubst_flags_t complain)
2134 /* Check for target-specific promotions. */
2135 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2136 if (promoted_type)
2137 exp = cp_convert (promoted_type, exp, complain);
2138 /* Perform the integral promotions first so that bitfield
2139 expressions (which may promote to "int", even if the bitfield is
2140 declared "unsigned") are promoted correctly. */
2141 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2142 exp = cp_perform_integral_promotions (exp, complain);
2143 /* Perform the other conversions. */
2144 exp = decay_conversion (exp, complain);
2146 return exp;
2149 /* C version. */
2151 tree
2152 default_conversion (tree exp)
2154 return cp_default_conversion (exp, tf_warning_or_error);
2157 /* EXPR is an expression with an integral or enumeration type.
2158 Perform the integral promotions in [conv.prom], and return the
2159 converted value. */
2161 tree
2162 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2164 tree type;
2165 tree promoted_type;
2167 expr = mark_rvalue_use (expr);
2168 if (error_operand_p (expr))
2169 return error_mark_node;
2171 /* [conv.prom]
2173 If the bitfield has an enumerated type, it is treated as any
2174 other value of that type for promotion purposes. */
2175 type = is_bitfield_expr_with_lowered_type (expr);
2176 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2177 type = TREE_TYPE (expr);
2178 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2179 /* Scoped enums don't promote. */
2180 if (SCOPED_ENUM_P (type))
2181 return expr;
2182 promoted_type = type_promotes_to (type);
2183 if (type != promoted_type)
2184 expr = cp_convert (promoted_type, expr, complain);
2185 return expr;
2188 /* C version. */
2190 tree
2191 perform_integral_promotions (tree expr)
2193 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2196 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2197 decay_conversion to one. */
2200 string_conv_p (const_tree totype, const_tree exp, int warn)
2202 tree t;
2204 if (!TYPE_PTR_P (totype))
2205 return 0;
2207 t = TREE_TYPE (totype);
2208 if (!same_type_p (t, char_type_node)
2209 && !same_type_p (t, char16_type_node)
2210 && !same_type_p (t, char32_type_node)
2211 && !same_type_p (t, wchar_type_node))
2212 return 0;
2214 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
2216 STRIP_ANY_LOCATION_WRAPPER (exp);
2218 if (TREE_CODE (exp) == STRING_CST)
2220 /* Make sure that we don't try to convert between char and wide chars. */
2221 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2222 return 0;
2224 else
2226 /* Is this a string constant which has decayed to 'const char *'? */
2227 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2228 if (!same_type_p (TREE_TYPE (exp), t))
2229 return 0;
2230 STRIP_NOPS (exp);
2231 if (TREE_CODE (exp) != ADDR_EXPR
2232 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2233 return 0;
2235 if (warn)
2237 if (cxx_dialect >= cxx11)
2238 pedwarn (loc, OPT_Wwrite_strings,
2239 "ISO C++ forbids converting a string constant to %qT",
2240 totype);
2241 else
2242 warning_at (loc, OPT_Wwrite_strings,
2243 "deprecated conversion from string constant to %qT",
2244 totype);
2247 return 1;
2250 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2251 can, for example, use as an lvalue. This code used to be in
2252 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2253 expressions, where we're dealing with aggregates. But now it's again only
2254 called from unary_complex_lvalue. The case (in particular) that led to
2255 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2256 get it there. */
2258 static tree
2259 rationalize_conditional_expr (enum tree_code code, tree t,
2260 tsubst_flags_t complain)
2262 location_t loc = cp_expr_loc_or_loc (t, input_location);
2264 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2265 the first operand is always the one to be used if both operands
2266 are equal, so we know what conditional expression this used to be. */
2267 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2269 tree op0 = TREE_OPERAND (t, 0);
2270 tree op1 = TREE_OPERAND (t, 1);
2272 /* The following code is incorrect if either operand side-effects. */
2273 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2274 && !TREE_SIDE_EFFECTS (op1));
2275 return
2276 build_conditional_expr (loc,
2277 build_x_binary_op (loc,
2278 (TREE_CODE (t) == MIN_EXPR
2279 ? LE_EXPR : GE_EXPR),
2280 op0, TREE_CODE (op0),
2281 op1, TREE_CODE (op1),
2282 /*overload=*/NULL,
2283 complain),
2284 cp_build_unary_op (code, op0, false, complain),
2285 cp_build_unary_op (code, op1, false, complain),
2286 complain);
2289 return
2290 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2291 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2292 complain),
2293 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2294 complain),
2295 complain);
2298 /* Given the TYPE of an anonymous union field inside T, return the
2299 FIELD_DECL for the field. If not found return NULL_TREE. Because
2300 anonymous unions can nest, we must also search all anonymous unions
2301 that are directly reachable. */
2303 tree
2304 lookup_anon_field (tree t, tree type)
2306 tree field;
2308 t = TYPE_MAIN_VARIANT (t);
2310 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2312 if (TREE_STATIC (field))
2313 continue;
2314 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2315 continue;
2317 /* If we find it directly, return the field. */
2318 if (DECL_NAME (field) == NULL_TREE
2319 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2321 return field;
2324 /* Otherwise, it could be nested, search harder. */
2325 if (DECL_NAME (field) == NULL_TREE
2326 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2328 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2329 if (subfield)
2330 return subfield;
2333 return NULL_TREE;
2336 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2337 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2338 non-NULL, it indicates the path to the base used to name MEMBER.
2339 If PRESERVE_REFERENCE is true, the expression returned will have
2340 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2341 returned will have the type referred to by the reference.
2343 This function does not perform access control; that is either done
2344 earlier by the parser when the name of MEMBER is resolved to MEMBER
2345 itself, or later when overload resolution selects one of the
2346 functions indicated by MEMBER. */
2348 tree
2349 build_class_member_access_expr (cp_expr object, tree member,
2350 tree access_path, bool preserve_reference,
2351 tsubst_flags_t complain)
2353 tree object_type;
2354 tree member_scope;
2355 tree result = NULL_TREE;
2356 tree using_decl = NULL_TREE;
2358 if (error_operand_p (object) || error_operand_p (member))
2359 return error_mark_node;
2361 gcc_assert (DECL_P (member) || BASELINK_P (member));
2363 /* [expr.ref]
2365 The type of the first expression shall be "class object" (of a
2366 complete type). */
2367 object_type = TREE_TYPE (object);
2368 if (!currently_open_class (object_type)
2369 && !complete_type_or_maybe_complain (object_type, object, complain))
2370 return error_mark_node;
2371 if (!CLASS_TYPE_P (object_type))
2373 if (complain & tf_error)
2375 if (INDIRECT_TYPE_P (object_type)
2376 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2377 error ("request for member %qD in %qE, which is of pointer "
2378 "type %qT (maybe you meant to use %<->%> ?)",
2379 member, object.get_value (), object_type);
2380 else
2381 error ("request for member %qD in %qE, which is of non-class "
2382 "type %qT", member, object.get_value (), object_type);
2384 return error_mark_node;
2387 /* The standard does not seem to actually say that MEMBER must be a
2388 member of OBJECT_TYPE. However, that is clearly what is
2389 intended. */
2390 if (DECL_P (member))
2392 member_scope = DECL_CLASS_CONTEXT (member);
2393 if (!mark_used (member, complain) && !(complain & tf_error))
2394 return error_mark_node;
2395 if (TREE_DEPRECATED (member))
2396 warn_deprecated_use (member, NULL_TREE);
2398 else
2399 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2400 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2401 presently be the anonymous union. Go outwards until we find a
2402 type related to OBJECT_TYPE. */
2403 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2404 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2405 object_type))
2406 member_scope = TYPE_CONTEXT (member_scope);
2407 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2409 if (complain & tf_error)
2411 if (TREE_CODE (member) == FIELD_DECL)
2412 error ("invalid use of nonstatic data member %qE", member);
2413 else
2414 error ("%qD is not a member of %qT", member, object_type);
2416 return error_mark_node;
2419 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2420 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2421 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2423 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2424 if (temp)
2425 object = cp_build_fold_indirect_ref (temp);
2428 /* In [expr.ref], there is an explicit list of the valid choices for
2429 MEMBER. We check for each of those cases here. */
2430 if (VAR_P (member))
2432 /* A static data member. */
2433 result = member;
2434 mark_exp_read (object);
2435 /* If OBJECT has side-effects, they are supposed to occur. */
2436 if (TREE_SIDE_EFFECTS (object))
2437 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2439 else if (TREE_CODE (member) == FIELD_DECL)
2441 /* A non-static data member. */
2442 bool null_object_p;
2443 int type_quals;
2444 tree member_type;
2446 if (INDIRECT_REF_P (object))
2447 null_object_p =
2448 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2449 else
2450 null_object_p = false;
2452 /* Convert OBJECT to the type of MEMBER. */
2453 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2454 TYPE_MAIN_VARIANT (member_scope)))
2456 tree binfo;
2457 base_kind kind;
2459 binfo = lookup_base (access_path ? access_path : object_type,
2460 member_scope, ba_unique, &kind, complain);
2461 if (binfo == error_mark_node)
2462 return error_mark_node;
2464 /* It is invalid to try to get to a virtual base of a
2465 NULL object. The most common cause is invalid use of
2466 offsetof macro. */
2467 if (null_object_p && kind == bk_via_virtual)
2469 if (complain & tf_error)
2471 error ("invalid access to non-static data member %qD in "
2472 "virtual base of NULL object", member);
2474 return error_mark_node;
2477 /* Convert to the base. */
2478 object = build_base_path (PLUS_EXPR, object, binfo,
2479 /*nonnull=*/1, complain);
2480 /* If we found the base successfully then we should be able
2481 to convert to it successfully. */
2482 gcc_assert (object != error_mark_node);
2485 /* If MEMBER is from an anonymous aggregate, we have converted
2486 OBJECT so that it refers to the class containing the
2487 anonymous union. Generate a reference to the anonymous union
2488 itself, and recur to find MEMBER. */
2489 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2490 /* When this code is called from build_field_call, the
2491 object already has the type of the anonymous union.
2492 That is because the COMPONENT_REF was already
2493 constructed, and was then disassembled before calling
2494 build_field_call. After the function-call code is
2495 cleaned up, this waste can be eliminated. */
2496 && (!same_type_ignoring_top_level_qualifiers_p
2497 (TREE_TYPE (object), DECL_CONTEXT (member))))
2499 tree anonymous_union;
2501 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2502 DECL_CONTEXT (member));
2503 object = build_class_member_access_expr (object,
2504 anonymous_union,
2505 /*access_path=*/NULL_TREE,
2506 preserve_reference,
2507 complain);
2510 /* Compute the type of the field, as described in [expr.ref]. */
2511 type_quals = TYPE_UNQUALIFIED;
2512 member_type = TREE_TYPE (member);
2513 if (!TYPE_REF_P (member_type))
2515 type_quals = (cp_type_quals (member_type)
2516 | cp_type_quals (object_type));
2518 /* A field is const (volatile) if the enclosing object, or the
2519 field itself, is const (volatile). But, a mutable field is
2520 not const, even within a const object. */
2521 if (DECL_MUTABLE_P (member))
2522 type_quals &= ~TYPE_QUAL_CONST;
2523 member_type = cp_build_qualified_type (member_type, type_quals);
2526 result = build3_loc (input_location, COMPONENT_REF, member_type,
2527 object, member, NULL_TREE);
2529 /* Mark the expression const or volatile, as appropriate. Even
2530 though we've dealt with the type above, we still have to mark the
2531 expression itself. */
2532 if (type_quals & TYPE_QUAL_CONST)
2533 TREE_READONLY (result) = 1;
2534 if (type_quals & TYPE_QUAL_VOLATILE)
2535 TREE_THIS_VOLATILE (result) = 1;
2537 else if (BASELINK_P (member))
2539 /* The member is a (possibly overloaded) member function. */
2540 tree functions;
2541 tree type;
2543 /* If the MEMBER is exactly one static member function, then we
2544 know the type of the expression. Otherwise, we must wait
2545 until overload resolution has been performed. */
2546 functions = BASELINK_FUNCTIONS (member);
2547 if (TREE_CODE (functions) == FUNCTION_DECL
2548 && DECL_STATIC_FUNCTION_P (functions))
2549 type = TREE_TYPE (functions);
2550 else
2551 type = unknown_type_node;
2552 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2553 base. That will happen when the function is called. */
2554 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2556 else if (TREE_CODE (member) == CONST_DECL)
2558 /* The member is an enumerator. */
2559 result = member;
2560 /* If OBJECT has side-effects, they are supposed to occur. */
2561 if (TREE_SIDE_EFFECTS (object))
2562 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2563 object, result);
2565 else if ((using_decl = strip_using_decl (member)) != member)
2566 result = build_class_member_access_expr (object,
2567 using_decl,
2568 access_path, preserve_reference,
2569 complain);
2570 else
2572 if (complain & tf_error)
2573 error ("invalid use of %qD", member);
2574 return error_mark_node;
2577 if (!preserve_reference)
2578 /* [expr.ref]
2580 If E2 is declared to have type "reference to T", then ... the
2581 type of E1.E2 is T. */
2582 result = convert_from_reference (result);
2584 return result;
2587 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2588 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2590 static tree
2591 lookup_destructor (tree object, tree scope, tree dtor_name,
2592 tsubst_flags_t complain)
2594 tree object_type = TREE_TYPE (object);
2595 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2596 tree expr;
2598 /* We've already complained about this destructor. */
2599 if (dtor_type == error_mark_node)
2600 return error_mark_node;
2602 if (scope && !check_dtor_name (scope, dtor_type))
2604 if (complain & tf_error)
2605 error ("qualified type %qT does not match destructor name ~%qT",
2606 scope, dtor_type);
2607 return error_mark_node;
2609 if (is_auto (dtor_type))
2610 dtor_type = object_type;
2611 else if (identifier_p (dtor_type))
2613 /* In a template, names we can't find a match for are still accepted
2614 destructor names, and we check them here. */
2615 if (check_dtor_name (object_type, dtor_type))
2616 dtor_type = object_type;
2617 else
2619 if (complain & tf_error)
2620 error ("object type %qT does not match destructor name ~%qT",
2621 object_type, dtor_type);
2622 return error_mark_node;
2626 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2628 if (complain & tf_error)
2629 error ("the type being destroyed is %qT, but the destructor "
2630 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2631 return error_mark_node;
2633 expr = lookup_member (dtor_type, complete_dtor_identifier,
2634 /*protect=*/1, /*want_type=*/false,
2635 tf_warning_or_error);
2636 if (!expr)
2638 if (complain & tf_error)
2639 cxx_incomplete_type_error (dtor_name, dtor_type);
2640 return error_mark_node;
2642 expr = (adjust_result_of_qualified_name_lookup
2643 (expr, dtor_type, object_type));
2644 if (scope == NULL_TREE)
2645 /* We need to call adjust_result_of_qualified_name_lookup in case the
2646 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2647 that we still get virtual function binding. */
2648 BASELINK_QUALIFIED_P (expr) = false;
2649 return expr;
2652 /* An expression of the form "A::template B" has been resolved to
2653 DECL. Issue a diagnostic if B is not a template or template
2654 specialization. */
2656 void
2657 check_template_keyword (tree decl)
2659 /* The standard says:
2661 [temp.names]
2663 If a name prefixed by the keyword template is not a member
2664 template, the program is ill-formed.
2666 DR 228 removed the restriction that the template be a member
2667 template.
2669 DR 96, if accepted would add the further restriction that explicit
2670 template arguments must be provided if the template keyword is
2671 used, but, as of 2005-10-16, that DR is still in "drafting". If
2672 this DR is accepted, then the semantic checks here can be
2673 simplified, as the entity named must in fact be a template
2674 specialization, rather than, as at present, a set of overloaded
2675 functions containing at least one template function. */
2676 if (TREE_CODE (decl) != TEMPLATE_DECL
2677 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2679 if (VAR_P (decl))
2681 if (DECL_USE_TEMPLATE (decl)
2682 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2684 else
2685 permerror (input_location, "%qD is not a template", decl);
2687 else if (!is_overloaded_fn (decl))
2688 permerror (input_location, "%qD is not a template", decl);
2689 else
2691 bool found = false;
2693 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2694 !found && iter; ++iter)
2696 tree fn = *iter;
2697 if (TREE_CODE (fn) == TEMPLATE_DECL
2698 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2699 || (TREE_CODE (fn) == FUNCTION_DECL
2700 && DECL_USE_TEMPLATE (fn)
2701 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2702 found = true;
2704 if (!found)
2705 permerror (input_location, "%qD is not a template", decl);
2710 /* Record that an access failure occurred on BASETYPE_PATH attempting
2711 to access FIELD_DECL. */
2713 void
2714 access_failure_info::record_access_failure (tree basetype_path,
2715 tree field_decl)
2717 m_was_inaccessible = true;
2718 m_basetype_path = basetype_path;
2719 m_field_decl = field_decl;
2722 /* If an access failure was recorded, then attempt to locate an
2723 accessor function for the pertinent field, and if one is
2724 available, add a note and fix-it hint suggesting using it. */
2726 void
2727 access_failure_info::maybe_suggest_accessor (bool const_p) const
2729 if (!m_was_inaccessible)
2730 return;
2732 tree accessor
2733 = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2734 if (!accessor)
2735 return;
2737 /* The accessor must itself be accessible for it to be a reasonable
2738 suggestion. */
2739 if (!accessible_p (m_basetype_path, accessor, true))
2740 return;
2742 rich_location richloc (line_table, input_location);
2743 pretty_printer pp;
2744 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2745 richloc.add_fixit_replace (pp_formatted_text (&pp));
2746 inform (&richloc, "field %q#D can be accessed via %q#D",
2747 m_field_decl, accessor);
2750 /* This function is called by the parser to process a class member
2751 access expression of the form OBJECT.NAME. NAME is a node used by
2752 the parser to represent a name; it is not yet a DECL. It may,
2753 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2754 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2755 there is no reason to do the lookup twice, so the parser keeps the
2756 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2757 be a template via the use of the "A::template B" syntax. */
2759 tree
2760 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2761 tsubst_flags_t complain)
2763 tree expr;
2764 tree object_type;
2765 tree member;
2766 tree access_path = NULL_TREE;
2767 tree orig_object = object;
2768 tree orig_name = name;
2770 if (object == error_mark_node || name == error_mark_node)
2771 return error_mark_node;
2773 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2774 if (!objc_is_public (object, name))
2775 return error_mark_node;
2777 object_type = TREE_TYPE (object);
2779 if (processing_template_decl)
2781 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2782 type_dependent_object_expression_p (object)
2783 /* If NAME is "f<args>", where either 'f' or 'args' is
2784 dependent, then the expression is dependent. */
2785 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2786 && dependent_template_id_p (TREE_OPERAND (name, 0),
2787 TREE_OPERAND (name, 1)))
2788 /* If NAME is "T::X" where "T" is dependent, then the
2789 expression is dependent. */
2790 || (TREE_CODE (name) == SCOPE_REF
2791 && TYPE_P (TREE_OPERAND (name, 0))
2792 && dependent_scope_p (TREE_OPERAND (name, 0))))
2794 dependent:
2795 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2796 orig_object, orig_name, NULL_TREE);
2798 object = build_non_dependent_expr (object);
2800 else if (c_dialect_objc ()
2801 && identifier_p (name)
2802 && (expr = objc_maybe_build_component_ref (object, name)))
2803 return expr;
2805 /* [expr.ref]
2807 The type of the first expression shall be "class object" (of a
2808 complete type). */
2809 if (!currently_open_class (object_type)
2810 && !complete_type_or_maybe_complain (object_type, object, complain))
2811 return error_mark_node;
2812 if (!CLASS_TYPE_P (object_type))
2814 if (complain & tf_error)
2816 if (INDIRECT_TYPE_P (object_type)
2817 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2818 error ("request for member %qD in %qE, which is of pointer "
2819 "type %qT (maybe you meant to use %<->%> ?)",
2820 name, object.get_value (), object_type);
2821 else
2822 error ("request for member %qD in %qE, which is of non-class "
2823 "type %qT", name, object.get_value (), object_type);
2825 return error_mark_node;
2828 if (BASELINK_P (name))
2829 /* A member function that has already been looked up. */
2830 member = name;
2831 else
2833 bool is_template_id = false;
2834 tree template_args = NULL_TREE;
2835 tree scope = NULL_TREE;
2837 access_path = object_type;
2839 if (TREE_CODE (name) == SCOPE_REF)
2841 /* A qualified name. The qualifying class or namespace `S'
2842 has already been looked up; it is either a TYPE or a
2843 NAMESPACE_DECL. */
2844 scope = TREE_OPERAND (name, 0);
2845 name = TREE_OPERAND (name, 1);
2847 /* If SCOPE is a namespace, then the qualified name does not
2848 name a member of OBJECT_TYPE. */
2849 if (TREE_CODE (scope) == NAMESPACE_DECL)
2851 if (complain & tf_error)
2852 error ("%<%D::%D%> is not a member of %qT",
2853 scope, name, object_type);
2854 return error_mark_node;
2858 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2860 is_template_id = true;
2861 template_args = TREE_OPERAND (name, 1);
2862 name = TREE_OPERAND (name, 0);
2864 if (!identifier_p (name))
2865 name = OVL_NAME (name);
2868 if (scope)
2870 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2872 gcc_assert (!is_template_id);
2873 /* Looking up a member enumerator (c++/56793). */
2874 if (!TYPE_CLASS_SCOPE_P (scope)
2875 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2877 if (complain & tf_error)
2878 error ("%<%D::%D%> is not a member of %qT",
2879 scope, name, object_type);
2880 return error_mark_node;
2882 tree val = lookup_enumerator (scope, name);
2883 if (!val)
2885 if (complain & tf_error)
2886 error ("%qD is not a member of %qD",
2887 name, scope);
2888 return error_mark_node;
2891 if (TREE_SIDE_EFFECTS (object))
2892 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2893 return val;
2896 gcc_assert (CLASS_TYPE_P (scope));
2897 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2899 if (constructor_name_p (name, scope))
2901 if (complain & tf_error)
2902 error ("cannot call constructor %<%T::%D%> directly",
2903 scope, name);
2904 return error_mark_node;
2907 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2908 access_path = lookup_base (object_type, scope, ba_check,
2909 NULL, complain);
2910 if (access_path == error_mark_node)
2911 return error_mark_node;
2912 if (!access_path)
2914 if (any_dependent_bases_p (object_type))
2915 goto dependent;
2916 if (complain & tf_error)
2917 error ("%qT is not a base of %qT", scope, object_type);
2918 return error_mark_node;
2922 if (TREE_CODE (name) == BIT_NOT_EXPR)
2924 if (dependent_type_p (object_type))
2925 /* The destructor isn't declared yet. */
2926 goto dependent;
2927 member = lookup_destructor (object, scope, name, complain);
2929 else
2931 /* Look up the member. */
2932 access_failure_info afi;
2933 member = lookup_member (access_path, name, /*protect=*/1,
2934 /*want_type=*/false, complain,
2935 &afi);
2936 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2937 if (member == NULL_TREE)
2939 if (dependent_type_p (object_type))
2940 /* Try again at instantiation time. */
2941 goto dependent;
2942 if (complain & tf_error)
2944 tree guessed_id = lookup_member_fuzzy (access_path, name,
2945 /*want_type=*/false);
2946 if (guessed_id)
2948 location_t bogus_component_loc = input_location;
2949 gcc_rich_location rich_loc (bogus_component_loc);
2950 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2951 guessed_id);
2952 error_at (&rich_loc,
2953 "%q#T has no member named %qE;"
2954 " did you mean %qE?",
2955 TREE_CODE (access_path) == TREE_BINFO
2956 ? TREE_TYPE (access_path) : object_type,
2957 name, guessed_id);
2959 else
2960 error ("%q#T has no member named %qE",
2961 TREE_CODE (access_path) == TREE_BINFO
2962 ? TREE_TYPE (access_path) : object_type, name);
2964 return error_mark_node;
2966 if (member == error_mark_node)
2967 return error_mark_node;
2968 if (DECL_P (member)
2969 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2970 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2971 wrong, so don't use it. */
2972 goto dependent;
2973 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2974 goto dependent;
2977 if (is_template_id)
2979 tree templ = member;
2981 if (BASELINK_P (templ))
2982 member = lookup_template_function (templ, template_args);
2983 else if (variable_template_p (templ))
2984 member = (lookup_and_finish_template_variable
2985 (templ, template_args, complain));
2986 else
2988 if (complain & tf_error)
2989 error ("%qD is not a member template function", name);
2990 return error_mark_node;
2995 if (TREE_DEPRECATED (member))
2996 warn_deprecated_use (member, NULL_TREE);
2998 if (template_p)
2999 check_template_keyword (member);
3001 expr = build_class_member_access_expr (object, member, access_path,
3002 /*preserve_reference=*/false,
3003 complain);
3004 if (processing_template_decl && expr != error_mark_node)
3006 if (BASELINK_P (member))
3008 if (TREE_CODE (orig_name) == SCOPE_REF)
3009 BASELINK_QUALIFIED_P (member) = 1;
3010 orig_name = member;
3012 return build_min_non_dep (COMPONENT_REF, expr,
3013 orig_object, orig_name,
3014 NULL_TREE);
3017 return expr;
3020 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3021 type. */
3023 tree
3024 build_simple_component_ref (tree object, tree member)
3026 tree type = cp_build_qualified_type (TREE_TYPE (member),
3027 cp_type_quals (TREE_TYPE (object)));
3028 return build3_loc (input_location,
3029 COMPONENT_REF, type,
3030 object, member, NULL_TREE);
3033 /* Return an expression for the MEMBER_NAME field in the internal
3034 representation of PTRMEM, a pointer-to-member function. (Each
3035 pointer-to-member function type gets its own RECORD_TYPE so it is
3036 more convenient to access the fields by name than by FIELD_DECL.)
3037 This routine converts the NAME to a FIELD_DECL and then creates the
3038 node for the complete expression. */
3040 tree
3041 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3043 tree ptrmem_type;
3044 tree member;
3046 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3048 unsigned int ix;
3049 tree index, value;
3050 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem),
3051 ix, index, value)
3052 if (index && DECL_P (index) && DECL_NAME (index) == member_name)
3053 return value;
3054 gcc_unreachable ();
3057 /* This code is a stripped down version of
3058 build_class_member_access_expr. It does not work to use that
3059 routine directly because it expects the object to be of class
3060 type. */
3061 ptrmem_type = TREE_TYPE (ptrmem);
3062 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3063 for (member = TYPE_FIELDS (ptrmem_type); member;
3064 member = DECL_CHAIN (member))
3065 if (DECL_NAME (member) == member_name)
3066 break;
3067 tree res = build_simple_component_ref (ptrmem, member);
3069 TREE_NO_WARNING (res) = 1;
3070 return res;
3073 /* Given an expression PTR for a pointer, return an expression
3074 for the value pointed to.
3075 ERRORSTRING is the name of the operator to appear in error messages.
3077 This function may need to overload OPERATOR_FNNAME.
3078 Must also handle REFERENCE_TYPEs for C++. */
3080 tree
3081 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3082 tsubst_flags_t complain)
3084 tree orig_expr = expr;
3085 tree rval;
3086 tree overload = NULL_TREE;
3088 if (processing_template_decl)
3090 /* Retain the type if we know the operand is a pointer. */
3091 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3092 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3093 if (type_dependent_expression_p (expr))
3094 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3095 expr = build_non_dependent_expr (expr);
3098 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3099 NULL_TREE, NULL_TREE, &overload, complain);
3100 if (!rval)
3101 rval = cp_build_indirect_ref (expr, errorstring, complain);
3103 if (processing_template_decl && rval != error_mark_node)
3105 if (overload != NULL_TREE)
3106 return (build_min_non_dep_op_overload
3107 (INDIRECT_REF, rval, overload, orig_expr));
3109 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3111 else
3112 return rval;
3115 /* The implementation of the above, and of indirection implied by other
3116 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3118 static tree
3119 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3120 tsubst_flags_t complain, bool do_fold)
3122 tree pointer, type;
3124 /* RO_NULL should only be used with the folding entry points below, not
3125 cp_build_indirect_ref. */
3126 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3128 if (ptr == current_class_ptr
3129 || (TREE_CODE (ptr) == NOP_EXPR
3130 && TREE_OPERAND (ptr, 0) == current_class_ptr
3131 && (same_type_ignoring_top_level_qualifiers_p
3132 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3133 return current_class_ref;
3135 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3136 ? ptr : decay_conversion (ptr, complain));
3137 if (pointer == error_mark_node)
3138 return error_mark_node;
3140 type = TREE_TYPE (pointer);
3142 if (INDIRECT_TYPE_P (type))
3144 /* [expr.unary.op]
3146 If the type of the expression is "pointer to T," the type
3147 of the result is "T." */
3148 tree t = TREE_TYPE (type);
3150 if ((CONVERT_EXPR_P (ptr)
3151 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3152 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3154 /* If a warning is issued, mark it to avoid duplicates from
3155 the backend. This only needs to be done at
3156 warn_strict_aliasing > 2. */
3157 if (warn_strict_aliasing > 2)
3158 if (strict_aliasing_warning (EXPR_LOCATION (ptr),
3159 type, TREE_OPERAND (ptr, 0)))
3160 TREE_NO_WARNING (ptr) = 1;
3163 if (VOID_TYPE_P (t))
3165 /* A pointer to incomplete type (other than cv void) can be
3166 dereferenced [expr.unary.op]/1 */
3167 if (complain & tf_error)
3168 error ("%qT is not a pointer-to-object type", type);
3169 return error_mark_node;
3171 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3172 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3173 /* The POINTER was something like `&x'. We simplify `*&x' to
3174 `x'. */
3175 return TREE_OPERAND (pointer, 0);
3176 else
3178 tree ref = build1 (INDIRECT_REF, t, pointer);
3180 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3181 so that we get the proper error message if the result is used
3182 to assign to. Also, &* is supposed to be a no-op. */
3183 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3184 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3185 TREE_SIDE_EFFECTS (ref)
3186 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3187 return ref;
3190 else if (!(complain & tf_error))
3191 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3193 /* `pointer' won't be an error_mark_node if we were given a
3194 pointer to member, so it's cool to check for this here. */
3195 else if (TYPE_PTRMEM_P (type))
3196 switch (errorstring)
3198 case RO_ARRAY_INDEXING:
3199 error ("invalid use of array indexing on pointer to member");
3200 break;
3201 case RO_UNARY_STAR:
3202 error ("invalid use of unary %<*%> on pointer to member");
3203 break;
3204 case RO_IMPLICIT_CONVERSION:
3205 error ("invalid use of implicit conversion on pointer to member");
3206 break;
3207 case RO_ARROW_STAR:
3208 error ("left hand operand of %<->*%> must be a pointer to class, "
3209 "but is a pointer to member of type %qT", type);
3210 break;
3211 default:
3212 gcc_unreachable ();
3214 else if (pointer != error_mark_node)
3215 invalid_indirection_error (input_location, type, errorstring);
3217 return error_mark_node;
3220 /* Entry point used by c-common, which expects folding. */
3222 tree
3223 build_indirect_ref (location_t /*loc*/,
3224 tree ptr, ref_operator errorstring)
3226 return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3229 /* Entry point used by internal indirection needs that don't correspond to any
3230 syntactic construct. */
3232 tree
3233 cp_build_fold_indirect_ref (tree pointer)
3235 return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3238 /* Entry point used by indirection needs that correspond to some syntactic
3239 construct. */
3241 tree
3242 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3243 tsubst_flags_t complain)
3245 return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3248 /* This handles expressions of the form "a[i]", which denotes
3249 an array reference.
3251 This is logically equivalent in C to *(a+i), but we may do it differently.
3252 If A is a variable or a member, we generate a primitive ARRAY_REF.
3253 This avoids forcing the array out of registers, and can work on
3254 arrays that are not lvalues (for example, members of structures returned
3255 by functions).
3257 If INDEX is of some user-defined type, it must be converted to
3258 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3259 will inherit the type of the array, which will be some pointer type.
3261 LOC is the location to use in building the array reference. */
3263 tree
3264 cp_build_array_ref (location_t loc, tree array, tree idx,
3265 tsubst_flags_t complain)
3267 tree ret;
3269 if (idx == 0)
3271 if (complain & tf_error)
3272 error_at (loc, "subscript missing in array reference");
3273 return error_mark_node;
3276 if (TREE_TYPE (array) == error_mark_node
3277 || TREE_TYPE (idx) == error_mark_node)
3278 return error_mark_node;
3280 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3281 inside it. */
3282 switch (TREE_CODE (array))
3284 case COMPOUND_EXPR:
3286 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3287 complain);
3288 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3289 TREE_OPERAND (array, 0), value);
3290 SET_EXPR_LOCATION (ret, loc);
3291 return ret;
3294 case COND_EXPR:
3295 ret = build_conditional_expr
3296 (loc, TREE_OPERAND (array, 0),
3297 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3298 complain),
3299 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3300 complain),
3301 complain);
3302 protected_set_expr_location (ret, loc);
3303 return ret;
3305 default:
3306 break;
3309 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3311 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3313 tree rval, type;
3315 warn_array_subscript_with_type_char (loc, idx);
3317 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3319 if (complain & tf_error)
3320 error_at (loc, "array subscript is not an integer");
3321 return error_mark_node;
3324 /* Apply integral promotions *after* noticing character types.
3325 (It is unclear why we do these promotions -- the standard
3326 does not say that we should. In fact, the natural thing would
3327 seem to be to convert IDX to ptrdiff_t; we're performing
3328 pointer arithmetic.) */
3329 idx = cp_perform_integral_promotions (idx, complain);
3331 /* An array that is indexed by a non-constant
3332 cannot be stored in a register; we must be able to do
3333 address arithmetic on its address.
3334 Likewise an array of elements of variable size. */
3335 if (TREE_CODE (idx) != INTEGER_CST
3336 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3337 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3338 != INTEGER_CST)))
3340 if (!cxx_mark_addressable (array, true))
3341 return error_mark_node;
3344 /* An array that is indexed by a constant value which is not within
3345 the array bounds cannot be stored in a register either; because we
3346 would get a crash in store_bit_field/extract_bit_field when trying
3347 to access a non-existent part of the register. */
3348 if (TREE_CODE (idx) == INTEGER_CST
3349 && TYPE_DOMAIN (TREE_TYPE (array))
3350 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3352 if (!cxx_mark_addressable (array))
3353 return error_mark_node;
3356 /* Note in C++ it is valid to subscript a `register' array, since
3357 it is valid to take the address of something with that
3358 storage specification. */
3359 if (extra_warnings)
3361 tree foo = array;
3362 while (TREE_CODE (foo) == COMPONENT_REF)
3363 foo = TREE_OPERAND (foo, 0);
3364 if (VAR_P (foo) && DECL_REGISTER (foo)
3365 && (complain & tf_warning))
3366 warning_at (loc, OPT_Wextra,
3367 "subscripting array declared %<register%>");
3370 type = TREE_TYPE (TREE_TYPE (array));
3371 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3372 /* Array ref is const/volatile if the array elements are
3373 or if the array is.. */
3374 TREE_READONLY (rval)
3375 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3376 TREE_SIDE_EFFECTS (rval)
3377 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3378 TREE_THIS_VOLATILE (rval)
3379 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3380 ret = require_complete_type_sfinae (rval, complain);
3381 protected_set_expr_location (ret, loc);
3382 if (non_lvalue)
3383 ret = non_lvalue_loc (loc, ret);
3384 return ret;
3388 tree ar = cp_default_conversion (array, complain);
3389 tree ind = cp_default_conversion (idx, complain);
3391 /* Put the integer in IND to simplify error checking. */
3392 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3393 std::swap (ar, ind);
3395 if (ar == error_mark_node || ind == error_mark_node)
3396 return error_mark_node;
3398 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3400 if (complain & tf_error)
3401 error_at (loc, "subscripted value is neither array nor pointer");
3402 return error_mark_node;
3404 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3406 if (complain & tf_error)
3407 error_at (loc, "array subscript is not an integer");
3408 return error_mark_node;
3411 warn_array_subscript_with_type_char (loc, idx);
3413 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3414 PLUS_EXPR, ar, ind,
3415 complain),
3416 RO_ARRAY_INDEXING,
3417 complain);
3418 protected_set_expr_location (ret, loc);
3419 if (non_lvalue)
3420 ret = non_lvalue_loc (loc, ret);
3421 return ret;
3425 /* Entry point for Obj-C++. */
3427 tree
3428 build_array_ref (location_t loc, tree array, tree idx)
3430 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3433 /* Resolve a pointer to member function. INSTANCE is the object
3434 instance to use, if the member points to a virtual member.
3436 This used to avoid checking for virtual functions if basetype
3437 has no virtual functions, according to an earlier ANSI draft.
3438 With the final ISO C++ rules, such an optimization is
3439 incorrect: A pointer to a derived member can be static_cast
3440 to pointer-to-base-member, as long as the dynamic object
3441 later has the right member. So now we only do this optimization
3442 when we know the dynamic type of the object. */
3444 tree
3445 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3446 tsubst_flags_t complain)
3448 if (TREE_CODE (function) == OFFSET_REF)
3449 function = TREE_OPERAND (function, 1);
3451 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3453 tree idx, delta, e1, e2, e3, vtbl;
3454 bool nonvirtual;
3455 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3456 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3458 tree instance_ptr = *instance_ptrptr;
3459 tree instance_save_expr = 0;
3460 if (instance_ptr == error_mark_node)
3462 if (TREE_CODE (function) == PTRMEM_CST)
3464 /* Extracting the function address from a pmf is only
3465 allowed with -Wno-pmf-conversions. It only works for
3466 pmf constants. */
3467 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3468 e1 = convert (fntype, e1);
3469 return e1;
3471 else
3473 if (complain & tf_error)
3474 error ("object missing in use of %qE", function);
3475 return error_mark_node;
3479 /* True if we know that the dynamic type of the object doesn't have
3480 virtual functions, so we can assume the PFN field is a pointer. */
3481 nonvirtual = (COMPLETE_TYPE_P (basetype)
3482 && !TYPE_POLYMORPHIC_P (basetype)
3483 && resolves_to_fixed_type_p (instance_ptr, 0));
3485 /* If we don't really have an object (i.e. in an ill-formed
3486 conversion from PMF to pointer), we can't resolve virtual
3487 functions anyway. */
3488 if (!nonvirtual && is_dummy_object (instance_ptr))
3489 nonvirtual = true;
3491 if (TREE_SIDE_EFFECTS (instance_ptr))
3492 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3494 if (TREE_SIDE_EFFECTS (function))
3495 function = save_expr (function);
3497 /* Start by extracting all the information from the PMF itself. */
3498 e3 = pfn_from_ptrmemfunc (function);
3499 delta = delta_from_ptrmemfunc (function);
3500 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3501 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3503 int flag_sanitize_save;
3504 case ptrmemfunc_vbit_in_pfn:
3505 e1 = cp_build_binary_op (input_location,
3506 BIT_AND_EXPR, idx, integer_one_node,
3507 complain);
3508 idx = cp_build_binary_op (input_location,
3509 MINUS_EXPR, idx, integer_one_node,
3510 complain);
3511 if (idx == error_mark_node)
3512 return error_mark_node;
3513 break;
3515 case ptrmemfunc_vbit_in_delta:
3516 e1 = cp_build_binary_op (input_location,
3517 BIT_AND_EXPR, delta, integer_one_node,
3518 complain);
3519 /* Don't instrument the RSHIFT_EXPR we're about to create because
3520 we're going to use DELTA number of times, and that wouldn't play
3521 well with SAVE_EXPRs therein. */
3522 flag_sanitize_save = flag_sanitize;
3523 flag_sanitize = 0;
3524 delta = cp_build_binary_op (input_location,
3525 RSHIFT_EXPR, delta, integer_one_node,
3526 complain);
3527 flag_sanitize = flag_sanitize_save;
3528 if (delta == error_mark_node)
3529 return error_mark_node;
3530 break;
3532 default:
3533 gcc_unreachable ();
3536 if (e1 == error_mark_node)
3537 return error_mark_node;
3539 /* Convert down to the right base before using the instance. A
3540 special case is that in a pointer to member of class C, C may
3541 be incomplete. In that case, the function will of course be
3542 a member of C, and no conversion is required. In fact,
3543 lookup_base will fail in that case, because incomplete
3544 classes do not have BINFOs. */
3545 if (!same_type_ignoring_top_level_qualifiers_p
3546 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3548 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3549 basetype, ba_check, NULL, complain);
3550 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3551 1, complain);
3552 if (instance_ptr == error_mark_node)
3553 return error_mark_node;
3555 /* ...and then the delta in the PMF. */
3556 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3558 /* Hand back the adjusted 'this' argument to our caller. */
3559 *instance_ptrptr = instance_ptr;
3561 if (nonvirtual)
3562 /* Now just return the pointer. */
3563 return e3;
3565 /* Next extract the vtable pointer from the object. */
3566 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3567 instance_ptr);
3568 vtbl = cp_build_fold_indirect_ref (vtbl);
3569 if (vtbl == error_mark_node)
3570 return error_mark_node;
3572 /* Finally, extract the function pointer from the vtable. */
3573 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3574 e2 = cp_build_fold_indirect_ref (e2);
3575 if (e2 == error_mark_node)
3576 return error_mark_node;
3577 TREE_CONSTANT (e2) = 1;
3579 /* When using function descriptors, the address of the
3580 vtable entry is treated as a function pointer. */
3581 if (TARGET_VTABLE_USES_DESCRIPTORS)
3582 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3583 cp_build_addr_expr (e2, complain));
3585 e2 = fold_convert (TREE_TYPE (e3), e2);
3586 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3587 if (e1 == error_mark_node)
3588 return error_mark_node;
3590 /* Make sure this doesn't get evaluated first inside one of the
3591 branches of the COND_EXPR. */
3592 if (instance_save_expr)
3593 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3594 instance_save_expr, e1);
3596 function = e1;
3598 return function;
3601 /* Used by the C-common bits. */
3602 tree
3603 build_function_call (location_t /*loc*/,
3604 tree function, tree params)
3606 return cp_build_function_call (function, params, tf_warning_or_error);
3609 /* Used by the C-common bits. */
3610 tree
3611 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3612 tree function, vec<tree, va_gc> *params,
3613 vec<tree, va_gc> * /*origtypes*/)
3615 vec<tree, va_gc> *orig_params = params;
3616 tree ret = cp_build_function_call_vec (function, &params,
3617 tf_warning_or_error);
3619 /* cp_build_function_call_vec can reallocate PARAMS by adding
3620 default arguments. That should never happen here. Verify
3621 that. */
3622 gcc_assert (params == orig_params);
3624 return ret;
3627 /* Build a function call using a tree list of arguments. */
3629 static tree
3630 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3632 vec<tree, va_gc> *vec;
3633 tree ret;
3635 vec = make_tree_vector ();
3636 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3637 vec_safe_push (vec, TREE_VALUE (params));
3638 ret = cp_build_function_call_vec (function, &vec, complain);
3639 release_tree_vector (vec);
3640 return ret;
3643 /* Build a function call using varargs. */
3645 tree
3646 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3648 vec<tree, va_gc> *vec;
3649 va_list args;
3650 tree ret, t;
3652 vec = make_tree_vector ();
3653 va_start (args, complain);
3654 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3655 vec_safe_push (vec, t);
3656 va_end (args);
3657 ret = cp_build_function_call_vec (function, &vec, complain);
3658 release_tree_vector (vec);
3659 return ret;
3662 /* Build a function call using a vector of arguments. PARAMS may be
3663 NULL if there are no parameters. This changes the contents of
3664 PARAMS. */
3666 tree
3667 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3668 tsubst_flags_t complain)
3670 tree fntype, fndecl;
3671 int is_method;
3672 tree original = function;
3673 int nargs;
3674 tree *argarray;
3675 tree parm_types;
3676 vec<tree, va_gc> *allocated = NULL;
3677 tree ret;
3679 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3680 expressions, like those used for ObjC messenger dispatches. */
3681 if (params != NULL && !vec_safe_is_empty (*params))
3682 function = objc_rewrite_function_call (function, (**params)[0]);
3684 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3685 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3686 if (TREE_CODE (function) == NOP_EXPR
3687 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3688 function = TREE_OPERAND (function, 0);
3690 if (TREE_CODE (function) == FUNCTION_DECL)
3692 /* If the function is a non-template member function
3693 or a non-template friend, then we need to check the
3694 constraints.
3696 Note that if overload resolution failed with a single
3697 candidate this function will be used to explicitly diagnose
3698 the failure for the single call expression. The check is
3699 technically redundant since we also would have failed in
3700 add_function_candidate. */
3701 if (flag_concepts
3702 && (complain & tf_error)
3703 && !constraints_satisfied_p (function))
3705 auto_diagnostic_group d;
3706 error ("cannot call function %qD", function);
3707 location_t loc = DECL_SOURCE_LOCATION (function);
3708 diagnose_constraints (loc, function, NULL_TREE);
3709 return error_mark_node;
3712 if (!mark_used (function, complain) && !(complain & tf_error))
3713 return error_mark_node;
3714 fndecl = function;
3716 /* Convert anything with function type to a pointer-to-function. */
3717 if (DECL_MAIN_P (function))
3719 if (complain & tf_error)
3720 pedwarn (input_location, OPT_Wpedantic,
3721 "ISO C++ forbids calling %<::main%> from within program");
3722 else
3723 return error_mark_node;
3725 function = build_addr_func (function, complain);
3727 else
3729 fndecl = NULL_TREE;
3731 function = build_addr_func (function, complain);
3734 if (function == error_mark_node)
3735 return error_mark_node;
3737 fntype = TREE_TYPE (function);
3739 if (TYPE_PTRMEMFUNC_P (fntype))
3741 if (complain & tf_error)
3742 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3743 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3744 original, original);
3745 return error_mark_node;
3748 is_method = (TYPE_PTR_P (fntype)
3749 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3751 if (!(TYPE_PTRFN_P (fntype)
3752 || is_method
3753 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3755 if (complain & tf_error)
3757 if (!flag_diagnostics_show_caret)
3758 error_at (input_location,
3759 "%qE cannot be used as a function", original);
3760 else if (DECL_P (original))
3761 error_at (input_location,
3762 "%qD cannot be used as a function", original);
3763 else
3764 error_at (input_location,
3765 "expression cannot be used as a function");
3768 return error_mark_node;
3771 /* fntype now gets the type of function pointed to. */
3772 fntype = TREE_TYPE (fntype);
3773 parm_types = TYPE_ARG_TYPES (fntype);
3775 if (params == NULL)
3777 allocated = make_tree_vector ();
3778 params = &allocated;
3781 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3782 complain);
3783 if (nargs < 0)
3784 return error_mark_node;
3786 argarray = (*params)->address ();
3788 /* Check for errors in format strings and inappropriately
3789 null parameters. */
3790 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3791 nargs, argarray, NULL);
3793 ret = build_cxx_call (function, nargs, argarray, complain);
3795 if (warned_p)
3797 tree c = extract_call_expr (ret);
3798 if (TREE_CODE (c) == CALL_EXPR)
3799 TREE_NO_WARNING (c) = 1;
3802 if (allocated != NULL)
3803 release_tree_vector (allocated);
3805 return ret;
3808 /* Subroutine of convert_arguments.
3809 Print an error message about a wrong number of arguments. */
3811 static void
3812 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3814 if (fndecl)
3816 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3818 if (DECL_NAME (fndecl) == NULL_TREE
3819 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3820 error_at (loc,
3821 too_many_p
3822 ? G_("too many arguments to constructor %q#D")
3823 : G_("too few arguments to constructor %q#D"),
3824 fndecl);
3825 else
3826 error_at (loc,
3827 too_many_p
3828 ? G_("too many arguments to member function %q#D")
3829 : G_("too few arguments to member function %q#D"),
3830 fndecl);
3832 else
3833 error_at (loc,
3834 too_many_p
3835 ? G_("too many arguments to function %q#D")
3836 : G_("too few arguments to function %q#D"),
3837 fndecl);
3838 if (!DECL_IS_BUILTIN (fndecl))
3839 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3841 else
3843 if (c_dialect_objc () && objc_message_selector ())
3844 error_at (loc,
3845 too_many_p
3846 ? G_("too many arguments to method %q#D")
3847 : G_("too few arguments to method %q#D"),
3848 objc_message_selector ());
3849 else
3850 error_at (loc, too_many_p ? G_("too many arguments to function")
3851 : G_("too few arguments to function"));
3855 /* Convert the actual parameter expressions in the list VALUES to the
3856 types in the list TYPELIST. The converted expressions are stored
3857 back in the VALUES vector.
3858 If parmdecls is exhausted, or when an element has NULL as its type,
3859 perform the default conversions.
3861 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3863 This is also where warnings about wrong number of args are generated.
3865 Returns the actual number of arguments processed (which might be less
3866 than the length of the vector), or -1 on error.
3868 In C++, unspecified trailing parameters can be filled in with their
3869 default arguments, if such were specified. Do so here. */
3871 static int
3872 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3873 int flags, tsubst_flags_t complain)
3875 tree typetail;
3876 unsigned int i;
3878 /* Argument passing is always copy-initialization. */
3879 flags |= LOOKUP_ONLYCONVERTING;
3881 for (i = 0, typetail = typelist;
3882 i < vec_safe_length (*values);
3883 i++)
3885 tree type = typetail ? TREE_VALUE (typetail) : 0;
3886 tree val = (**values)[i];
3888 if (val == error_mark_node || type == error_mark_node)
3889 return -1;
3891 if (type == void_type_node)
3893 if (complain & tf_error)
3895 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3896 return i;
3898 else
3899 return -1;
3902 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3903 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3904 if (TREE_CODE (val) == NOP_EXPR
3905 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3906 && (type == 0 || !TYPE_REF_P (type)))
3907 val = TREE_OPERAND (val, 0);
3909 if (type == 0 || !TYPE_REF_P (type))
3911 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3912 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3913 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3914 val = decay_conversion (val, complain);
3917 if (val == error_mark_node)
3918 return -1;
3920 if (type != 0)
3922 /* Formal parm type is specified by a function prototype. */
3923 tree parmval;
3925 if (!COMPLETE_TYPE_P (complete_type (type)))
3927 if (complain & tf_error)
3929 if (fndecl)
3930 error ("parameter %P of %qD has incomplete type %qT",
3931 i, fndecl, type);
3932 else
3933 error ("parameter %P has incomplete type %qT", i, type);
3935 parmval = error_mark_node;
3937 else
3939 parmval = convert_for_initialization
3940 (NULL_TREE, type, val, flags,
3941 ICR_ARGPASS, fndecl, i, complain);
3942 parmval = convert_for_arg_passing (type, parmval, complain);
3945 if (parmval == error_mark_node)
3946 return -1;
3948 (**values)[i] = parmval;
3950 else
3952 if (fndecl && magic_varargs_p (fndecl))
3953 /* Don't do ellipsis conversion for __built_in_constant_p
3954 as this will result in spurious errors for non-trivial
3955 types. */
3956 val = require_complete_type_sfinae (val, complain);
3957 else
3958 val = convert_arg_to_ellipsis (val, complain);
3960 (**values)[i] = val;
3963 if (typetail)
3964 typetail = TREE_CHAIN (typetail);
3967 if (typetail != 0 && typetail != void_list_node)
3969 /* See if there are default arguments that can be used. Because
3970 we hold default arguments in the FUNCTION_TYPE (which is so
3971 wrong), we can see default parameters here from deduced
3972 contexts (and via typeof) for indirect function calls.
3973 Fortunately we know whether we have a function decl to
3974 provide default arguments in a language conformant
3975 manner. */
3976 if (fndecl && TREE_PURPOSE (typetail)
3977 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3979 for (; typetail != void_list_node; ++i)
3981 /* After DR777, with explicit template args we can end up with a
3982 default argument followed by no default argument. */
3983 if (!TREE_PURPOSE (typetail))
3984 break;
3985 tree parmval
3986 = convert_default_arg (TREE_VALUE (typetail),
3987 TREE_PURPOSE (typetail),
3988 fndecl, i, complain);
3990 if (parmval == error_mark_node)
3991 return -1;
3993 vec_safe_push (*values, parmval);
3994 typetail = TREE_CHAIN (typetail);
3995 /* ends with `...'. */
3996 if (typetail == NULL_TREE)
3997 break;
4001 if (typetail && typetail != void_list_node)
4003 if (complain & tf_error)
4004 error_args_num (input_location, fndecl, /*too_many_p=*/false);
4005 return -1;
4009 return (int) i;
4012 /* Build a binary-operation expression, after performing default
4013 conversions on the operands. CODE is the kind of expression to
4014 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4015 are the tree codes which correspond to ARG1 and ARG2 when issuing
4016 warnings about possibly misplaced parentheses. They may differ
4017 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4018 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4019 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4020 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4021 ARG2_CODE as ERROR_MARK. */
4023 tree
4024 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4025 enum tree_code arg1_code, tree arg2,
4026 enum tree_code arg2_code, tree *overload_p,
4027 tsubst_flags_t complain)
4029 tree orig_arg1;
4030 tree orig_arg2;
4031 tree expr;
4032 tree overload = NULL_TREE;
4034 orig_arg1 = arg1;
4035 orig_arg2 = arg2;
4037 if (processing_template_decl)
4039 if (type_dependent_expression_p (arg1)
4040 || type_dependent_expression_p (arg2))
4041 return build_min_nt_loc (loc, code, arg1, arg2);
4042 arg1 = build_non_dependent_expr (arg1);
4043 arg2 = build_non_dependent_expr (arg2);
4046 if (code == DOTSTAR_EXPR)
4047 expr = build_m_component_ref (arg1, arg2, complain);
4048 else
4049 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4050 &overload, complain);
4052 if (overload_p != NULL)
4053 *overload_p = overload;
4055 /* Check for cases such as x+y<<z which users are likely to
4056 misinterpret. But don't warn about obj << x + y, since that is a
4057 common idiom for I/O. */
4058 if (warn_parentheses
4059 && (complain & tf_warning)
4060 && !processing_template_decl
4061 && !error_operand_p (arg1)
4062 && !error_operand_p (arg2)
4063 && (code != LSHIFT_EXPR
4064 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4065 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4066 arg2_code, orig_arg2);
4068 if (processing_template_decl && expr != error_mark_node)
4070 if (overload != NULL_TREE)
4071 return (build_min_non_dep_op_overload
4072 (code, expr, overload, orig_arg1, orig_arg2));
4074 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4077 return expr;
4080 /* Build and return an ARRAY_REF expression. */
4082 tree
4083 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4084 tsubst_flags_t complain)
4086 tree orig_arg1 = arg1;
4087 tree orig_arg2 = arg2;
4088 tree expr;
4089 tree overload = NULL_TREE;
4091 if (processing_template_decl)
4093 if (type_dependent_expression_p (arg1)
4094 || type_dependent_expression_p (arg2))
4095 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4096 NULL_TREE, NULL_TREE);
4097 arg1 = build_non_dependent_expr (arg1);
4098 arg2 = build_non_dependent_expr (arg2);
4101 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4102 NULL_TREE, &overload, complain);
4104 if (processing_template_decl && expr != error_mark_node)
4106 if (overload != NULL_TREE)
4107 return (build_min_non_dep_op_overload
4108 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4110 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4111 NULL_TREE, NULL_TREE);
4113 return expr;
4116 /* Return whether OP is an expression of enum type cast to integer
4117 type. In C++ even unsigned enum types are cast to signed integer
4118 types. We do not want to issue warnings about comparisons between
4119 signed and unsigned types when one of the types is an enum type.
4120 Those warnings are always false positives in practice. */
4122 static bool
4123 enum_cast_to_int (tree op)
4125 if (CONVERT_EXPR_P (op)
4126 && TREE_TYPE (op) == integer_type_node
4127 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4128 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4129 return true;
4131 /* The cast may have been pushed into a COND_EXPR. */
4132 if (TREE_CODE (op) == COND_EXPR)
4133 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4134 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4136 return false;
4139 /* For the c-common bits. */
4140 tree
4141 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4142 bool /*convert_p*/)
4144 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4147 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4148 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4150 static tree
4151 build_vec_cmp (tree_code code, tree type,
4152 tree arg0, tree arg1)
4154 tree zero_vec = build_zero_cst (type);
4155 tree minus_one_vec = build_minus_one_cst (type);
4156 tree cmp_type = build_same_sized_truth_vector_type(type);
4157 tree cmp = build2 (code, cmp_type, arg0, arg1);
4158 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4161 /* Possibly warn about an address never being NULL. */
4163 static void
4164 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4166 if (!warn_address
4167 || (complain & tf_warning) == 0
4168 || c_inhibit_evaluation_warnings != 0
4169 || TREE_NO_WARNING (op))
4170 return;
4172 tree cop = fold_non_dependent_expr (op, complain);
4174 if (TREE_CODE (cop) == ADDR_EXPR
4175 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4176 && !TREE_NO_WARNING (cop))
4177 warning_at (location, OPT_Waddress, "the address of %qD will never "
4178 "be NULL", TREE_OPERAND (cop, 0));
4180 if (CONVERT_EXPR_P (op)
4181 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4183 tree inner_op = op;
4184 STRIP_NOPS (inner_op);
4186 if (DECL_P (inner_op))
4187 warning_at (location, OPT_Waddress,
4188 "the compiler can assume that the address of "
4189 "%qD will never be NULL", inner_op);
4193 /* Build a binary-operation expression without default conversions.
4194 CODE is the kind of expression to build.
4195 LOCATION is the location_t of the operator in the source code.
4196 This function differs from `build' in several ways:
4197 the data type of the result is computed and recorded in it,
4198 warnings are generated if arg data types are invalid,
4199 special handling for addition and subtraction of pointers is known,
4200 and some optimization is done (operations on narrow ints
4201 are done in the narrower type when that gives the same result).
4202 Constant folding is also done before the result is returned.
4204 Note that the operands will never have enumeral types
4205 because either they have just had the default conversions performed
4206 or they have both just been converted to some other type in which
4207 the arithmetic is to be done.
4209 C++: must do special pointer arithmetic when implementing
4210 multiple inheritance, and deal with pointer to member functions. */
4212 tree
4213 cp_build_binary_op (location_t location,
4214 enum tree_code code, tree orig_op0, tree orig_op1,
4215 tsubst_flags_t complain)
4217 tree op0, op1;
4218 enum tree_code code0, code1;
4219 tree type0, type1;
4220 const char *invalid_op_diag;
4222 /* Expression code to give to the expression when it is built.
4223 Normally this is CODE, which is what the caller asked for,
4224 but in some special cases we change it. */
4225 enum tree_code resultcode = code;
4227 /* Data type in which the computation is to be performed.
4228 In the simplest cases this is the common type of the arguments. */
4229 tree result_type = NULL_TREE;
4231 /* Nonzero means operands have already been type-converted
4232 in whatever way is necessary.
4233 Zero means they need to be converted to RESULT_TYPE. */
4234 int converted = 0;
4236 /* Nonzero means create the expression with this type, rather than
4237 RESULT_TYPE. */
4238 tree build_type = 0;
4240 /* Nonzero means after finally constructing the expression
4241 convert it to this type. */
4242 tree final_type = 0;
4244 tree result, result_ovl;
4246 /* Nonzero if this is an operation like MIN or MAX which can
4247 safely be computed in short if both args are promoted shorts.
4248 Also implies COMMON.
4249 -1 indicates a bitwise operation; this makes a difference
4250 in the exact conditions for when it is safe to do the operation
4251 in a narrower mode. */
4252 int shorten = 0;
4254 /* Nonzero if this is a comparison operation;
4255 if both args are promoted shorts, compare the original shorts.
4256 Also implies COMMON. */
4257 int short_compare = 0;
4259 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4260 int common = 0;
4262 /* True if both operands have arithmetic type. */
4263 bool arithmetic_types_p;
4265 /* Apply default conversions. */
4266 op0 = orig_op0;
4267 op1 = orig_op1;
4269 /* Remember whether we're doing / or %. */
4270 bool doing_div_or_mod = false;
4272 /* Remember whether we're doing << or >>. */
4273 bool doing_shift = false;
4275 /* Tree holding instrumentation expression. */
4276 tree instrument_expr = NULL_TREE;
4278 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4279 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4280 || code == TRUTH_XOR_EXPR)
4282 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4283 op0 = decay_conversion (op0, complain);
4284 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4285 op1 = decay_conversion (op1, complain);
4287 else
4289 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4290 op0 = cp_default_conversion (op0, complain);
4291 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4292 op1 = cp_default_conversion (op1, complain);
4295 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4296 STRIP_TYPE_NOPS (op0);
4297 STRIP_TYPE_NOPS (op1);
4299 /* DTRT if one side is an overloaded function, but complain about it. */
4300 if (type_unknown_p (op0))
4302 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4303 if (t != error_mark_node)
4305 if (complain & tf_error)
4306 permerror (input_location, "assuming cast to type %qT from overloaded function",
4307 TREE_TYPE (t));
4308 op0 = t;
4311 if (type_unknown_p (op1))
4313 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4314 if (t != error_mark_node)
4316 if (complain & tf_error)
4317 permerror (input_location, "assuming cast to type %qT from overloaded function",
4318 TREE_TYPE (t));
4319 op1 = t;
4323 type0 = TREE_TYPE (op0);
4324 type1 = TREE_TYPE (op1);
4326 /* The expression codes of the data types of the arguments tell us
4327 whether the arguments are integers, floating, pointers, etc. */
4328 code0 = TREE_CODE (type0);
4329 code1 = TREE_CODE (type1);
4331 /* If an error was already reported for one of the arguments,
4332 avoid reporting another error. */
4333 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4334 return error_mark_node;
4336 if ((invalid_op_diag
4337 = targetm.invalid_binary_op (code, type0, type1)))
4339 if (complain & tf_error)
4340 error (invalid_op_diag);
4341 return error_mark_node;
4344 /* Issue warnings about peculiar, but valid, uses of NULL. */
4345 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4346 /* It's reasonable to use pointer values as operands of &&
4347 and ||, so NULL is no exception. */
4348 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4349 && ( /* Both are NULL (or 0) and the operation was not a
4350 comparison or a pointer subtraction. */
4351 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4352 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4353 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4354 || (!null_ptr_cst_p (orig_op0)
4355 && !TYPE_PTR_OR_PTRMEM_P (type0))
4356 || (!null_ptr_cst_p (orig_op1)
4357 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4358 && (complain & tf_warning))
4360 source_location loc =
4361 expansion_point_location_if_in_system_header (input_location);
4363 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4366 /* In case when one of the operands of the binary operation is
4367 a vector and another is a scalar -- convert scalar to vector. */
4368 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4370 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4371 complain & tf_error);
4373 switch (convert_flag)
4375 case stv_error:
4376 return error_mark_node;
4377 case stv_firstarg:
4379 op0 = convert (TREE_TYPE (type1), op0);
4380 op0 = save_expr (op0);
4381 op0 = build_vector_from_val (type1, op0);
4382 type0 = TREE_TYPE (op0);
4383 code0 = TREE_CODE (type0);
4384 converted = 1;
4385 break;
4387 case stv_secondarg:
4389 op1 = convert (TREE_TYPE (type0), op1);
4390 op1 = save_expr (op1);
4391 op1 = build_vector_from_val (type0, op1);
4392 type1 = TREE_TYPE (op1);
4393 code1 = TREE_CODE (type1);
4394 converted = 1;
4395 break;
4397 default:
4398 break;
4402 switch (code)
4404 case MINUS_EXPR:
4405 /* Subtraction of two similar pointers.
4406 We must subtract them as integers, then divide by object size. */
4407 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4408 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4409 TREE_TYPE (type1)))
4411 result = pointer_diff (location, op0, op1,
4412 common_pointer_type (type0, type1), complain,
4413 &instrument_expr);
4414 if (instrument_expr != NULL)
4415 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4416 instrument_expr, result);
4418 return result;
4420 /* In all other cases except pointer - int, the usual arithmetic
4421 rules apply. */
4422 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4424 common = 1;
4425 break;
4427 /* The pointer - int case is just like pointer + int; fall
4428 through. */
4429 gcc_fallthrough ();
4430 case PLUS_EXPR:
4431 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4432 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4434 tree ptr_operand;
4435 tree int_operand;
4436 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4437 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4438 if (processing_template_decl)
4440 result_type = TREE_TYPE (ptr_operand);
4441 break;
4443 return cp_pointer_int_sum (location, code,
4444 ptr_operand,
4445 int_operand,
4446 complain);
4448 common = 1;
4449 break;
4451 case MULT_EXPR:
4452 common = 1;
4453 break;
4455 case TRUNC_DIV_EXPR:
4456 case CEIL_DIV_EXPR:
4457 case FLOOR_DIV_EXPR:
4458 case ROUND_DIV_EXPR:
4459 case EXACT_DIV_EXPR:
4460 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4462 tree type0 = TREE_OPERAND (op0, 0);
4463 tree type1 = TREE_OPERAND (op1, 0);
4464 tree first_arg = type0;
4465 if (!TYPE_P (type0))
4466 type0 = TREE_TYPE (type0);
4467 if (!TYPE_P (type1))
4468 type1 = TREE_TYPE (type1);
4469 if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4470 && !(TREE_CODE (first_arg) == PARM_DECL
4471 && DECL_ARRAY_PARAMETER_P (first_arg)
4472 && warn_sizeof_array_argument)
4473 && (complain & tf_warning))
4475 auto_diagnostic_group d;
4476 if (warning_at (location, OPT_Wsizeof_pointer_div,
4477 "division %<sizeof (%T) / sizeof (%T)%> does "
4478 "not compute the number of array elements",
4479 type0, type1))
4480 if (DECL_P (first_arg))
4481 inform (DECL_SOURCE_LOCATION (first_arg),
4482 "first %<sizeof%> operand was declared here");
4486 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4487 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4488 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4489 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4491 enum tree_code tcode0 = code0, tcode1 = code1;
4492 tree cop1 = fold_non_dependent_expr (op1, complain);
4493 doing_div_or_mod = true;
4494 warn_for_div_by_zero (location, cop1);
4496 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4497 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4498 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4499 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4501 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4502 resultcode = RDIV_EXPR;
4503 else
4504 /* When dividing two signed integers, we have to promote to int.
4505 unless we divide by a constant != -1. Note that default
4506 conversion will have been performed on the operands at this
4507 point, so we have to dig out the original type to find out if
4508 it was unsigned. */
4509 shorten = ((TREE_CODE (op0) == NOP_EXPR
4510 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4511 || (TREE_CODE (op1) == INTEGER_CST
4512 && ! integer_all_onesp (op1)));
4514 common = 1;
4516 break;
4518 case BIT_AND_EXPR:
4519 case BIT_IOR_EXPR:
4520 case BIT_XOR_EXPR:
4521 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4522 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4523 && !VECTOR_FLOAT_TYPE_P (type0)
4524 && !VECTOR_FLOAT_TYPE_P (type1)))
4525 shorten = -1;
4526 break;
4528 case TRUNC_MOD_EXPR:
4529 case FLOOR_MOD_EXPR:
4531 tree cop1 = fold_non_dependent_expr (op1, complain);
4532 doing_div_or_mod = true;
4533 warn_for_div_by_zero (location, cop1);
4536 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4537 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4538 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4539 common = 1;
4540 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4542 /* Although it would be tempting to shorten always here, that loses
4543 on some targets, since the modulo instruction is undefined if the
4544 quotient can't be represented in the computation mode. We shorten
4545 only if unsigned or if dividing by something we know != -1. */
4546 shorten = ((TREE_CODE (op0) == NOP_EXPR
4547 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4548 || (TREE_CODE (op1) == INTEGER_CST
4549 && ! integer_all_onesp (op1)));
4550 common = 1;
4552 break;
4554 case TRUTH_ANDIF_EXPR:
4555 case TRUTH_ORIF_EXPR:
4556 case TRUTH_AND_EXPR:
4557 case TRUTH_OR_EXPR:
4558 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4560 if (!COMPARISON_CLASS_P (op1))
4561 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4562 build_zero_cst (type1), complain);
4563 if (code == TRUTH_ANDIF_EXPR)
4565 tree z = build_zero_cst (TREE_TYPE (op1));
4566 return build_conditional_expr (location, op0, op1, z, complain);
4568 else if (code == TRUTH_ORIF_EXPR)
4570 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4571 return build_conditional_expr (location, op0, m1, op1, complain);
4573 else
4574 gcc_unreachable ();
4576 if (VECTOR_TYPE_P (type0))
4578 if (!COMPARISON_CLASS_P (op0))
4579 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4580 build_zero_cst (type0), complain);
4581 if (!VECTOR_TYPE_P (type1))
4583 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4584 tree z = build_zero_cst (TREE_TYPE (op0));
4585 op1 = build_conditional_expr (location, op1, m1, z, complain);
4587 else if (!COMPARISON_CLASS_P (op1))
4588 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4589 build_zero_cst (type1), complain);
4591 if (code == TRUTH_ANDIF_EXPR)
4592 code = BIT_AND_EXPR;
4593 else if (code == TRUTH_ORIF_EXPR)
4594 code = BIT_IOR_EXPR;
4595 else
4596 gcc_unreachable ();
4598 return cp_build_binary_op (location, code, op0, op1, complain);
4601 result_type = boolean_type_node;
4602 break;
4604 /* Shift operations: result has same type as first operand;
4605 always convert second operand to int.
4606 Also set SHORT_SHIFT if shifting rightward. */
4608 case RSHIFT_EXPR:
4609 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4610 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4612 result_type = type0;
4613 converted = 1;
4615 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4616 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4617 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4618 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4619 TYPE_VECTOR_SUBPARTS (type1)))
4621 result_type = type0;
4622 converted = 1;
4624 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4626 tree const_op1 = fold_non_dependent_expr (op1, complain);
4627 if (TREE_CODE (const_op1) != INTEGER_CST)
4628 const_op1 = op1;
4629 result_type = type0;
4630 doing_shift = true;
4631 if (TREE_CODE (const_op1) == INTEGER_CST)
4633 if (tree_int_cst_lt (const_op1, integer_zero_node))
4635 if ((complain & tf_warning)
4636 && c_inhibit_evaluation_warnings == 0)
4637 warning (OPT_Wshift_count_negative,
4638 "right shift count is negative");
4640 else
4642 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4643 && (complain & tf_warning)
4644 && c_inhibit_evaluation_warnings == 0)
4645 warning (OPT_Wshift_count_overflow,
4646 "right shift count >= width of type");
4649 /* Avoid converting op1 to result_type later. */
4650 converted = 1;
4652 break;
4654 case LSHIFT_EXPR:
4655 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4656 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4658 result_type = type0;
4659 converted = 1;
4661 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4662 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4663 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4664 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4665 TYPE_VECTOR_SUBPARTS (type1)))
4667 result_type = type0;
4668 converted = 1;
4670 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4672 tree const_op0 = fold_non_dependent_expr (op0, complain);
4673 if (TREE_CODE (const_op0) != INTEGER_CST)
4674 const_op0 = op0;
4675 tree const_op1 = fold_non_dependent_expr (op1, complain);
4676 if (TREE_CODE (const_op1) != INTEGER_CST)
4677 const_op1 = op1;
4678 result_type = type0;
4679 doing_shift = true;
4680 if (TREE_CODE (const_op0) == INTEGER_CST
4681 && tree_int_cst_sgn (const_op0) < 0
4682 && (complain & tf_warning)
4683 && c_inhibit_evaluation_warnings == 0)
4684 warning (OPT_Wshift_negative_value,
4685 "left shift of negative value");
4686 if (TREE_CODE (const_op1) == INTEGER_CST)
4688 if (tree_int_cst_lt (const_op1, integer_zero_node))
4690 if ((complain & tf_warning)
4691 && c_inhibit_evaluation_warnings == 0)
4692 warning (OPT_Wshift_count_negative,
4693 "left shift count is negative");
4695 else if (compare_tree_int (const_op1,
4696 TYPE_PRECISION (type0)) >= 0)
4698 if ((complain & tf_warning)
4699 && c_inhibit_evaluation_warnings == 0)
4700 warning (OPT_Wshift_count_overflow,
4701 "left shift count >= width of type");
4703 else if (TREE_CODE (const_op0) == INTEGER_CST
4704 && (complain & tf_warning))
4705 maybe_warn_shift_overflow (location, const_op0, const_op1);
4707 /* Avoid converting op1 to result_type later. */
4708 converted = 1;
4710 break;
4712 case RROTATE_EXPR:
4713 case LROTATE_EXPR:
4714 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4716 result_type = type0;
4717 if (TREE_CODE (op1) == INTEGER_CST)
4719 if (tree_int_cst_lt (op1, integer_zero_node))
4721 if (complain & tf_warning)
4722 warning (0, (code == LROTATE_EXPR)
4723 ? G_("left rotate count is negative")
4724 : G_("right rotate count is negative"));
4726 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4728 if (complain & tf_warning)
4729 warning (0, (code == LROTATE_EXPR)
4730 ? G_("left rotate count >= width of type")
4731 : G_("right rotate count >= width of type"));
4734 /* Convert the shift-count to an integer, regardless of
4735 size of value being shifted. */
4736 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4737 op1 = cp_convert (integer_type_node, op1, complain);
4739 break;
4741 case EQ_EXPR:
4742 case NE_EXPR:
4743 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4744 goto vector_compare;
4745 if ((complain & tf_warning)
4746 && c_inhibit_evaluation_warnings == 0
4747 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4748 warning (OPT_Wfloat_equal,
4749 "comparing floating point with == or != is unsafe");
4750 if ((complain & tf_warning)
4751 && ((TREE_CODE (orig_op0) == STRING_CST
4752 && !integer_zerop (cp_fully_fold (op1)))
4753 || (TREE_CODE (orig_op1) == STRING_CST
4754 && !integer_zerop (cp_fully_fold (op0)))))
4755 warning (OPT_Waddress, "comparison with string literal results "
4756 "in unspecified behavior");
4758 build_type = boolean_type_node;
4759 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4760 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4761 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4762 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4763 short_compare = 1;
4764 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4765 && null_ptr_cst_p (orig_op1))
4766 /* Handle, eg, (void*)0 (c++/43906), and more. */
4767 || (code0 == POINTER_TYPE
4768 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4770 if (TYPE_PTR_P (type1))
4771 result_type = composite_pointer_type (type0, type1, op0, op1,
4772 CPO_COMPARISON, complain);
4773 else
4774 result_type = type0;
4776 if (char_type_p (TREE_TYPE (orig_op1)))
4778 auto_diagnostic_group d;
4779 if (warning (OPT_Wpointer_compare,
4780 "comparison between pointer and zero character "
4781 "constant"))
4782 inform (input_location,
4783 "did you mean to dereference the pointer?");
4785 warn_for_null_address (location, op0, complain);
4787 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4788 && null_ptr_cst_p (orig_op0))
4789 /* Handle, eg, (void*)0 (c++/43906), and more. */
4790 || (code1 == POINTER_TYPE
4791 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4793 if (TYPE_PTR_P (type0))
4794 result_type = composite_pointer_type (type0, type1, op0, op1,
4795 CPO_COMPARISON, complain);
4796 else
4797 result_type = type1;
4799 if (char_type_p (TREE_TYPE (orig_op0)))
4801 auto_diagnostic_group d;
4802 if (warning (OPT_Wpointer_compare,
4803 "comparison between pointer and zero character "
4804 "constant"))
4805 inform (input_location,
4806 "did you mean to dereference the pointer?");
4808 warn_for_null_address (location, op1, complain);
4810 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4811 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4812 result_type = composite_pointer_type (type0, type1, op0, op1,
4813 CPO_COMPARISON, complain);
4814 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4815 /* One of the operands must be of nullptr_t type. */
4816 result_type = TREE_TYPE (nullptr_node);
4817 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4819 result_type = type0;
4820 if (complain & tf_error)
4821 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4822 else
4823 return error_mark_node;
4825 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4827 result_type = type1;
4828 if (complain & tf_error)
4829 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4830 else
4831 return error_mark_node;
4833 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4835 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4836 == ptrmemfunc_vbit_in_delta)
4838 tree pfn0, delta0, e1, e2;
4840 if (TREE_SIDE_EFFECTS (op0))
4841 op0 = save_expr (op0);
4843 pfn0 = pfn_from_ptrmemfunc (op0);
4844 delta0 = delta_from_ptrmemfunc (op0);
4845 e1 = cp_build_binary_op (location,
4846 EQ_EXPR,
4847 pfn0,
4848 build_zero_cst (TREE_TYPE (pfn0)),
4849 complain);
4850 e2 = cp_build_binary_op (location,
4851 BIT_AND_EXPR,
4852 delta0,
4853 integer_one_node,
4854 complain);
4856 if (complain & tf_warning)
4857 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4859 e2 = cp_build_binary_op (location,
4860 EQ_EXPR, e2, integer_zero_node,
4861 complain);
4862 op0 = cp_build_binary_op (location,
4863 TRUTH_ANDIF_EXPR, e1, e2,
4864 complain);
4865 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4867 else
4869 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4870 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4872 result_type = TREE_TYPE (op0);
4874 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4875 return cp_build_binary_op (location, code, op1, op0, complain);
4876 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4878 tree type;
4879 /* E will be the final comparison. */
4880 tree e;
4881 /* E1 and E2 are for scratch. */
4882 tree e1;
4883 tree e2;
4884 tree pfn0;
4885 tree pfn1;
4886 tree delta0;
4887 tree delta1;
4889 type = composite_pointer_type (type0, type1, op0, op1,
4890 CPO_COMPARISON, complain);
4892 if (!same_type_p (TREE_TYPE (op0), type))
4893 op0 = cp_convert_and_check (type, op0, complain);
4894 if (!same_type_p (TREE_TYPE (op1), type))
4895 op1 = cp_convert_and_check (type, op1, complain);
4897 if (op0 == error_mark_node || op1 == error_mark_node)
4898 return error_mark_node;
4900 if (TREE_SIDE_EFFECTS (op0))
4901 op0 = save_expr (op0);
4902 if (TREE_SIDE_EFFECTS (op1))
4903 op1 = save_expr (op1);
4905 pfn0 = pfn_from_ptrmemfunc (op0);
4906 pfn0 = cp_fully_fold (pfn0);
4907 /* Avoid -Waddress warnings (c++/64877). */
4908 if (TREE_CODE (pfn0) == ADDR_EXPR)
4909 TREE_NO_WARNING (pfn0) = 1;
4910 pfn1 = pfn_from_ptrmemfunc (op1);
4911 pfn1 = cp_fully_fold (pfn1);
4912 delta0 = delta_from_ptrmemfunc (op0);
4913 delta1 = delta_from_ptrmemfunc (op1);
4914 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4915 == ptrmemfunc_vbit_in_delta)
4917 /* We generate:
4919 (op0.pfn == op1.pfn
4920 && ((op0.delta == op1.delta)
4921 || (!op0.pfn && op0.delta & 1 == 0
4922 && op1.delta & 1 == 0))
4924 The reason for the `!op0.pfn' bit is that a NULL
4925 pointer-to-member is any member with a zero PFN and
4926 LSB of the DELTA field is 0. */
4928 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4929 delta0,
4930 integer_one_node,
4931 complain);
4932 e1 = cp_build_binary_op (location,
4933 EQ_EXPR, e1, integer_zero_node,
4934 complain);
4935 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4936 delta1,
4937 integer_one_node,
4938 complain);
4939 e2 = cp_build_binary_op (location,
4940 EQ_EXPR, e2, integer_zero_node,
4941 complain);
4942 e1 = cp_build_binary_op (location,
4943 TRUTH_ANDIF_EXPR, e2, e1,
4944 complain);
4945 e2 = cp_build_binary_op (location, EQ_EXPR,
4946 pfn0,
4947 build_zero_cst (TREE_TYPE (pfn0)),
4948 complain);
4949 e2 = cp_build_binary_op (location,
4950 TRUTH_ANDIF_EXPR, e2, e1, complain);
4951 e1 = cp_build_binary_op (location,
4952 EQ_EXPR, delta0, delta1, complain);
4953 e1 = cp_build_binary_op (location,
4954 TRUTH_ORIF_EXPR, e1, e2, complain);
4956 else
4958 /* We generate:
4960 (op0.pfn == op1.pfn
4961 && (!op0.pfn || op0.delta == op1.delta))
4963 The reason for the `!op0.pfn' bit is that a NULL
4964 pointer-to-member is any member with a zero PFN; the
4965 DELTA field is unspecified. */
4967 e1 = cp_build_binary_op (location,
4968 EQ_EXPR, delta0, delta1, complain);
4969 e2 = cp_build_binary_op (location,
4970 EQ_EXPR,
4971 pfn0,
4972 build_zero_cst (TREE_TYPE (pfn0)),
4973 complain);
4974 e1 = cp_build_binary_op (location,
4975 TRUTH_ORIF_EXPR, e1, e2, complain);
4977 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4978 e = cp_build_binary_op (location,
4979 TRUTH_ANDIF_EXPR, e2, e1, complain);
4980 if (code == EQ_EXPR)
4981 return e;
4982 return cp_build_binary_op (location,
4983 EQ_EXPR, e, integer_zero_node, complain);
4985 else
4987 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4988 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4989 type1));
4990 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4991 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4992 type0));
4995 break;
4997 case MAX_EXPR:
4998 case MIN_EXPR:
4999 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
5000 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
5001 shorten = 1;
5002 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5003 result_type = composite_pointer_type (type0, type1, op0, op1,
5004 CPO_COMPARISON, complain);
5005 break;
5007 case LE_EXPR:
5008 case GE_EXPR:
5009 case LT_EXPR:
5010 case GT_EXPR:
5011 if (TREE_CODE (orig_op0) == STRING_CST
5012 || TREE_CODE (orig_op1) == STRING_CST)
5014 if (complain & tf_warning)
5015 warning (OPT_Waddress, "comparison with string literal results "
5016 "in unspecified behavior");
5019 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5021 vector_compare:
5022 tree intt;
5023 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5024 TREE_TYPE (type1))
5025 && !vector_types_compatible_elements_p (type0, type1))
5027 if (complain & tf_error)
5029 error_at (location, "comparing vectors with different "
5030 "element types");
5031 inform (location, "operand types are %qT and %qT",
5032 type0, type1);
5034 return error_mark_node;
5037 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5038 TYPE_VECTOR_SUBPARTS (type1)))
5040 if (complain & tf_error)
5042 error_at (location, "comparing vectors with different "
5043 "number of elements");
5044 inform (location, "operand types are %qT and %qT",
5045 type0, type1);
5047 return error_mark_node;
5050 /* It's not precisely specified how the usual arithmetic
5051 conversions apply to the vector types. Here, we use
5052 the unsigned type if one of the operands is signed and
5053 the other one is unsigned. */
5054 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5056 if (!TYPE_UNSIGNED (type0))
5057 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5058 else
5059 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5060 warning_at (location, OPT_Wsign_compare, "comparison between "
5061 "types %qT and %qT", type0, type1);
5064 /* Always construct signed integer vector type. */
5065 intt = c_common_type_for_size
5066 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5067 if (!intt)
5069 if (complain & tf_error)
5070 error_at (location, "could not find an integer type "
5071 "of the same size as %qT", TREE_TYPE (type0));
5072 return error_mark_node;
5074 result_type = build_opaque_vector_type (intt,
5075 TYPE_VECTOR_SUBPARTS (type0));
5076 converted = 1;
5077 return build_vec_cmp (resultcode, result_type, op0, op1);
5079 build_type = boolean_type_node;
5080 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5081 || code0 == ENUMERAL_TYPE)
5082 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5083 || code1 == ENUMERAL_TYPE))
5084 short_compare = 1;
5085 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5086 result_type = composite_pointer_type (type0, type1, op0, op1,
5087 CPO_COMPARISON, complain);
5088 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5090 result_type = type0;
5091 if (extra_warnings && (complain & tf_warning))
5092 warning (OPT_Wextra,
5093 "ordered comparison of pointer with integer zero");
5095 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5097 result_type = type1;
5098 if (extra_warnings && (complain & tf_warning))
5099 warning (OPT_Wextra,
5100 "ordered comparison of pointer with integer zero");
5102 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5103 /* One of the operands must be of nullptr_t type. */
5104 result_type = TREE_TYPE (nullptr_node);
5105 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5107 result_type = type0;
5108 if (complain & tf_error)
5109 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5110 else
5111 return error_mark_node;
5113 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5115 result_type = type1;
5116 if (complain & tf_error)
5117 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5118 else
5119 return error_mark_node;
5122 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5123 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5125 op0 = save_expr (op0);
5126 op1 = save_expr (op1);
5128 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5129 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5132 break;
5134 case UNORDERED_EXPR:
5135 case ORDERED_EXPR:
5136 case UNLT_EXPR:
5137 case UNLE_EXPR:
5138 case UNGT_EXPR:
5139 case UNGE_EXPR:
5140 case UNEQ_EXPR:
5141 build_type = integer_type_node;
5142 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5144 if (complain & tf_error)
5145 error ("unordered comparison on non-floating point argument");
5146 return error_mark_node;
5148 common = 1;
5149 break;
5151 default:
5152 break;
5155 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5156 || code0 == ENUMERAL_TYPE)
5157 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5158 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5159 arithmetic_types_p = 1;
5160 else
5162 arithmetic_types_p = 0;
5163 /* Vector arithmetic is only allowed when both sides are vectors. */
5164 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5166 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5167 || !vector_types_compatible_elements_p (type0, type1))
5169 if (complain & tf_error)
5171 /* "location" already embeds the locations of the
5172 operands, so we don't need to add them separately
5173 to richloc. */
5174 rich_location richloc (line_table, location);
5175 binary_op_error (&richloc, code, type0, type1);
5177 return error_mark_node;
5179 arithmetic_types_p = 1;
5182 /* Determine the RESULT_TYPE, if it is not already known. */
5183 if (!result_type
5184 && arithmetic_types_p
5185 && (shorten || common || short_compare))
5187 result_type = cp_common_type (type0, type1);
5188 if (complain & tf_warning)
5189 do_warn_double_promotion (result_type, type0, type1,
5190 "implicit conversion from %qH to %qI "
5191 "to match other operand of binary "
5192 "expression",
5193 location);
5196 if (!result_type)
5198 if (complain & tf_error)
5199 error_at (location,
5200 "invalid operands of types %qT and %qT to binary %qO",
5201 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5202 return error_mark_node;
5205 /* If we're in a template, the only thing we need to know is the
5206 RESULT_TYPE. */
5207 if (processing_template_decl)
5209 /* Since the middle-end checks the type when doing a build2, we
5210 need to build the tree in pieces. This built tree will never
5211 get out of the front-end as we replace it when instantiating
5212 the template. */
5213 tree tmp = build2 (resultcode,
5214 build_type ? build_type : result_type,
5215 NULL_TREE, op1);
5216 TREE_OPERAND (tmp, 0) = op0;
5217 return tmp;
5220 /* Remember the original type; RESULT_TYPE might be changed later on
5221 by shorten_binary_op. */
5222 tree orig_type = result_type;
5224 if (arithmetic_types_p)
5226 bool first_complex = (code0 == COMPLEX_TYPE);
5227 bool second_complex = (code1 == COMPLEX_TYPE);
5228 int none_complex = (!first_complex && !second_complex);
5230 /* Adapted from patch for c/24581. */
5231 if (first_complex != second_complex
5232 && (code == PLUS_EXPR
5233 || code == MINUS_EXPR
5234 || code == MULT_EXPR
5235 || (code == TRUNC_DIV_EXPR && first_complex))
5236 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5237 && flag_signed_zeros)
5239 /* An operation on mixed real/complex operands must be
5240 handled specially, but the language-independent code can
5241 more easily optimize the plain complex arithmetic if
5242 -fno-signed-zeros. */
5243 tree real_type = TREE_TYPE (result_type);
5244 tree real, imag;
5245 if (first_complex)
5247 if (TREE_TYPE (op0) != result_type)
5248 op0 = cp_convert_and_check (result_type, op0, complain);
5249 if (TREE_TYPE (op1) != real_type)
5250 op1 = cp_convert_and_check (real_type, op1, complain);
5252 else
5254 if (TREE_TYPE (op0) != real_type)
5255 op0 = cp_convert_and_check (real_type, op0, complain);
5256 if (TREE_TYPE (op1) != result_type)
5257 op1 = cp_convert_and_check (result_type, op1, complain);
5259 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5260 return error_mark_node;
5261 if (first_complex)
5263 op0 = save_expr (op0);
5264 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5265 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5266 switch (code)
5268 case MULT_EXPR:
5269 case TRUNC_DIV_EXPR:
5270 op1 = save_expr (op1);
5271 imag = build2 (resultcode, real_type, imag, op1);
5272 /* Fall through. */
5273 case PLUS_EXPR:
5274 case MINUS_EXPR:
5275 real = build2 (resultcode, real_type, real, op1);
5276 break;
5277 default:
5278 gcc_unreachable();
5281 else
5283 op1 = save_expr (op1);
5284 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5285 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5286 switch (code)
5288 case MULT_EXPR:
5289 op0 = save_expr (op0);
5290 imag = build2 (resultcode, real_type, op0, imag);
5291 /* Fall through. */
5292 case PLUS_EXPR:
5293 real = build2 (resultcode, real_type, op0, real);
5294 break;
5295 case MINUS_EXPR:
5296 real = build2 (resultcode, real_type, op0, real);
5297 imag = build1 (NEGATE_EXPR, real_type, imag);
5298 break;
5299 default:
5300 gcc_unreachable();
5303 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5304 return result;
5307 /* For certain operations (which identify themselves by shorten != 0)
5308 if both args were extended from the same smaller type,
5309 do the arithmetic in that type and then extend.
5311 shorten !=0 and !=1 indicates a bitwise operation.
5312 For them, this optimization is safe only if
5313 both args are zero-extended or both are sign-extended.
5314 Otherwise, we might change the result.
5315 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5316 but calculated in (unsigned short) it would be (unsigned short)-1. */
5318 if (shorten && none_complex)
5320 final_type = result_type;
5321 result_type = shorten_binary_op (result_type, op0, op1,
5322 shorten == -1);
5325 /* Comparison operations are shortened too but differently.
5326 They identify themselves by setting short_compare = 1. */
5328 if (short_compare)
5330 /* We call shorten_compare only for diagnostics. */
5331 tree xop0 = fold_simple (op0);
5332 tree xop1 = fold_simple (op1);
5333 tree xresult_type = result_type;
5334 enum tree_code xresultcode = resultcode;
5335 shorten_compare (location, &xop0, &xop1, &xresult_type,
5336 &xresultcode);
5339 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5340 && warn_sign_compare
5341 /* Do not warn until the template is instantiated; we cannot
5342 bound the ranges of the arguments until that point. */
5343 && !processing_template_decl
5344 && (complain & tf_warning)
5345 && c_inhibit_evaluation_warnings == 0
5346 /* Even unsigned enum types promote to signed int. We don't
5347 want to issue -Wsign-compare warnings for this case. */
5348 && !enum_cast_to_int (orig_op0)
5349 && !enum_cast_to_int (orig_op1))
5351 tree oop0 = maybe_constant_value (orig_op0);
5352 tree oop1 = maybe_constant_value (orig_op1);
5354 if (TREE_CODE (oop0) != INTEGER_CST)
5355 oop0 = cp_fully_fold (orig_op0);
5356 if (TREE_CODE (oop1) != INTEGER_CST)
5357 oop1 = cp_fully_fold (orig_op1);
5358 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5359 result_type, resultcode);
5363 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5364 Then the expression will be built.
5365 It will be given type FINAL_TYPE if that is nonzero;
5366 otherwise, it will be given type RESULT_TYPE. */
5367 if (! converted)
5369 warning_sentinel w (warn_sign_conversion, short_compare);
5370 if (TREE_TYPE (op0) != result_type)
5371 op0 = cp_convert_and_check (result_type, op0, complain);
5372 if (TREE_TYPE (op1) != result_type)
5373 op1 = cp_convert_and_check (result_type, op1, complain);
5375 if (op0 == error_mark_node || op1 == error_mark_node)
5376 return error_mark_node;
5379 if (build_type == NULL_TREE)
5380 build_type = result_type;
5382 if (sanitize_flags_p ((SANITIZE_SHIFT
5383 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5384 && current_function_decl != NULL_TREE
5385 && !processing_template_decl
5386 && (doing_div_or_mod || doing_shift))
5388 /* OP0 and/or OP1 might have side-effects. */
5389 op0 = cp_save_expr (op0);
5390 op1 = cp_save_expr (op1);
5391 op0 = fold_non_dependent_expr (op0, complain);
5392 op1 = fold_non_dependent_expr (op1, complain);
5393 if (doing_div_or_mod
5394 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5396 /* For diagnostics we want to use the promoted types without
5397 shorten_binary_op. So convert the arguments to the
5398 original result_type. */
5399 tree cop0 = op0;
5400 tree cop1 = op1;
5401 if (TREE_TYPE (cop0) != orig_type)
5402 cop0 = cp_convert (orig_type, op0, complain);
5403 if (TREE_TYPE (cop1) != orig_type)
5404 cop1 = cp_convert (orig_type, op1, complain);
5405 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5407 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5408 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5411 result = build2_loc (location, resultcode, build_type, op0, op1);
5412 if (final_type != 0)
5413 result = cp_convert (final_type, result, complain);
5415 if (instrument_expr != NULL)
5416 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5417 instrument_expr, result);
5419 if (!processing_template_decl)
5421 op0 = cp_fully_fold (op0);
5422 /* Only consider the second argument if the first isn't overflowed. */
5423 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5424 return result;
5425 op1 = cp_fully_fold (op1);
5426 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5427 return result;
5429 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5430 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5431 return result;
5433 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5434 if (TREE_OVERFLOW_P (result_ovl))
5435 overflow_warning (location, result_ovl);
5437 return result;
5440 /* Build a VEC_PERM_EXPR.
5441 This is a simple wrapper for c_build_vec_perm_expr. */
5442 tree
5443 build_x_vec_perm_expr (location_t loc,
5444 tree arg0, tree arg1, tree arg2,
5445 tsubst_flags_t complain)
5447 tree orig_arg0 = arg0;
5448 tree orig_arg1 = arg1;
5449 tree orig_arg2 = arg2;
5450 if (processing_template_decl)
5452 if (type_dependent_expression_p (arg0)
5453 || type_dependent_expression_p (arg1)
5454 || type_dependent_expression_p (arg2))
5455 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5456 arg0 = build_non_dependent_expr (arg0);
5457 if (arg1)
5458 arg1 = build_non_dependent_expr (arg1);
5459 arg2 = build_non_dependent_expr (arg2);
5461 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5462 if (processing_template_decl && exp != error_mark_node)
5463 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5464 orig_arg1, orig_arg2);
5465 return exp;
5468 /* Return a tree for the sum or difference (RESULTCODE says which)
5469 of pointer PTROP and integer INTOP. */
5471 static tree
5472 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5473 tree intop, tsubst_flags_t complain)
5475 tree res_type = TREE_TYPE (ptrop);
5477 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5478 in certain circumstance (when it's valid to do so). So we need
5479 to make sure it's complete. We don't need to check here, if we
5480 can actually complete it at all, as those checks will be done in
5481 pointer_int_sum() anyway. */
5482 complete_type (TREE_TYPE (res_type));
5484 return pointer_int_sum (loc, resultcode, ptrop,
5485 intop, complain & tf_warning_or_error);
5488 /* Return a tree for the difference of pointers OP0 and OP1.
5489 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5490 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5492 static tree
5493 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5494 tsubst_flags_t complain, tree *instrument_expr)
5496 tree result, inttype;
5497 tree restype = ptrdiff_type_node;
5498 tree target_type = TREE_TYPE (ptrtype);
5500 if (!complete_type_or_else (target_type, NULL_TREE))
5501 return error_mark_node;
5503 if (VOID_TYPE_P (target_type))
5505 if (complain & tf_error)
5506 permerror (loc, "ISO C++ forbids using pointer of "
5507 "type %<void *%> in subtraction");
5508 else
5509 return error_mark_node;
5511 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5513 if (complain & tf_error)
5514 permerror (loc, "ISO C++ forbids using pointer to "
5515 "a function in subtraction");
5516 else
5517 return error_mark_node;
5519 if (TREE_CODE (target_type) == METHOD_TYPE)
5521 if (complain & tf_error)
5522 permerror (loc, "ISO C++ forbids using pointer to "
5523 "a method in subtraction");
5524 else
5525 return error_mark_node;
5528 /* Determine integer type result of the subtraction. This will usually
5529 be the same as the result type (ptrdiff_t), but may need to be a wider
5530 type if pointers for the address space are wider than ptrdiff_t. */
5531 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5532 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5533 else
5534 inttype = restype;
5536 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5538 op0 = save_expr (op0);
5539 op1 = save_expr (op1);
5541 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5542 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5545 /* First do the subtraction, then build the divide operator
5546 and only convert at the very end.
5547 Do not do default conversions in case restype is a short type. */
5549 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5550 pointers. If some platform cannot provide that, or has a larger
5551 ptrdiff_type to support differences larger than half the address
5552 space, cast the pointers to some larger integer type and do the
5553 computations in that type. */
5554 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5555 op0 = cp_build_binary_op (loc,
5556 MINUS_EXPR,
5557 cp_convert (inttype, op0, complain),
5558 cp_convert (inttype, op1, complain),
5559 complain);
5560 else
5561 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5563 /* This generates an error if op1 is a pointer to an incomplete type. */
5564 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5566 if (complain & tf_error)
5567 error_at (loc, "invalid use of a pointer to an incomplete type in "
5568 "pointer arithmetic");
5569 else
5570 return error_mark_node;
5573 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5575 if (complain & tf_error)
5576 error_at (loc, "arithmetic on pointer to an empty aggregate");
5577 else
5578 return error_mark_node;
5581 op1 = (TYPE_PTROB_P (ptrtype)
5582 ? size_in_bytes_loc (loc, target_type)
5583 : integer_one_node);
5585 /* Do the division. */
5587 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5588 cp_convert (inttype, op1, complain));
5589 return cp_convert (restype, result, complain);
5592 /* Construct and perhaps optimize a tree representation
5593 for a unary operation. CODE, a tree_code, specifies the operation
5594 and XARG is the operand. */
5596 tree
5597 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5598 tsubst_flags_t complain)
5600 tree orig_expr = xarg;
5601 tree exp;
5602 int ptrmem = 0;
5603 tree overload = NULL_TREE;
5605 if (processing_template_decl)
5607 if (type_dependent_expression_p (xarg))
5608 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5610 xarg = build_non_dependent_expr (xarg);
5613 exp = NULL_TREE;
5615 /* [expr.unary.op] says:
5617 The address of an object of incomplete type can be taken.
5619 (And is just the ordinary address operator, not an overloaded
5620 "operator &".) However, if the type is a template
5621 specialization, we must complete the type at this point so that
5622 an overloaded "operator &" will be available if required. */
5623 if (code == ADDR_EXPR
5624 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5625 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5626 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5627 || (TREE_CODE (xarg) == OFFSET_REF)))
5628 /* Don't look for a function. */;
5629 else
5630 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5631 NULL_TREE, &overload, complain);
5633 if (!exp && code == ADDR_EXPR)
5635 if (is_overloaded_fn (xarg))
5637 tree fn = get_first_fn (xarg);
5638 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5640 if (complain & tf_error)
5641 error (DECL_CONSTRUCTOR_P (fn)
5642 ? G_("taking address of constructor %qD")
5643 : G_("taking address of destructor %qD"),
5644 fn);
5645 return error_mark_node;
5649 /* A pointer to member-function can be formed only by saying
5650 &X::mf. */
5651 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5652 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5654 if (TREE_CODE (xarg) != OFFSET_REF
5655 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5657 if (complain & tf_error)
5659 error ("invalid use of %qE to form a "
5660 "pointer-to-member-function", xarg.get_value ());
5661 if (TREE_CODE (xarg) != OFFSET_REF)
5662 inform (input_location, " a qualified-id is required");
5664 return error_mark_node;
5666 else
5668 if (complain & tf_error)
5669 error ("parentheses around %qE cannot be used to form a"
5670 " pointer-to-member-function",
5671 xarg.get_value ());
5672 else
5673 return error_mark_node;
5674 PTRMEM_OK_P (xarg) = 1;
5678 if (TREE_CODE (xarg) == OFFSET_REF)
5680 ptrmem = PTRMEM_OK_P (xarg);
5682 if (!ptrmem && !flag_ms_extensions
5683 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5685 /* A single non-static member, make sure we don't allow a
5686 pointer-to-member. */
5687 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5688 TREE_OPERAND (xarg, 0),
5689 ovl_make (TREE_OPERAND (xarg, 1)));
5690 PTRMEM_OK_P (xarg) = ptrmem;
5694 exp = cp_build_addr_expr_strict (xarg, complain);
5697 if (processing_template_decl && exp != error_mark_node)
5699 if (overload != NULL_TREE)
5700 return (build_min_non_dep_op_overload
5701 (code, exp, overload, orig_expr, integer_zero_node));
5703 exp = build_min_non_dep (code, exp, orig_expr,
5704 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5706 if (TREE_CODE (exp) == ADDR_EXPR)
5707 PTRMEM_OK_P (exp) = ptrmem;
5708 return exp;
5711 /* Construct and perhaps optimize a tree representation
5712 for __builtin_addressof operation. ARG specifies the operand. */
5714 tree
5715 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5717 tree orig_expr = arg;
5719 if (processing_template_decl)
5721 if (type_dependent_expression_p (arg))
5722 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5724 arg = build_non_dependent_expr (arg);
5727 tree exp = cp_build_addr_expr_strict (arg, complain);
5729 if (processing_template_decl && exp != error_mark_node)
5730 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5731 return exp;
5734 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5735 constants, where a null value is represented by an INTEGER_CST of
5736 -1. */
5738 tree
5739 cp_truthvalue_conversion (tree expr)
5741 tree type = TREE_TYPE (expr);
5742 if (TYPE_PTR_OR_PTRMEM_P (type)
5743 /* Avoid ICE on invalid use of non-static member function. */
5744 || TREE_CODE (expr) == FUNCTION_DECL)
5745 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5746 else
5747 return c_common_truthvalue_conversion (input_location, expr);
5750 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5752 tree
5753 condition_conversion (tree expr)
5755 tree t;
5756 /* Anything that might happen in a template should go through
5757 maybe_convert_cond. */
5758 gcc_assert (!processing_template_decl);
5759 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5760 tf_warning_or_error, LOOKUP_NORMAL);
5761 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5762 return t;
5765 /* Returns the address of T. This function will fold away
5766 ADDR_EXPR of INDIRECT_REF. */
5768 tree
5769 build_address (tree t)
5771 if (error_operand_p (t) || !cxx_mark_addressable (t))
5772 return error_mark_node;
5773 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5774 || processing_template_decl);
5775 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5776 if (TREE_CODE (t) != ADDR_EXPR)
5777 t = rvalue (t);
5778 return t;
5781 /* Return a NOP_EXPR converting EXPR to TYPE. */
5783 tree
5784 build_nop (tree type, tree expr)
5786 if (type == error_mark_node || error_operand_p (expr))
5787 return expr;
5788 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5791 /* Take the address of ARG, whatever that means under C++ semantics.
5792 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5793 and class rvalues as well.
5795 Nothing should call this function directly; instead, callers should use
5796 cp_build_addr_expr or cp_build_addr_expr_strict. */
5798 static tree
5799 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5801 tree argtype;
5802 tree val;
5804 if (!arg || error_operand_p (arg))
5805 return error_mark_node;
5807 arg = mark_lvalue_use (arg);
5808 if (error_operand_p (arg))
5809 return error_mark_node;
5811 argtype = lvalue_type (arg);
5813 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5815 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5816 && !really_overloaded_fn (arg))
5818 /* They're trying to take the address of a unique non-static
5819 member function. This is ill-formed (except in MS-land),
5820 but let's try to DTRT.
5821 Note: We only handle unique functions here because we don't
5822 want to complain if there's a static overload; non-unique
5823 cases will be handled by instantiate_type. But we need to
5824 handle this case here to allow casts on the resulting PMF.
5825 We could defer this in non-MS mode, but it's easier to give
5826 a useful error here. */
5828 /* Inside constant member functions, the `this' pointer
5829 contains an extra const qualifier. TYPE_MAIN_VARIANT
5830 is used here to remove this const from the diagnostics
5831 and the created OFFSET_REF. */
5832 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5833 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5834 if (!mark_used (fn, complain) && !(complain & tf_error))
5835 return error_mark_node;
5837 if (! flag_ms_extensions)
5839 tree name = DECL_NAME (fn);
5840 if (!(complain & tf_error))
5841 return error_mark_node;
5842 else if (current_class_type
5843 && TREE_OPERAND (arg, 0) == current_class_ref)
5844 /* An expression like &memfn. */
5845 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5846 " or parenthesized non-static member function to form"
5847 " a pointer to member function. Say %<&%T::%D%>",
5848 base, name);
5849 else
5850 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5851 " function to form a pointer to member function."
5852 " Say %<&%T::%D%>",
5853 base, name);
5855 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5858 /* Uninstantiated types are all functions. Taking the
5859 address of a function is a no-op, so just return the
5860 argument. */
5861 if (type_unknown_p (arg))
5862 return build1 (ADDR_EXPR, unknown_type_node, arg);
5864 if (TREE_CODE (arg) == OFFSET_REF)
5865 /* We want a pointer to member; bypass all the code for actually taking
5866 the address of something. */
5867 goto offset_ref;
5869 /* Anything not already handled and not a true memory reference
5870 is an error. */
5871 if (TREE_CODE (argtype) != FUNCTION_TYPE
5872 && TREE_CODE (argtype) != METHOD_TYPE)
5874 cp_lvalue_kind kind = lvalue_kind (arg);
5875 if (kind == clk_none)
5877 if (complain & tf_error)
5878 lvalue_error (input_location, lv_addressof);
5879 return error_mark_node;
5881 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5883 if (!(complain & tf_error))
5884 return error_mark_node;
5885 /* Make this a permerror because we used to accept it. */
5886 permerror (input_location, "taking address of rvalue");
5890 if (TYPE_REF_P (argtype))
5892 tree type = build_pointer_type (TREE_TYPE (argtype));
5893 arg = build1 (CONVERT_EXPR, type, arg);
5894 return arg;
5896 else if (pedantic && DECL_MAIN_P (arg))
5898 /* ARM $3.4 */
5899 /* Apparently a lot of autoconf scripts for C++ packages do this,
5900 so only complain if -Wpedantic. */
5901 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5902 pedwarn (input_location, OPT_Wpedantic,
5903 "ISO C++ forbids taking address of function %<::main%>");
5904 else if (flag_pedantic_errors)
5905 return error_mark_node;
5908 /* Let &* cancel out to simplify resulting code. */
5909 if (INDIRECT_REF_P (arg))
5911 arg = TREE_OPERAND (arg, 0);
5912 if (TYPE_REF_P (TREE_TYPE (arg)))
5914 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5915 arg = build1 (CONVERT_EXPR, type, arg);
5917 else
5918 /* Don't let this be an lvalue. */
5919 arg = rvalue (arg);
5920 return arg;
5923 /* Handle complex lvalues (when permitted)
5924 by reduction to simpler cases. */
5925 val = unary_complex_lvalue (ADDR_EXPR, arg);
5926 if (val != 0)
5927 return val;
5929 switch (TREE_CODE (arg))
5931 CASE_CONVERT:
5932 case FLOAT_EXPR:
5933 case FIX_TRUNC_EXPR:
5934 /* We should have handled this above in the lvalue_kind check. */
5935 gcc_unreachable ();
5936 break;
5938 case BASELINK:
5939 arg = BASELINK_FUNCTIONS (arg);
5940 /* Fall through. */
5942 case OVERLOAD:
5943 arg = OVL_FIRST (arg);
5944 break;
5946 case OFFSET_REF:
5947 offset_ref:
5948 /* Turn a reference to a non-static data member into a
5949 pointer-to-member. */
5951 tree type;
5952 tree t;
5954 gcc_assert (PTRMEM_OK_P (arg));
5956 t = TREE_OPERAND (arg, 1);
5957 if (TYPE_REF_P (TREE_TYPE (t)))
5959 if (complain & tf_error)
5960 error ("cannot create pointer to reference member %qD", t);
5961 return error_mark_node;
5964 type = build_ptrmem_type (context_for_name_lookup (t),
5965 TREE_TYPE (t));
5966 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5967 return t;
5970 default:
5971 break;
5974 if (argtype != error_mark_node)
5975 argtype = build_pointer_type (argtype);
5977 if (bitfield_p (arg))
5979 if (complain & tf_error)
5980 error ("attempt to take address of bit-field");
5981 return error_mark_node;
5984 /* In a template, we are processing a non-dependent expression
5985 so we can just form an ADDR_EXPR with the correct type. */
5986 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5988 if (TREE_CODE (arg) == FUNCTION_DECL
5989 && !mark_used (arg, complain) && !(complain & tf_error))
5990 return error_mark_node;
5991 val = build_address (arg);
5992 if (TREE_CODE (arg) == OFFSET_REF)
5993 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5995 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5997 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5999 /* We can only get here with a single static member
6000 function. */
6001 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
6002 && DECL_STATIC_FUNCTION_P (fn));
6003 if (!mark_used (fn, complain) && !(complain & tf_error))
6004 return error_mark_node;
6005 val = build_address (fn);
6006 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
6007 /* Do not lose object's side effects. */
6008 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
6009 TREE_OPERAND (arg, 0), val);
6011 else
6013 tree object = TREE_OPERAND (arg, 0);
6014 tree field = TREE_OPERAND (arg, 1);
6015 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6016 (TREE_TYPE (object), decl_type_context (field)));
6017 val = build_address (arg);
6020 if (TYPE_PTR_P (argtype)
6021 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6023 build_ptrmemfunc_type (argtype);
6024 val = build_ptrmemfunc (argtype, val, 0,
6025 /*c_cast_p=*/false,
6026 complain);
6029 return val;
6032 /* Take the address of ARG if it has one, even if it's an rvalue. */
6034 tree
6035 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6037 return cp_build_addr_expr_1 (arg, 0, complain);
6040 /* Take the address of ARG, but only if it's an lvalue. */
6042 static tree
6043 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6045 return cp_build_addr_expr_1 (arg, 1, complain);
6048 /* C++: Must handle pointers to members.
6050 Perhaps type instantiation should be extended to handle conversion
6051 from aggregates to types we don't yet know we want? (Or are those
6052 cases typically errors which should be reported?)
6054 NOCONVERT suppresses the default promotions (such as from short to int). */
6056 tree
6057 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6058 tsubst_flags_t complain)
6060 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6061 tree arg = xarg;
6062 location_t location = cp_expr_loc_or_loc (arg, input_location);
6063 tree argtype = 0;
6064 const char *errstring = NULL;
6065 tree val;
6066 const char *invalid_op_diag;
6068 if (!arg || error_operand_p (arg))
6069 return error_mark_node;
6071 if ((invalid_op_diag
6072 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6073 ? CONVERT_EXPR
6074 : code),
6075 TREE_TYPE (xarg))))
6077 if (complain & tf_error)
6078 error (invalid_op_diag);
6079 return error_mark_node;
6082 switch (code)
6084 case UNARY_PLUS_EXPR:
6085 case NEGATE_EXPR:
6087 int flags = WANT_ARITH | WANT_ENUM;
6088 /* Unary plus (but not unary minus) is allowed on pointers. */
6089 if (code == UNARY_PLUS_EXPR)
6090 flags |= WANT_POINTER;
6091 arg = build_expr_type_conversion (flags, arg, true);
6092 if (!arg)
6093 errstring = (code == NEGATE_EXPR
6094 ? _("wrong type argument to unary minus")
6095 : _("wrong type argument to unary plus"));
6096 else
6098 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6099 arg = cp_perform_integral_promotions (arg, complain);
6101 /* Make sure the result is not an lvalue: a unary plus or minus
6102 expression is always a rvalue. */
6103 arg = rvalue (arg);
6106 break;
6108 case BIT_NOT_EXPR:
6109 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6111 code = CONJ_EXPR;
6112 if (!noconvert)
6114 arg = cp_default_conversion (arg, complain);
6115 if (arg == error_mark_node)
6116 return error_mark_node;
6119 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6120 | WANT_VECTOR_OR_COMPLEX,
6121 arg, true)))
6122 errstring = _("wrong type argument to bit-complement");
6123 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6125 /* Warn if the expression has boolean value. */
6126 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6127 && (complain & tf_warning)
6128 && warning_at (location, OPT_Wbool_operation,
6129 "%<~%> on an expression of type bool"))
6130 inform (location, "did you mean to use logical not (%<!%>)?");
6131 arg = cp_perform_integral_promotions (arg, complain);
6133 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6134 arg = mark_rvalue_use (arg);
6135 break;
6137 case ABS_EXPR:
6138 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6139 errstring = _("wrong type argument to abs");
6140 else if (!noconvert)
6142 arg = cp_default_conversion (arg, complain);
6143 if (arg == error_mark_node)
6144 return error_mark_node;
6146 break;
6148 case CONJ_EXPR:
6149 /* Conjugating a real value is a no-op, but allow it anyway. */
6150 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6151 errstring = _("wrong type argument to conjugation");
6152 else if (!noconvert)
6154 arg = cp_default_conversion (arg, complain);
6155 if (arg == error_mark_node)
6156 return error_mark_node;
6158 break;
6160 case TRUTH_NOT_EXPR:
6161 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6162 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6163 build_zero_cst (TREE_TYPE (arg)), complain);
6164 arg = perform_implicit_conversion (boolean_type_node, arg,
6165 complain);
6166 val = invert_truthvalue_loc (input_location, arg);
6167 if (arg != error_mark_node)
6168 return val;
6169 errstring = _("in argument to unary !");
6170 break;
6172 case NOP_EXPR:
6173 break;
6175 case REALPART_EXPR:
6176 case IMAGPART_EXPR:
6177 arg = build_real_imag_expr (input_location, code, arg);
6178 return arg;
6180 case PREINCREMENT_EXPR:
6181 case POSTINCREMENT_EXPR:
6182 case PREDECREMENT_EXPR:
6183 case POSTDECREMENT_EXPR:
6184 /* Handle complex lvalues (when permitted)
6185 by reduction to simpler cases. */
6187 val = unary_complex_lvalue (code, arg);
6188 if (val != 0)
6189 return val;
6191 arg = mark_lvalue_use (arg);
6193 /* Increment or decrement the real part of the value,
6194 and don't change the imaginary part. */
6195 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6197 tree real, imag;
6199 arg = cp_stabilize_reference (arg);
6200 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6201 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6202 real = cp_build_unary_op (code, real, true, complain);
6203 if (real == error_mark_node || imag == error_mark_node)
6204 return error_mark_node;
6205 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6206 real, imag);
6209 /* Report invalid types. */
6211 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6212 arg, true)))
6214 if (code == PREINCREMENT_EXPR)
6215 errstring = _("no pre-increment operator for type");
6216 else if (code == POSTINCREMENT_EXPR)
6217 errstring = _("no post-increment operator for type");
6218 else if (code == PREDECREMENT_EXPR)
6219 errstring = _("no pre-decrement operator for type");
6220 else
6221 errstring = _("no post-decrement operator for type");
6222 break;
6224 else if (arg == error_mark_node)
6225 return error_mark_node;
6227 /* Report something read-only. */
6229 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6230 || TREE_READONLY (arg))
6232 if (complain & tf_error)
6233 cxx_readonly_error (location, arg,
6234 ((code == PREINCREMENT_EXPR
6235 || code == POSTINCREMENT_EXPR)
6236 ? lv_increment : lv_decrement));
6237 else
6238 return error_mark_node;
6242 tree inc;
6243 tree declared_type = unlowered_expr_type (arg);
6245 argtype = TREE_TYPE (arg);
6247 /* ARM $5.2.5 last annotation says this should be forbidden. */
6248 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6250 if (complain & tf_error)
6251 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6252 ? G_("ISO C++ forbids incrementing an enum")
6253 : G_("ISO C++ forbids decrementing an enum"));
6254 else
6255 return error_mark_node;
6258 /* Compute the increment. */
6260 if (TYPE_PTR_P (argtype))
6262 tree type = complete_type (TREE_TYPE (argtype));
6264 if (!COMPLETE_OR_VOID_TYPE_P (type))
6266 if (complain & tf_error)
6267 error (((code == PREINCREMENT_EXPR
6268 || code == POSTINCREMENT_EXPR))
6269 ? G_("cannot increment a pointer to incomplete type %qT")
6270 : G_("cannot decrement a pointer to incomplete type %qT"),
6271 TREE_TYPE (argtype));
6272 else
6273 return error_mark_node;
6275 else if (!TYPE_PTROB_P (argtype))
6277 if (complain & tf_error)
6278 pedwarn (input_location, OPT_Wpointer_arith,
6279 (code == PREINCREMENT_EXPR
6280 || code == POSTINCREMENT_EXPR)
6281 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6282 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6283 argtype);
6284 else
6285 return error_mark_node;
6288 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6290 else
6291 inc = VECTOR_TYPE_P (argtype)
6292 ? build_one_cst (argtype)
6293 : integer_one_node;
6295 inc = cp_convert (argtype, inc, complain);
6297 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6298 need to ask Objective-C to build the increment or decrement
6299 expression for it. */
6300 if (objc_is_property_ref (arg))
6301 return objc_build_incr_expr_for_property_ref (input_location, code,
6302 arg, inc);
6304 /* Complain about anything else that is not a true lvalue. */
6305 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6306 || code == POSTINCREMENT_EXPR)
6307 ? lv_increment : lv_decrement),
6308 complain))
6309 return error_mark_node;
6311 /* Forbid using -- or ++ in C++17 on `bool'. */
6312 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6314 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6316 if (complain & tf_error)
6317 error ("use of an operand of type %qT in %<operator--%> "
6318 "is forbidden", boolean_type_node);
6319 return error_mark_node;
6321 else
6323 if (cxx_dialect >= cxx17)
6325 if (complain & tf_error)
6326 error ("use of an operand of type %qT in "
6327 "%<operator++%> is forbidden in C++17",
6328 boolean_type_node);
6329 return error_mark_node;
6331 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6332 else if (!in_system_header_at (input_location))
6333 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6334 "in %<operator++%> is deprecated",
6335 boolean_type_node);
6337 val = boolean_increment (code, arg);
6339 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6340 /* An rvalue has no cv-qualifiers. */
6341 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6342 else
6343 val = build2 (code, TREE_TYPE (arg), arg, inc);
6345 TREE_SIDE_EFFECTS (val) = 1;
6346 return val;
6349 case ADDR_EXPR:
6350 /* Note that this operation never does default_conversion
6351 regardless of NOCONVERT. */
6352 return cp_build_addr_expr (arg, complain);
6354 default:
6355 break;
6358 if (!errstring)
6360 if (argtype == 0)
6361 argtype = TREE_TYPE (arg);
6362 return build1 (code, argtype, arg);
6365 if (complain & tf_error)
6366 error ("%s", errstring);
6367 return error_mark_node;
6370 /* Hook for the c-common bits that build a unary op. */
6371 tree
6372 build_unary_op (location_t /*location*/,
6373 enum tree_code code, tree xarg, bool noconvert)
6375 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6378 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6379 so that it is a valid lvalue even for GENERIC by replacing
6380 (lhs = rhs) with ((lhs = rhs), lhs)
6381 (--lhs) with ((--lhs), lhs)
6382 (++lhs) with ((++lhs), lhs)
6383 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6384 that it can be evaluated multiple times. */
6386 tree
6387 genericize_compound_lvalue (tree lvalue)
6389 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6390 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6391 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6392 TREE_OPERAND (lvalue, 1));
6393 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6394 lvalue, TREE_OPERAND (lvalue, 0));
6397 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6398 for certain kinds of expressions which are not really lvalues
6399 but which we can accept as lvalues.
6401 If ARG is not a kind of expression we can handle, return
6402 NULL_TREE. */
6404 tree
6405 unary_complex_lvalue (enum tree_code code, tree arg)
6407 /* Inside a template, making these kinds of adjustments is
6408 pointless; we are only concerned with the type of the
6409 expression. */
6410 if (processing_template_decl)
6411 return NULL_TREE;
6413 /* Handle (a, b) used as an "lvalue". */
6414 if (TREE_CODE (arg) == COMPOUND_EXPR)
6416 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6417 tf_warning_or_error);
6418 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6419 TREE_OPERAND (arg, 0), real_result);
6422 /* Handle (a ? b : c) used as an "lvalue". */
6423 if (TREE_CODE (arg) == COND_EXPR
6424 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6425 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6427 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6428 if (TREE_CODE (arg) == MODIFY_EXPR
6429 || TREE_CODE (arg) == PREINCREMENT_EXPR
6430 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6431 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6433 if (code != ADDR_EXPR)
6434 return NULL_TREE;
6436 /* Handle (a = b) used as an "lvalue" for `&'. */
6437 if (TREE_CODE (arg) == MODIFY_EXPR
6438 || TREE_CODE (arg) == INIT_EXPR)
6440 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6441 tf_warning_or_error);
6442 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6443 arg, real_result);
6444 TREE_NO_WARNING (arg) = 1;
6445 return arg;
6448 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6449 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6450 || TREE_CODE (arg) == OFFSET_REF)
6451 return NULL_TREE;
6453 /* We permit compiler to make function calls returning
6454 objects of aggregate type look like lvalues. */
6456 tree targ = arg;
6458 if (TREE_CODE (targ) == SAVE_EXPR)
6459 targ = TREE_OPERAND (targ, 0);
6461 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6463 if (TREE_CODE (arg) == SAVE_EXPR)
6464 targ = arg;
6465 else
6466 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6467 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6470 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6471 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6472 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6475 /* Don't let anything else be handled specially. */
6476 return NULL_TREE;
6479 /* Mark EXP saying that we need to be able to take the
6480 address of it; it should not be allocated in a register.
6481 Value is true if successful. ARRAY_REF_P is true if this
6482 is for ARRAY_REF construction - in that case we don't want
6483 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6484 it is fine to use ARRAY_REFs for vector subscripts on vector
6485 register variables.
6487 C++: we do not allow `current_class_ptr' to be addressable. */
6489 bool
6490 cxx_mark_addressable (tree exp, bool array_ref_p)
6492 tree x = exp;
6494 while (1)
6495 switch (TREE_CODE (x))
6497 case VIEW_CONVERT_EXPR:
6498 if (array_ref_p
6499 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6500 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6501 return true;
6502 /* FALLTHRU */
6503 case ADDR_EXPR:
6504 case COMPONENT_REF:
6505 case ARRAY_REF:
6506 case REALPART_EXPR:
6507 case IMAGPART_EXPR:
6508 x = TREE_OPERAND (x, 0);
6509 break;
6511 case PARM_DECL:
6512 if (x == current_class_ptr)
6514 error ("cannot take the address of %<this%>, which is an rvalue expression");
6515 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6516 return true;
6518 /* Fall through. */
6520 case VAR_DECL:
6521 /* Caller should not be trying to mark initialized
6522 constant fields addressable. */
6523 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6524 || DECL_IN_AGGR_P (x) == 0
6525 || TREE_STATIC (x)
6526 || DECL_EXTERNAL (x));
6527 /* Fall through. */
6529 case RESULT_DECL:
6530 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6531 && !DECL_ARTIFICIAL (x))
6533 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6535 error
6536 ("address of explicit register variable %qD requested", x);
6537 return false;
6539 else if (extra_warnings)
6540 warning
6541 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6543 TREE_ADDRESSABLE (x) = 1;
6544 return true;
6546 case CONST_DECL:
6547 case FUNCTION_DECL:
6548 TREE_ADDRESSABLE (x) = 1;
6549 return true;
6551 case CONSTRUCTOR:
6552 TREE_ADDRESSABLE (x) = 1;
6553 return true;
6555 case TARGET_EXPR:
6556 TREE_ADDRESSABLE (x) = 1;
6557 cxx_mark_addressable (TREE_OPERAND (x, 0));
6558 return true;
6560 default:
6561 return true;
6565 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6567 tree
6568 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6569 tsubst_flags_t complain)
6571 tree orig_ifexp = ifexp;
6572 tree orig_op1 = op1;
6573 tree orig_op2 = op2;
6574 tree expr;
6576 if (processing_template_decl)
6578 /* The standard says that the expression is type-dependent if
6579 IFEXP is type-dependent, even though the eventual type of the
6580 expression doesn't dependent on IFEXP. */
6581 if (type_dependent_expression_p (ifexp)
6582 /* As a GNU extension, the middle operand may be omitted. */
6583 || (op1 && type_dependent_expression_p (op1))
6584 || type_dependent_expression_p (op2))
6585 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6586 ifexp = build_non_dependent_expr (ifexp);
6587 if (op1)
6588 op1 = build_non_dependent_expr (op1);
6589 op2 = build_non_dependent_expr (op2);
6592 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6593 if (processing_template_decl && expr != error_mark_node)
6595 tree min = build_min_non_dep (COND_EXPR, expr,
6596 orig_ifexp, orig_op1, orig_op2);
6597 expr = convert_from_reference (min);
6599 return expr;
6602 /* Given a list of expressions, return a compound expression
6603 that performs them all and returns the value of the last of them. */
6605 tree
6606 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6607 tsubst_flags_t complain)
6609 tree expr = TREE_VALUE (list);
6611 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6612 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6614 if (complain & tf_error)
6615 pedwarn (cp_expr_loc_or_loc (expr, input_location), 0,
6616 "list-initializer for non-class type must not "
6617 "be parenthesized");
6618 else
6619 return error_mark_node;
6622 if (TREE_CHAIN (list))
6624 if (complain & tf_error)
6625 switch (exp)
6627 case ELK_INIT:
6628 permerror (input_location, "expression list treated as compound "
6629 "expression in initializer");
6630 break;
6631 case ELK_MEM_INIT:
6632 permerror (input_location, "expression list treated as compound "
6633 "expression in mem-initializer");
6634 break;
6635 case ELK_FUNC_CAST:
6636 permerror (input_location, "expression list treated as compound "
6637 "expression in functional cast");
6638 break;
6639 default:
6640 gcc_unreachable ();
6642 else
6643 return error_mark_node;
6645 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6646 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6647 expr, TREE_VALUE (list), complain);
6650 return expr;
6653 /* Like build_x_compound_expr_from_list, but using a VEC. */
6655 tree
6656 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6657 tsubst_flags_t complain)
6659 if (vec_safe_is_empty (vec))
6660 return NULL_TREE;
6661 else if (vec->length () == 1)
6662 return (*vec)[0];
6663 else
6665 tree expr;
6666 unsigned int ix;
6667 tree t;
6669 if (msg != NULL)
6671 if (complain & tf_error)
6672 permerror (input_location,
6673 "%s expression list treated as compound expression",
6674 msg);
6675 else
6676 return error_mark_node;
6679 expr = (*vec)[0];
6680 for (ix = 1; vec->iterate (ix, &t); ++ix)
6681 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6682 t, complain);
6684 return expr;
6688 /* Handle overloading of the ',' operator when needed. */
6690 tree
6691 build_x_compound_expr (location_t loc, tree op1, tree op2,
6692 tsubst_flags_t complain)
6694 tree result;
6695 tree orig_op1 = op1;
6696 tree orig_op2 = op2;
6697 tree overload = NULL_TREE;
6699 if (processing_template_decl)
6701 if (type_dependent_expression_p (op1)
6702 || type_dependent_expression_p (op2))
6703 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6704 op1 = build_non_dependent_expr (op1);
6705 op2 = build_non_dependent_expr (op2);
6708 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6709 NULL_TREE, &overload, complain);
6710 if (!result)
6711 result = cp_build_compound_expr (op1, op2, complain);
6713 if (processing_template_decl && result != error_mark_node)
6715 if (overload != NULL_TREE)
6716 return (build_min_non_dep_op_overload
6717 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6719 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6722 return result;
6725 /* Like cp_build_compound_expr, but for the c-common bits. */
6727 tree
6728 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6730 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6733 /* Build a compound expression. */
6735 tree
6736 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6738 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6740 if (lhs == error_mark_node || rhs == error_mark_node)
6741 return error_mark_node;
6743 if (TREE_CODE (rhs) == TARGET_EXPR)
6745 /* If the rhs is a TARGET_EXPR, then build the compound
6746 expression inside the target_expr's initializer. This
6747 helps the compiler to eliminate unnecessary temporaries. */
6748 tree init = TREE_OPERAND (rhs, 1);
6750 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6751 TREE_OPERAND (rhs, 1) = init;
6753 return rhs;
6756 if (type_unknown_p (rhs))
6758 if (complain & tf_error)
6759 error ("no context to resolve type of %qE", rhs);
6760 return error_mark_node;
6763 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6766 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6767 casts away constness. CAST gives the type of cast. Returns true
6768 if the cast is ill-formed, false if it is well-formed.
6770 ??? This function warns for casting away any qualifier not just
6771 const. We would like to specify exactly what qualifiers are casted
6772 away.
6775 static bool
6776 check_for_casting_away_constness (tree src_type, tree dest_type,
6777 enum tree_code cast, tsubst_flags_t complain)
6779 /* C-style casts are allowed to cast away constness. With
6780 WARN_CAST_QUAL, we still want to issue a warning. */
6781 if (cast == CAST_EXPR && !warn_cast_qual)
6782 return false;
6784 if (!casts_away_constness (src_type, dest_type, complain))
6785 return false;
6787 switch (cast)
6789 case CAST_EXPR:
6790 if (complain & tf_warning)
6791 warning (OPT_Wcast_qual,
6792 "cast from type %qT to type %qT casts away qualifiers",
6793 src_type, dest_type);
6794 return false;
6796 case STATIC_CAST_EXPR:
6797 if (complain & tf_error)
6798 error ("static_cast from type %qT to type %qT casts away qualifiers",
6799 src_type, dest_type);
6800 return true;
6802 case REINTERPRET_CAST_EXPR:
6803 if (complain & tf_error)
6804 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6805 src_type, dest_type);
6806 return true;
6808 default:
6809 gcc_unreachable();
6813 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6814 void
6815 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6817 if (warn_useless_cast
6818 && complain & tf_warning)
6820 if ((TYPE_REF_P (type)
6821 && (TYPE_REF_IS_RVALUE (type)
6822 ? xvalue_p (expr) : lvalue_p (expr))
6823 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6824 || same_type_p (TREE_TYPE (expr), type))
6825 warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6829 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6830 void
6831 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6833 if (warn_ignored_qualifiers
6834 && complain & tf_warning
6835 && !CLASS_TYPE_P (type)
6836 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6838 warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6839 "result type");
6843 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6844 (another pointer-to-member type in the same hierarchy) and return
6845 the converted expression. If ALLOW_INVERSE_P is permitted, a
6846 pointer-to-derived may be converted to pointer-to-base; otherwise,
6847 only the other direction is permitted. If C_CAST_P is true, this
6848 conversion is taking place as part of a C-style cast. */
6850 tree
6851 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6852 bool c_cast_p, tsubst_flags_t complain)
6854 if (same_type_p (type, TREE_TYPE (expr)))
6855 return expr;
6857 if (TYPE_PTRDATAMEM_P (type))
6859 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
6860 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
6861 tree delta = (get_delta_difference
6862 (obase, nbase,
6863 allow_inverse_p, c_cast_p, complain));
6865 if (delta == error_mark_node)
6866 return error_mark_node;
6868 if (!same_type_p (obase, nbase))
6870 if (TREE_CODE (expr) == PTRMEM_CST)
6871 expr = cplus_expand_constant (expr);
6873 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
6874 build_int_cst (TREE_TYPE (expr), -1),
6875 complain);
6876 tree op1 = build_nop (ptrdiff_type_node, expr);
6877 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
6878 complain);
6880 expr = fold_build3_loc (input_location,
6881 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6884 return build_nop (type, expr);
6886 else
6887 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6888 allow_inverse_p, c_cast_p, complain);
6891 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6892 this static_cast is being attempted as one of the possible casts
6893 allowed by a C-style cast. (In that case, accessibility of base
6894 classes is not considered, and it is OK to cast away
6895 constness.) Return the result of the cast. *VALID_P is set to
6896 indicate whether or not the cast was valid. */
6898 static tree
6899 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6900 bool *valid_p, tsubst_flags_t complain)
6902 tree intype;
6903 tree result;
6904 cp_lvalue_kind clk;
6906 /* Assume the cast is valid. */
6907 *valid_p = true;
6909 intype = unlowered_expr_type (expr);
6911 /* Save casted types in the function's used types hash table. */
6912 used_types_insert (type);
6914 /* A prvalue of non-class type is cv-unqualified. */
6915 if (!CLASS_TYPE_P (type))
6916 type = cv_unqualified (type);
6918 /* [expr.static.cast]
6920 An lvalue of type "cv1 B", where B is a class type, can be cast
6921 to type "reference to cv2 D", where D is a class derived (clause
6922 _class.derived_) from B, if a valid standard conversion from
6923 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6924 same cv-qualification as, or greater cv-qualification than, cv1,
6925 and B is not a virtual base class of D. */
6926 /* We check this case before checking the validity of "TYPE t =
6927 EXPR;" below because for this case:
6929 struct B {};
6930 struct D : public B { D(const B&); };
6931 extern B& b;
6932 void f() { static_cast<const D&>(b); }
6934 we want to avoid constructing a new D. The standard is not
6935 completely clear about this issue, but our interpretation is
6936 consistent with other compilers. */
6937 if (TYPE_REF_P (type)
6938 && CLASS_TYPE_P (TREE_TYPE (type))
6939 && CLASS_TYPE_P (intype)
6940 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6941 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6942 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6943 build_pointer_type (TYPE_MAIN_VARIANT
6944 (TREE_TYPE (type))),
6945 complain)
6946 && (c_cast_p
6947 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6949 tree base;
6951 if (processing_template_decl)
6952 return expr;
6954 /* There is a standard conversion from "D*" to "B*" even if "B"
6955 is ambiguous or inaccessible. If this is really a
6956 static_cast, then we check both for inaccessibility and
6957 ambiguity. However, if this is a static_cast being performed
6958 because the user wrote a C-style cast, then accessibility is
6959 not considered. */
6960 base = lookup_base (TREE_TYPE (type), intype,
6961 c_cast_p ? ba_unique : ba_check,
6962 NULL, complain);
6963 expr = build_address (expr);
6965 if (sanitize_flags_p (SANITIZE_VPTR))
6967 tree ubsan_check
6968 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6969 intype, expr);
6970 if (ubsan_check)
6971 expr = ubsan_check;
6974 /* Convert from "B*" to "D*". This function will check that "B"
6975 is not a virtual base of "D". Even if we don't have a guarantee
6976 that expr is NULL, if the static_cast is to a reference type,
6977 it is UB if it would be NULL, so omit the non-NULL check. */
6978 expr = build_base_path (MINUS_EXPR, expr, base,
6979 /*nonnull=*/flag_delete_null_pointer_checks,
6980 complain);
6982 /* Convert the pointer to a reference -- but then remember that
6983 there are no expressions with reference type in C++.
6985 We call rvalue so that there's an actual tree code
6986 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6987 is a variable with the same type, the conversion would get folded
6988 away, leaving just the variable and causing lvalue_kind to give
6989 the wrong answer. */
6990 expr = cp_fold_convert (type, expr);
6992 /* When -fsanitize=null, make sure to diagnose reference binding to
6993 NULL even when the reference is converted to pointer later on. */
6994 if (sanitize_flags_p (SANITIZE_NULL)
6995 && TREE_CODE (expr) == COND_EXPR
6996 && TREE_OPERAND (expr, 2)
6997 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
6998 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
6999 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
7001 return convert_from_reference (rvalue (expr));
7004 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7005 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7006 if (TYPE_REF_P (type)
7007 && TYPE_REF_IS_RVALUE (type)
7008 && (clk = real_lvalue_p (expr))
7009 && reference_related_p (TREE_TYPE (type), intype)
7010 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7012 if (processing_template_decl)
7013 return expr;
7014 if (clk == clk_ordinary)
7016 /* Handle the (non-bit-field) lvalue case here by casting to
7017 lvalue reference and then changing it to an rvalue reference.
7018 Casting an xvalue to rvalue reference will be handled by the
7019 main code path. */
7020 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7021 result = (perform_direct_initialization_if_possible
7022 (lref, expr, c_cast_p, complain));
7023 result = build1 (NON_LVALUE_EXPR, type, result);
7024 return convert_from_reference (result);
7026 else
7027 /* For a bit-field or packed field, bind to a temporary. */
7028 expr = rvalue (expr);
7031 /* Resolve overloaded address here rather than once in
7032 implicit_conversion and again in the inverse code below. */
7033 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7035 expr = instantiate_type (type, expr, complain);
7036 intype = TREE_TYPE (expr);
7039 /* [expr.static.cast]
7041 Any expression can be explicitly converted to type cv void. */
7042 if (VOID_TYPE_P (type))
7043 return convert_to_void (expr, ICV_CAST, complain);
7045 /* [class.abstract]
7046 An abstract class shall not be used ... as the type of an explicit
7047 conversion. */
7048 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7049 return error_mark_node;
7051 /* [expr.static.cast]
7053 An expression e can be explicitly converted to a type T using a
7054 static_cast of the form static_cast<T>(e) if the declaration T
7055 t(e);" is well-formed, for some invented temporary variable
7056 t. */
7057 result = perform_direct_initialization_if_possible (type, expr,
7058 c_cast_p, complain);
7059 if (result)
7061 if (processing_template_decl)
7062 return expr;
7064 result = convert_from_reference (result);
7066 /* [expr.static.cast]
7068 If T is a reference type, the result is an lvalue; otherwise,
7069 the result is an rvalue. */
7070 if (!TYPE_REF_P (type))
7072 result = rvalue (result);
7074 if (result == expr && SCALAR_TYPE_P (type))
7075 /* Leave some record of the cast. */
7076 result = build_nop (type, expr);
7078 return result;
7081 /* [expr.static.cast]
7083 The inverse of any standard conversion sequence (clause _conv_),
7084 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7085 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7086 (_conv.bool_) conversions, can be performed explicitly using
7087 static_cast subject to the restriction that the explicit
7088 conversion does not cast away constness (_expr.const.cast_), and
7089 the following additional rules for specific cases: */
7090 /* For reference, the conversions not excluded are: integral
7091 promotions, floating point promotion, integral conversions,
7092 floating point conversions, floating-integral conversions,
7093 pointer conversions, and pointer to member conversions. */
7094 /* DR 128
7096 A value of integral _or enumeration_ type can be explicitly
7097 converted to an enumeration type. */
7098 /* The effect of all that is that any conversion between any two
7099 types which are integral, floating, or enumeration types can be
7100 performed. */
7101 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7102 || SCALAR_FLOAT_TYPE_P (type))
7103 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7104 || SCALAR_FLOAT_TYPE_P (intype)))
7106 if (processing_template_decl)
7107 return expr;
7108 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7111 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7112 && CLASS_TYPE_P (TREE_TYPE (type))
7113 && CLASS_TYPE_P (TREE_TYPE (intype))
7114 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7115 (TREE_TYPE (intype))),
7116 build_pointer_type (TYPE_MAIN_VARIANT
7117 (TREE_TYPE (type))),
7118 complain))
7120 tree base;
7122 if (processing_template_decl)
7123 return expr;
7125 if (!c_cast_p
7126 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7127 complain))
7128 return error_mark_node;
7129 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7130 c_cast_p ? ba_unique : ba_check,
7131 NULL, complain);
7132 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7133 complain);
7135 if (sanitize_flags_p (SANITIZE_VPTR))
7137 tree ubsan_check
7138 = cp_ubsan_maybe_instrument_downcast (input_location, type,
7139 intype, expr);
7140 if (ubsan_check)
7141 expr = ubsan_check;
7144 return cp_fold_convert (type, expr);
7147 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7148 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7150 tree c1;
7151 tree c2;
7152 tree t1;
7153 tree t2;
7155 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7156 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7158 if (TYPE_PTRDATAMEM_P (type))
7160 t1 = (build_ptrmem_type
7161 (c1,
7162 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7163 t2 = (build_ptrmem_type
7164 (c2,
7165 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7167 else
7169 t1 = intype;
7170 t2 = type;
7172 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7174 if (!c_cast_p
7175 && check_for_casting_away_constness (intype, type,
7176 STATIC_CAST_EXPR,
7177 complain))
7178 return error_mark_node;
7179 if (processing_template_decl)
7180 return expr;
7181 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7182 c_cast_p, complain);
7186 /* [expr.static.cast]
7188 An rvalue of type "pointer to cv void" can be explicitly
7189 converted to a pointer to object type. A value of type pointer
7190 to object converted to "pointer to cv void" and back to the
7191 original pointer type will have its original value. */
7192 if (TYPE_PTR_P (intype)
7193 && VOID_TYPE_P (TREE_TYPE (intype))
7194 && TYPE_PTROB_P (type))
7196 if (!c_cast_p
7197 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7198 complain))
7199 return error_mark_node;
7200 if (processing_template_decl)
7201 return expr;
7202 return build_nop (type, expr);
7205 *valid_p = false;
7206 return error_mark_node;
7209 /* Return an expression representing static_cast<TYPE>(EXPR). */
7211 tree
7212 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7214 tree expr = oexpr;
7215 tree result;
7216 bool valid_p;
7218 if (type == error_mark_node || expr == error_mark_node)
7219 return error_mark_node;
7221 bool dependent = (dependent_type_p (type)
7222 || type_dependent_expression_p (expr));
7223 if (dependent)
7225 tmpl:
7226 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7227 /* We don't know if it will or will not have side effects. */
7228 TREE_SIDE_EFFECTS (expr) = 1;
7229 return convert_from_reference (expr);
7231 else if (processing_template_decl)
7232 expr = build_non_dependent_expr (expr);
7234 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7235 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7236 if (!TYPE_REF_P (type)
7237 && TREE_CODE (expr) == NOP_EXPR
7238 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7239 expr = TREE_OPERAND (expr, 0);
7241 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7242 complain);
7243 if (valid_p)
7245 if (result != error_mark_node)
7247 maybe_warn_about_useless_cast (type, expr, complain);
7248 maybe_warn_about_cast_ignoring_quals (type, complain);
7250 if (processing_template_decl)
7251 goto tmpl;
7252 return result;
7255 if (complain & tf_error)
7256 error ("invalid static_cast from type %qT to type %qT",
7257 TREE_TYPE (expr), type);
7258 return error_mark_node;
7261 /* EXPR is an expression with member function or pointer-to-member
7262 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7263 not permitted by ISO C++, but we accept it in some modes. If we
7264 are not in one of those modes, issue a diagnostic. Return the
7265 converted expression. */
7267 tree
7268 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7270 tree intype;
7271 tree decl;
7273 intype = TREE_TYPE (expr);
7274 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7275 || TREE_CODE (intype) == METHOD_TYPE);
7277 if (!(complain & tf_warning_or_error))
7278 return error_mark_node;
7280 if (pedantic || warn_pmf2ptr)
7281 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7282 "converting from %qH to %qI", intype, type);
7284 if (TREE_CODE (intype) == METHOD_TYPE)
7285 expr = build_addr_func (expr, complain);
7286 else if (TREE_CODE (expr) == PTRMEM_CST)
7287 expr = build_address (PTRMEM_CST_MEMBER (expr));
7288 else
7290 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7291 decl = build_address (decl);
7292 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7295 if (expr == error_mark_node)
7296 return error_mark_node;
7298 return build_nop (type, expr);
7301 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7302 constexpr evaluation knows to reject it. */
7304 static tree
7305 build_nop_reinterpret (tree type, tree expr)
7307 tree ret = build_nop (type, expr);
7308 if (ret != expr)
7309 REINTERPRET_CAST_P (ret) = true;
7310 return ret;
7313 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7314 If C_CAST_P is true, this reinterpret cast is being done as part of
7315 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7316 indicate whether or not reinterpret_cast was valid. */
7318 static tree
7319 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7320 bool *valid_p, tsubst_flags_t complain)
7322 tree intype;
7324 /* Assume the cast is invalid. */
7325 if (valid_p)
7326 *valid_p = true;
7328 if (type == error_mark_node || error_operand_p (expr))
7329 return error_mark_node;
7331 intype = TREE_TYPE (expr);
7333 /* Save casted types in the function's used types hash table. */
7334 used_types_insert (type);
7336 /* A prvalue of non-class type is cv-unqualified. */
7337 if (!CLASS_TYPE_P (type))
7338 type = cv_unqualified (type);
7340 /* [expr.reinterpret.cast]
7341 A glvalue expression of type T1 can be cast to the type
7342 "reference to T2" if an expression of type "pointer to T1" can be
7343 explicitly converted to the type "pointer to T2" using a
7344 reinterpret_cast. */
7345 if (TYPE_REF_P (type))
7347 if (TYPE_REF_IS_RVALUE (type))
7349 if (!obvalue_p (expr))
7350 /* Perform the temporary materialization conversion. */
7351 expr = get_target_expr_sfinae (expr, complain);
7353 else if (!lvalue_p (expr))
7355 if (complain & tf_error)
7356 error ("invalid cast of an rvalue expression of type "
7357 "%qT to type %qT",
7358 intype, type);
7359 return error_mark_node;
7362 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7363 "B" are related class types; the reinterpret_cast does not
7364 adjust the pointer. */
7365 if (TYPE_PTR_P (intype)
7366 && (complain & tf_warning)
7367 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7368 COMPARE_BASE | COMPARE_DERIVED)))
7369 warning (0, "casting %qT to %qT does not dereference pointer",
7370 intype, type);
7372 expr = cp_build_addr_expr (expr, complain);
7374 if (warn_strict_aliasing > 2)
7375 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7377 if (expr != error_mark_node)
7378 expr = build_reinterpret_cast_1
7379 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7380 valid_p, complain);
7381 if (expr != error_mark_node)
7382 /* cp_build_indirect_ref isn't right for rvalue refs. */
7383 expr = convert_from_reference (fold_convert (type, expr));
7384 return expr;
7387 /* As a G++ extension, we consider conversions from member
7388 functions, and pointers to member functions to
7389 pointer-to-function and pointer-to-void types. If
7390 -Wno-pmf-conversions has not been specified,
7391 convert_member_func_to_ptr will issue an error message. */
7392 if ((TYPE_PTRMEMFUNC_P (intype)
7393 || TREE_CODE (intype) == METHOD_TYPE)
7394 && TYPE_PTR_P (type)
7395 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7396 || VOID_TYPE_P (TREE_TYPE (type))))
7397 return convert_member_func_to_ptr (type, expr, complain);
7399 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7400 array-to-pointer, and function-to-pointer conversions are
7401 performed. */
7402 expr = decay_conversion (expr, complain);
7404 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7405 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7406 if (TREE_CODE (expr) == NOP_EXPR
7407 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7408 expr = TREE_OPERAND (expr, 0);
7410 if (error_operand_p (expr))
7411 return error_mark_node;
7413 intype = TREE_TYPE (expr);
7415 /* [expr.reinterpret.cast]
7416 A pointer can be converted to any integral type large enough to
7417 hold it. ... A value of type std::nullptr_t can be converted to
7418 an integral type; the conversion has the same meaning and
7419 validity as a conversion of (void*)0 to the integral type. */
7420 if (CP_INTEGRAL_TYPE_P (type)
7421 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7423 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7425 if (complain & tf_error)
7426 permerror (input_location, "cast from %qH to %qI loses precision",
7427 intype, type);
7428 else
7429 return error_mark_node;
7431 if (NULLPTR_TYPE_P (intype))
7432 return build_int_cst (type, 0);
7434 /* [expr.reinterpret.cast]
7435 A value of integral or enumeration type can be explicitly
7436 converted to a pointer. */
7437 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7438 /* OK */
7440 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7441 || TYPE_PTR_OR_PTRMEM_P (type))
7442 && same_type_p (type, intype))
7443 /* DR 799 */
7444 return rvalue (expr);
7445 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7447 if ((complain & tf_warning)
7448 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7449 TREE_TYPE (intype)))
7450 warning (OPT_Wcast_function_type,
7451 "cast between incompatible function types"
7452 " from %qH to %qI", intype, type);
7453 return build_nop_reinterpret (type, expr);
7455 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7457 if ((complain & tf_warning)
7458 && !cxx_safe_function_type_cast_p
7459 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7460 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7461 warning (OPT_Wcast_function_type,
7462 "cast between incompatible pointer to member types"
7463 " from %qH to %qI", intype, type);
7464 return build_nop_reinterpret (type, expr);
7466 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7467 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7469 if (!c_cast_p
7470 && check_for_casting_away_constness (intype, type,
7471 REINTERPRET_CAST_EXPR,
7472 complain))
7473 return error_mark_node;
7474 /* Warn about possible alignment problems. */
7475 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7476 && (complain & tf_warning)
7477 && !VOID_TYPE_P (type)
7478 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7479 && COMPLETE_TYPE_P (TREE_TYPE (type))
7480 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7481 && min_align_of_type (TREE_TYPE (type))
7482 > min_align_of_type (TREE_TYPE (intype)))
7483 warning (OPT_Wcast_align, "cast from %qH to %qI "
7484 "increases required alignment of target type", intype, type);
7486 if (warn_strict_aliasing <= 2)
7487 /* strict_aliasing_warning STRIP_NOPs its expr. */
7488 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7490 return build_nop_reinterpret (type, expr);
7492 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7493 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7495 if (complain & tf_warning)
7496 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7497 object pointer type or vice versa is conditionally-supported." */
7498 warning (OPT_Wconditionally_supported,
7499 "casting between pointer-to-function and pointer-to-object "
7500 "is conditionally-supported");
7501 return build_nop_reinterpret (type, expr);
7503 else if (VECTOR_TYPE_P (type))
7504 return convert_to_vector (type, expr);
7505 else if (VECTOR_TYPE_P (intype)
7506 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7507 return convert_to_integer_nofold (type, expr);
7508 else
7510 if (valid_p)
7511 *valid_p = false;
7512 if (complain & tf_error)
7513 error ("invalid cast from type %qT to type %qT", intype, type);
7514 return error_mark_node;
7517 expr = cp_convert (type, expr, complain);
7518 if (TREE_CODE (expr) == NOP_EXPR)
7519 /* Mark any nop_expr that created as a reintepret_cast. */
7520 REINTERPRET_CAST_P (expr) = true;
7521 return expr;
7524 tree
7525 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7527 tree r;
7529 if (type == error_mark_node || expr == error_mark_node)
7530 return error_mark_node;
7532 if (processing_template_decl)
7534 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7536 if (!TREE_SIDE_EFFECTS (t)
7537 && type_dependent_expression_p (expr))
7538 /* There might turn out to be side effects inside expr. */
7539 TREE_SIDE_EFFECTS (t) = 1;
7540 return convert_from_reference (t);
7543 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7544 /*valid_p=*/NULL, complain);
7545 if (r != error_mark_node)
7547 maybe_warn_about_useless_cast (type, expr, complain);
7548 maybe_warn_about_cast_ignoring_quals (type, complain);
7550 return r;
7553 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7554 return an appropriate expression. Otherwise, return
7555 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7556 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7557 performing a C-style cast, its value upon return will indicate
7558 whether or not the conversion succeeded. */
7560 static tree
7561 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7562 bool *valid_p)
7564 tree src_type;
7565 tree reference_type;
7567 /* Callers are responsible for handling error_mark_node as a
7568 destination type. */
7569 gcc_assert (dst_type != error_mark_node);
7570 /* In a template, callers should be building syntactic
7571 representations of casts, not using this machinery. */
7572 gcc_assert (!processing_template_decl);
7574 /* Assume the conversion is invalid. */
7575 if (valid_p)
7576 *valid_p = false;
7578 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7580 if (complain & tf_error)
7581 error ("invalid use of const_cast with type %qT, "
7582 "which is not a pointer, "
7583 "reference, nor a pointer-to-data-member type", dst_type);
7584 return error_mark_node;
7587 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7589 if (complain & tf_error)
7590 error ("invalid use of const_cast with type %qT, which is a pointer "
7591 "or reference to a function type", dst_type);
7592 return error_mark_node;
7595 /* A prvalue of non-class type is cv-unqualified. */
7596 dst_type = cv_unqualified (dst_type);
7598 /* Save casted types in the function's used types hash table. */
7599 used_types_insert (dst_type);
7601 src_type = TREE_TYPE (expr);
7602 /* Expressions do not really have reference types. */
7603 if (TYPE_REF_P (src_type))
7604 src_type = TREE_TYPE (src_type);
7606 /* [expr.const.cast]
7608 For two object types T1 and T2, if a pointer to T1 can be explicitly
7609 converted to the type "pointer to T2" using a const_cast, then the
7610 following conversions can also be made:
7612 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7613 type T2 using the cast const_cast<T2&>;
7615 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7616 type T2 using the cast const_cast<T2&&>; and
7618 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7619 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7621 if (TYPE_REF_P (dst_type))
7623 reference_type = dst_type;
7624 if (!TYPE_REF_IS_RVALUE (dst_type)
7625 ? lvalue_p (expr)
7626 : obvalue_p (expr))
7627 /* OK. */;
7628 else
7630 if (complain & tf_error)
7631 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7632 src_type, dst_type);
7633 return error_mark_node;
7635 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7636 src_type = build_pointer_type (src_type);
7638 else
7640 reference_type = NULL_TREE;
7641 /* If the destination type is not a reference type, the
7642 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7643 conversions are performed. */
7644 src_type = type_decays_to (src_type);
7645 if (src_type == error_mark_node)
7646 return error_mark_node;
7649 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7651 if (comp_ptr_ttypes_const (dst_type, src_type))
7653 if (valid_p)
7655 *valid_p = true;
7656 /* This cast is actually a C-style cast. Issue a warning if
7657 the user is making a potentially unsafe cast. */
7658 check_for_casting_away_constness (src_type, dst_type,
7659 CAST_EXPR, complain);
7660 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7661 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7662 && (complain & tf_warning)
7663 && min_align_of_type (TREE_TYPE (dst_type))
7664 > min_align_of_type (TREE_TYPE (src_type)))
7665 warning (OPT_Wcast_align, "cast from %qH to %qI "
7666 "increases required alignment of target type",
7667 src_type, dst_type);
7669 if (reference_type)
7671 expr = cp_build_addr_expr (expr, complain);
7672 if (expr == error_mark_node)
7673 return error_mark_node;
7674 expr = build_nop (reference_type, expr);
7675 return convert_from_reference (expr);
7677 else
7679 expr = decay_conversion (expr, complain);
7680 if (expr == error_mark_node)
7681 return error_mark_node;
7683 /* build_c_cast puts on a NOP_EXPR to make the result not an
7684 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7685 non-lvalue context. */
7686 if (TREE_CODE (expr) == NOP_EXPR
7687 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7688 expr = TREE_OPERAND (expr, 0);
7689 return build_nop (dst_type, expr);
7692 else if (valid_p
7693 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7694 TREE_TYPE (src_type)))
7695 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7696 complain);
7699 if (complain & tf_error)
7700 error ("invalid const_cast from type %qT to type %qT",
7701 src_type, dst_type);
7702 return error_mark_node;
7705 tree
7706 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7708 tree r;
7710 if (type == error_mark_node || error_operand_p (expr))
7711 return error_mark_node;
7713 if (processing_template_decl)
7715 tree t = build_min (CONST_CAST_EXPR, type, expr);
7717 if (!TREE_SIDE_EFFECTS (t)
7718 && type_dependent_expression_p (expr))
7719 /* There might turn out to be side effects inside expr. */
7720 TREE_SIDE_EFFECTS (t) = 1;
7721 return convert_from_reference (t);
7724 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7725 if (r != error_mark_node)
7727 maybe_warn_about_useless_cast (type, expr, complain);
7728 maybe_warn_about_cast_ignoring_quals (type, complain);
7730 return r;
7733 /* Like cp_build_c_cast, but for the c-common bits. */
7735 tree
7736 build_c_cast (location_t /*loc*/, tree type, tree expr)
7738 return cp_build_c_cast (type, expr, tf_warning_or_error);
7741 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7742 preserve location information even for tree nodes that don't
7743 support it. */
7745 cp_expr
7746 build_c_cast (location_t loc, tree type, cp_expr expr)
7748 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7749 result.set_location (loc);
7750 return result;
7753 /* Build an expression representing an explicit C-style cast to type
7754 TYPE of expression EXPR. */
7756 tree
7757 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7759 tree value = expr;
7760 tree result;
7761 bool valid_p;
7763 if (type == error_mark_node || error_operand_p (expr))
7764 return error_mark_node;
7766 if (processing_template_decl)
7768 tree t = build_min (CAST_EXPR, type,
7769 tree_cons (NULL_TREE, value, NULL_TREE));
7770 /* We don't know if it will or will not have side effects. */
7771 TREE_SIDE_EFFECTS (t) = 1;
7772 return convert_from_reference (t);
7775 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7776 'Class') should always be retained, because this information aids
7777 in method lookup. */
7778 if (objc_is_object_ptr (type)
7779 && objc_is_object_ptr (TREE_TYPE (expr)))
7780 return build_nop (type, expr);
7782 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7783 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7784 if (!TYPE_REF_P (type)
7785 && TREE_CODE (value) == NOP_EXPR
7786 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7787 value = TREE_OPERAND (value, 0);
7789 if (TREE_CODE (type) == ARRAY_TYPE)
7791 /* Allow casting from T1* to T2[] because Cfront allows it.
7792 NIHCL uses it. It is not valid ISO C++ however. */
7793 if (TYPE_PTR_P (TREE_TYPE (expr)))
7795 if (complain & tf_error)
7796 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7797 else
7798 return error_mark_node;
7799 type = build_pointer_type (TREE_TYPE (type));
7801 else
7803 if (complain & tf_error)
7804 error ("ISO C++ forbids casting to an array type %qT", type);
7805 return error_mark_node;
7809 if (TREE_CODE (type) == FUNCTION_TYPE
7810 || TREE_CODE (type) == METHOD_TYPE)
7812 if (complain & tf_error)
7813 error ("invalid cast to function type %qT", type);
7814 return error_mark_node;
7817 if (TYPE_PTR_P (type)
7818 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7819 /* Casting to an integer of smaller size is an error detected elsewhere. */
7820 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7821 /* Don't warn about converting any constant. */
7822 && !TREE_CONSTANT (value))
7823 warning_at (input_location, OPT_Wint_to_pointer_cast,
7824 "cast to pointer from integer of different size");
7826 /* A C-style cast can be a const_cast. */
7827 result = build_const_cast_1 (type, value, complain & tf_warning,
7828 &valid_p);
7829 if (valid_p)
7831 if (result != error_mark_node)
7833 maybe_warn_about_useless_cast (type, value, complain);
7834 maybe_warn_about_cast_ignoring_quals (type, complain);
7836 return result;
7839 /* Or a static cast. */
7840 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7841 &valid_p, complain);
7842 /* Or a reinterpret_cast. */
7843 if (!valid_p)
7844 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7845 &valid_p, complain);
7846 /* The static_cast or reinterpret_cast may be followed by a
7847 const_cast. */
7848 if (valid_p
7849 /* A valid cast may result in errors if, for example, a
7850 conversion to an ambiguous base class is required. */
7851 && !error_operand_p (result))
7853 tree result_type;
7855 maybe_warn_about_useless_cast (type, value, complain);
7856 maybe_warn_about_cast_ignoring_quals (type, complain);
7858 /* Non-class rvalues always have cv-unqualified type. */
7859 if (!CLASS_TYPE_P (type))
7860 type = TYPE_MAIN_VARIANT (type);
7861 result_type = TREE_TYPE (result);
7862 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
7863 result_type = TYPE_MAIN_VARIANT (result_type);
7864 /* If the type of RESULT does not match TYPE, perform a
7865 const_cast to make it match. If the static_cast or
7866 reinterpret_cast succeeded, we will differ by at most
7867 cv-qualification, so the follow-on const_cast is guaranteed
7868 to succeed. */
7869 if (!same_type_p (non_reference (type), non_reference (result_type)))
7871 result = build_const_cast_1 (type, result, false, &valid_p);
7872 gcc_assert (valid_p);
7874 return result;
7877 return error_mark_node;
7880 /* For use from the C common bits. */
7881 tree
7882 build_modify_expr (location_t location,
7883 tree lhs, tree /*lhs_origtype*/,
7884 enum tree_code modifycode,
7885 location_t /*rhs_location*/, tree rhs,
7886 tree /*rhs_origtype*/)
7888 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7889 tf_warning_or_error);
7892 /* Build an assignment expression of lvalue LHS from value RHS.
7893 MODIFYCODE is the code for a binary operator that we use
7894 to combine the old value of LHS with RHS to get the new value.
7895 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7897 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7899 tree
7900 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7901 tree rhs, tsubst_flags_t complain)
7903 lhs = mark_lvalue_use_nonread (lhs);
7905 tree result = NULL_TREE;
7906 tree newrhs = rhs;
7907 tree lhstype = TREE_TYPE (lhs);
7908 tree olhs = lhs;
7909 tree olhstype = lhstype;
7910 bool plain_assign = (modifycode == NOP_EXPR);
7911 bool compound_side_effects_p = false;
7912 tree preeval = NULL_TREE;
7914 /* Avoid duplicate error messages from operands that had errors. */
7915 if (error_operand_p (lhs) || error_operand_p (rhs))
7916 return error_mark_node;
7918 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7920 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7921 compound_side_effects_p = true;
7922 lhs = TREE_OPERAND (lhs, 1);
7925 /* Handle control structure constructs used as "lvalues". Note that we
7926 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7927 switch (TREE_CODE (lhs))
7929 /* Handle --foo = 5; as these are valid constructs in C++. */
7930 case PREDECREMENT_EXPR:
7931 case PREINCREMENT_EXPR:
7932 if (compound_side_effects_p)
7933 newrhs = rhs = stabilize_expr (rhs, &preeval);
7934 lhs = genericize_compound_lvalue (lhs);
7935 maybe_add_compound:
7936 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7937 and looked through the COMPOUND_EXPRs, readd them now around
7938 the resulting lhs. */
7939 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7941 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7942 tree *ptr = &TREE_OPERAND (lhs, 1);
7943 for (olhs = TREE_OPERAND (olhs, 1);
7944 TREE_CODE (olhs) == COMPOUND_EXPR;
7945 olhs = TREE_OPERAND (olhs, 1))
7947 *ptr = build2 (COMPOUND_EXPR, lhstype,
7948 TREE_OPERAND (olhs, 0), *ptr);
7949 ptr = &TREE_OPERAND (*ptr, 1);
7952 break;
7954 case MODIFY_EXPR:
7955 if (compound_side_effects_p)
7956 newrhs = rhs = stabilize_expr (rhs, &preeval);
7957 lhs = genericize_compound_lvalue (lhs);
7958 goto maybe_add_compound;
7960 case MIN_EXPR:
7961 case MAX_EXPR:
7962 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7963 when neither operand has side-effects. */
7964 if (!lvalue_or_else (lhs, lv_assign, complain))
7965 return error_mark_node;
7967 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7968 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7970 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7971 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7972 boolean_type_node,
7973 TREE_OPERAND (lhs, 0),
7974 TREE_OPERAND (lhs, 1)),
7975 TREE_OPERAND (lhs, 0),
7976 TREE_OPERAND (lhs, 1));
7977 gcc_fallthrough ();
7979 /* Handle (a ? b : c) used as an "lvalue". */
7980 case COND_EXPR:
7982 /* Produce (a ? (b = rhs) : (c = rhs))
7983 except that the RHS goes through a save-expr
7984 so the code to compute it is only emitted once. */
7985 tree cond;
7987 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7989 if (complain & tf_error)
7990 error ("void value not ignored as it ought to be");
7991 return error_mark_node;
7994 rhs = stabilize_expr (rhs, &preeval);
7996 /* Check this here to avoid odd errors when trying to convert
7997 a throw to the type of the COND_EXPR. */
7998 if (!lvalue_or_else (lhs, lv_assign, complain))
7999 return error_mark_node;
8001 cond = build_conditional_expr
8002 (input_location, TREE_OPERAND (lhs, 0),
8003 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
8004 modifycode, rhs, complain),
8005 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
8006 modifycode, rhs, complain),
8007 complain);
8009 if (cond == error_mark_node)
8010 return cond;
8011 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8012 and looked through the COMPOUND_EXPRs, readd them now around
8013 the resulting cond before adding the preevaluated rhs. */
8014 if (TREE_CODE (olhs) == COMPOUND_EXPR)
8016 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8017 TREE_OPERAND (olhs, 0), cond);
8018 tree *ptr = &TREE_OPERAND (cond, 1);
8019 for (olhs = TREE_OPERAND (olhs, 1);
8020 TREE_CODE (olhs) == COMPOUND_EXPR;
8021 olhs = TREE_OPERAND (olhs, 1))
8023 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8024 TREE_OPERAND (olhs, 0), *ptr);
8025 ptr = &TREE_OPERAND (*ptr, 1);
8028 /* Make sure the code to compute the rhs comes out
8029 before the split. */
8030 result = cond;
8031 goto ret;
8034 default:
8035 lhs = olhs;
8036 break;
8039 if (modifycode == INIT_EXPR)
8041 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8042 /* Do the default thing. */;
8043 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8045 /* Compound literal. */
8046 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8047 /* Call convert to generate an error; see PR 11063. */
8048 rhs = convert (lhstype, rhs);
8049 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8050 TREE_SIDE_EFFECTS (result) = 1;
8051 goto ret;
8053 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8054 /* Do the default thing. */;
8055 else
8057 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8058 result = build_special_member_call (lhs, complete_ctor_identifier,
8059 &rhs_vec, lhstype, LOOKUP_NORMAL,
8060 complain);
8061 release_tree_vector (rhs_vec);
8062 if (result == NULL_TREE)
8063 return error_mark_node;
8064 goto ret;
8067 else
8069 lhs = require_complete_type_sfinae (lhs, complain);
8070 if (lhs == error_mark_node)
8071 return error_mark_node;
8073 if (modifycode == NOP_EXPR)
8075 if (c_dialect_objc ())
8077 result = objc_maybe_build_modify_expr (lhs, rhs);
8078 if (result)
8079 goto ret;
8082 /* `operator=' is not an inheritable operator. */
8083 if (! MAYBE_CLASS_TYPE_P (lhstype))
8084 /* Do the default thing. */;
8085 else
8087 result = build_new_op (input_location, MODIFY_EXPR,
8088 LOOKUP_NORMAL, lhs, rhs,
8089 make_node (NOP_EXPR), /*overload=*/NULL,
8090 complain);
8091 if (result == NULL_TREE)
8092 return error_mark_node;
8093 goto ret;
8095 lhstype = olhstype;
8097 else
8099 tree init = NULL_TREE;
8101 /* A binary op has been requested. Combine the old LHS
8102 value with the RHS producing the value we should actually
8103 store into the LHS. */
8104 gcc_assert (!((TYPE_REF_P (lhstype)
8105 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8106 || MAYBE_CLASS_TYPE_P (lhstype)));
8108 /* Preevaluate the RHS to make sure its evaluation is complete
8109 before the lvalue-to-rvalue conversion of the LHS:
8111 [expr.ass] With respect to an indeterminately-sequenced
8112 function call, the operation of a compound assignment is a
8113 single evaluation. [ Note: Therefore, a function call shall
8114 not intervene between the lvalue-to-rvalue conversion and the
8115 side effect associated with any single compound assignment
8116 operator. -- end note ] */
8117 lhs = cp_stabilize_reference (lhs);
8118 rhs = decay_conversion (rhs, complain);
8119 if (rhs == error_mark_node)
8120 return error_mark_node;
8121 rhs = stabilize_expr (rhs, &init);
8122 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8123 if (newrhs == error_mark_node)
8125 if (complain & tf_error)
8126 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8127 TREE_TYPE (lhs), TREE_TYPE (rhs));
8128 return error_mark_node;
8131 if (init)
8132 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8134 /* Now it looks like a plain assignment. */
8135 modifycode = NOP_EXPR;
8136 if (c_dialect_objc ())
8138 result = objc_maybe_build_modify_expr (lhs, newrhs);
8139 if (result)
8140 goto ret;
8143 gcc_assert (!TYPE_REF_P (lhstype));
8144 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
8147 /* The left-hand side must be an lvalue. */
8148 if (!lvalue_or_else (lhs, lv_assign, complain))
8149 return error_mark_node;
8151 /* Warn about modifying something that is `const'. Don't warn if
8152 this is initialization. */
8153 if (modifycode != INIT_EXPR
8154 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8155 /* Functions are not modifiable, even though they are
8156 lvalues. */
8157 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8158 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8159 /* If it's an aggregate and any field is const, then it is
8160 effectively const. */
8161 || (CLASS_TYPE_P (lhstype)
8162 && C_TYPE_FIELDS_READONLY (lhstype))))
8164 if (complain & tf_error)
8165 cxx_readonly_error (loc, lhs, lv_assign);
8166 return error_mark_node;
8169 /* If storing into a structure or union member, it may have been given a
8170 lowered bitfield type. We need to convert to the declared type first,
8171 so retrieve it now. */
8173 olhstype = unlowered_expr_type (lhs);
8175 /* Convert new value to destination type. */
8177 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8179 int from_array;
8181 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8183 if (modifycode != INIT_EXPR)
8185 if (complain & tf_error)
8186 error ("assigning to an array from an initializer list");
8187 return error_mark_node;
8189 if (check_array_initializer (lhs, lhstype, newrhs))
8190 return error_mark_node;
8191 newrhs = digest_init (lhstype, newrhs, complain);
8192 if (newrhs == error_mark_node)
8193 return error_mark_node;
8196 /* C++11 8.5/17: "If the destination type is an array of characters,
8197 an array of char16_t, an array of char32_t, or an array of wchar_t,
8198 and the initializer is a string literal...". */
8199 else if (TREE_CODE (newrhs) == STRING_CST
8200 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8201 && modifycode == INIT_EXPR)
8203 newrhs = digest_init (lhstype, newrhs, complain);
8204 if (newrhs == error_mark_node)
8205 return error_mark_node;
8208 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8209 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8211 if (complain & tf_error)
8212 error ("incompatible types in assignment of %qT to %qT",
8213 TREE_TYPE (rhs), lhstype);
8214 return error_mark_node;
8217 /* Allow array assignment in compiler-generated code. */
8218 else if (!current_function_decl
8219 || !DECL_DEFAULTED_FN (current_function_decl))
8221 /* This routine is used for both initialization and assignment.
8222 Make sure the diagnostic message differentiates the context. */
8223 if (complain & tf_error)
8225 if (modifycode == INIT_EXPR)
8226 error ("array used as initializer");
8227 else
8228 error ("invalid array assignment");
8230 return error_mark_node;
8233 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8234 ? 1 + (modifycode != INIT_EXPR): 0;
8235 result = build_vec_init (lhs, NULL_TREE, newrhs,
8236 /*explicit_value_init_p=*/false,
8237 from_array, complain);
8238 goto ret;
8241 if (modifycode == INIT_EXPR)
8242 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8243 LOOKUP_ONLYCONVERTING. */
8244 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8245 ICR_INIT, NULL_TREE, 0,
8246 complain);
8247 else
8248 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8249 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8251 if (!same_type_p (lhstype, olhstype))
8252 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8254 if (modifycode != INIT_EXPR)
8256 if (TREE_CODE (newrhs) == CALL_EXPR
8257 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8258 newrhs = build_cplus_new (lhstype, newrhs, complain);
8260 /* Can't initialize directly from a TARGET_EXPR, since that would
8261 cause the lhs to be constructed twice, and possibly result in
8262 accidental self-initialization. So we force the TARGET_EXPR to be
8263 expanded without a target. */
8264 if (TREE_CODE (newrhs) == TARGET_EXPR)
8265 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8266 TREE_OPERAND (newrhs, 0));
8269 if (newrhs == error_mark_node)
8270 return error_mark_node;
8272 if (c_dialect_objc () && flag_objc_gc)
8274 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8276 if (result)
8277 goto ret;
8280 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8281 lhstype, lhs, newrhs);
8283 TREE_SIDE_EFFECTS (result) = 1;
8284 if (!plain_assign)
8285 TREE_NO_WARNING (result) = 1;
8287 ret:
8288 if (preeval)
8289 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8290 return result;
8293 cp_expr
8294 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8295 tree rhs, tsubst_flags_t complain)
8297 tree orig_lhs = lhs;
8298 tree orig_rhs = rhs;
8299 tree overload = NULL_TREE;
8300 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8302 if (processing_template_decl)
8304 if (modifycode == NOP_EXPR
8305 || type_dependent_expression_p (lhs)
8306 || type_dependent_expression_p (rhs))
8307 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8308 build_min_nt_loc (loc, modifycode, NULL_TREE,
8309 NULL_TREE), rhs);
8311 lhs = build_non_dependent_expr (lhs);
8312 rhs = build_non_dependent_expr (rhs);
8315 if (modifycode != NOP_EXPR)
8317 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8318 lhs, rhs, op, &overload, complain);
8319 if (rval)
8321 if (rval == error_mark_node)
8322 return rval;
8323 TREE_NO_WARNING (rval) = 1;
8324 if (processing_template_decl)
8326 if (overload != NULL_TREE)
8327 return (build_min_non_dep_op_overload
8328 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8330 return (build_min_non_dep
8331 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8333 return rval;
8336 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8339 /* Helper function for get_delta_difference which assumes FROM is a base
8340 class of TO. Returns a delta for the conversion of pointer-to-member
8341 of FROM to pointer-to-member of TO. If the conversion is invalid and
8342 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8343 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8344 If C_CAST_P is true, this conversion is taking place as part of a
8345 C-style cast. */
8347 static tree
8348 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8349 tsubst_flags_t complain)
8351 tree binfo;
8352 base_kind kind;
8354 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8355 &kind, complain);
8357 if (binfo == error_mark_node)
8359 if (!(complain & tf_error))
8360 return error_mark_node;
8362 error (" in pointer to member function conversion");
8363 return size_zero_node;
8365 else if (binfo)
8367 if (kind != bk_via_virtual)
8368 return BINFO_OFFSET (binfo);
8369 else
8370 /* FROM is a virtual base class of TO. Issue an error or warning
8371 depending on whether or not this is a reinterpret cast. */
8373 if (!(complain & tf_error))
8374 return error_mark_node;
8376 error ("pointer to member conversion via virtual base %qT",
8377 BINFO_TYPE (binfo_from_vbase (binfo)));
8379 return size_zero_node;
8382 else
8383 return NULL_TREE;
8386 /* Get difference in deltas for different pointer to member function
8387 types. If the conversion is invalid and tf_error is not set in
8388 COMPLAIN, returns error_mark_node, otherwise returns an integer
8389 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8390 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8391 conversions as well. If C_CAST_P is true this conversion is taking
8392 place as part of a C-style cast.
8394 Note that the naming of FROM and TO is kind of backwards; the return
8395 value is what we add to a TO in order to get a FROM. They are named
8396 this way because we call this function to find out how to convert from
8397 a pointer to member of FROM to a pointer to member of TO. */
8399 static tree
8400 get_delta_difference (tree from, tree to,
8401 bool allow_inverse_p,
8402 bool c_cast_p, tsubst_flags_t complain)
8404 tree result;
8406 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8407 /* Pointer to member of incomplete class is permitted*/
8408 result = size_zero_node;
8409 else
8410 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8412 if (result == error_mark_node)
8413 return error_mark_node;
8415 if (!result)
8417 if (!allow_inverse_p)
8419 if (!(complain & tf_error))
8420 return error_mark_node;
8422 error_not_base_type (from, to);
8423 error (" in pointer to member conversion");
8424 result = size_zero_node;
8426 else
8428 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8430 if (result == error_mark_node)
8431 return error_mark_node;
8433 if (result)
8434 result = size_diffop_loc (input_location,
8435 size_zero_node, result);
8436 else
8438 if (!(complain & tf_error))
8439 return error_mark_node;
8441 error_not_base_type (from, to);
8442 error (" in pointer to member conversion");
8443 result = size_zero_node;
8448 return convert_to_integer (ptrdiff_type_node, result);
8451 /* Return a constructor for the pointer-to-member-function TYPE using
8452 the other components as specified. */
8454 tree
8455 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8457 tree u = NULL_TREE;
8458 tree delta_field;
8459 tree pfn_field;
8460 vec<constructor_elt, va_gc> *v;
8462 /* Pull the FIELD_DECLs out of the type. */
8463 pfn_field = TYPE_FIELDS (type);
8464 delta_field = DECL_CHAIN (pfn_field);
8466 /* Make sure DELTA has the type we want. */
8467 delta = convert_and_check (input_location, delta_type_node, delta);
8469 /* Convert to the correct target type if necessary. */
8470 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8472 /* Finish creating the initializer. */
8473 vec_alloc (v, 2);
8474 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8475 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8476 u = build_constructor (type, v);
8477 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8478 TREE_STATIC (u) = (TREE_CONSTANT (u)
8479 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8480 != NULL_TREE)
8481 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8482 != NULL_TREE));
8483 return u;
8486 /* Build a constructor for a pointer to member function. It can be
8487 used to initialize global variables, local variable, or used
8488 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8489 want to be.
8491 If FORCE is nonzero, then force this conversion, even if
8492 we would rather not do it. Usually set when using an explicit
8493 cast. A C-style cast is being processed iff C_CAST_P is true.
8495 Return error_mark_node, if something goes wrong. */
8497 tree
8498 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8499 tsubst_flags_t complain)
8501 tree fn;
8502 tree pfn_type;
8503 tree to_type;
8505 if (error_operand_p (pfn))
8506 return error_mark_node;
8508 pfn_type = TREE_TYPE (pfn);
8509 to_type = build_ptrmemfunc_type (type);
8511 /* Handle multiple conversions of pointer to member functions. */
8512 if (TYPE_PTRMEMFUNC_P (pfn_type))
8514 tree delta = NULL_TREE;
8515 tree npfn = NULL_TREE;
8516 tree n;
8518 if (!force
8519 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8520 LOOKUP_NORMAL, complain))
8522 if (complain & tf_error)
8523 error ("invalid conversion to type %qT from type %qT",
8524 to_type, pfn_type);
8525 else
8526 return error_mark_node;
8529 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8530 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8531 force,
8532 c_cast_p, complain);
8533 if (n == error_mark_node)
8534 return error_mark_node;
8536 /* We don't have to do any conversion to convert a
8537 pointer-to-member to its own type. But, we don't want to
8538 just return a PTRMEM_CST if there's an explicit cast; that
8539 cast should make the expression an invalid template argument. */
8540 if (TREE_CODE (pfn) != PTRMEM_CST)
8542 if (same_type_p (to_type, pfn_type))
8543 return pfn;
8544 else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
8545 return build_reinterpret_cast (to_type, pfn,
8546 complain);
8549 if (TREE_SIDE_EFFECTS (pfn))
8550 pfn = save_expr (pfn);
8552 /* Obtain the function pointer and the current DELTA. */
8553 if (TREE_CODE (pfn) == PTRMEM_CST)
8554 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8555 else
8557 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8558 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8561 /* Just adjust the DELTA field. */
8562 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8563 (TREE_TYPE (delta), ptrdiff_type_node));
8564 if (!integer_zerop (n))
8566 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8567 n = cp_build_binary_op (input_location,
8568 LSHIFT_EXPR, n, integer_one_node,
8569 complain);
8570 delta = cp_build_binary_op (input_location,
8571 PLUS_EXPR, delta, n, complain);
8573 return build_ptrmemfunc1 (to_type, delta, npfn);
8576 /* Handle null pointer to member function conversions. */
8577 if (null_ptr_cst_p (pfn))
8579 pfn = cp_build_c_cast (type, pfn, complain);
8580 return build_ptrmemfunc1 (to_type,
8581 integer_zero_node,
8582 pfn);
8585 if (type_unknown_p (pfn))
8586 return instantiate_type (type, pfn, complain);
8588 fn = TREE_OPERAND (pfn, 0);
8589 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8590 /* In a template, we will have preserved the
8591 OFFSET_REF. */
8592 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8593 return make_ptrmem_cst (to_type, fn);
8596 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8597 given by CST.
8599 ??? There is no consistency as to the types returned for the above
8600 values. Some code acts as if it were a sizetype and some as if it were
8601 integer_type_node. */
8603 void
8604 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8606 tree type = TREE_TYPE (cst);
8607 tree fn = PTRMEM_CST_MEMBER (cst);
8608 tree ptr_class, fn_class;
8610 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8612 /* The class that the function belongs to. */
8613 fn_class = DECL_CONTEXT (fn);
8615 /* The class that we're creating a pointer to member of. */
8616 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8618 /* First, calculate the adjustment to the function's class. */
8619 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8620 /*c_cast_p=*/0, tf_warning_or_error);
8622 if (!DECL_VIRTUAL_P (fn))
8623 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8624 build_addr_func (fn, tf_warning_or_error));
8625 else
8627 /* If we're dealing with a virtual function, we have to adjust 'this'
8628 again, to point to the base which provides the vtable entry for
8629 fn; the call will do the opposite adjustment. */
8630 tree orig_class = DECL_CONTEXT (fn);
8631 tree binfo = binfo_or_else (orig_class, fn_class);
8632 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8633 *delta, BINFO_OFFSET (binfo));
8635 /* We set PFN to the vtable offset at which the function can be
8636 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8637 case delta is shifted left, and then incremented). */
8638 *pfn = DECL_VINDEX (fn);
8639 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8640 TYPE_SIZE_UNIT (vtable_entry_type));
8642 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8644 case ptrmemfunc_vbit_in_pfn:
8645 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8646 integer_one_node);
8647 break;
8649 case ptrmemfunc_vbit_in_delta:
8650 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8651 *delta, integer_one_node);
8652 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8653 *delta, integer_one_node);
8654 break;
8656 default:
8657 gcc_unreachable ();
8660 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8664 /* Return an expression for PFN from the pointer-to-member function
8665 given by T. */
8667 static tree
8668 pfn_from_ptrmemfunc (tree t)
8670 if (TREE_CODE (t) == PTRMEM_CST)
8672 tree delta;
8673 tree pfn;
8675 expand_ptrmemfunc_cst (t, &delta, &pfn);
8676 if (pfn)
8677 return pfn;
8680 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8683 /* Return an expression for DELTA from the pointer-to-member function
8684 given by T. */
8686 static tree
8687 delta_from_ptrmemfunc (tree t)
8689 if (TREE_CODE (t) == PTRMEM_CST)
8691 tree delta;
8692 tree pfn;
8694 expand_ptrmemfunc_cst (t, &delta, &pfn);
8695 if (delta)
8696 return delta;
8699 return build_ptrmemfunc_access_expr (t, delta_identifier);
8702 /* Convert value RHS to type TYPE as preparation for an assignment to
8703 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8704 implicit conversion is. If FNDECL is non-NULL, we are doing the
8705 conversion in order to pass the PARMNUMth argument of FNDECL.
8706 If FNDECL is NULL, we are doing the conversion in function pointer
8707 argument passing, conversion in initialization, etc. */
8709 static tree
8710 convert_for_assignment (tree type, tree rhs,
8711 impl_conv_rhs errtype, tree fndecl, int parmnum,
8712 tsubst_flags_t complain, int flags)
8714 tree rhstype;
8715 enum tree_code coder;
8717 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8718 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8719 rhs = TREE_OPERAND (rhs, 0);
8721 /* Handle [dcl.init.list] direct-list-initialization from
8722 single element of enumeration with a fixed underlying type. */
8723 if (is_direct_enum_init (type, rhs))
8725 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8726 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8728 warning_sentinel w (warn_useless_cast);
8729 warning_sentinel w2 (warn_ignored_qualifiers);
8730 rhs = cp_build_c_cast (type, elt, complain);
8732 else
8733 rhs = error_mark_node;
8736 rhstype = TREE_TYPE (rhs);
8737 coder = TREE_CODE (rhstype);
8739 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8740 && vector_types_convertible_p (type, rhstype, true))
8742 rhs = mark_rvalue_use (rhs);
8743 return convert (type, rhs);
8746 if (rhs == error_mark_node || rhstype == error_mark_node)
8747 return error_mark_node;
8748 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8749 return error_mark_node;
8751 /* The RHS of an assignment cannot have void type. */
8752 if (coder == VOID_TYPE)
8754 if (complain & tf_error)
8755 error ("void value not ignored as it ought to be");
8756 return error_mark_node;
8759 if (c_dialect_objc ())
8761 int parmno;
8762 tree selector;
8763 tree rname = fndecl;
8765 switch (errtype)
8767 case ICR_ASSIGN:
8768 parmno = -1;
8769 break;
8770 case ICR_INIT:
8771 parmno = -2;
8772 break;
8773 default:
8774 selector = objc_message_selector ();
8775 parmno = parmnum;
8776 if (selector && parmno > 1)
8778 rname = selector;
8779 parmno -= 1;
8783 if (objc_compare_types (type, rhstype, parmno, rname))
8785 rhs = mark_rvalue_use (rhs);
8786 return convert (type, rhs);
8790 /* [expr.ass]
8792 The expression is implicitly converted (clause _conv_) to the
8793 cv-unqualified type of the left operand.
8795 We allow bad conversions here because by the time we get to this point
8796 we are committed to doing the conversion. If we end up doing a bad
8797 conversion, convert_like will complain. */
8798 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8800 /* When -Wno-pmf-conversions is use, we just silently allow
8801 conversions from pointers-to-members to plain pointers. If
8802 the conversion doesn't work, cp_convert will complain. */
8803 if (!warn_pmf2ptr
8804 && TYPE_PTR_P (type)
8805 && TYPE_PTRMEMFUNC_P (rhstype))
8806 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8807 else
8809 if (complain & tf_error)
8811 /* If the right-hand side has unknown type, then it is an
8812 overloaded function. Call instantiate_type to get error
8813 messages. */
8814 if (rhstype == unknown_type_node)
8816 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8817 /* -fpermissive might allow this; recurse. */
8818 if (!seen_error ())
8819 return convert_for_assignment (type, r, errtype, fndecl,
8820 parmnum, complain, flags);
8822 else if (fndecl)
8823 complain_about_bad_argument (cp_expr_location (rhs),
8824 rhstype, type,
8825 fndecl, parmnum);
8826 else
8827 switch (errtype)
8829 case ICR_DEFAULT_ARGUMENT:
8830 error ("cannot convert %qH to %qI in default argument",
8831 rhstype, type);
8832 break;
8833 case ICR_ARGPASS:
8834 error ("cannot convert %qH to %qI in argument passing",
8835 rhstype, type);
8836 break;
8837 case ICR_CONVERTING:
8838 error ("cannot convert %qH to %qI",
8839 rhstype, type);
8840 break;
8841 case ICR_INIT:
8842 error ("cannot convert %qH to %qI in initialization",
8843 rhstype, type);
8844 break;
8845 case ICR_RETURN:
8846 error ("cannot convert %qH to %qI in return",
8847 rhstype, type);
8848 break;
8849 case ICR_ASSIGN:
8850 error ("cannot convert %qH to %qI in assignment",
8851 rhstype, type);
8852 break;
8853 default:
8854 gcc_unreachable();
8856 if (TYPE_PTR_P (rhstype)
8857 && TYPE_PTR_P (type)
8858 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8859 && CLASS_TYPE_P (TREE_TYPE (type))
8860 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8861 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8862 (TREE_TYPE (rhstype))),
8863 "class type %qT is incomplete", TREE_TYPE (rhstype));
8865 return error_mark_node;
8868 if (warn_suggest_attribute_format)
8870 const enum tree_code codel = TREE_CODE (type);
8871 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8872 && coder == codel
8873 && check_missing_format_attribute (type, rhstype)
8874 && (complain & tf_warning))
8875 switch (errtype)
8877 case ICR_ARGPASS:
8878 case ICR_DEFAULT_ARGUMENT:
8879 if (fndecl)
8880 warning (OPT_Wsuggest_attribute_format,
8881 "parameter %qP of %qD might be a candidate "
8882 "for a format attribute", parmnum, fndecl);
8883 else
8884 warning (OPT_Wsuggest_attribute_format,
8885 "parameter might be a candidate "
8886 "for a format attribute");
8887 break;
8888 case ICR_CONVERTING:
8889 warning (OPT_Wsuggest_attribute_format,
8890 "target of conversion might be a candidate "
8891 "for a format attribute");
8892 break;
8893 case ICR_INIT:
8894 warning (OPT_Wsuggest_attribute_format,
8895 "target of initialization might be a candidate "
8896 "for a format attribute");
8897 break;
8898 case ICR_RETURN:
8899 warning (OPT_Wsuggest_attribute_format,
8900 "return type might be a candidate "
8901 "for a format attribute");
8902 break;
8903 case ICR_ASSIGN:
8904 warning (OPT_Wsuggest_attribute_format,
8905 "left-hand side of assignment might be a candidate "
8906 "for a format attribute");
8907 break;
8908 default:
8909 gcc_unreachable();
8913 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8914 does not. */
8915 if (warn_parentheses
8916 && TREE_CODE (type) == BOOLEAN_TYPE
8917 && TREE_CODE (rhs) == MODIFY_EXPR
8918 && !TREE_NO_WARNING (rhs)
8919 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8920 && (complain & tf_warning))
8922 location_t loc = cp_expr_loc_or_loc (rhs, input_location);
8924 warning_at (loc, OPT_Wparentheses,
8925 "suggest parentheses around assignment used as truth value");
8926 TREE_NO_WARNING (rhs) = 1;
8929 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8930 complain, flags);
8933 /* Convert RHS to be of type TYPE.
8934 If EXP is nonzero, it is the target of the initialization.
8935 ERRTYPE indicates what kind of error the implicit conversion is.
8937 Two major differences between the behavior of
8938 `convert_for_assignment' and `convert_for_initialization'
8939 are that references are bashed in the former, while
8940 copied in the latter, and aggregates are assigned in
8941 the former (operator=) while initialized in the
8942 latter (X(X&)).
8944 If using constructor make sure no conversion operator exists, if one does
8945 exist, an ambiguity exists. */
8947 tree
8948 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8949 impl_conv_rhs errtype, tree fndecl, int parmnum,
8950 tsubst_flags_t complain)
8952 enum tree_code codel = TREE_CODE (type);
8953 tree rhstype;
8954 enum tree_code coder;
8956 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8957 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8958 if (TREE_CODE (rhs) == NOP_EXPR
8959 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8960 && codel != REFERENCE_TYPE)
8961 rhs = TREE_OPERAND (rhs, 0);
8963 if (type == error_mark_node
8964 || rhs == error_mark_node
8965 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8966 return error_mark_node;
8968 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8970 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8971 && TREE_CODE (type) != ARRAY_TYPE
8972 && (!TYPE_REF_P (type)
8973 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8974 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8975 && !TYPE_REFFN_P (type))
8976 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8977 rhs = decay_conversion (rhs, complain);
8979 rhstype = TREE_TYPE (rhs);
8980 coder = TREE_CODE (rhstype);
8982 if (coder == ERROR_MARK)
8983 return error_mark_node;
8985 /* We accept references to incomplete types, so we can
8986 return here before checking if RHS is of complete type. */
8988 if (codel == REFERENCE_TYPE)
8990 /* This should eventually happen in convert_arguments. */
8991 int savew = 0, savee = 0;
8993 if (fndecl)
8994 savew = warningcount + werrorcount, savee = errorcount;
8995 rhs = initialize_reference (type, rhs, flags, complain);
8997 if (fndecl
8998 && (warningcount + werrorcount > savew || errorcount > savee))
8999 inform (DECL_SOURCE_LOCATION (fndecl),
9000 "in passing argument %P of %qD", parmnum, fndecl);
9002 return rhs;
9005 if (exp != 0)
9006 exp = require_complete_type_sfinae (exp, complain);
9007 if (exp == error_mark_node)
9008 return error_mark_node;
9010 rhstype = non_reference (rhstype);
9012 type = complete_type (type);
9014 if (DIRECT_INIT_EXPR_P (type, rhs))
9015 /* Don't try to do copy-initialization if we already have
9016 direct-initialization. */
9017 return rhs;
9019 if (MAYBE_CLASS_TYPE_P (type))
9020 return perform_implicit_conversion_flags (type, rhs, complain, flags);
9022 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
9023 complain, flags);
9026 /* If RETVAL is the address of, or a reference to, a local variable or
9027 temporary give an appropriate warning and return true. */
9029 static bool
9030 maybe_warn_about_returning_address_of_local (tree retval)
9032 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9033 tree whats_returned = fold_for_warn (retval);
9034 location_t loc = cp_expr_loc_or_loc (retval, input_location);
9036 for (;;)
9038 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9039 whats_returned = TREE_OPERAND (whats_returned, 1);
9040 else if (CONVERT_EXPR_P (whats_returned)
9041 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9042 whats_returned = TREE_OPERAND (whats_returned, 0);
9043 else
9044 break;
9047 if (TREE_CODE (whats_returned) == TARGET_EXPR
9048 && is_std_init_list (TREE_TYPE (whats_returned)))
9050 tree init = TARGET_EXPR_INITIAL (whats_returned);
9051 if (TREE_CODE (init) == CONSTRUCTOR)
9052 /* Pull out the array address. */
9053 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
9054 else if (TREE_CODE (init) == INDIRECT_REF)
9055 /* The source of a trivial copy looks like *(T*)&var. */
9056 whats_returned = TREE_OPERAND (init, 0);
9057 else
9058 return false;
9059 STRIP_NOPS (whats_returned);
9062 /* As a special case, we handle a call to std::move or std::forward. */
9063 if (TREE_CODE (whats_returned) == CALL_EXPR
9064 && (is_std_move_p (whats_returned)
9065 || is_std_forward_p (whats_returned)))
9067 tree arg = CALL_EXPR_ARG (whats_returned, 0);
9068 return maybe_warn_about_returning_address_of_local (arg);
9071 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9072 return false;
9073 whats_returned = TREE_OPERAND (whats_returned, 0);
9075 while (TREE_CODE (whats_returned) == COMPONENT_REF
9076 || TREE_CODE (whats_returned) == ARRAY_REF)
9077 whats_returned = TREE_OPERAND (whats_returned, 0);
9079 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9080 || TREE_CODE (whats_returned) == TARGET_EXPR)
9082 if (TYPE_REF_P (valtype))
9083 warning_at (loc, OPT_Wreturn_local_addr,
9084 "returning reference to temporary");
9085 else if (is_std_init_list (valtype))
9086 warning_at (loc, OPT_Winit_list_lifetime,
9087 "returning temporary initializer_list does not extend "
9088 "the lifetime of the underlying array");
9089 return true;
9092 if (DECL_P (whats_returned)
9093 && DECL_NAME (whats_returned)
9094 && DECL_FUNCTION_SCOPE_P (whats_returned)
9095 && !is_capture_proxy (whats_returned)
9096 && !(TREE_STATIC (whats_returned)
9097 || TREE_PUBLIC (whats_returned)))
9099 bool w = false;
9100 auto_diagnostic_group d;
9101 if (TYPE_REF_P (valtype))
9102 w = warning_at (loc, OPT_Wreturn_local_addr,
9103 "reference to local variable %qD returned",
9104 whats_returned);
9105 else if (is_std_init_list (valtype))
9106 w = warning_at (loc, OPT_Winit_list_lifetime,
9107 "returning local initializer_list variable %qD "
9108 "does not extend the lifetime of the underlying array",
9109 whats_returned);
9110 else if (TREE_CODE (whats_returned) == LABEL_DECL)
9111 w = warning_at (loc, OPT_Wreturn_local_addr,
9112 "address of label %qD returned",
9113 whats_returned);
9114 else
9115 w = warning_at (loc, OPT_Wreturn_local_addr,
9116 "address of local variable %qD returned",
9117 whats_returned);
9118 if (w)
9119 inform (DECL_SOURCE_LOCATION (whats_returned),
9120 "declared here");
9121 return true;
9124 return false;
9127 /* Returns true if DECL is in the std namespace. */
9129 static bool
9130 decl_in_std_namespace_p (tree decl)
9132 return (decl != NULL_TREE
9133 && DECL_NAMESPACE_STD_P (decl_namespace_context (decl)));
9136 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
9138 static bool
9139 is_std_forward_p (tree fn)
9141 /* std::forward only takes one argument. */
9142 if (call_expr_nargs (fn) != 1)
9143 return false;
9145 tree fndecl = cp_get_callee_fndecl_nofold (fn);
9146 if (!decl_in_std_namespace_p (fndecl))
9147 return false;
9149 tree name = DECL_NAME (fndecl);
9150 return name && id_equal (name, "forward");
9153 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9155 static bool
9156 is_std_move_p (tree fn)
9158 /* std::move only takes one argument. */
9159 if (call_expr_nargs (fn) != 1)
9160 return false;
9162 tree fndecl = cp_get_callee_fndecl_nofold (fn);
9163 if (!decl_in_std_namespace_p (fndecl))
9164 return false;
9166 tree name = DECL_NAME (fndecl);
9167 return name && id_equal (name, "move");
9170 /* Returns true if RETVAL is a good candidate for the NRVO as per
9171 [class.copy.elision]. FUNCTYPE is the type the function is declared
9172 to return. */
9174 static bool
9175 can_do_nrvo_p (tree retval, tree functype)
9177 tree result = DECL_RESULT (current_function_decl);
9178 return (retval != NULL_TREE
9179 && !processing_template_decl
9180 /* Must be a local, automatic variable. */
9181 && VAR_P (retval)
9182 && DECL_CONTEXT (retval) == current_function_decl
9183 && !TREE_STATIC (retval)
9184 /* And not a lambda or anonymous union proxy. */
9185 && !DECL_HAS_VALUE_EXPR_P (retval)
9186 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9187 /* The cv-unqualified type of the returned value must be the
9188 same as the cv-unqualified return type of the
9189 function. */
9190 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9191 (TYPE_MAIN_VARIANT (functype)))
9192 /* And the returned value must be non-volatile. */
9193 && !TYPE_VOLATILE (TREE_TYPE (retval)));
9196 /* Returns true if we should treat RETVAL, an expression being returned,
9197 as if it were designated by an rvalue. See [class.copy.elision].
9198 PARM_P is true if a function parameter is OK in this context. */
9200 bool
9201 treat_lvalue_as_rvalue_p (tree retval, bool parm_ok)
9203 return ((cxx_dialect != cxx98)
9204 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9205 || (parm_ok && TREE_CODE (retval) == PARM_DECL))
9206 && DECL_CONTEXT (retval) == current_function_decl
9207 && !TREE_STATIC (retval));
9210 /* Warn about wrong usage of std::move in a return statement. RETVAL
9211 is the expression we are returning; FUNCTYPE is the type the function
9212 is declared to return. */
9214 static void
9215 maybe_warn_pessimizing_move (tree retval, tree functype)
9217 if (!(warn_pessimizing_move || warn_redundant_move))
9218 return;
9220 location_t loc = cp_expr_loc_or_loc (retval, input_location);
9222 /* C++98 doesn't know move. */
9223 if (cxx_dialect < cxx11)
9224 return;
9226 /* Wait until instantiation time, since we can't gauge if we should do
9227 the NRVO until then. */
9228 if (processing_template_decl)
9229 return;
9231 /* This is only interesting for class types. */
9232 if (!CLASS_TYPE_P (functype))
9233 return;
9235 /* We're looking for *std::move<T&> (&arg). */
9236 if (REFERENCE_REF_P (retval)
9237 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9239 tree fn = TREE_OPERAND (retval, 0);
9240 if (is_std_move_p (fn))
9242 tree arg = CALL_EXPR_ARG (fn, 0);
9243 STRIP_NOPS (arg);
9244 if (TREE_CODE (arg) == ADDR_EXPR)
9245 arg = TREE_OPERAND (arg, 0);
9246 arg = convert_from_reference (arg);
9247 /* Warn if we could do copy elision were it not for the move. */
9248 if (can_do_nrvo_p (arg, functype))
9250 auto_diagnostic_group d;
9251 if (warning_at (loc, OPT_Wpessimizing_move,
9252 "moving a local object in a return statement "
9253 "prevents copy elision"))
9254 inform (loc, "remove %<std::move%> call");
9256 /* Warn if the move is redundant. It is redundant when we would
9257 do maybe-rvalue overload resolution even without std::move. */
9258 else if (treat_lvalue_as_rvalue_p (arg, /*parm_ok*/true))
9260 auto_diagnostic_group d;
9261 if (warning_at (loc, OPT_Wredundant_move,
9262 "redundant move in return statement"))
9263 inform (loc, "remove %<std::move%> call");
9269 /* Check that returning RETVAL from the current function is valid.
9270 Return an expression explicitly showing all conversions required to
9271 change RETVAL into the function return type, and to assign it to
9272 the DECL_RESULT for the function. Set *NO_WARNING to true if
9273 code reaches end of non-void function warning shouldn't be issued
9274 on this RETURN_EXPR. */
9276 tree
9277 check_return_expr (tree retval, bool *no_warning)
9279 tree result;
9280 /* The type actually returned by the function. */
9281 tree valtype;
9282 /* The type the function is declared to return, or void if
9283 the declared type is incomplete. */
9284 tree functype;
9285 int fn_returns_value_p;
9287 *no_warning = false;
9289 /* A `volatile' function is one that isn't supposed to return, ever.
9290 (This is a G++ extension, used to get better code for functions
9291 that call the `volatile' function.) */
9292 if (TREE_THIS_VOLATILE (current_function_decl))
9293 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9295 /* Check for various simple errors. */
9296 if (DECL_DESTRUCTOR_P (current_function_decl))
9298 if (retval)
9299 error ("returning a value from a destructor");
9300 return NULL_TREE;
9302 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9304 if (in_function_try_handler)
9305 /* If a return statement appears in a handler of the
9306 function-try-block of a constructor, the program is ill-formed. */
9307 error ("cannot return from a handler of a function-try-block of a constructor");
9308 else if (retval)
9309 /* You can't return a value from a constructor. */
9310 error ("returning a value from a constructor");
9311 return NULL_TREE;
9314 const tree saved_retval = retval;
9316 if (processing_template_decl)
9318 current_function_returns_value = 1;
9320 if (check_for_bare_parameter_packs (retval))
9321 return error_mark_node;
9323 /* If one of the types might be void, we can't tell whether we're
9324 returning a value. */
9325 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9326 && !current_function_auto_return_pattern)
9327 || (retval != NULL_TREE
9328 && (TREE_TYPE (retval) == NULL_TREE
9329 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9330 goto dependent;
9333 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9335 /* Deduce auto return type from a return statement. */
9336 if (current_function_auto_return_pattern)
9338 tree auto_node;
9339 tree type;
9341 if (!retval && !is_auto (current_function_auto_return_pattern))
9343 /* Give a helpful error message. */
9344 error ("return-statement with no value, in function returning %qT",
9345 current_function_auto_return_pattern);
9346 inform (input_location, "only plain %<auto%> return type can be "
9347 "deduced to %<void%>");
9348 type = error_mark_node;
9350 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9352 error ("returning initializer list");
9353 type = error_mark_node;
9355 else
9357 if (!retval)
9358 retval = void_node;
9359 auto_node = type_uses_auto (current_function_auto_return_pattern);
9360 type = do_auto_deduction (current_function_auto_return_pattern,
9361 retval, auto_node);
9364 if (type == error_mark_node)
9365 /* Leave it. */;
9366 else if (functype == current_function_auto_return_pattern)
9367 apply_deduced_return_type (current_function_decl, type);
9368 else if (!same_type_p (type, functype))
9370 if (LAMBDA_FUNCTION_P (current_function_decl))
9371 error ("inconsistent types %qT and %qT deduced for "
9372 "lambda return type", functype, type);
9373 else
9374 error ("inconsistent deduction for auto return type: "
9375 "%qT and then %qT", functype, type);
9377 functype = type;
9380 result = DECL_RESULT (current_function_decl);
9381 valtype = TREE_TYPE (result);
9382 gcc_assert (valtype != NULL_TREE);
9383 fn_returns_value_p = !VOID_TYPE_P (valtype);
9385 /* Check for a return statement with no return value in a function
9386 that's supposed to return a value. */
9387 if (!retval && fn_returns_value_p)
9389 if (functype != error_mark_node)
9390 permerror (input_location, "return-statement with no value, in "
9391 "function returning %qT", valtype);
9392 /* Remember that this function did return. */
9393 current_function_returns_value = 1;
9394 /* And signal caller that TREE_NO_WARNING should be set on the
9395 RETURN_EXPR to avoid control reaches end of non-void function
9396 warnings in tree-cfg.c. */
9397 *no_warning = true;
9399 /* Check for a return statement with a value in a function that
9400 isn't supposed to return a value. */
9401 else if (retval && !fn_returns_value_p)
9403 if (VOID_TYPE_P (TREE_TYPE (retval)))
9404 /* You can return a `void' value from a function of `void'
9405 type. In that case, we have to evaluate the expression for
9406 its side-effects. */
9407 finish_expr_stmt (retval);
9408 else
9409 permerror (input_location,
9410 "return-statement with a value, in function "
9411 "returning %qT", valtype);
9412 current_function_returns_null = 1;
9414 /* There's really no value to return, after all. */
9415 return NULL_TREE;
9417 else if (!retval)
9418 /* Remember that this function can sometimes return without a
9419 value. */
9420 current_function_returns_null = 1;
9421 else
9422 /* Remember that this function did return a value. */
9423 current_function_returns_value = 1;
9425 /* Check for erroneous operands -- but after giving ourselves a
9426 chance to provide an error about returning a value from a void
9427 function. */
9428 if (error_operand_p (retval))
9430 current_function_return_value = error_mark_node;
9431 return error_mark_node;
9434 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9435 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9436 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9437 && ! flag_check_new
9438 && retval && null_ptr_cst_p (retval))
9439 warning (0, "%<operator new%> must not return NULL unless it is "
9440 "declared %<throw()%> (or -fcheck-new is in effect)");
9442 /* Effective C++ rule 15. See also start_function. */
9443 if (warn_ecpp
9444 && DECL_NAME (current_function_decl) == assign_op_identifier
9445 && !type_dependent_expression_p (retval))
9447 bool warn = true;
9449 /* The function return type must be a reference to the current
9450 class. */
9451 if (TYPE_REF_P (valtype)
9452 && same_type_ignoring_top_level_qualifiers_p
9453 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9455 /* Returning '*this' is obviously OK. */
9456 if (retval == current_class_ref)
9457 warn = false;
9458 /* If we are calling a function whose return type is the same of
9459 the current class reference, it is ok. */
9460 else if (INDIRECT_REF_P (retval)
9461 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9462 warn = false;
9465 if (warn)
9466 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9469 if (dependent_type_p (functype)
9470 || type_dependent_expression_p (retval))
9472 dependent:
9473 /* We should not have changed the return value. */
9474 gcc_assert (retval == saved_retval);
9475 return retval;
9478 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9480 [...] For a function with a class return type, if the expression
9481 in the return statement is the name of a local object, and the cv-
9482 unqualified type of the local object is the same as the function
9483 return type, an implementation is permitted to omit creating the tem-
9484 porary object to hold the function return value [...]
9486 So, if this is a value-returning function that always returns the same
9487 local variable, remember it.
9489 It might be nice to be more flexible, and choose the first suitable
9490 variable even if the function sometimes returns something else, but
9491 then we run the risk of clobbering the variable we chose if the other
9492 returned expression uses the chosen variable somehow. And people expect
9493 this restriction, anyway. (jason 2000-11-19)
9495 See finish_function and finalize_nrv for the rest of this optimization. */
9497 bool named_return_value_okay_p = can_do_nrvo_p (retval, functype);
9498 if (fn_returns_value_p && flag_elide_constructors)
9500 if (named_return_value_okay_p
9501 && (current_function_return_value == NULL_TREE
9502 || current_function_return_value == retval))
9503 current_function_return_value = retval;
9504 else
9505 current_function_return_value = error_mark_node;
9508 /* We don't need to do any conversions when there's nothing being
9509 returned. */
9510 if (!retval)
9511 return NULL_TREE;
9513 if (!named_return_value_okay_p)
9514 maybe_warn_pessimizing_move (retval, functype);
9516 /* Do any required conversions. */
9517 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9518 /* No conversions are required. */
9520 else
9522 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9524 /* The functype's return type will have been set to void, if it
9525 was an incomplete type. Just treat this as 'return;' */
9526 if (VOID_TYPE_P (functype))
9527 return error_mark_node;
9529 /* If we had an id-expression obfuscated by force_paren_expr, we need
9530 to undo it so we can try to treat it as an rvalue below. */
9531 retval = maybe_undo_parenthesized_ref (retval);
9533 if (processing_template_decl)
9534 retval = build_non_dependent_expr (retval);
9536 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9537 treated as an rvalue for the purposes of overload resolution to
9538 favor move constructors over copy constructors.
9540 Note that these conditions are similar to, but not as strict as,
9541 the conditions for the named return value optimization. */
9542 bool converted = false;
9543 if (treat_lvalue_as_rvalue_p (retval, /*parm_ok*/true)
9544 /* This is only interesting for class type. */
9545 && CLASS_TYPE_P (functype))
9547 tree moved = move (retval);
9548 moved = convert_for_initialization
9549 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9550 ICR_RETURN, NULL_TREE, 0, tf_none);
9551 if (moved != error_mark_node)
9553 retval = moved;
9554 converted = true;
9558 /* First convert the value to the function's return type, then
9559 to the type of return value's location to handle the
9560 case that functype is smaller than the valtype. */
9561 if (!converted)
9562 retval = convert_for_initialization
9563 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9564 tf_warning_or_error);
9565 retval = convert (valtype, retval);
9567 /* If the conversion failed, treat this just like `return;'. */
9568 if (retval == error_mark_node)
9569 return retval;
9570 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9571 else if (! cfun->returns_struct
9572 && TREE_CODE (retval) == TARGET_EXPR
9573 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9574 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9575 TREE_OPERAND (retval, 0));
9576 else if (!processing_template_decl
9577 && maybe_warn_about_returning_address_of_local (retval)
9578 && INDIRECT_TYPE_P (valtype))
9579 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9580 build_zero_cst (TREE_TYPE (retval)));
9583 if (processing_template_decl)
9584 return saved_retval;
9586 /* Actually copy the value returned into the appropriate location. */
9587 if (retval && retval != result)
9588 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9590 return retval;
9594 /* Returns nonzero if the pointer-type FROM can be converted to the
9595 pointer-type TO via a qualification conversion. If CONSTP is -1,
9596 then we return nonzero if the pointers are similar, and the
9597 cv-qualification signature of FROM is a proper subset of that of TO.
9599 If CONSTP is positive, then all outer pointers have been
9600 const-qualified. */
9602 static int
9603 comp_ptr_ttypes_real (tree to, tree from, int constp)
9605 bool to_more_cv_qualified = false;
9606 bool is_opaque_pointer = false;
9608 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9610 if (TREE_CODE (to) != TREE_CODE (from))
9611 return 0;
9613 if (TREE_CODE (from) == OFFSET_TYPE
9614 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9615 TYPE_OFFSET_BASETYPE (to)))
9616 return 0;
9618 /* Const and volatile mean something different for function types,
9619 so the usual checks are not appropriate. */
9620 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9622 if (!at_least_as_qualified_p (to, from))
9623 return 0;
9625 if (!at_least_as_qualified_p (from, to))
9627 if (constp == 0)
9628 return 0;
9629 to_more_cv_qualified = true;
9632 if (constp > 0)
9633 constp &= TYPE_READONLY (to);
9636 if (VECTOR_TYPE_P (to))
9637 is_opaque_pointer = vector_targets_convertible_p (to, from);
9639 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9640 return ((constp >= 0 || to_more_cv_qualified)
9641 && (is_opaque_pointer
9642 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9646 /* When comparing, say, char ** to char const **, this function takes
9647 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9648 types to this function. */
9651 comp_ptr_ttypes (tree to, tree from)
9653 return comp_ptr_ttypes_real (to, from, 1);
9656 /* Returns true iff FNTYPE is a non-class type that involves
9657 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9658 if a parameter type is ill-formed. */
9660 bool
9661 error_type_p (const_tree type)
9663 tree t;
9665 switch (TREE_CODE (type))
9667 case ERROR_MARK:
9668 return true;
9670 case POINTER_TYPE:
9671 case REFERENCE_TYPE:
9672 case OFFSET_TYPE:
9673 return error_type_p (TREE_TYPE (type));
9675 case FUNCTION_TYPE:
9676 case METHOD_TYPE:
9677 if (error_type_p (TREE_TYPE (type)))
9678 return true;
9679 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9680 if (error_type_p (TREE_VALUE (t)))
9681 return true;
9682 return false;
9684 case RECORD_TYPE:
9685 if (TYPE_PTRMEMFUNC_P (type))
9686 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9687 return false;
9689 default:
9690 return false;
9694 /* Returns true if to and from are (possibly multi-level) pointers to the same
9695 type or inheritance-related types, regardless of cv-quals. */
9697 bool
9698 ptr_reasonably_similar (const_tree to, const_tree from)
9700 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9702 /* Any target type is similar enough to void. */
9703 if (VOID_TYPE_P (to))
9704 return !error_type_p (from);
9705 if (VOID_TYPE_P (from))
9706 return !error_type_p (to);
9708 if (TREE_CODE (to) != TREE_CODE (from))
9709 return false;
9711 if (TREE_CODE (from) == OFFSET_TYPE
9712 && comptypes (TYPE_OFFSET_BASETYPE (to),
9713 TYPE_OFFSET_BASETYPE (from),
9714 COMPARE_BASE | COMPARE_DERIVED))
9715 continue;
9717 if (VECTOR_TYPE_P (to)
9718 && vector_types_convertible_p (to, from, false))
9719 return true;
9721 if (TREE_CODE (to) == INTEGER_TYPE
9722 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9723 return true;
9725 if (TREE_CODE (to) == FUNCTION_TYPE)
9726 return !error_type_p (to) && !error_type_p (from);
9728 if (!TYPE_PTR_P (to))
9730 /* When either type is incomplete avoid DERIVED_FROM_P,
9731 which may call complete_type (c++/57942). */
9732 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9733 return comptypes
9734 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9735 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9740 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9741 pointer-to-member types) are the same, ignoring cv-qualification at
9742 all levels. */
9744 bool
9745 comp_ptr_ttypes_const (tree to, tree from)
9747 bool is_opaque_pointer = false;
9749 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9751 if (TREE_CODE (to) != TREE_CODE (from))
9752 return false;
9754 if (TREE_CODE (from) == OFFSET_TYPE
9755 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9756 TYPE_OFFSET_BASETYPE (to)))
9757 continue;
9759 if (VECTOR_TYPE_P (to))
9760 is_opaque_pointer = vector_targets_convertible_p (to, from);
9762 if (!TYPE_PTR_P (to))
9763 return (is_opaque_pointer
9764 || same_type_ignoring_top_level_qualifiers_p (to, from));
9768 /* Returns the type qualifiers for this type, including the qualifiers on the
9769 elements for an array type. */
9772 cp_type_quals (const_tree type)
9774 int quals;
9775 /* This CONST_CAST is okay because strip_array_types returns its
9776 argument unmodified and we assign it to a const_tree. */
9777 type = strip_array_types (CONST_CAST_TREE (type));
9778 if (type == error_mark_node
9779 /* Quals on a FUNCTION_TYPE are memfn quals. */
9780 || TREE_CODE (type) == FUNCTION_TYPE)
9781 return TYPE_UNQUALIFIED;
9782 quals = TYPE_QUALS (type);
9783 /* METHOD and REFERENCE_TYPEs should never have quals. */
9784 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9785 && !TYPE_REF_P (type))
9786 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9787 == TYPE_UNQUALIFIED));
9788 return quals;
9791 /* Returns the function-ref-qualifier for TYPE */
9793 cp_ref_qualifier
9794 type_memfn_rqual (const_tree type)
9796 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9797 || TREE_CODE (type) == METHOD_TYPE);
9799 if (!FUNCTION_REF_QUALIFIED (type))
9800 return REF_QUAL_NONE;
9801 else if (FUNCTION_RVALUE_QUALIFIED (type))
9802 return REF_QUAL_RVALUE;
9803 else
9804 return REF_QUAL_LVALUE;
9807 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9808 METHOD_TYPE. */
9811 type_memfn_quals (const_tree type)
9813 if (TREE_CODE (type) == FUNCTION_TYPE)
9814 return TYPE_QUALS (type);
9815 else if (TREE_CODE (type) == METHOD_TYPE)
9816 return cp_type_quals (class_of_this_parm (type));
9817 else
9818 gcc_unreachable ();
9821 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9822 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9824 tree
9825 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9827 /* Could handle METHOD_TYPE here if necessary. */
9828 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9829 if (TYPE_QUALS (type) == memfn_quals
9830 && type_memfn_rqual (type) == rqual)
9831 return type;
9833 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9834 complex. */
9835 tree result = build_qualified_type (type, memfn_quals);
9836 return build_ref_qualified_type (result, rqual);
9839 /* Returns nonzero if TYPE is const or volatile. */
9841 bool
9842 cv_qualified_p (const_tree type)
9844 int quals = cp_type_quals (type);
9845 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9848 /* Returns nonzero if the TYPE contains a mutable member. */
9850 bool
9851 cp_has_mutable_p (const_tree type)
9853 /* This CONST_CAST is okay because strip_array_types returns its
9854 argument unmodified and we assign it to a const_tree. */
9855 type = strip_array_types (CONST_CAST_TREE(type));
9857 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9860 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9861 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9862 approximation. In particular, consider:
9864 int f();
9865 struct S { int i; };
9866 const S s = { f(); }
9868 Here, we will make "s" as TREE_READONLY (because it is declared
9869 "const") -- only to reverse ourselves upon seeing that the
9870 initializer is non-constant. */
9872 void
9873 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9875 tree type = TREE_TYPE (decl);
9877 if (type == error_mark_node)
9878 return;
9880 if (TREE_CODE (decl) == TYPE_DECL)
9881 return;
9883 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9884 && type_quals != TYPE_UNQUALIFIED));
9886 /* Avoid setting TREE_READONLY incorrectly. */
9887 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9888 constructor can produce constant init, so rely on cp_finish_decl to
9889 clear TREE_READONLY if the variable has non-constant init. */
9891 /* If the type has (or might have) a mutable component, that component
9892 might be modified. */
9893 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9894 type_quals &= ~TYPE_QUAL_CONST;
9896 c_apply_type_quals_to_decl (type_quals, decl);
9899 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9900 exemplar types such that casting T1 to T2 is casting away constness
9901 if and only if there is no implicit conversion from T1 to T2. */
9903 static void
9904 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9906 int quals1;
9907 int quals2;
9909 /* [expr.const.cast]
9911 For multi-level pointer to members and multi-level mixed pointers
9912 and pointers to members (conv.qual), the "member" aspect of a
9913 pointer to member level is ignored when determining if a const
9914 cv-qualifier has been cast away. */
9915 /* [expr.const.cast]
9917 For two pointer types:
9919 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9920 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9921 K is min(N,M)
9923 casting from X1 to X2 casts away constness if, for a non-pointer
9924 type T there does not exist an implicit conversion (clause
9925 _conv_) from:
9927 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9931 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9932 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9933 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9935 *t1 = cp_build_qualified_type (void_type_node,
9936 cp_type_quals (*t1));
9937 *t2 = cp_build_qualified_type (void_type_node,
9938 cp_type_quals (*t2));
9939 return;
9942 quals1 = cp_type_quals (*t1);
9943 quals2 = cp_type_quals (*t2);
9945 if (TYPE_PTRDATAMEM_P (*t1))
9946 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9947 else
9948 *t1 = TREE_TYPE (*t1);
9949 if (TYPE_PTRDATAMEM_P (*t2))
9950 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9951 else
9952 *t2 = TREE_TYPE (*t2);
9954 casts_away_constness_r (t1, t2, complain);
9955 *t1 = build_pointer_type (*t1);
9956 *t2 = build_pointer_type (*t2);
9957 *t1 = cp_build_qualified_type (*t1, quals1);
9958 *t2 = cp_build_qualified_type (*t2, quals2);
9961 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9962 constness.
9964 ??? This function returns non-zero if casting away qualifiers not
9965 just const. We would like to return to the caller exactly which
9966 qualifiers are casted away to give more accurate diagnostics.
9969 static bool
9970 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9972 if (TYPE_REF_P (t2))
9974 /* [expr.const.cast]
9976 Casting from an lvalue of type T1 to an lvalue of type T2
9977 using a reference cast casts away constness if a cast from an
9978 rvalue of type "pointer to T1" to the type "pointer to T2"
9979 casts away constness. */
9980 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
9981 return casts_away_constness (build_pointer_type (t1),
9982 build_pointer_type (TREE_TYPE (t2)),
9983 complain);
9986 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9987 /* [expr.const.cast]
9989 Casting from an rvalue of type "pointer to data member of X
9990 of type T1" to the type "pointer to data member of Y of type
9991 T2" casts away constness if a cast from an rvalue of type
9992 "pointer to T1" to the type "pointer to T2" casts away
9993 constness. */
9994 return casts_away_constness
9995 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9996 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9997 complain);
9999 /* Casting away constness is only something that makes sense for
10000 pointer or reference types. */
10001 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
10002 return false;
10004 /* Top-level qualifiers don't matter. */
10005 t1 = TYPE_MAIN_VARIANT (t1);
10006 t2 = TYPE_MAIN_VARIANT (t2);
10007 casts_away_constness_r (&t1, &t2, complain);
10008 if (!can_convert (t2, t1, complain))
10009 return true;
10011 return false;
10014 /* If T is a REFERENCE_TYPE return the type to which T refers.
10015 Otherwise, return T itself. */
10017 tree
10018 non_reference (tree t)
10020 if (t && TYPE_REF_P (t))
10021 t = TREE_TYPE (t);
10022 return t;
10026 /* Return nonzero if REF is an lvalue valid for this language;
10027 otherwise, print an error message and return zero. USE says
10028 how the lvalue is being used and so selects the error message. */
10031 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
10033 cp_lvalue_kind kind = lvalue_kind (ref);
10035 if (kind == clk_none)
10037 if (complain & tf_error)
10038 lvalue_error (input_location, use);
10039 return 0;
10041 else if (kind & (clk_rvalueref|clk_class))
10043 if (!(complain & tf_error))
10044 return 0;
10045 /* Make this a permerror because we used to accept it. */
10046 permerror (input_location, "using rvalue as lvalue");
10048 return 1;
10051 /* Return true if a user-defined literal operator is a raw operator. */
10053 bool
10054 check_raw_literal_operator (const_tree decl)
10056 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10057 tree argtype;
10058 int arity;
10059 bool maybe_raw_p = false;
10061 /* Count the number and type of arguments and check for ellipsis. */
10062 for (argtype = argtypes, arity = 0;
10063 argtype && argtype != void_list_node;
10064 ++arity, argtype = TREE_CHAIN (argtype))
10066 tree t = TREE_VALUE (argtype);
10068 if (same_type_p (t, const_string_type_node))
10069 maybe_raw_p = true;
10071 if (!argtype)
10072 return false; /* Found ellipsis. */
10074 if (!maybe_raw_p || arity != 1)
10075 return false;
10077 return true;
10081 /* Return true if a user-defined literal operator has one of the allowed
10082 argument types. */
10084 bool
10085 check_literal_operator_args (const_tree decl,
10086 bool *long_long_unsigned_p, bool *long_double_p)
10088 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
10090 *long_long_unsigned_p = false;
10091 *long_double_p = false;
10092 if (processing_template_decl || processing_specialization)
10093 return argtypes == void_list_node;
10094 else
10096 tree argtype;
10097 int arity;
10098 int max_arity = 2;
10100 /* Count the number and type of arguments and check for ellipsis. */
10101 for (argtype = argtypes, arity = 0;
10102 argtype && argtype != void_list_node;
10103 argtype = TREE_CHAIN (argtype))
10105 tree t = TREE_VALUE (argtype);
10106 ++arity;
10108 if (TYPE_PTR_P (t))
10110 bool maybe_raw_p = false;
10111 t = TREE_TYPE (t);
10112 if (cp_type_quals (t) != TYPE_QUAL_CONST)
10113 return false;
10114 t = TYPE_MAIN_VARIANT (t);
10115 if ((maybe_raw_p = same_type_p (t, char_type_node))
10116 || same_type_p (t, wchar_type_node)
10117 || same_type_p (t, char16_type_node)
10118 || same_type_p (t, char32_type_node))
10120 argtype = TREE_CHAIN (argtype);
10121 if (!argtype)
10122 return false;
10123 t = TREE_VALUE (argtype);
10124 if (maybe_raw_p && argtype == void_list_node)
10125 return true;
10126 else if (same_type_p (t, size_type_node))
10128 ++arity;
10129 continue;
10131 else
10132 return false;
10135 else if (same_type_p (t, long_long_unsigned_type_node))
10137 max_arity = 1;
10138 *long_long_unsigned_p = true;
10140 else if (same_type_p (t, long_double_type_node))
10142 max_arity = 1;
10143 *long_double_p = true;
10145 else if (same_type_p (t, char_type_node))
10146 max_arity = 1;
10147 else if (same_type_p (t, wchar_type_node))
10148 max_arity = 1;
10149 else if (same_type_p (t, char16_type_node))
10150 max_arity = 1;
10151 else if (same_type_p (t, char32_type_node))
10152 max_arity = 1;
10153 else
10154 return false;
10156 if (!argtype)
10157 return false; /* Found ellipsis. */
10159 if (arity != max_arity)
10160 return false;
10162 return true;
10166 /* Always returns false since unlike C90, C++ has no concept of implicit
10167 function declarations. */
10169 bool
10170 c_decl_implicit (const_tree)
10172 return false;