PR c++/84125
[official-gcc.git] / gcc / cp / typeck.c
blob83e767829986d614317f6777184c0344d11a5b50
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 STRIP_ANY_LOCATION_WRAPPER (exp);
2207 if (TREE_CODE (exp) == STRING_CST)
2209 /* Make sure that we don't try to convert between char and wide chars. */
2210 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2211 return 0;
2213 else
2215 /* Is this a string constant which has decayed to 'const char *'? */
2216 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2217 if (!same_type_p (TREE_TYPE (exp), t))
2218 return 0;
2219 STRIP_NOPS (exp);
2220 if (TREE_CODE (exp) != ADDR_EXPR
2221 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2222 return 0;
2224 if (warn)
2226 if (cxx_dialect >= cxx11)
2227 pedwarn (input_location, OPT_Wwrite_strings,
2228 "ISO C++ forbids converting a string constant to %qT",
2229 totype);
2230 else
2231 warning (OPT_Wwrite_strings,
2232 "deprecated conversion from string constant to %qT",
2233 totype);
2236 return 1;
2239 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2240 can, for example, use as an lvalue. This code used to be in
2241 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2242 expressions, where we're dealing with aggregates. But now it's again only
2243 called from unary_complex_lvalue. The case (in particular) that led to
2244 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2245 get it there. */
2247 static tree
2248 rationalize_conditional_expr (enum tree_code code, tree t,
2249 tsubst_flags_t complain)
2251 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2253 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2254 the first operand is always the one to be used if both operands
2255 are equal, so we know what conditional expression this used to be. */
2256 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2258 tree op0 = TREE_OPERAND (t, 0);
2259 tree op1 = TREE_OPERAND (t, 1);
2261 /* The following code is incorrect if either operand side-effects. */
2262 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2263 && !TREE_SIDE_EFFECTS (op1));
2264 return
2265 build_conditional_expr (loc,
2266 build_x_binary_op (loc,
2267 (TREE_CODE (t) == MIN_EXPR
2268 ? LE_EXPR : GE_EXPR),
2269 op0, TREE_CODE (op0),
2270 op1, TREE_CODE (op1),
2271 /*overload=*/NULL,
2272 complain),
2273 cp_build_unary_op (code, op0, false, complain),
2274 cp_build_unary_op (code, op1, false, complain),
2275 complain);
2278 return
2279 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2280 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2281 complain),
2282 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2283 complain),
2284 complain);
2287 /* Given the TYPE of an anonymous union field inside T, return the
2288 FIELD_DECL for the field. If not found return NULL_TREE. Because
2289 anonymous unions can nest, we must also search all anonymous unions
2290 that are directly reachable. */
2292 tree
2293 lookup_anon_field (tree t, tree type)
2295 tree field;
2297 t = TYPE_MAIN_VARIANT (t);
2299 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2301 if (TREE_STATIC (field))
2302 continue;
2303 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2304 continue;
2306 /* If we find it directly, return the field. */
2307 if (DECL_NAME (field) == NULL_TREE
2308 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2310 return field;
2313 /* Otherwise, it could be nested, search harder. */
2314 if (DECL_NAME (field) == NULL_TREE
2315 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2317 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2318 if (subfield)
2319 return subfield;
2322 return NULL_TREE;
2325 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2326 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2327 non-NULL, it indicates the path to the base used to name MEMBER.
2328 If PRESERVE_REFERENCE is true, the expression returned will have
2329 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2330 returned will have the type referred to by the reference.
2332 This function does not perform access control; that is either done
2333 earlier by the parser when the name of MEMBER is resolved to MEMBER
2334 itself, or later when overload resolution selects one of the
2335 functions indicated by MEMBER. */
2337 tree
2338 build_class_member_access_expr (cp_expr object, tree member,
2339 tree access_path, bool preserve_reference,
2340 tsubst_flags_t complain)
2342 tree object_type;
2343 tree member_scope;
2344 tree result = NULL_TREE;
2345 tree using_decl = NULL_TREE;
2347 if (error_operand_p (object) || error_operand_p (member))
2348 return error_mark_node;
2350 gcc_assert (DECL_P (member) || BASELINK_P (member));
2352 /* [expr.ref]
2354 The type of the first expression shall be "class object" (of a
2355 complete type). */
2356 object_type = TREE_TYPE (object);
2357 if (!currently_open_class (object_type)
2358 && !complete_type_or_maybe_complain (object_type, object, complain))
2359 return error_mark_node;
2360 if (!CLASS_TYPE_P (object_type))
2362 if (complain & tf_error)
2364 if (POINTER_TYPE_P (object_type)
2365 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2366 error ("request for member %qD in %qE, which is of pointer "
2367 "type %qT (maybe you meant to use %<->%> ?)",
2368 member, object.get_value (), object_type);
2369 else
2370 error ("request for member %qD in %qE, which is of non-class "
2371 "type %qT", member, object.get_value (), object_type);
2373 return error_mark_node;
2376 /* The standard does not seem to actually say that MEMBER must be a
2377 member of OBJECT_TYPE. However, that is clearly what is
2378 intended. */
2379 if (DECL_P (member))
2381 member_scope = DECL_CLASS_CONTEXT (member);
2382 if (!mark_used (member, complain) && !(complain & tf_error))
2383 return error_mark_node;
2384 if (TREE_DEPRECATED (member))
2385 warn_deprecated_use (member, NULL_TREE);
2387 else
2388 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2389 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2390 presently be the anonymous union. Go outwards until we find a
2391 type related to OBJECT_TYPE. */
2392 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2393 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2394 object_type))
2395 member_scope = TYPE_CONTEXT (member_scope);
2396 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2398 if (complain & tf_error)
2400 if (TREE_CODE (member) == FIELD_DECL)
2401 error ("invalid use of nonstatic data member %qE", member);
2402 else
2403 error ("%qD is not a member of %qT", member, object_type);
2405 return error_mark_node;
2408 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2409 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2410 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2412 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2413 if (temp)
2414 object = cp_build_fold_indirect_ref (temp);
2417 /* In [expr.ref], there is an explicit list of the valid choices for
2418 MEMBER. We check for each of those cases here. */
2419 if (VAR_P (member))
2421 /* A static data member. */
2422 result = member;
2423 mark_exp_read (object);
2424 /* If OBJECT has side-effects, they are supposed to occur. */
2425 if (TREE_SIDE_EFFECTS (object))
2426 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2428 else if (TREE_CODE (member) == FIELD_DECL)
2430 /* A non-static data member. */
2431 bool null_object_p;
2432 int type_quals;
2433 tree member_type;
2435 if (INDIRECT_REF_P (object))
2436 null_object_p =
2437 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2438 else
2439 null_object_p = false;
2441 /* Convert OBJECT to the type of MEMBER. */
2442 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2443 TYPE_MAIN_VARIANT (member_scope)))
2445 tree binfo;
2446 base_kind kind;
2448 binfo = lookup_base (access_path ? access_path : object_type,
2449 member_scope, ba_unique, &kind, complain);
2450 if (binfo == error_mark_node)
2451 return error_mark_node;
2453 /* It is invalid to try to get to a virtual base of a
2454 NULL object. The most common cause is invalid use of
2455 offsetof macro. */
2456 if (null_object_p && kind == bk_via_virtual)
2458 if (complain & tf_error)
2460 error ("invalid access to non-static data member %qD in "
2461 "virtual base of NULL object", member);
2463 return error_mark_node;
2466 /* Convert to the base. */
2467 object = build_base_path (PLUS_EXPR, object, binfo,
2468 /*nonnull=*/1, complain);
2469 /* If we found the base successfully then we should be able
2470 to convert to it successfully. */
2471 gcc_assert (object != error_mark_node);
2474 /* If MEMBER is from an anonymous aggregate, we have converted
2475 OBJECT so that it refers to the class containing the
2476 anonymous union. Generate a reference to the anonymous union
2477 itself, and recur to find MEMBER. */
2478 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2479 /* When this code is called from build_field_call, the
2480 object already has the type of the anonymous union.
2481 That is because the COMPONENT_REF was already
2482 constructed, and was then disassembled before calling
2483 build_field_call. After the function-call code is
2484 cleaned up, this waste can be eliminated. */
2485 && (!same_type_ignoring_top_level_qualifiers_p
2486 (TREE_TYPE (object), DECL_CONTEXT (member))))
2488 tree anonymous_union;
2490 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2491 DECL_CONTEXT (member));
2492 object = build_class_member_access_expr (object,
2493 anonymous_union,
2494 /*access_path=*/NULL_TREE,
2495 preserve_reference,
2496 complain);
2499 /* Compute the type of the field, as described in [expr.ref]. */
2500 type_quals = TYPE_UNQUALIFIED;
2501 member_type = TREE_TYPE (member);
2502 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2504 type_quals = (cp_type_quals (member_type)
2505 | cp_type_quals (object_type));
2507 /* A field is const (volatile) if the enclosing object, or the
2508 field itself, is const (volatile). But, a mutable field is
2509 not const, even within a const object. */
2510 if (DECL_MUTABLE_P (member))
2511 type_quals &= ~TYPE_QUAL_CONST;
2512 member_type = cp_build_qualified_type (member_type, type_quals);
2515 result = build3_loc (input_location, COMPONENT_REF, member_type,
2516 object, member, NULL_TREE);
2518 /* Mark the expression const or volatile, as appropriate. Even
2519 though we've dealt with the type above, we still have to mark the
2520 expression itself. */
2521 if (type_quals & TYPE_QUAL_CONST)
2522 TREE_READONLY (result) = 1;
2523 if (type_quals & TYPE_QUAL_VOLATILE)
2524 TREE_THIS_VOLATILE (result) = 1;
2526 else if (BASELINK_P (member))
2528 /* The member is a (possibly overloaded) member function. */
2529 tree functions;
2530 tree type;
2532 /* If the MEMBER is exactly one static member function, then we
2533 know the type of the expression. Otherwise, we must wait
2534 until overload resolution has been performed. */
2535 functions = BASELINK_FUNCTIONS (member);
2536 if (TREE_CODE (functions) == FUNCTION_DECL
2537 && DECL_STATIC_FUNCTION_P (functions))
2538 type = TREE_TYPE (functions);
2539 else
2540 type = unknown_type_node;
2541 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2542 base. That will happen when the function is called. */
2543 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2545 else if (TREE_CODE (member) == CONST_DECL)
2547 /* The member is an enumerator. */
2548 result = member;
2549 /* If OBJECT has side-effects, they are supposed to occur. */
2550 if (TREE_SIDE_EFFECTS (object))
2551 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2552 object, result);
2554 else if ((using_decl = strip_using_decl (member)) != member)
2555 result = build_class_member_access_expr (object,
2556 using_decl,
2557 access_path, preserve_reference,
2558 complain);
2559 else
2561 if (complain & tf_error)
2562 error ("invalid use of %qD", member);
2563 return error_mark_node;
2566 if (!preserve_reference)
2567 /* [expr.ref]
2569 If E2 is declared to have type "reference to T", then ... the
2570 type of E1.E2 is T. */
2571 result = convert_from_reference (result);
2573 return result;
2576 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2577 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2579 static tree
2580 lookup_destructor (tree object, tree scope, tree dtor_name,
2581 tsubst_flags_t complain)
2583 tree object_type = TREE_TYPE (object);
2584 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2585 tree expr;
2587 /* We've already complained about this destructor. */
2588 if (dtor_type == error_mark_node)
2589 return error_mark_node;
2591 if (scope && !check_dtor_name (scope, dtor_type))
2593 if (complain & tf_error)
2594 error ("qualified type %qT does not match destructor name ~%qT",
2595 scope, dtor_type);
2596 return error_mark_node;
2598 if (is_auto (dtor_type))
2599 dtor_type = object_type;
2600 else if (identifier_p (dtor_type))
2602 /* In a template, names we can't find a match for are still accepted
2603 destructor names, and we check them here. */
2604 if (check_dtor_name (object_type, dtor_type))
2605 dtor_type = object_type;
2606 else
2608 if (complain & tf_error)
2609 error ("object type %qT does not match destructor name ~%qT",
2610 object_type, dtor_type);
2611 return error_mark_node;
2615 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2617 if (complain & tf_error)
2618 error ("the type being destroyed is %qT, but the destructor "
2619 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2620 return error_mark_node;
2622 expr = lookup_member (dtor_type, complete_dtor_identifier,
2623 /*protect=*/1, /*want_type=*/false,
2624 tf_warning_or_error);
2625 if (!expr)
2627 if (complain & tf_error)
2628 cxx_incomplete_type_error (dtor_name, dtor_type);
2629 return error_mark_node;
2631 expr = (adjust_result_of_qualified_name_lookup
2632 (expr, dtor_type, object_type));
2633 if (scope == NULL_TREE)
2634 /* We need to call adjust_result_of_qualified_name_lookup in case the
2635 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2636 that we still get virtual function binding. */
2637 BASELINK_QUALIFIED_P (expr) = false;
2638 return expr;
2641 /* An expression of the form "A::template B" has been resolved to
2642 DECL. Issue a diagnostic if B is not a template or template
2643 specialization. */
2645 void
2646 check_template_keyword (tree decl)
2648 /* The standard says:
2650 [temp.names]
2652 If a name prefixed by the keyword template is not a member
2653 template, the program is ill-formed.
2655 DR 228 removed the restriction that the template be a member
2656 template.
2658 DR 96, if accepted would add the further restriction that explicit
2659 template arguments must be provided if the template keyword is
2660 used, but, as of 2005-10-16, that DR is still in "drafting". If
2661 this DR is accepted, then the semantic checks here can be
2662 simplified, as the entity named must in fact be a template
2663 specialization, rather than, as at present, a set of overloaded
2664 functions containing at least one template function. */
2665 if (TREE_CODE (decl) != TEMPLATE_DECL
2666 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2668 if (VAR_P (decl))
2670 if (DECL_USE_TEMPLATE (decl)
2671 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2673 else
2674 permerror (input_location, "%qD is not a template", decl);
2676 else if (!is_overloaded_fn (decl))
2677 permerror (input_location, "%qD is not a template", decl);
2678 else
2680 bool found = false;
2682 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2683 !found && iter; ++iter)
2685 tree fn = *iter;
2686 if (TREE_CODE (fn) == TEMPLATE_DECL
2687 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2688 || (TREE_CODE (fn) == FUNCTION_DECL
2689 && DECL_USE_TEMPLATE (fn)
2690 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2691 found = true;
2693 if (!found)
2694 permerror (input_location, "%qD is not a template", decl);
2699 /* Record that an access failure occurred on BASETYPE_PATH attempting
2700 to access FIELD_DECL. */
2702 void
2703 access_failure_info::record_access_failure (tree basetype_path,
2704 tree field_decl)
2706 m_was_inaccessible = true;
2707 m_basetype_path = basetype_path;
2708 m_field_decl = field_decl;
2711 /* If an access failure was recorded, then attempt to locate an
2712 accessor function for the pertinent field, and if one is
2713 available, add a note and fix-it hint suggesting using it. */
2715 void
2716 access_failure_info::maybe_suggest_accessor (bool const_p) const
2718 if (!m_was_inaccessible)
2719 return;
2721 tree accessor
2722 = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2723 if (!accessor)
2724 return;
2726 /* The accessor must itself be accessible for it to be a reasonable
2727 suggestion. */
2728 if (!accessible_p (m_basetype_path, accessor, true))
2729 return;
2731 rich_location richloc (line_table, input_location);
2732 pretty_printer pp;
2733 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2734 richloc.add_fixit_replace (pp_formatted_text (&pp));
2735 inform (&richloc, "field %q#D can be accessed via %q#D",
2736 m_field_decl, accessor);
2739 /* This function is called by the parser to process a class member
2740 access expression of the form OBJECT.NAME. NAME is a node used by
2741 the parser to represent a name; it is not yet a DECL. It may,
2742 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2743 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2744 there is no reason to do the lookup twice, so the parser keeps the
2745 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2746 be a template via the use of the "A::template B" syntax. */
2748 tree
2749 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2750 tsubst_flags_t complain)
2752 tree expr;
2753 tree object_type;
2754 tree member;
2755 tree access_path = NULL_TREE;
2756 tree orig_object = object;
2757 tree orig_name = name;
2759 if (object == error_mark_node || name == error_mark_node)
2760 return error_mark_node;
2762 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2763 if (!objc_is_public (object, name))
2764 return error_mark_node;
2766 object_type = TREE_TYPE (object);
2768 if (processing_template_decl)
2770 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2771 type_dependent_object_expression_p (object)
2772 /* If NAME is "f<args>", where either 'f' or 'args' is
2773 dependent, then the expression is dependent. */
2774 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2775 && dependent_template_id_p (TREE_OPERAND (name, 0),
2776 TREE_OPERAND (name, 1)))
2777 /* If NAME is "T::X" where "T" is dependent, then the
2778 expression is dependent. */
2779 || (TREE_CODE (name) == SCOPE_REF
2780 && TYPE_P (TREE_OPERAND (name, 0))
2781 && dependent_scope_p (TREE_OPERAND (name, 0))))
2783 dependent:
2784 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2785 orig_object, orig_name, NULL_TREE);
2787 object = build_non_dependent_expr (object);
2789 else if (c_dialect_objc ()
2790 && identifier_p (name)
2791 && (expr = objc_maybe_build_component_ref (object, name)))
2792 return expr;
2794 /* [expr.ref]
2796 The type of the first expression shall be "class object" (of a
2797 complete type). */
2798 if (!currently_open_class (object_type)
2799 && !complete_type_or_maybe_complain (object_type, object, complain))
2800 return error_mark_node;
2801 if (!CLASS_TYPE_P (object_type))
2803 if (complain & tf_error)
2805 if (POINTER_TYPE_P (object_type)
2806 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2807 error ("request for member %qD in %qE, which is of pointer "
2808 "type %qT (maybe you meant to use %<->%> ?)",
2809 name, object.get_value (), object_type);
2810 else
2811 error ("request for member %qD in %qE, which is of non-class "
2812 "type %qT", name, object.get_value (), object_type);
2814 return error_mark_node;
2817 if (BASELINK_P (name))
2818 /* A member function that has already been looked up. */
2819 member = name;
2820 else
2822 bool is_template_id = false;
2823 tree template_args = NULL_TREE;
2824 tree scope = NULL_TREE;
2826 access_path = object_type;
2828 if (TREE_CODE (name) == SCOPE_REF)
2830 /* A qualified name. The qualifying class or namespace `S'
2831 has already been looked up; it is either a TYPE or a
2832 NAMESPACE_DECL. */
2833 scope = TREE_OPERAND (name, 0);
2834 name = TREE_OPERAND (name, 1);
2836 /* If SCOPE is a namespace, then the qualified name does not
2837 name a member of OBJECT_TYPE. */
2838 if (TREE_CODE (scope) == NAMESPACE_DECL)
2840 if (complain & tf_error)
2841 error ("%<%D::%D%> is not a member of %qT",
2842 scope, name, object_type);
2843 return error_mark_node;
2847 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2849 is_template_id = true;
2850 template_args = TREE_OPERAND (name, 1);
2851 name = TREE_OPERAND (name, 0);
2853 if (!identifier_p (name))
2854 name = OVL_NAME (name);
2857 if (scope)
2859 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2861 gcc_assert (!is_template_id);
2862 /* Looking up a member enumerator (c++/56793). */
2863 if (!TYPE_CLASS_SCOPE_P (scope)
2864 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2866 if (complain & tf_error)
2867 error ("%<%D::%D%> is not a member of %qT",
2868 scope, name, object_type);
2869 return error_mark_node;
2871 tree val = lookup_enumerator (scope, name);
2872 if (!val)
2874 if (complain & tf_error)
2875 error ("%qD is not a member of %qD",
2876 name, scope);
2877 return error_mark_node;
2880 if (TREE_SIDE_EFFECTS (object))
2881 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2882 return val;
2885 gcc_assert (CLASS_TYPE_P (scope));
2886 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2888 if (constructor_name_p (name, scope))
2890 if (complain & tf_error)
2891 error ("cannot call constructor %<%T::%D%> directly",
2892 scope, name);
2893 return error_mark_node;
2896 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2897 access_path = lookup_base (object_type, scope, ba_check,
2898 NULL, complain);
2899 if (access_path == error_mark_node)
2900 return error_mark_node;
2901 if (!access_path)
2903 if (any_dependent_bases_p (object_type))
2904 goto dependent;
2905 if (complain & tf_error)
2906 error ("%qT is not a base of %qT", scope, object_type);
2907 return error_mark_node;
2911 if (TREE_CODE (name) == BIT_NOT_EXPR)
2913 if (dependent_type_p (object_type))
2914 /* The destructor isn't declared yet. */
2915 goto dependent;
2916 member = lookup_destructor (object, scope, name, complain);
2918 else
2920 /* Look up the member. */
2921 access_failure_info afi;
2922 member = lookup_member (access_path, name, /*protect=*/1,
2923 /*want_type=*/false, complain,
2924 &afi);
2925 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2926 if (member == NULL_TREE)
2928 if (dependent_type_p (object_type))
2929 /* Try again at instantiation time. */
2930 goto dependent;
2931 if (complain & tf_error)
2933 tree guessed_id = lookup_member_fuzzy (access_path, name,
2934 /*want_type=*/false);
2935 if (guessed_id)
2937 location_t bogus_component_loc = input_location;
2938 gcc_rich_location rich_loc (bogus_component_loc);
2939 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2940 guessed_id);
2941 error_at (&rich_loc,
2942 "%q#T has no member named %qE;"
2943 " did you mean %qE?",
2944 TREE_CODE (access_path) == TREE_BINFO
2945 ? TREE_TYPE (access_path) : object_type,
2946 name, guessed_id);
2948 else
2949 error ("%q#T has no member named %qE",
2950 TREE_CODE (access_path) == TREE_BINFO
2951 ? TREE_TYPE (access_path) : object_type, name);
2953 return error_mark_node;
2955 if (member == error_mark_node)
2956 return error_mark_node;
2957 if (DECL_P (member)
2958 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2959 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2960 wrong, so don't use it. */
2961 goto dependent;
2962 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2963 goto dependent;
2966 if (is_template_id)
2968 tree templ = member;
2970 if (BASELINK_P (templ))
2971 member = lookup_template_function (templ, template_args);
2972 else if (variable_template_p (templ))
2973 member = (lookup_and_finish_template_variable
2974 (templ, template_args, complain));
2975 else
2977 if (complain & tf_error)
2978 error ("%qD is not a member template function", name);
2979 return error_mark_node;
2984 if (TREE_DEPRECATED (member))
2985 warn_deprecated_use (member, NULL_TREE);
2987 if (template_p)
2988 check_template_keyword (member);
2990 expr = build_class_member_access_expr (object, member, access_path,
2991 /*preserve_reference=*/false,
2992 complain);
2993 if (processing_template_decl && expr != error_mark_node)
2995 if (BASELINK_P (member))
2997 if (TREE_CODE (orig_name) == SCOPE_REF)
2998 BASELINK_QUALIFIED_P (member) = 1;
2999 orig_name = member;
3001 return build_min_non_dep (COMPONENT_REF, expr,
3002 orig_object, orig_name,
3003 NULL_TREE);
3006 return expr;
3009 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3010 type. */
3012 tree
3013 build_simple_component_ref (tree object, tree member)
3015 tree type = cp_build_qualified_type (TREE_TYPE (member),
3016 cp_type_quals (TREE_TYPE (object)));
3017 return build3_loc (input_location,
3018 COMPONENT_REF, type,
3019 object, member, NULL_TREE);
3022 /* Return an expression for the MEMBER_NAME field in the internal
3023 representation of PTRMEM, a pointer-to-member function. (Each
3024 pointer-to-member function type gets its own RECORD_TYPE so it is
3025 more convenient to access the fields by name than by FIELD_DECL.)
3026 This routine converts the NAME to a FIELD_DECL and then creates the
3027 node for the complete expression. */
3029 tree
3030 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3032 tree ptrmem_type;
3033 tree member;
3035 /* This code is a stripped down version of
3036 build_class_member_access_expr. It does not work to use that
3037 routine directly because it expects the object to be of class
3038 type. */
3039 ptrmem_type = TREE_TYPE (ptrmem);
3040 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3041 for (member = TYPE_FIELDS (ptrmem_type); member;
3042 member = DECL_CHAIN (member))
3043 if (DECL_NAME (member) == member_name)
3044 break;
3045 tree res = build_simple_component_ref (ptrmem, member);
3047 TREE_NO_WARNING (res) = 1;
3048 return res;
3051 /* Given an expression PTR for a pointer, return an expression
3052 for the value pointed to.
3053 ERRORSTRING is the name of the operator to appear in error messages.
3055 This function may need to overload OPERATOR_FNNAME.
3056 Must also handle REFERENCE_TYPEs for C++. */
3058 tree
3059 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3060 tsubst_flags_t complain)
3062 tree orig_expr = expr;
3063 tree rval;
3064 tree overload = NULL_TREE;
3066 if (processing_template_decl)
3068 /* Retain the type if we know the operand is a pointer. */
3069 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
3070 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3071 if (type_dependent_expression_p (expr))
3072 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3073 expr = build_non_dependent_expr (expr);
3076 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3077 NULL_TREE, NULL_TREE, &overload, complain);
3078 if (!rval)
3079 rval = cp_build_indirect_ref (expr, errorstring, complain);
3081 if (processing_template_decl && rval != error_mark_node)
3083 if (overload != NULL_TREE)
3084 return (build_min_non_dep_op_overload
3085 (INDIRECT_REF, rval, overload, orig_expr));
3087 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3089 else
3090 return rval;
3093 /* The implementation of the above, and of indirection implied by other
3094 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3096 static tree
3097 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3098 tsubst_flags_t complain, bool do_fold)
3100 tree pointer, type;
3102 /* RO_NULL should only be used with the folding entry points below, not
3103 cp_build_indirect_ref. */
3104 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3106 if (ptr == current_class_ptr
3107 || (TREE_CODE (ptr) == NOP_EXPR
3108 && TREE_OPERAND (ptr, 0) == current_class_ptr
3109 && (same_type_ignoring_top_level_qualifiers_p
3110 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3111 return current_class_ref;
3113 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3114 ? ptr : decay_conversion (ptr, complain));
3115 if (pointer == error_mark_node)
3116 return error_mark_node;
3118 type = TREE_TYPE (pointer);
3120 if (POINTER_TYPE_P (type))
3122 /* [expr.unary.op]
3124 If the type of the expression is "pointer to T," the type
3125 of the result is "T." */
3126 tree t = TREE_TYPE (type);
3128 if ((CONVERT_EXPR_P (ptr)
3129 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3130 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3132 /* If a warning is issued, mark it to avoid duplicates from
3133 the backend. This only needs to be done at
3134 warn_strict_aliasing > 2. */
3135 if (warn_strict_aliasing > 2)
3136 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
3137 type, TREE_OPERAND (ptr, 0)))
3138 TREE_NO_WARNING (ptr) = 1;
3141 if (VOID_TYPE_P (t))
3143 /* A pointer to incomplete type (other than cv void) can be
3144 dereferenced [expr.unary.op]/1 */
3145 if (complain & tf_error)
3146 error ("%qT is not a pointer-to-object type", type);
3147 return error_mark_node;
3149 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3150 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3151 /* The POINTER was something like `&x'. We simplify `*&x' to
3152 `x'. */
3153 return TREE_OPERAND (pointer, 0);
3154 else
3156 tree ref = build1 (INDIRECT_REF, t, pointer);
3158 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3159 so that we get the proper error message if the result is used
3160 to assign to. Also, &* is supposed to be a no-op. */
3161 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3162 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3163 TREE_SIDE_EFFECTS (ref)
3164 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3165 return ref;
3168 else if (!(complain & tf_error))
3169 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3171 /* `pointer' won't be an error_mark_node if we were given a
3172 pointer to member, so it's cool to check for this here. */
3173 else if (TYPE_PTRMEM_P (type))
3174 switch (errorstring)
3176 case RO_ARRAY_INDEXING:
3177 error ("invalid use of array indexing on pointer to member");
3178 break;
3179 case RO_UNARY_STAR:
3180 error ("invalid use of unary %<*%> on pointer to member");
3181 break;
3182 case RO_IMPLICIT_CONVERSION:
3183 error ("invalid use of implicit conversion on pointer to member");
3184 break;
3185 case RO_ARROW_STAR:
3186 error ("left hand operand of %<->*%> must be a pointer to class, "
3187 "but is a pointer to member of type %qT", type);
3188 break;
3189 default:
3190 gcc_unreachable ();
3192 else if (pointer != error_mark_node)
3193 invalid_indirection_error (input_location, type, errorstring);
3195 return error_mark_node;
3198 /* Entry point used by c-common, which expects folding. */
3200 tree
3201 build_indirect_ref (location_t /*loc*/,
3202 tree ptr, ref_operator errorstring)
3204 return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3207 /* Entry point used by internal indirection needs that don't correspond to any
3208 syntactic construct. */
3210 tree
3211 cp_build_fold_indirect_ref (tree pointer)
3213 return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3216 /* Entry point used by indirection needs that correspond to some syntactic
3217 construct. */
3219 tree
3220 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3221 tsubst_flags_t complain)
3223 return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3226 /* This handles expressions of the form "a[i]", which denotes
3227 an array reference.
3229 This is logically equivalent in C to *(a+i), but we may do it differently.
3230 If A is a variable or a member, we generate a primitive ARRAY_REF.
3231 This avoids forcing the array out of registers, and can work on
3232 arrays that are not lvalues (for example, members of structures returned
3233 by functions).
3235 If INDEX is of some user-defined type, it must be converted to
3236 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3237 will inherit the type of the array, which will be some pointer type.
3239 LOC is the location to use in building the array reference. */
3241 tree
3242 cp_build_array_ref (location_t loc, tree array, tree idx,
3243 tsubst_flags_t complain)
3245 tree ret;
3247 if (idx == 0)
3249 if (complain & tf_error)
3250 error_at (loc, "subscript missing in array reference");
3251 return error_mark_node;
3254 if (TREE_TYPE (array) == error_mark_node
3255 || TREE_TYPE (idx) == error_mark_node)
3256 return error_mark_node;
3258 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3259 inside it. */
3260 switch (TREE_CODE (array))
3262 case COMPOUND_EXPR:
3264 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3265 complain);
3266 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3267 TREE_OPERAND (array, 0), value);
3268 SET_EXPR_LOCATION (ret, loc);
3269 return ret;
3272 case COND_EXPR:
3273 ret = build_conditional_expr
3274 (loc, TREE_OPERAND (array, 0),
3275 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3276 complain),
3277 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3278 complain),
3279 complain);
3280 protected_set_expr_location (ret, loc);
3281 return ret;
3283 default:
3284 break;
3287 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3289 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3291 tree rval, type;
3293 warn_array_subscript_with_type_char (loc, idx);
3295 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3297 if (complain & tf_error)
3298 error_at (loc, "array subscript is not an integer");
3299 return error_mark_node;
3302 /* Apply integral promotions *after* noticing character types.
3303 (It is unclear why we do these promotions -- the standard
3304 does not say that we should. In fact, the natural thing would
3305 seem to be to convert IDX to ptrdiff_t; we're performing
3306 pointer arithmetic.) */
3307 idx = cp_perform_integral_promotions (idx, complain);
3309 /* An array that is indexed by a non-constant
3310 cannot be stored in a register; we must be able to do
3311 address arithmetic on its address.
3312 Likewise an array of elements of variable size. */
3313 if (TREE_CODE (idx) != INTEGER_CST
3314 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3315 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3316 != INTEGER_CST)))
3318 if (!cxx_mark_addressable (array, true))
3319 return error_mark_node;
3322 /* An array that is indexed by a constant value which is not within
3323 the array bounds cannot be stored in a register either; because we
3324 would get a crash in store_bit_field/extract_bit_field when trying
3325 to access a non-existent part of the register. */
3326 if (TREE_CODE (idx) == INTEGER_CST
3327 && TYPE_DOMAIN (TREE_TYPE (array))
3328 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3330 if (!cxx_mark_addressable (array))
3331 return error_mark_node;
3334 /* Note in C++ it is valid to subscript a `register' array, since
3335 it is valid to take the address of something with that
3336 storage specification. */
3337 if (extra_warnings)
3339 tree foo = array;
3340 while (TREE_CODE (foo) == COMPONENT_REF)
3341 foo = TREE_OPERAND (foo, 0);
3342 if (VAR_P (foo) && DECL_REGISTER (foo)
3343 && (complain & tf_warning))
3344 warning_at (loc, OPT_Wextra,
3345 "subscripting array declared %<register%>");
3348 type = TREE_TYPE (TREE_TYPE (array));
3349 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3350 /* Array ref is const/volatile if the array elements are
3351 or if the array is.. */
3352 TREE_READONLY (rval)
3353 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3354 TREE_SIDE_EFFECTS (rval)
3355 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3356 TREE_THIS_VOLATILE (rval)
3357 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3358 ret = require_complete_type_sfinae (rval, complain);
3359 protected_set_expr_location (ret, loc);
3360 if (non_lvalue)
3361 ret = non_lvalue_loc (loc, ret);
3362 return ret;
3366 tree ar = cp_default_conversion (array, complain);
3367 tree ind = cp_default_conversion (idx, complain);
3369 /* Put the integer in IND to simplify error checking. */
3370 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3371 std::swap (ar, ind);
3373 if (ar == error_mark_node || ind == error_mark_node)
3374 return error_mark_node;
3376 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3378 if (complain & tf_error)
3379 error_at (loc, "subscripted value is neither array nor pointer");
3380 return error_mark_node;
3382 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3384 if (complain & tf_error)
3385 error_at (loc, "array subscript is not an integer");
3386 return error_mark_node;
3389 warn_array_subscript_with_type_char (loc, idx);
3391 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3392 PLUS_EXPR, ar, ind,
3393 complain),
3394 RO_ARRAY_INDEXING,
3395 complain);
3396 protected_set_expr_location (ret, loc);
3397 if (non_lvalue)
3398 ret = non_lvalue_loc (loc, ret);
3399 return ret;
3403 /* Entry point for Obj-C++. */
3405 tree
3406 build_array_ref (location_t loc, tree array, tree idx)
3408 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3411 /* Resolve a pointer to member function. INSTANCE is the object
3412 instance to use, if the member points to a virtual member.
3414 This used to avoid checking for virtual functions if basetype
3415 has no virtual functions, according to an earlier ANSI draft.
3416 With the final ISO C++ rules, such an optimization is
3417 incorrect: A pointer to a derived member can be static_cast
3418 to pointer-to-base-member, as long as the dynamic object
3419 later has the right member. So now we only do this optimization
3420 when we know the dynamic type of the object. */
3422 tree
3423 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3424 tsubst_flags_t complain)
3426 if (TREE_CODE (function) == OFFSET_REF)
3427 function = TREE_OPERAND (function, 1);
3429 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3431 tree idx, delta, e1, e2, e3, vtbl;
3432 bool nonvirtual;
3433 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3434 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3436 tree instance_ptr = *instance_ptrptr;
3437 tree instance_save_expr = 0;
3438 if (instance_ptr == error_mark_node)
3440 if (TREE_CODE (function) == PTRMEM_CST)
3442 /* Extracting the function address from a pmf is only
3443 allowed with -Wno-pmf-conversions. It only works for
3444 pmf constants. */
3445 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3446 e1 = convert (fntype, e1);
3447 return e1;
3449 else
3451 if (complain & tf_error)
3452 error ("object missing in use of %qE", function);
3453 return error_mark_node;
3457 /* True if we know that the dynamic type of the object doesn't have
3458 virtual functions, so we can assume the PFN field is a pointer. */
3459 nonvirtual = (COMPLETE_TYPE_P (basetype)
3460 && !TYPE_POLYMORPHIC_P (basetype)
3461 && resolves_to_fixed_type_p (instance_ptr, 0));
3463 /* If we don't really have an object (i.e. in an ill-formed
3464 conversion from PMF to pointer), we can't resolve virtual
3465 functions anyway. */
3466 if (!nonvirtual && is_dummy_object (instance_ptr))
3467 nonvirtual = true;
3469 if (TREE_SIDE_EFFECTS (instance_ptr))
3470 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3472 if (TREE_SIDE_EFFECTS (function))
3473 function = save_expr (function);
3475 /* Start by extracting all the information from the PMF itself. */
3476 e3 = pfn_from_ptrmemfunc (function);
3477 delta = delta_from_ptrmemfunc (function);
3478 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3479 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3481 int flag_sanitize_save;
3482 case ptrmemfunc_vbit_in_pfn:
3483 e1 = cp_build_binary_op (input_location,
3484 BIT_AND_EXPR, idx, integer_one_node,
3485 complain);
3486 idx = cp_build_binary_op (input_location,
3487 MINUS_EXPR, idx, integer_one_node,
3488 complain);
3489 if (idx == error_mark_node)
3490 return error_mark_node;
3491 break;
3493 case ptrmemfunc_vbit_in_delta:
3494 e1 = cp_build_binary_op (input_location,
3495 BIT_AND_EXPR, delta, integer_one_node,
3496 complain);
3497 /* Don't instrument the RSHIFT_EXPR we're about to create because
3498 we're going to use DELTA number of times, and that wouldn't play
3499 well with SAVE_EXPRs therein. */
3500 flag_sanitize_save = flag_sanitize;
3501 flag_sanitize = 0;
3502 delta = cp_build_binary_op (input_location,
3503 RSHIFT_EXPR, delta, integer_one_node,
3504 complain);
3505 flag_sanitize = flag_sanitize_save;
3506 if (delta == error_mark_node)
3507 return error_mark_node;
3508 break;
3510 default:
3511 gcc_unreachable ();
3514 if (e1 == error_mark_node)
3515 return error_mark_node;
3517 /* Convert down to the right base before using the instance. A
3518 special case is that in a pointer to member of class C, C may
3519 be incomplete. In that case, the function will of course be
3520 a member of C, and no conversion is required. In fact,
3521 lookup_base will fail in that case, because incomplete
3522 classes do not have BINFOs. */
3523 if (!same_type_ignoring_top_level_qualifiers_p
3524 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3526 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3527 basetype, ba_check, NULL, complain);
3528 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3529 1, complain);
3530 if (instance_ptr == error_mark_node)
3531 return error_mark_node;
3533 /* ...and then the delta in the PMF. */
3534 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3536 /* Hand back the adjusted 'this' argument to our caller. */
3537 *instance_ptrptr = instance_ptr;
3539 if (nonvirtual)
3540 /* Now just return the pointer. */
3541 return e3;
3543 /* Next extract the vtable pointer from the object. */
3544 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3545 instance_ptr);
3546 vtbl = cp_build_fold_indirect_ref (vtbl);
3547 if (vtbl == error_mark_node)
3548 return error_mark_node;
3550 /* Finally, extract the function pointer from the vtable. */
3551 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3552 e2 = cp_build_fold_indirect_ref (e2);
3553 if (e2 == error_mark_node)
3554 return error_mark_node;
3555 TREE_CONSTANT (e2) = 1;
3557 /* When using function descriptors, the address of the
3558 vtable entry is treated as a function pointer. */
3559 if (TARGET_VTABLE_USES_DESCRIPTORS)
3560 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3561 cp_build_addr_expr (e2, complain));
3563 e2 = fold_convert (TREE_TYPE (e3), e2);
3564 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3565 if (e1 == error_mark_node)
3566 return error_mark_node;
3568 /* Make sure this doesn't get evaluated first inside one of the
3569 branches of the COND_EXPR. */
3570 if (instance_save_expr)
3571 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3572 instance_save_expr, e1);
3574 function = e1;
3576 return function;
3579 /* Used by the C-common bits. */
3580 tree
3581 build_function_call (location_t /*loc*/,
3582 tree function, tree params)
3584 return cp_build_function_call (function, params, tf_warning_or_error);
3587 /* Used by the C-common bits. */
3588 tree
3589 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3590 tree function, vec<tree, va_gc> *params,
3591 vec<tree, va_gc> * /*origtypes*/)
3593 vec<tree, va_gc> *orig_params = params;
3594 tree ret = cp_build_function_call_vec (function, &params,
3595 tf_warning_or_error);
3597 /* cp_build_function_call_vec can reallocate PARAMS by adding
3598 default arguments. That should never happen here. Verify
3599 that. */
3600 gcc_assert (params == orig_params);
3602 return ret;
3605 /* Build a function call using a tree list of arguments. */
3607 static tree
3608 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3610 vec<tree, va_gc> *vec;
3611 tree ret;
3613 vec = make_tree_vector ();
3614 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3615 vec_safe_push (vec, TREE_VALUE (params));
3616 ret = cp_build_function_call_vec (function, &vec, complain);
3617 release_tree_vector (vec);
3618 return ret;
3621 /* Build a function call using varargs. */
3623 tree
3624 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3626 vec<tree, va_gc> *vec;
3627 va_list args;
3628 tree ret, t;
3630 vec = make_tree_vector ();
3631 va_start (args, complain);
3632 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3633 vec_safe_push (vec, t);
3634 va_end (args);
3635 ret = cp_build_function_call_vec (function, &vec, complain);
3636 release_tree_vector (vec);
3637 return ret;
3640 /* Build a function call using a vector of arguments. PARAMS may be
3641 NULL if there are no parameters. This changes the contents of
3642 PARAMS. */
3644 tree
3645 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3646 tsubst_flags_t complain)
3648 tree fntype, fndecl;
3649 int is_method;
3650 tree original = function;
3651 int nargs;
3652 tree *argarray;
3653 tree parm_types;
3654 vec<tree, va_gc> *allocated = NULL;
3655 tree ret;
3657 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3658 expressions, like those used for ObjC messenger dispatches. */
3659 if (params != NULL && !vec_safe_is_empty (*params))
3660 function = objc_rewrite_function_call (function, (**params)[0]);
3662 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3663 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3664 if (TREE_CODE (function) == NOP_EXPR
3665 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3666 function = TREE_OPERAND (function, 0);
3668 if (TREE_CODE (function) == FUNCTION_DECL)
3670 /* If the function is a non-template member function
3671 or a non-template friend, then we need to check the
3672 constraints.
3674 Note that if overload resolution failed with a single
3675 candidate this function will be used to explicitly diagnose
3676 the failure for the single call expression. The check is
3677 technically redundant since we also would have failed in
3678 add_function_candidate. */
3679 if (flag_concepts
3680 && (complain & tf_error)
3681 && !constraints_satisfied_p (function))
3683 error ("cannot call function %qD", function);
3684 location_t loc = DECL_SOURCE_LOCATION (function);
3685 diagnose_constraints (loc, function, NULL_TREE);
3686 return error_mark_node;
3689 if (!mark_used (function, complain) && !(complain & tf_error))
3690 return error_mark_node;
3691 fndecl = function;
3693 /* Convert anything with function type to a pointer-to-function. */
3694 if (DECL_MAIN_P (function))
3696 if (complain & tf_error)
3697 pedwarn (input_location, OPT_Wpedantic,
3698 "ISO C++ forbids calling %<::main%> from within program");
3699 else
3700 return error_mark_node;
3702 function = build_addr_func (function, complain);
3704 else
3706 fndecl = NULL_TREE;
3708 function = build_addr_func (function, complain);
3711 if (function == error_mark_node)
3712 return error_mark_node;
3714 fntype = TREE_TYPE (function);
3716 if (TYPE_PTRMEMFUNC_P (fntype))
3718 if (complain & tf_error)
3719 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3720 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3721 original, original);
3722 return error_mark_node;
3725 is_method = (TYPE_PTR_P (fntype)
3726 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3728 if (!(TYPE_PTRFN_P (fntype)
3729 || is_method
3730 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3732 if (complain & tf_error)
3734 if (!flag_diagnostics_show_caret)
3735 error_at (input_location,
3736 "%qE cannot be used as a function", original);
3737 else if (DECL_P (original))
3738 error_at (input_location,
3739 "%qD cannot be used as a function", original);
3740 else
3741 error_at (input_location,
3742 "expression cannot be used as a function");
3745 return error_mark_node;
3748 /* fntype now gets the type of function pointed to. */
3749 fntype = TREE_TYPE (fntype);
3750 parm_types = TYPE_ARG_TYPES (fntype);
3752 if (params == NULL)
3754 allocated = make_tree_vector ();
3755 params = &allocated;
3758 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3759 complain);
3760 if (nargs < 0)
3761 return error_mark_node;
3763 argarray = (*params)->address ();
3765 /* Check for errors in format strings and inappropriately
3766 null parameters. */
3767 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3768 nargs, argarray, NULL);
3770 ret = build_cxx_call (function, nargs, argarray, complain);
3772 if (warned_p)
3774 tree c = extract_call_expr (ret);
3775 if (TREE_CODE (c) == CALL_EXPR)
3776 TREE_NO_WARNING (c) = 1;
3779 if (allocated != NULL)
3780 release_tree_vector (allocated);
3782 return ret;
3785 /* Subroutine of convert_arguments.
3786 Print an error message about a wrong number of arguments. */
3788 static void
3789 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3791 if (fndecl)
3793 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3795 if (DECL_NAME (fndecl) == NULL_TREE
3796 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3797 error_at (loc,
3798 too_many_p
3799 ? G_("too many arguments to constructor %q#D")
3800 : G_("too few arguments to constructor %q#D"),
3801 fndecl);
3802 else
3803 error_at (loc,
3804 too_many_p
3805 ? G_("too many arguments to member function %q#D")
3806 : G_("too few arguments to member function %q#D"),
3807 fndecl);
3809 else
3810 error_at (loc,
3811 too_many_p
3812 ? G_("too many arguments to function %q#D")
3813 : G_("too few arguments to function %q#D"),
3814 fndecl);
3815 if (!DECL_IS_BUILTIN (fndecl))
3816 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3818 else
3820 if (c_dialect_objc () && objc_message_selector ())
3821 error_at (loc,
3822 too_many_p
3823 ? G_("too many arguments to method %q#D")
3824 : G_("too few arguments to method %q#D"),
3825 objc_message_selector ());
3826 else
3827 error_at (loc, too_many_p ? G_("too many arguments to function")
3828 : G_("too few arguments to function"));
3832 /* Convert the actual parameter expressions in the list VALUES to the
3833 types in the list TYPELIST. The converted expressions are stored
3834 back in the VALUES vector.
3835 If parmdecls is exhausted, or when an element has NULL as its type,
3836 perform the default conversions.
3838 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3840 This is also where warnings about wrong number of args are generated.
3842 Returns the actual number of arguments processed (which might be less
3843 than the length of the vector), or -1 on error.
3845 In C++, unspecified trailing parameters can be filled in with their
3846 default arguments, if such were specified. Do so here. */
3848 static int
3849 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3850 int flags, tsubst_flags_t complain)
3852 tree typetail;
3853 unsigned int i;
3855 /* Argument passing is always copy-initialization. */
3856 flags |= LOOKUP_ONLYCONVERTING;
3858 for (i = 0, typetail = typelist;
3859 i < vec_safe_length (*values);
3860 i++)
3862 tree type = typetail ? TREE_VALUE (typetail) : 0;
3863 tree val = (**values)[i];
3865 if (val == error_mark_node || type == error_mark_node)
3866 return -1;
3868 if (type == void_type_node)
3870 if (complain & tf_error)
3872 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3873 return i;
3875 else
3876 return -1;
3879 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3880 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3881 if (TREE_CODE (val) == NOP_EXPR
3882 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3883 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3884 val = TREE_OPERAND (val, 0);
3886 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3888 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3889 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3890 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3891 val = decay_conversion (val, complain);
3894 if (val == error_mark_node)
3895 return -1;
3897 if (type != 0)
3899 /* Formal parm type is specified by a function prototype. */
3900 tree parmval;
3902 if (!COMPLETE_TYPE_P (complete_type (type)))
3904 if (complain & tf_error)
3906 if (fndecl)
3907 error ("parameter %P of %qD has incomplete type %qT",
3908 i, fndecl, type);
3909 else
3910 error ("parameter %P has incomplete type %qT", i, type);
3912 parmval = error_mark_node;
3914 else
3916 parmval = convert_for_initialization
3917 (NULL_TREE, type, val, flags,
3918 ICR_ARGPASS, fndecl, i, complain);
3919 parmval = convert_for_arg_passing (type, parmval, complain);
3922 if (parmval == error_mark_node)
3923 return -1;
3925 (**values)[i] = parmval;
3927 else
3929 if (fndecl && magic_varargs_p (fndecl))
3930 /* Don't do ellipsis conversion for __built_in_constant_p
3931 as this will result in spurious errors for non-trivial
3932 types. */
3933 val = require_complete_type_sfinae (val, complain);
3934 else
3935 val = convert_arg_to_ellipsis (val, complain);
3937 (**values)[i] = val;
3940 if (typetail)
3941 typetail = TREE_CHAIN (typetail);
3944 if (typetail != 0 && typetail != void_list_node)
3946 /* See if there are default arguments that can be used. Because
3947 we hold default arguments in the FUNCTION_TYPE (which is so
3948 wrong), we can see default parameters here from deduced
3949 contexts (and via typeof) for indirect function calls.
3950 Fortunately we know whether we have a function decl to
3951 provide default arguments in a language conformant
3952 manner. */
3953 if (fndecl && TREE_PURPOSE (typetail)
3954 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3956 for (; typetail != void_list_node; ++i)
3958 /* After DR777, with explicit template args we can end up with a
3959 default argument followed by no default argument. */
3960 if (!TREE_PURPOSE (typetail))
3961 break;
3962 tree parmval
3963 = convert_default_arg (TREE_VALUE (typetail),
3964 TREE_PURPOSE (typetail),
3965 fndecl, i, complain);
3967 if (parmval == error_mark_node)
3968 return -1;
3970 vec_safe_push (*values, parmval);
3971 typetail = TREE_CHAIN (typetail);
3972 /* ends with `...'. */
3973 if (typetail == NULL_TREE)
3974 break;
3978 if (typetail && typetail != void_list_node)
3980 if (complain & tf_error)
3981 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3982 return -1;
3986 return (int) i;
3989 /* Build a binary-operation expression, after performing default
3990 conversions on the operands. CODE is the kind of expression to
3991 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3992 are the tree codes which correspond to ARG1 and ARG2 when issuing
3993 warnings about possibly misplaced parentheses. They may differ
3994 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3995 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3996 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3997 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3998 ARG2_CODE as ERROR_MARK. */
4000 tree
4001 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4002 enum tree_code arg1_code, tree arg2,
4003 enum tree_code arg2_code, tree *overload_p,
4004 tsubst_flags_t complain)
4006 tree orig_arg1;
4007 tree orig_arg2;
4008 tree expr;
4009 tree overload = NULL_TREE;
4011 orig_arg1 = arg1;
4012 orig_arg2 = arg2;
4014 if (processing_template_decl)
4016 if (type_dependent_expression_p (arg1)
4017 || type_dependent_expression_p (arg2))
4018 return build_min_nt_loc (loc, code, arg1, arg2);
4019 arg1 = build_non_dependent_expr (arg1);
4020 arg2 = build_non_dependent_expr (arg2);
4023 if (code == DOTSTAR_EXPR)
4024 expr = build_m_component_ref (arg1, arg2, complain);
4025 else
4026 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4027 &overload, complain);
4029 if (overload_p != NULL)
4030 *overload_p = overload;
4032 /* Check for cases such as x+y<<z which users are likely to
4033 misinterpret. But don't warn about obj << x + y, since that is a
4034 common idiom for I/O. */
4035 if (warn_parentheses
4036 && (complain & tf_warning)
4037 && !processing_template_decl
4038 && !error_operand_p (arg1)
4039 && !error_operand_p (arg2)
4040 && (code != LSHIFT_EXPR
4041 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4042 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4043 arg2_code, orig_arg2);
4045 if (processing_template_decl && expr != error_mark_node)
4047 if (overload != NULL_TREE)
4048 return (build_min_non_dep_op_overload
4049 (code, expr, overload, orig_arg1, orig_arg2));
4051 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4054 return expr;
4057 /* Build and return an ARRAY_REF expression. */
4059 tree
4060 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4061 tsubst_flags_t complain)
4063 tree orig_arg1 = arg1;
4064 tree orig_arg2 = arg2;
4065 tree expr;
4066 tree overload = NULL_TREE;
4068 if (processing_template_decl)
4070 if (type_dependent_expression_p (arg1)
4071 || type_dependent_expression_p (arg2))
4072 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4073 NULL_TREE, NULL_TREE);
4074 arg1 = build_non_dependent_expr (arg1);
4075 arg2 = build_non_dependent_expr (arg2);
4078 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4079 NULL_TREE, &overload, complain);
4081 if (processing_template_decl && expr != error_mark_node)
4083 if (overload != NULL_TREE)
4084 return (build_min_non_dep_op_overload
4085 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4087 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4088 NULL_TREE, NULL_TREE);
4090 return expr;
4093 /* Return whether OP is an expression of enum type cast to integer
4094 type. In C++ even unsigned enum types are cast to signed integer
4095 types. We do not want to issue warnings about comparisons between
4096 signed and unsigned types when one of the types is an enum type.
4097 Those warnings are always false positives in practice. */
4099 static bool
4100 enum_cast_to_int (tree op)
4102 if (CONVERT_EXPR_P (op)
4103 && TREE_TYPE (op) == integer_type_node
4104 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4105 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4106 return true;
4108 /* The cast may have been pushed into a COND_EXPR. */
4109 if (TREE_CODE (op) == COND_EXPR)
4110 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4111 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4113 return false;
4116 /* For the c-common bits. */
4117 tree
4118 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4119 bool /*convert_p*/)
4121 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4124 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4125 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4127 static tree
4128 build_vec_cmp (tree_code code, tree type,
4129 tree arg0, tree arg1)
4131 tree zero_vec = build_zero_cst (type);
4132 tree minus_one_vec = build_minus_one_cst (type);
4133 tree cmp_type = build_same_sized_truth_vector_type(type);
4134 tree cmp = build2 (code, cmp_type, arg0, arg1);
4135 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4138 /* Possibly warn about an address never being NULL. */
4140 static void
4141 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4143 if (!warn_address
4144 || (complain & tf_warning) == 0
4145 || c_inhibit_evaluation_warnings != 0
4146 || TREE_NO_WARNING (op))
4147 return;
4149 tree cop = fold_non_dependent_expr (op);
4151 if (TREE_CODE (cop) == ADDR_EXPR
4152 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4153 && !TREE_NO_WARNING (cop))
4154 warning_at (location, OPT_Waddress, "the address of %qD will never "
4155 "be NULL", TREE_OPERAND (cop, 0));
4157 if (CONVERT_EXPR_P (op)
4158 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4160 tree inner_op = op;
4161 STRIP_NOPS (inner_op);
4163 if (DECL_P (inner_op))
4164 warning_at (location, OPT_Waddress,
4165 "the compiler can assume that the address of "
4166 "%qD will never be NULL", inner_op);
4170 /* Build a binary-operation expression without default conversions.
4171 CODE is the kind of expression to build.
4172 LOCATION is the location_t of the operator in the source code.
4173 This function differs from `build' in several ways:
4174 the data type of the result is computed and recorded in it,
4175 warnings are generated if arg data types are invalid,
4176 special handling for addition and subtraction of pointers is known,
4177 and some optimization is done (operations on narrow ints
4178 are done in the narrower type when that gives the same result).
4179 Constant folding is also done before the result is returned.
4181 Note that the operands will never have enumeral types
4182 because either they have just had the default conversions performed
4183 or they have both just been converted to some other type in which
4184 the arithmetic is to be done.
4186 C++: must do special pointer arithmetic when implementing
4187 multiple inheritance, and deal with pointer to member functions. */
4189 tree
4190 cp_build_binary_op (location_t location,
4191 enum tree_code code, tree orig_op0, tree orig_op1,
4192 tsubst_flags_t complain)
4194 tree op0, op1;
4195 enum tree_code code0, code1;
4196 tree type0, type1;
4197 const char *invalid_op_diag;
4199 /* Expression code to give to the expression when it is built.
4200 Normally this is CODE, which is what the caller asked for,
4201 but in some special cases we change it. */
4202 enum tree_code resultcode = code;
4204 /* Data type in which the computation is to be performed.
4205 In the simplest cases this is the common type of the arguments. */
4206 tree result_type = NULL_TREE;
4208 /* Nonzero means operands have already been type-converted
4209 in whatever way is necessary.
4210 Zero means they need to be converted to RESULT_TYPE. */
4211 int converted = 0;
4213 /* Nonzero means create the expression with this type, rather than
4214 RESULT_TYPE. */
4215 tree build_type = 0;
4217 /* Nonzero means after finally constructing the expression
4218 convert it to this type. */
4219 tree final_type = 0;
4221 tree result, result_ovl;
4223 /* Nonzero if this is an operation like MIN or MAX which can
4224 safely be computed in short if both args are promoted shorts.
4225 Also implies COMMON.
4226 -1 indicates a bitwise operation; this makes a difference
4227 in the exact conditions for when it is safe to do the operation
4228 in a narrower mode. */
4229 int shorten = 0;
4231 /* Nonzero if this is a comparison operation;
4232 if both args are promoted shorts, compare the original shorts.
4233 Also implies COMMON. */
4234 int short_compare = 0;
4236 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4237 int common = 0;
4239 /* True if both operands have arithmetic type. */
4240 bool arithmetic_types_p;
4242 /* Apply default conversions. */
4243 op0 = orig_op0;
4244 op1 = orig_op1;
4246 /* Remember whether we're doing / or %. */
4247 bool doing_div_or_mod = false;
4249 /* Remember whether we're doing << or >>. */
4250 bool doing_shift = false;
4252 /* Tree holding instrumentation expression. */
4253 tree instrument_expr = NULL_TREE;
4255 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4256 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4257 || code == TRUTH_XOR_EXPR)
4259 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4260 op0 = decay_conversion (op0, complain);
4261 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4262 op1 = decay_conversion (op1, complain);
4264 else
4266 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4267 op0 = cp_default_conversion (op0, complain);
4268 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4269 op1 = cp_default_conversion (op1, complain);
4272 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4273 STRIP_TYPE_NOPS (op0);
4274 STRIP_TYPE_NOPS (op1);
4276 /* DTRT if one side is an overloaded function, but complain about it. */
4277 if (type_unknown_p (op0))
4279 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4280 if (t != error_mark_node)
4282 if (complain & tf_error)
4283 permerror (input_location, "assuming cast to type %qT from overloaded function",
4284 TREE_TYPE (t));
4285 op0 = t;
4288 if (type_unknown_p (op1))
4290 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4291 if (t != error_mark_node)
4293 if (complain & tf_error)
4294 permerror (input_location, "assuming cast to type %qT from overloaded function",
4295 TREE_TYPE (t));
4296 op1 = t;
4300 type0 = TREE_TYPE (op0);
4301 type1 = TREE_TYPE (op1);
4303 /* The expression codes of the data types of the arguments tell us
4304 whether the arguments are integers, floating, pointers, etc. */
4305 code0 = TREE_CODE (type0);
4306 code1 = TREE_CODE (type1);
4308 /* If an error was already reported for one of the arguments,
4309 avoid reporting another error. */
4310 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4311 return error_mark_node;
4313 if ((invalid_op_diag
4314 = targetm.invalid_binary_op (code, type0, type1)))
4316 if (complain & tf_error)
4317 error (invalid_op_diag);
4318 return error_mark_node;
4321 /* Issue warnings about peculiar, but valid, uses of NULL. */
4322 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4323 /* It's reasonable to use pointer values as operands of &&
4324 and ||, so NULL is no exception. */
4325 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4326 && ( /* Both are NULL (or 0) and the operation was not a
4327 comparison or a pointer subtraction. */
4328 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4329 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4330 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4331 || (!null_ptr_cst_p (orig_op0)
4332 && !TYPE_PTR_OR_PTRMEM_P (type0))
4333 || (!null_ptr_cst_p (orig_op1)
4334 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4335 && (complain & tf_warning))
4337 source_location loc =
4338 expansion_point_location_if_in_system_header (input_location);
4340 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4343 /* In case when one of the operands of the binary operation is
4344 a vector and another is a scalar -- convert scalar to vector. */
4345 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4347 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4348 complain & tf_error);
4350 switch (convert_flag)
4352 case stv_error:
4353 return error_mark_node;
4354 case stv_firstarg:
4356 op0 = convert (TREE_TYPE (type1), op0);
4357 op0 = save_expr (op0);
4358 op0 = build_vector_from_val (type1, op0);
4359 type0 = TREE_TYPE (op0);
4360 code0 = TREE_CODE (type0);
4361 converted = 1;
4362 break;
4364 case stv_secondarg:
4366 op1 = convert (TREE_TYPE (type0), op1);
4367 op1 = save_expr (op1);
4368 op1 = build_vector_from_val (type0, op1);
4369 type1 = TREE_TYPE (op1);
4370 code1 = TREE_CODE (type1);
4371 converted = 1;
4372 break;
4374 default:
4375 break;
4379 switch (code)
4381 case MINUS_EXPR:
4382 /* Subtraction of two similar pointers.
4383 We must subtract them as integers, then divide by object size. */
4384 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4385 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4386 TREE_TYPE (type1)))
4388 result = pointer_diff (location, op0, op1,
4389 common_pointer_type (type0, type1), complain,
4390 &instrument_expr);
4391 if (instrument_expr != NULL)
4392 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4393 instrument_expr, result);
4395 return result;
4397 /* In all other cases except pointer - int, the usual arithmetic
4398 rules apply. */
4399 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4401 common = 1;
4402 break;
4404 /* The pointer - int case is just like pointer + int; fall
4405 through. */
4406 gcc_fallthrough ();
4407 case PLUS_EXPR:
4408 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4409 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4411 tree ptr_operand;
4412 tree int_operand;
4413 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4414 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4415 if (processing_template_decl)
4417 result_type = TREE_TYPE (ptr_operand);
4418 break;
4420 return cp_pointer_int_sum (location, code,
4421 ptr_operand,
4422 int_operand,
4423 complain);
4425 common = 1;
4426 break;
4428 case MULT_EXPR:
4429 common = 1;
4430 break;
4432 case TRUNC_DIV_EXPR:
4433 case CEIL_DIV_EXPR:
4434 case FLOOR_DIV_EXPR:
4435 case ROUND_DIV_EXPR:
4436 case EXACT_DIV_EXPR:
4437 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4439 tree type0 = TREE_OPERAND (op0, 0);
4440 tree type1 = TREE_OPERAND (op1, 0);
4441 tree first_arg = type0;
4442 if (!TYPE_P (type0))
4443 type0 = TREE_TYPE (type0);
4444 if (!TYPE_P (type1))
4445 type1 = TREE_TYPE (type1);
4446 if (POINTER_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4447 && !(TREE_CODE (first_arg) == PARM_DECL
4448 && DECL_ARRAY_PARAMETER_P (first_arg)
4449 && warn_sizeof_array_argument)
4450 && (complain & tf_warning))
4451 if (warning_at (location, OPT_Wsizeof_pointer_div,
4452 "division %<sizeof (%T) / sizeof (%T)%> does "
4453 "not compute the number of array elements",
4454 type0, type1))
4455 if (DECL_P (first_arg))
4456 inform (DECL_SOURCE_LOCATION (first_arg),
4457 "first %<sizeof%> operand was declared here");
4460 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4461 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4462 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4463 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4465 enum tree_code tcode0 = code0, tcode1 = code1;
4466 tree cop1 = fold_non_dependent_expr (op1);
4467 doing_div_or_mod = true;
4468 warn_for_div_by_zero (location, cop1);
4470 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4471 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4472 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4473 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4475 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4476 resultcode = RDIV_EXPR;
4477 else
4478 /* When dividing two signed integers, we have to promote to int.
4479 unless we divide by a constant != -1. Note that default
4480 conversion will have been performed on the operands at this
4481 point, so we have to dig out the original type to find out if
4482 it was unsigned. */
4483 shorten = ((TREE_CODE (op0) == NOP_EXPR
4484 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4485 || (TREE_CODE (op1) == INTEGER_CST
4486 && ! integer_all_onesp (op1)));
4488 common = 1;
4490 break;
4492 case BIT_AND_EXPR:
4493 case BIT_IOR_EXPR:
4494 case BIT_XOR_EXPR:
4495 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4496 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4497 && !VECTOR_FLOAT_TYPE_P (type0)
4498 && !VECTOR_FLOAT_TYPE_P (type1)))
4499 shorten = -1;
4500 break;
4502 case TRUNC_MOD_EXPR:
4503 case FLOOR_MOD_EXPR:
4505 tree cop1 = fold_non_dependent_expr (op1);
4506 doing_div_or_mod = true;
4507 warn_for_div_by_zero (location, cop1);
4510 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4511 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4512 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4513 common = 1;
4514 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4516 /* Although it would be tempting to shorten always here, that loses
4517 on some targets, since the modulo instruction is undefined if the
4518 quotient can't be represented in the computation mode. We shorten
4519 only if unsigned or if dividing by something we know != -1. */
4520 shorten = ((TREE_CODE (op0) == NOP_EXPR
4521 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4522 || (TREE_CODE (op1) == INTEGER_CST
4523 && ! integer_all_onesp (op1)));
4524 common = 1;
4526 break;
4528 case TRUTH_ANDIF_EXPR:
4529 case TRUTH_ORIF_EXPR:
4530 case TRUTH_AND_EXPR:
4531 case TRUTH_OR_EXPR:
4532 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4534 if (!COMPARISON_CLASS_P (op1))
4535 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4536 build_zero_cst (type1), complain);
4537 if (code == TRUTH_ANDIF_EXPR)
4539 tree z = build_zero_cst (TREE_TYPE (op1));
4540 return build_conditional_expr (location, op0, op1, z, complain);
4542 else if (code == TRUTH_ORIF_EXPR)
4544 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4545 return build_conditional_expr (location, op0, m1, op1, complain);
4547 else
4548 gcc_unreachable ();
4550 if (VECTOR_TYPE_P (type0))
4552 if (!COMPARISON_CLASS_P (op0))
4553 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4554 build_zero_cst (type0), complain);
4555 if (!VECTOR_TYPE_P (type1))
4557 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4558 tree z = build_zero_cst (TREE_TYPE (op0));
4559 op1 = build_conditional_expr (location, op1, m1, z, complain);
4561 else if (!COMPARISON_CLASS_P (op1))
4562 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4563 build_zero_cst (type1), complain);
4565 if (code == TRUTH_ANDIF_EXPR)
4566 code = BIT_AND_EXPR;
4567 else if (code == TRUTH_ORIF_EXPR)
4568 code = BIT_IOR_EXPR;
4569 else
4570 gcc_unreachable ();
4572 return cp_build_binary_op (location, code, op0, op1, complain);
4575 result_type = boolean_type_node;
4576 break;
4578 /* Shift operations: result has same type as first operand;
4579 always convert second operand to int.
4580 Also set SHORT_SHIFT if shifting rightward. */
4582 case RSHIFT_EXPR:
4583 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4584 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4586 result_type = type0;
4587 converted = 1;
4589 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4590 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4591 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4592 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4593 TYPE_VECTOR_SUBPARTS (type1)))
4595 result_type = type0;
4596 converted = 1;
4598 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4600 tree const_op1 = fold_non_dependent_expr (op1);
4601 if (TREE_CODE (const_op1) != INTEGER_CST)
4602 const_op1 = op1;
4603 result_type = type0;
4604 doing_shift = true;
4605 if (TREE_CODE (const_op1) == INTEGER_CST)
4607 if (tree_int_cst_lt (const_op1, integer_zero_node))
4609 if ((complain & tf_warning)
4610 && c_inhibit_evaluation_warnings == 0)
4611 warning (OPT_Wshift_count_negative,
4612 "right shift count is negative");
4614 else
4616 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4617 && (complain & tf_warning)
4618 && c_inhibit_evaluation_warnings == 0)
4619 warning (OPT_Wshift_count_overflow,
4620 "right shift count >= width of type");
4623 /* Avoid converting op1 to result_type later. */
4624 converted = 1;
4626 break;
4628 case LSHIFT_EXPR:
4629 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4630 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4632 result_type = type0;
4633 converted = 1;
4635 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4636 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4637 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4638 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4639 TYPE_VECTOR_SUBPARTS (type1)))
4641 result_type = type0;
4642 converted = 1;
4644 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4646 tree const_op0 = fold_non_dependent_expr (op0);
4647 if (TREE_CODE (const_op0) != INTEGER_CST)
4648 const_op0 = op0;
4649 tree const_op1 = fold_non_dependent_expr (op1);
4650 if (TREE_CODE (const_op1) != INTEGER_CST)
4651 const_op1 = op1;
4652 result_type = type0;
4653 doing_shift = true;
4654 if (TREE_CODE (const_op0) == INTEGER_CST
4655 && tree_int_cst_sgn (const_op0) < 0
4656 && (complain & tf_warning)
4657 && c_inhibit_evaluation_warnings == 0)
4658 warning (OPT_Wshift_negative_value,
4659 "left shift of negative value");
4660 if (TREE_CODE (const_op1) == INTEGER_CST)
4662 if (tree_int_cst_lt (const_op1, integer_zero_node))
4664 if ((complain & tf_warning)
4665 && c_inhibit_evaluation_warnings == 0)
4666 warning (OPT_Wshift_count_negative,
4667 "left shift count is negative");
4669 else if (compare_tree_int (const_op1,
4670 TYPE_PRECISION (type0)) >= 0)
4672 if ((complain & tf_warning)
4673 && c_inhibit_evaluation_warnings == 0)
4674 warning (OPT_Wshift_count_overflow,
4675 "left shift count >= width of type");
4677 else if (TREE_CODE (const_op0) == INTEGER_CST
4678 && (complain & tf_warning))
4679 maybe_warn_shift_overflow (location, const_op0, const_op1);
4681 /* Avoid converting op1 to result_type later. */
4682 converted = 1;
4684 break;
4686 case RROTATE_EXPR:
4687 case LROTATE_EXPR:
4688 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4690 result_type = type0;
4691 if (TREE_CODE (op1) == INTEGER_CST)
4693 if (tree_int_cst_lt (op1, integer_zero_node))
4695 if (complain & tf_warning)
4696 warning (0, (code == LROTATE_EXPR)
4697 ? G_("left rotate count is negative")
4698 : G_("right rotate count is negative"));
4700 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4702 if (complain & tf_warning)
4703 warning (0, (code == LROTATE_EXPR)
4704 ? G_("left rotate count >= width of type")
4705 : G_("right rotate count >= width of type"));
4708 /* Convert the shift-count to an integer, regardless of
4709 size of value being shifted. */
4710 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4711 op1 = cp_convert (integer_type_node, op1, complain);
4713 break;
4715 case EQ_EXPR:
4716 case NE_EXPR:
4717 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4718 goto vector_compare;
4719 if ((complain & tf_warning)
4720 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4721 warning (OPT_Wfloat_equal,
4722 "comparing floating point with == or != is unsafe");
4723 if ((complain & tf_warning)
4724 && ((TREE_CODE (orig_op0) == STRING_CST
4725 && !integer_zerop (cp_fully_fold (op1)))
4726 || (TREE_CODE (orig_op1) == STRING_CST
4727 && !integer_zerop (cp_fully_fold (op0)))))
4728 warning (OPT_Waddress, "comparison with string literal results "
4729 "in unspecified behavior");
4731 build_type = boolean_type_node;
4732 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4733 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4734 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4735 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4736 short_compare = 1;
4737 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4738 && null_ptr_cst_p (orig_op1))
4739 /* Handle, eg, (void*)0 (c++/43906), and more. */
4740 || (code0 == POINTER_TYPE
4741 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4743 if (TYPE_PTR_P (type1))
4744 result_type = composite_pointer_type (type0, type1, op0, op1,
4745 CPO_COMPARISON, complain);
4746 else
4747 result_type = type0;
4749 if (char_type_p (TREE_TYPE (orig_op1))
4750 && warning (OPT_Wpointer_compare,
4751 "comparison between pointer and zero character "
4752 "constant"))
4753 inform (input_location,
4754 "did you mean to dereference the pointer?");
4755 warn_for_null_address (location, op0, complain);
4757 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4758 && null_ptr_cst_p (orig_op0))
4759 /* Handle, eg, (void*)0 (c++/43906), and more. */
4760 || (code1 == POINTER_TYPE
4761 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4763 if (TYPE_PTR_P (type0))
4764 result_type = composite_pointer_type (type0, type1, op0, op1,
4765 CPO_COMPARISON, complain);
4766 else
4767 result_type = type1;
4769 if (char_type_p (TREE_TYPE (orig_op0))
4770 && warning (OPT_Wpointer_compare,
4771 "comparison between pointer and zero character "
4772 "constant"))
4773 inform (input_location,
4774 "did you mean to dereference the pointer?");
4775 warn_for_null_address (location, op1, complain);
4777 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4778 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4779 result_type = composite_pointer_type (type0, type1, op0, op1,
4780 CPO_COMPARISON, complain);
4781 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4782 /* One of the operands must be of nullptr_t type. */
4783 result_type = TREE_TYPE (nullptr_node);
4784 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4786 result_type = type0;
4787 if (complain & tf_error)
4788 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4789 else
4790 return error_mark_node;
4792 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4794 result_type = type1;
4795 if (complain & tf_error)
4796 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4797 else
4798 return error_mark_node;
4800 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4802 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4803 == ptrmemfunc_vbit_in_delta)
4805 tree pfn0, delta0, e1, e2;
4807 if (TREE_SIDE_EFFECTS (op0))
4808 op0 = save_expr (op0);
4810 pfn0 = pfn_from_ptrmemfunc (op0);
4811 delta0 = delta_from_ptrmemfunc (op0);
4812 e1 = cp_build_binary_op (location,
4813 EQ_EXPR,
4814 pfn0,
4815 build_zero_cst (TREE_TYPE (pfn0)),
4816 complain);
4817 e2 = cp_build_binary_op (location,
4818 BIT_AND_EXPR,
4819 delta0,
4820 integer_one_node,
4821 complain);
4823 if (complain & tf_warning)
4824 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4826 e2 = cp_build_binary_op (location,
4827 EQ_EXPR, e2, integer_zero_node,
4828 complain);
4829 op0 = cp_build_binary_op (location,
4830 TRUTH_ANDIF_EXPR, e1, e2,
4831 complain);
4832 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4834 else
4836 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4837 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4839 result_type = TREE_TYPE (op0);
4841 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4842 return cp_build_binary_op (location, code, op1, op0, complain);
4843 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4845 tree type;
4846 /* E will be the final comparison. */
4847 tree e;
4848 /* E1 and E2 are for scratch. */
4849 tree e1;
4850 tree e2;
4851 tree pfn0;
4852 tree pfn1;
4853 tree delta0;
4854 tree delta1;
4856 type = composite_pointer_type (type0, type1, op0, op1,
4857 CPO_COMPARISON, complain);
4859 if (!same_type_p (TREE_TYPE (op0), type))
4860 op0 = cp_convert_and_check (type, op0, complain);
4861 if (!same_type_p (TREE_TYPE (op1), type))
4862 op1 = cp_convert_and_check (type, op1, complain);
4864 if (op0 == error_mark_node || op1 == error_mark_node)
4865 return error_mark_node;
4867 if (TREE_SIDE_EFFECTS (op0))
4868 op0 = save_expr (op0);
4869 if (TREE_SIDE_EFFECTS (op1))
4870 op1 = save_expr (op1);
4872 pfn0 = pfn_from_ptrmemfunc (op0);
4873 pfn0 = cp_fully_fold (pfn0);
4874 /* Avoid -Waddress warnings (c++/64877). */
4875 if (TREE_CODE (pfn0) == ADDR_EXPR)
4876 TREE_NO_WARNING (pfn0) = 1;
4877 pfn1 = pfn_from_ptrmemfunc (op1);
4878 pfn1 = cp_fully_fold (pfn1);
4879 delta0 = delta_from_ptrmemfunc (op0);
4880 delta1 = delta_from_ptrmemfunc (op1);
4881 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4882 == ptrmemfunc_vbit_in_delta)
4884 /* We generate:
4886 (op0.pfn == op1.pfn
4887 && ((op0.delta == op1.delta)
4888 || (!op0.pfn && op0.delta & 1 == 0
4889 && op1.delta & 1 == 0))
4891 The reason for the `!op0.pfn' bit is that a NULL
4892 pointer-to-member is any member with a zero PFN and
4893 LSB of the DELTA field is 0. */
4895 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4896 delta0,
4897 integer_one_node,
4898 complain);
4899 e1 = cp_build_binary_op (location,
4900 EQ_EXPR, e1, integer_zero_node,
4901 complain);
4902 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4903 delta1,
4904 integer_one_node,
4905 complain);
4906 e2 = cp_build_binary_op (location,
4907 EQ_EXPR, e2, integer_zero_node,
4908 complain);
4909 e1 = cp_build_binary_op (location,
4910 TRUTH_ANDIF_EXPR, e2, e1,
4911 complain);
4912 e2 = cp_build_binary_op (location, EQ_EXPR,
4913 pfn0,
4914 build_zero_cst (TREE_TYPE (pfn0)),
4915 complain);
4916 e2 = cp_build_binary_op (location,
4917 TRUTH_ANDIF_EXPR, e2, e1, complain);
4918 e1 = cp_build_binary_op (location,
4919 EQ_EXPR, delta0, delta1, complain);
4920 e1 = cp_build_binary_op (location,
4921 TRUTH_ORIF_EXPR, e1, e2, complain);
4923 else
4925 /* We generate:
4927 (op0.pfn == op1.pfn
4928 && (!op0.pfn || op0.delta == op1.delta))
4930 The reason for the `!op0.pfn' bit is that a NULL
4931 pointer-to-member is any member with a zero PFN; the
4932 DELTA field is unspecified. */
4934 e1 = cp_build_binary_op (location,
4935 EQ_EXPR, delta0, delta1, complain);
4936 e2 = cp_build_binary_op (location,
4937 EQ_EXPR,
4938 pfn0,
4939 build_zero_cst (TREE_TYPE (pfn0)),
4940 complain);
4941 e1 = cp_build_binary_op (location,
4942 TRUTH_ORIF_EXPR, e1, e2, complain);
4944 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4945 e = cp_build_binary_op (location,
4946 TRUTH_ANDIF_EXPR, e2, e1, complain);
4947 if (code == EQ_EXPR)
4948 return e;
4949 return cp_build_binary_op (location,
4950 EQ_EXPR, e, integer_zero_node, complain);
4952 else
4954 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4955 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4956 type1));
4957 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4958 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4959 type0));
4962 break;
4964 case MAX_EXPR:
4965 case MIN_EXPR:
4966 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4967 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4968 shorten = 1;
4969 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4970 result_type = composite_pointer_type (type0, type1, op0, op1,
4971 CPO_COMPARISON, complain);
4972 break;
4974 case LE_EXPR:
4975 case GE_EXPR:
4976 case LT_EXPR:
4977 case GT_EXPR:
4978 if (TREE_CODE (orig_op0) == STRING_CST
4979 || TREE_CODE (orig_op1) == STRING_CST)
4981 if (complain & tf_warning)
4982 warning (OPT_Waddress, "comparison with string literal results "
4983 "in unspecified behavior");
4986 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4988 vector_compare:
4989 tree intt;
4990 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4991 TREE_TYPE (type1))
4992 && !vector_types_compatible_elements_p (type0, type1))
4994 if (complain & tf_error)
4996 error_at (location, "comparing vectors with different "
4997 "element types");
4998 inform (location, "operand types are %qT and %qT",
4999 type0, type1);
5001 return error_mark_node;
5004 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5005 TYPE_VECTOR_SUBPARTS (type1)))
5007 if (complain & tf_error)
5009 error_at (location, "comparing vectors with different "
5010 "number of elements");
5011 inform (location, "operand types are %qT and %qT",
5012 type0, type1);
5014 return error_mark_node;
5017 /* It's not precisely specified how the usual arithmetic
5018 conversions apply to the vector types. Here, we use
5019 the unsigned type if one of the operands is signed and
5020 the other one is unsigned. */
5021 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5023 if (!TYPE_UNSIGNED (type0))
5024 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5025 else
5026 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5027 warning_at (location, OPT_Wsign_compare, "comparison between "
5028 "types %qT and %qT", type0, type1);
5031 /* Always construct signed integer vector type. */
5032 intt = c_common_type_for_size
5033 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5034 if (!intt)
5036 if (complain & tf_error)
5037 error_at (location, "could not find an integer type "
5038 "of the same size as %qT", TREE_TYPE (type0));
5039 return error_mark_node;
5041 result_type = build_opaque_vector_type (intt,
5042 TYPE_VECTOR_SUBPARTS (type0));
5043 converted = 1;
5044 return build_vec_cmp (resultcode, result_type, op0, op1);
5046 build_type = boolean_type_node;
5047 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5048 || code0 == ENUMERAL_TYPE)
5049 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5050 || code1 == ENUMERAL_TYPE))
5051 short_compare = 1;
5052 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5053 result_type = composite_pointer_type (type0, type1, op0, op1,
5054 CPO_COMPARISON, complain);
5055 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5057 result_type = type0;
5058 if (extra_warnings && (complain & tf_warning))
5059 warning (OPT_Wextra,
5060 "ordered comparison of pointer with integer zero");
5062 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5064 result_type = type1;
5065 if (extra_warnings && (complain & tf_warning))
5066 warning (OPT_Wextra,
5067 "ordered comparison of pointer with integer zero");
5069 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5070 /* One of the operands must be of nullptr_t type. */
5071 result_type = TREE_TYPE (nullptr_node);
5072 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5074 result_type = type0;
5075 if (complain & tf_error)
5076 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5077 else
5078 return error_mark_node;
5080 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5082 result_type = type1;
5083 if (complain & tf_error)
5084 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5085 else
5086 return error_mark_node;
5089 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5090 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5092 op0 = save_expr (op0);
5093 op1 = save_expr (op1);
5095 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5096 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5099 break;
5101 case UNORDERED_EXPR:
5102 case ORDERED_EXPR:
5103 case UNLT_EXPR:
5104 case UNLE_EXPR:
5105 case UNGT_EXPR:
5106 case UNGE_EXPR:
5107 case UNEQ_EXPR:
5108 build_type = integer_type_node;
5109 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5111 if (complain & tf_error)
5112 error ("unordered comparison on non-floating point argument");
5113 return error_mark_node;
5115 common = 1;
5116 break;
5118 default:
5119 break;
5122 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5123 || code0 == ENUMERAL_TYPE)
5124 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5125 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5126 arithmetic_types_p = 1;
5127 else
5129 arithmetic_types_p = 0;
5130 /* Vector arithmetic is only allowed when both sides are vectors. */
5131 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5133 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5134 || !vector_types_compatible_elements_p (type0, type1))
5136 if (complain & tf_error)
5138 /* "location" already embeds the locations of the
5139 operands, so we don't need to add them separately
5140 to richloc. */
5141 rich_location richloc (line_table, location);
5142 binary_op_error (&richloc, code, type0, type1);
5144 return error_mark_node;
5146 arithmetic_types_p = 1;
5149 /* Determine the RESULT_TYPE, if it is not already known. */
5150 if (!result_type
5151 && arithmetic_types_p
5152 && (shorten || common || short_compare))
5154 result_type = cp_common_type (type0, type1);
5155 if (complain & tf_warning)
5156 do_warn_double_promotion (result_type, type0, type1,
5157 "implicit conversion from %qH to %qI "
5158 "to match other operand of binary "
5159 "expression",
5160 location);
5163 if (!result_type)
5165 if (complain & tf_error)
5166 error_at (location,
5167 "invalid operands of types %qT and %qT to binary %qO",
5168 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5169 return error_mark_node;
5172 /* If we're in a template, the only thing we need to know is the
5173 RESULT_TYPE. */
5174 if (processing_template_decl)
5176 /* Since the middle-end checks the type when doing a build2, we
5177 need to build the tree in pieces. This built tree will never
5178 get out of the front-end as we replace it when instantiating
5179 the template. */
5180 tree tmp = build2 (resultcode,
5181 build_type ? build_type : result_type,
5182 NULL_TREE, op1);
5183 TREE_OPERAND (tmp, 0) = op0;
5184 return tmp;
5187 /* Remember the original type; RESULT_TYPE might be changed later on
5188 by shorten_binary_op. */
5189 tree orig_type = result_type;
5191 if (arithmetic_types_p)
5193 bool first_complex = (code0 == COMPLEX_TYPE);
5194 bool second_complex = (code1 == COMPLEX_TYPE);
5195 int none_complex = (!first_complex && !second_complex);
5197 /* Adapted from patch for c/24581. */
5198 if (first_complex != second_complex
5199 && (code == PLUS_EXPR
5200 || code == MINUS_EXPR
5201 || code == MULT_EXPR
5202 || (code == TRUNC_DIV_EXPR && first_complex))
5203 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5204 && flag_signed_zeros)
5206 /* An operation on mixed real/complex operands must be
5207 handled specially, but the language-independent code can
5208 more easily optimize the plain complex arithmetic if
5209 -fno-signed-zeros. */
5210 tree real_type = TREE_TYPE (result_type);
5211 tree real, imag;
5212 if (first_complex)
5214 if (TREE_TYPE (op0) != result_type)
5215 op0 = cp_convert_and_check (result_type, op0, complain);
5216 if (TREE_TYPE (op1) != real_type)
5217 op1 = cp_convert_and_check (real_type, op1, complain);
5219 else
5221 if (TREE_TYPE (op0) != real_type)
5222 op0 = cp_convert_and_check (real_type, op0, complain);
5223 if (TREE_TYPE (op1) != result_type)
5224 op1 = cp_convert_and_check (result_type, op1, complain);
5226 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5227 return error_mark_node;
5228 if (first_complex)
5230 op0 = save_expr (op0);
5231 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5232 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5233 switch (code)
5235 case MULT_EXPR:
5236 case TRUNC_DIV_EXPR:
5237 op1 = save_expr (op1);
5238 imag = build2 (resultcode, real_type, imag, op1);
5239 /* Fall through. */
5240 case PLUS_EXPR:
5241 case MINUS_EXPR:
5242 real = build2 (resultcode, real_type, real, op1);
5243 break;
5244 default:
5245 gcc_unreachable();
5248 else
5250 op1 = save_expr (op1);
5251 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5252 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5253 switch (code)
5255 case MULT_EXPR:
5256 op0 = save_expr (op0);
5257 imag = build2 (resultcode, real_type, op0, imag);
5258 /* Fall through. */
5259 case PLUS_EXPR:
5260 real = build2 (resultcode, real_type, op0, real);
5261 break;
5262 case MINUS_EXPR:
5263 real = build2 (resultcode, real_type, op0, real);
5264 imag = build1 (NEGATE_EXPR, real_type, imag);
5265 break;
5266 default:
5267 gcc_unreachable();
5270 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5271 return result;
5274 /* For certain operations (which identify themselves by shorten != 0)
5275 if both args were extended from the same smaller type,
5276 do the arithmetic in that type and then extend.
5278 shorten !=0 and !=1 indicates a bitwise operation.
5279 For them, this optimization is safe only if
5280 both args are zero-extended or both are sign-extended.
5281 Otherwise, we might change the result.
5282 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5283 but calculated in (unsigned short) it would be (unsigned short)-1. */
5285 if (shorten && none_complex)
5287 final_type = result_type;
5288 result_type = shorten_binary_op (result_type, op0, op1,
5289 shorten == -1);
5292 /* Comparison operations are shortened too but differently.
5293 They identify themselves by setting short_compare = 1. */
5295 if (short_compare)
5297 /* We call shorten_compare only for diagnostic-reason. */
5298 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5299 xresult_type = result_type;
5300 enum tree_code xresultcode = resultcode;
5301 shorten_compare (location, &xop0, &xop1, &xresult_type,
5302 &xresultcode);
5305 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5306 && warn_sign_compare
5307 /* Do not warn until the template is instantiated; we cannot
5308 bound the ranges of the arguments until that point. */
5309 && !processing_template_decl
5310 && (complain & tf_warning)
5311 && c_inhibit_evaluation_warnings == 0
5312 /* Even unsigned enum types promote to signed int. We don't
5313 want to issue -Wsign-compare warnings for this case. */
5314 && !enum_cast_to_int (orig_op0)
5315 && !enum_cast_to_int (orig_op1))
5317 tree oop0 = maybe_constant_value (orig_op0);
5318 tree oop1 = maybe_constant_value (orig_op1);
5320 if (TREE_CODE (oop0) != INTEGER_CST)
5321 oop0 = cp_fully_fold (orig_op0);
5322 if (TREE_CODE (oop1) != INTEGER_CST)
5323 oop1 = cp_fully_fold (orig_op1);
5324 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5325 result_type, resultcode);
5329 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5330 Then the expression will be built.
5331 It will be given type FINAL_TYPE if that is nonzero;
5332 otherwise, it will be given type RESULT_TYPE. */
5333 if (! converted)
5335 if (TREE_TYPE (op0) != result_type)
5336 op0 = cp_convert_and_check (result_type, op0, complain);
5337 if (TREE_TYPE (op1) != result_type)
5338 op1 = cp_convert_and_check (result_type, op1, complain);
5340 if (op0 == error_mark_node || op1 == error_mark_node)
5341 return error_mark_node;
5344 if (build_type == NULL_TREE)
5345 build_type = result_type;
5347 if (sanitize_flags_p ((SANITIZE_SHIFT
5348 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5349 && current_function_decl != NULL_TREE
5350 && !processing_template_decl
5351 && (doing_div_or_mod || doing_shift))
5353 /* OP0 and/or OP1 might have side-effects. */
5354 op0 = cp_save_expr (op0);
5355 op1 = cp_save_expr (op1);
5356 op0 = fold_non_dependent_expr (op0);
5357 op1 = fold_non_dependent_expr (op1);
5358 if (doing_div_or_mod
5359 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5361 /* For diagnostics we want to use the promoted types without
5362 shorten_binary_op. So convert the arguments to the
5363 original result_type. */
5364 tree cop0 = op0;
5365 tree cop1 = op1;
5366 if (TREE_TYPE (cop0) != orig_type)
5367 cop0 = cp_convert (orig_type, op0, complain);
5368 if (TREE_TYPE (cop1) != orig_type)
5369 cop1 = cp_convert (orig_type, op1, complain);
5370 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5372 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5373 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5376 result = build2_loc (location, resultcode, build_type, op0, op1);
5377 if (final_type != 0)
5378 result = cp_convert (final_type, result, complain);
5380 if (instrument_expr != NULL)
5381 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5382 instrument_expr, result);
5384 if (!processing_template_decl)
5386 op0 = cp_fully_fold (op0);
5387 /* Only consider the second argument if the first isn't overflowed. */
5388 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5389 return result;
5390 op1 = cp_fully_fold (op1);
5391 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5392 return result;
5394 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5395 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5396 return result;
5398 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5399 if (TREE_OVERFLOW_P (result_ovl))
5400 overflow_warning (location, result_ovl);
5402 return result;
5405 /* Build a VEC_PERM_EXPR.
5406 This is a simple wrapper for c_build_vec_perm_expr. */
5407 tree
5408 build_x_vec_perm_expr (location_t loc,
5409 tree arg0, tree arg1, tree arg2,
5410 tsubst_flags_t complain)
5412 tree orig_arg0 = arg0;
5413 tree orig_arg1 = arg1;
5414 tree orig_arg2 = arg2;
5415 if (processing_template_decl)
5417 if (type_dependent_expression_p (arg0)
5418 || type_dependent_expression_p (arg1)
5419 || type_dependent_expression_p (arg2))
5420 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5421 arg0 = build_non_dependent_expr (arg0);
5422 if (arg1)
5423 arg1 = build_non_dependent_expr (arg1);
5424 arg2 = build_non_dependent_expr (arg2);
5426 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5427 if (processing_template_decl && exp != error_mark_node)
5428 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5429 orig_arg1, orig_arg2);
5430 return exp;
5433 /* Return a tree for the sum or difference (RESULTCODE says which)
5434 of pointer PTROP and integer INTOP. */
5436 static tree
5437 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5438 tree intop, tsubst_flags_t complain)
5440 tree res_type = TREE_TYPE (ptrop);
5442 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5443 in certain circumstance (when it's valid to do so). So we need
5444 to make sure it's complete. We don't need to check here, if we
5445 can actually complete it at all, as those checks will be done in
5446 pointer_int_sum() anyway. */
5447 complete_type (TREE_TYPE (res_type));
5449 return pointer_int_sum (loc, resultcode, ptrop,
5450 intop, complain & tf_warning_or_error);
5453 /* Return a tree for the difference of pointers OP0 and OP1.
5454 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5455 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5457 static tree
5458 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5459 tsubst_flags_t complain, tree *instrument_expr)
5461 tree result, inttype;
5462 tree restype = ptrdiff_type_node;
5463 tree target_type = TREE_TYPE (ptrtype);
5465 if (!complete_type_or_else (target_type, NULL_TREE))
5466 return error_mark_node;
5468 if (VOID_TYPE_P (target_type))
5470 if (complain & tf_error)
5471 permerror (loc, "ISO C++ forbids using pointer of "
5472 "type %<void *%> in subtraction");
5473 else
5474 return error_mark_node;
5476 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5478 if (complain & tf_error)
5479 permerror (loc, "ISO C++ forbids using pointer to "
5480 "a function in subtraction");
5481 else
5482 return error_mark_node;
5484 if (TREE_CODE (target_type) == METHOD_TYPE)
5486 if (complain & tf_error)
5487 permerror (loc, "ISO C++ forbids using pointer to "
5488 "a method in subtraction");
5489 else
5490 return error_mark_node;
5493 /* Determine integer type result of the subtraction. This will usually
5494 be the same as the result type (ptrdiff_t), but may need to be a wider
5495 type if pointers for the address space are wider than ptrdiff_t. */
5496 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5497 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5498 else
5499 inttype = restype;
5501 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5503 op0 = save_expr (op0);
5504 op1 = save_expr (op1);
5506 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5507 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5510 /* First do the subtraction, then build the divide operator
5511 and only convert at the very end.
5512 Do not do default conversions in case restype is a short type. */
5514 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5515 pointers. If some platform cannot provide that, or has a larger
5516 ptrdiff_type to support differences larger than half the address
5517 space, cast the pointers to some larger integer type and do the
5518 computations in that type. */
5519 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5520 op0 = cp_build_binary_op (loc,
5521 MINUS_EXPR,
5522 cp_convert (inttype, op0, complain),
5523 cp_convert (inttype, op1, complain),
5524 complain);
5525 else
5526 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5528 /* This generates an error if op1 is a pointer to an incomplete type. */
5529 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5531 if (complain & tf_error)
5532 error_at (loc, "invalid use of a pointer to an incomplete type in "
5533 "pointer arithmetic");
5534 else
5535 return error_mark_node;
5538 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5540 if (complain & tf_error)
5541 error_at (loc, "arithmetic on pointer to an empty aggregate");
5542 else
5543 return error_mark_node;
5546 op1 = (TYPE_PTROB_P (ptrtype)
5547 ? size_in_bytes_loc (loc, target_type)
5548 : integer_one_node);
5550 /* Do the division. */
5552 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5553 cp_convert (inttype, op1, complain));
5554 return cp_convert (restype, result, complain);
5557 /* Construct and perhaps optimize a tree representation
5558 for a unary operation. CODE, a tree_code, specifies the operation
5559 and XARG is the operand. */
5561 tree
5562 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5563 tsubst_flags_t complain)
5565 tree orig_expr = xarg;
5566 tree exp;
5567 int ptrmem = 0;
5568 tree overload = NULL_TREE;
5570 if (processing_template_decl)
5572 if (type_dependent_expression_p (xarg))
5573 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5575 xarg = build_non_dependent_expr (xarg);
5578 exp = NULL_TREE;
5580 /* [expr.unary.op] says:
5582 The address of an object of incomplete type can be taken.
5584 (And is just the ordinary address operator, not an overloaded
5585 "operator &".) However, if the type is a template
5586 specialization, we must complete the type at this point so that
5587 an overloaded "operator &" will be available if required. */
5588 if (code == ADDR_EXPR
5589 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5590 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5591 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5592 || (TREE_CODE (xarg) == OFFSET_REF)))
5593 /* Don't look for a function. */;
5594 else
5595 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5596 NULL_TREE, &overload, complain);
5598 if (!exp && code == ADDR_EXPR)
5600 if (is_overloaded_fn (xarg))
5602 tree fn = get_first_fn (xarg);
5603 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5605 if (complain & tf_error)
5606 error (DECL_CONSTRUCTOR_P (fn)
5607 ? G_("taking address of constructor %qD")
5608 : G_("taking address of destructor %qD"),
5609 fn);
5610 return error_mark_node;
5614 /* A pointer to member-function can be formed only by saying
5615 &X::mf. */
5616 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5617 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5619 if (TREE_CODE (xarg) != OFFSET_REF
5620 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5622 if (complain & tf_error)
5624 error ("invalid use of %qE to form a "
5625 "pointer-to-member-function", xarg.get_value ());
5626 if (TREE_CODE (xarg) != OFFSET_REF)
5627 inform (input_location, " a qualified-id is required");
5629 return error_mark_node;
5631 else
5633 if (complain & tf_error)
5634 error ("parentheses around %qE cannot be used to form a"
5635 " pointer-to-member-function",
5636 xarg.get_value ());
5637 else
5638 return error_mark_node;
5639 PTRMEM_OK_P (xarg) = 1;
5643 if (TREE_CODE (xarg) == OFFSET_REF)
5645 ptrmem = PTRMEM_OK_P (xarg);
5647 if (!ptrmem && !flag_ms_extensions
5648 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5650 /* A single non-static member, make sure we don't allow a
5651 pointer-to-member. */
5652 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5653 TREE_OPERAND (xarg, 0),
5654 ovl_make (TREE_OPERAND (xarg, 1)));
5655 PTRMEM_OK_P (xarg) = ptrmem;
5659 exp = cp_build_addr_expr_strict (xarg, complain);
5662 if (processing_template_decl && exp != error_mark_node)
5664 if (overload != NULL_TREE)
5665 return (build_min_non_dep_op_overload
5666 (code, exp, overload, orig_expr, integer_zero_node));
5668 exp = build_min_non_dep (code, exp, orig_expr,
5669 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5671 if (TREE_CODE (exp) == ADDR_EXPR)
5672 PTRMEM_OK_P (exp) = ptrmem;
5673 return exp;
5676 /* Construct and perhaps optimize a tree representation
5677 for __builtin_addressof operation. ARG specifies the operand. */
5679 tree
5680 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5682 tree orig_expr = arg;
5684 if (processing_template_decl)
5686 if (type_dependent_expression_p (arg))
5687 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5689 arg = build_non_dependent_expr (arg);
5692 tree exp = cp_build_addr_expr_strict (arg, complain);
5694 if (processing_template_decl && exp != error_mark_node)
5695 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5696 return exp;
5699 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5700 constants, where a null value is represented by an INTEGER_CST of
5701 -1. */
5703 tree
5704 cp_truthvalue_conversion (tree expr)
5706 tree type = TREE_TYPE (expr);
5707 if (TYPE_PTR_OR_PTRMEM_P (type)
5708 /* Avoid ICE on invalid use of non-static member function. */
5709 || TREE_CODE (expr) == FUNCTION_DECL)
5710 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5711 else
5712 return c_common_truthvalue_conversion (input_location, expr);
5715 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5717 tree
5718 condition_conversion (tree expr)
5720 tree t;
5721 /* Anything that might happen in a template should go through
5722 maybe_convert_cond. */
5723 gcc_assert (!processing_template_decl);
5724 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5725 tf_warning_or_error, LOOKUP_NORMAL);
5726 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5727 return t;
5730 /* Returns the address of T. This function will fold away
5731 ADDR_EXPR of INDIRECT_REF. */
5733 tree
5734 build_address (tree t)
5736 if (error_operand_p (t) || !cxx_mark_addressable (t))
5737 return error_mark_node;
5738 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5739 || processing_template_decl);
5740 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5741 if (TREE_CODE (t) != ADDR_EXPR)
5742 t = rvalue (t);
5743 return t;
5746 /* Return a NOP_EXPR converting EXPR to TYPE. */
5748 tree
5749 build_nop (tree type, tree expr)
5751 if (type == error_mark_node || error_operand_p (expr))
5752 return expr;
5753 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5756 /* Take the address of ARG, whatever that means under C++ semantics.
5757 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5758 and class rvalues as well.
5760 Nothing should call this function directly; instead, callers should use
5761 cp_build_addr_expr or cp_build_addr_expr_strict. */
5763 static tree
5764 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5766 tree argtype;
5767 tree val;
5769 if (!arg || error_operand_p (arg))
5770 return error_mark_node;
5772 arg = mark_lvalue_use (arg);
5773 if (error_operand_p (arg))
5774 return error_mark_node;
5776 argtype = lvalue_type (arg);
5778 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5780 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5781 && !really_overloaded_fn (arg))
5783 /* They're trying to take the address of a unique non-static
5784 member function. This is ill-formed (except in MS-land),
5785 but let's try to DTRT.
5786 Note: We only handle unique functions here because we don't
5787 want to complain if there's a static overload; non-unique
5788 cases will be handled by instantiate_type. But we need to
5789 handle this case here to allow casts on the resulting PMF.
5790 We could defer this in non-MS mode, but it's easier to give
5791 a useful error here. */
5793 /* Inside constant member functions, the `this' pointer
5794 contains an extra const qualifier. TYPE_MAIN_VARIANT
5795 is used here to remove this const from the diagnostics
5796 and the created OFFSET_REF. */
5797 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5798 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5799 if (!mark_used (fn, complain) && !(complain & tf_error))
5800 return error_mark_node;
5802 if (! flag_ms_extensions)
5804 tree name = DECL_NAME (fn);
5805 if (!(complain & tf_error))
5806 return error_mark_node;
5807 else if (current_class_type
5808 && TREE_OPERAND (arg, 0) == current_class_ref)
5809 /* An expression like &memfn. */
5810 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5811 " or parenthesized non-static member function to form"
5812 " a pointer to member function. Say %<&%T::%D%>",
5813 base, name);
5814 else
5815 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5816 " function to form a pointer to member function."
5817 " Say %<&%T::%D%>",
5818 base, name);
5820 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5823 /* Uninstantiated types are all functions. Taking the
5824 address of a function is a no-op, so just return the
5825 argument. */
5826 if (type_unknown_p (arg))
5827 return build1 (ADDR_EXPR, unknown_type_node, arg);
5829 if (TREE_CODE (arg) == OFFSET_REF)
5830 /* We want a pointer to member; bypass all the code for actually taking
5831 the address of something. */
5832 goto offset_ref;
5834 /* Anything not already handled and not a true memory reference
5835 is an error. */
5836 if (TREE_CODE (argtype) != FUNCTION_TYPE
5837 && TREE_CODE (argtype) != METHOD_TYPE)
5839 cp_lvalue_kind kind = lvalue_kind (arg);
5840 if (kind == clk_none)
5842 if (complain & tf_error)
5843 lvalue_error (input_location, lv_addressof);
5844 return error_mark_node;
5846 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5848 if (!(complain & tf_error))
5849 return error_mark_node;
5850 if (kind & clk_class)
5851 /* Make this a permerror because we used to accept it. */
5852 permerror (input_location, "taking address of temporary");
5853 else
5854 error ("taking address of xvalue (rvalue reference)");
5858 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5860 tree type = build_pointer_type (TREE_TYPE (argtype));
5861 arg = build1 (CONVERT_EXPR, type, arg);
5862 return arg;
5864 else if (pedantic && DECL_MAIN_P (arg))
5866 /* ARM $3.4 */
5867 /* Apparently a lot of autoconf scripts for C++ packages do this,
5868 so only complain if -Wpedantic. */
5869 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5870 pedwarn (input_location, OPT_Wpedantic,
5871 "ISO C++ forbids taking address of function %<::main%>");
5872 else if (flag_pedantic_errors)
5873 return error_mark_node;
5876 /* Let &* cancel out to simplify resulting code. */
5877 if (INDIRECT_REF_P (arg))
5879 arg = TREE_OPERAND (arg, 0);
5880 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5882 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5883 arg = build1 (CONVERT_EXPR, type, arg);
5885 else
5886 /* Don't let this be an lvalue. */
5887 arg = rvalue (arg);
5888 return arg;
5891 /* ??? Cope with user tricks that amount to offsetof. */
5892 if (TREE_CODE (argtype) != FUNCTION_TYPE
5893 && TREE_CODE (argtype) != METHOD_TYPE
5894 && argtype != unknown_type_node
5895 && (val = get_base_address (arg))
5896 && COMPLETE_TYPE_P (TREE_TYPE (val))
5897 && INDIRECT_REF_P (val)
5898 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5900 tree type = build_pointer_type (argtype);
5901 return fold_convert (type, fold_offsetof_1 (arg));
5904 /* Handle complex lvalues (when permitted)
5905 by reduction to simpler cases. */
5906 val = unary_complex_lvalue (ADDR_EXPR, arg);
5907 if (val != 0)
5908 return val;
5910 switch (TREE_CODE (arg))
5912 CASE_CONVERT:
5913 case FLOAT_EXPR:
5914 case FIX_TRUNC_EXPR:
5915 /* We should have handled this above in the lvalue_kind check. */
5916 gcc_unreachable ();
5917 break;
5919 case BASELINK:
5920 arg = BASELINK_FUNCTIONS (arg);
5921 /* Fall through. */
5923 case OVERLOAD:
5924 arg = OVL_FIRST (arg);
5925 break;
5927 case OFFSET_REF:
5928 offset_ref:
5929 /* Turn a reference to a non-static data member into a
5930 pointer-to-member. */
5932 tree type;
5933 tree t;
5935 gcc_assert (PTRMEM_OK_P (arg));
5937 t = TREE_OPERAND (arg, 1);
5938 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5940 if (complain & tf_error)
5941 error ("cannot create pointer to reference member %qD", t);
5942 return error_mark_node;
5945 type = build_ptrmem_type (context_for_name_lookup (t),
5946 TREE_TYPE (t));
5947 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5948 return t;
5951 default:
5952 break;
5955 if (argtype != error_mark_node)
5956 argtype = build_pointer_type (argtype);
5958 if (bitfield_p (arg))
5960 if (complain & tf_error)
5961 error ("attempt to take address of bit-field");
5962 return error_mark_node;
5965 /* In a template, we are processing a non-dependent expression
5966 so we can just form an ADDR_EXPR with the correct type. */
5967 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5969 val = build_address (arg);
5970 if (TREE_CODE (arg) == OFFSET_REF)
5971 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5973 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5975 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5977 /* We can only get here with a single static member
5978 function. */
5979 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5980 && DECL_STATIC_FUNCTION_P (fn));
5981 if (!mark_used (fn, complain) && !(complain & tf_error))
5982 return error_mark_node;
5983 val = build_address (fn);
5984 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5985 /* Do not lose object's side effects. */
5986 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5987 TREE_OPERAND (arg, 0), val);
5989 else
5991 tree object = TREE_OPERAND (arg, 0);
5992 tree field = TREE_OPERAND (arg, 1);
5993 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5994 (TREE_TYPE (object), decl_type_context (field)));
5995 val = build_address (arg);
5998 if (TYPE_PTR_P (argtype)
5999 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6001 build_ptrmemfunc_type (argtype);
6002 val = build_ptrmemfunc (argtype, val, 0,
6003 /*c_cast_p=*/false,
6004 complain);
6007 return val;
6010 /* Take the address of ARG if it has one, even if it's an rvalue. */
6012 tree
6013 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6015 return cp_build_addr_expr_1 (arg, 0, complain);
6018 /* Take the address of ARG, but only if it's an lvalue. */
6020 static tree
6021 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6023 return cp_build_addr_expr_1 (arg, 1, complain);
6026 /* C++: Must handle pointers to members.
6028 Perhaps type instantiation should be extended to handle conversion
6029 from aggregates to types we don't yet know we want? (Or are those
6030 cases typically errors which should be reported?)
6032 NOCONVERT suppresses the default promotions (such as from short to int). */
6034 tree
6035 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6036 tsubst_flags_t complain)
6038 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6039 tree arg = xarg;
6040 location_t location = EXPR_LOC_OR_LOC (arg, input_location);
6041 tree argtype = 0;
6042 const char *errstring = NULL;
6043 tree val;
6044 const char *invalid_op_diag;
6046 if (!arg || error_operand_p (arg))
6047 return error_mark_node;
6049 if ((invalid_op_diag
6050 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6051 ? CONVERT_EXPR
6052 : code),
6053 TREE_TYPE (xarg))))
6055 if (complain & tf_error)
6056 error (invalid_op_diag);
6057 return error_mark_node;
6060 switch (code)
6062 case UNARY_PLUS_EXPR:
6063 case NEGATE_EXPR:
6065 int flags = WANT_ARITH | WANT_ENUM;
6066 /* Unary plus (but not unary minus) is allowed on pointers. */
6067 if (code == UNARY_PLUS_EXPR)
6068 flags |= WANT_POINTER;
6069 arg = build_expr_type_conversion (flags, arg, true);
6070 if (!arg)
6071 errstring = (code == NEGATE_EXPR
6072 ? _("wrong type argument to unary minus")
6073 : _("wrong type argument to unary plus"));
6074 else
6076 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6077 arg = cp_perform_integral_promotions (arg, complain);
6079 /* Make sure the result is not an lvalue: a unary plus or minus
6080 expression is always a rvalue. */
6081 arg = rvalue (arg);
6084 break;
6086 case BIT_NOT_EXPR:
6087 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6089 code = CONJ_EXPR;
6090 if (!noconvert)
6092 arg = cp_default_conversion (arg, complain);
6093 if (arg == error_mark_node)
6094 return error_mark_node;
6097 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6098 | WANT_VECTOR_OR_COMPLEX,
6099 arg, true)))
6100 errstring = _("wrong type argument to bit-complement");
6101 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6103 /* Warn if the expression has boolean value. */
6104 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6105 && (complain & tf_warning)
6106 && warning_at (location, OPT_Wbool_operation,
6107 "%<~%> on an expression of type bool"))
6108 inform (location, "did you mean to use logical not (%<!%>)?");
6109 arg = cp_perform_integral_promotions (arg, complain);
6111 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6112 arg = mark_rvalue_use (arg);
6113 break;
6115 case ABS_EXPR:
6116 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6117 errstring = _("wrong type argument to abs");
6118 else if (!noconvert)
6120 arg = cp_default_conversion (arg, complain);
6121 if (arg == error_mark_node)
6122 return error_mark_node;
6124 break;
6126 case CONJ_EXPR:
6127 /* Conjugating a real value is a no-op, but allow it anyway. */
6128 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6129 errstring = _("wrong type argument to conjugation");
6130 else if (!noconvert)
6132 arg = cp_default_conversion (arg, complain);
6133 if (arg == error_mark_node)
6134 return error_mark_node;
6136 break;
6138 case TRUTH_NOT_EXPR:
6139 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6140 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6141 build_zero_cst (TREE_TYPE (arg)), complain);
6142 arg = perform_implicit_conversion (boolean_type_node, arg,
6143 complain);
6144 val = invert_truthvalue_loc (input_location, arg);
6145 if (arg != error_mark_node)
6146 return val;
6147 errstring = _("in argument to unary !");
6148 break;
6150 case NOP_EXPR:
6151 break;
6153 case REALPART_EXPR:
6154 case IMAGPART_EXPR:
6155 arg = build_real_imag_expr (input_location, code, arg);
6156 return arg;
6158 case PREINCREMENT_EXPR:
6159 case POSTINCREMENT_EXPR:
6160 case PREDECREMENT_EXPR:
6161 case POSTDECREMENT_EXPR:
6162 /* Handle complex lvalues (when permitted)
6163 by reduction to simpler cases. */
6165 val = unary_complex_lvalue (code, arg);
6166 if (val != 0)
6167 return val;
6169 arg = mark_lvalue_use (arg);
6171 /* Increment or decrement the real part of the value,
6172 and don't change the imaginary part. */
6173 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6175 tree real, imag;
6177 arg = cp_stabilize_reference (arg);
6178 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6179 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6180 real = cp_build_unary_op (code, real, true, complain);
6181 if (real == error_mark_node || imag == error_mark_node)
6182 return error_mark_node;
6183 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6184 real, imag);
6187 /* Report invalid types. */
6189 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6190 arg, true)))
6192 if (code == PREINCREMENT_EXPR)
6193 errstring = _("no pre-increment operator for type");
6194 else if (code == POSTINCREMENT_EXPR)
6195 errstring = _("no post-increment operator for type");
6196 else if (code == PREDECREMENT_EXPR)
6197 errstring = _("no pre-decrement operator for type");
6198 else
6199 errstring = _("no post-decrement operator for type");
6200 break;
6202 else if (arg == error_mark_node)
6203 return error_mark_node;
6205 /* Report something read-only. */
6207 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6208 || TREE_READONLY (arg))
6210 if (complain & tf_error)
6211 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6212 || code == POSTINCREMENT_EXPR)
6213 ? lv_increment : lv_decrement));
6214 else
6215 return error_mark_node;
6219 tree inc;
6220 tree declared_type = unlowered_expr_type (arg);
6222 argtype = TREE_TYPE (arg);
6224 /* ARM $5.2.5 last annotation says this should be forbidden. */
6225 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6227 if (complain & tf_error)
6228 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6229 ? G_("ISO C++ forbids incrementing an enum")
6230 : G_("ISO C++ forbids decrementing an enum"));
6231 else
6232 return error_mark_node;
6235 /* Compute the increment. */
6237 if (TYPE_PTR_P (argtype))
6239 tree type = complete_type (TREE_TYPE (argtype));
6241 if (!COMPLETE_OR_VOID_TYPE_P (type))
6243 if (complain & tf_error)
6244 error (((code == PREINCREMENT_EXPR
6245 || code == POSTINCREMENT_EXPR))
6246 ? G_("cannot increment a pointer to incomplete type %qT")
6247 : G_("cannot decrement a pointer to incomplete type %qT"),
6248 TREE_TYPE (argtype));
6249 else
6250 return error_mark_node;
6252 else if (!TYPE_PTROB_P (argtype))
6254 if (complain & tf_error)
6255 pedwarn (input_location, OPT_Wpointer_arith,
6256 (code == PREINCREMENT_EXPR
6257 || code == POSTINCREMENT_EXPR)
6258 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6259 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6260 argtype);
6261 else
6262 return error_mark_node;
6265 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6267 else
6268 inc = VECTOR_TYPE_P (argtype)
6269 ? build_one_cst (argtype)
6270 : integer_one_node;
6272 inc = cp_convert (argtype, inc, complain);
6274 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6275 need to ask Objective-C to build the increment or decrement
6276 expression for it. */
6277 if (objc_is_property_ref (arg))
6278 return objc_build_incr_expr_for_property_ref (input_location, code,
6279 arg, inc);
6281 /* Complain about anything else that is not a true lvalue. */
6282 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6283 || code == POSTINCREMENT_EXPR)
6284 ? lv_increment : lv_decrement),
6285 complain))
6286 return error_mark_node;
6288 /* Forbid using -- or ++ in C++17 on `bool'. */
6289 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6291 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6293 if (complain & tf_error)
6294 error ("use of an operand of type %qT in %<operator--%> "
6295 "is forbidden", boolean_type_node);
6296 return error_mark_node;
6298 else
6300 if (cxx_dialect >= cxx17)
6302 if (complain & tf_error)
6303 error ("use of an operand of type %qT in "
6304 "%<operator++%> is forbidden in C++17",
6305 boolean_type_node);
6306 return error_mark_node;
6308 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6309 else if (!in_system_header_at (input_location))
6310 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6311 "in %<operator++%> is deprecated",
6312 boolean_type_node);
6314 val = boolean_increment (code, arg);
6316 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6317 /* An rvalue has no cv-qualifiers. */
6318 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6319 else
6320 val = build2 (code, TREE_TYPE (arg), arg, inc);
6322 TREE_SIDE_EFFECTS (val) = 1;
6323 return val;
6326 case ADDR_EXPR:
6327 /* Note that this operation never does default_conversion
6328 regardless of NOCONVERT. */
6329 return cp_build_addr_expr (arg, complain);
6331 default:
6332 break;
6335 if (!errstring)
6337 if (argtype == 0)
6338 argtype = TREE_TYPE (arg);
6339 return build1 (code, argtype, arg);
6342 if (complain & tf_error)
6343 error ("%s", errstring);
6344 return error_mark_node;
6347 /* Hook for the c-common bits that build a unary op. */
6348 tree
6349 build_unary_op (location_t /*location*/,
6350 enum tree_code code, tree xarg, bool noconvert)
6352 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6355 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6356 for certain kinds of expressions which are not really lvalues
6357 but which we can accept as lvalues.
6359 If ARG is not a kind of expression we can handle, return
6360 NULL_TREE. */
6362 tree
6363 unary_complex_lvalue (enum tree_code code, tree arg)
6365 /* Inside a template, making these kinds of adjustments is
6366 pointless; we are only concerned with the type of the
6367 expression. */
6368 if (processing_template_decl)
6369 return NULL_TREE;
6371 /* Handle (a, b) used as an "lvalue". */
6372 if (TREE_CODE (arg) == COMPOUND_EXPR)
6374 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6375 tf_warning_or_error);
6376 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6377 TREE_OPERAND (arg, 0), real_result);
6380 /* Handle (a ? b : c) used as an "lvalue". */
6381 if (TREE_CODE (arg) == COND_EXPR
6382 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6383 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6385 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6386 if (TREE_CODE (arg) == MODIFY_EXPR
6387 || TREE_CODE (arg) == PREINCREMENT_EXPR
6388 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6390 tree lvalue = TREE_OPERAND (arg, 0);
6391 if (TREE_SIDE_EFFECTS (lvalue))
6393 lvalue = cp_stabilize_reference (lvalue);
6394 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6395 lvalue, TREE_OPERAND (arg, 1));
6397 return unary_complex_lvalue
6398 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6401 if (code != ADDR_EXPR)
6402 return NULL_TREE;
6404 /* Handle (a = b) used as an "lvalue" for `&'. */
6405 if (TREE_CODE (arg) == MODIFY_EXPR
6406 || TREE_CODE (arg) == INIT_EXPR)
6408 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6409 tf_warning_or_error);
6410 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6411 arg, real_result);
6412 TREE_NO_WARNING (arg) = 1;
6413 return arg;
6416 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6417 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6418 || TREE_CODE (arg) == OFFSET_REF)
6419 return NULL_TREE;
6421 /* We permit compiler to make function calls returning
6422 objects of aggregate type look like lvalues. */
6424 tree targ = arg;
6426 if (TREE_CODE (targ) == SAVE_EXPR)
6427 targ = TREE_OPERAND (targ, 0);
6429 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6431 if (TREE_CODE (arg) == SAVE_EXPR)
6432 targ = arg;
6433 else
6434 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6435 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6438 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6439 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6440 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6443 /* Don't let anything else be handled specially. */
6444 return NULL_TREE;
6447 /* Mark EXP saying that we need to be able to take the
6448 address of it; it should not be allocated in a register.
6449 Value is true if successful. ARRAY_REF_P is true if this
6450 is for ARRAY_REF construction - in that case we don't want
6451 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6452 it is fine to use ARRAY_REFs for vector subscripts on vector
6453 register variables.
6455 C++: we do not allow `current_class_ptr' to be addressable. */
6457 bool
6458 cxx_mark_addressable (tree exp, bool array_ref_p)
6460 tree x = exp;
6462 while (1)
6463 switch (TREE_CODE (x))
6465 case VIEW_CONVERT_EXPR:
6466 if (array_ref_p
6467 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6468 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6469 return true;
6470 /* FALLTHRU */
6471 case ADDR_EXPR:
6472 case COMPONENT_REF:
6473 case ARRAY_REF:
6474 case REALPART_EXPR:
6475 case IMAGPART_EXPR:
6476 x = TREE_OPERAND (x, 0);
6477 break;
6479 case PARM_DECL:
6480 if (x == current_class_ptr)
6482 error ("cannot take the address of %<this%>, which is an rvalue expression");
6483 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6484 return true;
6486 /* Fall through. */
6488 case VAR_DECL:
6489 /* Caller should not be trying to mark initialized
6490 constant fields addressable. */
6491 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6492 || DECL_IN_AGGR_P (x) == 0
6493 || TREE_STATIC (x)
6494 || DECL_EXTERNAL (x));
6495 /* Fall through. */
6497 case RESULT_DECL:
6498 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6499 && !DECL_ARTIFICIAL (x))
6501 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6503 error
6504 ("address of explicit register variable %qD requested", x);
6505 return false;
6507 else if (extra_warnings)
6508 warning
6509 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6511 TREE_ADDRESSABLE (x) = 1;
6512 return true;
6514 case CONST_DECL:
6515 case FUNCTION_DECL:
6516 TREE_ADDRESSABLE (x) = 1;
6517 return true;
6519 case CONSTRUCTOR:
6520 TREE_ADDRESSABLE (x) = 1;
6521 return true;
6523 case TARGET_EXPR:
6524 TREE_ADDRESSABLE (x) = 1;
6525 cxx_mark_addressable (TREE_OPERAND (x, 0));
6526 return true;
6528 default:
6529 return true;
6533 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6535 tree
6536 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6537 tsubst_flags_t complain)
6539 tree orig_ifexp = ifexp;
6540 tree orig_op1 = op1;
6541 tree orig_op2 = op2;
6542 tree expr;
6544 if (processing_template_decl)
6546 /* The standard says that the expression is type-dependent if
6547 IFEXP is type-dependent, even though the eventual type of the
6548 expression doesn't dependent on IFEXP. */
6549 if (type_dependent_expression_p (ifexp)
6550 /* As a GNU extension, the middle operand may be omitted. */
6551 || (op1 && type_dependent_expression_p (op1))
6552 || type_dependent_expression_p (op2))
6553 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6554 ifexp = build_non_dependent_expr (ifexp);
6555 if (op1)
6556 op1 = build_non_dependent_expr (op1);
6557 op2 = build_non_dependent_expr (op2);
6560 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6561 if (processing_template_decl && expr != error_mark_node)
6563 tree min = build_min_non_dep (COND_EXPR, expr,
6564 orig_ifexp, orig_op1, orig_op2);
6565 /* Remember that the result is an lvalue or xvalue. */
6566 if (glvalue_p (expr) && !glvalue_p (min))
6567 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6568 !lvalue_p (expr));
6569 expr = convert_from_reference (min);
6571 return expr;
6574 /* Given a list of expressions, return a compound expression
6575 that performs them all and returns the value of the last of them. */
6577 tree
6578 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6579 tsubst_flags_t complain)
6581 tree expr = TREE_VALUE (list);
6583 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6584 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6586 if (complain & tf_error)
6587 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6588 "list-initializer for non-class type must not "
6589 "be parenthesized");
6590 else
6591 return error_mark_node;
6594 if (TREE_CHAIN (list))
6596 if (complain & tf_error)
6597 switch (exp)
6599 case ELK_INIT:
6600 permerror (input_location, "expression list treated as compound "
6601 "expression in initializer");
6602 break;
6603 case ELK_MEM_INIT:
6604 permerror (input_location, "expression list treated as compound "
6605 "expression in mem-initializer");
6606 break;
6607 case ELK_FUNC_CAST:
6608 permerror (input_location, "expression list treated as compound "
6609 "expression in functional cast");
6610 break;
6611 default:
6612 gcc_unreachable ();
6614 else
6615 return error_mark_node;
6617 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6618 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6619 expr, TREE_VALUE (list), complain);
6622 return expr;
6625 /* Like build_x_compound_expr_from_list, but using a VEC. */
6627 tree
6628 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6629 tsubst_flags_t complain)
6631 if (vec_safe_is_empty (vec))
6632 return NULL_TREE;
6633 else if (vec->length () == 1)
6634 return (*vec)[0];
6635 else
6637 tree expr;
6638 unsigned int ix;
6639 tree t;
6641 if (msg != NULL)
6643 if (complain & tf_error)
6644 permerror (input_location,
6645 "%s expression list treated as compound expression",
6646 msg);
6647 else
6648 return error_mark_node;
6651 expr = (*vec)[0];
6652 for (ix = 1; vec->iterate (ix, &t); ++ix)
6653 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6654 t, complain);
6656 return expr;
6660 /* Handle overloading of the ',' operator when needed. */
6662 tree
6663 build_x_compound_expr (location_t loc, tree op1, tree op2,
6664 tsubst_flags_t complain)
6666 tree result;
6667 tree orig_op1 = op1;
6668 tree orig_op2 = op2;
6669 tree overload = NULL_TREE;
6671 if (processing_template_decl)
6673 if (type_dependent_expression_p (op1)
6674 || type_dependent_expression_p (op2))
6675 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6676 op1 = build_non_dependent_expr (op1);
6677 op2 = build_non_dependent_expr (op2);
6680 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6681 NULL_TREE, &overload, complain);
6682 if (!result)
6683 result = cp_build_compound_expr (op1, op2, complain);
6685 if (processing_template_decl && result != error_mark_node)
6687 if (overload != NULL_TREE)
6688 return (build_min_non_dep_op_overload
6689 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6691 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6694 return result;
6697 /* Like cp_build_compound_expr, but for the c-common bits. */
6699 tree
6700 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6702 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6705 /* Build a compound expression. */
6707 tree
6708 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6710 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6712 if (lhs == error_mark_node || rhs == error_mark_node)
6713 return error_mark_node;
6715 if (TREE_CODE (rhs) == TARGET_EXPR)
6717 /* If the rhs is a TARGET_EXPR, then build the compound
6718 expression inside the target_expr's initializer. This
6719 helps the compiler to eliminate unnecessary temporaries. */
6720 tree init = TREE_OPERAND (rhs, 1);
6722 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6723 TREE_OPERAND (rhs, 1) = init;
6725 return rhs;
6728 if (type_unknown_p (rhs))
6730 if (complain & tf_error)
6731 error ("no context to resolve type of %qE", rhs);
6732 return error_mark_node;
6735 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6738 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6739 casts away constness. CAST gives the type of cast. Returns true
6740 if the cast is ill-formed, false if it is well-formed.
6742 ??? This function warns for casting away any qualifier not just
6743 const. We would like to specify exactly what qualifiers are casted
6744 away.
6747 static bool
6748 check_for_casting_away_constness (tree src_type, tree dest_type,
6749 enum tree_code cast, tsubst_flags_t complain)
6751 /* C-style casts are allowed to cast away constness. With
6752 WARN_CAST_QUAL, we still want to issue a warning. */
6753 if (cast == CAST_EXPR && !warn_cast_qual)
6754 return false;
6756 if (!casts_away_constness (src_type, dest_type, complain))
6757 return false;
6759 switch (cast)
6761 case CAST_EXPR:
6762 if (complain & tf_warning)
6763 warning (OPT_Wcast_qual,
6764 "cast from type %qT to type %qT casts away qualifiers",
6765 src_type, dest_type);
6766 return false;
6768 case STATIC_CAST_EXPR:
6769 if (complain & tf_error)
6770 error ("static_cast from type %qT to type %qT casts away qualifiers",
6771 src_type, dest_type);
6772 return true;
6774 case REINTERPRET_CAST_EXPR:
6775 if (complain & tf_error)
6776 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6777 src_type, dest_type);
6778 return true;
6780 default:
6781 gcc_unreachable();
6785 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6786 void
6787 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6789 if (warn_useless_cast
6790 && complain & tf_warning)
6792 if ((TREE_CODE (type) == REFERENCE_TYPE
6793 && (TYPE_REF_IS_RVALUE (type)
6794 ? xvalue_p (expr) : lvalue_p (expr))
6795 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6796 || same_type_p (TREE_TYPE (expr), type))
6797 warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6801 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6802 void
6803 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6805 if (warn_ignored_qualifiers
6806 && complain & tf_warning
6807 && !CLASS_TYPE_P (type)
6808 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6810 warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6811 "result type");
6815 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6816 (another pointer-to-member type in the same hierarchy) and return
6817 the converted expression. If ALLOW_INVERSE_P is permitted, a
6818 pointer-to-derived may be converted to pointer-to-base; otherwise,
6819 only the other direction is permitted. If C_CAST_P is true, this
6820 conversion is taking place as part of a C-style cast. */
6822 tree
6823 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6824 bool c_cast_p, tsubst_flags_t complain)
6826 if (same_type_p (type, TREE_TYPE (expr)))
6827 return expr;
6829 if (TYPE_PTRDATAMEM_P (type))
6831 tree delta;
6833 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6834 TYPE_PTRMEM_CLASS_TYPE (type),
6835 allow_inverse_p,
6836 c_cast_p, complain);
6837 if (delta == error_mark_node)
6838 return error_mark_node;
6840 if (!integer_zerop (delta))
6842 tree cond, op1, op2;
6844 if (TREE_CODE (expr) == PTRMEM_CST)
6845 expr = cplus_expand_constant (expr);
6846 cond = cp_build_binary_op (input_location,
6847 EQ_EXPR,
6848 expr,
6849 build_int_cst (TREE_TYPE (expr), -1),
6850 complain);
6851 op1 = build_nop (ptrdiff_type_node, expr);
6852 op2 = cp_build_binary_op (input_location,
6853 PLUS_EXPR, op1, delta,
6854 complain);
6856 expr = fold_build3_loc (input_location,
6857 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6861 return build_nop (type, expr);
6863 else
6864 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6865 allow_inverse_p, c_cast_p, complain);
6868 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6869 this static_cast is being attempted as one of the possible casts
6870 allowed by a C-style cast. (In that case, accessibility of base
6871 classes is not considered, and it is OK to cast away
6872 constness.) Return the result of the cast. *VALID_P is set to
6873 indicate whether or not the cast was valid. */
6875 static tree
6876 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6877 bool *valid_p, tsubst_flags_t complain)
6879 tree intype;
6880 tree result;
6881 cp_lvalue_kind clk;
6883 /* Assume the cast is valid. */
6884 *valid_p = true;
6886 intype = unlowered_expr_type (expr);
6888 /* Save casted types in the function's used types hash table. */
6889 used_types_insert (type);
6891 /* A prvalue of non-class type is cv-unqualified. */
6892 if (!CLASS_TYPE_P (type))
6893 type = cv_unqualified (type);
6895 /* [expr.static.cast]
6897 An lvalue of type "cv1 B", where B is a class type, can be cast
6898 to type "reference to cv2 D", where D is a class derived (clause
6899 _class.derived_) from B, if a valid standard conversion from
6900 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6901 same cv-qualification as, or greater cv-qualification than, cv1,
6902 and B is not a virtual base class of D. */
6903 /* We check this case before checking the validity of "TYPE t =
6904 EXPR;" below because for this case:
6906 struct B {};
6907 struct D : public B { D(const B&); };
6908 extern B& b;
6909 void f() { static_cast<const D&>(b); }
6911 we want to avoid constructing a new D. The standard is not
6912 completely clear about this issue, but our interpretation is
6913 consistent with other compilers. */
6914 if (TREE_CODE (type) == REFERENCE_TYPE
6915 && CLASS_TYPE_P (TREE_TYPE (type))
6916 && CLASS_TYPE_P (intype)
6917 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6918 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6919 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6920 build_pointer_type (TYPE_MAIN_VARIANT
6921 (TREE_TYPE (type))),
6922 complain)
6923 && (c_cast_p
6924 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6926 tree base;
6928 if (processing_template_decl)
6929 return expr;
6931 /* There is a standard conversion from "D*" to "B*" even if "B"
6932 is ambiguous or inaccessible. If this is really a
6933 static_cast, then we check both for inaccessibility and
6934 ambiguity. However, if this is a static_cast being performed
6935 because the user wrote a C-style cast, then accessibility is
6936 not considered. */
6937 base = lookup_base (TREE_TYPE (type), intype,
6938 c_cast_p ? ba_unique : ba_check,
6939 NULL, complain);
6940 expr = build_address (expr);
6942 if (sanitize_flags_p (SANITIZE_VPTR))
6944 tree ubsan_check
6945 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6946 intype, expr);
6947 if (ubsan_check)
6948 expr = ubsan_check;
6951 /* Convert from "B*" to "D*". This function will check that "B"
6952 is not a virtual base of "D". Even if we don't have a guarantee
6953 that expr is NULL, if the static_cast is to a reference type,
6954 it is UB if it would be NULL, so omit the non-NULL check. */
6955 expr = build_base_path (MINUS_EXPR, expr, base,
6956 /*nonnull=*/flag_delete_null_pointer_checks,
6957 complain);
6959 /* Convert the pointer to a reference -- but then remember that
6960 there are no expressions with reference type in C++.
6962 We call rvalue so that there's an actual tree code
6963 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6964 is a variable with the same type, the conversion would get folded
6965 away, leaving just the variable and causing lvalue_kind to give
6966 the wrong answer. */
6967 expr = cp_fold_convert (type, expr);
6969 /* When -fsanitize=null, make sure to diagnose reference binding to
6970 NULL even when the reference is converted to pointer later on. */
6971 if (sanitize_flags_p (SANITIZE_NULL)
6972 && TREE_CODE (expr) == COND_EXPR
6973 && TREE_OPERAND (expr, 2)
6974 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
6975 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
6976 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
6978 return convert_from_reference (rvalue (expr));
6981 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6982 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6983 if (TREE_CODE (type) == REFERENCE_TYPE
6984 && TYPE_REF_IS_RVALUE (type)
6985 && (clk = real_lvalue_p (expr))
6986 && reference_related_p (TREE_TYPE (type), intype)
6987 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6989 if (processing_template_decl)
6990 return expr;
6991 if (clk == clk_ordinary)
6993 /* Handle the (non-bit-field) lvalue case here by casting to
6994 lvalue reference and then changing it to an rvalue reference.
6995 Casting an xvalue to rvalue reference will be handled by the
6996 main code path. */
6997 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6998 result = (perform_direct_initialization_if_possible
6999 (lref, expr, c_cast_p, complain));
7000 result = build1 (NON_LVALUE_EXPR, type, result);
7001 return convert_from_reference (result);
7003 else
7004 /* For a bit-field or packed field, bind to a temporary. */
7005 expr = rvalue (expr);
7008 /* Resolve overloaded address here rather than once in
7009 implicit_conversion and again in the inverse code below. */
7010 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7012 expr = instantiate_type (type, expr, complain);
7013 intype = TREE_TYPE (expr);
7016 /* [expr.static.cast]
7018 Any expression can be explicitly converted to type cv void. */
7019 if (VOID_TYPE_P (type))
7020 return convert_to_void (expr, ICV_CAST, complain);
7022 /* [class.abstract]
7023 An abstract class shall not be used ... as the type of an explicit
7024 conversion. */
7025 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7026 return error_mark_node;
7028 /* [expr.static.cast]
7030 An expression e can be explicitly converted to a type T using a
7031 static_cast of the form static_cast<T>(e) if the declaration T
7032 t(e);" is well-formed, for some invented temporary variable
7033 t. */
7034 result = perform_direct_initialization_if_possible (type, expr,
7035 c_cast_p, complain);
7036 if (result)
7038 if (processing_template_decl)
7039 return expr;
7041 result = convert_from_reference (result);
7043 /* [expr.static.cast]
7045 If T is a reference type, the result is an lvalue; otherwise,
7046 the result is an rvalue. */
7047 if (TREE_CODE (type) != REFERENCE_TYPE)
7049 result = rvalue (result);
7051 if (result == expr && SCALAR_TYPE_P (type))
7052 /* Leave some record of the cast. */
7053 result = build_nop (type, expr);
7055 return result;
7058 /* [expr.static.cast]
7060 The inverse of any standard conversion sequence (clause _conv_),
7061 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7062 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7063 (_conv.bool_) conversions, can be performed explicitly using
7064 static_cast subject to the restriction that the explicit
7065 conversion does not cast away constness (_expr.const.cast_), and
7066 the following additional rules for specific cases: */
7067 /* For reference, the conversions not excluded are: integral
7068 promotions, floating point promotion, integral conversions,
7069 floating point conversions, floating-integral conversions,
7070 pointer conversions, and pointer to member conversions. */
7071 /* DR 128
7073 A value of integral _or enumeration_ type can be explicitly
7074 converted to an enumeration type. */
7075 /* The effect of all that is that any conversion between any two
7076 types which are integral, floating, or enumeration types can be
7077 performed. */
7078 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7079 || SCALAR_FLOAT_TYPE_P (type))
7080 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7081 || SCALAR_FLOAT_TYPE_P (intype)))
7083 if (processing_template_decl)
7084 return expr;
7085 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7088 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7089 && CLASS_TYPE_P (TREE_TYPE (type))
7090 && CLASS_TYPE_P (TREE_TYPE (intype))
7091 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7092 (TREE_TYPE (intype))),
7093 build_pointer_type (TYPE_MAIN_VARIANT
7094 (TREE_TYPE (type))),
7095 complain))
7097 tree base;
7099 if (processing_template_decl)
7100 return expr;
7102 if (!c_cast_p
7103 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7104 complain))
7105 return error_mark_node;
7106 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7107 c_cast_p ? ba_unique : ba_check,
7108 NULL, complain);
7109 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7110 complain);
7112 if (sanitize_flags_p (SANITIZE_VPTR))
7114 tree ubsan_check
7115 = cp_ubsan_maybe_instrument_downcast (input_location, type,
7116 intype, expr);
7117 if (ubsan_check)
7118 expr = ubsan_check;
7121 return cp_fold_convert (type, expr);
7124 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7125 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7127 tree c1;
7128 tree c2;
7129 tree t1;
7130 tree t2;
7132 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7133 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7135 if (TYPE_PTRDATAMEM_P (type))
7137 t1 = (build_ptrmem_type
7138 (c1,
7139 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7140 t2 = (build_ptrmem_type
7141 (c2,
7142 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7144 else
7146 t1 = intype;
7147 t2 = type;
7149 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7151 if (!c_cast_p
7152 && check_for_casting_away_constness (intype, type,
7153 STATIC_CAST_EXPR,
7154 complain))
7155 return error_mark_node;
7156 if (processing_template_decl)
7157 return expr;
7158 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7159 c_cast_p, complain);
7163 /* [expr.static.cast]
7165 An rvalue of type "pointer to cv void" can be explicitly
7166 converted to a pointer to object type. A value of type pointer
7167 to object converted to "pointer to cv void" and back to the
7168 original pointer type will have its original value. */
7169 if (TYPE_PTR_P (intype)
7170 && VOID_TYPE_P (TREE_TYPE (intype))
7171 && TYPE_PTROB_P (type))
7173 if (!c_cast_p
7174 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7175 complain))
7176 return error_mark_node;
7177 if (processing_template_decl)
7178 return expr;
7179 return build_nop (type, expr);
7182 *valid_p = false;
7183 return error_mark_node;
7186 /* Return an expression representing static_cast<TYPE>(EXPR). */
7188 tree
7189 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7191 tree expr = oexpr;
7192 tree result;
7193 bool valid_p;
7195 if (type == error_mark_node || expr == error_mark_node)
7196 return error_mark_node;
7198 bool dependent = (dependent_type_p (type)
7199 || type_dependent_expression_p (expr));
7200 if (dependent)
7202 tmpl:
7203 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7204 /* We don't know if it will or will not have side effects. */
7205 TREE_SIDE_EFFECTS (expr) = 1;
7206 return convert_from_reference (expr);
7208 else if (processing_template_decl)
7209 expr = build_non_dependent_expr (expr);
7211 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7212 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7213 if (TREE_CODE (type) != REFERENCE_TYPE
7214 && TREE_CODE (expr) == NOP_EXPR
7215 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7216 expr = TREE_OPERAND (expr, 0);
7218 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7219 complain);
7220 if (valid_p)
7222 if (result != error_mark_node)
7224 maybe_warn_about_useless_cast (type, expr, complain);
7225 maybe_warn_about_cast_ignoring_quals (type, complain);
7227 if (processing_template_decl)
7228 goto tmpl;
7229 return result;
7232 if (complain & tf_error)
7233 error ("invalid static_cast from type %qT to type %qT",
7234 TREE_TYPE (expr), type);
7235 return error_mark_node;
7238 /* EXPR is an expression with member function or pointer-to-member
7239 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7240 not permitted by ISO C++, but we accept it in some modes. If we
7241 are not in one of those modes, issue a diagnostic. Return the
7242 converted expression. */
7244 tree
7245 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7247 tree intype;
7248 tree decl;
7250 intype = TREE_TYPE (expr);
7251 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7252 || TREE_CODE (intype) == METHOD_TYPE);
7254 if (!(complain & tf_warning_or_error))
7255 return error_mark_node;
7257 if (pedantic || warn_pmf2ptr)
7258 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7259 "converting from %qH to %qI", intype, type);
7261 if (TREE_CODE (intype) == METHOD_TYPE)
7262 expr = build_addr_func (expr, complain);
7263 else if (TREE_CODE (expr) == PTRMEM_CST)
7264 expr = build_address (PTRMEM_CST_MEMBER (expr));
7265 else
7267 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7268 decl = build_address (decl);
7269 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7272 if (expr == error_mark_node)
7273 return error_mark_node;
7275 return build_nop (type, expr);
7278 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7279 If C_CAST_P is true, this reinterpret cast is being done as part of
7280 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7281 indicate whether or not reinterpret_cast was valid. */
7283 static tree
7284 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7285 bool *valid_p, tsubst_flags_t complain)
7287 tree intype;
7289 /* Assume the cast is invalid. */
7290 if (valid_p)
7291 *valid_p = true;
7293 if (type == error_mark_node || error_operand_p (expr))
7294 return error_mark_node;
7296 intype = TREE_TYPE (expr);
7298 /* Save casted types in the function's used types hash table. */
7299 used_types_insert (type);
7301 /* A prvalue of non-class type is cv-unqualified. */
7302 if (!CLASS_TYPE_P (type))
7303 type = cv_unqualified (type);
7305 /* [expr.reinterpret.cast]
7306 An lvalue expression of type T1 can be cast to the type
7307 "reference to T2" if an expression of type "pointer to T1" can be
7308 explicitly converted to the type "pointer to T2" using a
7309 reinterpret_cast. */
7310 if (TREE_CODE (type) == REFERENCE_TYPE)
7312 if (! lvalue_p (expr))
7314 if (complain & tf_error)
7315 error ("invalid cast of an rvalue expression of type "
7316 "%qT to type %qT",
7317 intype, type);
7318 return error_mark_node;
7321 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7322 "B" are related class types; the reinterpret_cast does not
7323 adjust the pointer. */
7324 if (TYPE_PTR_P (intype)
7325 && (complain & tf_warning)
7326 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7327 COMPARE_BASE | COMPARE_DERIVED)))
7328 warning (0, "casting %qT to %qT does not dereference pointer",
7329 intype, type);
7331 expr = cp_build_addr_expr (expr, complain);
7333 if (warn_strict_aliasing > 2)
7334 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
7336 if (expr != error_mark_node)
7337 expr = build_reinterpret_cast_1
7338 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7339 valid_p, complain);
7340 if (expr != error_mark_node)
7341 /* cp_build_indirect_ref isn't right for rvalue refs. */
7342 expr = convert_from_reference (fold_convert (type, expr));
7343 return expr;
7346 /* As a G++ extension, we consider conversions from member
7347 functions, and pointers to member functions to
7348 pointer-to-function and pointer-to-void types. If
7349 -Wno-pmf-conversions has not been specified,
7350 convert_member_func_to_ptr will issue an error message. */
7351 if ((TYPE_PTRMEMFUNC_P (intype)
7352 || TREE_CODE (intype) == METHOD_TYPE)
7353 && TYPE_PTR_P (type)
7354 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7355 || VOID_TYPE_P (TREE_TYPE (type))))
7356 return convert_member_func_to_ptr (type, expr, complain);
7358 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7359 array-to-pointer, and function-to-pointer conversions are
7360 performed. */
7361 expr = decay_conversion (expr, complain);
7363 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7364 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7365 if (TREE_CODE (expr) == NOP_EXPR
7366 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7367 expr = TREE_OPERAND (expr, 0);
7369 if (error_operand_p (expr))
7370 return error_mark_node;
7372 intype = TREE_TYPE (expr);
7374 /* [expr.reinterpret.cast]
7375 A pointer can be converted to any integral type large enough to
7376 hold it. ... A value of type std::nullptr_t can be converted to
7377 an integral type; the conversion has the same meaning and
7378 validity as a conversion of (void*)0 to the integral type. */
7379 if (CP_INTEGRAL_TYPE_P (type)
7380 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7382 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7384 if (complain & tf_error)
7385 permerror (input_location, "cast from %qH to %qI loses precision",
7386 intype, type);
7387 else
7388 return error_mark_node;
7390 if (NULLPTR_TYPE_P (intype))
7391 return build_int_cst (type, 0);
7393 /* [expr.reinterpret.cast]
7394 A value of integral or enumeration type can be explicitly
7395 converted to a pointer. */
7396 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7397 /* OK */
7399 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7400 || TYPE_PTR_OR_PTRMEM_P (type))
7401 && same_type_p (type, intype))
7402 /* DR 799 */
7403 return rvalue (expr);
7404 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7406 if ((complain & tf_warning)
7407 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7408 TREE_TYPE (intype)))
7409 warning (OPT_Wcast_function_type,
7410 "cast between incompatible function types"
7411 " from %qH to %qI", intype, type);
7412 return build_nop (type, expr);
7414 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7416 if ((complain & tf_warning)
7417 && !cxx_safe_function_type_cast_p
7418 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7419 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7420 warning (OPT_Wcast_function_type,
7421 "cast between incompatible pointer to member types"
7422 " from %qH to %qI", intype, type);
7423 return build_nop (type, expr);
7425 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7426 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7428 tree sexpr = expr;
7430 if (!c_cast_p
7431 && check_for_casting_away_constness (intype, type,
7432 REINTERPRET_CAST_EXPR,
7433 complain))
7434 return error_mark_node;
7435 /* Warn about possible alignment problems. */
7436 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7437 && (complain & tf_warning)
7438 && !VOID_TYPE_P (type)
7439 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7440 && COMPLETE_TYPE_P (TREE_TYPE (type))
7441 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7442 && min_align_of_type (TREE_TYPE (type))
7443 > min_align_of_type (TREE_TYPE (intype)))
7444 warning (OPT_Wcast_align, "cast from %qH to %qI "
7445 "increases required alignment of target type", intype, type);
7447 /* We need to strip nops here, because the front end likes to
7448 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7449 STRIP_NOPS (sexpr);
7450 if (warn_strict_aliasing <= 2)
7451 strict_aliasing_warning (intype, type, sexpr);
7453 return build_nop (type, expr);
7455 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7456 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7458 if (complain & tf_warning)
7459 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7460 object pointer type or vice versa is conditionally-supported." */
7461 warning (OPT_Wconditionally_supported,
7462 "casting between pointer-to-function and pointer-to-object "
7463 "is conditionally-supported");
7464 return build_nop (type, expr);
7466 else if (VECTOR_TYPE_P (type))
7467 return convert_to_vector (type, expr);
7468 else if (VECTOR_TYPE_P (intype)
7469 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7470 return convert_to_integer_nofold (type, expr);
7471 else
7473 if (valid_p)
7474 *valid_p = false;
7475 if (complain & tf_error)
7476 error ("invalid cast from type %qT to type %qT", intype, type);
7477 return error_mark_node;
7480 return cp_convert (type, expr, complain);
7483 tree
7484 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7486 tree r;
7488 if (type == error_mark_node || expr == error_mark_node)
7489 return error_mark_node;
7491 if (processing_template_decl)
7493 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7495 if (!TREE_SIDE_EFFECTS (t)
7496 && type_dependent_expression_p (expr))
7497 /* There might turn out to be side effects inside expr. */
7498 TREE_SIDE_EFFECTS (t) = 1;
7499 return convert_from_reference (t);
7502 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7503 /*valid_p=*/NULL, complain);
7504 if (r != error_mark_node)
7506 maybe_warn_about_useless_cast (type, expr, complain);
7507 maybe_warn_about_cast_ignoring_quals (type, complain);
7509 return r;
7512 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7513 return an appropriate expression. Otherwise, return
7514 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7515 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7516 performing a C-style cast, its value upon return will indicate
7517 whether or not the conversion succeeded. */
7519 static tree
7520 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7521 bool *valid_p)
7523 tree src_type;
7524 tree reference_type;
7526 /* Callers are responsible for handling error_mark_node as a
7527 destination type. */
7528 gcc_assert (dst_type != error_mark_node);
7529 /* In a template, callers should be building syntactic
7530 representations of casts, not using this machinery. */
7531 gcc_assert (!processing_template_decl);
7533 /* Assume the conversion is invalid. */
7534 if (valid_p)
7535 *valid_p = false;
7537 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7539 if (complain & tf_error)
7540 error ("invalid use of const_cast with type %qT, "
7541 "which is not a pointer, "
7542 "reference, nor a pointer-to-data-member type", dst_type);
7543 return error_mark_node;
7546 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7548 if (complain & tf_error)
7549 error ("invalid use of const_cast with type %qT, which is a pointer "
7550 "or reference to a function type", dst_type);
7551 return error_mark_node;
7554 /* A prvalue of non-class type is cv-unqualified. */
7555 dst_type = cv_unqualified (dst_type);
7557 /* Save casted types in the function's used types hash table. */
7558 used_types_insert (dst_type);
7560 src_type = TREE_TYPE (expr);
7561 /* Expressions do not really have reference types. */
7562 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7563 src_type = TREE_TYPE (src_type);
7565 /* [expr.const.cast]
7567 For two object types T1 and T2, if a pointer to T1 can be explicitly
7568 converted to the type "pointer to T2" using a const_cast, then the
7569 following conversions can also be made:
7571 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7572 type T2 using the cast const_cast<T2&>;
7574 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7575 type T2 using the cast const_cast<T2&&>; and
7577 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7578 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7580 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7582 reference_type = dst_type;
7583 if (!TYPE_REF_IS_RVALUE (dst_type)
7584 ? lvalue_p (expr)
7585 : obvalue_p (expr))
7586 /* OK. */;
7587 else
7589 if (complain & tf_error)
7590 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7591 src_type, dst_type);
7592 return error_mark_node;
7594 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7595 src_type = build_pointer_type (src_type);
7597 else
7599 reference_type = NULL_TREE;
7600 /* If the destination type is not a reference type, the
7601 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7602 conversions are performed. */
7603 src_type = type_decays_to (src_type);
7604 if (src_type == error_mark_node)
7605 return error_mark_node;
7608 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7610 if (comp_ptr_ttypes_const (dst_type, src_type))
7612 if (valid_p)
7614 *valid_p = true;
7615 /* This cast is actually a C-style cast. Issue a warning if
7616 the user is making a potentially unsafe cast. */
7617 check_for_casting_away_constness (src_type, dst_type,
7618 CAST_EXPR, complain);
7619 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7620 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7621 && (complain & tf_warning)
7622 && min_align_of_type (TREE_TYPE (dst_type))
7623 > min_align_of_type (TREE_TYPE (src_type)))
7624 warning (OPT_Wcast_align, "cast from %qH to %qI "
7625 "increases required alignment of target type",
7626 src_type, dst_type);
7628 if (reference_type)
7630 expr = cp_build_addr_expr (expr, complain);
7631 if (expr == error_mark_node)
7632 return error_mark_node;
7633 expr = build_nop (reference_type, expr);
7634 return convert_from_reference (expr);
7636 else
7638 expr = decay_conversion (expr, complain);
7639 if (expr == error_mark_node)
7640 return error_mark_node;
7642 /* build_c_cast puts on a NOP_EXPR to make the result not an
7643 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7644 non-lvalue context. */
7645 if (TREE_CODE (expr) == NOP_EXPR
7646 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7647 expr = TREE_OPERAND (expr, 0);
7648 return build_nop (dst_type, expr);
7651 else if (valid_p
7652 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7653 TREE_TYPE (src_type)))
7654 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7655 complain);
7658 if (complain & tf_error)
7659 error ("invalid const_cast from type %qT to type %qT",
7660 src_type, dst_type);
7661 return error_mark_node;
7664 tree
7665 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7667 tree r;
7669 if (type == error_mark_node || error_operand_p (expr))
7670 return error_mark_node;
7672 if (processing_template_decl)
7674 tree t = build_min (CONST_CAST_EXPR, type, expr);
7676 if (!TREE_SIDE_EFFECTS (t)
7677 && type_dependent_expression_p (expr))
7678 /* There might turn out to be side effects inside expr. */
7679 TREE_SIDE_EFFECTS (t) = 1;
7680 return convert_from_reference (t);
7683 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7684 if (r != error_mark_node)
7686 maybe_warn_about_useless_cast (type, expr, complain);
7687 maybe_warn_about_cast_ignoring_quals (type, complain);
7689 return r;
7692 /* Like cp_build_c_cast, but for the c-common bits. */
7694 tree
7695 build_c_cast (location_t /*loc*/, tree type, tree expr)
7697 return cp_build_c_cast (type, expr, tf_warning_or_error);
7700 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7701 preserve location information even for tree nodes that don't
7702 support it. */
7704 cp_expr
7705 build_c_cast (location_t loc, tree type, cp_expr expr)
7707 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7708 result.set_location (loc);
7709 return result;
7712 /* Build an expression representing an explicit C-style cast to type
7713 TYPE of expression EXPR. */
7715 tree
7716 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7718 tree value = expr;
7719 tree result;
7720 bool valid_p;
7722 if (type == error_mark_node || error_operand_p (expr))
7723 return error_mark_node;
7725 if (processing_template_decl)
7727 tree t = build_min (CAST_EXPR, type,
7728 tree_cons (NULL_TREE, value, NULL_TREE));
7729 /* We don't know if it will or will not have side effects. */
7730 TREE_SIDE_EFFECTS (t) = 1;
7731 return convert_from_reference (t);
7734 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7735 'Class') should always be retained, because this information aids
7736 in method lookup. */
7737 if (objc_is_object_ptr (type)
7738 && objc_is_object_ptr (TREE_TYPE (expr)))
7739 return build_nop (type, expr);
7741 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7742 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7743 if (TREE_CODE (type) != REFERENCE_TYPE
7744 && TREE_CODE (value) == NOP_EXPR
7745 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7746 value = TREE_OPERAND (value, 0);
7748 if (TREE_CODE (type) == ARRAY_TYPE)
7750 /* Allow casting from T1* to T2[] because Cfront allows it.
7751 NIHCL uses it. It is not valid ISO C++ however. */
7752 if (TYPE_PTR_P (TREE_TYPE (expr)))
7754 if (complain & tf_error)
7755 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7756 else
7757 return error_mark_node;
7758 type = build_pointer_type (TREE_TYPE (type));
7760 else
7762 if (complain & tf_error)
7763 error ("ISO C++ forbids casting to an array type %qT", type);
7764 return error_mark_node;
7768 if (TREE_CODE (type) == FUNCTION_TYPE
7769 || TREE_CODE (type) == METHOD_TYPE)
7771 if (complain & tf_error)
7772 error ("invalid cast to function type %qT", type);
7773 return error_mark_node;
7776 if (TYPE_PTR_P (type)
7777 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7778 /* Casting to an integer of smaller size is an error detected elsewhere. */
7779 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7780 /* Don't warn about converting any constant. */
7781 && !TREE_CONSTANT (value))
7782 warning_at (input_location, OPT_Wint_to_pointer_cast,
7783 "cast to pointer from integer of different size");
7785 /* A C-style cast can be a const_cast. */
7786 result = build_const_cast_1 (type, value, complain & tf_warning,
7787 &valid_p);
7788 if (valid_p)
7790 if (result != error_mark_node)
7792 maybe_warn_about_useless_cast (type, value, complain);
7793 maybe_warn_about_cast_ignoring_quals (type, complain);
7795 return result;
7798 /* Or a static cast. */
7799 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7800 &valid_p, complain);
7801 /* Or a reinterpret_cast. */
7802 if (!valid_p)
7803 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7804 &valid_p, complain);
7805 /* The static_cast or reinterpret_cast may be followed by a
7806 const_cast. */
7807 if (valid_p
7808 /* A valid cast may result in errors if, for example, a
7809 conversion to an ambiguous base class is required. */
7810 && !error_operand_p (result))
7812 tree result_type;
7814 maybe_warn_about_useless_cast (type, value, complain);
7815 maybe_warn_about_cast_ignoring_quals (type, complain);
7817 /* Non-class rvalues always have cv-unqualified type. */
7818 if (!CLASS_TYPE_P (type))
7819 type = TYPE_MAIN_VARIANT (type);
7820 result_type = TREE_TYPE (result);
7821 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7822 result_type = TYPE_MAIN_VARIANT (result_type);
7823 /* If the type of RESULT does not match TYPE, perform a
7824 const_cast to make it match. If the static_cast or
7825 reinterpret_cast succeeded, we will differ by at most
7826 cv-qualification, so the follow-on const_cast is guaranteed
7827 to succeed. */
7828 if (!same_type_p (non_reference (type), non_reference (result_type)))
7830 result = build_const_cast_1 (type, result, false, &valid_p);
7831 gcc_assert (valid_p);
7833 return result;
7836 return error_mark_node;
7839 /* For use from the C common bits. */
7840 tree
7841 build_modify_expr (location_t location,
7842 tree lhs, tree /*lhs_origtype*/,
7843 enum tree_code modifycode,
7844 location_t /*rhs_location*/, tree rhs,
7845 tree /*rhs_origtype*/)
7847 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7848 tf_warning_or_error);
7851 /* Build an assignment expression of lvalue LHS from value RHS.
7852 MODIFYCODE is the code for a binary operator that we use
7853 to combine the old value of LHS with RHS to get the new value.
7854 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7856 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7858 tree
7859 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7860 tree rhs, tsubst_flags_t complain)
7862 lhs = mark_lvalue_use_nonread (lhs);
7864 tree result = NULL_TREE;
7865 tree newrhs = rhs;
7866 tree lhstype = TREE_TYPE (lhs);
7867 tree olhs = lhs;
7868 tree olhstype = lhstype;
7869 bool plain_assign = (modifycode == NOP_EXPR);
7870 bool compound_side_effects_p = false;
7871 tree preeval = NULL_TREE;
7873 /* Avoid duplicate error messages from operands that had errors. */
7874 if (error_operand_p (lhs) || error_operand_p (rhs))
7875 return error_mark_node;
7877 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7879 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7880 compound_side_effects_p = true;
7881 lhs = TREE_OPERAND (lhs, 1);
7884 /* Handle control structure constructs used as "lvalues". Note that we
7885 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7886 switch (TREE_CODE (lhs))
7888 /* Handle --foo = 5; as these are valid constructs in C++. */
7889 case PREDECREMENT_EXPR:
7890 case PREINCREMENT_EXPR:
7891 if (compound_side_effects_p)
7892 newrhs = rhs = stabilize_expr (rhs, &preeval);
7893 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7894 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7895 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7896 TREE_OPERAND (lhs, 1));
7897 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7898 maybe_add_compound:
7899 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7900 and looked through the COMPOUND_EXPRs, readd them now around
7901 the resulting lhs. */
7902 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7904 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7905 tree *ptr = &TREE_OPERAND (lhs, 1);
7906 for (olhs = TREE_OPERAND (olhs, 1);
7907 TREE_CODE (olhs) == COMPOUND_EXPR;
7908 olhs = TREE_OPERAND (olhs, 1))
7910 *ptr = build2 (COMPOUND_EXPR, lhstype,
7911 TREE_OPERAND (olhs, 0), *ptr);
7912 ptr = &TREE_OPERAND (*ptr, 1);
7915 break;
7917 case MODIFY_EXPR:
7918 if (compound_side_effects_p)
7919 newrhs = rhs = stabilize_expr (rhs, &preeval);
7920 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7921 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7922 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7923 TREE_OPERAND (lhs, 1));
7924 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7925 goto maybe_add_compound;
7927 case MIN_EXPR:
7928 case MAX_EXPR:
7929 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7930 when neither operand has side-effects. */
7931 if (!lvalue_or_else (lhs, lv_assign, complain))
7932 return error_mark_node;
7934 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7935 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7937 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7938 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7939 boolean_type_node,
7940 TREE_OPERAND (lhs, 0),
7941 TREE_OPERAND (lhs, 1)),
7942 TREE_OPERAND (lhs, 0),
7943 TREE_OPERAND (lhs, 1));
7944 gcc_fallthrough ();
7946 /* Handle (a ? b : c) used as an "lvalue". */
7947 case COND_EXPR:
7949 /* Produce (a ? (b = rhs) : (c = rhs))
7950 except that the RHS goes through a save-expr
7951 so the code to compute it is only emitted once. */
7952 tree cond;
7954 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7956 if (complain & tf_error)
7957 error ("void value not ignored as it ought to be");
7958 return error_mark_node;
7961 rhs = stabilize_expr (rhs, &preeval);
7963 /* Check this here to avoid odd errors when trying to convert
7964 a throw to the type of the COND_EXPR. */
7965 if (!lvalue_or_else (lhs, lv_assign, complain))
7966 return error_mark_node;
7968 cond = build_conditional_expr
7969 (input_location, TREE_OPERAND (lhs, 0),
7970 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7971 modifycode, rhs, complain),
7972 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7973 modifycode, rhs, complain),
7974 complain);
7976 if (cond == error_mark_node)
7977 return cond;
7978 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7979 and looked through the COMPOUND_EXPRs, readd them now around
7980 the resulting cond before adding the preevaluated rhs. */
7981 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7983 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7984 TREE_OPERAND (olhs, 0), cond);
7985 tree *ptr = &TREE_OPERAND (cond, 1);
7986 for (olhs = TREE_OPERAND (olhs, 1);
7987 TREE_CODE (olhs) == COMPOUND_EXPR;
7988 olhs = TREE_OPERAND (olhs, 1))
7990 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7991 TREE_OPERAND (olhs, 0), *ptr);
7992 ptr = &TREE_OPERAND (*ptr, 1);
7995 /* Make sure the code to compute the rhs comes out
7996 before the split. */
7997 result = cond;
7998 goto ret;
8001 default:
8002 lhs = olhs;
8003 break;
8006 if (modifycode == INIT_EXPR)
8008 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8009 /* Do the default thing. */;
8010 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8012 /* Compound literal. */
8013 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8014 /* Call convert to generate an error; see PR 11063. */
8015 rhs = convert (lhstype, rhs);
8016 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8017 TREE_SIDE_EFFECTS (result) = 1;
8018 goto ret;
8020 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8021 /* Do the default thing. */;
8022 else
8024 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8025 result = build_special_member_call (lhs, complete_ctor_identifier,
8026 &rhs_vec, lhstype, LOOKUP_NORMAL,
8027 complain);
8028 release_tree_vector (rhs_vec);
8029 if (result == NULL_TREE)
8030 return error_mark_node;
8031 goto ret;
8034 else
8036 lhs = require_complete_type_sfinae (lhs, complain);
8037 if (lhs == error_mark_node)
8038 return error_mark_node;
8040 if (modifycode == NOP_EXPR)
8042 if (c_dialect_objc ())
8044 result = objc_maybe_build_modify_expr (lhs, rhs);
8045 if (result)
8046 goto ret;
8049 /* `operator=' is not an inheritable operator. */
8050 if (! MAYBE_CLASS_TYPE_P (lhstype))
8051 /* Do the default thing. */;
8052 else
8054 result = build_new_op (input_location, MODIFY_EXPR,
8055 LOOKUP_NORMAL, lhs, rhs,
8056 make_node (NOP_EXPR), /*overload=*/NULL,
8057 complain);
8058 if (result == NULL_TREE)
8059 return error_mark_node;
8060 goto ret;
8062 lhstype = olhstype;
8064 else
8066 tree init = NULL_TREE;
8068 /* A binary op has been requested. Combine the old LHS
8069 value with the RHS producing the value we should actually
8070 store into the LHS. */
8071 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
8072 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8073 || MAYBE_CLASS_TYPE_P (lhstype)));
8075 /* Preevaluate the RHS to make sure its evaluation is complete
8076 before the lvalue-to-rvalue conversion of the LHS:
8078 [expr.ass] With respect to an indeterminately-sequenced
8079 function call, the operation of a compound assignment is a
8080 single evaluation. [ Note: Therefore, a function call shall
8081 not intervene between the lvalue-to-rvalue conversion and the
8082 side effect associated with any single compound assignment
8083 operator. -- end note ] */
8084 lhs = cp_stabilize_reference (lhs);
8085 rhs = rvalue (rhs);
8086 if (rhs == error_mark_node)
8087 return error_mark_node;
8088 rhs = stabilize_expr (rhs, &init);
8089 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8090 if (newrhs == error_mark_node)
8092 if (complain & tf_error)
8093 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8094 TREE_TYPE (lhs), TREE_TYPE (rhs));
8095 return error_mark_node;
8098 if (init)
8099 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8101 /* Now it looks like a plain assignment. */
8102 modifycode = NOP_EXPR;
8103 if (c_dialect_objc ())
8105 result = objc_maybe_build_modify_expr (lhs, newrhs);
8106 if (result)
8107 goto ret;
8110 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
8111 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
8114 /* The left-hand side must be an lvalue. */
8115 if (!lvalue_or_else (lhs, lv_assign, complain))
8116 return error_mark_node;
8118 /* Warn about modifying something that is `const'. Don't warn if
8119 this is initialization. */
8120 if (modifycode != INIT_EXPR
8121 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8122 /* Functions are not modifiable, even though they are
8123 lvalues. */
8124 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8125 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8126 /* If it's an aggregate and any field is const, then it is
8127 effectively const. */
8128 || (CLASS_TYPE_P (lhstype)
8129 && C_TYPE_FIELDS_READONLY (lhstype))))
8131 if (complain & tf_error)
8132 cxx_readonly_error (lhs, lv_assign);
8133 return error_mark_node;
8136 /* If storing into a structure or union member, it may have been given a
8137 lowered bitfield type. We need to convert to the declared type first,
8138 so retrieve it now. */
8140 olhstype = unlowered_expr_type (lhs);
8142 /* Convert new value to destination type. */
8144 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8146 int from_array;
8148 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8150 if (modifycode != INIT_EXPR)
8152 if (complain & tf_error)
8153 error ("assigning to an array from an initializer list");
8154 return error_mark_node;
8156 if (check_array_initializer (lhs, lhstype, newrhs))
8157 return error_mark_node;
8158 newrhs = digest_init (lhstype, newrhs, complain);
8159 if (newrhs == error_mark_node)
8160 return error_mark_node;
8163 /* C++11 8.5/17: "If the destination type is an array of characters,
8164 an array of char16_t, an array of char32_t, or an array of wchar_t,
8165 and the initializer is a string literal...". */
8166 else if (TREE_CODE (newrhs) == STRING_CST
8167 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8168 && modifycode == INIT_EXPR)
8170 newrhs = digest_init (lhstype, newrhs, complain);
8171 if (newrhs == error_mark_node)
8172 return error_mark_node;
8175 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8176 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8178 if (complain & tf_error)
8179 error ("incompatible types in assignment of %qT to %qT",
8180 TREE_TYPE (rhs), lhstype);
8181 return error_mark_node;
8184 /* Allow array assignment in compiler-generated code. */
8185 else if (!current_function_decl
8186 || !DECL_DEFAULTED_FN (current_function_decl))
8188 /* This routine is used for both initialization and assignment.
8189 Make sure the diagnostic message differentiates the context. */
8190 if (complain & tf_error)
8192 if (modifycode == INIT_EXPR)
8193 error ("array used as initializer");
8194 else
8195 error ("invalid array assignment");
8197 return error_mark_node;
8200 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8201 ? 1 + (modifycode != INIT_EXPR): 0;
8202 result = build_vec_init (lhs, NULL_TREE, newrhs,
8203 /*explicit_value_init_p=*/false,
8204 from_array, complain);
8205 goto ret;
8208 if (modifycode == INIT_EXPR)
8209 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8210 LOOKUP_ONLYCONVERTING. */
8211 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8212 ICR_INIT, NULL_TREE, 0,
8213 complain);
8214 else
8215 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8216 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8218 if (!same_type_p (lhstype, olhstype))
8219 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8221 if (modifycode != INIT_EXPR)
8223 if (TREE_CODE (newrhs) == CALL_EXPR
8224 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8225 newrhs = build_cplus_new (lhstype, newrhs, complain);
8227 /* Can't initialize directly from a TARGET_EXPR, since that would
8228 cause the lhs to be constructed twice, and possibly result in
8229 accidental self-initialization. So we force the TARGET_EXPR to be
8230 expanded without a target. */
8231 if (TREE_CODE (newrhs) == TARGET_EXPR)
8232 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8233 TREE_OPERAND (newrhs, 0));
8236 if (newrhs == error_mark_node)
8237 return error_mark_node;
8239 if (c_dialect_objc () && flag_objc_gc)
8241 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8243 if (result)
8244 goto ret;
8247 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8248 lhstype, lhs, newrhs);
8250 TREE_SIDE_EFFECTS (result) = 1;
8251 if (!plain_assign)
8252 TREE_NO_WARNING (result) = 1;
8254 ret:
8255 if (preeval)
8256 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8257 return result;
8260 cp_expr
8261 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8262 tree rhs, tsubst_flags_t complain)
8264 tree orig_lhs = lhs;
8265 tree orig_rhs = rhs;
8266 tree overload = NULL_TREE;
8267 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8269 if (processing_template_decl)
8271 if (modifycode == NOP_EXPR
8272 || type_dependent_expression_p (lhs)
8273 || type_dependent_expression_p (rhs))
8274 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8275 build_min_nt_loc (loc, modifycode, NULL_TREE,
8276 NULL_TREE), rhs);
8278 lhs = build_non_dependent_expr (lhs);
8279 rhs = build_non_dependent_expr (rhs);
8282 if (modifycode != NOP_EXPR)
8284 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8285 lhs, rhs, op, &overload, complain);
8286 if (rval)
8288 if (rval == error_mark_node)
8289 return rval;
8290 TREE_NO_WARNING (rval) = 1;
8291 if (processing_template_decl)
8293 if (overload != NULL_TREE)
8294 return (build_min_non_dep_op_overload
8295 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8297 return (build_min_non_dep
8298 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8300 return rval;
8303 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8306 /* Helper function for get_delta_difference which assumes FROM is a base
8307 class of TO. Returns a delta for the conversion of pointer-to-member
8308 of FROM to pointer-to-member of TO. If the conversion is invalid and
8309 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8310 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8311 If C_CAST_P is true, this conversion is taking place as part of a
8312 C-style cast. */
8314 static tree
8315 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8316 tsubst_flags_t complain)
8318 tree binfo;
8319 base_kind kind;
8321 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8322 &kind, complain);
8324 if (binfo == error_mark_node)
8326 if (!(complain & tf_error))
8327 return error_mark_node;
8329 error (" in pointer to member function conversion");
8330 return size_zero_node;
8332 else if (binfo)
8334 if (kind != bk_via_virtual)
8335 return BINFO_OFFSET (binfo);
8336 else
8337 /* FROM is a virtual base class of TO. Issue an error or warning
8338 depending on whether or not this is a reinterpret cast. */
8340 if (!(complain & tf_error))
8341 return error_mark_node;
8343 error ("pointer to member conversion via virtual base %qT",
8344 BINFO_TYPE (binfo_from_vbase (binfo)));
8346 return size_zero_node;
8349 else
8350 return NULL_TREE;
8353 /* Get difference in deltas for different pointer to member function
8354 types. If the conversion is invalid and tf_error is not set in
8355 COMPLAIN, returns error_mark_node, otherwise returns an integer
8356 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8357 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8358 conversions as well. If C_CAST_P is true this conversion is taking
8359 place as part of a C-style cast.
8361 Note that the naming of FROM and TO is kind of backwards; the return
8362 value is what we add to a TO in order to get a FROM. They are named
8363 this way because we call this function to find out how to convert from
8364 a pointer to member of FROM to a pointer to member of TO. */
8366 static tree
8367 get_delta_difference (tree from, tree to,
8368 bool allow_inverse_p,
8369 bool c_cast_p, tsubst_flags_t complain)
8371 tree result;
8373 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8374 /* Pointer to member of incomplete class is permitted*/
8375 result = size_zero_node;
8376 else
8377 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8379 if (result == error_mark_node)
8380 return error_mark_node;
8382 if (!result)
8384 if (!allow_inverse_p)
8386 if (!(complain & tf_error))
8387 return error_mark_node;
8389 error_not_base_type (from, to);
8390 error (" in pointer to member conversion");
8391 result = size_zero_node;
8393 else
8395 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8397 if (result == error_mark_node)
8398 return error_mark_node;
8400 if (result)
8401 result = size_diffop_loc (input_location,
8402 size_zero_node, result);
8403 else
8405 if (!(complain & tf_error))
8406 return error_mark_node;
8408 error_not_base_type (from, to);
8409 error (" in pointer to member conversion");
8410 result = size_zero_node;
8415 return convert_to_integer (ptrdiff_type_node, result);
8418 /* Return a constructor for the pointer-to-member-function TYPE using
8419 the other components as specified. */
8421 tree
8422 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8424 tree u = NULL_TREE;
8425 tree delta_field;
8426 tree pfn_field;
8427 vec<constructor_elt, va_gc> *v;
8429 /* Pull the FIELD_DECLs out of the type. */
8430 pfn_field = TYPE_FIELDS (type);
8431 delta_field = DECL_CHAIN (pfn_field);
8433 /* Make sure DELTA has the type we want. */
8434 delta = convert_and_check (input_location, delta_type_node, delta);
8436 /* Convert to the correct target type if necessary. */
8437 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8439 /* Finish creating the initializer. */
8440 vec_alloc (v, 2);
8441 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8442 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8443 u = build_constructor (type, v);
8444 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8445 TREE_STATIC (u) = (TREE_CONSTANT (u)
8446 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8447 != NULL_TREE)
8448 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8449 != NULL_TREE));
8450 return u;
8453 /* Build a constructor for a pointer to member function. It can be
8454 used to initialize global variables, local variable, or used
8455 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8456 want to be.
8458 If FORCE is nonzero, then force this conversion, even if
8459 we would rather not do it. Usually set when using an explicit
8460 cast. A C-style cast is being processed iff C_CAST_P is true.
8462 Return error_mark_node, if something goes wrong. */
8464 tree
8465 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8466 tsubst_flags_t complain)
8468 tree fn;
8469 tree pfn_type;
8470 tree to_type;
8472 if (error_operand_p (pfn))
8473 return error_mark_node;
8475 pfn_type = TREE_TYPE (pfn);
8476 to_type = build_ptrmemfunc_type (type);
8478 /* Handle multiple conversions of pointer to member functions. */
8479 if (TYPE_PTRMEMFUNC_P (pfn_type))
8481 tree delta = NULL_TREE;
8482 tree npfn = NULL_TREE;
8483 tree n;
8485 if (!force
8486 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8487 LOOKUP_NORMAL, complain))
8489 if (complain & tf_error)
8490 error ("invalid conversion to type %qT from type %qT",
8491 to_type, pfn_type);
8492 else
8493 return error_mark_node;
8496 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8497 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8498 force,
8499 c_cast_p, complain);
8500 if (n == error_mark_node)
8501 return error_mark_node;
8503 /* We don't have to do any conversion to convert a
8504 pointer-to-member to its own type. But, we don't want to
8505 just return a PTRMEM_CST if there's an explicit cast; that
8506 cast should make the expression an invalid template argument. */
8507 if (TREE_CODE (pfn) != PTRMEM_CST)
8509 if (same_type_p (to_type, pfn_type))
8510 return pfn;
8511 else if (integer_zerop (n))
8512 return build_reinterpret_cast (to_type, pfn,
8513 complain);
8516 if (TREE_SIDE_EFFECTS (pfn))
8517 pfn = save_expr (pfn);
8519 /* Obtain the function pointer and the current DELTA. */
8520 if (TREE_CODE (pfn) == PTRMEM_CST)
8521 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8522 else
8524 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8525 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8528 /* Just adjust the DELTA field. */
8529 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8530 (TREE_TYPE (delta), ptrdiff_type_node));
8531 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8532 n = cp_build_binary_op (input_location,
8533 LSHIFT_EXPR, n, integer_one_node,
8534 complain);
8535 delta = cp_build_binary_op (input_location,
8536 PLUS_EXPR, delta, n, complain);
8537 return build_ptrmemfunc1 (to_type, delta, npfn);
8540 /* Handle null pointer to member function conversions. */
8541 if (null_ptr_cst_p (pfn))
8543 pfn = cp_build_c_cast (type, pfn, complain);
8544 return build_ptrmemfunc1 (to_type,
8545 integer_zero_node,
8546 pfn);
8549 if (type_unknown_p (pfn))
8550 return instantiate_type (type, pfn, complain);
8552 fn = TREE_OPERAND (pfn, 0);
8553 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8554 /* In a template, we will have preserved the
8555 OFFSET_REF. */
8556 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8557 return make_ptrmem_cst (to_type, fn);
8560 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8561 given by CST.
8563 ??? There is no consistency as to the types returned for the above
8564 values. Some code acts as if it were a sizetype and some as if it were
8565 integer_type_node. */
8567 void
8568 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8570 tree type = TREE_TYPE (cst);
8571 tree fn = PTRMEM_CST_MEMBER (cst);
8572 tree ptr_class, fn_class;
8574 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8576 /* The class that the function belongs to. */
8577 fn_class = DECL_CONTEXT (fn);
8579 /* The class that we're creating a pointer to member of. */
8580 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8582 /* First, calculate the adjustment to the function's class. */
8583 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8584 /*c_cast_p=*/0, tf_warning_or_error);
8586 if (!DECL_VIRTUAL_P (fn))
8587 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8588 build_addr_func (fn, tf_warning_or_error));
8589 else
8591 /* If we're dealing with a virtual function, we have to adjust 'this'
8592 again, to point to the base which provides the vtable entry for
8593 fn; the call will do the opposite adjustment. */
8594 tree orig_class = DECL_CONTEXT (fn);
8595 tree binfo = binfo_or_else (orig_class, fn_class);
8596 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8597 *delta, BINFO_OFFSET (binfo));
8599 /* We set PFN to the vtable offset at which the function can be
8600 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8601 case delta is shifted left, and then incremented). */
8602 *pfn = DECL_VINDEX (fn);
8603 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8604 TYPE_SIZE_UNIT (vtable_entry_type));
8606 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8608 case ptrmemfunc_vbit_in_pfn:
8609 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8610 integer_one_node);
8611 break;
8613 case ptrmemfunc_vbit_in_delta:
8614 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8615 *delta, integer_one_node);
8616 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8617 *delta, integer_one_node);
8618 break;
8620 default:
8621 gcc_unreachable ();
8624 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8628 /* Return an expression for PFN from the pointer-to-member function
8629 given by T. */
8631 static tree
8632 pfn_from_ptrmemfunc (tree t)
8634 if (TREE_CODE (t) == PTRMEM_CST)
8636 tree delta;
8637 tree pfn;
8639 expand_ptrmemfunc_cst (t, &delta, &pfn);
8640 if (pfn)
8641 return pfn;
8644 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8647 /* Return an expression for DELTA from the pointer-to-member function
8648 given by T. */
8650 static tree
8651 delta_from_ptrmemfunc (tree t)
8653 if (TREE_CODE (t) == PTRMEM_CST)
8655 tree delta;
8656 tree pfn;
8658 expand_ptrmemfunc_cst (t, &delta, &pfn);
8659 if (delta)
8660 return delta;
8663 return build_ptrmemfunc_access_expr (t, delta_identifier);
8666 /* Convert value RHS to type TYPE as preparation for an assignment to
8667 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8668 implicit conversion is. If FNDECL is non-NULL, we are doing the
8669 conversion in order to pass the PARMNUMth argument of FNDECL.
8670 If FNDECL is NULL, we are doing the conversion in function pointer
8671 argument passing, conversion in initialization, etc. */
8673 static tree
8674 convert_for_assignment (tree type, tree rhs,
8675 impl_conv_rhs errtype, tree fndecl, int parmnum,
8676 tsubst_flags_t complain, int flags)
8678 tree rhstype;
8679 enum tree_code coder;
8681 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8682 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8683 rhs = TREE_OPERAND (rhs, 0);
8685 /* Handle [dcl.init.list] direct-list-initialization from
8686 single element of enumeration with a fixed underlying type. */
8687 if (is_direct_enum_init (type, rhs))
8689 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8690 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8692 warning_sentinel w (warn_useless_cast);
8693 warning_sentinel w2 (warn_ignored_qualifiers);
8694 rhs = cp_build_c_cast (type, elt, complain);
8696 else
8697 rhs = error_mark_node;
8700 rhstype = TREE_TYPE (rhs);
8701 coder = TREE_CODE (rhstype);
8703 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8704 && vector_types_convertible_p (type, rhstype, true))
8706 rhs = mark_rvalue_use (rhs);
8707 return convert (type, rhs);
8710 if (rhs == error_mark_node || rhstype == error_mark_node)
8711 return error_mark_node;
8712 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8713 return error_mark_node;
8715 /* The RHS of an assignment cannot have void type. */
8716 if (coder == VOID_TYPE)
8718 if (complain & tf_error)
8719 error ("void value not ignored as it ought to be");
8720 return error_mark_node;
8723 if (c_dialect_objc ())
8725 int parmno;
8726 tree selector;
8727 tree rname = fndecl;
8729 switch (errtype)
8731 case ICR_ASSIGN:
8732 parmno = -1;
8733 break;
8734 case ICR_INIT:
8735 parmno = -2;
8736 break;
8737 default:
8738 selector = objc_message_selector ();
8739 parmno = parmnum;
8740 if (selector && parmno > 1)
8742 rname = selector;
8743 parmno -= 1;
8747 if (objc_compare_types (type, rhstype, parmno, rname))
8749 rhs = mark_rvalue_use (rhs);
8750 return convert (type, rhs);
8754 /* [expr.ass]
8756 The expression is implicitly converted (clause _conv_) to the
8757 cv-unqualified type of the left operand.
8759 We allow bad conversions here because by the time we get to this point
8760 we are committed to doing the conversion. If we end up doing a bad
8761 conversion, convert_like will complain. */
8762 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8764 /* When -Wno-pmf-conversions is use, we just silently allow
8765 conversions from pointers-to-members to plain pointers. If
8766 the conversion doesn't work, cp_convert will complain. */
8767 if (!warn_pmf2ptr
8768 && TYPE_PTR_P (type)
8769 && TYPE_PTRMEMFUNC_P (rhstype))
8770 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8771 else
8773 if (complain & tf_error)
8775 /* If the right-hand side has unknown type, then it is an
8776 overloaded function. Call instantiate_type to get error
8777 messages. */
8778 if (rhstype == unknown_type_node)
8780 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8781 /* -fpermissive might allow this; recurse. */
8782 if (!seen_error ())
8783 return convert_for_assignment (type, r, errtype, fndecl,
8784 parmnum, complain, flags);
8786 else if (fndecl)
8787 error ("cannot convert %qH to %qI for argument %qP to %qD",
8788 rhstype, type, parmnum, fndecl);
8789 else
8790 switch (errtype)
8792 case ICR_DEFAULT_ARGUMENT:
8793 error ("cannot convert %qH to %qI in default argument",
8794 rhstype, type);
8795 break;
8796 case ICR_ARGPASS:
8797 error ("cannot convert %qH to %qI in argument passing",
8798 rhstype, type);
8799 break;
8800 case ICR_CONVERTING:
8801 error ("cannot convert %qH to %qI",
8802 rhstype, type);
8803 break;
8804 case ICR_INIT:
8805 error ("cannot convert %qH to %qI in initialization",
8806 rhstype, type);
8807 break;
8808 case ICR_RETURN:
8809 error ("cannot convert %qH to %qI in return",
8810 rhstype, type);
8811 break;
8812 case ICR_ASSIGN:
8813 error ("cannot convert %qH to %qI in assignment",
8814 rhstype, type);
8815 break;
8816 default:
8817 gcc_unreachable();
8819 if (TYPE_PTR_P (rhstype)
8820 && TYPE_PTR_P (type)
8821 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8822 && CLASS_TYPE_P (TREE_TYPE (type))
8823 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8824 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8825 (TREE_TYPE (rhstype))),
8826 "class type %qT is incomplete", TREE_TYPE (rhstype));
8828 return error_mark_node;
8831 if (warn_suggest_attribute_format)
8833 const enum tree_code codel = TREE_CODE (type);
8834 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8835 && coder == codel
8836 && check_missing_format_attribute (type, rhstype)
8837 && (complain & tf_warning))
8838 switch (errtype)
8840 case ICR_ARGPASS:
8841 case ICR_DEFAULT_ARGUMENT:
8842 if (fndecl)
8843 warning (OPT_Wsuggest_attribute_format,
8844 "parameter %qP of %qD might be a candidate "
8845 "for a format attribute", parmnum, fndecl);
8846 else
8847 warning (OPT_Wsuggest_attribute_format,
8848 "parameter might be a candidate "
8849 "for a format attribute");
8850 break;
8851 case ICR_CONVERTING:
8852 warning (OPT_Wsuggest_attribute_format,
8853 "target of conversion might be a candidate "
8854 "for a format attribute");
8855 break;
8856 case ICR_INIT:
8857 warning (OPT_Wsuggest_attribute_format,
8858 "target of initialization might be a candidate "
8859 "for a format attribute");
8860 break;
8861 case ICR_RETURN:
8862 warning (OPT_Wsuggest_attribute_format,
8863 "return type might be a candidate "
8864 "for a format attribute");
8865 break;
8866 case ICR_ASSIGN:
8867 warning (OPT_Wsuggest_attribute_format,
8868 "left-hand side of assignment might be a candidate "
8869 "for a format attribute");
8870 break;
8871 default:
8872 gcc_unreachable();
8876 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8877 does not. */
8878 if (warn_parentheses
8879 && TREE_CODE (type) == BOOLEAN_TYPE
8880 && TREE_CODE (rhs) == MODIFY_EXPR
8881 && !TREE_NO_WARNING (rhs)
8882 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8883 && (complain & tf_warning))
8885 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8887 warning_at (loc, OPT_Wparentheses,
8888 "suggest parentheses around assignment used as truth value");
8889 TREE_NO_WARNING (rhs) = 1;
8892 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8893 complain, flags);
8896 /* Convert RHS to be of type TYPE.
8897 If EXP is nonzero, it is the target of the initialization.
8898 ERRTYPE indicates what kind of error the implicit conversion is.
8900 Two major differences between the behavior of
8901 `convert_for_assignment' and `convert_for_initialization'
8902 are that references are bashed in the former, while
8903 copied in the latter, and aggregates are assigned in
8904 the former (operator=) while initialized in the
8905 latter (X(X&)).
8907 If using constructor make sure no conversion operator exists, if one does
8908 exist, an ambiguity exists. */
8910 tree
8911 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8912 impl_conv_rhs errtype, tree fndecl, int parmnum,
8913 tsubst_flags_t complain)
8915 enum tree_code codel = TREE_CODE (type);
8916 tree rhstype;
8917 enum tree_code coder;
8919 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8920 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8921 if (TREE_CODE (rhs) == NOP_EXPR
8922 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8923 && codel != REFERENCE_TYPE)
8924 rhs = TREE_OPERAND (rhs, 0);
8926 if (type == error_mark_node
8927 || rhs == error_mark_node
8928 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8929 return error_mark_node;
8931 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8933 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8934 && TREE_CODE (type) != ARRAY_TYPE
8935 && (TREE_CODE (type) != REFERENCE_TYPE
8936 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8937 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8938 && !TYPE_REFFN_P (type))
8939 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8940 rhs = decay_conversion (rhs, complain);
8942 rhstype = TREE_TYPE (rhs);
8943 coder = TREE_CODE (rhstype);
8945 if (coder == ERROR_MARK)
8946 return error_mark_node;
8948 /* We accept references to incomplete types, so we can
8949 return here before checking if RHS is of complete type. */
8951 if (codel == REFERENCE_TYPE)
8953 /* This should eventually happen in convert_arguments. */
8954 int savew = 0, savee = 0;
8956 if (fndecl)
8957 savew = warningcount + werrorcount, savee = errorcount;
8958 rhs = initialize_reference (type, rhs, flags, complain);
8960 if (fndecl
8961 && (warningcount + werrorcount > savew || errorcount > savee))
8962 inform (DECL_SOURCE_LOCATION (fndecl),
8963 "in passing argument %P of %qD", parmnum, fndecl);
8965 return rhs;
8968 if (exp != 0)
8969 exp = require_complete_type_sfinae (exp, complain);
8970 if (exp == error_mark_node)
8971 return error_mark_node;
8973 rhstype = non_reference (rhstype);
8975 type = complete_type (type);
8977 if (DIRECT_INIT_EXPR_P (type, rhs))
8978 /* Don't try to do copy-initialization if we already have
8979 direct-initialization. */
8980 return rhs;
8982 if (MAYBE_CLASS_TYPE_P (type))
8983 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8985 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8986 complain, flags);
8989 /* If RETVAL is the address of, or a reference to, a local variable or
8990 temporary give an appropriate warning and return true. */
8992 static bool
8993 maybe_warn_about_returning_address_of_local (tree retval)
8995 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8996 tree whats_returned = fold_for_warn (retval);
8998 for (;;)
9000 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9001 whats_returned = TREE_OPERAND (whats_returned, 1);
9002 else if (CONVERT_EXPR_P (whats_returned)
9003 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9004 whats_returned = TREE_OPERAND (whats_returned, 0);
9005 else
9006 break;
9009 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9010 return false;
9011 whats_returned = TREE_OPERAND (whats_returned, 0);
9013 while (TREE_CODE (whats_returned) == COMPONENT_REF
9014 || TREE_CODE (whats_returned) == ARRAY_REF)
9015 whats_returned = TREE_OPERAND (whats_returned, 0);
9017 if (TREE_CODE (valtype) == REFERENCE_TYPE)
9019 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9020 || TREE_CODE (whats_returned) == TARGET_EXPR)
9022 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
9023 return true;
9025 if (VAR_P (whats_returned)
9026 && DECL_NAME (whats_returned)
9027 && TEMP_NAME_P (DECL_NAME (whats_returned)))
9029 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
9030 return true;
9034 if (DECL_P (whats_returned)
9035 && DECL_NAME (whats_returned)
9036 && DECL_FUNCTION_SCOPE_P (whats_returned)
9037 && !is_capture_proxy (whats_returned)
9038 && !(TREE_STATIC (whats_returned)
9039 || TREE_PUBLIC (whats_returned)))
9041 if (TREE_CODE (valtype) == REFERENCE_TYPE)
9042 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9043 OPT_Wreturn_local_addr,
9044 "reference to local variable %qD returned",
9045 whats_returned);
9046 else if (TREE_CODE (whats_returned) == LABEL_DECL)
9047 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9048 OPT_Wreturn_local_addr, "address of label %qD returned",
9049 whats_returned);
9050 else
9051 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9052 OPT_Wreturn_local_addr, "address of local variable %qD "
9053 "returned", whats_returned);
9054 return true;
9057 return false;
9060 /* Check that returning RETVAL from the current function is valid.
9061 Return an expression explicitly showing all conversions required to
9062 change RETVAL into the function return type, and to assign it to
9063 the DECL_RESULT for the function. Set *NO_WARNING to true if
9064 code reaches end of non-void function warning shouldn't be issued
9065 on this RETURN_EXPR. */
9067 tree
9068 check_return_expr (tree retval, bool *no_warning)
9070 tree result;
9071 /* The type actually returned by the function. */
9072 tree valtype;
9073 /* The type the function is declared to return, or void if
9074 the declared type is incomplete. */
9075 tree functype;
9076 int fn_returns_value_p;
9077 bool named_return_value_okay_p;
9079 *no_warning = false;
9081 /* A `volatile' function is one that isn't supposed to return, ever.
9082 (This is a G++ extension, used to get better code for functions
9083 that call the `volatile' function.) */
9084 if (TREE_THIS_VOLATILE (current_function_decl))
9085 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9087 /* Check for various simple errors. */
9088 if (DECL_DESTRUCTOR_P (current_function_decl))
9090 if (retval)
9091 error ("returning a value from a destructor");
9092 return NULL_TREE;
9094 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9096 if (in_function_try_handler)
9097 /* If a return statement appears in a handler of the
9098 function-try-block of a constructor, the program is ill-formed. */
9099 error ("cannot return from a handler of a function-try-block of a constructor");
9100 else if (retval)
9101 /* You can't return a value from a constructor. */
9102 error ("returning a value from a constructor");
9103 return NULL_TREE;
9106 const tree saved_retval = retval;
9108 if (processing_template_decl)
9110 current_function_returns_value = 1;
9112 if (check_for_bare_parameter_packs (retval))
9113 return error_mark_node;
9115 /* If one of the types might be void, we can't tell whether we're
9116 returning a value. */
9117 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9118 && !current_function_auto_return_pattern)
9119 || (retval != NULL_TREE
9120 && (TREE_TYPE (retval) == NULL_TREE
9121 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9122 goto dependent;
9125 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9127 /* Deduce auto return type from a return statement. */
9128 if (current_function_auto_return_pattern)
9130 tree auto_node;
9131 tree type;
9133 if (!retval && !is_auto (current_function_auto_return_pattern))
9135 /* Give a helpful error message. */
9136 error ("return-statement with no value, in function returning %qT",
9137 current_function_auto_return_pattern);
9138 inform (input_location, "only plain %<auto%> return type can be "
9139 "deduced to %<void%>");
9140 type = error_mark_node;
9142 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9144 error ("returning initializer list");
9145 type = error_mark_node;
9147 else
9149 if (!retval)
9150 retval = void_node;
9151 auto_node = type_uses_auto (current_function_auto_return_pattern);
9152 type = do_auto_deduction (current_function_auto_return_pattern,
9153 retval, auto_node);
9156 if (type == error_mark_node)
9157 /* Leave it. */;
9158 else if (functype == current_function_auto_return_pattern)
9159 apply_deduced_return_type (current_function_decl, type);
9160 else if (!same_type_p (type, functype))
9162 if (LAMBDA_FUNCTION_P (current_function_decl))
9163 error ("inconsistent types %qT and %qT deduced for "
9164 "lambda return type", functype, type);
9165 else
9166 error ("inconsistent deduction for auto return type: "
9167 "%qT and then %qT", functype, type);
9169 functype = type;
9172 result = DECL_RESULT (current_function_decl);
9173 valtype = TREE_TYPE (result);
9174 gcc_assert (valtype != NULL_TREE);
9175 fn_returns_value_p = !VOID_TYPE_P (valtype);
9177 /* Check for a return statement with no return value in a function
9178 that's supposed to return a value. */
9179 if (!retval && fn_returns_value_p)
9181 if (functype != error_mark_node)
9182 permerror (input_location, "return-statement with no value, in "
9183 "function returning %qT", valtype);
9184 /* Remember that this function did return. */
9185 current_function_returns_value = 1;
9186 /* And signal caller that TREE_NO_WARNING should be set on the
9187 RETURN_EXPR to avoid control reaches end of non-void function
9188 warnings in tree-cfg.c. */
9189 *no_warning = true;
9191 /* Check for a return statement with a value in a function that
9192 isn't supposed to return a value. */
9193 else if (retval && !fn_returns_value_p)
9195 if (VOID_TYPE_P (TREE_TYPE (retval)))
9196 /* You can return a `void' value from a function of `void'
9197 type. In that case, we have to evaluate the expression for
9198 its side-effects. */
9199 finish_expr_stmt (retval);
9200 else
9201 permerror (input_location,
9202 "return-statement with a value, in function "
9203 "returning %qT", valtype);
9204 current_function_returns_null = 1;
9206 /* There's really no value to return, after all. */
9207 return NULL_TREE;
9209 else if (!retval)
9210 /* Remember that this function can sometimes return without a
9211 value. */
9212 current_function_returns_null = 1;
9213 else
9214 /* Remember that this function did return a value. */
9215 current_function_returns_value = 1;
9217 /* Check for erroneous operands -- but after giving ourselves a
9218 chance to provide an error about returning a value from a void
9219 function. */
9220 if (error_operand_p (retval))
9222 current_function_return_value = error_mark_node;
9223 return error_mark_node;
9226 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9227 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9228 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9229 && ! flag_check_new
9230 && retval && null_ptr_cst_p (retval))
9231 warning (0, "%<operator new%> must not return NULL unless it is "
9232 "declared %<throw()%> (or -fcheck-new is in effect)");
9234 /* Effective C++ rule 15. See also start_function. */
9235 if (warn_ecpp
9236 && DECL_NAME (current_function_decl) == assign_op_identifier)
9238 bool warn = true;
9240 /* The function return type must be a reference to the current
9241 class. */
9242 if (TREE_CODE (valtype) == REFERENCE_TYPE
9243 && same_type_ignoring_top_level_qualifiers_p
9244 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9246 /* Returning '*this' is obviously OK. */
9247 if (retval == current_class_ref)
9248 warn = false;
9249 /* If we are calling a function whose return type is the same of
9250 the current class reference, it is ok. */
9251 else if (INDIRECT_REF_P (retval)
9252 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9253 warn = false;
9256 if (warn)
9257 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9260 if (dependent_type_p (functype)
9261 || type_dependent_expression_p (retval))
9263 dependent:
9264 /* We should not have changed the return value. */
9265 gcc_assert (retval == saved_retval);
9266 return retval;
9269 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9271 [...] For a function with a class return type, if the expression
9272 in the return statement is the name of a local object, and the cv-
9273 unqualified type of the local object is the same as the function
9274 return type, an implementation is permitted to omit creating the tem-
9275 porary object to hold the function return value [...]
9277 So, if this is a value-returning function that always returns the same
9278 local variable, remember it.
9280 It might be nice to be more flexible, and choose the first suitable
9281 variable even if the function sometimes returns something else, but
9282 then we run the risk of clobbering the variable we chose if the other
9283 returned expression uses the chosen variable somehow. And people expect
9284 this restriction, anyway. (jason 2000-11-19)
9286 See finish_function and finalize_nrv for the rest of this optimization. */
9288 named_return_value_okay_p =
9289 (retval != NULL_TREE
9290 && !processing_template_decl
9291 /* Must be a local, automatic variable. */
9292 && VAR_P (retval)
9293 && DECL_CONTEXT (retval) == current_function_decl
9294 && ! TREE_STATIC (retval)
9295 /* And not a lambda or anonymous union proxy. */
9296 && !DECL_HAS_VALUE_EXPR_P (retval)
9297 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9298 /* The cv-unqualified type of the returned value must be the
9299 same as the cv-unqualified return type of the
9300 function. */
9301 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9302 (TYPE_MAIN_VARIANT (functype)))
9303 /* And the returned value must be non-volatile. */
9304 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9306 if (fn_returns_value_p && flag_elide_constructors)
9308 if (named_return_value_okay_p
9309 && (current_function_return_value == NULL_TREE
9310 || current_function_return_value == retval))
9311 current_function_return_value = retval;
9312 else
9313 current_function_return_value = error_mark_node;
9316 /* We don't need to do any conversions when there's nothing being
9317 returned. */
9318 if (!retval)
9319 return NULL_TREE;
9321 /* Do any required conversions. */
9322 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9323 /* No conversions are required. */
9325 else
9327 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9329 /* The functype's return type will have been set to void, if it
9330 was an incomplete type. Just treat this as 'return;' */
9331 if (VOID_TYPE_P (functype))
9332 return error_mark_node;
9334 /* If we had an id-expression obfuscated by force_paren_expr, we need
9335 to undo it so we can try to treat it as an rvalue below. */
9336 retval = maybe_undo_parenthesized_ref (retval);
9338 if (processing_template_decl)
9339 retval = build_non_dependent_expr (retval);
9341 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9342 treated as an rvalue for the purposes of overload resolution to
9343 favor move constructors over copy constructors.
9345 Note that these conditions are similar to, but not as strict as,
9346 the conditions for the named return value optimization. */
9347 bool converted = false;
9348 if ((cxx_dialect != cxx98)
9349 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9350 || TREE_CODE (retval) == PARM_DECL)
9351 && DECL_CONTEXT (retval) == current_function_decl
9352 && !TREE_STATIC (retval)
9353 /* This is only interesting for class type. */
9354 && CLASS_TYPE_P (functype))
9356 tree moved = move (retval);
9357 moved = convert_for_initialization
9358 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9359 ICR_RETURN, NULL_TREE, 0, tf_none);
9360 if (moved != error_mark_node)
9362 retval = moved;
9363 converted = true;
9367 /* First convert the value to the function's return type, then
9368 to the type of return value's location to handle the
9369 case that functype is smaller than the valtype. */
9370 if (!converted)
9371 retval = convert_for_initialization
9372 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9373 tf_warning_or_error);
9374 retval = convert (valtype, retval);
9376 /* If the conversion failed, treat this just like `return;'. */
9377 if (retval == error_mark_node)
9378 return retval;
9379 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9380 else if (! cfun->returns_struct
9381 && TREE_CODE (retval) == TARGET_EXPR
9382 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9383 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9384 TREE_OPERAND (retval, 0));
9385 else if (!processing_template_decl
9386 && maybe_warn_about_returning_address_of_local (retval))
9387 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9388 build_zero_cst (TREE_TYPE (retval)));
9391 if (processing_template_decl)
9392 return saved_retval;
9394 /* Actually copy the value returned into the appropriate location. */
9395 if (retval && retval != result)
9396 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9398 return retval;
9402 /* Returns nonzero if the pointer-type FROM can be converted to the
9403 pointer-type TO via a qualification conversion. If CONSTP is -1,
9404 then we return nonzero if the pointers are similar, and the
9405 cv-qualification signature of FROM is a proper subset of that of TO.
9407 If CONSTP is positive, then all outer pointers have been
9408 const-qualified. */
9410 static int
9411 comp_ptr_ttypes_real (tree to, tree from, int constp)
9413 bool to_more_cv_qualified = false;
9414 bool is_opaque_pointer = false;
9416 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9418 if (TREE_CODE (to) != TREE_CODE (from))
9419 return 0;
9421 if (TREE_CODE (from) == OFFSET_TYPE
9422 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9423 TYPE_OFFSET_BASETYPE (to)))
9424 return 0;
9426 /* Const and volatile mean something different for function types,
9427 so the usual checks are not appropriate. */
9428 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9430 if (!at_least_as_qualified_p (to, from))
9431 return 0;
9433 if (!at_least_as_qualified_p (from, to))
9435 if (constp == 0)
9436 return 0;
9437 to_more_cv_qualified = true;
9440 if (constp > 0)
9441 constp &= TYPE_READONLY (to);
9444 if (VECTOR_TYPE_P (to))
9445 is_opaque_pointer = vector_targets_convertible_p (to, from);
9447 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9448 return ((constp >= 0 || to_more_cv_qualified)
9449 && (is_opaque_pointer
9450 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9454 /* When comparing, say, char ** to char const **, this function takes
9455 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9456 types to this function. */
9459 comp_ptr_ttypes (tree to, tree from)
9461 return comp_ptr_ttypes_real (to, from, 1);
9464 /* Returns true iff FNTYPE is a non-class type that involves
9465 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9466 if a parameter type is ill-formed. */
9468 bool
9469 error_type_p (const_tree type)
9471 tree t;
9473 switch (TREE_CODE (type))
9475 case ERROR_MARK:
9476 return true;
9478 case POINTER_TYPE:
9479 case REFERENCE_TYPE:
9480 case OFFSET_TYPE:
9481 return error_type_p (TREE_TYPE (type));
9483 case FUNCTION_TYPE:
9484 case METHOD_TYPE:
9485 if (error_type_p (TREE_TYPE (type)))
9486 return true;
9487 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9488 if (error_type_p (TREE_VALUE (t)))
9489 return true;
9490 return false;
9492 case RECORD_TYPE:
9493 if (TYPE_PTRMEMFUNC_P (type))
9494 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9495 return false;
9497 default:
9498 return false;
9502 /* Returns true if to and from are (possibly multi-level) pointers to the same
9503 type or inheritance-related types, regardless of cv-quals. */
9505 bool
9506 ptr_reasonably_similar (const_tree to, const_tree from)
9508 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9510 /* Any target type is similar enough to void. */
9511 if (VOID_TYPE_P (to))
9512 return !error_type_p (from);
9513 if (VOID_TYPE_P (from))
9514 return !error_type_p (to);
9516 if (TREE_CODE (to) != TREE_CODE (from))
9517 return false;
9519 if (TREE_CODE (from) == OFFSET_TYPE
9520 && comptypes (TYPE_OFFSET_BASETYPE (to),
9521 TYPE_OFFSET_BASETYPE (from),
9522 COMPARE_BASE | COMPARE_DERIVED))
9523 continue;
9525 if (VECTOR_TYPE_P (to)
9526 && vector_types_convertible_p (to, from, false))
9527 return true;
9529 if (TREE_CODE (to) == INTEGER_TYPE
9530 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9531 return true;
9533 if (TREE_CODE (to) == FUNCTION_TYPE)
9534 return !error_type_p (to) && !error_type_p (from);
9536 if (!TYPE_PTR_P (to))
9538 /* When either type is incomplete avoid DERIVED_FROM_P,
9539 which may call complete_type (c++/57942). */
9540 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9541 return comptypes
9542 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9543 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9548 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9549 pointer-to-member types) are the same, ignoring cv-qualification at
9550 all levels. */
9552 bool
9553 comp_ptr_ttypes_const (tree to, tree from)
9555 bool is_opaque_pointer = false;
9557 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9559 if (TREE_CODE (to) != TREE_CODE (from))
9560 return false;
9562 if (TREE_CODE (from) == OFFSET_TYPE
9563 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9564 TYPE_OFFSET_BASETYPE (to)))
9565 continue;
9567 if (VECTOR_TYPE_P (to))
9568 is_opaque_pointer = vector_targets_convertible_p (to, from);
9570 if (!TYPE_PTR_P (to))
9571 return (is_opaque_pointer
9572 || same_type_ignoring_top_level_qualifiers_p (to, from));
9576 /* Returns the type qualifiers for this type, including the qualifiers on the
9577 elements for an array type. */
9580 cp_type_quals (const_tree type)
9582 int quals;
9583 /* This CONST_CAST is okay because strip_array_types returns its
9584 argument unmodified and we assign it to a const_tree. */
9585 type = strip_array_types (CONST_CAST_TREE (type));
9586 if (type == error_mark_node
9587 /* Quals on a FUNCTION_TYPE are memfn quals. */
9588 || TREE_CODE (type) == FUNCTION_TYPE)
9589 return TYPE_UNQUALIFIED;
9590 quals = TYPE_QUALS (type);
9591 /* METHOD and REFERENCE_TYPEs should never have quals. */
9592 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9593 && TREE_CODE (type) != REFERENCE_TYPE)
9594 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9595 == TYPE_UNQUALIFIED));
9596 return quals;
9599 /* Returns the function-ref-qualifier for TYPE */
9601 cp_ref_qualifier
9602 type_memfn_rqual (const_tree type)
9604 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9605 || TREE_CODE (type) == METHOD_TYPE);
9607 if (!FUNCTION_REF_QUALIFIED (type))
9608 return REF_QUAL_NONE;
9609 else if (FUNCTION_RVALUE_QUALIFIED (type))
9610 return REF_QUAL_RVALUE;
9611 else
9612 return REF_QUAL_LVALUE;
9615 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9616 METHOD_TYPE. */
9619 type_memfn_quals (const_tree type)
9621 if (TREE_CODE (type) == FUNCTION_TYPE)
9622 return TYPE_QUALS (type);
9623 else if (TREE_CODE (type) == METHOD_TYPE)
9624 return cp_type_quals (class_of_this_parm (type));
9625 else
9626 gcc_unreachable ();
9629 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9630 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9632 tree
9633 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9635 /* Could handle METHOD_TYPE here if necessary. */
9636 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9637 if (TYPE_QUALS (type) == memfn_quals
9638 && type_memfn_rqual (type) == rqual)
9639 return type;
9641 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9642 complex. */
9643 tree result = build_qualified_type (type, memfn_quals);
9644 return build_ref_qualified_type (result, rqual);
9647 /* Returns nonzero if TYPE is const or volatile. */
9649 bool
9650 cv_qualified_p (const_tree type)
9652 int quals = cp_type_quals (type);
9653 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9656 /* Returns nonzero if the TYPE contains a mutable member. */
9658 bool
9659 cp_has_mutable_p (const_tree type)
9661 /* This CONST_CAST is okay because strip_array_types returns its
9662 argument unmodified and we assign it to a const_tree. */
9663 type = strip_array_types (CONST_CAST_TREE(type));
9665 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9668 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9669 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9670 approximation. In particular, consider:
9672 int f();
9673 struct S { int i; };
9674 const S s = { f(); }
9676 Here, we will make "s" as TREE_READONLY (because it is declared
9677 "const") -- only to reverse ourselves upon seeing that the
9678 initializer is non-constant. */
9680 void
9681 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9683 tree type = TREE_TYPE (decl);
9685 if (type == error_mark_node)
9686 return;
9688 if (TREE_CODE (decl) == TYPE_DECL)
9689 return;
9691 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9692 && type_quals != TYPE_UNQUALIFIED));
9694 /* Avoid setting TREE_READONLY incorrectly. */
9695 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9696 constructor can produce constant init, so rely on cp_finish_decl to
9697 clear TREE_READONLY if the variable has non-constant init. */
9699 /* If the type has (or might have) a mutable component, that component
9700 might be modified. */
9701 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9702 type_quals &= ~TYPE_QUAL_CONST;
9704 c_apply_type_quals_to_decl (type_quals, decl);
9707 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9708 exemplar types such that casting T1 to T2 is casting away constness
9709 if and only if there is no implicit conversion from T1 to T2. */
9711 static void
9712 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9714 int quals1;
9715 int quals2;
9717 /* [expr.const.cast]
9719 For multi-level pointer to members and multi-level mixed pointers
9720 and pointers to members (conv.qual), the "member" aspect of a
9721 pointer to member level is ignored when determining if a const
9722 cv-qualifier has been cast away. */
9723 /* [expr.const.cast]
9725 For two pointer types:
9727 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9728 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9729 K is min(N,M)
9731 casting from X1 to X2 casts away constness if, for a non-pointer
9732 type T there does not exist an implicit conversion (clause
9733 _conv_) from:
9735 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9739 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9740 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9741 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9743 *t1 = cp_build_qualified_type (void_type_node,
9744 cp_type_quals (*t1));
9745 *t2 = cp_build_qualified_type (void_type_node,
9746 cp_type_quals (*t2));
9747 return;
9750 quals1 = cp_type_quals (*t1);
9751 quals2 = cp_type_quals (*t2);
9753 if (TYPE_PTRDATAMEM_P (*t1))
9754 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9755 else
9756 *t1 = TREE_TYPE (*t1);
9757 if (TYPE_PTRDATAMEM_P (*t2))
9758 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9759 else
9760 *t2 = TREE_TYPE (*t2);
9762 casts_away_constness_r (t1, t2, complain);
9763 *t1 = build_pointer_type (*t1);
9764 *t2 = build_pointer_type (*t2);
9765 *t1 = cp_build_qualified_type (*t1, quals1);
9766 *t2 = cp_build_qualified_type (*t2, quals2);
9769 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9770 constness.
9772 ??? This function returns non-zero if casting away qualifiers not
9773 just const. We would like to return to the caller exactly which
9774 qualifiers are casted away to give more accurate diagnostics.
9777 static bool
9778 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9780 if (TREE_CODE (t2) == REFERENCE_TYPE)
9782 /* [expr.const.cast]
9784 Casting from an lvalue of type T1 to an lvalue of type T2
9785 using a reference cast casts away constness if a cast from an
9786 rvalue of type "pointer to T1" to the type "pointer to T2"
9787 casts away constness. */
9788 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9789 return casts_away_constness (build_pointer_type (t1),
9790 build_pointer_type (TREE_TYPE (t2)),
9791 complain);
9794 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9795 /* [expr.const.cast]
9797 Casting from an rvalue of type "pointer to data member of X
9798 of type T1" to the type "pointer to data member of Y of type
9799 T2" casts away constness if a cast from an rvalue of type
9800 "pointer to T1" to the type "pointer to T2" casts away
9801 constness. */
9802 return casts_away_constness
9803 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9804 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9805 complain);
9807 /* Casting away constness is only something that makes sense for
9808 pointer or reference types. */
9809 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9810 return false;
9812 /* Top-level qualifiers don't matter. */
9813 t1 = TYPE_MAIN_VARIANT (t1);
9814 t2 = TYPE_MAIN_VARIANT (t2);
9815 casts_away_constness_r (&t1, &t2, complain);
9816 if (!can_convert (t2, t1, complain))
9817 return true;
9819 return false;
9822 /* If T is a REFERENCE_TYPE return the type to which T refers.
9823 Otherwise, return T itself. */
9825 tree
9826 non_reference (tree t)
9828 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9829 t = TREE_TYPE (t);
9830 return t;
9834 /* Return nonzero if REF is an lvalue valid for this language;
9835 otherwise, print an error message and return zero. USE says
9836 how the lvalue is being used and so selects the error message. */
9839 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9841 cp_lvalue_kind kind = lvalue_kind (ref);
9843 if (kind == clk_none)
9845 if (complain & tf_error)
9846 lvalue_error (input_location, use);
9847 return 0;
9849 else if (kind & (clk_rvalueref|clk_class))
9851 if (!(complain & tf_error))
9852 return 0;
9853 if (kind & clk_class)
9854 /* Make this a permerror because we used to accept it. */
9855 permerror (input_location, "using temporary as lvalue");
9856 else
9857 error ("using xvalue (rvalue reference) as lvalue");
9859 return 1;
9862 /* Return true if a user-defined literal operator is a raw operator. */
9864 bool
9865 check_raw_literal_operator (const_tree decl)
9867 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9868 tree argtype;
9869 int arity;
9870 bool maybe_raw_p = false;
9872 /* Count the number and type of arguments and check for ellipsis. */
9873 for (argtype = argtypes, arity = 0;
9874 argtype && argtype != void_list_node;
9875 ++arity, argtype = TREE_CHAIN (argtype))
9877 tree t = TREE_VALUE (argtype);
9879 if (same_type_p (t, const_string_type_node))
9880 maybe_raw_p = true;
9882 if (!argtype)
9883 return false; /* Found ellipsis. */
9885 if (!maybe_raw_p || arity != 1)
9886 return false;
9888 return true;
9892 /* Return true if a user-defined literal operator has one of the allowed
9893 argument types. */
9895 bool
9896 check_literal_operator_args (const_tree decl,
9897 bool *long_long_unsigned_p, bool *long_double_p)
9899 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9901 *long_long_unsigned_p = false;
9902 *long_double_p = false;
9903 if (processing_template_decl || processing_specialization)
9904 return argtypes == void_list_node;
9905 else
9907 tree argtype;
9908 int arity;
9909 int max_arity = 2;
9911 /* Count the number and type of arguments and check for ellipsis. */
9912 for (argtype = argtypes, arity = 0;
9913 argtype && argtype != void_list_node;
9914 argtype = TREE_CHAIN (argtype))
9916 tree t = TREE_VALUE (argtype);
9917 ++arity;
9919 if (TYPE_PTR_P (t))
9921 bool maybe_raw_p = false;
9922 t = TREE_TYPE (t);
9923 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9924 return false;
9925 t = TYPE_MAIN_VARIANT (t);
9926 if ((maybe_raw_p = same_type_p (t, char_type_node))
9927 || same_type_p (t, wchar_type_node)
9928 || same_type_p (t, char16_type_node)
9929 || same_type_p (t, char32_type_node))
9931 argtype = TREE_CHAIN (argtype);
9932 if (!argtype)
9933 return false;
9934 t = TREE_VALUE (argtype);
9935 if (maybe_raw_p && argtype == void_list_node)
9936 return true;
9937 else if (same_type_p (t, size_type_node))
9939 ++arity;
9940 continue;
9942 else
9943 return false;
9946 else if (same_type_p (t, long_long_unsigned_type_node))
9948 max_arity = 1;
9949 *long_long_unsigned_p = true;
9951 else if (same_type_p (t, long_double_type_node))
9953 max_arity = 1;
9954 *long_double_p = true;
9956 else if (same_type_p (t, char_type_node))
9957 max_arity = 1;
9958 else if (same_type_p (t, wchar_type_node))
9959 max_arity = 1;
9960 else if (same_type_p (t, char16_type_node))
9961 max_arity = 1;
9962 else if (same_type_p (t, char32_type_node))
9963 max_arity = 1;
9964 else
9965 return false;
9967 if (!argtype)
9968 return false; /* Found ellipsis. */
9970 if (arity != max_arity)
9971 return false;
9973 return true;
9977 /* Always returns false since unlike C90, C++ has no concept of implicit
9978 function declarations. */
9980 bool
9981 c_decl_implicit (const_tree)
9983 return false;