Add support for adjusting the number of units in a mode
[official-gcc.git] / gcc / cp / typeck.c
blobdc04b4bee848f93b72acb6f2d5942a50f9258e4b
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);
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.)
69 Returns error_mark_node if the VALUE does not have
70 complete type when this function returns. */
72 tree
73 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
75 tree type;
77 if (processing_template_decl || value == error_mark_node)
78 return value;
80 if (TREE_CODE (value) == OVERLOAD)
81 type = unknown_type_node;
82 else
83 type = TREE_TYPE (value);
85 if (type == error_mark_node)
86 return error_mark_node;
88 /* First, detect a valid value with a complete type. */
89 if (COMPLETE_TYPE_P (type))
90 return value;
92 if (complete_type_or_maybe_complain (type, value, complain))
93 return value;
94 else
95 return error_mark_node;
98 tree
99 require_complete_type (tree value)
101 return require_complete_type_sfinae (value, tf_warning_or_error);
104 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
105 a template instantiation, do the instantiation. Returns TYPE,
106 whether or not it could be completed, unless something goes
107 horribly wrong, in which case the error_mark_node is returned. */
109 tree
110 complete_type (tree type)
112 if (type == NULL_TREE)
113 /* Rather than crash, we return something sure to cause an error
114 at some point. */
115 return error_mark_node;
117 if (type == error_mark_node || COMPLETE_TYPE_P (type))
119 else if (TREE_CODE (type) == ARRAY_TYPE)
121 tree t = complete_type (TREE_TYPE (type));
122 unsigned int needs_constructing, has_nontrivial_dtor;
123 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
124 layout_type (type);
125 needs_constructing
126 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
127 has_nontrivial_dtor
128 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
129 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
131 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
132 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
135 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
136 instantiate_class_template (TYPE_MAIN_VARIANT (type));
138 return type;
141 /* Like complete_type, but issue an error if the TYPE cannot be completed.
142 VALUE is used for informative diagnostics.
143 Returns NULL_TREE if the type cannot be made complete. */
145 tree
146 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
148 type = complete_type (type);
149 if (type == error_mark_node)
150 /* We already issued an error. */
151 return NULL_TREE;
152 else if (!COMPLETE_TYPE_P (type))
154 if (complain & tf_error)
155 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
156 return NULL_TREE;
158 else
159 return type;
162 tree
163 complete_type_or_else (tree type, tree value)
165 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
169 /* Return the common type of two parameter lists.
170 We assume that comptypes has already been done and returned 1;
171 if that isn't so, this may crash.
173 As an optimization, free the space we allocate if the parameter
174 lists are already common. */
176 static tree
177 commonparms (tree p1, tree p2)
179 tree oldargs = p1, newargs, n;
180 int i, len;
181 int any_change = 0;
183 len = list_length (p1);
184 newargs = tree_last (p1);
186 if (newargs == void_list_node)
187 i = 1;
188 else
190 i = 0;
191 newargs = 0;
194 for (; i < len; i++)
195 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
197 n = newargs;
199 for (i = 0; p1;
200 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
202 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
204 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
205 any_change = 1;
207 else if (! TREE_PURPOSE (p1))
209 if (TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
212 any_change = 1;
215 else
217 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
218 any_change = 1;
219 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 if (TREE_VALUE (p1) != TREE_VALUE (p2))
223 any_change = 1;
224 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
226 else
227 TREE_VALUE (n) = TREE_VALUE (p1);
229 if (! any_change)
230 return oldargs;
232 return newargs;
235 /* Given a type, perhaps copied for a typedef,
236 find the "original" version of it. */
237 static tree
238 original_type (tree t)
240 int quals = cp_type_quals (t);
241 while (t != error_mark_node
242 && TYPE_NAME (t) != NULL_TREE)
244 tree x = TYPE_NAME (t);
245 if (TREE_CODE (x) != TYPE_DECL)
246 break;
247 x = DECL_ORIGINAL_TYPE (x);
248 if (x == NULL_TREE)
249 break;
250 t = x;
252 return cp_build_qualified_type (t, quals);
255 /* Return the common type for two arithmetic types T1 and T2 under the
256 usual arithmetic conversions. The default conversions have already
257 been applied, and enumerated types converted to their compatible
258 integer types. */
260 static tree
261 cp_common_type (tree t1, tree t2)
263 enum tree_code code1 = TREE_CODE (t1);
264 enum tree_code code2 = TREE_CODE (t2);
265 tree attributes;
266 int i;
269 /* In what follows, we slightly generalize the rules given in [expr] so
270 as to deal with `long long' and `complex'. First, merge the
271 attributes. */
272 attributes = (*targetm.merge_type_attributes) (t1, t2);
274 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
276 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
277 return build_type_attribute_variant (t1, attributes);
278 else
279 return NULL_TREE;
282 /* FIXME: Attributes. */
283 gcc_assert (ARITHMETIC_TYPE_P (t1)
284 || VECTOR_TYPE_P (t1)
285 || UNSCOPED_ENUM_P (t1));
286 gcc_assert (ARITHMETIC_TYPE_P (t2)
287 || VECTOR_TYPE_P (t2)
288 || UNSCOPED_ENUM_P (t2));
290 /* If one type is complex, form the common type of the non-complex
291 components, then make that complex. Use T1 or T2 if it is the
292 required type. */
293 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
295 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
296 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
297 tree subtype
298 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
300 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
301 return build_type_attribute_variant (t1, attributes);
302 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
303 return build_type_attribute_variant (t2, attributes);
304 else
305 return build_type_attribute_variant (build_complex_type (subtype),
306 attributes);
309 if (code1 == VECTOR_TYPE)
311 /* When we get here we should have two vectors of the same size.
312 Just prefer the unsigned one if present. */
313 if (TYPE_UNSIGNED (t1))
314 return build_type_attribute_variant (t1, attributes);
315 else
316 return build_type_attribute_variant (t2, attributes);
319 /* If only one is real, use it as the result. */
320 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
321 return build_type_attribute_variant (t1, attributes);
322 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
323 return build_type_attribute_variant (t2, attributes);
325 /* Both real or both integers; use the one with greater precision. */
326 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
327 return build_type_attribute_variant (t1, attributes);
328 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
329 return build_type_attribute_variant (t2, attributes);
331 /* The types are the same; no need to do anything fancy. */
332 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
333 return build_type_attribute_variant (t1, attributes);
335 if (code1 != REAL_TYPE)
337 /* If one is unsigned long long, then convert the other to unsigned
338 long long. */
339 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
340 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
341 return build_type_attribute_variant (long_long_unsigned_type_node,
342 attributes);
343 /* If one is a long long, and the other is an unsigned long, and
344 long long can represent all the values of an unsigned long, then
345 convert to a long long. Otherwise, convert to an unsigned long
346 long. Otherwise, if either operand is long long, convert the
347 other to long long.
349 Since we're here, we know the TYPE_PRECISION is the same;
350 therefore converting to long long cannot represent all the values
351 of an unsigned long, so we choose unsigned long long in that
352 case. */
353 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
354 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
356 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
357 ? long_long_unsigned_type_node
358 : long_long_integer_type_node);
359 return build_type_attribute_variant (t, attributes);
362 /* Go through the same procedure, but for longs. */
363 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
364 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
365 return build_type_attribute_variant (long_unsigned_type_node,
366 attributes);
367 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
368 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
370 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
371 ? long_unsigned_type_node : long_integer_type_node);
372 return build_type_attribute_variant (t, attributes);
375 /* For __intN types, either the type is __int128 (and is lower
376 priority than the types checked above, but higher than other
377 128-bit types) or it's known to not be the same size as other
378 types (enforced in toplev.c). Prefer the unsigned type. */
379 for (i = 0; i < NUM_INT_N_ENTS; i ++)
381 if (int_n_enabled_p [i]
382 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
383 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
384 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? int_n_trees[i].unsigned_type
389 : int_n_trees[i].signed_type);
390 return build_type_attribute_variant (t, attributes);
394 /* Otherwise prefer the unsigned one. */
395 if (TYPE_UNSIGNED (t1))
396 return build_type_attribute_variant (t1, attributes);
397 else
398 return build_type_attribute_variant (t2, attributes);
400 else
402 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
403 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
404 return build_type_attribute_variant (long_double_type_node,
405 attributes);
406 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
407 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
408 return build_type_attribute_variant (double_type_node,
409 attributes);
410 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
411 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
412 return build_type_attribute_variant (float_type_node,
413 attributes);
415 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
416 the standard C++ floating-point types. Logic earlier in this
417 function has already eliminated the possibility that
418 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
419 compelling reason to choose one or the other. */
420 return build_type_attribute_variant (t1, attributes);
424 /* T1 and T2 are arithmetic or enumeration types. Return the type
425 that will result from the "usual arithmetic conversions" on T1 and
426 T2 as described in [expr]. */
428 tree
429 type_after_usual_arithmetic_conversions (tree t1, tree t2)
431 gcc_assert (ARITHMETIC_TYPE_P (t1)
432 || VECTOR_TYPE_P (t1)
433 || UNSCOPED_ENUM_P (t1));
434 gcc_assert (ARITHMETIC_TYPE_P (t2)
435 || VECTOR_TYPE_P (t2)
436 || UNSCOPED_ENUM_P (t2));
438 /* Perform the integral promotions. We do not promote real types here. */
439 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
440 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
442 t1 = type_promotes_to (t1);
443 t2 = type_promotes_to (t2);
446 return cp_common_type (t1, t2);
449 static void
450 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
451 composite_pointer_operation operation)
453 switch (operation)
455 case CPO_COMPARISON:
456 emit_diagnostic (kind, input_location, 0,
457 "comparison between "
458 "distinct pointer types %qT and %qT lacks a cast",
459 t1, t2);
460 break;
461 case CPO_CONVERSION:
462 emit_diagnostic (kind, input_location, 0,
463 "conversion between "
464 "distinct pointer types %qT and %qT lacks a cast",
465 t1, t2);
466 break;
467 case CPO_CONDITIONAL_EXPR:
468 emit_diagnostic (kind, input_location, 0,
469 "conditional expression between "
470 "distinct pointer types %qT and %qT lacks a cast",
471 t1, t2);
472 break;
473 default:
474 gcc_unreachable ();
478 /* Subroutine of composite_pointer_type to implement the recursive
479 case. See that function for documentation of the parameters. */
481 static tree
482 composite_pointer_type_r (tree t1, tree t2,
483 composite_pointer_operation operation,
484 tsubst_flags_t complain)
486 tree pointee1;
487 tree pointee2;
488 tree result_type;
489 tree attributes;
491 /* Determine the types pointed to by T1 and T2. */
492 if (TYPE_PTR_P (t1))
494 pointee1 = TREE_TYPE (t1);
495 pointee2 = TREE_TYPE (t2);
497 else
499 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
500 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
503 /* [expr.rel]
505 Otherwise, the composite pointer type is a pointer type
506 similar (_conv.qual_) to the type of one of the operands,
507 with a cv-qualification signature (_conv.qual_) that is the
508 union of the cv-qualification signatures of the operand
509 types. */
510 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
511 result_type = pointee1;
512 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
513 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
515 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
516 complain);
517 if (result_type == error_mark_node)
518 return error_mark_node;
520 else
522 if (complain & tf_error)
523 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
524 else
525 return error_mark_node;
526 result_type = void_type_node;
528 result_type = cp_build_qualified_type (result_type,
529 (cp_type_quals (pointee1)
530 | cp_type_quals (pointee2)));
531 /* If the original types were pointers to members, so is the
532 result. */
533 if (TYPE_PTRMEM_P (t1))
535 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
536 TYPE_PTRMEM_CLASS_TYPE (t2)))
538 if (complain & tf_error)
539 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
540 else
541 return error_mark_node;
543 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
544 result_type);
546 else
547 result_type = build_pointer_type (result_type);
549 /* Merge the attributes. */
550 attributes = (*targetm.merge_type_attributes) (t1, t2);
551 return build_type_attribute_variant (result_type, attributes);
554 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
555 ARG1 and ARG2 are the values with those types. The OPERATION is to
556 describe the operation between the pointer types,
557 in case an error occurs.
559 This routine also implements the computation of a common type for
560 pointers-to-members as per [expr.eq]. */
562 tree
563 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
564 composite_pointer_operation operation,
565 tsubst_flags_t complain)
567 tree class1;
568 tree class2;
570 /* [expr.rel]
572 If one operand is a null pointer constant, the composite pointer
573 type is the type of the other operand. */
574 if (null_ptr_cst_p (arg1))
575 return t2;
576 if (null_ptr_cst_p (arg2))
577 return t1;
579 /* We have:
581 [expr.rel]
583 If one of the operands has type "pointer to cv1 void*", then
584 the other has type "pointer to cv2T", and the composite pointer
585 type is "pointer to cv12 void", where cv12 is the union of cv1
586 and cv2.
588 If either type is a pointer to void, make sure it is T1. */
589 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
590 std::swap (t1, t2);
592 /* Now, if T1 is a pointer to void, merge the qualifiers. */
593 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
595 tree attributes;
596 tree result_type;
598 if (TYPE_PTRFN_P (t2))
600 if (complain & tf_error)
602 switch (operation)
604 case CPO_COMPARISON:
605 pedwarn (input_location, OPT_Wpedantic,
606 "ISO C++ forbids comparison between pointer "
607 "of type %<void *%> and pointer-to-function");
608 break;
609 case CPO_CONVERSION:
610 pedwarn (input_location, OPT_Wpedantic,
611 "ISO C++ forbids conversion between pointer "
612 "of type %<void *%> and pointer-to-function");
613 break;
614 case CPO_CONDITIONAL_EXPR:
615 pedwarn (input_location, OPT_Wpedantic,
616 "ISO C++ forbids conditional expression between "
617 "pointer of type %<void *%> and "
618 "pointer-to-function");
619 break;
620 default:
621 gcc_unreachable ();
624 else
625 return error_mark_node;
627 result_type
628 = cp_build_qualified_type (void_type_node,
629 (cp_type_quals (TREE_TYPE (t1))
630 | cp_type_quals (TREE_TYPE (t2))));
631 result_type = build_pointer_type (result_type);
632 /* Merge the attributes. */
633 attributes = (*targetm.merge_type_attributes) (t1, t2);
634 return build_type_attribute_variant (result_type, attributes);
637 if (c_dialect_objc () && TYPE_PTR_P (t1)
638 && TYPE_PTR_P (t2))
640 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
641 return objc_common_type (t1, t2);
644 /* if T1 or T2 is "pointer to noexcept function" and the other type is
645 "pointer to function", where the function types are otherwise the same,
646 "pointer to function" */
647 if (fnptr_conv_p (t1, t2))
648 return t1;
649 if (fnptr_conv_p (t2, t1))
650 return t2;
652 /* [expr.eq] permits the application of a pointer conversion to
653 bring the pointers to a common type. */
654 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
655 && CLASS_TYPE_P (TREE_TYPE (t1))
656 && CLASS_TYPE_P (TREE_TYPE (t2))
657 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
658 TREE_TYPE (t2)))
660 class1 = TREE_TYPE (t1);
661 class2 = TREE_TYPE (t2);
663 if (DERIVED_FROM_P (class1, class2))
664 t2 = (build_pointer_type
665 (cp_build_qualified_type (class1, cp_type_quals (class2))));
666 else if (DERIVED_FROM_P (class2, class1))
667 t1 = (build_pointer_type
668 (cp_build_qualified_type (class2, cp_type_quals (class1))));
669 else
671 if (complain & tf_error)
672 composite_pointer_error (DK_ERROR, t1, t2, operation);
673 return error_mark_node;
676 /* [expr.eq] permits the application of a pointer-to-member
677 conversion to change the class type of one of the types. */
678 else if (TYPE_PTRMEM_P (t1)
679 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
680 TYPE_PTRMEM_CLASS_TYPE (t2)))
682 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
683 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
685 if (DERIVED_FROM_P (class1, class2))
686 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
687 else if (DERIVED_FROM_P (class2, class1))
688 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
689 else
691 if (complain & tf_error)
692 switch (operation)
694 case CPO_COMPARISON:
695 error ("comparison between distinct "
696 "pointer-to-member types %qT and %qT lacks a cast",
697 t1, t2);
698 break;
699 case CPO_CONVERSION:
700 error ("conversion between distinct "
701 "pointer-to-member types %qT and %qT lacks a cast",
702 t1, t2);
703 break;
704 case CPO_CONDITIONAL_EXPR:
705 error ("conditional expression between distinct "
706 "pointer-to-member types %qT and %qT lacks a cast",
707 t1, t2);
708 break;
709 default:
710 gcc_unreachable ();
712 return error_mark_node;
716 return composite_pointer_type_r (t1, t2, operation, complain);
719 /* Return the merged type of two types.
720 We assume that comptypes has already been done and returned 1;
721 if that isn't so, this may crash.
723 This just combines attributes and default arguments; any other
724 differences would cause the two types to compare unalike. */
726 tree
727 merge_types (tree t1, tree t2)
729 enum tree_code code1;
730 enum tree_code code2;
731 tree attributes;
733 /* Save time if the two types are the same. */
734 if (t1 == t2)
735 return t1;
736 if (original_type (t1) == original_type (t2))
737 return t1;
739 /* If one type is nonsense, use the other. */
740 if (t1 == error_mark_node)
741 return t2;
742 if (t2 == error_mark_node)
743 return t1;
745 /* Handle merging an auto redeclaration with a previous deduced
746 return type. */
747 if (is_auto (t1))
748 return t2;
750 /* Merge the attributes. */
751 attributes = (*targetm.merge_type_attributes) (t1, t2);
753 if (TYPE_PTRMEMFUNC_P (t1))
754 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
755 if (TYPE_PTRMEMFUNC_P (t2))
756 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
758 code1 = TREE_CODE (t1);
759 code2 = TREE_CODE (t2);
760 if (code1 != code2)
762 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
763 if (code1 == TYPENAME_TYPE)
765 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
766 code1 = TREE_CODE (t1);
768 else
770 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
771 code2 = TREE_CODE (t2);
775 switch (code1)
777 case POINTER_TYPE:
778 case REFERENCE_TYPE:
779 /* For two pointers, do this recursively on the target type. */
781 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
782 int quals = cp_type_quals (t1);
784 if (code1 == POINTER_TYPE)
786 t1 = build_pointer_type (target);
787 if (TREE_CODE (target) == METHOD_TYPE)
788 t1 = build_ptrmemfunc_type (t1);
790 else
791 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
792 t1 = build_type_attribute_variant (t1, attributes);
793 t1 = cp_build_qualified_type (t1, quals);
795 return t1;
798 case OFFSET_TYPE:
800 int quals;
801 tree pointee;
802 quals = cp_type_quals (t1);
803 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
804 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
805 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
806 pointee);
807 t1 = cp_build_qualified_type (t1, quals);
808 break;
811 case ARRAY_TYPE:
813 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
814 /* Save space: see if the result is identical to one of the args. */
815 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
816 return build_type_attribute_variant (t1, attributes);
817 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
818 return build_type_attribute_variant (t2, attributes);
819 /* Merge the element types, and have a size if either arg has one. */
820 t1 = build_cplus_array_type
821 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
822 break;
825 case FUNCTION_TYPE:
826 /* Function types: prefer the one that specified arg types.
827 If both do, merge the arg types. Also merge the return types. */
829 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
830 tree p1 = TYPE_ARG_TYPES (t1);
831 tree p2 = TYPE_ARG_TYPES (t2);
832 tree parms;
833 tree rval, raises;
834 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
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 rval = build_function_type (valtype, parms);
851 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
852 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
853 rval = apply_memfn_quals (rval,
854 type_memfn_quals (t1),
855 type_memfn_rqual (t1));
856 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
857 TYPE_RAISES_EXCEPTIONS (t2));
858 t1 = build_exception_variant (rval, raises);
859 if (late_return_type_p)
860 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
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);
874 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
876 /* If this was a member function type, get back to the
877 original type of type member function (i.e., without
878 the class instance variable up front. */
879 t1 = build_function_type (TREE_TYPE (t1),
880 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
881 t2 = build_function_type (TREE_TYPE (t2),
882 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
883 t3 = merge_types (t1, t2);
884 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
885 TYPE_ARG_TYPES (t3));
886 t1 = build_exception_variant (t3, raises);
887 t1 = build_ref_qualified_type (t1, rqual);
888 if (late_return_type_1_p)
889 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
890 if (late_return_type_2_p)
891 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
892 break;
895 case TYPENAME_TYPE:
896 /* There is no need to merge attributes into a TYPENAME_TYPE.
897 When the type is instantiated it will have whatever
898 attributes result from the instantiation. */
899 return t1;
901 default:;
904 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
905 return t1;
906 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
907 return t2;
908 else
909 return cp_build_type_attribute_variant (t1, attributes);
912 /* Return the ARRAY_TYPE type without its domain. */
914 tree
915 strip_array_domain (tree type)
917 tree t2;
918 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
919 if (TYPE_DOMAIN (type) == NULL_TREE)
920 return type;
921 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
922 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
925 /* Wrapper around cp_common_type that is used by c-common.c and other
926 front end optimizations that remove promotions.
928 Return the common type for two arithmetic types T1 and T2 under the
929 usual arithmetic conversions. The default conversions have already
930 been applied, and enumerated types converted to their compatible
931 integer types. */
933 tree
934 common_type (tree t1, tree t2)
936 /* If one type is nonsense, use the other */
937 if (t1 == error_mark_node)
938 return t2;
939 if (t2 == error_mark_node)
940 return t1;
942 return cp_common_type (t1, t2);
945 /* Return the common type of two pointer types T1 and T2. This is the
946 type for the result of most arithmetic operations if the operands
947 have the given two types.
949 We assume that comp_target_types has already been done and returned
950 nonzero; if that isn't so, this may crash. */
952 tree
953 common_pointer_type (tree t1, tree t2)
955 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
956 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
957 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
959 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
960 CPO_CONVERSION, tf_warning_or_error);
963 /* Compare two exception specifier types for exactness or subsetness, if
964 allowed. Returns false for mismatch, true for match (same, or
965 derived and !exact).
967 [except.spec] "If a class X ... objects of class X or any class publicly
968 and unambiguously derived from X. Similarly, if a pointer type Y * ...
969 exceptions of type Y * or that are pointers to any type publicly and
970 unambiguously derived from Y. Otherwise a function only allows exceptions
971 that have the same type ..."
972 This does not mention cv qualifiers and is different to what throw
973 [except.throw] and catch [except.catch] will do. They will ignore the
974 top level cv qualifiers, and allow qualifiers in the pointer to class
975 example.
977 We implement the letter of the standard. */
979 static bool
980 comp_except_types (tree a, tree b, bool exact)
982 if (same_type_p (a, b))
983 return true;
984 else if (!exact)
986 if (cp_type_quals (a) || cp_type_quals (b))
987 return false;
989 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
991 a = TREE_TYPE (a);
992 b = TREE_TYPE (b);
993 if (cp_type_quals (a) || cp_type_quals (b))
994 return false;
997 if (TREE_CODE (a) != RECORD_TYPE
998 || TREE_CODE (b) != RECORD_TYPE)
999 return false;
1001 if (publicly_uniquely_derived_p (a, b))
1002 return true;
1004 return false;
1007 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1008 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1009 If EXACT is ce_type, the C++17 type compatibility rules apply.
1010 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1011 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1012 are unordered, but we've already filtered out duplicates. Most lists will
1013 be in order, we should try to make use of that. */
1015 bool
1016 comp_except_specs (const_tree t1, const_tree t2, int exact)
1018 const_tree probe;
1019 const_tree base;
1020 int length = 0;
1022 if (t1 == t2)
1023 return true;
1025 /* First handle noexcept. */
1026 if (exact < ce_exact)
1028 if (exact == ce_type
1029 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1030 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1031 return true;
1033 /* noexcept(false) is compatible with no exception-specification,
1034 and less strict than any spec. */
1035 if (t1 == noexcept_false_spec)
1036 return t2 == NULL_TREE || exact == ce_derived;
1037 /* Even a derived noexcept(false) is compatible with no
1038 exception-specification. */
1039 if (t2 == noexcept_false_spec)
1040 return t1 == NULL_TREE;
1042 /* Otherwise, if we aren't looking for an exact match, noexcept is
1043 equivalent to throw(). */
1044 if (t1 == noexcept_true_spec)
1045 t1 = empty_except_spec;
1046 if (t2 == noexcept_true_spec)
1047 t2 = empty_except_spec;
1050 /* If any noexcept is left, it is only comparable to itself;
1051 either we're looking for an exact match or we're redeclaring a
1052 template with dependent noexcept. */
1053 if ((t1 && TREE_PURPOSE (t1))
1054 || (t2 && TREE_PURPOSE (t2)))
1055 return (t1 && t2
1056 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1058 if (t1 == NULL_TREE) /* T1 is ... */
1059 return t2 == NULL_TREE || exact == ce_derived;
1060 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1061 return t2 != NULL_TREE && !TREE_VALUE (t2);
1062 if (t2 == NULL_TREE) /* T2 is ... */
1063 return false;
1064 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1065 return exact == ce_derived;
1067 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1068 Count how many we find, to determine exactness. For exact matching and
1069 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1070 O(nm). */
1071 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1073 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1075 tree a = TREE_VALUE (probe);
1076 tree b = TREE_VALUE (t2);
1078 if (comp_except_types (a, b, exact))
1080 if (probe == base && exact > ce_derived)
1081 base = TREE_CHAIN (probe);
1082 length++;
1083 break;
1086 if (probe == NULL_TREE)
1087 return false;
1089 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1092 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1093 [] can match [size]. */
1095 static bool
1096 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1098 tree d1;
1099 tree d2;
1100 tree max1, max2;
1102 if (t1 == t2)
1103 return true;
1105 /* The type of the array elements must be the same. */
1106 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1107 return false;
1109 d1 = TYPE_DOMAIN (t1);
1110 d2 = TYPE_DOMAIN (t2);
1112 if (d1 == d2)
1113 return true;
1115 /* If one of the arrays is dimensionless, and the other has a
1116 dimension, they are of different types. However, it is valid to
1117 write:
1119 extern int a[];
1120 int a[3];
1122 by [basic.link]:
1124 declarations for an array object can specify
1125 array types that differ by the presence or absence of a major
1126 array bound (_dcl.array_). */
1127 if (!d1 || !d2)
1128 return allow_redeclaration;
1130 /* Check that the dimensions are the same. */
1132 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1133 return false;
1134 max1 = TYPE_MAX_VALUE (d1);
1135 max2 = TYPE_MAX_VALUE (d2);
1137 if (!cp_tree_equal (max1, max2))
1138 return false;
1140 return true;
1143 /* Compare the relative position of T1 and T2 into their respective
1144 template parameter list.
1145 T1 and T2 must be template parameter types.
1146 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1148 static bool
1149 comp_template_parms_position (tree t1, tree t2)
1151 tree index1, index2;
1152 gcc_assert (t1 && t2
1153 && TREE_CODE (t1) == TREE_CODE (t2)
1154 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1155 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1156 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1158 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1159 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1161 /* Then compare their relative position. */
1162 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1163 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1164 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1165 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1166 return false;
1168 /* In C++14 we can end up comparing 'auto' to a normal template
1169 parameter. Don't confuse them. */
1170 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1171 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1173 return true;
1176 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1178 static bool
1179 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1181 t1 = TYPE_MAIN_VARIANT (t1);
1182 t2 = TYPE_MAIN_VARIANT (t2);
1184 if (TREE_CODE (t1) == POINTER_TYPE
1185 && TREE_CODE (t2) == POINTER_TYPE)
1186 return true;
1188 /* The signedness of the parameter matters only when an integral
1189 type smaller than int is promoted to int, otherwise only the
1190 precision of the parameter matters.
1191 This check should make sure that the callee does not see
1192 undefined values in argument registers. */
1193 if (INTEGRAL_TYPE_P (t1)
1194 && INTEGRAL_TYPE_P (t2)
1195 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1196 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1197 || !targetm.calls.promote_prototypes (NULL_TREE)
1198 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1199 return true;
1201 return same_type_p (t1, t2);
1204 /* Check if a type cast between two function types can be considered safe. */
1206 static bool
1207 cxx_safe_function_type_cast_p (tree t1, tree t2)
1209 if (TREE_TYPE (t1) == void_type_node &&
1210 TYPE_ARG_TYPES (t1) == void_list_node)
1211 return true;
1213 if (TREE_TYPE (t2) == void_type_node &&
1214 TYPE_ARG_TYPES (t2) == void_list_node)
1215 return true;
1217 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1218 return false;
1220 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1221 t1 && t2;
1222 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1223 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1224 return false;
1226 return true;
1229 /* Subroutine in comptypes. */
1231 static bool
1232 structural_comptypes (tree t1, tree t2, int strict)
1234 if (t1 == t2)
1235 return true;
1237 /* Suppress errors caused by previously reported errors. */
1238 if (t1 == error_mark_node || t2 == error_mark_node)
1239 return false;
1241 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1243 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1244 current instantiation. */
1245 if (TREE_CODE (t1) == TYPENAME_TYPE)
1246 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1248 if (TREE_CODE (t2) == TYPENAME_TYPE)
1249 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1251 if (TYPE_PTRMEMFUNC_P (t1))
1252 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1253 if (TYPE_PTRMEMFUNC_P (t2))
1254 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1256 /* Different classes of types can't be compatible. */
1257 if (TREE_CODE (t1) != TREE_CODE (t2))
1258 return false;
1260 /* Qualifiers must match. For array types, we will check when we
1261 recur on the array element types. */
1262 if (TREE_CODE (t1) != ARRAY_TYPE
1263 && cp_type_quals (t1) != cp_type_quals (t2))
1264 return false;
1265 if (TREE_CODE (t1) == FUNCTION_TYPE
1266 && type_memfn_quals (t1) != type_memfn_quals (t2))
1267 return false;
1268 /* Need to check this before TYPE_MAIN_VARIANT.
1269 FIXME function qualifiers should really change the main variant. */
1270 if (TREE_CODE (t1) == FUNCTION_TYPE
1271 || TREE_CODE (t1) == METHOD_TYPE)
1273 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1274 return false;
1275 if (flag_noexcept_type
1276 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1277 TYPE_RAISES_EXCEPTIONS (t2),
1278 ce_type))
1279 return false;
1282 /* Allow for two different type nodes which have essentially the same
1283 definition. Note that we already checked for equality of the type
1284 qualifiers (just above). */
1286 if (TREE_CODE (t1) != ARRAY_TYPE
1287 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1288 return true;
1291 /* Compare the types. Break out if they could be the same. */
1292 switch (TREE_CODE (t1))
1294 case VOID_TYPE:
1295 case BOOLEAN_TYPE:
1296 /* All void and bool types are the same. */
1297 break;
1299 case INTEGER_TYPE:
1300 case FIXED_POINT_TYPE:
1301 case REAL_TYPE:
1302 /* With these nodes, we can't determine type equivalence by
1303 looking at what is stored in the nodes themselves, because
1304 two nodes might have different TYPE_MAIN_VARIANTs but still
1305 represent the same type. For example, wchar_t and int could
1306 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1307 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1308 and are distinct types. On the other hand, int and the
1309 following typedef
1311 typedef int INT __attribute((may_alias));
1313 have identical properties, different TYPE_MAIN_VARIANTs, but
1314 represent the same type. The canonical type system keeps
1315 track of equivalence in this case, so we fall back on it. */
1316 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1318 case TEMPLATE_TEMPLATE_PARM:
1319 case BOUND_TEMPLATE_TEMPLATE_PARM:
1320 if (!comp_template_parms_position (t1, t2))
1321 return false;
1322 if (!comp_template_parms
1323 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1324 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1325 return false;
1326 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1327 break;
1328 /* Don't check inheritance. */
1329 strict = COMPARE_STRICT;
1330 /* Fall through. */
1332 case RECORD_TYPE:
1333 case UNION_TYPE:
1334 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1335 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1336 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1337 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1338 break;
1340 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1341 break;
1342 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1343 break;
1345 return false;
1347 case OFFSET_TYPE:
1348 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1349 strict & ~COMPARE_REDECLARATION))
1350 return false;
1351 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1352 return false;
1353 break;
1355 case REFERENCE_TYPE:
1356 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1357 return false;
1358 /* fall through to checks for pointer types */
1359 gcc_fallthrough ();
1361 case POINTER_TYPE:
1362 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1363 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1364 return false;
1365 break;
1367 case METHOD_TYPE:
1368 case FUNCTION_TYPE:
1369 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1370 return false;
1371 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1372 return false;
1373 break;
1375 case ARRAY_TYPE:
1376 /* Target types must match incl. qualifiers. */
1377 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1378 return false;
1379 break;
1381 case TEMPLATE_TYPE_PARM:
1382 /* If T1 and T2 don't have the same relative position in their
1383 template parameters set, they can't be equal. */
1384 if (!comp_template_parms_position (t1, t2))
1385 return false;
1386 /* Constrained 'auto's are distinct from parms that don't have the same
1387 constraints. */
1388 if (!equivalent_placeholder_constraints (t1, t2))
1389 return false;
1390 break;
1392 case TYPENAME_TYPE:
1393 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1394 TYPENAME_TYPE_FULLNAME (t2)))
1395 return false;
1396 /* Qualifiers don't matter on scopes. */
1397 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1398 TYPE_CONTEXT (t2)))
1399 return false;
1400 break;
1402 case UNBOUND_CLASS_TEMPLATE:
1403 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1404 return false;
1405 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1406 return false;
1407 break;
1409 case COMPLEX_TYPE:
1410 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1411 return false;
1412 break;
1414 case VECTOR_TYPE:
1415 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1416 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1417 return false;
1418 break;
1420 case TYPE_PACK_EXPANSION:
1421 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1422 PACK_EXPANSION_PATTERN (t2))
1423 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1424 PACK_EXPANSION_EXTRA_ARGS (t2)));
1426 case DECLTYPE_TYPE:
1427 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1428 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1429 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1430 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1431 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1432 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1433 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1434 DECLTYPE_TYPE_EXPR (t2)))
1435 return false;
1436 break;
1438 case UNDERLYING_TYPE:
1439 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1440 UNDERLYING_TYPE_TYPE (t2));
1442 default:
1443 return false;
1446 /* If we get here, we know that from a target independent POV the
1447 types are the same. Make sure the target attributes are also
1448 the same. */
1449 return comp_type_attributes (t1, t2);
1452 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1453 is a bitwise-or of the COMPARE_* flags. */
1455 bool
1456 comptypes (tree t1, tree t2, int strict)
1458 if (strict == COMPARE_STRICT)
1460 if (t1 == t2)
1461 return true;
1463 if (t1 == error_mark_node || t2 == error_mark_node)
1464 return false;
1466 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1467 /* At least one of the types requires structural equality, so
1468 perform a deep check. */
1469 return structural_comptypes (t1, t2, strict);
1471 if (flag_checking && USE_CANONICAL_TYPES)
1473 bool result = structural_comptypes (t1, t2, strict);
1475 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1476 /* The two types are structurally equivalent, but their
1477 canonical types were different. This is a failure of the
1478 canonical type propagation code.*/
1479 internal_error
1480 ("canonical types differ for identical types %qT and %qT",
1481 t1, t2);
1482 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1483 /* Two types are structurally different, but the canonical
1484 types are the same. This means we were over-eager in
1485 assigning canonical types. */
1486 internal_error
1487 ("same canonical type node for different types %qT and %qT",
1488 t1, t2);
1490 return result;
1492 if (!flag_checking && USE_CANONICAL_TYPES)
1493 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1494 else
1495 return structural_comptypes (t1, t2, strict);
1497 else if (strict == COMPARE_STRUCTURAL)
1498 return structural_comptypes (t1, t2, COMPARE_STRICT);
1499 else
1500 return structural_comptypes (t1, t2, strict);
1503 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1504 top-level qualifiers. */
1506 bool
1507 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1509 if (type1 == error_mark_node || type2 == error_mark_node)
1510 return false;
1512 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1513 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1514 return same_type_p (type1, type2);
1517 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1519 bool
1520 at_least_as_qualified_p (const_tree type1, const_tree type2)
1522 int q1 = cp_type_quals (type1);
1523 int q2 = cp_type_quals (type2);
1525 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1526 return (q1 & q2) == q2;
1529 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1530 more cv-qualified that TYPE1, and 0 otherwise. */
1533 comp_cv_qualification (int q1, int q2)
1535 if (q1 == q2)
1536 return 0;
1538 if ((q1 & q2) == q2)
1539 return 1;
1540 else if ((q1 & q2) == q1)
1541 return -1;
1543 return 0;
1547 comp_cv_qualification (const_tree type1, const_tree type2)
1549 int q1 = cp_type_quals (type1);
1550 int q2 = cp_type_quals (type2);
1551 return comp_cv_qualification (q1, q2);
1554 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1555 subset of the cv-qualification signature of TYPE2, and the types
1556 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1559 comp_cv_qual_signature (tree type1, tree type2)
1561 if (comp_ptr_ttypes_real (type2, type1, -1))
1562 return 1;
1563 else if (comp_ptr_ttypes_real (type1, type2, -1))
1564 return -1;
1565 else
1566 return 0;
1569 /* Subroutines of `comptypes'. */
1571 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1572 equivalent in the sense that functions with those parameter types
1573 can have equivalent types. The two lists must be equivalent,
1574 element by element. */
1576 bool
1577 compparms (const_tree parms1, const_tree parms2)
1579 const_tree t1, t2;
1581 /* An unspecified parmlist matches any specified parmlist
1582 whose argument types don't need default promotions. */
1584 for (t1 = parms1, t2 = parms2;
1585 t1 || t2;
1586 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1588 /* If one parmlist is shorter than the other,
1589 they fail to match. */
1590 if (!t1 || !t2)
1591 return false;
1592 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1593 return false;
1595 return true;
1599 /* Process a sizeof or alignof expression where the operand is a
1600 type. */
1602 tree
1603 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1605 tree value;
1606 bool dependent_p;
1608 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1609 if (type == error_mark_node)
1610 return error_mark_node;
1612 type = non_reference (type);
1613 if (TREE_CODE (type) == METHOD_TYPE)
1615 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 else
1620 return error_mark_node;
1621 value = size_one_node;
1624 dependent_p = dependent_type_p (type);
1625 if (!dependent_p)
1626 complete_type (type);
1627 if (dependent_p
1628 /* VLA types will have a non-constant size. In the body of an
1629 uninstantiated template, we don't need to try to compute the
1630 value, because the sizeof expression is not an integral
1631 constant expression in that case. And, if we do try to
1632 compute the value, we'll likely end up with SAVE_EXPRs, which
1633 the template substitution machinery does not expect to see. */
1634 || (processing_template_decl
1635 && COMPLETE_TYPE_P (type)
1636 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1638 value = build_min (op, size_type_node, type);
1639 TREE_READONLY (value) = 1;
1640 return value;
1643 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1644 op == SIZEOF_EXPR, false,
1645 complain);
1648 /* Return the size of the type, without producing any warnings for
1649 types whose size cannot be taken. This routine should be used only
1650 in some other routine that has already produced a diagnostic about
1651 using the size of such a type. */
1652 tree
1653 cxx_sizeof_nowarn (tree type)
1655 if (TREE_CODE (type) == FUNCTION_TYPE
1656 || VOID_TYPE_P (type)
1657 || TREE_CODE (type) == ERROR_MARK)
1658 return size_one_node;
1659 else if (!COMPLETE_TYPE_P (type))
1660 return size_zero_node;
1661 else
1662 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1665 /* Process a sizeof expression where the operand is an expression. */
1667 static tree
1668 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1670 if (e == error_mark_node)
1671 return error_mark_node;
1673 if (processing_template_decl)
1675 e = build_min (SIZEOF_EXPR, size_type_node, e);
1676 TREE_SIDE_EFFECTS (e) = 0;
1677 TREE_READONLY (e) = 1;
1679 return e;
1682 /* To get the size of a static data member declared as an array of
1683 unknown bound, we need to instantiate it. */
1684 if (VAR_P (e)
1685 && VAR_HAD_UNKNOWN_BOUND (e)
1686 && DECL_TEMPLATE_INSTANTIATION (e))
1687 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1689 if (TREE_CODE (e) == PARM_DECL
1690 && DECL_ARRAY_PARAMETER_P (e)
1691 && (complain & tf_warning))
1693 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1694 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1695 inform (DECL_SOURCE_LOCATION (e), "declared here");
1698 e = mark_type_use (e);
1700 if (bitfield_p (e))
1702 if (complain & tf_error)
1703 error ("invalid application of %<sizeof%> to a bit-field");
1704 else
1705 return error_mark_node;
1706 e = char_type_node;
1708 else if (is_overloaded_fn (e))
1710 if (complain & tf_error)
1711 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1712 "function type");
1713 else
1714 return error_mark_node;
1715 e = char_type_node;
1717 else if (type_unknown_p (e))
1719 if (complain & tf_error)
1720 cxx_incomplete_type_error (e, TREE_TYPE (e));
1721 else
1722 return error_mark_node;
1723 e = char_type_node;
1725 else
1726 e = TREE_TYPE (e);
1728 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1731 /* Implement the __alignof keyword: Return the minimum required
1732 alignment of E, measured in bytes. For VAR_DECL's and
1733 FIELD_DECL's return DECL_ALIGN (which can be set from an
1734 "aligned" __attribute__ specification). */
1736 static tree
1737 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1739 tree t;
1741 if (e == error_mark_node)
1742 return error_mark_node;
1744 if (processing_template_decl)
1746 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1747 TREE_SIDE_EFFECTS (e) = 0;
1748 TREE_READONLY (e) = 1;
1750 return e;
1753 e = mark_type_use (e);
1755 if (VAR_P (e))
1756 t = size_int (DECL_ALIGN_UNIT (e));
1757 else if (bitfield_p (e))
1759 if (complain & tf_error)
1760 error ("invalid application of %<__alignof%> to a bit-field");
1761 else
1762 return error_mark_node;
1763 t = size_one_node;
1765 else if (TREE_CODE (e) == COMPONENT_REF
1766 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1767 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1768 else if (is_overloaded_fn (e))
1770 if (complain & tf_error)
1771 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1772 "function type");
1773 else
1774 return error_mark_node;
1775 if (TREE_CODE (e) == FUNCTION_DECL)
1776 t = size_int (DECL_ALIGN_UNIT (e));
1777 else
1778 t = size_one_node;
1780 else if (type_unknown_p (e))
1782 if (complain & tf_error)
1783 cxx_incomplete_type_error (e, TREE_TYPE (e));
1784 else
1785 return error_mark_node;
1786 t = size_one_node;
1788 else
1789 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1790 complain & tf_error);
1792 return fold_convert (size_type_node, t);
1795 /* Process a sizeof or alignof expression E with code OP where the operand
1796 is an expression. */
1798 tree
1799 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1801 if (op == SIZEOF_EXPR)
1802 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1803 else
1804 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1807 /* Build a representation of an expression 'alignas(E).' Return the
1808 folded integer value of E if it is an integral constant expression
1809 that resolves to a valid alignment. If E depends on a template
1810 parameter, return a syntactic representation tree of kind
1811 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1812 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1814 tree
1815 cxx_alignas_expr (tree e)
1817 if (e == NULL_TREE || e == error_mark_node
1818 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1819 return e;
1821 if (TYPE_P (e))
1822 /* [dcl.align]/3:
1824 When the alignment-specifier is of the form
1825 alignas(type-id ), it shall have the same effect as
1826 alignas(alignof(type-id )). */
1828 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1830 /* If we reach this point, it means the alignas expression if of
1831 the form "alignas(assignment-expression)", so we should follow
1832 what is stated by [dcl.align]/2. */
1834 if (value_dependent_expression_p (e))
1835 /* Leave value-dependent expression alone for now. */
1836 return e;
1838 e = instantiate_non_dependent_expr (e);
1839 e = mark_rvalue_use (e);
1841 /* [dcl.align]/2 says:
1843 the assignment-expression shall be an integral constant
1844 expression. */
1846 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1848 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1849 return error_mark_node;
1852 return cxx_constant_value (e);
1856 /* EXPR is being used in a context that is not a function call.
1857 Enforce:
1859 [expr.ref]
1861 The expression can be used only as the left-hand operand of a
1862 member function call.
1864 [expr.mptr.operator]
1866 If the result of .* or ->* is a function, then that result can be
1867 used only as the operand for the function call operator ().
1869 by issuing an error message if appropriate. Returns true iff EXPR
1870 violates these rules. */
1872 bool
1873 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1875 if (expr == NULL_TREE)
1876 return false;
1877 /* Don't enforce this in MS mode. */
1878 if (flag_ms_extensions)
1879 return false;
1880 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1881 expr = get_first_fn (expr);
1882 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1884 if (complain & tf_error)
1886 if (DECL_P (expr))
1888 error_at (loc, "invalid use of non-static member function %qD",
1889 expr);
1890 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1892 else
1893 error_at (loc, "invalid use of non-static member function of "
1894 "type %qT", TREE_TYPE (expr));
1896 return true;
1898 return false;
1901 /* If EXP is a reference to a bitfield, and the type of EXP does not
1902 match the declared type of the bitfield, return the declared type
1903 of the bitfield. Otherwise, return NULL_TREE. */
1905 tree
1906 is_bitfield_expr_with_lowered_type (const_tree exp)
1908 switch (TREE_CODE (exp))
1910 case COND_EXPR:
1911 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1912 ? TREE_OPERAND (exp, 1)
1913 : TREE_OPERAND (exp, 0)))
1914 return NULL_TREE;
1915 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1917 case COMPOUND_EXPR:
1918 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1920 case MODIFY_EXPR:
1921 case SAVE_EXPR:
1922 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1924 case COMPONENT_REF:
1926 tree field;
1928 field = TREE_OPERAND (exp, 1);
1929 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1930 return NULL_TREE;
1931 if (same_type_ignoring_top_level_qualifiers_p
1932 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1933 return NULL_TREE;
1934 return DECL_BIT_FIELD_TYPE (field);
1937 case VAR_DECL:
1938 if (DECL_HAS_VALUE_EXPR_P (exp))
1939 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1940 (CONST_CAST_TREE (exp)));
1941 return NULL_TREE;
1943 CASE_CONVERT:
1944 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1945 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1946 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1947 /* Fallthrough. */
1949 default:
1950 return NULL_TREE;
1954 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1955 bitfield with a lowered type, the type of EXP is returned, rather
1956 than NULL_TREE. */
1958 tree
1959 unlowered_expr_type (const_tree exp)
1961 tree type;
1962 tree etype = TREE_TYPE (exp);
1964 type = is_bitfield_expr_with_lowered_type (exp);
1965 if (type)
1966 type = cp_build_qualified_type (type, cp_type_quals (etype));
1967 else
1968 type = etype;
1970 return type;
1973 /* Perform the conversions in [expr] that apply when an lvalue appears
1974 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1975 function-to-pointer conversions. In addition, bitfield references are
1976 converted to their declared types. Note that this function does not perform
1977 the lvalue-to-rvalue conversion for class types. If you need that conversion
1978 for class types, then you probably need to use force_rvalue.
1980 Although the returned value is being used as an rvalue, this
1981 function does not wrap the returned expression in a
1982 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1983 that the return value is no longer an lvalue. */
1985 tree
1986 decay_conversion (tree exp,
1987 tsubst_flags_t complain,
1988 bool reject_builtin /* = true */)
1990 tree type;
1991 enum tree_code code;
1992 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1994 type = TREE_TYPE (exp);
1995 if (type == error_mark_node)
1996 return error_mark_node;
1998 exp = resolve_nondeduced_context (exp, complain);
1999 if (type_unknown_p (exp))
2001 if (complain & tf_error)
2002 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
2003 return error_mark_node;
2006 code = TREE_CODE (type);
2008 if (error_operand_p (exp))
2009 return error_mark_node;
2011 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2012 return nullptr_node;
2014 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2015 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2016 if (code == VOID_TYPE)
2018 if (complain & tf_error)
2019 error_at (loc, "void value not ignored as it ought to be");
2020 return error_mark_node;
2022 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2023 return error_mark_node;
2024 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2026 exp = mark_lvalue_use (exp);
2027 if (reject_builtin && reject_gcc_builtin (exp, loc))
2028 return error_mark_node;
2029 return cp_build_addr_expr (exp, complain);
2031 if (code == ARRAY_TYPE)
2033 tree adr;
2034 tree ptrtype;
2036 exp = mark_lvalue_use (exp);
2038 if (INDIRECT_REF_P (exp))
2039 return build_nop (build_pointer_type (TREE_TYPE (type)),
2040 TREE_OPERAND (exp, 0));
2042 if (TREE_CODE (exp) == COMPOUND_EXPR)
2044 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2045 if (op1 == error_mark_node)
2046 return error_mark_node;
2047 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2048 TREE_OPERAND (exp, 0), op1);
2051 if (!obvalue_p (exp)
2052 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2054 if (complain & tf_error)
2055 error_at (loc, "invalid use of non-lvalue array");
2056 return error_mark_node;
2059 /* Don't let an array compound literal decay to a pointer. It can
2060 still be used to initialize an array or bind to a reference. */
2061 if (TREE_CODE (exp) == TARGET_EXPR)
2063 if (complain & tf_error)
2064 error_at (loc, "taking address of temporary array");
2065 return error_mark_node;
2068 ptrtype = build_pointer_type (TREE_TYPE (type));
2070 if (VAR_P (exp))
2072 if (!cxx_mark_addressable (exp))
2073 return error_mark_node;
2074 adr = build_nop (ptrtype, build_address (exp));
2075 return adr;
2077 /* This way is better for a COMPONENT_REF since it can
2078 simplify the offset for a component. */
2079 adr = cp_build_addr_expr (exp, complain);
2080 return cp_convert (ptrtype, adr, complain);
2083 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2084 exp = mark_rvalue_use (exp, loc, reject_builtin);
2086 /* If a bitfield is used in a context where integral promotion
2087 applies, then the caller is expected to have used
2088 default_conversion. That function promotes bitfields correctly
2089 before calling this function. At this point, if we have a
2090 bitfield referenced, we may assume that is not subject to
2091 promotion, and that, therefore, the type of the resulting rvalue
2092 is the declared type of the bitfield. */
2093 exp = convert_bitfield_to_declared_type (exp);
2095 /* We do not call rvalue() here because we do not want to wrap EXP
2096 in a NON_LVALUE_EXPR. */
2098 /* [basic.lval]
2100 Non-class rvalues always have cv-unqualified types. */
2101 type = TREE_TYPE (exp);
2102 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2103 exp = build_nop (cv_unqualified (type), exp);
2105 if (!complete_type_or_maybe_complain (type, exp, complain))
2106 return error_mark_node;
2108 return exp;
2111 /* Perform preparatory conversions, as part of the "usual arithmetic
2112 conversions". In particular, as per [expr]:
2114 Whenever an lvalue expression appears as an operand of an
2115 operator that expects the rvalue for that operand, the
2116 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2117 standard conversions are applied to convert the expression to an
2118 rvalue.
2120 In addition, we perform integral promotions here, as those are
2121 applied to both operands to a binary operator before determining
2122 what additional conversions should apply. */
2124 static tree
2125 cp_default_conversion (tree exp, tsubst_flags_t complain)
2127 /* Check for target-specific promotions. */
2128 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2129 if (promoted_type)
2130 exp = cp_convert (promoted_type, exp, complain);
2131 /* Perform the integral promotions first so that bitfield
2132 expressions (which may promote to "int", even if the bitfield is
2133 declared "unsigned") are promoted correctly. */
2134 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2135 exp = cp_perform_integral_promotions (exp, complain);
2136 /* Perform the other conversions. */
2137 exp = decay_conversion (exp, complain);
2139 return exp;
2142 /* C version. */
2144 tree
2145 default_conversion (tree exp)
2147 return cp_default_conversion (exp, tf_warning_or_error);
2150 /* EXPR is an expression with an integral or enumeration type.
2151 Perform the integral promotions in [conv.prom], and return the
2152 converted value. */
2154 tree
2155 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2157 tree type;
2158 tree promoted_type;
2160 expr = mark_rvalue_use (expr);
2162 /* [conv.prom]
2164 If the bitfield has an enumerated type, it is treated as any
2165 other value of that type for promotion purposes. */
2166 type = is_bitfield_expr_with_lowered_type (expr);
2167 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2168 type = TREE_TYPE (expr);
2169 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2170 /* Scoped enums don't promote. */
2171 if (SCOPED_ENUM_P (type))
2172 return expr;
2173 promoted_type = type_promotes_to (type);
2174 if (type != promoted_type)
2175 expr = cp_convert (promoted_type, expr, complain);
2176 return expr;
2179 /* C version. */
2181 tree
2182 perform_integral_promotions (tree expr)
2184 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2187 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2188 decay_conversion to one. */
2191 string_conv_p (const_tree totype, const_tree exp, int warn)
2193 tree t;
2195 if (!TYPE_PTR_P (totype))
2196 return 0;
2198 t = TREE_TYPE (totype);
2199 if (!same_type_p (t, char_type_node)
2200 && !same_type_p (t, char16_type_node)
2201 && !same_type_p (t, char32_type_node)
2202 && !same_type_p (t, wchar_type_node))
2203 return 0;
2205 if (TREE_CODE (exp) == STRING_CST)
2207 /* Make sure that we don't try to convert between char and wide chars. */
2208 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2209 return 0;
2211 else
2213 /* Is this a string constant which has decayed to 'const char *'? */
2214 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2215 if (!same_type_p (TREE_TYPE (exp), t))
2216 return 0;
2217 STRIP_NOPS (exp);
2218 if (TREE_CODE (exp) != ADDR_EXPR
2219 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2220 return 0;
2222 if (warn)
2224 if (cxx_dialect >= cxx11)
2225 pedwarn (input_location, OPT_Wwrite_strings,
2226 "ISO C++ forbids converting a string constant to %qT",
2227 totype);
2228 else
2229 warning (OPT_Wwrite_strings,
2230 "deprecated conversion from string constant to %qT",
2231 totype);
2234 return 1;
2237 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2238 can, for example, use as an lvalue. This code used to be in
2239 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2240 expressions, where we're dealing with aggregates. But now it's again only
2241 called from unary_complex_lvalue. The case (in particular) that led to
2242 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2243 get it there. */
2245 static tree
2246 rationalize_conditional_expr (enum tree_code code, tree t,
2247 tsubst_flags_t complain)
2249 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2251 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2252 the first operand is always the one to be used if both operands
2253 are equal, so we know what conditional expression this used to be. */
2254 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2256 tree op0 = TREE_OPERAND (t, 0);
2257 tree op1 = TREE_OPERAND (t, 1);
2259 /* The following code is incorrect if either operand side-effects. */
2260 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2261 && !TREE_SIDE_EFFECTS (op1));
2262 return
2263 build_conditional_expr (loc,
2264 build_x_binary_op (loc,
2265 (TREE_CODE (t) == MIN_EXPR
2266 ? LE_EXPR : GE_EXPR),
2267 op0, TREE_CODE (op0),
2268 op1, TREE_CODE (op1),
2269 /*overload=*/NULL,
2270 complain),
2271 cp_build_unary_op (code, op0, false, complain),
2272 cp_build_unary_op (code, op1, false, complain),
2273 complain);
2276 return
2277 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2278 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2279 complain),
2280 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2281 complain),
2282 complain);
2285 /* Given the TYPE of an anonymous union field inside T, return the
2286 FIELD_DECL for the field. If not found return NULL_TREE. Because
2287 anonymous unions can nest, we must also search all anonymous unions
2288 that are directly reachable. */
2290 tree
2291 lookup_anon_field (tree t, tree type)
2293 tree field;
2295 t = TYPE_MAIN_VARIANT (t);
2297 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2299 if (TREE_STATIC (field))
2300 continue;
2301 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2302 continue;
2304 /* If we find it directly, return the field. */
2305 if (DECL_NAME (field) == NULL_TREE
2306 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2308 return field;
2311 /* Otherwise, it could be nested, search harder. */
2312 if (DECL_NAME (field) == NULL_TREE
2313 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2315 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2316 if (subfield)
2317 return subfield;
2320 return NULL_TREE;
2323 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2324 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2325 non-NULL, it indicates the path to the base used to name MEMBER.
2326 If PRESERVE_REFERENCE is true, the expression returned will have
2327 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2328 returned will have the type referred to by the reference.
2330 This function does not perform access control; that is either done
2331 earlier by the parser when the name of MEMBER is resolved to MEMBER
2332 itself, or later when overload resolution selects one of the
2333 functions indicated by MEMBER. */
2335 tree
2336 build_class_member_access_expr (cp_expr object, tree member,
2337 tree access_path, bool preserve_reference,
2338 tsubst_flags_t complain)
2340 tree object_type;
2341 tree member_scope;
2342 tree result = NULL_TREE;
2343 tree using_decl = NULL_TREE;
2345 if (error_operand_p (object) || error_operand_p (member))
2346 return error_mark_node;
2348 gcc_assert (DECL_P (member) || BASELINK_P (member));
2350 /* [expr.ref]
2352 The type of the first expression shall be "class object" (of a
2353 complete type). */
2354 object_type = TREE_TYPE (object);
2355 if (!currently_open_class (object_type)
2356 && !complete_type_or_maybe_complain (object_type, object, complain))
2357 return error_mark_node;
2358 if (!CLASS_TYPE_P (object_type))
2360 if (complain & tf_error)
2362 if (POINTER_TYPE_P (object_type)
2363 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2364 error ("request for member %qD in %qE, which is of pointer "
2365 "type %qT (maybe you meant to use %<->%> ?)",
2366 member, object.get_value (), object_type);
2367 else
2368 error ("request for member %qD in %qE, which is of non-class "
2369 "type %qT", member, object.get_value (), object_type);
2371 return error_mark_node;
2374 /* The standard does not seem to actually say that MEMBER must be a
2375 member of OBJECT_TYPE. However, that is clearly what is
2376 intended. */
2377 if (DECL_P (member))
2379 member_scope = DECL_CLASS_CONTEXT (member);
2380 if (!mark_used (member, complain) && !(complain & tf_error))
2381 return error_mark_node;
2382 if (TREE_DEPRECATED (member))
2383 warn_deprecated_use (member, NULL_TREE);
2385 else
2386 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2387 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2388 presently be the anonymous union. Go outwards until we find a
2389 type related to OBJECT_TYPE. */
2390 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2391 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2392 object_type))
2393 member_scope = TYPE_CONTEXT (member_scope);
2394 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2396 if (complain & tf_error)
2398 if (TREE_CODE (member) == FIELD_DECL)
2399 error ("invalid use of nonstatic data member %qE", member);
2400 else
2401 error ("%qD is not a member of %qT", member, object_type);
2403 return error_mark_node;
2406 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2407 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2408 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2410 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2411 if (temp)
2412 object = cp_build_fold_indirect_ref (temp);
2415 /* In [expr.ref], there is an explicit list of the valid choices for
2416 MEMBER. We check for each of those cases here. */
2417 if (VAR_P (member))
2419 /* A static data member. */
2420 result = member;
2421 mark_exp_read (object);
2422 /* If OBJECT has side-effects, they are supposed to occur. */
2423 if (TREE_SIDE_EFFECTS (object))
2424 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2426 else if (TREE_CODE (member) == FIELD_DECL)
2428 /* A non-static data member. */
2429 bool null_object_p;
2430 int type_quals;
2431 tree member_type;
2433 if (INDIRECT_REF_P (object))
2434 null_object_p =
2435 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2436 else
2437 null_object_p = false;
2439 /* Convert OBJECT to the type of MEMBER. */
2440 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2441 TYPE_MAIN_VARIANT (member_scope)))
2443 tree binfo;
2444 base_kind kind;
2446 binfo = lookup_base (access_path ? access_path : object_type,
2447 member_scope, ba_unique, &kind, complain);
2448 if (binfo == error_mark_node)
2449 return error_mark_node;
2451 /* It is invalid to try to get to a virtual base of a
2452 NULL object. The most common cause is invalid use of
2453 offsetof macro. */
2454 if (null_object_p && kind == bk_via_virtual)
2456 if (complain & tf_error)
2458 error ("invalid access to non-static data member %qD in "
2459 "virtual base of NULL object", member);
2461 return error_mark_node;
2464 /* Convert to the base. */
2465 object = build_base_path (PLUS_EXPR, object, binfo,
2466 /*nonnull=*/1, complain);
2467 /* If we found the base successfully then we should be able
2468 to convert to it successfully. */
2469 gcc_assert (object != error_mark_node);
2472 /* If MEMBER is from an anonymous aggregate, we have converted
2473 OBJECT so that it refers to the class containing the
2474 anonymous union. Generate a reference to the anonymous union
2475 itself, and recur to find MEMBER. */
2476 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2477 /* When this code is called from build_field_call, the
2478 object already has the type of the anonymous union.
2479 That is because the COMPONENT_REF was already
2480 constructed, and was then disassembled before calling
2481 build_field_call. After the function-call code is
2482 cleaned up, this waste can be eliminated. */
2483 && (!same_type_ignoring_top_level_qualifiers_p
2484 (TREE_TYPE (object), DECL_CONTEXT (member))))
2486 tree anonymous_union;
2488 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2489 DECL_CONTEXT (member));
2490 object = build_class_member_access_expr (object,
2491 anonymous_union,
2492 /*access_path=*/NULL_TREE,
2493 preserve_reference,
2494 complain);
2497 /* Compute the type of the field, as described in [expr.ref]. */
2498 type_quals = TYPE_UNQUALIFIED;
2499 member_type = TREE_TYPE (member);
2500 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2502 type_quals = (cp_type_quals (member_type)
2503 | cp_type_quals (object_type));
2505 /* A field is const (volatile) if the enclosing object, or the
2506 field itself, is const (volatile). But, a mutable field is
2507 not const, even within a const object. */
2508 if (DECL_MUTABLE_P (member))
2509 type_quals &= ~TYPE_QUAL_CONST;
2510 member_type = cp_build_qualified_type (member_type, type_quals);
2513 result = build3_loc (input_location, COMPONENT_REF, member_type,
2514 object, member, NULL_TREE);
2516 /* Mark the expression const or volatile, as appropriate. Even
2517 though we've dealt with the type above, we still have to mark the
2518 expression itself. */
2519 if (type_quals & TYPE_QUAL_CONST)
2520 TREE_READONLY (result) = 1;
2521 if (type_quals & TYPE_QUAL_VOLATILE)
2522 TREE_THIS_VOLATILE (result) = 1;
2524 else if (BASELINK_P (member))
2526 /* The member is a (possibly overloaded) member function. */
2527 tree functions;
2528 tree type;
2530 /* If the MEMBER is exactly one static member function, then we
2531 know the type of the expression. Otherwise, we must wait
2532 until overload resolution has been performed. */
2533 functions = BASELINK_FUNCTIONS (member);
2534 if (TREE_CODE (functions) == FUNCTION_DECL
2535 && DECL_STATIC_FUNCTION_P (functions))
2536 type = TREE_TYPE (functions);
2537 else
2538 type = unknown_type_node;
2539 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2540 base. That will happen when the function is called. */
2541 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2543 else if (TREE_CODE (member) == CONST_DECL)
2545 /* The member is an enumerator. */
2546 result = member;
2547 /* If OBJECT has side-effects, they are supposed to occur. */
2548 if (TREE_SIDE_EFFECTS (object))
2549 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2550 object, result);
2552 else if ((using_decl = strip_using_decl (member)) != member)
2553 result = build_class_member_access_expr (object,
2554 using_decl,
2555 access_path, preserve_reference,
2556 complain);
2557 else
2559 if (complain & tf_error)
2560 error ("invalid use of %qD", member);
2561 return error_mark_node;
2564 if (!preserve_reference)
2565 /* [expr.ref]
2567 If E2 is declared to have type "reference to T", then ... the
2568 type of E1.E2 is T. */
2569 result = convert_from_reference (result);
2571 return result;
2574 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2575 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2577 static tree
2578 lookup_destructor (tree object, tree scope, tree dtor_name,
2579 tsubst_flags_t complain)
2581 tree object_type = TREE_TYPE (object);
2582 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2583 tree expr;
2585 /* We've already complained about this destructor. */
2586 if (dtor_type == error_mark_node)
2587 return error_mark_node;
2589 if (scope && !check_dtor_name (scope, dtor_type))
2591 if (complain & tf_error)
2592 error ("qualified type %qT does not match destructor name ~%qT",
2593 scope, dtor_type);
2594 return error_mark_node;
2596 if (is_auto (dtor_type))
2597 dtor_type = object_type;
2598 else if (identifier_p (dtor_type))
2600 /* In a template, names we can't find a match for are still accepted
2601 destructor names, and we check them here. */
2602 if (check_dtor_name (object_type, dtor_type))
2603 dtor_type = object_type;
2604 else
2606 if (complain & tf_error)
2607 error ("object type %qT does not match destructor name ~%qT",
2608 object_type, dtor_type);
2609 return error_mark_node;
2613 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2615 if (complain & tf_error)
2616 error ("the type being destroyed is %qT, but the destructor "
2617 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2618 return error_mark_node;
2620 expr = lookup_member (dtor_type, complete_dtor_identifier,
2621 /*protect=*/1, /*want_type=*/false,
2622 tf_warning_or_error);
2623 if (!expr)
2625 if (complain & tf_error)
2626 cxx_incomplete_type_error (dtor_name, dtor_type);
2627 return error_mark_node;
2629 expr = (adjust_result_of_qualified_name_lookup
2630 (expr, dtor_type, object_type));
2631 if (scope == NULL_TREE)
2632 /* We need to call adjust_result_of_qualified_name_lookup in case the
2633 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2634 that we still get virtual function binding. */
2635 BASELINK_QUALIFIED_P (expr) = false;
2636 return expr;
2639 /* An expression of the form "A::template B" has been resolved to
2640 DECL. Issue a diagnostic if B is not a template or template
2641 specialization. */
2643 void
2644 check_template_keyword (tree decl)
2646 /* The standard says:
2648 [temp.names]
2650 If a name prefixed by the keyword template is not a member
2651 template, the program is ill-formed.
2653 DR 228 removed the restriction that the template be a member
2654 template.
2656 DR 96, if accepted would add the further restriction that explicit
2657 template arguments must be provided if the template keyword is
2658 used, but, as of 2005-10-16, that DR is still in "drafting". If
2659 this DR is accepted, then the semantic checks here can be
2660 simplified, as the entity named must in fact be a template
2661 specialization, rather than, as at present, a set of overloaded
2662 functions containing at least one template function. */
2663 if (TREE_CODE (decl) != TEMPLATE_DECL
2664 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2666 if (VAR_P (decl))
2668 if (DECL_USE_TEMPLATE (decl)
2669 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2671 else
2672 permerror (input_location, "%qD is not a template", decl);
2674 else if (!is_overloaded_fn (decl))
2675 permerror (input_location, "%qD is not a template", decl);
2676 else
2678 bool found = false;
2680 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2681 !found && iter; ++iter)
2683 tree fn = *iter;
2684 if (TREE_CODE (fn) == TEMPLATE_DECL
2685 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2686 || (TREE_CODE (fn) == FUNCTION_DECL
2687 && DECL_USE_TEMPLATE (fn)
2688 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2689 found = true;
2691 if (!found)
2692 permerror (input_location, "%qD is not a template", decl);
2697 /* Record that an access failure occurred on BASETYPE_PATH attempting
2698 to access FIELD_DECL. */
2700 void
2701 access_failure_info::record_access_failure (tree basetype_path,
2702 tree field_decl)
2704 m_was_inaccessible = true;
2705 m_basetype_path = basetype_path;
2706 m_field_decl = field_decl;
2709 /* If an access failure was recorded, then attempt to locate an
2710 accessor function for the pertinent field, and if one is
2711 available, add a note and fix-it hint suggesting using it. */
2713 void
2714 access_failure_info::maybe_suggest_accessor (bool const_p) const
2716 if (!m_was_inaccessible)
2717 return;
2719 tree accessor
2720 = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2721 if (!accessor)
2722 return;
2724 /* The accessor must itself be accessible for it to be a reasonable
2725 suggestion. */
2726 if (!accessible_p (m_basetype_path, accessor, true))
2727 return;
2729 rich_location richloc (line_table, input_location);
2730 pretty_printer pp;
2731 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2732 richloc.add_fixit_replace (pp_formatted_text (&pp));
2733 inform (&richloc, "field %q#D can be accessed via %q#D",
2734 m_field_decl, accessor);
2737 /* This function is called by the parser to process a class member
2738 access expression of the form OBJECT.NAME. NAME is a node used by
2739 the parser to represent a name; it is not yet a DECL. It may,
2740 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2741 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2742 there is no reason to do the lookup twice, so the parser keeps the
2743 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2744 be a template via the use of the "A::template B" syntax. */
2746 tree
2747 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2748 tsubst_flags_t complain)
2750 tree expr;
2751 tree object_type;
2752 tree member;
2753 tree access_path = NULL_TREE;
2754 tree orig_object = object;
2755 tree orig_name = name;
2757 if (object == error_mark_node || name == error_mark_node)
2758 return error_mark_node;
2760 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2761 if (!objc_is_public (object, name))
2762 return error_mark_node;
2764 object_type = TREE_TYPE (object);
2766 if (processing_template_decl)
2768 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2769 type_dependent_object_expression_p (object)
2770 /* If NAME is "f<args>", where either 'f' or 'args' is
2771 dependent, then the expression is dependent. */
2772 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2773 && dependent_template_id_p (TREE_OPERAND (name, 0),
2774 TREE_OPERAND (name, 1)))
2775 /* If NAME is "T::X" where "T" is dependent, then the
2776 expression is dependent. */
2777 || (TREE_CODE (name) == SCOPE_REF
2778 && TYPE_P (TREE_OPERAND (name, 0))
2779 && dependent_scope_p (TREE_OPERAND (name, 0))))
2781 dependent:
2782 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2783 orig_object, orig_name, NULL_TREE);
2785 object = build_non_dependent_expr (object);
2787 else if (c_dialect_objc ()
2788 && identifier_p (name)
2789 && (expr = objc_maybe_build_component_ref (object, name)))
2790 return expr;
2792 /* [expr.ref]
2794 The type of the first expression shall be "class object" (of a
2795 complete type). */
2796 if (!currently_open_class (object_type)
2797 && !complete_type_or_maybe_complain (object_type, object, complain))
2798 return error_mark_node;
2799 if (!CLASS_TYPE_P (object_type))
2801 if (complain & tf_error)
2803 if (POINTER_TYPE_P (object_type)
2804 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2805 error ("request for member %qD in %qE, which is of pointer "
2806 "type %qT (maybe you meant to use %<->%> ?)",
2807 name, object.get_value (), object_type);
2808 else
2809 error ("request for member %qD in %qE, which is of non-class "
2810 "type %qT", name, object.get_value (), object_type);
2812 return error_mark_node;
2815 if (BASELINK_P (name))
2816 /* A member function that has already been looked up. */
2817 member = name;
2818 else
2820 bool is_template_id = false;
2821 tree template_args = NULL_TREE;
2822 tree scope = NULL_TREE;
2824 access_path = object_type;
2826 if (TREE_CODE (name) == SCOPE_REF)
2828 /* A qualified name. The qualifying class or namespace `S'
2829 has already been looked up; it is either a TYPE or a
2830 NAMESPACE_DECL. */
2831 scope = TREE_OPERAND (name, 0);
2832 name = TREE_OPERAND (name, 1);
2834 /* If SCOPE is a namespace, then the qualified name does not
2835 name a member of OBJECT_TYPE. */
2836 if (TREE_CODE (scope) == NAMESPACE_DECL)
2838 if (complain & tf_error)
2839 error ("%<%D::%D%> is not a member of %qT",
2840 scope, name, object_type);
2841 return error_mark_node;
2845 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2847 is_template_id = true;
2848 template_args = TREE_OPERAND (name, 1);
2849 name = TREE_OPERAND (name, 0);
2851 if (!identifier_p (name))
2852 name = OVL_NAME (name);
2855 if (scope)
2857 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2859 gcc_assert (!is_template_id);
2860 /* Looking up a member enumerator (c++/56793). */
2861 if (!TYPE_CLASS_SCOPE_P (scope)
2862 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2864 if (complain & tf_error)
2865 error ("%<%D::%D%> is not a member of %qT",
2866 scope, name, object_type);
2867 return error_mark_node;
2869 tree val = lookup_enumerator (scope, name);
2870 if (!val)
2872 if (complain & tf_error)
2873 error ("%qD is not a member of %qD",
2874 name, scope);
2875 return error_mark_node;
2878 if (TREE_SIDE_EFFECTS (object))
2879 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2880 return val;
2883 gcc_assert (CLASS_TYPE_P (scope));
2884 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2886 if (constructor_name_p (name, scope))
2888 if (complain & tf_error)
2889 error ("cannot call constructor %<%T::%D%> directly",
2890 scope, name);
2891 return error_mark_node;
2894 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2895 access_path = lookup_base (object_type, scope, ba_check,
2896 NULL, complain);
2897 if (access_path == error_mark_node)
2898 return error_mark_node;
2899 if (!access_path)
2901 if (any_dependent_bases_p (object_type))
2902 goto dependent;
2903 if (complain & tf_error)
2904 error ("%qT is not a base of %qT", scope, object_type);
2905 return error_mark_node;
2909 if (TREE_CODE (name) == BIT_NOT_EXPR)
2911 if (dependent_type_p (object_type))
2912 /* The destructor isn't declared yet. */
2913 goto dependent;
2914 member = lookup_destructor (object, scope, name, complain);
2916 else
2918 /* Look up the member. */
2919 access_failure_info afi;
2920 member = lookup_member (access_path, name, /*protect=*/1,
2921 /*want_type=*/false, complain,
2922 &afi);
2923 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2924 if (member == NULL_TREE)
2926 if (dependent_type_p (object_type))
2927 /* Try again at instantiation time. */
2928 goto dependent;
2929 if (complain & tf_error)
2931 tree guessed_id = lookup_member_fuzzy (access_path, name,
2932 /*want_type=*/false);
2933 if (guessed_id)
2935 location_t bogus_component_loc = input_location;
2936 gcc_rich_location rich_loc (bogus_component_loc);
2937 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2938 guessed_id);
2939 error_at (&rich_loc,
2940 "%q#T has no member named %qE;"
2941 " did you mean %qE?",
2942 TREE_CODE (access_path) == TREE_BINFO
2943 ? TREE_TYPE (access_path) : object_type,
2944 name, guessed_id);
2946 else
2947 error ("%q#T has no member named %qE",
2948 TREE_CODE (access_path) == TREE_BINFO
2949 ? TREE_TYPE (access_path) : object_type, name);
2951 return error_mark_node;
2953 if (member == error_mark_node)
2954 return error_mark_node;
2955 if (DECL_P (member)
2956 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2957 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2958 wrong, so don't use it. */
2959 goto dependent;
2960 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2961 goto dependent;
2964 if (is_template_id)
2966 tree templ = member;
2968 if (BASELINK_P (templ))
2969 member = lookup_template_function (templ, template_args);
2970 else if (variable_template_p (templ))
2971 member = (lookup_and_finish_template_variable
2972 (templ, template_args, complain));
2973 else
2975 if (complain & tf_error)
2976 error ("%qD is not a member template function", name);
2977 return error_mark_node;
2982 if (TREE_DEPRECATED (member))
2983 warn_deprecated_use (member, NULL_TREE);
2985 if (template_p)
2986 check_template_keyword (member);
2988 expr = build_class_member_access_expr (object, member, access_path,
2989 /*preserve_reference=*/false,
2990 complain);
2991 if (processing_template_decl && expr != error_mark_node)
2993 if (BASELINK_P (member))
2995 if (TREE_CODE (orig_name) == SCOPE_REF)
2996 BASELINK_QUALIFIED_P (member) = 1;
2997 orig_name = member;
2999 return build_min_non_dep (COMPONENT_REF, expr,
3000 orig_object, orig_name,
3001 NULL_TREE);
3004 return expr;
3007 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3008 type. */
3010 tree
3011 build_simple_component_ref (tree object, tree member)
3013 tree type = cp_build_qualified_type (TREE_TYPE (member),
3014 cp_type_quals (TREE_TYPE (object)));
3015 return build3_loc (input_location,
3016 COMPONENT_REF, type,
3017 object, member, NULL_TREE);
3020 /* Return an expression for the MEMBER_NAME field in the internal
3021 representation of PTRMEM, a pointer-to-member function. (Each
3022 pointer-to-member function type gets its own RECORD_TYPE so it is
3023 more convenient to access the fields by name than by FIELD_DECL.)
3024 This routine converts the NAME to a FIELD_DECL and then creates the
3025 node for the complete expression. */
3027 tree
3028 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3030 tree ptrmem_type;
3031 tree member;
3033 /* This code is a stripped down version of
3034 build_class_member_access_expr. It does not work to use that
3035 routine directly because it expects the object to be of class
3036 type. */
3037 ptrmem_type = TREE_TYPE (ptrmem);
3038 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3039 for (member = TYPE_FIELDS (ptrmem_type); member;
3040 member = DECL_CHAIN (member))
3041 if (DECL_NAME (member) == member_name)
3042 break;
3043 tree res = build_simple_component_ref (ptrmem, member);
3045 TREE_NO_WARNING (res) = 1;
3046 return res;
3049 /* Given an expression PTR for a pointer, return an expression
3050 for the value pointed to.
3051 ERRORSTRING is the name of the operator to appear in error messages.
3053 This function may need to overload OPERATOR_FNNAME.
3054 Must also handle REFERENCE_TYPEs for C++. */
3056 tree
3057 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3058 tsubst_flags_t complain)
3060 tree orig_expr = expr;
3061 tree rval;
3062 tree overload = NULL_TREE;
3064 if (processing_template_decl)
3066 /* Retain the type if we know the operand is a pointer. */
3067 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
3068 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3069 if (type_dependent_expression_p (expr))
3070 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3071 expr = build_non_dependent_expr (expr);
3074 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3075 NULL_TREE, NULL_TREE, &overload, complain);
3076 if (!rval)
3077 rval = cp_build_indirect_ref (expr, errorstring, complain);
3079 if (processing_template_decl && rval != error_mark_node)
3081 if (overload != NULL_TREE)
3082 return (build_min_non_dep_op_overload
3083 (INDIRECT_REF, rval, overload, orig_expr));
3085 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3087 else
3088 return rval;
3091 /* The implementation of the above, and of indirection implied by other
3092 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3094 static tree
3095 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3096 tsubst_flags_t complain, bool do_fold)
3098 tree pointer, type;
3100 /* RO_NULL should only be used with the folding entry points below, not
3101 cp_build_indirect_ref. */
3102 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3104 if (ptr == current_class_ptr
3105 || (TREE_CODE (ptr) == NOP_EXPR
3106 && TREE_OPERAND (ptr, 0) == current_class_ptr
3107 && (same_type_ignoring_top_level_qualifiers_p
3108 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3109 return current_class_ref;
3111 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3112 ? ptr : decay_conversion (ptr, complain));
3113 if (pointer == error_mark_node)
3114 return error_mark_node;
3116 type = TREE_TYPE (pointer);
3118 if (POINTER_TYPE_P (type))
3120 /* [expr.unary.op]
3122 If the type of the expression is "pointer to T," the type
3123 of the result is "T." */
3124 tree t = TREE_TYPE (type);
3126 if ((CONVERT_EXPR_P (ptr)
3127 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3128 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3130 /* If a warning is issued, mark it to avoid duplicates from
3131 the backend. This only needs to be done at
3132 warn_strict_aliasing > 2. */
3133 if (warn_strict_aliasing > 2)
3134 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
3135 type, TREE_OPERAND (ptr, 0)))
3136 TREE_NO_WARNING (ptr) = 1;
3139 if (VOID_TYPE_P (t))
3141 /* A pointer to incomplete type (other than cv void) can be
3142 dereferenced [expr.unary.op]/1 */
3143 if (complain & tf_error)
3144 error ("%qT is not a pointer-to-object type", type);
3145 return error_mark_node;
3147 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3148 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3149 /* The POINTER was something like `&x'. We simplify `*&x' to
3150 `x'. */
3151 return TREE_OPERAND (pointer, 0);
3152 else
3154 tree ref = build1 (INDIRECT_REF, t, pointer);
3156 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3157 so that we get the proper error message if the result is used
3158 to assign to. Also, &* is supposed to be a no-op. */
3159 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3160 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3161 TREE_SIDE_EFFECTS (ref)
3162 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3163 return ref;
3166 else if (!(complain & tf_error))
3167 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3169 /* `pointer' won't be an error_mark_node if we were given a
3170 pointer to member, so it's cool to check for this here. */
3171 else if (TYPE_PTRMEM_P (type))
3172 switch (errorstring)
3174 case RO_ARRAY_INDEXING:
3175 error ("invalid use of array indexing on pointer to member");
3176 break;
3177 case RO_UNARY_STAR:
3178 error ("invalid use of unary %<*%> on pointer to member");
3179 break;
3180 case RO_IMPLICIT_CONVERSION:
3181 error ("invalid use of implicit conversion on pointer to member");
3182 break;
3183 case RO_ARROW_STAR:
3184 error ("left hand operand of %<->*%> must be a pointer to class, "
3185 "but is a pointer to member of type %qT", type);
3186 break;
3187 default:
3188 gcc_unreachable ();
3190 else if (pointer != error_mark_node)
3191 invalid_indirection_error (input_location, type, errorstring);
3193 return error_mark_node;
3196 /* Entry point used by c-common, which expects folding. */
3198 tree
3199 build_indirect_ref (location_t /*loc*/,
3200 tree ptr, ref_operator errorstring)
3202 return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3205 /* Entry point used by internal indirection needs that don't correspond to any
3206 syntactic construct. */
3208 tree
3209 cp_build_fold_indirect_ref (tree pointer)
3211 return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3214 /* Entry point used by indirection needs that correspond to some syntactic
3215 construct. */
3217 tree
3218 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3219 tsubst_flags_t complain)
3221 return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3224 /* This handles expressions of the form "a[i]", which denotes
3225 an array reference.
3227 This is logically equivalent in C to *(a+i), but we may do it differently.
3228 If A is a variable or a member, we generate a primitive ARRAY_REF.
3229 This avoids forcing the array out of registers, and can work on
3230 arrays that are not lvalues (for example, members of structures returned
3231 by functions).
3233 If INDEX is of some user-defined type, it must be converted to
3234 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3235 will inherit the type of the array, which will be some pointer type.
3237 LOC is the location to use in building the array reference. */
3239 tree
3240 cp_build_array_ref (location_t loc, tree array, tree idx,
3241 tsubst_flags_t complain)
3243 tree ret;
3245 if (idx == 0)
3247 if (complain & tf_error)
3248 error_at (loc, "subscript missing in array reference");
3249 return error_mark_node;
3252 if (TREE_TYPE (array) == error_mark_node
3253 || TREE_TYPE (idx) == error_mark_node)
3254 return error_mark_node;
3256 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3257 inside it. */
3258 switch (TREE_CODE (array))
3260 case COMPOUND_EXPR:
3262 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3263 complain);
3264 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3265 TREE_OPERAND (array, 0), value);
3266 SET_EXPR_LOCATION (ret, loc);
3267 return ret;
3270 case COND_EXPR:
3271 ret = build_conditional_expr
3272 (loc, TREE_OPERAND (array, 0),
3273 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3274 complain),
3275 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3276 complain),
3277 complain);
3278 protected_set_expr_location (ret, loc);
3279 return ret;
3281 default:
3282 break;
3285 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3287 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3289 tree rval, type;
3291 warn_array_subscript_with_type_char (loc, idx);
3293 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3295 if (complain & tf_error)
3296 error_at (loc, "array subscript is not an integer");
3297 return error_mark_node;
3300 /* Apply integral promotions *after* noticing character types.
3301 (It is unclear why we do these promotions -- the standard
3302 does not say that we should. In fact, the natural thing would
3303 seem to be to convert IDX to ptrdiff_t; we're performing
3304 pointer arithmetic.) */
3305 idx = cp_perform_integral_promotions (idx, complain);
3307 /* An array that is indexed by a non-constant
3308 cannot be stored in a register; we must be able to do
3309 address arithmetic on its address.
3310 Likewise an array of elements of variable size. */
3311 if (TREE_CODE (idx) != INTEGER_CST
3312 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3313 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3314 != INTEGER_CST)))
3316 if (!cxx_mark_addressable (array, true))
3317 return error_mark_node;
3320 /* An array that is indexed by a constant value which is not within
3321 the array bounds cannot be stored in a register either; because we
3322 would get a crash in store_bit_field/extract_bit_field when trying
3323 to access a non-existent part of the register. */
3324 if (TREE_CODE (idx) == INTEGER_CST
3325 && TYPE_DOMAIN (TREE_TYPE (array))
3326 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3328 if (!cxx_mark_addressable (array))
3329 return error_mark_node;
3332 /* Note in C++ it is valid to subscript a `register' array, since
3333 it is valid to take the address of something with that
3334 storage specification. */
3335 if (extra_warnings)
3337 tree foo = array;
3338 while (TREE_CODE (foo) == COMPONENT_REF)
3339 foo = TREE_OPERAND (foo, 0);
3340 if (VAR_P (foo) && DECL_REGISTER (foo)
3341 && (complain & tf_warning))
3342 warning_at (loc, OPT_Wextra,
3343 "subscripting array declared %<register%>");
3346 type = TREE_TYPE (TREE_TYPE (array));
3347 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3348 /* Array ref is const/volatile if the array elements are
3349 or if the array is.. */
3350 TREE_READONLY (rval)
3351 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3352 TREE_SIDE_EFFECTS (rval)
3353 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3354 TREE_THIS_VOLATILE (rval)
3355 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3356 ret = require_complete_type_sfinae (rval, complain);
3357 protected_set_expr_location (ret, loc);
3358 if (non_lvalue)
3359 ret = non_lvalue_loc (loc, ret);
3360 return ret;
3364 tree ar = cp_default_conversion (array, complain);
3365 tree ind = cp_default_conversion (idx, complain);
3367 /* Put the integer in IND to simplify error checking. */
3368 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3369 std::swap (ar, ind);
3371 if (ar == error_mark_node || ind == error_mark_node)
3372 return error_mark_node;
3374 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3376 if (complain & tf_error)
3377 error_at (loc, "subscripted value is neither array nor pointer");
3378 return error_mark_node;
3380 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3382 if (complain & tf_error)
3383 error_at (loc, "array subscript is not an integer");
3384 return error_mark_node;
3387 warn_array_subscript_with_type_char (loc, idx);
3389 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3390 PLUS_EXPR, ar, ind,
3391 complain),
3392 RO_ARRAY_INDEXING,
3393 complain);
3394 protected_set_expr_location (ret, loc);
3395 if (non_lvalue)
3396 ret = non_lvalue_loc (loc, ret);
3397 return ret;
3401 /* Entry point for Obj-C++. */
3403 tree
3404 build_array_ref (location_t loc, tree array, tree idx)
3406 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3409 /* Resolve a pointer to member function. INSTANCE is the object
3410 instance to use, if the member points to a virtual member.
3412 This used to avoid checking for virtual functions if basetype
3413 has no virtual functions, according to an earlier ANSI draft.
3414 With the final ISO C++ rules, such an optimization is
3415 incorrect: A pointer to a derived member can be static_cast
3416 to pointer-to-base-member, as long as the dynamic object
3417 later has the right member. So now we only do this optimization
3418 when we know the dynamic type of the object. */
3420 tree
3421 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3422 tsubst_flags_t complain)
3424 if (TREE_CODE (function) == OFFSET_REF)
3425 function = TREE_OPERAND (function, 1);
3427 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3429 tree idx, delta, e1, e2, e3, vtbl;
3430 bool nonvirtual;
3431 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3432 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3434 tree instance_ptr = *instance_ptrptr;
3435 tree instance_save_expr = 0;
3436 if (instance_ptr == error_mark_node)
3438 if (TREE_CODE (function) == PTRMEM_CST)
3440 /* Extracting the function address from a pmf is only
3441 allowed with -Wno-pmf-conversions. It only works for
3442 pmf constants. */
3443 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3444 e1 = convert (fntype, e1);
3445 return e1;
3447 else
3449 if (complain & tf_error)
3450 error ("object missing in use of %qE", function);
3451 return error_mark_node;
3455 /* True if we know that the dynamic type of the object doesn't have
3456 virtual functions, so we can assume the PFN field is a pointer. */
3457 nonvirtual = (COMPLETE_TYPE_P (basetype)
3458 && !TYPE_POLYMORPHIC_P (basetype)
3459 && resolves_to_fixed_type_p (instance_ptr, 0));
3461 /* If we don't really have an object (i.e. in an ill-formed
3462 conversion from PMF to pointer), we can't resolve virtual
3463 functions anyway. */
3464 if (!nonvirtual && is_dummy_object (instance_ptr))
3465 nonvirtual = true;
3467 if (TREE_SIDE_EFFECTS (instance_ptr))
3468 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3470 if (TREE_SIDE_EFFECTS (function))
3471 function = save_expr (function);
3473 /* Start by extracting all the information from the PMF itself. */
3474 e3 = pfn_from_ptrmemfunc (function);
3475 delta = delta_from_ptrmemfunc (function);
3476 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3477 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3479 int flag_sanitize_save;
3480 case ptrmemfunc_vbit_in_pfn:
3481 e1 = cp_build_binary_op (input_location,
3482 BIT_AND_EXPR, idx, integer_one_node,
3483 complain);
3484 idx = cp_build_binary_op (input_location,
3485 MINUS_EXPR, idx, integer_one_node,
3486 complain);
3487 if (idx == error_mark_node)
3488 return error_mark_node;
3489 break;
3491 case ptrmemfunc_vbit_in_delta:
3492 e1 = cp_build_binary_op (input_location,
3493 BIT_AND_EXPR, delta, integer_one_node,
3494 complain);
3495 /* Don't instrument the RSHIFT_EXPR we're about to create because
3496 we're going to use DELTA number of times, and that wouldn't play
3497 well with SAVE_EXPRs therein. */
3498 flag_sanitize_save = flag_sanitize;
3499 flag_sanitize = 0;
3500 delta = cp_build_binary_op (input_location,
3501 RSHIFT_EXPR, delta, integer_one_node,
3502 complain);
3503 flag_sanitize = flag_sanitize_save;
3504 if (delta == error_mark_node)
3505 return error_mark_node;
3506 break;
3508 default:
3509 gcc_unreachable ();
3512 if (e1 == error_mark_node)
3513 return error_mark_node;
3515 /* Convert down to the right base before using the instance. A
3516 special case is that in a pointer to member of class C, C may
3517 be incomplete. In that case, the function will of course be
3518 a member of C, and no conversion is required. In fact,
3519 lookup_base will fail in that case, because incomplete
3520 classes do not have BINFOs. */
3521 if (!same_type_ignoring_top_level_qualifiers_p
3522 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3524 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3525 basetype, ba_check, NULL, complain);
3526 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3527 1, complain);
3528 if (instance_ptr == error_mark_node)
3529 return error_mark_node;
3531 /* ...and then the delta in the PMF. */
3532 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3534 /* Hand back the adjusted 'this' argument to our caller. */
3535 *instance_ptrptr = instance_ptr;
3537 if (nonvirtual)
3538 /* Now just return the pointer. */
3539 return e3;
3541 /* Next extract the vtable pointer from the object. */
3542 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3543 instance_ptr);
3544 vtbl = cp_build_fold_indirect_ref (vtbl);
3545 if (vtbl == error_mark_node)
3546 return error_mark_node;
3548 /* Finally, extract the function pointer from the vtable. */
3549 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3550 e2 = cp_build_fold_indirect_ref (e2);
3551 if (e2 == error_mark_node)
3552 return error_mark_node;
3553 TREE_CONSTANT (e2) = 1;
3555 /* When using function descriptors, the address of the
3556 vtable entry is treated as a function pointer. */
3557 if (TARGET_VTABLE_USES_DESCRIPTORS)
3558 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3559 cp_build_addr_expr (e2, complain));
3561 e2 = fold_convert (TREE_TYPE (e3), e2);
3562 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3563 if (e1 == error_mark_node)
3564 return error_mark_node;
3566 /* Make sure this doesn't get evaluated first inside one of the
3567 branches of the COND_EXPR. */
3568 if (instance_save_expr)
3569 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3570 instance_save_expr, e1);
3572 function = e1;
3574 return function;
3577 /* Used by the C-common bits. */
3578 tree
3579 build_function_call (location_t /*loc*/,
3580 tree function, tree params)
3582 return cp_build_function_call (function, params, tf_warning_or_error);
3585 /* Used by the C-common bits. */
3586 tree
3587 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3588 tree function, vec<tree, va_gc> *params,
3589 vec<tree, va_gc> * /*origtypes*/)
3591 vec<tree, va_gc> *orig_params = params;
3592 tree ret = cp_build_function_call_vec (function, &params,
3593 tf_warning_or_error);
3595 /* cp_build_function_call_vec can reallocate PARAMS by adding
3596 default arguments. That should never happen here. Verify
3597 that. */
3598 gcc_assert (params == orig_params);
3600 return ret;
3603 /* Build a function call using a tree list of arguments. */
3605 static tree
3606 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3608 vec<tree, va_gc> *vec;
3609 tree ret;
3611 vec = make_tree_vector ();
3612 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3613 vec_safe_push (vec, TREE_VALUE (params));
3614 ret = cp_build_function_call_vec (function, &vec, complain);
3615 release_tree_vector (vec);
3616 return ret;
3619 /* Build a function call using varargs. */
3621 tree
3622 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3624 vec<tree, va_gc> *vec;
3625 va_list args;
3626 tree ret, t;
3628 vec = make_tree_vector ();
3629 va_start (args, complain);
3630 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3631 vec_safe_push (vec, t);
3632 va_end (args);
3633 ret = cp_build_function_call_vec (function, &vec, complain);
3634 release_tree_vector (vec);
3635 return ret;
3638 /* Build a function call using a vector of arguments. PARAMS may be
3639 NULL if there are no parameters. This changes the contents of
3640 PARAMS. */
3642 tree
3643 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3644 tsubst_flags_t complain)
3646 tree fntype, fndecl;
3647 int is_method;
3648 tree original = function;
3649 int nargs;
3650 tree *argarray;
3651 tree parm_types;
3652 vec<tree, va_gc> *allocated = NULL;
3653 tree ret;
3655 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3656 expressions, like those used for ObjC messenger dispatches. */
3657 if (params != NULL && !vec_safe_is_empty (*params))
3658 function = objc_rewrite_function_call (function, (**params)[0]);
3660 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3661 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3662 if (TREE_CODE (function) == NOP_EXPR
3663 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3664 function = TREE_OPERAND (function, 0);
3666 if (TREE_CODE (function) == FUNCTION_DECL)
3668 /* If the function is a non-template member function
3669 or a non-template friend, then we need to check the
3670 constraints.
3672 Note that if overload resolution failed with a single
3673 candidate this function will be used to explicitly diagnose
3674 the failure for the single call expression. The check is
3675 technically redundant since we also would have failed in
3676 add_function_candidate. */
3677 if (flag_concepts
3678 && (complain & tf_error)
3679 && !constraints_satisfied_p (function))
3681 error ("cannot call function %qD", function);
3682 location_t loc = DECL_SOURCE_LOCATION (function);
3683 diagnose_constraints (loc, function, NULL_TREE);
3684 return error_mark_node;
3687 if (!mark_used (function, complain) && !(complain & tf_error))
3688 return error_mark_node;
3689 fndecl = function;
3691 /* Convert anything with function type to a pointer-to-function. */
3692 if (DECL_MAIN_P (function))
3694 if (complain & tf_error)
3695 pedwarn (input_location, OPT_Wpedantic,
3696 "ISO C++ forbids calling %<::main%> from within program");
3697 else
3698 return error_mark_node;
3700 function = build_addr_func (function, complain);
3702 else
3704 fndecl = NULL_TREE;
3706 function = build_addr_func (function, complain);
3709 if (function == error_mark_node)
3710 return error_mark_node;
3712 fntype = TREE_TYPE (function);
3714 if (TYPE_PTRMEMFUNC_P (fntype))
3716 if (complain & tf_error)
3717 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3718 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3719 original, original);
3720 return error_mark_node;
3723 is_method = (TYPE_PTR_P (fntype)
3724 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3726 if (!(TYPE_PTRFN_P (fntype)
3727 || is_method
3728 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3730 if (complain & tf_error)
3732 if (!flag_diagnostics_show_caret)
3733 error_at (input_location,
3734 "%qE cannot be used as a function", original);
3735 else if (DECL_P (original))
3736 error_at (input_location,
3737 "%qD cannot be used as a function", original);
3738 else
3739 error_at (input_location,
3740 "expression cannot be used as a function");
3743 return error_mark_node;
3746 /* fntype now gets the type of function pointed to. */
3747 fntype = TREE_TYPE (fntype);
3748 parm_types = TYPE_ARG_TYPES (fntype);
3750 if (params == NULL)
3752 allocated = make_tree_vector ();
3753 params = &allocated;
3756 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3757 complain);
3758 if (nargs < 0)
3759 return error_mark_node;
3761 argarray = (*params)->address ();
3763 /* Check for errors in format strings and inappropriately
3764 null parameters. */
3765 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3766 nargs, argarray, NULL);
3768 ret = build_cxx_call (function, nargs, argarray, complain);
3770 if (warned_p)
3772 tree c = extract_call_expr (ret);
3773 if (TREE_CODE (c) == CALL_EXPR)
3774 TREE_NO_WARNING (c) = 1;
3777 if (allocated != NULL)
3778 release_tree_vector (allocated);
3780 return ret;
3783 /* Subroutine of convert_arguments.
3784 Print an error message about a wrong number of arguments. */
3786 static void
3787 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3789 if (fndecl)
3791 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3793 if (DECL_NAME (fndecl) == NULL_TREE
3794 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3795 error_at (loc,
3796 too_many_p
3797 ? G_("too many arguments to constructor %q#D")
3798 : G_("too few arguments to constructor %q#D"),
3799 fndecl);
3800 else
3801 error_at (loc,
3802 too_many_p
3803 ? G_("too many arguments to member function %q#D")
3804 : G_("too few arguments to member function %q#D"),
3805 fndecl);
3807 else
3808 error_at (loc,
3809 too_many_p
3810 ? G_("too many arguments to function %q#D")
3811 : G_("too few arguments to function %q#D"),
3812 fndecl);
3813 if (!DECL_IS_BUILTIN (fndecl))
3814 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3816 else
3818 if (c_dialect_objc () && objc_message_selector ())
3819 error_at (loc,
3820 too_many_p
3821 ? G_("too many arguments to method %q#D")
3822 : G_("too few arguments to method %q#D"),
3823 objc_message_selector ());
3824 else
3825 error_at (loc, too_many_p ? G_("too many arguments to function")
3826 : G_("too few arguments to function"));
3830 /* Convert the actual parameter expressions in the list VALUES to the
3831 types in the list TYPELIST. The converted expressions are stored
3832 back in the VALUES vector.
3833 If parmdecls is exhausted, or when an element has NULL as its type,
3834 perform the default conversions.
3836 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3838 This is also where warnings about wrong number of args are generated.
3840 Returns the actual number of arguments processed (which might be less
3841 than the length of the vector), or -1 on error.
3843 In C++, unspecified trailing parameters can be filled in with their
3844 default arguments, if such were specified. Do so here. */
3846 static int
3847 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3848 int flags, tsubst_flags_t complain)
3850 tree typetail;
3851 unsigned int i;
3853 /* Argument passing is always copy-initialization. */
3854 flags |= LOOKUP_ONLYCONVERTING;
3856 for (i = 0, typetail = typelist;
3857 i < vec_safe_length (*values);
3858 i++)
3860 tree type = typetail ? TREE_VALUE (typetail) : 0;
3861 tree val = (**values)[i];
3863 if (val == error_mark_node || type == error_mark_node)
3864 return -1;
3866 if (type == void_type_node)
3868 if (complain & tf_error)
3870 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3871 return i;
3873 else
3874 return -1;
3877 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3878 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3879 if (TREE_CODE (val) == NOP_EXPR
3880 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3881 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3882 val = TREE_OPERAND (val, 0);
3884 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3886 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3887 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3888 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3889 val = decay_conversion (val, complain);
3892 if (val == error_mark_node)
3893 return -1;
3895 if (type != 0)
3897 /* Formal parm type is specified by a function prototype. */
3898 tree parmval;
3900 if (!COMPLETE_TYPE_P (complete_type (type)))
3902 if (complain & tf_error)
3904 if (fndecl)
3905 error ("parameter %P of %qD has incomplete type %qT",
3906 i, fndecl, type);
3907 else
3908 error ("parameter %P has incomplete type %qT", i, type);
3910 parmval = error_mark_node;
3912 else
3914 parmval = convert_for_initialization
3915 (NULL_TREE, type, val, flags,
3916 ICR_ARGPASS, fndecl, i, complain);
3917 parmval = convert_for_arg_passing (type, parmval, complain);
3920 if (parmval == error_mark_node)
3921 return -1;
3923 (**values)[i] = parmval;
3925 else
3927 if (fndecl && magic_varargs_p (fndecl))
3928 /* Don't do ellipsis conversion for __built_in_constant_p
3929 as this will result in spurious errors for non-trivial
3930 types. */
3931 val = require_complete_type_sfinae (val, complain);
3932 else
3933 val = convert_arg_to_ellipsis (val, complain);
3935 (**values)[i] = val;
3938 if (typetail)
3939 typetail = TREE_CHAIN (typetail);
3942 if (typetail != 0 && typetail != void_list_node)
3944 /* See if there are default arguments that can be used. Because
3945 we hold default arguments in the FUNCTION_TYPE (which is so
3946 wrong), we can see default parameters here from deduced
3947 contexts (and via typeof) for indirect function calls.
3948 Fortunately we know whether we have a function decl to
3949 provide default arguments in a language conformant
3950 manner. */
3951 if (fndecl && TREE_PURPOSE (typetail)
3952 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3954 for (; typetail != void_list_node; ++i)
3956 /* After DR777, with explicit template args we can end up with a
3957 default argument followed by no default argument. */
3958 if (!TREE_PURPOSE (typetail))
3959 break;
3960 tree parmval
3961 = convert_default_arg (TREE_VALUE (typetail),
3962 TREE_PURPOSE (typetail),
3963 fndecl, i, complain);
3965 if (parmval == error_mark_node)
3966 return -1;
3968 vec_safe_push (*values, parmval);
3969 typetail = TREE_CHAIN (typetail);
3970 /* ends with `...'. */
3971 if (typetail == NULL_TREE)
3972 break;
3976 if (typetail && typetail != void_list_node)
3978 if (complain & tf_error)
3979 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3980 return -1;
3984 return (int) i;
3987 /* Build a binary-operation expression, after performing default
3988 conversions on the operands. CODE is the kind of expression to
3989 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3990 are the tree codes which correspond to ARG1 and ARG2 when issuing
3991 warnings about possibly misplaced parentheses. They may differ
3992 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3993 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3994 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3995 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3996 ARG2_CODE as ERROR_MARK. */
3998 tree
3999 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4000 enum tree_code arg1_code, tree arg2,
4001 enum tree_code arg2_code, tree *overload_p,
4002 tsubst_flags_t complain)
4004 tree orig_arg1;
4005 tree orig_arg2;
4006 tree expr;
4007 tree overload = NULL_TREE;
4009 orig_arg1 = arg1;
4010 orig_arg2 = arg2;
4012 if (processing_template_decl)
4014 if (type_dependent_expression_p (arg1)
4015 || type_dependent_expression_p (arg2))
4016 return build_min_nt_loc (loc, code, arg1, arg2);
4017 arg1 = build_non_dependent_expr (arg1);
4018 arg2 = build_non_dependent_expr (arg2);
4021 if (code == DOTSTAR_EXPR)
4022 expr = build_m_component_ref (arg1, arg2, complain);
4023 else
4024 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4025 &overload, complain);
4027 if (overload_p != NULL)
4028 *overload_p = overload;
4030 /* Check for cases such as x+y<<z which users are likely to
4031 misinterpret. But don't warn about obj << x + y, since that is a
4032 common idiom for I/O. */
4033 if (warn_parentheses
4034 && (complain & tf_warning)
4035 && !processing_template_decl
4036 && !error_operand_p (arg1)
4037 && !error_operand_p (arg2)
4038 && (code != LSHIFT_EXPR
4039 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4040 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4041 arg2_code, orig_arg2);
4043 if (processing_template_decl && expr != error_mark_node)
4045 if (overload != NULL_TREE)
4046 return (build_min_non_dep_op_overload
4047 (code, expr, overload, orig_arg1, orig_arg2));
4049 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4052 return expr;
4055 /* Build and return an ARRAY_REF expression. */
4057 tree
4058 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4059 tsubst_flags_t complain)
4061 tree orig_arg1 = arg1;
4062 tree orig_arg2 = arg2;
4063 tree expr;
4064 tree overload = NULL_TREE;
4066 if (processing_template_decl)
4068 if (type_dependent_expression_p (arg1)
4069 || type_dependent_expression_p (arg2))
4070 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4071 NULL_TREE, NULL_TREE);
4072 arg1 = build_non_dependent_expr (arg1);
4073 arg2 = build_non_dependent_expr (arg2);
4076 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4077 NULL_TREE, &overload, complain);
4079 if (processing_template_decl && expr != error_mark_node)
4081 if (overload != NULL_TREE)
4082 return (build_min_non_dep_op_overload
4083 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4085 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4086 NULL_TREE, NULL_TREE);
4088 return expr;
4091 /* Return whether OP is an expression of enum type cast to integer
4092 type. In C++ even unsigned enum types are cast to signed integer
4093 types. We do not want to issue warnings about comparisons between
4094 signed and unsigned types when one of the types is an enum type.
4095 Those warnings are always false positives in practice. */
4097 static bool
4098 enum_cast_to_int (tree op)
4100 if (CONVERT_EXPR_P (op)
4101 && TREE_TYPE (op) == integer_type_node
4102 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4103 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4104 return true;
4106 /* The cast may have been pushed into a COND_EXPR. */
4107 if (TREE_CODE (op) == COND_EXPR)
4108 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4109 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4111 return false;
4114 /* For the c-common bits. */
4115 tree
4116 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4117 bool /*convert_p*/)
4119 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4122 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4123 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4125 static tree
4126 build_vec_cmp (tree_code code, tree type,
4127 tree arg0, tree arg1)
4129 tree zero_vec = build_zero_cst (type);
4130 tree minus_one_vec = build_minus_one_cst (type);
4131 tree cmp_type = build_same_sized_truth_vector_type(type);
4132 tree cmp = build2 (code, cmp_type, arg0, arg1);
4133 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4136 /* Possibly warn about an address never being NULL. */
4138 static void
4139 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4141 if (!warn_address
4142 || (complain & tf_warning) == 0
4143 || c_inhibit_evaluation_warnings != 0
4144 || TREE_NO_WARNING (op))
4145 return;
4147 tree cop = fold_non_dependent_expr (op);
4149 if (TREE_CODE (cop) == ADDR_EXPR
4150 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4151 && !TREE_NO_WARNING (cop))
4152 warning_at (location, OPT_Waddress, "the address of %qD will never "
4153 "be NULL", TREE_OPERAND (cop, 0));
4155 if (CONVERT_EXPR_P (op)
4156 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4158 tree inner_op = op;
4159 STRIP_NOPS (inner_op);
4161 if (DECL_P (inner_op))
4162 warning_at (location, OPT_Waddress,
4163 "the compiler can assume that the address of "
4164 "%qD will never be NULL", inner_op);
4168 /* Build a binary-operation expression without default conversions.
4169 CODE is the kind of expression to build.
4170 LOCATION is the location_t of the operator in the source code.
4171 This function differs from `build' in several ways:
4172 the data type of the result is computed and recorded in it,
4173 warnings are generated if arg data types are invalid,
4174 special handling for addition and subtraction of pointers is known,
4175 and some optimization is done (operations on narrow ints
4176 are done in the narrower type when that gives the same result).
4177 Constant folding is also done before the result is returned.
4179 Note that the operands will never have enumeral types
4180 because either they have just had the default conversions performed
4181 or they have both just been converted to some other type in which
4182 the arithmetic is to be done.
4184 C++: must do special pointer arithmetic when implementing
4185 multiple inheritance, and deal with pointer to member functions. */
4187 tree
4188 cp_build_binary_op (location_t location,
4189 enum tree_code code, tree orig_op0, tree orig_op1,
4190 tsubst_flags_t complain)
4192 tree op0, op1;
4193 enum tree_code code0, code1;
4194 tree type0, type1;
4195 const char *invalid_op_diag;
4197 /* Expression code to give to the expression when it is built.
4198 Normally this is CODE, which is what the caller asked for,
4199 but in some special cases we change it. */
4200 enum tree_code resultcode = code;
4202 /* Data type in which the computation is to be performed.
4203 In the simplest cases this is the common type of the arguments. */
4204 tree result_type = NULL_TREE;
4206 /* Nonzero means operands have already been type-converted
4207 in whatever way is necessary.
4208 Zero means they need to be converted to RESULT_TYPE. */
4209 int converted = 0;
4211 /* Nonzero means create the expression with this type, rather than
4212 RESULT_TYPE. */
4213 tree build_type = 0;
4215 /* Nonzero means after finally constructing the expression
4216 convert it to this type. */
4217 tree final_type = 0;
4219 tree result, result_ovl;
4221 /* Nonzero if this is an operation like MIN or MAX which can
4222 safely be computed in short if both args are promoted shorts.
4223 Also implies COMMON.
4224 -1 indicates a bitwise operation; this makes a difference
4225 in the exact conditions for when it is safe to do the operation
4226 in a narrower mode. */
4227 int shorten = 0;
4229 /* Nonzero if this is a comparison operation;
4230 if both args are promoted shorts, compare the original shorts.
4231 Also implies COMMON. */
4232 int short_compare = 0;
4234 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4235 int common = 0;
4237 /* True if both operands have arithmetic type. */
4238 bool arithmetic_types_p;
4240 /* Apply default conversions. */
4241 op0 = orig_op0;
4242 op1 = orig_op1;
4244 /* Remember whether we're doing / or %. */
4245 bool doing_div_or_mod = false;
4247 /* Remember whether we're doing << or >>. */
4248 bool doing_shift = false;
4250 /* Tree holding instrumentation expression. */
4251 tree instrument_expr = NULL_TREE;
4253 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4254 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4255 || code == TRUTH_XOR_EXPR)
4257 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4258 op0 = decay_conversion (op0, complain);
4259 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4260 op1 = decay_conversion (op1, complain);
4262 else
4264 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4265 op0 = cp_default_conversion (op0, complain);
4266 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4267 op1 = cp_default_conversion (op1, complain);
4270 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4271 STRIP_TYPE_NOPS (op0);
4272 STRIP_TYPE_NOPS (op1);
4274 /* DTRT if one side is an overloaded function, but complain about it. */
4275 if (type_unknown_p (op0))
4277 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4278 if (t != error_mark_node)
4280 if (complain & tf_error)
4281 permerror (input_location, "assuming cast to type %qT from overloaded function",
4282 TREE_TYPE (t));
4283 op0 = t;
4286 if (type_unknown_p (op1))
4288 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4289 if (t != error_mark_node)
4291 if (complain & tf_error)
4292 permerror (input_location, "assuming cast to type %qT from overloaded function",
4293 TREE_TYPE (t));
4294 op1 = t;
4298 type0 = TREE_TYPE (op0);
4299 type1 = TREE_TYPE (op1);
4301 /* The expression codes of the data types of the arguments tell us
4302 whether the arguments are integers, floating, pointers, etc. */
4303 code0 = TREE_CODE (type0);
4304 code1 = TREE_CODE (type1);
4306 /* If an error was already reported for one of the arguments,
4307 avoid reporting another error. */
4308 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4309 return error_mark_node;
4311 if ((invalid_op_diag
4312 = targetm.invalid_binary_op (code, type0, type1)))
4314 if (complain & tf_error)
4315 error (invalid_op_diag);
4316 return error_mark_node;
4319 /* Issue warnings about peculiar, but valid, uses of NULL. */
4320 if ((orig_op0 == null_node || orig_op1 == null_node)
4321 /* It's reasonable to use pointer values as operands of &&
4322 and ||, so NULL is no exception. */
4323 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4324 && ( /* Both are NULL (or 0) and the operation was not a
4325 comparison or a pointer subtraction. */
4326 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4327 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4328 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4329 || (!null_ptr_cst_p (orig_op0)
4330 && !TYPE_PTR_OR_PTRMEM_P (type0))
4331 || (!null_ptr_cst_p (orig_op1)
4332 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4333 && (complain & tf_warning))
4335 source_location loc =
4336 expansion_point_location_if_in_system_header (input_location);
4338 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4341 /* In case when one of the operands of the binary operation is
4342 a vector and another is a scalar -- convert scalar to vector. */
4343 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4345 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4346 complain & tf_error);
4348 switch (convert_flag)
4350 case stv_error:
4351 return error_mark_node;
4352 case stv_firstarg:
4354 op0 = convert (TREE_TYPE (type1), op0);
4355 op0 = save_expr (op0);
4356 op0 = build_vector_from_val (type1, op0);
4357 type0 = TREE_TYPE (op0);
4358 code0 = TREE_CODE (type0);
4359 converted = 1;
4360 break;
4362 case stv_secondarg:
4364 op1 = convert (TREE_TYPE (type0), op1);
4365 op1 = save_expr (op1);
4366 op1 = build_vector_from_val (type0, op1);
4367 type1 = TREE_TYPE (op1);
4368 code1 = TREE_CODE (type1);
4369 converted = 1;
4370 break;
4372 default:
4373 break;
4377 switch (code)
4379 case MINUS_EXPR:
4380 /* Subtraction of two similar pointers.
4381 We must subtract them as integers, then divide by object size. */
4382 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4383 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4384 TREE_TYPE (type1)))
4386 result = pointer_diff (location, op0, op1,
4387 common_pointer_type (type0, type1), complain,
4388 &instrument_expr);
4389 if (instrument_expr != NULL)
4390 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4391 instrument_expr, result);
4393 return result;
4395 /* In all other cases except pointer - int, the usual arithmetic
4396 rules apply. */
4397 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4399 common = 1;
4400 break;
4402 /* The pointer - int case is just like pointer + int; fall
4403 through. */
4404 gcc_fallthrough ();
4405 case PLUS_EXPR:
4406 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4407 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4409 tree ptr_operand;
4410 tree int_operand;
4411 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4412 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4413 if (processing_template_decl)
4415 result_type = TREE_TYPE (ptr_operand);
4416 break;
4418 return cp_pointer_int_sum (location, code,
4419 ptr_operand,
4420 int_operand,
4421 complain);
4423 common = 1;
4424 break;
4426 case MULT_EXPR:
4427 common = 1;
4428 break;
4430 case TRUNC_DIV_EXPR:
4431 case CEIL_DIV_EXPR:
4432 case FLOOR_DIV_EXPR:
4433 case ROUND_DIV_EXPR:
4434 case EXACT_DIV_EXPR:
4435 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4437 tree type0 = TREE_OPERAND (op0, 0);
4438 tree type1 = TREE_OPERAND (op1, 0);
4439 tree first_arg = type0;
4440 if (!TYPE_P (type0))
4441 type0 = TREE_TYPE (type0);
4442 if (!TYPE_P (type1))
4443 type1 = TREE_TYPE (type1);
4444 if (POINTER_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4445 && !(TREE_CODE (first_arg) == PARM_DECL
4446 && DECL_ARRAY_PARAMETER_P (first_arg)
4447 && warn_sizeof_array_argument)
4448 && (complain & tf_warning))
4449 if (warning_at (location, OPT_Wsizeof_pointer_div,
4450 "division %<sizeof (%T) / sizeof (%T)%> does "
4451 "not compute the number of array elements",
4452 type0, type1))
4453 if (DECL_P (first_arg))
4454 inform (DECL_SOURCE_LOCATION (first_arg),
4455 "first %<sizeof%> operand was declared here");
4458 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4459 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4460 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4461 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4463 enum tree_code tcode0 = code0, tcode1 = code1;
4464 tree cop1 = fold_non_dependent_expr (op1);
4465 doing_div_or_mod = true;
4466 warn_for_div_by_zero (location, cop1);
4468 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4469 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4470 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4471 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4473 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4474 resultcode = RDIV_EXPR;
4475 else
4476 /* When dividing two signed integers, we have to promote to int.
4477 unless we divide by a constant != -1. Note that default
4478 conversion will have been performed on the operands at this
4479 point, so we have to dig out the original type to find out if
4480 it was unsigned. */
4481 shorten = ((TREE_CODE (op0) == NOP_EXPR
4482 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4483 || (TREE_CODE (op1) == INTEGER_CST
4484 && ! integer_all_onesp (op1)));
4486 common = 1;
4488 break;
4490 case BIT_AND_EXPR:
4491 case BIT_IOR_EXPR:
4492 case BIT_XOR_EXPR:
4493 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4494 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4495 && !VECTOR_FLOAT_TYPE_P (type0)
4496 && !VECTOR_FLOAT_TYPE_P (type1)))
4497 shorten = -1;
4498 break;
4500 case TRUNC_MOD_EXPR:
4501 case FLOOR_MOD_EXPR:
4503 tree cop1 = fold_non_dependent_expr (op1);
4504 doing_div_or_mod = true;
4505 warn_for_div_by_zero (location, cop1);
4508 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4509 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4510 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4511 common = 1;
4512 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4514 /* Although it would be tempting to shorten always here, that loses
4515 on some targets, since the modulo instruction is undefined if the
4516 quotient can't be represented in the computation mode. We shorten
4517 only if unsigned or if dividing by something we know != -1. */
4518 shorten = ((TREE_CODE (op0) == NOP_EXPR
4519 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4520 || (TREE_CODE (op1) == INTEGER_CST
4521 && ! integer_all_onesp (op1)));
4522 common = 1;
4524 break;
4526 case TRUTH_ANDIF_EXPR:
4527 case TRUTH_ORIF_EXPR:
4528 case TRUTH_AND_EXPR:
4529 case TRUTH_OR_EXPR:
4530 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4532 if (!COMPARISON_CLASS_P (op1))
4533 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4534 build_zero_cst (type1), complain);
4535 if (code == TRUTH_ANDIF_EXPR)
4537 tree z = build_zero_cst (TREE_TYPE (op1));
4538 return build_conditional_expr (location, op0, op1, z, complain);
4540 else if (code == TRUTH_ORIF_EXPR)
4542 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4543 return build_conditional_expr (location, op0, m1, op1, complain);
4545 else
4546 gcc_unreachable ();
4548 if (VECTOR_TYPE_P (type0))
4550 if (!COMPARISON_CLASS_P (op0))
4551 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4552 build_zero_cst (type0), complain);
4553 if (!VECTOR_TYPE_P (type1))
4555 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4556 tree z = build_zero_cst (TREE_TYPE (op0));
4557 op1 = build_conditional_expr (location, op1, m1, z, complain);
4559 else if (!COMPARISON_CLASS_P (op1))
4560 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4561 build_zero_cst (type1), complain);
4563 if (code == TRUTH_ANDIF_EXPR)
4564 code = BIT_AND_EXPR;
4565 else if (code == TRUTH_ORIF_EXPR)
4566 code = BIT_IOR_EXPR;
4567 else
4568 gcc_unreachable ();
4570 return cp_build_binary_op (location, code, op0, op1, complain);
4573 result_type = boolean_type_node;
4574 break;
4576 /* Shift operations: result has same type as first operand;
4577 always convert second operand to int.
4578 Also set SHORT_SHIFT if shifting rightward. */
4580 case RSHIFT_EXPR:
4581 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4582 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4584 result_type = type0;
4585 converted = 1;
4587 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4588 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4589 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4590 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4591 TYPE_VECTOR_SUBPARTS (type1)))
4593 result_type = type0;
4594 converted = 1;
4596 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4598 tree const_op1 = fold_non_dependent_expr (op1);
4599 if (TREE_CODE (const_op1) != INTEGER_CST)
4600 const_op1 = op1;
4601 result_type = type0;
4602 doing_shift = true;
4603 if (TREE_CODE (const_op1) == INTEGER_CST)
4605 if (tree_int_cst_lt (const_op1, integer_zero_node))
4607 if ((complain & tf_warning)
4608 && c_inhibit_evaluation_warnings == 0)
4609 warning (OPT_Wshift_count_negative,
4610 "right shift count is negative");
4612 else
4614 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4615 && (complain & tf_warning)
4616 && c_inhibit_evaluation_warnings == 0)
4617 warning (OPT_Wshift_count_overflow,
4618 "right shift count >= width of type");
4621 /* Avoid converting op1 to result_type later. */
4622 converted = 1;
4624 break;
4626 case LSHIFT_EXPR:
4627 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4628 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4630 result_type = type0;
4631 converted = 1;
4633 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4634 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4635 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4636 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4637 TYPE_VECTOR_SUBPARTS (type1)))
4639 result_type = type0;
4640 converted = 1;
4642 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4644 tree const_op0 = fold_non_dependent_expr (op0);
4645 if (TREE_CODE (const_op0) != INTEGER_CST)
4646 const_op0 = op0;
4647 tree const_op1 = fold_non_dependent_expr (op1);
4648 if (TREE_CODE (const_op1) != INTEGER_CST)
4649 const_op1 = op1;
4650 result_type = type0;
4651 doing_shift = true;
4652 if (TREE_CODE (const_op0) == INTEGER_CST
4653 && tree_int_cst_sgn (const_op0) < 0
4654 && (complain & tf_warning)
4655 && c_inhibit_evaluation_warnings == 0)
4656 warning (OPT_Wshift_negative_value,
4657 "left shift of negative value");
4658 if (TREE_CODE (const_op1) == INTEGER_CST)
4660 if (tree_int_cst_lt (const_op1, integer_zero_node))
4662 if ((complain & tf_warning)
4663 && c_inhibit_evaluation_warnings == 0)
4664 warning (OPT_Wshift_count_negative,
4665 "left shift count is negative");
4667 else if (compare_tree_int (const_op1,
4668 TYPE_PRECISION (type0)) >= 0)
4670 if ((complain & tf_warning)
4671 && c_inhibit_evaluation_warnings == 0)
4672 warning (OPT_Wshift_count_overflow,
4673 "left shift count >= width of type");
4675 else if (TREE_CODE (const_op0) == INTEGER_CST
4676 && (complain & tf_warning))
4677 maybe_warn_shift_overflow (location, const_op0, const_op1);
4679 /* Avoid converting op1 to result_type later. */
4680 converted = 1;
4682 break;
4684 case RROTATE_EXPR:
4685 case LROTATE_EXPR:
4686 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4688 result_type = type0;
4689 if (TREE_CODE (op1) == INTEGER_CST)
4691 if (tree_int_cst_lt (op1, integer_zero_node))
4693 if (complain & tf_warning)
4694 warning (0, (code == LROTATE_EXPR)
4695 ? G_("left rotate count is negative")
4696 : G_("right rotate count is negative"));
4698 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4700 if (complain & tf_warning)
4701 warning (0, (code == LROTATE_EXPR)
4702 ? G_("left rotate count >= width of type")
4703 : G_("right rotate count >= width of type"));
4706 /* Convert the shift-count to an integer, regardless of
4707 size of value being shifted. */
4708 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4709 op1 = cp_convert (integer_type_node, op1, complain);
4711 break;
4713 case EQ_EXPR:
4714 case NE_EXPR:
4715 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4716 goto vector_compare;
4717 if ((complain & tf_warning)
4718 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4719 warning (OPT_Wfloat_equal,
4720 "comparing floating point with == or != is unsafe");
4721 if ((complain & tf_warning)
4722 && ((TREE_CODE (orig_op0) == STRING_CST
4723 && !integer_zerop (cp_fully_fold (op1)))
4724 || (TREE_CODE (orig_op1) == STRING_CST
4725 && !integer_zerop (cp_fully_fold (op0)))))
4726 warning (OPT_Waddress, "comparison with string literal results "
4727 "in unspecified behavior");
4729 build_type = boolean_type_node;
4730 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4731 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4732 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4733 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4734 short_compare = 1;
4735 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4736 && null_ptr_cst_p (orig_op1))
4737 /* Handle, eg, (void*)0 (c++/43906), and more. */
4738 || (code0 == POINTER_TYPE
4739 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4741 if (TYPE_PTR_P (type1))
4742 result_type = composite_pointer_type (type0, type1, op0, op1,
4743 CPO_COMPARISON, complain);
4744 else
4745 result_type = type0;
4747 if (char_type_p (TREE_TYPE (orig_op1))
4748 && warning (OPT_Wpointer_compare,
4749 "comparison between pointer and zero character "
4750 "constant"))
4751 inform (input_location,
4752 "did you mean to dereference the pointer?");
4753 warn_for_null_address (location, op0, complain);
4755 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4756 && null_ptr_cst_p (orig_op0))
4757 /* Handle, eg, (void*)0 (c++/43906), and more. */
4758 || (code1 == POINTER_TYPE
4759 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4761 if (TYPE_PTR_P (type0))
4762 result_type = composite_pointer_type (type0, type1, op0, op1,
4763 CPO_COMPARISON, complain);
4764 else
4765 result_type = type1;
4767 if (char_type_p (TREE_TYPE (orig_op0))
4768 && warning (OPT_Wpointer_compare,
4769 "comparison between pointer and zero character "
4770 "constant"))
4771 inform (input_location,
4772 "did you mean to dereference the pointer?");
4773 warn_for_null_address (location, op1, complain);
4775 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4776 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4777 result_type = composite_pointer_type (type0, type1, op0, op1,
4778 CPO_COMPARISON, complain);
4779 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4780 /* One of the operands must be of nullptr_t type. */
4781 result_type = TREE_TYPE (nullptr_node);
4782 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4784 result_type = type0;
4785 if (complain & tf_error)
4786 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4787 else
4788 return error_mark_node;
4790 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4792 result_type = type1;
4793 if (complain & tf_error)
4794 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4795 else
4796 return error_mark_node;
4798 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4800 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4801 == ptrmemfunc_vbit_in_delta)
4803 tree pfn0, delta0, e1, e2;
4805 if (TREE_SIDE_EFFECTS (op0))
4806 op0 = save_expr (op0);
4808 pfn0 = pfn_from_ptrmemfunc (op0);
4809 delta0 = delta_from_ptrmemfunc (op0);
4810 e1 = cp_build_binary_op (location,
4811 EQ_EXPR,
4812 pfn0,
4813 build_zero_cst (TREE_TYPE (pfn0)),
4814 complain);
4815 e2 = cp_build_binary_op (location,
4816 BIT_AND_EXPR,
4817 delta0,
4818 integer_one_node,
4819 complain);
4821 if (complain & tf_warning)
4822 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4824 e2 = cp_build_binary_op (location,
4825 EQ_EXPR, e2, integer_zero_node,
4826 complain);
4827 op0 = cp_build_binary_op (location,
4828 TRUTH_ANDIF_EXPR, e1, e2,
4829 complain);
4830 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4832 else
4834 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4835 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4837 result_type = TREE_TYPE (op0);
4839 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4840 return cp_build_binary_op (location, code, op1, op0, complain);
4841 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4843 tree type;
4844 /* E will be the final comparison. */
4845 tree e;
4846 /* E1 and E2 are for scratch. */
4847 tree e1;
4848 tree e2;
4849 tree pfn0;
4850 tree pfn1;
4851 tree delta0;
4852 tree delta1;
4854 type = composite_pointer_type (type0, type1, op0, op1,
4855 CPO_COMPARISON, complain);
4857 if (!same_type_p (TREE_TYPE (op0), type))
4858 op0 = cp_convert_and_check (type, op0, complain);
4859 if (!same_type_p (TREE_TYPE (op1), type))
4860 op1 = cp_convert_and_check (type, op1, complain);
4862 if (op0 == error_mark_node || op1 == error_mark_node)
4863 return error_mark_node;
4865 if (TREE_SIDE_EFFECTS (op0))
4866 op0 = save_expr (op0);
4867 if (TREE_SIDE_EFFECTS (op1))
4868 op1 = save_expr (op1);
4870 pfn0 = pfn_from_ptrmemfunc (op0);
4871 pfn0 = cp_fully_fold (pfn0);
4872 /* Avoid -Waddress warnings (c++/64877). */
4873 if (TREE_CODE (pfn0) == ADDR_EXPR)
4874 TREE_NO_WARNING (pfn0) = 1;
4875 pfn1 = pfn_from_ptrmemfunc (op1);
4876 pfn1 = cp_fully_fold (pfn1);
4877 delta0 = delta_from_ptrmemfunc (op0);
4878 delta1 = delta_from_ptrmemfunc (op1);
4879 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4880 == ptrmemfunc_vbit_in_delta)
4882 /* We generate:
4884 (op0.pfn == op1.pfn
4885 && ((op0.delta == op1.delta)
4886 || (!op0.pfn && op0.delta & 1 == 0
4887 && op1.delta & 1 == 0))
4889 The reason for the `!op0.pfn' bit is that a NULL
4890 pointer-to-member is any member with a zero PFN and
4891 LSB of the DELTA field is 0. */
4893 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4894 delta0,
4895 integer_one_node,
4896 complain);
4897 e1 = cp_build_binary_op (location,
4898 EQ_EXPR, e1, integer_zero_node,
4899 complain);
4900 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4901 delta1,
4902 integer_one_node,
4903 complain);
4904 e2 = cp_build_binary_op (location,
4905 EQ_EXPR, e2, integer_zero_node,
4906 complain);
4907 e1 = cp_build_binary_op (location,
4908 TRUTH_ANDIF_EXPR, e2, e1,
4909 complain);
4910 e2 = cp_build_binary_op (location, EQ_EXPR,
4911 pfn0,
4912 build_zero_cst (TREE_TYPE (pfn0)),
4913 complain);
4914 e2 = cp_build_binary_op (location,
4915 TRUTH_ANDIF_EXPR, e2, e1, complain);
4916 e1 = cp_build_binary_op (location,
4917 EQ_EXPR, delta0, delta1, complain);
4918 e1 = cp_build_binary_op (location,
4919 TRUTH_ORIF_EXPR, e1, e2, complain);
4921 else
4923 /* We generate:
4925 (op0.pfn == op1.pfn
4926 && (!op0.pfn || op0.delta == op1.delta))
4928 The reason for the `!op0.pfn' bit is that a NULL
4929 pointer-to-member is any member with a zero PFN; the
4930 DELTA field is unspecified. */
4932 e1 = cp_build_binary_op (location,
4933 EQ_EXPR, delta0, delta1, complain);
4934 e2 = cp_build_binary_op (location,
4935 EQ_EXPR,
4936 pfn0,
4937 build_zero_cst (TREE_TYPE (pfn0)),
4938 complain);
4939 e1 = cp_build_binary_op (location,
4940 TRUTH_ORIF_EXPR, e1, e2, complain);
4942 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4943 e = cp_build_binary_op (location,
4944 TRUTH_ANDIF_EXPR, e2, e1, complain);
4945 if (code == EQ_EXPR)
4946 return e;
4947 return cp_build_binary_op (location,
4948 EQ_EXPR, e, integer_zero_node, complain);
4950 else
4952 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4953 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4954 type1));
4955 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4956 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4957 type0));
4960 break;
4962 case MAX_EXPR:
4963 case MIN_EXPR:
4964 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4965 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4966 shorten = 1;
4967 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4968 result_type = composite_pointer_type (type0, type1, op0, op1,
4969 CPO_COMPARISON, complain);
4970 break;
4972 case LE_EXPR:
4973 case GE_EXPR:
4974 case LT_EXPR:
4975 case GT_EXPR:
4976 if (TREE_CODE (orig_op0) == STRING_CST
4977 || TREE_CODE (orig_op1) == STRING_CST)
4979 if (complain & tf_warning)
4980 warning (OPT_Waddress, "comparison with string literal results "
4981 "in unspecified behavior");
4984 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4986 vector_compare:
4987 tree intt;
4988 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4989 TREE_TYPE (type1))
4990 && !vector_types_compatible_elements_p (type0, type1))
4992 if (complain & tf_error)
4994 error_at (location, "comparing vectors with different "
4995 "element types");
4996 inform (location, "operand types are %qT and %qT",
4997 type0, type1);
4999 return error_mark_node;
5002 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5003 TYPE_VECTOR_SUBPARTS (type1)))
5005 if (complain & tf_error)
5007 error_at (location, "comparing vectors with different "
5008 "number of elements");
5009 inform (location, "operand types are %qT and %qT",
5010 type0, type1);
5012 return error_mark_node;
5015 /* It's not precisely specified how the usual arithmetic
5016 conversions apply to the vector types. Here, we use
5017 the unsigned type if one of the operands is signed and
5018 the other one is unsigned. */
5019 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5021 if (!TYPE_UNSIGNED (type0))
5022 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5023 else
5024 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5025 warning_at (location, OPT_Wsign_compare, "comparison between "
5026 "types %qT and %qT", type0, type1);
5029 /* Always construct signed integer vector type. */
5030 intt = c_common_type_for_size
5031 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5032 if (!intt)
5034 if (complain & tf_error)
5035 error_at (location, "could not find an integer type "
5036 "of the same size as %qT", TREE_TYPE (type0));
5037 return error_mark_node;
5039 result_type = build_opaque_vector_type (intt,
5040 TYPE_VECTOR_SUBPARTS (type0));
5041 converted = 1;
5042 return build_vec_cmp (resultcode, result_type, op0, op1);
5044 build_type = boolean_type_node;
5045 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5046 || code0 == ENUMERAL_TYPE)
5047 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5048 || code1 == ENUMERAL_TYPE))
5049 short_compare = 1;
5050 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5051 result_type = composite_pointer_type (type0, type1, op0, op1,
5052 CPO_COMPARISON, complain);
5053 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5055 result_type = type0;
5056 if (extra_warnings && (complain & tf_warning))
5057 warning (OPT_Wextra,
5058 "ordered comparison of pointer with integer zero");
5060 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5062 result_type = type1;
5063 if (extra_warnings && (complain & tf_warning))
5064 warning (OPT_Wextra,
5065 "ordered comparison of pointer with integer zero");
5067 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5068 /* One of the operands must be of nullptr_t type. */
5069 result_type = TREE_TYPE (nullptr_node);
5070 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5072 result_type = type0;
5073 if (complain & tf_error)
5074 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5075 else
5076 return error_mark_node;
5078 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5080 result_type = type1;
5081 if (complain & tf_error)
5082 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5083 else
5084 return error_mark_node;
5087 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5088 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5090 op0 = save_expr (op0);
5091 op1 = save_expr (op1);
5093 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5094 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5097 break;
5099 case UNORDERED_EXPR:
5100 case ORDERED_EXPR:
5101 case UNLT_EXPR:
5102 case UNLE_EXPR:
5103 case UNGT_EXPR:
5104 case UNGE_EXPR:
5105 case UNEQ_EXPR:
5106 build_type = integer_type_node;
5107 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5109 if (complain & tf_error)
5110 error ("unordered comparison on non-floating point argument");
5111 return error_mark_node;
5113 common = 1;
5114 break;
5116 default:
5117 break;
5120 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5121 || code0 == ENUMERAL_TYPE)
5122 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5123 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5124 arithmetic_types_p = 1;
5125 else
5127 arithmetic_types_p = 0;
5128 /* Vector arithmetic is only allowed when both sides are vectors. */
5129 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5131 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5132 || !vector_types_compatible_elements_p (type0, type1))
5134 if (complain & tf_error)
5136 /* "location" already embeds the locations of the
5137 operands, so we don't need to add them separately
5138 to richloc. */
5139 rich_location richloc (line_table, location);
5140 binary_op_error (&richloc, code, type0, type1);
5142 return error_mark_node;
5144 arithmetic_types_p = 1;
5147 /* Determine the RESULT_TYPE, if it is not already known. */
5148 if (!result_type
5149 && arithmetic_types_p
5150 && (shorten || common || short_compare))
5152 result_type = cp_common_type (type0, type1);
5153 if (complain & tf_warning)
5154 do_warn_double_promotion (result_type, type0, type1,
5155 "implicit conversion from %qH to %qI "
5156 "to match other operand of binary "
5157 "expression",
5158 location);
5161 if (!result_type)
5163 if (complain & tf_error)
5164 error_at (location,
5165 "invalid operands of types %qT and %qT to binary %qO",
5166 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5167 return error_mark_node;
5170 /* If we're in a template, the only thing we need to know is the
5171 RESULT_TYPE. */
5172 if (processing_template_decl)
5174 /* Since the middle-end checks the type when doing a build2, we
5175 need to build the tree in pieces. This built tree will never
5176 get out of the front-end as we replace it when instantiating
5177 the template. */
5178 tree tmp = build2 (resultcode,
5179 build_type ? build_type : result_type,
5180 NULL_TREE, op1);
5181 TREE_OPERAND (tmp, 0) = op0;
5182 return tmp;
5185 /* Remember the original type; RESULT_TYPE might be changed later on
5186 by shorten_binary_op. */
5187 tree orig_type = result_type;
5189 if (arithmetic_types_p)
5191 bool first_complex = (code0 == COMPLEX_TYPE);
5192 bool second_complex = (code1 == COMPLEX_TYPE);
5193 int none_complex = (!first_complex && !second_complex);
5195 /* Adapted from patch for c/24581. */
5196 if (first_complex != second_complex
5197 && (code == PLUS_EXPR
5198 || code == MINUS_EXPR
5199 || code == MULT_EXPR
5200 || (code == TRUNC_DIV_EXPR && first_complex))
5201 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5202 && flag_signed_zeros)
5204 /* An operation on mixed real/complex operands must be
5205 handled specially, but the language-independent code can
5206 more easily optimize the plain complex arithmetic if
5207 -fno-signed-zeros. */
5208 tree real_type = TREE_TYPE (result_type);
5209 tree real, imag;
5210 if (first_complex)
5212 if (TREE_TYPE (op0) != result_type)
5213 op0 = cp_convert_and_check (result_type, op0, complain);
5214 if (TREE_TYPE (op1) != real_type)
5215 op1 = cp_convert_and_check (real_type, op1, complain);
5217 else
5219 if (TREE_TYPE (op0) != real_type)
5220 op0 = cp_convert_and_check (real_type, op0, complain);
5221 if (TREE_TYPE (op1) != result_type)
5222 op1 = cp_convert_and_check (result_type, op1, complain);
5224 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5225 return error_mark_node;
5226 if (first_complex)
5228 op0 = save_expr (op0);
5229 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5230 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5231 switch (code)
5233 case MULT_EXPR:
5234 case TRUNC_DIV_EXPR:
5235 op1 = save_expr (op1);
5236 imag = build2 (resultcode, real_type, imag, op1);
5237 /* Fall through. */
5238 case PLUS_EXPR:
5239 case MINUS_EXPR:
5240 real = build2 (resultcode, real_type, real, op1);
5241 break;
5242 default:
5243 gcc_unreachable();
5246 else
5248 op1 = save_expr (op1);
5249 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5250 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5251 switch (code)
5253 case MULT_EXPR:
5254 op0 = save_expr (op0);
5255 imag = build2 (resultcode, real_type, op0, imag);
5256 /* Fall through. */
5257 case PLUS_EXPR:
5258 real = build2 (resultcode, real_type, op0, real);
5259 break;
5260 case MINUS_EXPR:
5261 real = build2 (resultcode, real_type, op0, real);
5262 imag = build1 (NEGATE_EXPR, real_type, imag);
5263 break;
5264 default:
5265 gcc_unreachable();
5268 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5269 return result;
5272 /* For certain operations (which identify themselves by shorten != 0)
5273 if both args were extended from the same smaller type,
5274 do the arithmetic in that type and then extend.
5276 shorten !=0 and !=1 indicates a bitwise operation.
5277 For them, this optimization is safe only if
5278 both args are zero-extended or both are sign-extended.
5279 Otherwise, we might change the result.
5280 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5281 but calculated in (unsigned short) it would be (unsigned short)-1. */
5283 if (shorten && none_complex)
5285 final_type = result_type;
5286 result_type = shorten_binary_op (result_type, op0, op1,
5287 shorten == -1);
5290 /* Comparison operations are shortened too but differently.
5291 They identify themselves by setting short_compare = 1. */
5293 if (short_compare)
5295 /* We call shorten_compare only for diagnostic-reason. */
5296 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5297 xresult_type = result_type;
5298 enum tree_code xresultcode = resultcode;
5299 shorten_compare (location, &xop0, &xop1, &xresult_type,
5300 &xresultcode);
5303 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5304 && warn_sign_compare
5305 /* Do not warn until the template is instantiated; we cannot
5306 bound the ranges of the arguments until that point. */
5307 && !processing_template_decl
5308 && (complain & tf_warning)
5309 && c_inhibit_evaluation_warnings == 0
5310 /* Even unsigned enum types promote to signed int. We don't
5311 want to issue -Wsign-compare warnings for this case. */
5312 && !enum_cast_to_int (orig_op0)
5313 && !enum_cast_to_int (orig_op1))
5315 tree oop0 = maybe_constant_value (orig_op0);
5316 tree oop1 = maybe_constant_value (orig_op1);
5318 if (TREE_CODE (oop0) != INTEGER_CST)
5319 oop0 = cp_fully_fold (orig_op0);
5320 if (TREE_CODE (oop1) != INTEGER_CST)
5321 oop1 = cp_fully_fold (orig_op1);
5322 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5323 result_type, resultcode);
5327 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5328 Then the expression will be built.
5329 It will be given type FINAL_TYPE if that is nonzero;
5330 otherwise, it will be given type RESULT_TYPE. */
5331 if (! converted)
5333 if (TREE_TYPE (op0) != result_type)
5334 op0 = cp_convert_and_check (result_type, op0, complain);
5335 if (TREE_TYPE (op1) != result_type)
5336 op1 = cp_convert_and_check (result_type, op1, complain);
5338 if (op0 == error_mark_node || op1 == error_mark_node)
5339 return error_mark_node;
5342 if (build_type == NULL_TREE)
5343 build_type = result_type;
5345 if (sanitize_flags_p ((SANITIZE_SHIFT
5346 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5347 && current_function_decl != NULL_TREE
5348 && !processing_template_decl
5349 && (doing_div_or_mod || doing_shift))
5351 /* OP0 and/or OP1 might have side-effects. */
5352 op0 = cp_save_expr (op0);
5353 op1 = cp_save_expr (op1);
5354 op0 = fold_non_dependent_expr (op0);
5355 op1 = fold_non_dependent_expr (op1);
5356 if (doing_div_or_mod
5357 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5359 /* For diagnostics we want to use the promoted types without
5360 shorten_binary_op. So convert the arguments to the
5361 original result_type. */
5362 tree cop0 = op0;
5363 tree cop1 = op1;
5364 if (TREE_TYPE (cop0) != orig_type)
5365 cop0 = cp_convert (orig_type, op0, complain);
5366 if (TREE_TYPE (cop1) != orig_type)
5367 cop1 = cp_convert (orig_type, op1, complain);
5368 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5370 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5371 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5374 result = build2_loc (location, resultcode, build_type, op0, op1);
5375 if (final_type != 0)
5376 result = cp_convert (final_type, result, complain);
5378 if (instrument_expr != NULL)
5379 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5380 instrument_expr, result);
5382 if (!processing_template_decl)
5384 op0 = cp_fully_fold (op0);
5385 /* Only consider the second argument if the first isn't overflowed. */
5386 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5387 return result;
5388 op1 = cp_fully_fold (op1);
5389 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5390 return result;
5392 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5393 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5394 return result;
5396 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5397 if (TREE_OVERFLOW_P (result_ovl))
5398 overflow_warning (location, result_ovl);
5400 return result;
5403 /* Build a VEC_PERM_EXPR.
5404 This is a simple wrapper for c_build_vec_perm_expr. */
5405 tree
5406 build_x_vec_perm_expr (location_t loc,
5407 tree arg0, tree arg1, tree arg2,
5408 tsubst_flags_t complain)
5410 tree orig_arg0 = arg0;
5411 tree orig_arg1 = arg1;
5412 tree orig_arg2 = arg2;
5413 if (processing_template_decl)
5415 if (type_dependent_expression_p (arg0)
5416 || type_dependent_expression_p (arg1)
5417 || type_dependent_expression_p (arg2))
5418 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5419 arg0 = build_non_dependent_expr (arg0);
5420 if (arg1)
5421 arg1 = build_non_dependent_expr (arg1);
5422 arg2 = build_non_dependent_expr (arg2);
5424 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5425 if (processing_template_decl && exp != error_mark_node)
5426 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5427 orig_arg1, orig_arg2);
5428 return exp;
5431 /* Return a tree for the sum or difference (RESULTCODE says which)
5432 of pointer PTROP and integer INTOP. */
5434 static tree
5435 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5436 tree intop, tsubst_flags_t complain)
5438 tree res_type = TREE_TYPE (ptrop);
5440 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5441 in certain circumstance (when it's valid to do so). So we need
5442 to make sure it's complete. We don't need to check here, if we
5443 can actually complete it at all, as those checks will be done in
5444 pointer_int_sum() anyway. */
5445 complete_type (TREE_TYPE (res_type));
5447 return pointer_int_sum (loc, resultcode, ptrop,
5448 intop, complain & tf_warning_or_error);
5451 /* Return a tree for the difference of pointers OP0 and OP1.
5452 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5453 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5455 static tree
5456 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5457 tsubst_flags_t complain, tree *instrument_expr)
5459 tree result, inttype;
5460 tree restype = ptrdiff_type_node;
5461 tree target_type = TREE_TYPE (ptrtype);
5463 if (!complete_type_or_else (target_type, NULL_TREE))
5464 return error_mark_node;
5466 if (VOID_TYPE_P (target_type))
5468 if (complain & tf_error)
5469 permerror (loc, "ISO C++ forbids using pointer of "
5470 "type %<void *%> in subtraction");
5471 else
5472 return error_mark_node;
5474 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5476 if (complain & tf_error)
5477 permerror (loc, "ISO C++ forbids using pointer to "
5478 "a function in subtraction");
5479 else
5480 return error_mark_node;
5482 if (TREE_CODE (target_type) == METHOD_TYPE)
5484 if (complain & tf_error)
5485 permerror (loc, "ISO C++ forbids using pointer to "
5486 "a method in subtraction");
5487 else
5488 return error_mark_node;
5491 /* Determine integer type result of the subtraction. This will usually
5492 be the same as the result type (ptrdiff_t), but may need to be a wider
5493 type if pointers for the address space are wider than ptrdiff_t. */
5494 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5495 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5496 else
5497 inttype = restype;
5499 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5501 op0 = save_expr (op0);
5502 op1 = save_expr (op1);
5504 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5505 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5508 /* First do the subtraction, then build the divide operator
5509 and only convert at the very end.
5510 Do not do default conversions in case restype is a short type. */
5512 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5513 pointers. If some platform cannot provide that, or has a larger
5514 ptrdiff_type to support differences larger than half the address
5515 space, cast the pointers to some larger integer type and do the
5516 computations in that type. */
5517 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5518 op0 = cp_build_binary_op (loc,
5519 MINUS_EXPR,
5520 cp_convert (inttype, op0, complain),
5521 cp_convert (inttype, op1, complain),
5522 complain);
5523 else
5524 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5526 /* This generates an error if op1 is a pointer to an incomplete type. */
5527 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5529 if (complain & tf_error)
5530 error_at (loc, "invalid use of a pointer to an incomplete type in "
5531 "pointer arithmetic");
5532 else
5533 return error_mark_node;
5536 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5538 if (complain & tf_error)
5539 error_at (loc, "arithmetic on pointer to an empty aggregate");
5540 else
5541 return error_mark_node;
5544 op1 = (TYPE_PTROB_P (ptrtype)
5545 ? size_in_bytes_loc (loc, target_type)
5546 : integer_one_node);
5548 /* Do the division. */
5550 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5551 cp_convert (inttype, op1, complain));
5552 return cp_convert (restype, result, complain);
5555 /* Construct and perhaps optimize a tree representation
5556 for a unary operation. CODE, a tree_code, specifies the operation
5557 and XARG is the operand. */
5559 tree
5560 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5561 tsubst_flags_t complain)
5563 tree orig_expr = xarg;
5564 tree exp;
5565 int ptrmem = 0;
5566 tree overload = NULL_TREE;
5568 if (processing_template_decl)
5570 if (type_dependent_expression_p (xarg))
5571 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5573 xarg = build_non_dependent_expr (xarg);
5576 exp = NULL_TREE;
5578 /* [expr.unary.op] says:
5580 The address of an object of incomplete type can be taken.
5582 (And is just the ordinary address operator, not an overloaded
5583 "operator &".) However, if the type is a template
5584 specialization, we must complete the type at this point so that
5585 an overloaded "operator &" will be available if required. */
5586 if (code == ADDR_EXPR
5587 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5588 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5589 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5590 || (TREE_CODE (xarg) == OFFSET_REF)))
5591 /* Don't look for a function. */;
5592 else
5593 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5594 NULL_TREE, &overload, complain);
5596 if (!exp && code == ADDR_EXPR)
5598 if (is_overloaded_fn (xarg))
5600 tree fn = get_first_fn (xarg);
5601 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5603 if (complain & tf_error)
5604 error (DECL_CONSTRUCTOR_P (fn)
5605 ? G_("taking address of constructor %qD")
5606 : G_("taking address of destructor %qD"),
5607 fn);
5608 return error_mark_node;
5612 /* A pointer to member-function can be formed only by saying
5613 &X::mf. */
5614 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5615 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5617 if (TREE_CODE (xarg) != OFFSET_REF
5618 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5620 if (complain & tf_error)
5622 error ("invalid use of %qE to form a "
5623 "pointer-to-member-function", xarg.get_value ());
5624 if (TREE_CODE (xarg) != OFFSET_REF)
5625 inform (input_location, " a qualified-id is required");
5627 return error_mark_node;
5629 else
5631 if (complain & tf_error)
5632 error ("parentheses around %qE cannot be used to form a"
5633 " pointer-to-member-function",
5634 xarg.get_value ());
5635 else
5636 return error_mark_node;
5637 PTRMEM_OK_P (xarg) = 1;
5641 if (TREE_CODE (xarg) == OFFSET_REF)
5643 ptrmem = PTRMEM_OK_P (xarg);
5645 if (!ptrmem && !flag_ms_extensions
5646 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5648 /* A single non-static member, make sure we don't allow a
5649 pointer-to-member. */
5650 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5651 TREE_OPERAND (xarg, 0),
5652 ovl_make (TREE_OPERAND (xarg, 1)));
5653 PTRMEM_OK_P (xarg) = ptrmem;
5657 exp = cp_build_addr_expr_strict (xarg, complain);
5660 if (processing_template_decl && exp != error_mark_node)
5662 if (overload != NULL_TREE)
5663 return (build_min_non_dep_op_overload
5664 (code, exp, overload, orig_expr, integer_zero_node));
5666 exp = build_min_non_dep (code, exp, orig_expr,
5667 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5669 if (TREE_CODE (exp) == ADDR_EXPR)
5670 PTRMEM_OK_P (exp) = ptrmem;
5671 return exp;
5674 /* Construct and perhaps optimize a tree representation
5675 for __builtin_addressof operation. ARG specifies the operand. */
5677 tree
5678 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5680 tree orig_expr = arg;
5682 if (processing_template_decl)
5684 if (type_dependent_expression_p (arg))
5685 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5687 arg = build_non_dependent_expr (arg);
5690 tree exp = cp_build_addr_expr_strict (arg, complain);
5692 if (processing_template_decl && exp != error_mark_node)
5693 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5694 return exp;
5697 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5698 constants, where a null value is represented by an INTEGER_CST of
5699 -1. */
5701 tree
5702 cp_truthvalue_conversion (tree expr)
5704 tree type = TREE_TYPE (expr);
5705 if (TYPE_PTR_OR_PTRMEM_P (type)
5706 /* Avoid ICE on invalid use of non-static member function. */
5707 || TREE_CODE (expr) == FUNCTION_DECL)
5708 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5709 else
5710 return c_common_truthvalue_conversion (input_location, expr);
5713 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5715 tree
5716 condition_conversion (tree expr)
5718 tree t;
5719 /* Anything that might happen in a template should go through
5720 maybe_convert_cond. */
5721 gcc_assert (!processing_template_decl);
5722 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5723 tf_warning_or_error, LOOKUP_NORMAL);
5724 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5725 return t;
5728 /* Returns the address of T. This function will fold away
5729 ADDR_EXPR of INDIRECT_REF. */
5731 tree
5732 build_address (tree t)
5734 if (error_operand_p (t) || !cxx_mark_addressable (t))
5735 return error_mark_node;
5736 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5737 t = build_fold_addr_expr (t);
5738 if (TREE_CODE (t) != ADDR_EXPR)
5739 t = rvalue (t);
5740 return t;
5743 /* Return a NOP_EXPR converting EXPR to TYPE. */
5745 tree
5746 build_nop (tree type, tree expr)
5748 if (type == error_mark_node || error_operand_p (expr))
5749 return expr;
5750 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5753 /* Take the address of ARG, whatever that means under C++ semantics.
5754 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5755 and class rvalues as well.
5757 Nothing should call this function directly; instead, callers should use
5758 cp_build_addr_expr or cp_build_addr_expr_strict. */
5760 static tree
5761 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5763 tree argtype;
5764 tree val;
5766 if (!arg || error_operand_p (arg))
5767 return error_mark_node;
5769 arg = mark_lvalue_use (arg);
5770 if (error_operand_p (arg))
5771 return error_mark_node;
5773 argtype = lvalue_type (arg);
5775 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5777 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5778 && !really_overloaded_fn (arg))
5780 /* They're trying to take the address of a unique non-static
5781 member function. This is ill-formed (except in MS-land),
5782 but let's try to DTRT.
5783 Note: We only handle unique functions here because we don't
5784 want to complain if there's a static overload; non-unique
5785 cases will be handled by instantiate_type. But we need to
5786 handle this case here to allow casts on the resulting PMF.
5787 We could defer this in non-MS mode, but it's easier to give
5788 a useful error here. */
5790 /* Inside constant member functions, the `this' pointer
5791 contains an extra const qualifier. TYPE_MAIN_VARIANT
5792 is used here to remove this const from the diagnostics
5793 and the created OFFSET_REF. */
5794 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5795 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5796 if (!mark_used (fn, complain) && !(complain & tf_error))
5797 return error_mark_node;
5799 if (! flag_ms_extensions)
5801 tree name = DECL_NAME (fn);
5802 if (!(complain & tf_error))
5803 return error_mark_node;
5804 else if (current_class_type
5805 && TREE_OPERAND (arg, 0) == current_class_ref)
5806 /* An expression like &memfn. */
5807 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5808 " or parenthesized non-static member function to form"
5809 " a pointer to member function. Say %<&%T::%D%>",
5810 base, name);
5811 else
5812 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5813 " function to form a pointer to member function."
5814 " Say %<&%T::%D%>",
5815 base, name);
5817 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5820 /* Uninstantiated types are all functions. Taking the
5821 address of a function is a no-op, so just return the
5822 argument. */
5823 if (type_unknown_p (arg))
5824 return build1 (ADDR_EXPR, unknown_type_node, arg);
5826 if (TREE_CODE (arg) == OFFSET_REF)
5827 /* We want a pointer to member; bypass all the code for actually taking
5828 the address of something. */
5829 goto offset_ref;
5831 /* Anything not already handled and not a true memory reference
5832 is an error. */
5833 if (TREE_CODE (argtype) != FUNCTION_TYPE
5834 && TREE_CODE (argtype) != METHOD_TYPE)
5836 cp_lvalue_kind kind = lvalue_kind (arg);
5837 if (kind == clk_none)
5839 if (complain & tf_error)
5840 lvalue_error (input_location, lv_addressof);
5841 return error_mark_node;
5843 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5845 if (!(complain & tf_error))
5846 return error_mark_node;
5847 if (kind & clk_class)
5848 /* Make this a permerror because we used to accept it. */
5849 permerror (input_location, "taking address of temporary");
5850 else
5851 error ("taking address of xvalue (rvalue reference)");
5855 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5857 tree type = build_pointer_type (TREE_TYPE (argtype));
5858 arg = build1 (CONVERT_EXPR, type, arg);
5859 return arg;
5861 else if (pedantic && DECL_MAIN_P (arg))
5863 /* ARM $3.4 */
5864 /* Apparently a lot of autoconf scripts for C++ packages do this,
5865 so only complain if -Wpedantic. */
5866 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5867 pedwarn (input_location, OPT_Wpedantic,
5868 "ISO C++ forbids taking address of function %<::main%>");
5869 else if (flag_pedantic_errors)
5870 return error_mark_node;
5873 /* Let &* cancel out to simplify resulting code. */
5874 if (INDIRECT_REF_P (arg))
5876 arg = TREE_OPERAND (arg, 0);
5877 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5879 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5880 arg = build1 (CONVERT_EXPR, type, arg);
5882 else
5883 /* Don't let this be an lvalue. */
5884 arg = rvalue (arg);
5885 return arg;
5888 /* ??? Cope with user tricks that amount to offsetof. */
5889 if (TREE_CODE (argtype) != FUNCTION_TYPE
5890 && TREE_CODE (argtype) != METHOD_TYPE
5891 && argtype != unknown_type_node
5892 && (val = get_base_address (arg))
5893 && COMPLETE_TYPE_P (TREE_TYPE (val))
5894 && INDIRECT_REF_P (val)
5895 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5897 tree type = build_pointer_type (argtype);
5898 return fold_convert (type, fold_offsetof_1 (arg));
5901 /* Handle complex lvalues (when permitted)
5902 by reduction to simpler cases. */
5903 val = unary_complex_lvalue (ADDR_EXPR, arg);
5904 if (val != 0)
5905 return val;
5907 switch (TREE_CODE (arg))
5909 CASE_CONVERT:
5910 case FLOAT_EXPR:
5911 case FIX_TRUNC_EXPR:
5912 /* We should have handled this above in the lvalue_kind check. */
5913 gcc_unreachable ();
5914 break;
5916 case BASELINK:
5917 arg = BASELINK_FUNCTIONS (arg);
5918 /* Fall through. */
5920 case OVERLOAD:
5921 arg = OVL_FIRST (arg);
5922 break;
5924 case OFFSET_REF:
5925 offset_ref:
5926 /* Turn a reference to a non-static data member into a
5927 pointer-to-member. */
5929 tree type;
5930 tree t;
5932 gcc_assert (PTRMEM_OK_P (arg));
5934 t = TREE_OPERAND (arg, 1);
5935 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5937 if (complain & tf_error)
5938 error ("cannot create pointer to reference member %qD", t);
5939 return error_mark_node;
5942 type = build_ptrmem_type (context_for_name_lookup (t),
5943 TREE_TYPE (t));
5944 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5945 return t;
5948 default:
5949 break;
5952 if (argtype != error_mark_node)
5953 argtype = build_pointer_type (argtype);
5955 if (bitfield_p (arg))
5957 if (complain & tf_error)
5958 error ("attempt to take address of bit-field");
5959 return error_mark_node;
5962 /* In a template, we are processing a non-dependent expression
5963 so we can just form an ADDR_EXPR with the correct type. */
5964 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5966 val = build_address (arg);
5967 if (TREE_CODE (arg) == OFFSET_REF)
5968 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5970 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5972 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5974 /* We can only get here with a single static member
5975 function. */
5976 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5977 && DECL_STATIC_FUNCTION_P (fn));
5978 if (!mark_used (fn, complain) && !(complain & tf_error))
5979 return error_mark_node;
5980 val = build_address (fn);
5981 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5982 /* Do not lose object's side effects. */
5983 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5984 TREE_OPERAND (arg, 0), val);
5986 else
5988 tree object = TREE_OPERAND (arg, 0);
5989 tree field = TREE_OPERAND (arg, 1);
5990 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5991 (TREE_TYPE (object), decl_type_context (field)));
5992 val = build_address (arg);
5995 if (TYPE_PTR_P (argtype)
5996 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5998 build_ptrmemfunc_type (argtype);
5999 val = build_ptrmemfunc (argtype, val, 0,
6000 /*c_cast_p=*/false,
6001 complain);
6004 return val;
6007 /* Take the address of ARG if it has one, even if it's an rvalue. */
6009 tree
6010 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6012 return cp_build_addr_expr_1 (arg, 0, complain);
6015 /* Take the address of ARG, but only if it's an lvalue. */
6017 static tree
6018 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6020 return cp_build_addr_expr_1 (arg, 1, complain);
6023 /* C++: Must handle pointers to members.
6025 Perhaps type instantiation should be extended to handle conversion
6026 from aggregates to types we don't yet know we want? (Or are those
6027 cases typically errors which should be reported?)
6029 NOCONVERT suppresses the default promotions (such as from short to int). */
6031 tree
6032 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6033 tsubst_flags_t complain)
6035 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6036 tree arg = xarg;
6037 location_t location = EXPR_LOC_OR_LOC (arg, input_location);
6038 tree argtype = 0;
6039 const char *errstring = NULL;
6040 tree val;
6041 const char *invalid_op_diag;
6043 if (!arg || error_operand_p (arg))
6044 return error_mark_node;
6046 if ((invalid_op_diag
6047 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6048 ? CONVERT_EXPR
6049 : code),
6050 TREE_TYPE (xarg))))
6052 if (complain & tf_error)
6053 error (invalid_op_diag);
6054 return error_mark_node;
6057 switch (code)
6059 case UNARY_PLUS_EXPR:
6060 case NEGATE_EXPR:
6062 int flags = WANT_ARITH | WANT_ENUM;
6063 /* Unary plus (but not unary minus) is allowed on pointers. */
6064 if (code == UNARY_PLUS_EXPR)
6065 flags |= WANT_POINTER;
6066 arg = build_expr_type_conversion (flags, arg, true);
6067 if (!arg)
6068 errstring = (code == NEGATE_EXPR
6069 ? _("wrong type argument to unary minus")
6070 : _("wrong type argument to unary plus"));
6071 else
6073 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6074 arg = cp_perform_integral_promotions (arg, complain);
6076 /* Make sure the result is not an lvalue: a unary plus or minus
6077 expression is always a rvalue. */
6078 arg = rvalue (arg);
6081 break;
6083 case BIT_NOT_EXPR:
6084 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6086 code = CONJ_EXPR;
6087 if (!noconvert)
6089 arg = cp_default_conversion (arg, complain);
6090 if (arg == error_mark_node)
6091 return error_mark_node;
6094 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6095 | WANT_VECTOR_OR_COMPLEX,
6096 arg, true)))
6097 errstring = _("wrong type argument to bit-complement");
6098 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6100 /* Warn if the expression has boolean value. */
6101 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6102 && (complain & tf_warning)
6103 && warning_at (location, OPT_Wbool_operation,
6104 "%<~%> on an expression of type bool"))
6105 inform (location, "did you mean to use logical not (%<!%>)?");
6106 arg = cp_perform_integral_promotions (arg, complain);
6108 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6109 arg = mark_rvalue_use (arg);
6110 break;
6112 case ABS_EXPR:
6113 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6114 errstring = _("wrong type argument to abs");
6115 else if (!noconvert)
6117 arg = cp_default_conversion (arg, complain);
6118 if (arg == error_mark_node)
6119 return error_mark_node;
6121 break;
6123 case CONJ_EXPR:
6124 /* Conjugating a real value is a no-op, but allow it anyway. */
6125 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6126 errstring = _("wrong type argument to conjugation");
6127 else if (!noconvert)
6129 arg = cp_default_conversion (arg, complain);
6130 if (arg == error_mark_node)
6131 return error_mark_node;
6133 break;
6135 case TRUTH_NOT_EXPR:
6136 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6137 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6138 build_zero_cst (TREE_TYPE (arg)), complain);
6139 arg = perform_implicit_conversion (boolean_type_node, arg,
6140 complain);
6141 val = invert_truthvalue_loc (input_location, arg);
6142 if (arg != error_mark_node)
6143 return val;
6144 errstring = _("in argument to unary !");
6145 break;
6147 case NOP_EXPR:
6148 break;
6150 case REALPART_EXPR:
6151 case IMAGPART_EXPR:
6152 arg = build_real_imag_expr (input_location, code, arg);
6153 return arg;
6155 case PREINCREMENT_EXPR:
6156 case POSTINCREMENT_EXPR:
6157 case PREDECREMENT_EXPR:
6158 case POSTDECREMENT_EXPR:
6159 /* Handle complex lvalues (when permitted)
6160 by reduction to simpler cases. */
6162 val = unary_complex_lvalue (code, arg);
6163 if (val != 0)
6164 return val;
6166 arg = mark_lvalue_use (arg);
6168 /* Increment or decrement the real part of the value,
6169 and don't change the imaginary part. */
6170 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6172 tree real, imag;
6174 arg = cp_stabilize_reference (arg);
6175 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6176 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6177 real = cp_build_unary_op (code, real, true, complain);
6178 if (real == error_mark_node || imag == error_mark_node)
6179 return error_mark_node;
6180 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6181 real, imag);
6184 /* Report invalid types. */
6186 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6187 arg, true)))
6189 if (code == PREINCREMENT_EXPR)
6190 errstring = _("no pre-increment operator for type");
6191 else if (code == POSTINCREMENT_EXPR)
6192 errstring = _("no post-increment operator for type");
6193 else if (code == PREDECREMENT_EXPR)
6194 errstring = _("no pre-decrement operator for type");
6195 else
6196 errstring = _("no post-decrement operator for type");
6197 break;
6199 else if (arg == error_mark_node)
6200 return error_mark_node;
6202 /* Report something read-only. */
6204 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6205 || TREE_READONLY (arg))
6207 if (complain & tf_error)
6208 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6209 || code == POSTINCREMENT_EXPR)
6210 ? lv_increment : lv_decrement));
6211 else
6212 return error_mark_node;
6216 tree inc;
6217 tree declared_type = unlowered_expr_type (arg);
6219 argtype = TREE_TYPE (arg);
6221 /* ARM $5.2.5 last annotation says this should be forbidden. */
6222 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6224 if (complain & tf_error)
6225 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6226 ? G_("ISO C++ forbids incrementing an enum")
6227 : G_("ISO C++ forbids decrementing an enum"));
6228 else
6229 return error_mark_node;
6232 /* Compute the increment. */
6234 if (TYPE_PTR_P (argtype))
6236 tree type = complete_type (TREE_TYPE (argtype));
6238 if (!COMPLETE_OR_VOID_TYPE_P (type))
6240 if (complain & tf_error)
6241 error (((code == PREINCREMENT_EXPR
6242 || code == POSTINCREMENT_EXPR))
6243 ? G_("cannot increment a pointer to incomplete type %qT")
6244 : G_("cannot decrement a pointer to incomplete type %qT"),
6245 TREE_TYPE (argtype));
6246 else
6247 return error_mark_node;
6249 else if (!TYPE_PTROB_P (argtype))
6251 if (complain & tf_error)
6252 pedwarn (input_location, OPT_Wpointer_arith,
6253 (code == PREINCREMENT_EXPR
6254 || code == POSTINCREMENT_EXPR)
6255 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6256 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6257 argtype);
6258 else
6259 return error_mark_node;
6262 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6264 else
6265 inc = VECTOR_TYPE_P (argtype)
6266 ? build_one_cst (argtype)
6267 : integer_one_node;
6269 inc = cp_convert (argtype, inc, complain);
6271 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6272 need to ask Objective-C to build the increment or decrement
6273 expression for it. */
6274 if (objc_is_property_ref (arg))
6275 return objc_build_incr_expr_for_property_ref (input_location, code,
6276 arg, inc);
6278 /* Complain about anything else that is not a true lvalue. */
6279 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6280 || code == POSTINCREMENT_EXPR)
6281 ? lv_increment : lv_decrement),
6282 complain))
6283 return error_mark_node;
6285 /* Forbid using -- or ++ in C++17 on `bool'. */
6286 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6288 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6290 if (complain & tf_error)
6291 error ("use of an operand of type %qT in %<operator--%> "
6292 "is forbidden", boolean_type_node);
6293 return error_mark_node;
6295 else
6297 if (cxx_dialect >= cxx17)
6299 if (complain & tf_error)
6300 error ("use of an operand of type %qT in "
6301 "%<operator++%> is forbidden in C++17",
6302 boolean_type_node);
6303 return error_mark_node;
6305 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6306 else if (!in_system_header_at (input_location))
6307 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6308 "in %<operator++%> is deprecated",
6309 boolean_type_node);
6311 val = boolean_increment (code, arg);
6313 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6314 /* An rvalue has no cv-qualifiers. */
6315 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6316 else
6317 val = build2 (code, TREE_TYPE (arg), arg, inc);
6319 TREE_SIDE_EFFECTS (val) = 1;
6320 return val;
6323 case ADDR_EXPR:
6324 /* Note that this operation never does default_conversion
6325 regardless of NOCONVERT. */
6326 return cp_build_addr_expr (arg, complain);
6328 default:
6329 break;
6332 if (!errstring)
6334 if (argtype == 0)
6335 argtype = TREE_TYPE (arg);
6336 return build1 (code, argtype, arg);
6339 if (complain & tf_error)
6340 error ("%s", errstring);
6341 return error_mark_node;
6344 /* Hook for the c-common bits that build a unary op. */
6345 tree
6346 build_unary_op (location_t /*location*/,
6347 enum tree_code code, tree xarg, bool noconvert)
6349 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6352 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6353 for certain kinds of expressions which are not really lvalues
6354 but which we can accept as lvalues.
6356 If ARG is not a kind of expression we can handle, return
6357 NULL_TREE. */
6359 tree
6360 unary_complex_lvalue (enum tree_code code, tree arg)
6362 /* Inside a template, making these kinds of adjustments is
6363 pointless; we are only concerned with the type of the
6364 expression. */
6365 if (processing_template_decl)
6366 return NULL_TREE;
6368 /* Handle (a, b) used as an "lvalue". */
6369 if (TREE_CODE (arg) == COMPOUND_EXPR)
6371 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6372 tf_warning_or_error);
6373 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6374 TREE_OPERAND (arg, 0), real_result);
6377 /* Handle (a ? b : c) used as an "lvalue". */
6378 if (TREE_CODE (arg) == COND_EXPR
6379 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6380 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6382 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6383 if (TREE_CODE (arg) == MODIFY_EXPR
6384 || TREE_CODE (arg) == PREINCREMENT_EXPR
6385 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6387 tree lvalue = TREE_OPERAND (arg, 0);
6388 if (TREE_SIDE_EFFECTS (lvalue))
6390 lvalue = cp_stabilize_reference (lvalue);
6391 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6392 lvalue, TREE_OPERAND (arg, 1));
6394 return unary_complex_lvalue
6395 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6398 if (code != ADDR_EXPR)
6399 return NULL_TREE;
6401 /* Handle (a = b) used as an "lvalue" for `&'. */
6402 if (TREE_CODE (arg) == MODIFY_EXPR
6403 || TREE_CODE (arg) == INIT_EXPR)
6405 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6406 tf_warning_or_error);
6407 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6408 arg, real_result);
6409 TREE_NO_WARNING (arg) = 1;
6410 return arg;
6413 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6414 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6415 || TREE_CODE (arg) == OFFSET_REF)
6416 return NULL_TREE;
6418 /* We permit compiler to make function calls returning
6419 objects of aggregate type look like lvalues. */
6421 tree targ = arg;
6423 if (TREE_CODE (targ) == SAVE_EXPR)
6424 targ = TREE_OPERAND (targ, 0);
6426 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6428 if (TREE_CODE (arg) == SAVE_EXPR)
6429 targ = arg;
6430 else
6431 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6432 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6435 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6436 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6437 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6440 /* Don't let anything else be handled specially. */
6441 return NULL_TREE;
6444 /* Mark EXP saying that we need to be able to take the
6445 address of it; it should not be allocated in a register.
6446 Value is true if successful. ARRAY_REF_P is true if this
6447 is for ARRAY_REF construction - in that case we don't want
6448 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6449 it is fine to use ARRAY_REFs for vector subscripts on vector
6450 register variables.
6452 C++: we do not allow `current_class_ptr' to be addressable. */
6454 bool
6455 cxx_mark_addressable (tree exp, bool array_ref_p)
6457 tree x = exp;
6459 while (1)
6460 switch (TREE_CODE (x))
6462 case VIEW_CONVERT_EXPR:
6463 if (array_ref_p
6464 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6465 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6466 return true;
6467 /* FALLTHRU */
6468 case ADDR_EXPR:
6469 case COMPONENT_REF:
6470 case ARRAY_REF:
6471 case REALPART_EXPR:
6472 case IMAGPART_EXPR:
6473 x = TREE_OPERAND (x, 0);
6474 break;
6476 case PARM_DECL:
6477 if (x == current_class_ptr)
6479 error ("cannot take the address of %<this%>, which is an rvalue expression");
6480 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6481 return true;
6483 /* Fall through. */
6485 case VAR_DECL:
6486 /* Caller should not be trying to mark initialized
6487 constant fields addressable. */
6488 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6489 || DECL_IN_AGGR_P (x) == 0
6490 || TREE_STATIC (x)
6491 || DECL_EXTERNAL (x));
6492 /* Fall through. */
6494 case RESULT_DECL:
6495 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6496 && !DECL_ARTIFICIAL (x))
6498 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6500 error
6501 ("address of explicit register variable %qD requested", x);
6502 return false;
6504 else if (extra_warnings)
6505 warning
6506 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6508 TREE_ADDRESSABLE (x) = 1;
6509 return true;
6511 case CONST_DECL:
6512 case FUNCTION_DECL:
6513 TREE_ADDRESSABLE (x) = 1;
6514 return true;
6516 case CONSTRUCTOR:
6517 TREE_ADDRESSABLE (x) = 1;
6518 return true;
6520 case TARGET_EXPR:
6521 TREE_ADDRESSABLE (x) = 1;
6522 cxx_mark_addressable (TREE_OPERAND (x, 0));
6523 return true;
6525 default:
6526 return true;
6530 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6532 tree
6533 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6534 tsubst_flags_t complain)
6536 tree orig_ifexp = ifexp;
6537 tree orig_op1 = op1;
6538 tree orig_op2 = op2;
6539 tree expr;
6541 if (processing_template_decl)
6543 /* The standard says that the expression is type-dependent if
6544 IFEXP is type-dependent, even though the eventual type of the
6545 expression doesn't dependent on IFEXP. */
6546 if (type_dependent_expression_p (ifexp)
6547 /* As a GNU extension, the middle operand may be omitted. */
6548 || (op1 && type_dependent_expression_p (op1))
6549 || type_dependent_expression_p (op2))
6550 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6551 ifexp = build_non_dependent_expr (ifexp);
6552 if (op1)
6553 op1 = build_non_dependent_expr (op1);
6554 op2 = build_non_dependent_expr (op2);
6557 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6558 if (processing_template_decl && expr != error_mark_node)
6560 tree min = build_min_non_dep (COND_EXPR, expr,
6561 orig_ifexp, orig_op1, orig_op2);
6562 /* Remember that the result is an lvalue or xvalue. */
6563 if (glvalue_p (expr) && !glvalue_p (min))
6564 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6565 !lvalue_p (expr));
6566 expr = convert_from_reference (min);
6568 return expr;
6571 /* Given a list of expressions, return a compound expression
6572 that performs them all and returns the value of the last of them. */
6574 tree
6575 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6576 tsubst_flags_t complain)
6578 tree expr = TREE_VALUE (list);
6580 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6581 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6583 if (complain & tf_error)
6584 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6585 "list-initializer for non-class type must not "
6586 "be parenthesized");
6587 else
6588 return error_mark_node;
6591 if (TREE_CHAIN (list))
6593 if (complain & tf_error)
6594 switch (exp)
6596 case ELK_INIT:
6597 permerror (input_location, "expression list treated as compound "
6598 "expression in initializer");
6599 break;
6600 case ELK_MEM_INIT:
6601 permerror (input_location, "expression list treated as compound "
6602 "expression in mem-initializer");
6603 break;
6604 case ELK_FUNC_CAST:
6605 permerror (input_location, "expression list treated as compound "
6606 "expression in functional cast");
6607 break;
6608 default:
6609 gcc_unreachable ();
6611 else
6612 return error_mark_node;
6614 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6615 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6616 expr, TREE_VALUE (list), complain);
6619 return expr;
6622 /* Like build_x_compound_expr_from_list, but using a VEC. */
6624 tree
6625 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6626 tsubst_flags_t complain)
6628 if (vec_safe_is_empty (vec))
6629 return NULL_TREE;
6630 else if (vec->length () == 1)
6631 return (*vec)[0];
6632 else
6634 tree expr;
6635 unsigned int ix;
6636 tree t;
6638 if (msg != NULL)
6640 if (complain & tf_error)
6641 permerror (input_location,
6642 "%s expression list treated as compound expression",
6643 msg);
6644 else
6645 return error_mark_node;
6648 expr = (*vec)[0];
6649 for (ix = 1; vec->iterate (ix, &t); ++ix)
6650 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6651 t, complain);
6653 return expr;
6657 /* Handle overloading of the ',' operator when needed. */
6659 tree
6660 build_x_compound_expr (location_t loc, tree op1, tree op2,
6661 tsubst_flags_t complain)
6663 tree result;
6664 tree orig_op1 = op1;
6665 tree orig_op2 = op2;
6666 tree overload = NULL_TREE;
6668 if (processing_template_decl)
6670 if (type_dependent_expression_p (op1)
6671 || type_dependent_expression_p (op2))
6672 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6673 op1 = build_non_dependent_expr (op1);
6674 op2 = build_non_dependent_expr (op2);
6677 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6678 NULL_TREE, &overload, complain);
6679 if (!result)
6680 result = cp_build_compound_expr (op1, op2, complain);
6682 if (processing_template_decl && result != error_mark_node)
6684 if (overload != NULL_TREE)
6685 return (build_min_non_dep_op_overload
6686 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6688 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6691 return result;
6694 /* Like cp_build_compound_expr, but for the c-common bits. */
6696 tree
6697 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6699 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6702 /* Build a compound expression. */
6704 tree
6705 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6707 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6709 if (lhs == error_mark_node || rhs == error_mark_node)
6710 return error_mark_node;
6712 if (TREE_CODE (rhs) == TARGET_EXPR)
6714 /* If the rhs is a TARGET_EXPR, then build the compound
6715 expression inside the target_expr's initializer. This
6716 helps the compiler to eliminate unnecessary temporaries. */
6717 tree init = TREE_OPERAND (rhs, 1);
6719 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6720 TREE_OPERAND (rhs, 1) = init;
6722 return rhs;
6725 if (type_unknown_p (rhs))
6727 if (complain & tf_error)
6728 error ("no context to resolve type of %qE", rhs);
6729 return error_mark_node;
6732 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6735 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6736 casts away constness. CAST gives the type of cast. Returns true
6737 if the cast is ill-formed, false if it is well-formed.
6739 ??? This function warns for casting away any qualifier not just
6740 const. We would like to specify exactly what qualifiers are casted
6741 away.
6744 static bool
6745 check_for_casting_away_constness (tree src_type, tree dest_type,
6746 enum tree_code cast, tsubst_flags_t complain)
6748 /* C-style casts are allowed to cast away constness. With
6749 WARN_CAST_QUAL, we still want to issue a warning. */
6750 if (cast == CAST_EXPR && !warn_cast_qual)
6751 return false;
6753 if (!casts_away_constness (src_type, dest_type, complain))
6754 return false;
6756 switch (cast)
6758 case CAST_EXPR:
6759 if (complain & tf_warning)
6760 warning (OPT_Wcast_qual,
6761 "cast from type %qT to type %qT casts away qualifiers",
6762 src_type, dest_type);
6763 return false;
6765 case STATIC_CAST_EXPR:
6766 if (complain & tf_error)
6767 error ("static_cast from type %qT to type %qT casts away qualifiers",
6768 src_type, dest_type);
6769 return true;
6771 case REINTERPRET_CAST_EXPR:
6772 if (complain & tf_error)
6773 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6774 src_type, dest_type);
6775 return true;
6777 default:
6778 gcc_unreachable();
6782 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6783 void
6784 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6786 if (warn_useless_cast
6787 && complain & tf_warning)
6789 if ((TREE_CODE (type) == REFERENCE_TYPE
6790 && (TYPE_REF_IS_RVALUE (type)
6791 ? xvalue_p (expr) : lvalue_p (expr))
6792 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6793 || same_type_p (TREE_TYPE (expr), type))
6794 warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6798 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6799 void
6800 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6802 if (warn_ignored_qualifiers
6803 && complain & tf_warning
6804 && !CLASS_TYPE_P (type)
6805 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6807 warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6808 "result type");
6812 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6813 (another pointer-to-member type in the same hierarchy) and return
6814 the converted expression. If ALLOW_INVERSE_P is permitted, a
6815 pointer-to-derived may be converted to pointer-to-base; otherwise,
6816 only the other direction is permitted. If C_CAST_P is true, this
6817 conversion is taking place as part of a C-style cast. */
6819 tree
6820 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6821 bool c_cast_p, tsubst_flags_t complain)
6823 if (same_type_p (type, TREE_TYPE (expr)))
6824 return expr;
6826 if (TYPE_PTRDATAMEM_P (type))
6828 tree delta;
6830 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6831 TYPE_PTRMEM_CLASS_TYPE (type),
6832 allow_inverse_p,
6833 c_cast_p, complain);
6834 if (delta == error_mark_node)
6835 return error_mark_node;
6837 if (!integer_zerop (delta))
6839 tree cond, op1, op2;
6841 if (TREE_CODE (expr) == PTRMEM_CST)
6842 expr = cplus_expand_constant (expr);
6843 cond = cp_build_binary_op (input_location,
6844 EQ_EXPR,
6845 expr,
6846 build_int_cst (TREE_TYPE (expr), -1),
6847 complain);
6848 op1 = build_nop (ptrdiff_type_node, expr);
6849 op2 = cp_build_binary_op (input_location,
6850 PLUS_EXPR, op1, delta,
6851 complain);
6853 expr = fold_build3_loc (input_location,
6854 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6858 return build_nop (type, expr);
6860 else
6861 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6862 allow_inverse_p, c_cast_p, complain);
6865 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6866 this static_cast is being attempted as one of the possible casts
6867 allowed by a C-style cast. (In that case, accessibility of base
6868 classes is not considered, and it is OK to cast away
6869 constness.) Return the result of the cast. *VALID_P is set to
6870 indicate whether or not the cast was valid. */
6872 static tree
6873 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6874 bool *valid_p, tsubst_flags_t complain)
6876 tree intype;
6877 tree result;
6878 cp_lvalue_kind clk;
6880 /* Assume the cast is valid. */
6881 *valid_p = true;
6883 intype = unlowered_expr_type (expr);
6885 /* Save casted types in the function's used types hash table. */
6886 used_types_insert (type);
6888 /* A prvalue of non-class type is cv-unqualified. */
6889 if (!CLASS_TYPE_P (type))
6890 type = cv_unqualified (type);
6892 /* [expr.static.cast]
6894 An lvalue of type "cv1 B", where B is a class type, can be cast
6895 to type "reference to cv2 D", where D is a class derived (clause
6896 _class.derived_) from B, if a valid standard conversion from
6897 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6898 same cv-qualification as, or greater cv-qualification than, cv1,
6899 and B is not a virtual base class of D. */
6900 /* We check this case before checking the validity of "TYPE t =
6901 EXPR;" below because for this case:
6903 struct B {};
6904 struct D : public B { D(const B&); };
6905 extern B& b;
6906 void f() { static_cast<const D&>(b); }
6908 we want to avoid constructing a new D. The standard is not
6909 completely clear about this issue, but our interpretation is
6910 consistent with other compilers. */
6911 if (TREE_CODE (type) == REFERENCE_TYPE
6912 && CLASS_TYPE_P (TREE_TYPE (type))
6913 && CLASS_TYPE_P (intype)
6914 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6915 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6916 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6917 build_pointer_type (TYPE_MAIN_VARIANT
6918 (TREE_TYPE (type))),
6919 complain)
6920 && (c_cast_p
6921 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6923 tree base;
6925 if (processing_template_decl)
6926 return expr;
6928 /* There is a standard conversion from "D*" to "B*" even if "B"
6929 is ambiguous or inaccessible. If this is really a
6930 static_cast, then we check both for inaccessibility and
6931 ambiguity. However, if this is a static_cast being performed
6932 because the user wrote a C-style cast, then accessibility is
6933 not considered. */
6934 base = lookup_base (TREE_TYPE (type), intype,
6935 c_cast_p ? ba_unique : ba_check,
6936 NULL, complain);
6937 expr = build_address (expr);
6939 if (sanitize_flags_p (SANITIZE_VPTR))
6941 tree ubsan_check
6942 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6943 intype, expr);
6944 if (ubsan_check)
6945 expr = ubsan_check;
6948 /* Convert from "B*" to "D*". This function will check that "B"
6949 is not a virtual base of "D". Even if we don't have a guarantee
6950 that expr is NULL, if the static_cast is to a reference type,
6951 it is UB if it would be NULL, so omit the non-NULL check. */
6952 expr = build_base_path (MINUS_EXPR, expr, base,
6953 /*nonnull=*/flag_delete_null_pointer_checks,
6954 complain);
6956 /* Convert the pointer to a reference -- but then remember that
6957 there are no expressions with reference type in C++.
6959 We call rvalue so that there's an actual tree code
6960 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6961 is a variable with the same type, the conversion would get folded
6962 away, leaving just the variable and causing lvalue_kind to give
6963 the wrong answer. */
6964 expr = cp_fold_convert (type, expr);
6966 /* When -fsanitize=null, make sure to diagnose reference binding to
6967 NULL even when the reference is converted to pointer later on. */
6968 if (sanitize_flags_p (SANITIZE_NULL)
6969 && TREE_CODE (expr) == COND_EXPR
6970 && TREE_OPERAND (expr, 2)
6971 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
6972 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
6973 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
6975 return convert_from_reference (rvalue (expr));
6978 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6979 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6980 if (TREE_CODE (type) == REFERENCE_TYPE
6981 && TYPE_REF_IS_RVALUE (type)
6982 && (clk = real_lvalue_p (expr))
6983 && reference_related_p (TREE_TYPE (type), intype)
6984 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6986 if (processing_template_decl)
6987 return expr;
6988 if (clk == clk_ordinary)
6990 /* Handle the (non-bit-field) lvalue case here by casting to
6991 lvalue reference and then changing it to an rvalue reference.
6992 Casting an xvalue to rvalue reference will be handled by the
6993 main code path. */
6994 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6995 result = (perform_direct_initialization_if_possible
6996 (lref, expr, c_cast_p, complain));
6997 result = build1 (NON_LVALUE_EXPR, type, result);
6998 return convert_from_reference (result);
7000 else
7001 /* For a bit-field or packed field, bind to a temporary. */
7002 expr = rvalue (expr);
7005 /* Resolve overloaded address here rather than once in
7006 implicit_conversion and again in the inverse code below. */
7007 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7009 expr = instantiate_type (type, expr, complain);
7010 intype = TREE_TYPE (expr);
7013 /* [expr.static.cast]
7015 Any expression can be explicitly converted to type cv void. */
7016 if (VOID_TYPE_P (type))
7017 return convert_to_void (expr, ICV_CAST, complain);
7019 /* [class.abstract]
7020 An abstract class shall not be used ... as the type of an explicit
7021 conversion. */
7022 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7023 return error_mark_node;
7025 /* [expr.static.cast]
7027 An expression e can be explicitly converted to a type T using a
7028 static_cast of the form static_cast<T>(e) if the declaration T
7029 t(e);" is well-formed, for some invented temporary variable
7030 t. */
7031 result = perform_direct_initialization_if_possible (type, expr,
7032 c_cast_p, complain);
7033 if (result)
7035 if (processing_template_decl)
7036 return expr;
7038 result = convert_from_reference (result);
7040 /* [expr.static.cast]
7042 If T is a reference type, the result is an lvalue; otherwise,
7043 the result is an rvalue. */
7044 if (TREE_CODE (type) != REFERENCE_TYPE)
7046 result = rvalue (result);
7048 if (result == expr && SCALAR_TYPE_P (type))
7049 /* Leave some record of the cast. */
7050 result = build_nop (type, expr);
7052 return result;
7055 /* [expr.static.cast]
7057 The inverse of any standard conversion sequence (clause _conv_),
7058 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7059 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7060 (_conv.bool_) conversions, can be performed explicitly using
7061 static_cast subject to the restriction that the explicit
7062 conversion does not cast away constness (_expr.const.cast_), and
7063 the following additional rules for specific cases: */
7064 /* For reference, the conversions not excluded are: integral
7065 promotions, floating point promotion, integral conversions,
7066 floating point conversions, floating-integral conversions,
7067 pointer conversions, and pointer to member conversions. */
7068 /* DR 128
7070 A value of integral _or enumeration_ type can be explicitly
7071 converted to an enumeration type. */
7072 /* The effect of all that is that any conversion between any two
7073 types which are integral, floating, or enumeration types can be
7074 performed. */
7075 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7076 || SCALAR_FLOAT_TYPE_P (type))
7077 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7078 || SCALAR_FLOAT_TYPE_P (intype)))
7080 if (processing_template_decl)
7081 return expr;
7082 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7085 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7086 && CLASS_TYPE_P (TREE_TYPE (type))
7087 && CLASS_TYPE_P (TREE_TYPE (intype))
7088 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7089 (TREE_TYPE (intype))),
7090 build_pointer_type (TYPE_MAIN_VARIANT
7091 (TREE_TYPE (type))),
7092 complain))
7094 tree base;
7096 if (processing_template_decl)
7097 return expr;
7099 if (!c_cast_p
7100 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7101 complain))
7102 return error_mark_node;
7103 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7104 c_cast_p ? ba_unique : ba_check,
7105 NULL, complain);
7106 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7107 complain);
7109 if (sanitize_flags_p (SANITIZE_VPTR))
7111 tree ubsan_check
7112 = cp_ubsan_maybe_instrument_downcast (input_location, type,
7113 intype, expr);
7114 if (ubsan_check)
7115 expr = ubsan_check;
7118 return cp_fold_convert (type, expr);
7121 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7122 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7124 tree c1;
7125 tree c2;
7126 tree t1;
7127 tree t2;
7129 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7130 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7132 if (TYPE_PTRDATAMEM_P (type))
7134 t1 = (build_ptrmem_type
7135 (c1,
7136 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7137 t2 = (build_ptrmem_type
7138 (c2,
7139 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7141 else
7143 t1 = intype;
7144 t2 = type;
7146 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7148 if (!c_cast_p
7149 && check_for_casting_away_constness (intype, type,
7150 STATIC_CAST_EXPR,
7151 complain))
7152 return error_mark_node;
7153 if (processing_template_decl)
7154 return expr;
7155 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7156 c_cast_p, complain);
7160 /* [expr.static.cast]
7162 An rvalue of type "pointer to cv void" can be explicitly
7163 converted to a pointer to object type. A value of type pointer
7164 to object converted to "pointer to cv void" and back to the
7165 original pointer type will have its original value. */
7166 if (TYPE_PTR_P (intype)
7167 && VOID_TYPE_P (TREE_TYPE (intype))
7168 && TYPE_PTROB_P (type))
7170 if (!c_cast_p
7171 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7172 complain))
7173 return error_mark_node;
7174 if (processing_template_decl)
7175 return expr;
7176 return build_nop (type, expr);
7179 *valid_p = false;
7180 return error_mark_node;
7183 /* Return an expression representing static_cast<TYPE>(EXPR). */
7185 tree
7186 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7188 tree expr = oexpr;
7189 tree result;
7190 bool valid_p;
7192 if (type == error_mark_node || expr == error_mark_node)
7193 return error_mark_node;
7195 bool dependent = (dependent_type_p (type)
7196 || type_dependent_expression_p (expr));
7197 if (dependent)
7199 tmpl:
7200 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7201 /* We don't know if it will or will not have side effects. */
7202 TREE_SIDE_EFFECTS (expr) = 1;
7203 return convert_from_reference (expr);
7206 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7207 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7208 if (TREE_CODE (type) != REFERENCE_TYPE
7209 && TREE_CODE (expr) == NOP_EXPR
7210 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7211 expr = TREE_OPERAND (expr, 0);
7213 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7214 complain);
7215 if (valid_p)
7217 if (result != error_mark_node)
7219 maybe_warn_about_useless_cast (type, expr, complain);
7220 maybe_warn_about_cast_ignoring_quals (type, complain);
7222 if (processing_template_decl)
7223 goto tmpl;
7224 return result;
7227 if (complain & tf_error)
7228 error ("invalid static_cast from type %qT to type %qT",
7229 TREE_TYPE (expr), type);
7230 return error_mark_node;
7233 /* EXPR is an expression with member function or pointer-to-member
7234 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7235 not permitted by ISO C++, but we accept it in some modes. If we
7236 are not in one of those modes, issue a diagnostic. Return the
7237 converted expression. */
7239 tree
7240 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7242 tree intype;
7243 tree decl;
7245 intype = TREE_TYPE (expr);
7246 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7247 || TREE_CODE (intype) == METHOD_TYPE);
7249 if (!(complain & tf_warning_or_error))
7250 return error_mark_node;
7252 if (pedantic || warn_pmf2ptr)
7253 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7254 "converting from %qH to %qI", intype, type);
7256 if (TREE_CODE (intype) == METHOD_TYPE)
7257 expr = build_addr_func (expr, complain);
7258 else if (TREE_CODE (expr) == PTRMEM_CST)
7259 expr = build_address (PTRMEM_CST_MEMBER (expr));
7260 else
7262 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7263 decl = build_address (decl);
7264 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7267 if (expr == error_mark_node)
7268 return error_mark_node;
7270 return build_nop (type, expr);
7273 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7274 If C_CAST_P is true, this reinterpret cast is being done as part of
7275 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7276 indicate whether or not reinterpret_cast was valid. */
7278 static tree
7279 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7280 bool *valid_p, tsubst_flags_t complain)
7282 tree intype;
7284 /* Assume the cast is invalid. */
7285 if (valid_p)
7286 *valid_p = true;
7288 if (type == error_mark_node || error_operand_p (expr))
7289 return error_mark_node;
7291 intype = TREE_TYPE (expr);
7293 /* Save casted types in the function's used types hash table. */
7294 used_types_insert (type);
7296 /* A prvalue of non-class type is cv-unqualified. */
7297 if (!CLASS_TYPE_P (type))
7298 type = cv_unqualified (type);
7300 /* [expr.reinterpret.cast]
7301 An lvalue expression of type T1 can be cast to the type
7302 "reference to T2" if an expression of type "pointer to T1" can be
7303 explicitly converted to the type "pointer to T2" using a
7304 reinterpret_cast. */
7305 if (TREE_CODE (type) == REFERENCE_TYPE)
7307 if (! lvalue_p (expr))
7309 if (complain & tf_error)
7310 error ("invalid cast of an rvalue expression of type "
7311 "%qT to type %qT",
7312 intype, type);
7313 return error_mark_node;
7316 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7317 "B" are related class types; the reinterpret_cast does not
7318 adjust the pointer. */
7319 if (TYPE_PTR_P (intype)
7320 && (complain & tf_warning)
7321 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7322 COMPARE_BASE | COMPARE_DERIVED)))
7323 warning (0, "casting %qT to %qT does not dereference pointer",
7324 intype, type);
7326 expr = cp_build_addr_expr (expr, complain);
7328 if (warn_strict_aliasing > 2)
7329 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
7331 if (expr != error_mark_node)
7332 expr = build_reinterpret_cast_1
7333 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7334 valid_p, complain);
7335 if (expr != error_mark_node)
7336 /* cp_build_indirect_ref isn't right for rvalue refs. */
7337 expr = convert_from_reference (fold_convert (type, expr));
7338 return expr;
7341 /* As a G++ extension, we consider conversions from member
7342 functions, and pointers to member functions to
7343 pointer-to-function and pointer-to-void types. If
7344 -Wno-pmf-conversions has not been specified,
7345 convert_member_func_to_ptr will issue an error message. */
7346 if ((TYPE_PTRMEMFUNC_P (intype)
7347 || TREE_CODE (intype) == METHOD_TYPE)
7348 && TYPE_PTR_P (type)
7349 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7350 || VOID_TYPE_P (TREE_TYPE (type))))
7351 return convert_member_func_to_ptr (type, expr, complain);
7353 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7354 array-to-pointer, and function-to-pointer conversions are
7355 performed. */
7356 expr = decay_conversion (expr, complain);
7358 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7359 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7360 if (TREE_CODE (expr) == NOP_EXPR
7361 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7362 expr = TREE_OPERAND (expr, 0);
7364 if (error_operand_p (expr))
7365 return error_mark_node;
7367 intype = TREE_TYPE (expr);
7369 /* [expr.reinterpret.cast]
7370 A pointer can be converted to any integral type large enough to
7371 hold it. ... A value of type std::nullptr_t can be converted to
7372 an integral type; the conversion has the same meaning and
7373 validity as a conversion of (void*)0 to the integral type. */
7374 if (CP_INTEGRAL_TYPE_P (type)
7375 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7377 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7379 if (complain & tf_error)
7380 permerror (input_location, "cast from %qH to %qI loses precision",
7381 intype, type);
7382 else
7383 return error_mark_node;
7385 if (NULLPTR_TYPE_P (intype))
7386 return build_int_cst (type, 0);
7388 /* [expr.reinterpret.cast]
7389 A value of integral or enumeration type can be explicitly
7390 converted to a pointer. */
7391 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7392 /* OK */
7394 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7395 || TYPE_PTR_OR_PTRMEM_P (type))
7396 && same_type_p (type, intype))
7397 /* DR 799 */
7398 return rvalue (expr);
7399 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7401 if ((complain & tf_warning)
7402 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7403 TREE_TYPE (intype)))
7404 warning (OPT_Wcast_function_type,
7405 "cast between incompatible function types"
7406 " from %qH to %qI", intype, type);
7407 return build_nop (type, expr);
7409 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7411 if ((complain & tf_warning)
7412 && !cxx_safe_function_type_cast_p
7413 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7414 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7415 warning (OPT_Wcast_function_type,
7416 "cast between incompatible pointer to member types"
7417 " from %qH to %qI", intype, type);
7418 return build_nop (type, expr);
7420 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7421 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7423 tree sexpr = expr;
7425 if (!c_cast_p
7426 && check_for_casting_away_constness (intype, type,
7427 REINTERPRET_CAST_EXPR,
7428 complain))
7429 return error_mark_node;
7430 /* Warn about possible alignment problems. */
7431 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7432 && (complain & tf_warning)
7433 && !VOID_TYPE_P (type)
7434 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7435 && COMPLETE_TYPE_P (TREE_TYPE (type))
7436 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7437 && min_align_of_type (TREE_TYPE (type))
7438 > min_align_of_type (TREE_TYPE (intype)))
7439 warning (OPT_Wcast_align, "cast from %qH to %qI "
7440 "increases required alignment of target type", intype, type);
7442 /* We need to strip nops here, because the front end likes to
7443 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7444 STRIP_NOPS (sexpr);
7445 if (warn_strict_aliasing <= 2)
7446 strict_aliasing_warning (intype, type, sexpr);
7448 return build_nop (type, expr);
7450 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7451 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7453 if (complain & tf_warning)
7454 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7455 object pointer type or vice versa is conditionally-supported." */
7456 warning (OPT_Wconditionally_supported,
7457 "casting between pointer-to-function and pointer-to-object "
7458 "is conditionally-supported");
7459 return build_nop (type, expr);
7461 else if (VECTOR_TYPE_P (type))
7462 return convert_to_vector (type, expr);
7463 else if (VECTOR_TYPE_P (intype)
7464 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7465 return convert_to_integer_nofold (type, expr);
7466 else
7468 if (valid_p)
7469 *valid_p = false;
7470 if (complain & tf_error)
7471 error ("invalid cast from type %qT to type %qT", intype, type);
7472 return error_mark_node;
7475 return cp_convert (type, expr, complain);
7478 tree
7479 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7481 tree r;
7483 if (type == error_mark_node || expr == error_mark_node)
7484 return error_mark_node;
7486 if (processing_template_decl)
7488 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7490 if (!TREE_SIDE_EFFECTS (t)
7491 && type_dependent_expression_p (expr))
7492 /* There might turn out to be side effects inside expr. */
7493 TREE_SIDE_EFFECTS (t) = 1;
7494 return convert_from_reference (t);
7497 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7498 /*valid_p=*/NULL, complain);
7499 if (r != error_mark_node)
7501 maybe_warn_about_useless_cast (type, expr, complain);
7502 maybe_warn_about_cast_ignoring_quals (type, complain);
7504 return r;
7507 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7508 return an appropriate expression. Otherwise, return
7509 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7510 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7511 performing a C-style cast, its value upon return will indicate
7512 whether or not the conversion succeeded. */
7514 static tree
7515 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7516 bool *valid_p)
7518 tree src_type;
7519 tree reference_type;
7521 /* Callers are responsible for handling error_mark_node as a
7522 destination type. */
7523 gcc_assert (dst_type != error_mark_node);
7524 /* In a template, callers should be building syntactic
7525 representations of casts, not using this machinery. */
7526 gcc_assert (!processing_template_decl);
7528 /* Assume the conversion is invalid. */
7529 if (valid_p)
7530 *valid_p = false;
7532 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7534 if (complain & tf_error)
7535 error ("invalid use of const_cast with type %qT, "
7536 "which is not a pointer, "
7537 "reference, nor a pointer-to-data-member type", dst_type);
7538 return error_mark_node;
7541 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7543 if (complain & tf_error)
7544 error ("invalid use of const_cast with type %qT, which is a pointer "
7545 "or reference to a function type", dst_type);
7546 return error_mark_node;
7549 /* A prvalue of non-class type is cv-unqualified. */
7550 dst_type = cv_unqualified (dst_type);
7552 /* Save casted types in the function's used types hash table. */
7553 used_types_insert (dst_type);
7555 src_type = TREE_TYPE (expr);
7556 /* Expressions do not really have reference types. */
7557 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7558 src_type = TREE_TYPE (src_type);
7560 /* [expr.const.cast]
7562 For two object types T1 and T2, if a pointer to T1 can be explicitly
7563 converted to the type "pointer to T2" using a const_cast, then the
7564 following conversions can also be made:
7566 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7567 type T2 using the cast const_cast<T2&>;
7569 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7570 type T2 using the cast const_cast<T2&&>; and
7572 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7573 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7575 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7577 reference_type = dst_type;
7578 if (!TYPE_REF_IS_RVALUE (dst_type)
7579 ? lvalue_p (expr)
7580 : obvalue_p (expr))
7581 /* OK. */;
7582 else
7584 if (complain & tf_error)
7585 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7586 src_type, dst_type);
7587 return error_mark_node;
7589 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7590 src_type = build_pointer_type (src_type);
7592 else
7594 reference_type = NULL_TREE;
7595 /* If the destination type is not a reference type, the
7596 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7597 conversions are performed. */
7598 src_type = type_decays_to (src_type);
7599 if (src_type == error_mark_node)
7600 return error_mark_node;
7603 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7605 if (comp_ptr_ttypes_const (dst_type, src_type))
7607 if (valid_p)
7609 *valid_p = true;
7610 /* This cast is actually a C-style cast. Issue a warning if
7611 the user is making a potentially unsafe cast. */
7612 check_for_casting_away_constness (src_type, dst_type,
7613 CAST_EXPR, complain);
7614 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7615 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7616 && (complain & tf_warning)
7617 && min_align_of_type (TREE_TYPE (dst_type))
7618 > min_align_of_type (TREE_TYPE (src_type)))
7619 warning (OPT_Wcast_align, "cast from %qH to %qI "
7620 "increases required alignment of target type",
7621 src_type, dst_type);
7623 if (reference_type)
7625 expr = cp_build_addr_expr (expr, complain);
7626 if (expr == error_mark_node)
7627 return error_mark_node;
7628 expr = build_nop (reference_type, expr);
7629 return convert_from_reference (expr);
7631 else
7633 expr = decay_conversion (expr, complain);
7634 if (expr == error_mark_node)
7635 return error_mark_node;
7637 /* build_c_cast puts on a NOP_EXPR to make the result not an
7638 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7639 non-lvalue context. */
7640 if (TREE_CODE (expr) == NOP_EXPR
7641 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7642 expr = TREE_OPERAND (expr, 0);
7643 return build_nop (dst_type, expr);
7646 else if (valid_p
7647 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7648 TREE_TYPE (src_type)))
7649 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7650 complain);
7653 if (complain & tf_error)
7654 error ("invalid const_cast from type %qT to type %qT",
7655 src_type, dst_type);
7656 return error_mark_node;
7659 tree
7660 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7662 tree r;
7664 if (type == error_mark_node || error_operand_p (expr))
7665 return error_mark_node;
7667 if (processing_template_decl)
7669 tree t = build_min (CONST_CAST_EXPR, type, expr);
7671 if (!TREE_SIDE_EFFECTS (t)
7672 && type_dependent_expression_p (expr))
7673 /* There might turn out to be side effects inside expr. */
7674 TREE_SIDE_EFFECTS (t) = 1;
7675 return convert_from_reference (t);
7678 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7679 if (r != error_mark_node)
7681 maybe_warn_about_useless_cast (type, expr, complain);
7682 maybe_warn_about_cast_ignoring_quals (type, complain);
7684 return r;
7687 /* Like cp_build_c_cast, but for the c-common bits. */
7689 tree
7690 build_c_cast (location_t /*loc*/, tree type, tree expr)
7692 return cp_build_c_cast (type, expr, tf_warning_or_error);
7695 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7696 preserve location information even for tree nodes that don't
7697 support it. */
7699 cp_expr
7700 build_c_cast (location_t loc, tree type, cp_expr expr)
7702 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7703 result.set_location (loc);
7704 return result;
7707 /* Build an expression representing an explicit C-style cast to type
7708 TYPE of expression EXPR. */
7710 tree
7711 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7713 tree value = expr;
7714 tree result;
7715 bool valid_p;
7717 if (type == error_mark_node || error_operand_p (expr))
7718 return error_mark_node;
7720 if (processing_template_decl)
7722 tree t = build_min (CAST_EXPR, type,
7723 tree_cons (NULL_TREE, value, NULL_TREE));
7724 /* We don't know if it will or will not have side effects. */
7725 TREE_SIDE_EFFECTS (t) = 1;
7726 return convert_from_reference (t);
7729 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7730 'Class') should always be retained, because this information aids
7731 in method lookup. */
7732 if (objc_is_object_ptr (type)
7733 && objc_is_object_ptr (TREE_TYPE (expr)))
7734 return build_nop (type, expr);
7736 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7737 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7738 if (TREE_CODE (type) != REFERENCE_TYPE
7739 && TREE_CODE (value) == NOP_EXPR
7740 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7741 value = TREE_OPERAND (value, 0);
7743 if (TREE_CODE (type) == ARRAY_TYPE)
7745 /* Allow casting from T1* to T2[] because Cfront allows it.
7746 NIHCL uses it. It is not valid ISO C++ however. */
7747 if (TYPE_PTR_P (TREE_TYPE (expr)))
7749 if (complain & tf_error)
7750 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7751 else
7752 return error_mark_node;
7753 type = build_pointer_type (TREE_TYPE (type));
7755 else
7757 if (complain & tf_error)
7758 error ("ISO C++ forbids casting to an array type %qT", type);
7759 return error_mark_node;
7763 if (TREE_CODE (type) == FUNCTION_TYPE
7764 || TREE_CODE (type) == METHOD_TYPE)
7766 if (complain & tf_error)
7767 error ("invalid cast to function type %qT", type);
7768 return error_mark_node;
7771 if (TYPE_PTR_P (type)
7772 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7773 /* Casting to an integer of smaller size is an error detected elsewhere. */
7774 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7775 /* Don't warn about converting any constant. */
7776 && !TREE_CONSTANT (value))
7777 warning_at (input_location, OPT_Wint_to_pointer_cast,
7778 "cast to pointer from integer of different size");
7780 /* A C-style cast can be a const_cast. */
7781 result = build_const_cast_1 (type, value, complain & tf_warning,
7782 &valid_p);
7783 if (valid_p)
7785 if (result != error_mark_node)
7787 maybe_warn_about_useless_cast (type, value, complain);
7788 maybe_warn_about_cast_ignoring_quals (type, complain);
7790 return result;
7793 /* Or a static cast. */
7794 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7795 &valid_p, complain);
7796 /* Or a reinterpret_cast. */
7797 if (!valid_p)
7798 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7799 &valid_p, complain);
7800 /* The static_cast or reinterpret_cast may be followed by a
7801 const_cast. */
7802 if (valid_p
7803 /* A valid cast may result in errors if, for example, a
7804 conversion to an ambiguous base class is required. */
7805 && !error_operand_p (result))
7807 tree result_type;
7809 maybe_warn_about_useless_cast (type, value, complain);
7810 maybe_warn_about_cast_ignoring_quals (type, complain);
7812 /* Non-class rvalues always have cv-unqualified type. */
7813 if (!CLASS_TYPE_P (type))
7814 type = TYPE_MAIN_VARIANT (type);
7815 result_type = TREE_TYPE (result);
7816 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7817 result_type = TYPE_MAIN_VARIANT (result_type);
7818 /* If the type of RESULT does not match TYPE, perform a
7819 const_cast to make it match. If the static_cast or
7820 reinterpret_cast succeeded, we will differ by at most
7821 cv-qualification, so the follow-on const_cast is guaranteed
7822 to succeed. */
7823 if (!same_type_p (non_reference (type), non_reference (result_type)))
7825 result = build_const_cast_1 (type, result, false, &valid_p);
7826 gcc_assert (valid_p);
7828 return result;
7831 return error_mark_node;
7834 /* For use from the C common bits. */
7835 tree
7836 build_modify_expr (location_t location,
7837 tree lhs, tree /*lhs_origtype*/,
7838 enum tree_code modifycode,
7839 location_t /*rhs_location*/, tree rhs,
7840 tree /*rhs_origtype*/)
7842 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7843 tf_warning_or_error);
7846 /* Build an assignment expression of lvalue LHS from value RHS.
7847 MODIFYCODE is the code for a binary operator that we use
7848 to combine the old value of LHS with RHS to get the new value.
7849 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7851 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7853 tree
7854 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7855 tree rhs, tsubst_flags_t complain)
7857 lhs = mark_lvalue_use_nonread (lhs);
7859 tree result = NULL_TREE;
7860 tree newrhs = rhs;
7861 tree lhstype = TREE_TYPE (lhs);
7862 tree olhs = lhs;
7863 tree olhstype = lhstype;
7864 bool plain_assign = (modifycode == NOP_EXPR);
7865 bool compound_side_effects_p = false;
7866 tree preeval = NULL_TREE;
7868 /* Avoid duplicate error messages from operands that had errors. */
7869 if (error_operand_p (lhs) || error_operand_p (rhs))
7870 return error_mark_node;
7872 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7874 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7875 compound_side_effects_p = true;
7876 lhs = TREE_OPERAND (lhs, 1);
7879 /* Handle control structure constructs used as "lvalues". Note that we
7880 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7881 switch (TREE_CODE (lhs))
7883 /* Handle --foo = 5; as these are valid constructs in C++. */
7884 case PREDECREMENT_EXPR:
7885 case PREINCREMENT_EXPR:
7886 if (compound_side_effects_p)
7887 newrhs = rhs = stabilize_expr (rhs, &preeval);
7888 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7889 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7890 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7891 TREE_OPERAND (lhs, 1));
7892 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7893 maybe_add_compound:
7894 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7895 and looked through the COMPOUND_EXPRs, readd them now around
7896 the resulting lhs. */
7897 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7899 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7900 tree *ptr = &TREE_OPERAND (lhs, 1);
7901 for (olhs = TREE_OPERAND (olhs, 1);
7902 TREE_CODE (olhs) == COMPOUND_EXPR;
7903 olhs = TREE_OPERAND (olhs, 1))
7905 *ptr = build2 (COMPOUND_EXPR, lhstype,
7906 TREE_OPERAND (olhs, 0), *ptr);
7907 ptr = &TREE_OPERAND (*ptr, 1);
7910 break;
7912 case MODIFY_EXPR:
7913 if (compound_side_effects_p)
7914 newrhs = rhs = stabilize_expr (rhs, &preeval);
7915 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7916 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7917 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7918 TREE_OPERAND (lhs, 1));
7919 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7920 goto maybe_add_compound;
7922 case MIN_EXPR:
7923 case MAX_EXPR:
7924 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7925 when neither operand has side-effects. */
7926 if (!lvalue_or_else (lhs, lv_assign, complain))
7927 return error_mark_node;
7929 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7930 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7932 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7933 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7934 boolean_type_node,
7935 TREE_OPERAND (lhs, 0),
7936 TREE_OPERAND (lhs, 1)),
7937 TREE_OPERAND (lhs, 0),
7938 TREE_OPERAND (lhs, 1));
7939 gcc_fallthrough ();
7941 /* Handle (a ? b : c) used as an "lvalue". */
7942 case COND_EXPR:
7944 /* Produce (a ? (b = rhs) : (c = rhs))
7945 except that the RHS goes through a save-expr
7946 so the code to compute it is only emitted once. */
7947 tree cond;
7949 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7951 if (complain & tf_error)
7952 error ("void value not ignored as it ought to be");
7953 return error_mark_node;
7956 rhs = stabilize_expr (rhs, &preeval);
7958 /* Check this here to avoid odd errors when trying to convert
7959 a throw to the type of the COND_EXPR. */
7960 if (!lvalue_or_else (lhs, lv_assign, complain))
7961 return error_mark_node;
7963 cond = build_conditional_expr
7964 (input_location, TREE_OPERAND (lhs, 0),
7965 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7966 modifycode, rhs, complain),
7967 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7968 modifycode, rhs, complain),
7969 complain);
7971 if (cond == error_mark_node)
7972 return cond;
7973 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7974 and looked through the COMPOUND_EXPRs, readd them now around
7975 the resulting cond before adding the preevaluated rhs. */
7976 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7978 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7979 TREE_OPERAND (olhs, 0), cond);
7980 tree *ptr = &TREE_OPERAND (cond, 1);
7981 for (olhs = TREE_OPERAND (olhs, 1);
7982 TREE_CODE (olhs) == COMPOUND_EXPR;
7983 olhs = TREE_OPERAND (olhs, 1))
7985 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7986 TREE_OPERAND (olhs, 0), *ptr);
7987 ptr = &TREE_OPERAND (*ptr, 1);
7990 /* Make sure the code to compute the rhs comes out
7991 before the split. */
7992 result = cond;
7993 goto ret;
7996 default:
7997 lhs = olhs;
7998 break;
8001 if (modifycode == INIT_EXPR)
8003 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8004 /* Do the default thing. */;
8005 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8007 /* Compound literal. */
8008 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8009 /* Call convert to generate an error; see PR 11063. */
8010 rhs = convert (lhstype, rhs);
8011 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8012 TREE_SIDE_EFFECTS (result) = 1;
8013 goto ret;
8015 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8016 /* Do the default thing. */;
8017 else
8019 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8020 result = build_special_member_call (lhs, complete_ctor_identifier,
8021 &rhs_vec, lhstype, LOOKUP_NORMAL,
8022 complain);
8023 release_tree_vector (rhs_vec);
8024 if (result == NULL_TREE)
8025 return error_mark_node;
8026 goto ret;
8029 else
8031 lhs = require_complete_type_sfinae (lhs, complain);
8032 if (lhs == error_mark_node)
8033 return error_mark_node;
8035 if (modifycode == NOP_EXPR)
8037 if (c_dialect_objc ())
8039 result = objc_maybe_build_modify_expr (lhs, rhs);
8040 if (result)
8041 goto ret;
8044 /* `operator=' is not an inheritable operator. */
8045 if (! MAYBE_CLASS_TYPE_P (lhstype))
8046 /* Do the default thing. */;
8047 else
8049 result = build_new_op (input_location, MODIFY_EXPR,
8050 LOOKUP_NORMAL, lhs, rhs,
8051 make_node (NOP_EXPR), /*overload=*/NULL,
8052 complain);
8053 if (result == NULL_TREE)
8054 return error_mark_node;
8055 goto ret;
8057 lhstype = olhstype;
8059 else
8061 tree init = NULL_TREE;
8063 /* A binary op has been requested. Combine the old LHS
8064 value with the RHS producing the value we should actually
8065 store into the LHS. */
8066 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
8067 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8068 || MAYBE_CLASS_TYPE_P (lhstype)));
8070 /* Preevaluate the RHS to make sure its evaluation is complete
8071 before the lvalue-to-rvalue conversion of the LHS:
8073 [expr.ass] With respect to an indeterminately-sequenced
8074 function call, the operation of a compound assignment is a
8075 single evaluation. [ Note: Therefore, a function call shall
8076 not intervene between the lvalue-to-rvalue conversion and the
8077 side effect associated with any single compound assignment
8078 operator. -- end note ] */
8079 lhs = cp_stabilize_reference (lhs);
8080 rhs = rvalue (rhs);
8081 if (rhs == error_mark_node)
8082 return error_mark_node;
8083 rhs = stabilize_expr (rhs, &init);
8084 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8085 if (newrhs == error_mark_node)
8087 if (complain & tf_error)
8088 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8089 TREE_TYPE (lhs), TREE_TYPE (rhs));
8090 return error_mark_node;
8093 if (init)
8094 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8096 /* Now it looks like a plain assignment. */
8097 modifycode = NOP_EXPR;
8098 if (c_dialect_objc ())
8100 result = objc_maybe_build_modify_expr (lhs, newrhs);
8101 if (result)
8102 goto ret;
8105 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
8106 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
8109 /* The left-hand side must be an lvalue. */
8110 if (!lvalue_or_else (lhs, lv_assign, complain))
8111 return error_mark_node;
8113 /* Warn about modifying something that is `const'. Don't warn if
8114 this is initialization. */
8115 if (modifycode != INIT_EXPR
8116 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8117 /* Functions are not modifiable, even though they are
8118 lvalues. */
8119 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8120 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8121 /* If it's an aggregate and any field is const, then it is
8122 effectively const. */
8123 || (CLASS_TYPE_P (lhstype)
8124 && C_TYPE_FIELDS_READONLY (lhstype))))
8126 if (complain & tf_error)
8127 cxx_readonly_error (lhs, lv_assign);
8128 return error_mark_node;
8131 /* If storing into a structure or union member, it may have been given a
8132 lowered bitfield type. We need to convert to the declared type first,
8133 so retrieve it now. */
8135 olhstype = unlowered_expr_type (lhs);
8137 /* Convert new value to destination type. */
8139 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8141 int from_array;
8143 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8145 if (modifycode != INIT_EXPR)
8147 if (complain & tf_error)
8148 error ("assigning to an array from an initializer list");
8149 return error_mark_node;
8151 if (check_array_initializer (lhs, lhstype, newrhs))
8152 return error_mark_node;
8153 newrhs = digest_init (lhstype, newrhs, complain);
8154 if (newrhs == error_mark_node)
8155 return error_mark_node;
8158 /* C++11 8.5/17: "If the destination type is an array of characters,
8159 an array of char16_t, an array of char32_t, or an array of wchar_t,
8160 and the initializer is a string literal...". */
8161 else if (TREE_CODE (newrhs) == STRING_CST
8162 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8163 && modifycode == INIT_EXPR)
8165 newrhs = digest_init (lhstype, newrhs, complain);
8166 if (newrhs == error_mark_node)
8167 return error_mark_node;
8170 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8171 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8173 if (complain & tf_error)
8174 error ("incompatible types in assignment of %qT to %qT",
8175 TREE_TYPE (rhs), lhstype);
8176 return error_mark_node;
8179 /* Allow array assignment in compiler-generated code. */
8180 else if (!current_function_decl
8181 || !DECL_DEFAULTED_FN (current_function_decl))
8183 /* This routine is used for both initialization and assignment.
8184 Make sure the diagnostic message differentiates the context. */
8185 if (complain & tf_error)
8187 if (modifycode == INIT_EXPR)
8188 error ("array used as initializer");
8189 else
8190 error ("invalid array assignment");
8192 return error_mark_node;
8195 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8196 ? 1 + (modifycode != INIT_EXPR): 0;
8197 result = build_vec_init (lhs, NULL_TREE, newrhs,
8198 /*explicit_value_init_p=*/false,
8199 from_array, complain);
8200 goto ret;
8203 if (modifycode == INIT_EXPR)
8204 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8205 LOOKUP_ONLYCONVERTING. */
8206 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8207 ICR_INIT, NULL_TREE, 0,
8208 complain);
8209 else
8210 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8211 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8213 if (!same_type_p (lhstype, olhstype))
8214 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8216 if (modifycode != INIT_EXPR)
8218 if (TREE_CODE (newrhs) == CALL_EXPR
8219 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8220 newrhs = build_cplus_new (lhstype, newrhs, complain);
8222 /* Can't initialize directly from a TARGET_EXPR, since that would
8223 cause the lhs to be constructed twice, and possibly result in
8224 accidental self-initialization. So we force the TARGET_EXPR to be
8225 expanded without a target. */
8226 if (TREE_CODE (newrhs) == TARGET_EXPR)
8227 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8228 TREE_OPERAND (newrhs, 0));
8231 if (newrhs == error_mark_node)
8232 return error_mark_node;
8234 if (c_dialect_objc () && flag_objc_gc)
8236 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8238 if (result)
8239 goto ret;
8242 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8243 lhstype, lhs, newrhs);
8245 TREE_SIDE_EFFECTS (result) = 1;
8246 if (!plain_assign)
8247 TREE_NO_WARNING (result) = 1;
8249 ret:
8250 if (preeval)
8251 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8252 return result;
8255 cp_expr
8256 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8257 tree rhs, tsubst_flags_t complain)
8259 tree orig_lhs = lhs;
8260 tree orig_rhs = rhs;
8261 tree overload = NULL_TREE;
8262 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8264 if (processing_template_decl)
8266 if (modifycode == NOP_EXPR
8267 || type_dependent_expression_p (lhs)
8268 || type_dependent_expression_p (rhs))
8269 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8270 build_min_nt_loc (loc, modifycode, NULL_TREE,
8271 NULL_TREE), rhs);
8273 lhs = build_non_dependent_expr (lhs);
8274 rhs = build_non_dependent_expr (rhs);
8277 if (modifycode != NOP_EXPR)
8279 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8280 lhs, rhs, op, &overload, complain);
8281 if (rval)
8283 if (rval == error_mark_node)
8284 return rval;
8285 TREE_NO_WARNING (rval) = 1;
8286 if (processing_template_decl)
8288 if (overload != NULL_TREE)
8289 return (build_min_non_dep_op_overload
8290 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8292 return (build_min_non_dep
8293 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8295 return rval;
8298 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8301 /* Helper function for get_delta_difference which assumes FROM is a base
8302 class of TO. Returns a delta for the conversion of pointer-to-member
8303 of FROM to pointer-to-member of TO. If the conversion is invalid and
8304 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8305 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8306 If C_CAST_P is true, this conversion is taking place as part of a
8307 C-style cast. */
8309 static tree
8310 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8311 tsubst_flags_t complain)
8313 tree binfo;
8314 base_kind kind;
8316 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8317 &kind, complain);
8319 if (binfo == error_mark_node)
8321 if (!(complain & tf_error))
8322 return error_mark_node;
8324 error (" in pointer to member function conversion");
8325 return size_zero_node;
8327 else if (binfo)
8329 if (kind != bk_via_virtual)
8330 return BINFO_OFFSET (binfo);
8331 else
8332 /* FROM is a virtual base class of TO. Issue an error or warning
8333 depending on whether or not this is a reinterpret cast. */
8335 if (!(complain & tf_error))
8336 return error_mark_node;
8338 error ("pointer to member conversion via virtual base %qT",
8339 BINFO_TYPE (binfo_from_vbase (binfo)));
8341 return size_zero_node;
8344 else
8345 return NULL_TREE;
8348 /* Get difference in deltas for different pointer to member function
8349 types. If the conversion is invalid and tf_error is not set in
8350 COMPLAIN, returns error_mark_node, otherwise returns an integer
8351 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8352 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8353 conversions as well. If C_CAST_P is true this conversion is taking
8354 place as part of a C-style cast.
8356 Note that the naming of FROM and TO is kind of backwards; the return
8357 value is what we add to a TO in order to get a FROM. They are named
8358 this way because we call this function to find out how to convert from
8359 a pointer to member of FROM to a pointer to member of TO. */
8361 static tree
8362 get_delta_difference (tree from, tree to,
8363 bool allow_inverse_p,
8364 bool c_cast_p, tsubst_flags_t complain)
8366 tree result;
8368 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8369 /* Pointer to member of incomplete class is permitted*/
8370 result = size_zero_node;
8371 else
8372 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8374 if (result == error_mark_node)
8375 return error_mark_node;
8377 if (!result)
8379 if (!allow_inverse_p)
8381 if (!(complain & tf_error))
8382 return error_mark_node;
8384 error_not_base_type (from, to);
8385 error (" in pointer to member conversion");
8386 result = size_zero_node;
8388 else
8390 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8392 if (result == error_mark_node)
8393 return error_mark_node;
8395 if (result)
8396 result = size_diffop_loc (input_location,
8397 size_zero_node, result);
8398 else
8400 if (!(complain & tf_error))
8401 return error_mark_node;
8403 error_not_base_type (from, to);
8404 error (" in pointer to member conversion");
8405 result = size_zero_node;
8410 return convert_to_integer (ptrdiff_type_node, result);
8413 /* Return a constructor for the pointer-to-member-function TYPE using
8414 the other components as specified. */
8416 tree
8417 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8419 tree u = NULL_TREE;
8420 tree delta_field;
8421 tree pfn_field;
8422 vec<constructor_elt, va_gc> *v;
8424 /* Pull the FIELD_DECLs out of the type. */
8425 pfn_field = TYPE_FIELDS (type);
8426 delta_field = DECL_CHAIN (pfn_field);
8428 /* Make sure DELTA has the type we want. */
8429 delta = convert_and_check (input_location, delta_type_node, delta);
8431 /* Convert to the correct target type if necessary. */
8432 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8434 /* Finish creating the initializer. */
8435 vec_alloc (v, 2);
8436 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8437 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8438 u = build_constructor (type, v);
8439 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8440 TREE_STATIC (u) = (TREE_CONSTANT (u)
8441 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8442 != NULL_TREE)
8443 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8444 != NULL_TREE));
8445 return u;
8448 /* Build a constructor for a pointer to member function. It can be
8449 used to initialize global variables, local variable, or used
8450 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8451 want to be.
8453 If FORCE is nonzero, then force this conversion, even if
8454 we would rather not do it. Usually set when using an explicit
8455 cast. A C-style cast is being processed iff C_CAST_P is true.
8457 Return error_mark_node, if something goes wrong. */
8459 tree
8460 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8461 tsubst_flags_t complain)
8463 tree fn;
8464 tree pfn_type;
8465 tree to_type;
8467 if (error_operand_p (pfn))
8468 return error_mark_node;
8470 pfn_type = TREE_TYPE (pfn);
8471 to_type = build_ptrmemfunc_type (type);
8473 /* Handle multiple conversions of pointer to member functions. */
8474 if (TYPE_PTRMEMFUNC_P (pfn_type))
8476 tree delta = NULL_TREE;
8477 tree npfn = NULL_TREE;
8478 tree n;
8480 if (!force
8481 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8482 LOOKUP_NORMAL, complain))
8484 if (complain & tf_error)
8485 error ("invalid conversion to type %qT from type %qT",
8486 to_type, pfn_type);
8487 else
8488 return error_mark_node;
8491 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8492 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8493 force,
8494 c_cast_p, complain);
8495 if (n == error_mark_node)
8496 return error_mark_node;
8498 /* We don't have to do any conversion to convert a
8499 pointer-to-member to its own type. But, we don't want to
8500 just return a PTRMEM_CST if there's an explicit cast; that
8501 cast should make the expression an invalid template argument. */
8502 if (TREE_CODE (pfn) != PTRMEM_CST)
8504 if (same_type_p (to_type, pfn_type))
8505 return pfn;
8506 else if (integer_zerop (n))
8507 return build_reinterpret_cast (to_type, pfn,
8508 complain);
8511 if (TREE_SIDE_EFFECTS (pfn))
8512 pfn = save_expr (pfn);
8514 /* Obtain the function pointer and the current DELTA. */
8515 if (TREE_CODE (pfn) == PTRMEM_CST)
8516 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8517 else
8519 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8520 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8523 /* Just adjust the DELTA field. */
8524 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8525 (TREE_TYPE (delta), ptrdiff_type_node));
8526 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8527 n = cp_build_binary_op (input_location,
8528 LSHIFT_EXPR, n, integer_one_node,
8529 complain);
8530 delta = cp_build_binary_op (input_location,
8531 PLUS_EXPR, delta, n, complain);
8532 return build_ptrmemfunc1 (to_type, delta, npfn);
8535 /* Handle null pointer to member function conversions. */
8536 if (null_ptr_cst_p (pfn))
8538 pfn = cp_build_c_cast (type, pfn, complain);
8539 return build_ptrmemfunc1 (to_type,
8540 integer_zero_node,
8541 pfn);
8544 if (type_unknown_p (pfn))
8545 return instantiate_type (type, pfn, complain);
8547 fn = TREE_OPERAND (pfn, 0);
8548 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8549 /* In a template, we will have preserved the
8550 OFFSET_REF. */
8551 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8552 return make_ptrmem_cst (to_type, fn);
8555 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8556 given by CST.
8558 ??? There is no consistency as to the types returned for the above
8559 values. Some code acts as if it were a sizetype and some as if it were
8560 integer_type_node. */
8562 void
8563 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8565 tree type = TREE_TYPE (cst);
8566 tree fn = PTRMEM_CST_MEMBER (cst);
8567 tree ptr_class, fn_class;
8569 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8571 /* The class that the function belongs to. */
8572 fn_class = DECL_CONTEXT (fn);
8574 /* The class that we're creating a pointer to member of. */
8575 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8577 /* First, calculate the adjustment to the function's class. */
8578 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8579 /*c_cast_p=*/0, tf_warning_or_error);
8581 if (!DECL_VIRTUAL_P (fn))
8582 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8583 build_addr_func (fn, tf_warning_or_error));
8584 else
8586 /* If we're dealing with a virtual function, we have to adjust 'this'
8587 again, to point to the base which provides the vtable entry for
8588 fn; the call will do the opposite adjustment. */
8589 tree orig_class = DECL_CONTEXT (fn);
8590 tree binfo = binfo_or_else (orig_class, fn_class);
8591 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8592 *delta, BINFO_OFFSET (binfo));
8594 /* We set PFN to the vtable offset at which the function can be
8595 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8596 case delta is shifted left, and then incremented). */
8597 *pfn = DECL_VINDEX (fn);
8598 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8599 TYPE_SIZE_UNIT (vtable_entry_type));
8601 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8603 case ptrmemfunc_vbit_in_pfn:
8604 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8605 integer_one_node);
8606 break;
8608 case ptrmemfunc_vbit_in_delta:
8609 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8610 *delta, integer_one_node);
8611 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8612 *delta, integer_one_node);
8613 break;
8615 default:
8616 gcc_unreachable ();
8619 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8623 /* Return an expression for PFN from the pointer-to-member function
8624 given by T. */
8626 static tree
8627 pfn_from_ptrmemfunc (tree t)
8629 if (TREE_CODE (t) == PTRMEM_CST)
8631 tree delta;
8632 tree pfn;
8634 expand_ptrmemfunc_cst (t, &delta, &pfn);
8635 if (pfn)
8636 return pfn;
8639 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8642 /* Return an expression for DELTA from the pointer-to-member function
8643 given by T. */
8645 static tree
8646 delta_from_ptrmemfunc (tree t)
8648 if (TREE_CODE (t) == PTRMEM_CST)
8650 tree delta;
8651 tree pfn;
8653 expand_ptrmemfunc_cst (t, &delta, &pfn);
8654 if (delta)
8655 return delta;
8658 return build_ptrmemfunc_access_expr (t, delta_identifier);
8661 /* Convert value RHS to type TYPE as preparation for an assignment to
8662 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8663 implicit conversion is. If FNDECL is non-NULL, we are doing the
8664 conversion in order to pass the PARMNUMth argument of FNDECL.
8665 If FNDECL is NULL, we are doing the conversion in function pointer
8666 argument passing, conversion in initialization, etc. */
8668 static tree
8669 convert_for_assignment (tree type, tree rhs,
8670 impl_conv_rhs errtype, tree fndecl, int parmnum,
8671 tsubst_flags_t complain, int flags)
8673 tree rhstype;
8674 enum tree_code coder;
8676 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8677 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8678 rhs = TREE_OPERAND (rhs, 0);
8680 /* Handle [dcl.init.list] direct-list-initialization from
8681 single element of enumeration with a fixed underlying type. */
8682 if (is_direct_enum_init (type, rhs))
8684 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8685 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8687 warning_sentinel w (warn_useless_cast);
8688 rhs = cp_build_c_cast (type, elt, complain);
8690 else
8691 rhs = error_mark_node;
8694 rhstype = TREE_TYPE (rhs);
8695 coder = TREE_CODE (rhstype);
8697 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8698 && vector_types_convertible_p (type, rhstype, true))
8700 rhs = mark_rvalue_use (rhs);
8701 return convert (type, rhs);
8704 if (rhs == error_mark_node || rhstype == error_mark_node)
8705 return error_mark_node;
8706 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8707 return error_mark_node;
8709 /* The RHS of an assignment cannot have void type. */
8710 if (coder == VOID_TYPE)
8712 if (complain & tf_error)
8713 error ("void value not ignored as it ought to be");
8714 return error_mark_node;
8717 if (c_dialect_objc ())
8719 int parmno;
8720 tree selector;
8721 tree rname = fndecl;
8723 switch (errtype)
8725 case ICR_ASSIGN:
8726 parmno = -1;
8727 break;
8728 case ICR_INIT:
8729 parmno = -2;
8730 break;
8731 default:
8732 selector = objc_message_selector ();
8733 parmno = parmnum;
8734 if (selector && parmno > 1)
8736 rname = selector;
8737 parmno -= 1;
8741 if (objc_compare_types (type, rhstype, parmno, rname))
8743 rhs = mark_rvalue_use (rhs);
8744 return convert (type, rhs);
8748 /* [expr.ass]
8750 The expression is implicitly converted (clause _conv_) to the
8751 cv-unqualified type of the left operand.
8753 We allow bad conversions here because by the time we get to this point
8754 we are committed to doing the conversion. If we end up doing a bad
8755 conversion, convert_like will complain. */
8756 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8758 /* When -Wno-pmf-conversions is use, we just silently allow
8759 conversions from pointers-to-members to plain pointers. If
8760 the conversion doesn't work, cp_convert will complain. */
8761 if (!warn_pmf2ptr
8762 && TYPE_PTR_P (type)
8763 && TYPE_PTRMEMFUNC_P (rhstype))
8764 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8765 else
8767 if (complain & tf_error)
8769 /* If the right-hand side has unknown type, then it is an
8770 overloaded function. Call instantiate_type to get error
8771 messages. */
8772 if (rhstype == unknown_type_node)
8774 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8775 /* -fpermissive might allow this; recurse. */
8776 if (!seen_error ())
8777 return convert_for_assignment (type, r, errtype, fndecl,
8778 parmnum, complain, flags);
8780 else if (fndecl)
8781 error ("cannot convert %qH to %qI for argument %qP to %qD",
8782 rhstype, type, parmnum, fndecl);
8783 else
8784 switch (errtype)
8786 case ICR_DEFAULT_ARGUMENT:
8787 error ("cannot convert %qH to %qI in default argument",
8788 rhstype, type);
8789 break;
8790 case ICR_ARGPASS:
8791 error ("cannot convert %qH to %qI in argument passing",
8792 rhstype, type);
8793 break;
8794 case ICR_CONVERTING:
8795 error ("cannot convert %qH to %qI",
8796 rhstype, type);
8797 break;
8798 case ICR_INIT:
8799 error ("cannot convert %qH to %qI in initialization",
8800 rhstype, type);
8801 break;
8802 case ICR_RETURN:
8803 error ("cannot convert %qH to %qI in return",
8804 rhstype, type);
8805 break;
8806 case ICR_ASSIGN:
8807 error ("cannot convert %qH to %qI in assignment",
8808 rhstype, type);
8809 break;
8810 default:
8811 gcc_unreachable();
8813 if (TYPE_PTR_P (rhstype)
8814 && TYPE_PTR_P (type)
8815 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8816 && CLASS_TYPE_P (TREE_TYPE (type))
8817 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8818 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8819 (TREE_TYPE (rhstype))),
8820 "class type %qT is incomplete", TREE_TYPE (rhstype));
8822 return error_mark_node;
8825 if (warn_suggest_attribute_format)
8827 const enum tree_code codel = TREE_CODE (type);
8828 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8829 && coder == codel
8830 && check_missing_format_attribute (type, rhstype)
8831 && (complain & tf_warning))
8832 switch (errtype)
8834 case ICR_ARGPASS:
8835 case ICR_DEFAULT_ARGUMENT:
8836 if (fndecl)
8837 warning (OPT_Wsuggest_attribute_format,
8838 "parameter %qP of %qD might be a candidate "
8839 "for a format attribute", parmnum, fndecl);
8840 else
8841 warning (OPT_Wsuggest_attribute_format,
8842 "parameter might be a candidate "
8843 "for a format attribute");
8844 break;
8845 case ICR_CONVERTING:
8846 warning (OPT_Wsuggest_attribute_format,
8847 "target of conversion might be a candidate "
8848 "for a format attribute");
8849 break;
8850 case ICR_INIT:
8851 warning (OPT_Wsuggest_attribute_format,
8852 "target of initialization might be a candidate "
8853 "for a format attribute");
8854 break;
8855 case ICR_RETURN:
8856 warning (OPT_Wsuggest_attribute_format,
8857 "return type might be a candidate "
8858 "for a format attribute");
8859 break;
8860 case ICR_ASSIGN:
8861 warning (OPT_Wsuggest_attribute_format,
8862 "left-hand side of assignment might be a candidate "
8863 "for a format attribute");
8864 break;
8865 default:
8866 gcc_unreachable();
8870 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8871 does not. */
8872 if (warn_parentheses
8873 && TREE_CODE (type) == BOOLEAN_TYPE
8874 && TREE_CODE (rhs) == MODIFY_EXPR
8875 && !TREE_NO_WARNING (rhs)
8876 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8877 && (complain & tf_warning))
8879 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8881 warning_at (loc, OPT_Wparentheses,
8882 "suggest parentheses around assignment used as truth value");
8883 TREE_NO_WARNING (rhs) = 1;
8886 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8887 complain, flags);
8890 /* Convert RHS to be of type TYPE.
8891 If EXP is nonzero, it is the target of the initialization.
8892 ERRTYPE indicates what kind of error the implicit conversion is.
8894 Two major differences between the behavior of
8895 `convert_for_assignment' and `convert_for_initialization'
8896 are that references are bashed in the former, while
8897 copied in the latter, and aggregates are assigned in
8898 the former (operator=) while initialized in the
8899 latter (X(X&)).
8901 If using constructor make sure no conversion operator exists, if one does
8902 exist, an ambiguity exists. */
8904 tree
8905 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8906 impl_conv_rhs errtype, tree fndecl, int parmnum,
8907 tsubst_flags_t complain)
8909 enum tree_code codel = TREE_CODE (type);
8910 tree rhstype;
8911 enum tree_code coder;
8913 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8914 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8915 if (TREE_CODE (rhs) == NOP_EXPR
8916 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8917 && codel != REFERENCE_TYPE)
8918 rhs = TREE_OPERAND (rhs, 0);
8920 if (type == error_mark_node
8921 || rhs == error_mark_node
8922 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8923 return error_mark_node;
8925 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8927 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8928 && TREE_CODE (type) != ARRAY_TYPE
8929 && (TREE_CODE (type) != REFERENCE_TYPE
8930 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8931 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8932 && !TYPE_REFFN_P (type))
8933 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8934 rhs = decay_conversion (rhs, complain);
8936 rhstype = TREE_TYPE (rhs);
8937 coder = TREE_CODE (rhstype);
8939 if (coder == ERROR_MARK)
8940 return error_mark_node;
8942 /* We accept references to incomplete types, so we can
8943 return here before checking if RHS is of complete type. */
8945 if (codel == REFERENCE_TYPE)
8947 /* This should eventually happen in convert_arguments. */
8948 int savew = 0, savee = 0;
8950 if (fndecl)
8951 savew = warningcount + werrorcount, savee = errorcount;
8952 rhs = initialize_reference (type, rhs, flags, complain);
8954 if (fndecl
8955 && (warningcount + werrorcount > savew || errorcount > savee))
8956 inform (DECL_SOURCE_LOCATION (fndecl),
8957 "in passing argument %P of %qD", parmnum, fndecl);
8959 return rhs;
8962 if (exp != 0)
8963 exp = require_complete_type_sfinae (exp, complain);
8964 if (exp == error_mark_node)
8965 return error_mark_node;
8967 rhstype = non_reference (rhstype);
8969 type = complete_type (type);
8971 if (DIRECT_INIT_EXPR_P (type, rhs))
8972 /* Don't try to do copy-initialization if we already have
8973 direct-initialization. */
8974 return rhs;
8976 if (MAYBE_CLASS_TYPE_P (type))
8977 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8979 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8980 complain, flags);
8983 /* If RETVAL is the address of, or a reference to, a local variable or
8984 temporary give an appropriate warning and return true. */
8986 static bool
8987 maybe_warn_about_returning_address_of_local (tree retval)
8989 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8990 tree whats_returned = fold_for_warn (retval);
8992 for (;;)
8994 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8995 whats_returned = TREE_OPERAND (whats_returned, 1);
8996 else if (CONVERT_EXPR_P (whats_returned)
8997 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8998 whats_returned = TREE_OPERAND (whats_returned, 0);
8999 else
9000 break;
9003 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9004 return false;
9005 whats_returned = TREE_OPERAND (whats_returned, 0);
9007 while (TREE_CODE (whats_returned) == COMPONENT_REF
9008 || TREE_CODE (whats_returned) == ARRAY_REF)
9009 whats_returned = TREE_OPERAND (whats_returned, 0);
9011 if (TREE_CODE (valtype) == REFERENCE_TYPE)
9013 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9014 || TREE_CODE (whats_returned) == TARGET_EXPR)
9016 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
9017 return true;
9019 if (VAR_P (whats_returned)
9020 && DECL_NAME (whats_returned)
9021 && TEMP_NAME_P (DECL_NAME (whats_returned)))
9023 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
9024 return true;
9028 if (DECL_P (whats_returned)
9029 && DECL_NAME (whats_returned)
9030 && DECL_FUNCTION_SCOPE_P (whats_returned)
9031 && !is_capture_proxy (whats_returned)
9032 && !(TREE_STATIC (whats_returned)
9033 || TREE_PUBLIC (whats_returned)))
9035 if (TREE_CODE (valtype) == REFERENCE_TYPE)
9036 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9037 OPT_Wreturn_local_addr,
9038 "reference to local variable %qD returned",
9039 whats_returned);
9040 else if (TREE_CODE (whats_returned) == LABEL_DECL)
9041 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9042 OPT_Wreturn_local_addr, "address of label %qD returned",
9043 whats_returned);
9044 else
9045 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9046 OPT_Wreturn_local_addr, "address of local variable %qD "
9047 "returned", whats_returned);
9048 return true;
9051 return false;
9054 /* Check that returning RETVAL from the current function is valid.
9055 Return an expression explicitly showing all conversions required to
9056 change RETVAL into the function return type, and to assign it to
9057 the DECL_RESULT for the function. Set *NO_WARNING to true if
9058 code reaches end of non-void function warning shouldn't be issued
9059 on this RETURN_EXPR. */
9061 tree
9062 check_return_expr (tree retval, bool *no_warning)
9064 tree result;
9065 /* The type actually returned by the function. */
9066 tree valtype;
9067 /* The type the function is declared to return, or void if
9068 the declared type is incomplete. */
9069 tree functype;
9070 int fn_returns_value_p;
9071 bool named_return_value_okay_p;
9073 *no_warning = false;
9075 /* A `volatile' function is one that isn't supposed to return, ever.
9076 (This is a G++ extension, used to get better code for functions
9077 that call the `volatile' function.) */
9078 if (TREE_THIS_VOLATILE (current_function_decl))
9079 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9081 /* Check for various simple errors. */
9082 if (DECL_DESTRUCTOR_P (current_function_decl))
9084 if (retval)
9085 error ("returning a value from a destructor");
9086 return NULL_TREE;
9088 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9090 if (in_function_try_handler)
9091 /* If a return statement appears in a handler of the
9092 function-try-block of a constructor, the program is ill-formed. */
9093 error ("cannot return from a handler of a function-try-block of a constructor");
9094 else if (retval)
9095 /* You can't return a value from a constructor. */
9096 error ("returning a value from a constructor");
9097 return NULL_TREE;
9100 const tree saved_retval = retval;
9102 if (processing_template_decl)
9104 current_function_returns_value = 1;
9106 if (check_for_bare_parameter_packs (retval))
9107 return error_mark_node;
9109 /* If one of the types might be void, we can't tell whether we're
9110 returning a value. */
9111 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9112 && !current_function_auto_return_pattern)
9113 || (retval != NULL_TREE
9114 && (TREE_TYPE (retval) == NULL_TREE
9115 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9116 goto dependent;
9119 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9121 /* Deduce auto return type from a return statement. */
9122 if (current_function_auto_return_pattern)
9124 tree auto_node;
9125 tree type;
9127 if (!retval && !is_auto (current_function_auto_return_pattern))
9129 /* Give a helpful error message. */
9130 error ("return-statement with no value, in function returning %qT",
9131 current_function_auto_return_pattern);
9132 inform (input_location, "only plain %<auto%> return type can be "
9133 "deduced to %<void%>");
9134 type = error_mark_node;
9136 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9138 error ("returning initializer list");
9139 type = error_mark_node;
9141 else
9143 if (!retval)
9144 retval = void_node;
9145 auto_node = type_uses_auto (current_function_auto_return_pattern);
9146 type = do_auto_deduction (current_function_auto_return_pattern,
9147 retval, auto_node);
9150 if (type == error_mark_node)
9151 /* Leave it. */;
9152 else if (functype == current_function_auto_return_pattern)
9153 apply_deduced_return_type (current_function_decl, type);
9154 else if (!same_type_p (type, functype))
9156 if (LAMBDA_FUNCTION_P (current_function_decl))
9157 error ("inconsistent types %qT and %qT deduced for "
9158 "lambda return type", functype, type);
9159 else
9160 error ("inconsistent deduction for auto return type: "
9161 "%qT and then %qT", functype, type);
9163 functype = type;
9166 result = DECL_RESULT (current_function_decl);
9167 valtype = TREE_TYPE (result);
9168 gcc_assert (valtype != NULL_TREE);
9169 fn_returns_value_p = !VOID_TYPE_P (valtype);
9171 /* Check for a return statement with no return value in a function
9172 that's supposed to return a value. */
9173 if (!retval && fn_returns_value_p)
9175 if (functype != error_mark_node)
9176 permerror (input_location, "return-statement with no value, in "
9177 "function returning %qT", valtype);
9178 /* Remember that this function did return. */
9179 current_function_returns_value = 1;
9180 /* And signal caller that TREE_NO_WARNING should be set on the
9181 RETURN_EXPR to avoid control reaches end of non-void function
9182 warnings in tree-cfg.c. */
9183 *no_warning = true;
9185 /* Check for a return statement with a value in a function that
9186 isn't supposed to return a value. */
9187 else if (retval && !fn_returns_value_p)
9189 if (VOID_TYPE_P (TREE_TYPE (retval)))
9190 /* You can return a `void' value from a function of `void'
9191 type. In that case, we have to evaluate the expression for
9192 its side-effects. */
9193 finish_expr_stmt (retval);
9194 else
9195 permerror (input_location,
9196 "return-statement with a value, in function "
9197 "returning %qT", valtype);
9198 current_function_returns_null = 1;
9200 /* There's really no value to return, after all. */
9201 return NULL_TREE;
9203 else if (!retval)
9204 /* Remember that this function can sometimes return without a
9205 value. */
9206 current_function_returns_null = 1;
9207 else
9208 /* Remember that this function did return a value. */
9209 current_function_returns_value = 1;
9211 /* Check for erroneous operands -- but after giving ourselves a
9212 chance to provide an error about returning a value from a void
9213 function. */
9214 if (error_operand_p (retval))
9216 current_function_return_value = error_mark_node;
9217 return error_mark_node;
9220 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9221 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9222 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9223 && ! flag_check_new
9224 && retval && null_ptr_cst_p (retval))
9225 warning (0, "%<operator new%> must not return NULL unless it is "
9226 "declared %<throw()%> (or -fcheck-new is in effect)");
9228 /* Effective C++ rule 15. See also start_function. */
9229 if (warn_ecpp
9230 && DECL_NAME (current_function_decl) == assign_op_identifier)
9232 bool warn = true;
9234 /* The function return type must be a reference to the current
9235 class. */
9236 if (TREE_CODE (valtype) == REFERENCE_TYPE
9237 && same_type_ignoring_top_level_qualifiers_p
9238 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9240 /* Returning '*this' is obviously OK. */
9241 if (retval == current_class_ref)
9242 warn = false;
9243 /* If we are calling a function whose return type is the same of
9244 the current class reference, it is ok. */
9245 else if (INDIRECT_REF_P (retval)
9246 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9247 warn = false;
9250 if (warn)
9251 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9254 if (dependent_type_p (functype)
9255 || type_dependent_expression_p (retval))
9257 dependent:
9258 /* We should not have changed the return value. */
9259 gcc_assert (retval == saved_retval);
9260 return retval;
9263 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9265 [...] For a function with a class return type, if the expression
9266 in the return statement is the name of a local object, and the cv-
9267 unqualified type of the local object is the same as the function
9268 return type, an implementation is permitted to omit creating the tem-
9269 porary object to hold the function return value [...]
9271 So, if this is a value-returning function that always returns the same
9272 local variable, remember it.
9274 It might be nice to be more flexible, and choose the first suitable
9275 variable even if the function sometimes returns something else, but
9276 then we run the risk of clobbering the variable we chose if the other
9277 returned expression uses the chosen variable somehow. And people expect
9278 this restriction, anyway. (jason 2000-11-19)
9280 See finish_function and finalize_nrv for the rest of this optimization. */
9282 named_return_value_okay_p =
9283 (retval != NULL_TREE
9284 && !processing_template_decl
9285 /* Must be a local, automatic variable. */
9286 && VAR_P (retval)
9287 && DECL_CONTEXT (retval) == current_function_decl
9288 && ! TREE_STATIC (retval)
9289 /* And not a lambda or anonymous union proxy. */
9290 && !DECL_HAS_VALUE_EXPR_P (retval)
9291 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9292 /* The cv-unqualified type of the returned value must be the
9293 same as the cv-unqualified return type of the
9294 function. */
9295 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9296 (TYPE_MAIN_VARIANT (functype)))
9297 /* And the returned value must be non-volatile. */
9298 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9300 if (fn_returns_value_p && flag_elide_constructors)
9302 if (named_return_value_okay_p
9303 && (current_function_return_value == NULL_TREE
9304 || current_function_return_value == retval))
9305 current_function_return_value = retval;
9306 else
9307 current_function_return_value = error_mark_node;
9310 /* We don't need to do any conversions when there's nothing being
9311 returned. */
9312 if (!retval)
9313 return NULL_TREE;
9315 /* Do any required conversions. */
9316 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9317 /* No conversions are required. */
9319 else
9321 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9323 /* The functype's return type will have been set to void, if it
9324 was an incomplete type. Just treat this as 'return;' */
9325 if (VOID_TYPE_P (functype))
9326 return error_mark_node;
9328 /* If we had an id-expression obfuscated by force_paren_expr, we need
9329 to undo it so we can try to treat it as an rvalue below. */
9330 retval = maybe_undo_parenthesized_ref (retval);
9332 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9333 treated as an rvalue for the purposes of overload resolution to
9334 favor move constructors over copy constructors.
9336 Note that these conditions are similar to, but not as strict as,
9337 the conditions for the named return value optimization. */
9338 bool converted = false;
9339 if ((cxx_dialect != cxx98)
9340 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9341 || TREE_CODE (retval) == PARM_DECL)
9342 && DECL_CONTEXT (retval) == current_function_decl
9343 && !TREE_STATIC (retval)
9344 /* This is only interesting for class type. */
9345 && CLASS_TYPE_P (functype))
9347 tree moved = move (retval);
9348 moved = convert_for_initialization
9349 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9350 ICR_RETURN, NULL_TREE, 0, tf_none);
9351 if (moved != error_mark_node)
9353 retval = moved;
9354 converted = true;
9358 /* First convert the value to the function's return type, then
9359 to the type of return value's location to handle the
9360 case that functype is smaller than the valtype. */
9361 if (!converted)
9362 retval = convert_for_initialization
9363 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9364 tf_warning_or_error);
9365 retval = convert (valtype, retval);
9367 /* If the conversion failed, treat this just like `return;'. */
9368 if (retval == error_mark_node)
9369 return retval;
9370 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9371 else if (! cfun->returns_struct
9372 && TREE_CODE (retval) == TARGET_EXPR
9373 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9374 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9375 TREE_OPERAND (retval, 0));
9376 else if (!processing_template_decl
9377 && maybe_warn_about_returning_address_of_local (retval))
9378 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9379 build_zero_cst (TREE_TYPE (retval)));
9382 if (processing_template_decl)
9383 return saved_retval;
9385 /* Actually copy the value returned into the appropriate location. */
9386 if (retval && retval != result)
9387 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9389 return retval;
9393 /* Returns nonzero if the pointer-type FROM can be converted to the
9394 pointer-type TO via a qualification conversion. If CONSTP is -1,
9395 then we return nonzero if the pointers are similar, and the
9396 cv-qualification signature of FROM is a proper subset of that of TO.
9398 If CONSTP is positive, then all outer pointers have been
9399 const-qualified. */
9401 static int
9402 comp_ptr_ttypes_real (tree to, tree from, int constp)
9404 bool to_more_cv_qualified = false;
9405 bool is_opaque_pointer = false;
9407 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9409 if (TREE_CODE (to) != TREE_CODE (from))
9410 return 0;
9412 if (TREE_CODE (from) == OFFSET_TYPE
9413 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9414 TYPE_OFFSET_BASETYPE (to)))
9415 return 0;
9417 /* Const and volatile mean something different for function types,
9418 so the usual checks are not appropriate. */
9419 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9421 if (!at_least_as_qualified_p (to, from))
9422 return 0;
9424 if (!at_least_as_qualified_p (from, to))
9426 if (constp == 0)
9427 return 0;
9428 to_more_cv_qualified = true;
9431 if (constp > 0)
9432 constp &= TYPE_READONLY (to);
9435 if (VECTOR_TYPE_P (to))
9436 is_opaque_pointer = vector_targets_convertible_p (to, from);
9438 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9439 return ((constp >= 0 || to_more_cv_qualified)
9440 && (is_opaque_pointer
9441 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9445 /* When comparing, say, char ** to char const **, this function takes
9446 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9447 types to this function. */
9450 comp_ptr_ttypes (tree to, tree from)
9452 return comp_ptr_ttypes_real (to, from, 1);
9455 /* Returns true iff FNTYPE is a non-class type that involves
9456 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9457 if a parameter type is ill-formed. */
9459 bool
9460 error_type_p (const_tree type)
9462 tree t;
9464 switch (TREE_CODE (type))
9466 case ERROR_MARK:
9467 return true;
9469 case POINTER_TYPE:
9470 case REFERENCE_TYPE:
9471 case OFFSET_TYPE:
9472 return error_type_p (TREE_TYPE (type));
9474 case FUNCTION_TYPE:
9475 case METHOD_TYPE:
9476 if (error_type_p (TREE_TYPE (type)))
9477 return true;
9478 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9479 if (error_type_p (TREE_VALUE (t)))
9480 return true;
9481 return false;
9483 case RECORD_TYPE:
9484 if (TYPE_PTRMEMFUNC_P (type))
9485 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9486 return false;
9488 default:
9489 return false;
9493 /* Returns true if to and from are (possibly multi-level) pointers to the same
9494 type or inheritance-related types, regardless of cv-quals. */
9496 bool
9497 ptr_reasonably_similar (const_tree to, const_tree from)
9499 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9501 /* Any target type is similar enough to void. */
9502 if (VOID_TYPE_P (to))
9503 return !error_type_p (from);
9504 if (VOID_TYPE_P (from))
9505 return !error_type_p (to);
9507 if (TREE_CODE (to) != TREE_CODE (from))
9508 return false;
9510 if (TREE_CODE (from) == OFFSET_TYPE
9511 && comptypes (TYPE_OFFSET_BASETYPE (to),
9512 TYPE_OFFSET_BASETYPE (from),
9513 COMPARE_BASE | COMPARE_DERIVED))
9514 continue;
9516 if (VECTOR_TYPE_P (to)
9517 && vector_types_convertible_p (to, from, false))
9518 return true;
9520 if (TREE_CODE (to) == INTEGER_TYPE
9521 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9522 return true;
9524 if (TREE_CODE (to) == FUNCTION_TYPE)
9525 return !error_type_p (to) && !error_type_p (from);
9527 if (!TYPE_PTR_P (to))
9529 /* When either type is incomplete avoid DERIVED_FROM_P,
9530 which may call complete_type (c++/57942). */
9531 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9532 return comptypes
9533 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9534 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9539 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9540 pointer-to-member types) are the same, ignoring cv-qualification at
9541 all levels. */
9543 bool
9544 comp_ptr_ttypes_const (tree to, tree from)
9546 bool is_opaque_pointer = false;
9548 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9550 if (TREE_CODE (to) != TREE_CODE (from))
9551 return false;
9553 if (TREE_CODE (from) == OFFSET_TYPE
9554 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9555 TYPE_OFFSET_BASETYPE (to)))
9556 continue;
9558 if (VECTOR_TYPE_P (to))
9559 is_opaque_pointer = vector_targets_convertible_p (to, from);
9561 if (!TYPE_PTR_P (to))
9562 return (is_opaque_pointer
9563 || same_type_ignoring_top_level_qualifiers_p (to, from));
9567 /* Returns the type qualifiers for this type, including the qualifiers on the
9568 elements for an array type. */
9571 cp_type_quals (const_tree type)
9573 int quals;
9574 /* This CONST_CAST is okay because strip_array_types returns its
9575 argument unmodified and we assign it to a const_tree. */
9576 type = strip_array_types (CONST_CAST_TREE (type));
9577 if (type == error_mark_node
9578 /* Quals on a FUNCTION_TYPE are memfn quals. */
9579 || TREE_CODE (type) == FUNCTION_TYPE)
9580 return TYPE_UNQUALIFIED;
9581 quals = TYPE_QUALS (type);
9582 /* METHOD and REFERENCE_TYPEs should never have quals. */
9583 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9584 && TREE_CODE (type) != REFERENCE_TYPE)
9585 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9586 == TYPE_UNQUALIFIED));
9587 return quals;
9590 /* Returns the function-ref-qualifier for TYPE */
9592 cp_ref_qualifier
9593 type_memfn_rqual (const_tree type)
9595 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9596 || TREE_CODE (type) == METHOD_TYPE);
9598 if (!FUNCTION_REF_QUALIFIED (type))
9599 return REF_QUAL_NONE;
9600 else if (FUNCTION_RVALUE_QUALIFIED (type))
9601 return REF_QUAL_RVALUE;
9602 else
9603 return REF_QUAL_LVALUE;
9606 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9607 METHOD_TYPE. */
9610 type_memfn_quals (const_tree type)
9612 if (TREE_CODE (type) == FUNCTION_TYPE)
9613 return TYPE_QUALS (type);
9614 else if (TREE_CODE (type) == METHOD_TYPE)
9615 return cp_type_quals (class_of_this_parm (type));
9616 else
9617 gcc_unreachable ();
9620 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9621 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9623 tree
9624 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9626 /* Could handle METHOD_TYPE here if necessary. */
9627 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9628 if (TYPE_QUALS (type) == memfn_quals
9629 && type_memfn_rqual (type) == rqual)
9630 return type;
9632 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9633 complex. */
9634 tree result = build_qualified_type (type, memfn_quals);
9635 return build_ref_qualified_type (result, rqual);
9638 /* Returns nonzero if TYPE is const or volatile. */
9640 bool
9641 cv_qualified_p (const_tree type)
9643 int quals = cp_type_quals (type);
9644 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9647 /* Returns nonzero if the TYPE contains a mutable member. */
9649 bool
9650 cp_has_mutable_p (const_tree type)
9652 /* This CONST_CAST is okay because strip_array_types returns its
9653 argument unmodified and we assign it to a const_tree. */
9654 type = strip_array_types (CONST_CAST_TREE(type));
9656 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9659 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9660 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9661 approximation. In particular, consider:
9663 int f();
9664 struct S { int i; };
9665 const S s = { f(); }
9667 Here, we will make "s" as TREE_READONLY (because it is declared
9668 "const") -- only to reverse ourselves upon seeing that the
9669 initializer is non-constant. */
9671 void
9672 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9674 tree type = TREE_TYPE (decl);
9676 if (type == error_mark_node)
9677 return;
9679 if (TREE_CODE (decl) == TYPE_DECL)
9680 return;
9682 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9683 && type_quals != TYPE_UNQUALIFIED));
9685 /* Avoid setting TREE_READONLY incorrectly. */
9686 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9687 constructor can produce constant init, so rely on cp_finish_decl to
9688 clear TREE_READONLY if the variable has non-constant init. */
9690 /* If the type has (or might have) a mutable component, that component
9691 might be modified. */
9692 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9693 type_quals &= ~TYPE_QUAL_CONST;
9695 c_apply_type_quals_to_decl (type_quals, decl);
9698 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9699 exemplar types such that casting T1 to T2 is casting away constness
9700 if and only if there is no implicit conversion from T1 to T2. */
9702 static void
9703 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9705 int quals1;
9706 int quals2;
9708 /* [expr.const.cast]
9710 For multi-level pointer to members and multi-level mixed pointers
9711 and pointers to members (conv.qual), the "member" aspect of a
9712 pointer to member level is ignored when determining if a const
9713 cv-qualifier has been cast away. */
9714 /* [expr.const.cast]
9716 For two pointer types:
9718 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9719 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9720 K is min(N,M)
9722 casting from X1 to X2 casts away constness if, for a non-pointer
9723 type T there does not exist an implicit conversion (clause
9724 _conv_) from:
9726 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9730 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9731 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9732 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9734 *t1 = cp_build_qualified_type (void_type_node,
9735 cp_type_quals (*t1));
9736 *t2 = cp_build_qualified_type (void_type_node,
9737 cp_type_quals (*t2));
9738 return;
9741 quals1 = cp_type_quals (*t1);
9742 quals2 = cp_type_quals (*t2);
9744 if (TYPE_PTRDATAMEM_P (*t1))
9745 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9746 else
9747 *t1 = TREE_TYPE (*t1);
9748 if (TYPE_PTRDATAMEM_P (*t2))
9749 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9750 else
9751 *t2 = TREE_TYPE (*t2);
9753 casts_away_constness_r (t1, t2, complain);
9754 *t1 = build_pointer_type (*t1);
9755 *t2 = build_pointer_type (*t2);
9756 *t1 = cp_build_qualified_type (*t1, quals1);
9757 *t2 = cp_build_qualified_type (*t2, quals2);
9760 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9761 constness.
9763 ??? This function returns non-zero if casting away qualifiers not
9764 just const. We would like to return to the caller exactly which
9765 qualifiers are casted away to give more accurate diagnostics.
9768 static bool
9769 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9771 if (TREE_CODE (t2) == REFERENCE_TYPE)
9773 /* [expr.const.cast]
9775 Casting from an lvalue of type T1 to an lvalue of type T2
9776 using a reference cast casts away constness if a cast from an
9777 rvalue of type "pointer to T1" to the type "pointer to T2"
9778 casts away constness. */
9779 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9780 return casts_away_constness (build_pointer_type (t1),
9781 build_pointer_type (TREE_TYPE (t2)),
9782 complain);
9785 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9786 /* [expr.const.cast]
9788 Casting from an rvalue of type "pointer to data member of X
9789 of type T1" to the type "pointer to data member of Y of type
9790 T2" casts away constness if a cast from an rvalue of type
9791 "pointer to T1" to the type "pointer to T2" casts away
9792 constness. */
9793 return casts_away_constness
9794 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9795 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9796 complain);
9798 /* Casting away constness is only something that makes sense for
9799 pointer or reference types. */
9800 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9801 return false;
9803 /* Top-level qualifiers don't matter. */
9804 t1 = TYPE_MAIN_VARIANT (t1);
9805 t2 = TYPE_MAIN_VARIANT (t2);
9806 casts_away_constness_r (&t1, &t2, complain);
9807 if (!can_convert (t2, t1, complain))
9808 return true;
9810 return false;
9813 /* If T is a REFERENCE_TYPE return the type to which T refers.
9814 Otherwise, return T itself. */
9816 tree
9817 non_reference (tree t)
9819 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9820 t = TREE_TYPE (t);
9821 return t;
9825 /* Return nonzero if REF is an lvalue valid for this language;
9826 otherwise, print an error message and return zero. USE says
9827 how the lvalue is being used and so selects the error message. */
9830 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9832 cp_lvalue_kind kind = lvalue_kind (ref);
9834 if (kind == clk_none)
9836 if (complain & tf_error)
9837 lvalue_error (input_location, use);
9838 return 0;
9840 else if (kind & (clk_rvalueref|clk_class))
9842 if (!(complain & tf_error))
9843 return 0;
9844 if (kind & clk_class)
9845 /* Make this a permerror because we used to accept it. */
9846 permerror (input_location, "using temporary as lvalue");
9847 else
9848 error ("using xvalue (rvalue reference) as lvalue");
9850 return 1;
9853 /* Return true if a user-defined literal operator is a raw operator. */
9855 bool
9856 check_raw_literal_operator (const_tree decl)
9858 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9859 tree argtype;
9860 int arity;
9861 bool maybe_raw_p = false;
9863 /* Count the number and type of arguments and check for ellipsis. */
9864 for (argtype = argtypes, arity = 0;
9865 argtype && argtype != void_list_node;
9866 ++arity, argtype = TREE_CHAIN (argtype))
9868 tree t = TREE_VALUE (argtype);
9870 if (same_type_p (t, const_string_type_node))
9871 maybe_raw_p = true;
9873 if (!argtype)
9874 return false; /* Found ellipsis. */
9876 if (!maybe_raw_p || arity != 1)
9877 return false;
9879 return true;
9883 /* Return true if a user-defined literal operator has one of the allowed
9884 argument types. */
9886 bool
9887 check_literal_operator_args (const_tree decl,
9888 bool *long_long_unsigned_p, bool *long_double_p)
9890 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9892 *long_long_unsigned_p = false;
9893 *long_double_p = false;
9894 if (processing_template_decl || processing_specialization)
9895 return argtypes == void_list_node;
9896 else
9898 tree argtype;
9899 int arity;
9900 int max_arity = 2;
9902 /* Count the number and type of arguments and check for ellipsis. */
9903 for (argtype = argtypes, arity = 0;
9904 argtype && argtype != void_list_node;
9905 argtype = TREE_CHAIN (argtype))
9907 tree t = TREE_VALUE (argtype);
9908 ++arity;
9910 if (TYPE_PTR_P (t))
9912 bool maybe_raw_p = false;
9913 t = TREE_TYPE (t);
9914 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9915 return false;
9916 t = TYPE_MAIN_VARIANT (t);
9917 if ((maybe_raw_p = same_type_p (t, char_type_node))
9918 || same_type_p (t, wchar_type_node)
9919 || same_type_p (t, char16_type_node)
9920 || same_type_p (t, char32_type_node))
9922 argtype = TREE_CHAIN (argtype);
9923 if (!argtype)
9924 return false;
9925 t = TREE_VALUE (argtype);
9926 if (maybe_raw_p && argtype == void_list_node)
9927 return true;
9928 else if (same_type_p (t, size_type_node))
9930 ++arity;
9931 continue;
9933 else
9934 return false;
9937 else if (same_type_p (t, long_long_unsigned_type_node))
9939 max_arity = 1;
9940 *long_long_unsigned_p = true;
9942 else if (same_type_p (t, long_double_type_node))
9944 max_arity = 1;
9945 *long_double_p = true;
9947 else if (same_type_p (t, char_type_node))
9948 max_arity = 1;
9949 else if (same_type_p (t, wchar_type_node))
9950 max_arity = 1;
9951 else if (same_type_p (t, char16_type_node))
9952 max_arity = 1;
9953 else if (same_type_p (t, char32_type_node))
9954 max_arity = 1;
9955 else
9956 return false;
9958 if (!argtype)
9959 return false; /* Found ellipsis. */
9961 if (arity != max_arity)
9962 return false;
9964 return true;
9968 /* Always returns false since unlike C90, C++ has no concept of implicit
9969 function declarations. */
9971 bool
9972 c_decl_implicit (const_tree)
9974 return false;