2018-04-04 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / typeck.c
blob2b7a771bbeb300178617097dc004f7c607ca98c7
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:;
902 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
903 return t1;
904 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
905 return t2;
906 break;
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))
2013 mark_rvalue_use (exp, loc, reject_builtin);
2014 return nullptr_node;
2017 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2018 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2019 if (code == VOID_TYPE)
2021 if (complain & tf_error)
2022 error_at (loc, "void value not ignored as it ought to be");
2023 return error_mark_node;
2025 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2026 return error_mark_node;
2027 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2029 exp = mark_lvalue_use (exp);
2030 if (reject_builtin && reject_gcc_builtin (exp, loc))
2031 return error_mark_node;
2032 return cp_build_addr_expr (exp, complain);
2034 if (code == ARRAY_TYPE)
2036 tree adr;
2037 tree ptrtype;
2039 exp = mark_lvalue_use (exp);
2041 if (INDIRECT_REF_P (exp))
2042 return build_nop (build_pointer_type (TREE_TYPE (type)),
2043 TREE_OPERAND (exp, 0));
2045 if (TREE_CODE (exp) == COMPOUND_EXPR)
2047 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2048 if (op1 == error_mark_node)
2049 return error_mark_node;
2050 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2051 TREE_OPERAND (exp, 0), op1);
2054 if (!obvalue_p (exp)
2055 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2057 if (complain & tf_error)
2058 error_at (loc, "invalid use of non-lvalue array");
2059 return error_mark_node;
2062 /* Don't let an array compound literal decay to a pointer. It can
2063 still be used to initialize an array or bind to a reference. */
2064 if (TREE_CODE (exp) == TARGET_EXPR)
2066 if (complain & tf_error)
2067 error_at (loc, "taking address of temporary array");
2068 return error_mark_node;
2071 ptrtype = build_pointer_type (TREE_TYPE (type));
2073 if (VAR_P (exp))
2075 if (!cxx_mark_addressable (exp))
2076 return error_mark_node;
2077 adr = build_nop (ptrtype, build_address (exp));
2078 return adr;
2080 /* This way is better for a COMPONENT_REF since it can
2081 simplify the offset for a component. */
2082 adr = cp_build_addr_expr (exp, complain);
2083 return cp_convert (ptrtype, adr, complain);
2086 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2087 exp = mark_rvalue_use (exp, loc, reject_builtin);
2089 /* If a bitfield is used in a context where integral promotion
2090 applies, then the caller is expected to have used
2091 default_conversion. That function promotes bitfields correctly
2092 before calling this function. At this point, if we have a
2093 bitfield referenced, we may assume that is not subject to
2094 promotion, and that, therefore, the type of the resulting rvalue
2095 is the declared type of the bitfield. */
2096 exp = convert_bitfield_to_declared_type (exp);
2098 /* We do not call rvalue() here because we do not want to wrap EXP
2099 in a NON_LVALUE_EXPR. */
2101 /* [basic.lval]
2103 Non-class rvalues always have cv-unqualified types. */
2104 type = TREE_TYPE (exp);
2105 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2106 exp = build_nop (cv_unqualified (type), exp);
2108 if (!complete_type_or_maybe_complain (type, exp, complain))
2109 return error_mark_node;
2111 return exp;
2114 /* Perform preparatory conversions, as part of the "usual arithmetic
2115 conversions". In particular, as per [expr]:
2117 Whenever an lvalue expression appears as an operand of an
2118 operator that expects the rvalue for that operand, the
2119 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2120 standard conversions are applied to convert the expression to an
2121 rvalue.
2123 In addition, we perform integral promotions here, as those are
2124 applied to both operands to a binary operator before determining
2125 what additional conversions should apply. */
2127 static tree
2128 cp_default_conversion (tree exp, tsubst_flags_t complain)
2130 /* Check for target-specific promotions. */
2131 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2132 if (promoted_type)
2133 exp = cp_convert (promoted_type, exp, complain);
2134 /* Perform the integral promotions first so that bitfield
2135 expressions (which may promote to "int", even if the bitfield is
2136 declared "unsigned") are promoted correctly. */
2137 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2138 exp = cp_perform_integral_promotions (exp, complain);
2139 /* Perform the other conversions. */
2140 exp = decay_conversion (exp, complain);
2142 return exp;
2145 /* C version. */
2147 tree
2148 default_conversion (tree exp)
2150 return cp_default_conversion (exp, tf_warning_or_error);
2153 /* EXPR is an expression with an integral or enumeration type.
2154 Perform the integral promotions in [conv.prom], and return the
2155 converted value. */
2157 tree
2158 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2160 tree type;
2161 tree promoted_type;
2163 expr = mark_rvalue_use (expr);
2164 if (error_operand_p (expr))
2165 return error_mark_node;
2167 /* [conv.prom]
2169 If the bitfield has an enumerated type, it is treated as any
2170 other value of that type for promotion purposes. */
2171 type = is_bitfield_expr_with_lowered_type (expr);
2172 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2173 type = TREE_TYPE (expr);
2174 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2175 /* Scoped enums don't promote. */
2176 if (SCOPED_ENUM_P (type))
2177 return expr;
2178 promoted_type = type_promotes_to (type);
2179 if (type != promoted_type)
2180 expr = cp_convert (promoted_type, expr, complain);
2181 return expr;
2184 /* C version. */
2186 tree
2187 perform_integral_promotions (tree expr)
2189 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2192 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2193 decay_conversion to one. */
2196 string_conv_p (const_tree totype, const_tree exp, int warn)
2198 tree t;
2200 if (!TYPE_PTR_P (totype))
2201 return 0;
2203 t = TREE_TYPE (totype);
2204 if (!same_type_p (t, char_type_node)
2205 && !same_type_p (t, char16_type_node)
2206 && !same_type_p (t, char32_type_node)
2207 && !same_type_p (t, wchar_type_node))
2208 return 0;
2210 STRIP_ANY_LOCATION_WRAPPER (exp);
2212 if (TREE_CODE (exp) == STRING_CST)
2214 /* Make sure that we don't try to convert between char and wide chars. */
2215 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2216 return 0;
2218 else
2220 /* Is this a string constant which has decayed to 'const char *'? */
2221 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2222 if (!same_type_p (TREE_TYPE (exp), t))
2223 return 0;
2224 STRIP_NOPS (exp);
2225 if (TREE_CODE (exp) != ADDR_EXPR
2226 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2227 return 0;
2229 if (warn)
2231 if (cxx_dialect >= cxx11)
2232 pedwarn (input_location, OPT_Wwrite_strings,
2233 "ISO C++ forbids converting a string constant to %qT",
2234 totype);
2235 else
2236 warning (OPT_Wwrite_strings,
2237 "deprecated conversion from string constant to %qT",
2238 totype);
2241 return 1;
2244 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2245 can, for example, use as an lvalue. This code used to be in
2246 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2247 expressions, where we're dealing with aggregates. But now it's again only
2248 called from unary_complex_lvalue. The case (in particular) that led to
2249 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2250 get it there. */
2252 static tree
2253 rationalize_conditional_expr (enum tree_code code, tree t,
2254 tsubst_flags_t complain)
2256 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2258 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2259 the first operand is always the one to be used if both operands
2260 are equal, so we know what conditional expression this used to be. */
2261 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2263 tree op0 = TREE_OPERAND (t, 0);
2264 tree op1 = TREE_OPERAND (t, 1);
2266 /* The following code is incorrect if either operand side-effects. */
2267 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2268 && !TREE_SIDE_EFFECTS (op1));
2269 return
2270 build_conditional_expr (loc,
2271 build_x_binary_op (loc,
2272 (TREE_CODE (t) == MIN_EXPR
2273 ? LE_EXPR : GE_EXPR),
2274 op0, TREE_CODE (op0),
2275 op1, TREE_CODE (op1),
2276 /*overload=*/NULL,
2277 complain),
2278 cp_build_unary_op (code, op0, false, complain),
2279 cp_build_unary_op (code, op1, false, complain),
2280 complain);
2283 return
2284 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2285 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2286 complain),
2287 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2288 complain),
2289 complain);
2292 /* Given the TYPE of an anonymous union field inside T, return the
2293 FIELD_DECL for the field. If not found return NULL_TREE. Because
2294 anonymous unions can nest, we must also search all anonymous unions
2295 that are directly reachable. */
2297 tree
2298 lookup_anon_field (tree t, tree type)
2300 tree field;
2302 t = TYPE_MAIN_VARIANT (t);
2304 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2306 if (TREE_STATIC (field))
2307 continue;
2308 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2309 continue;
2311 /* If we find it directly, return the field. */
2312 if (DECL_NAME (field) == NULL_TREE
2313 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2315 return field;
2318 /* Otherwise, it could be nested, search harder. */
2319 if (DECL_NAME (field) == NULL_TREE
2320 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2322 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2323 if (subfield)
2324 return subfield;
2327 return NULL_TREE;
2330 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2331 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2332 non-NULL, it indicates the path to the base used to name MEMBER.
2333 If PRESERVE_REFERENCE is true, the expression returned will have
2334 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2335 returned will have the type referred to by the reference.
2337 This function does not perform access control; that is either done
2338 earlier by the parser when the name of MEMBER is resolved to MEMBER
2339 itself, or later when overload resolution selects one of the
2340 functions indicated by MEMBER. */
2342 tree
2343 build_class_member_access_expr (cp_expr object, tree member,
2344 tree access_path, bool preserve_reference,
2345 tsubst_flags_t complain)
2347 tree object_type;
2348 tree member_scope;
2349 tree result = NULL_TREE;
2350 tree using_decl = NULL_TREE;
2352 if (error_operand_p (object) || error_operand_p (member))
2353 return error_mark_node;
2355 gcc_assert (DECL_P (member) || BASELINK_P (member));
2357 /* [expr.ref]
2359 The type of the first expression shall be "class object" (of a
2360 complete type). */
2361 object_type = TREE_TYPE (object);
2362 if (!currently_open_class (object_type)
2363 && !complete_type_or_maybe_complain (object_type, object, complain))
2364 return error_mark_node;
2365 if (!CLASS_TYPE_P (object_type))
2367 if (complain & tf_error)
2369 if (POINTER_TYPE_P (object_type)
2370 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2371 error ("request for member %qD in %qE, which is of pointer "
2372 "type %qT (maybe you meant to use %<->%> ?)",
2373 member, object.get_value (), object_type);
2374 else
2375 error ("request for member %qD in %qE, which is of non-class "
2376 "type %qT", member, object.get_value (), object_type);
2378 return error_mark_node;
2381 /* The standard does not seem to actually say that MEMBER must be a
2382 member of OBJECT_TYPE. However, that is clearly what is
2383 intended. */
2384 if (DECL_P (member))
2386 member_scope = DECL_CLASS_CONTEXT (member);
2387 if (!mark_used (member, complain) && !(complain & tf_error))
2388 return error_mark_node;
2389 if (TREE_DEPRECATED (member))
2390 warn_deprecated_use (member, NULL_TREE);
2392 else
2393 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2394 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2395 presently be the anonymous union. Go outwards until we find a
2396 type related to OBJECT_TYPE. */
2397 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2398 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2399 object_type))
2400 member_scope = TYPE_CONTEXT (member_scope);
2401 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2403 if (complain & tf_error)
2405 if (TREE_CODE (member) == FIELD_DECL)
2406 error ("invalid use of nonstatic data member %qE", member);
2407 else
2408 error ("%qD is not a member of %qT", member, object_type);
2410 return error_mark_node;
2413 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2414 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2415 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2417 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2418 if (temp)
2419 object = cp_build_fold_indirect_ref (temp);
2422 /* In [expr.ref], there is an explicit list of the valid choices for
2423 MEMBER. We check for each of those cases here. */
2424 if (VAR_P (member))
2426 /* A static data member. */
2427 result = member;
2428 mark_exp_read (object);
2429 /* If OBJECT has side-effects, they are supposed to occur. */
2430 if (TREE_SIDE_EFFECTS (object))
2431 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2433 else if (TREE_CODE (member) == FIELD_DECL)
2435 /* A non-static data member. */
2436 bool null_object_p;
2437 int type_quals;
2438 tree member_type;
2440 if (INDIRECT_REF_P (object))
2441 null_object_p =
2442 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2443 else
2444 null_object_p = false;
2446 /* Convert OBJECT to the type of MEMBER. */
2447 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2448 TYPE_MAIN_VARIANT (member_scope)))
2450 tree binfo;
2451 base_kind kind;
2453 binfo = lookup_base (access_path ? access_path : object_type,
2454 member_scope, ba_unique, &kind, complain);
2455 if (binfo == error_mark_node)
2456 return error_mark_node;
2458 /* It is invalid to try to get to a virtual base of a
2459 NULL object. The most common cause is invalid use of
2460 offsetof macro. */
2461 if (null_object_p && kind == bk_via_virtual)
2463 if (complain & tf_error)
2465 error ("invalid access to non-static data member %qD in "
2466 "virtual base of NULL object", member);
2468 return error_mark_node;
2471 /* Convert to the base. */
2472 object = build_base_path (PLUS_EXPR, object, binfo,
2473 /*nonnull=*/1, complain);
2474 /* If we found the base successfully then we should be able
2475 to convert to it successfully. */
2476 gcc_assert (object != error_mark_node);
2479 /* If MEMBER is from an anonymous aggregate, we have converted
2480 OBJECT so that it refers to the class containing the
2481 anonymous union. Generate a reference to the anonymous union
2482 itself, and recur to find MEMBER. */
2483 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2484 /* When this code is called from build_field_call, the
2485 object already has the type of the anonymous union.
2486 That is because the COMPONENT_REF was already
2487 constructed, and was then disassembled before calling
2488 build_field_call. After the function-call code is
2489 cleaned up, this waste can be eliminated. */
2490 && (!same_type_ignoring_top_level_qualifiers_p
2491 (TREE_TYPE (object), DECL_CONTEXT (member))))
2493 tree anonymous_union;
2495 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2496 DECL_CONTEXT (member));
2497 object = build_class_member_access_expr (object,
2498 anonymous_union,
2499 /*access_path=*/NULL_TREE,
2500 preserve_reference,
2501 complain);
2504 /* Compute the type of the field, as described in [expr.ref]. */
2505 type_quals = TYPE_UNQUALIFIED;
2506 member_type = TREE_TYPE (member);
2507 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2509 type_quals = (cp_type_quals (member_type)
2510 | cp_type_quals (object_type));
2512 /* A field is const (volatile) if the enclosing object, or the
2513 field itself, is const (volatile). But, a mutable field is
2514 not const, even within a const object. */
2515 if (DECL_MUTABLE_P (member))
2516 type_quals &= ~TYPE_QUAL_CONST;
2517 member_type = cp_build_qualified_type (member_type, type_quals);
2520 result = build3_loc (input_location, COMPONENT_REF, member_type,
2521 object, member, NULL_TREE);
2523 /* Mark the expression const or volatile, as appropriate. Even
2524 though we've dealt with the type above, we still have to mark the
2525 expression itself. */
2526 if (type_quals & TYPE_QUAL_CONST)
2527 TREE_READONLY (result) = 1;
2528 if (type_quals & TYPE_QUAL_VOLATILE)
2529 TREE_THIS_VOLATILE (result) = 1;
2531 else if (BASELINK_P (member))
2533 /* The member is a (possibly overloaded) member function. */
2534 tree functions;
2535 tree type;
2537 /* If the MEMBER is exactly one static member function, then we
2538 know the type of the expression. Otherwise, we must wait
2539 until overload resolution has been performed. */
2540 functions = BASELINK_FUNCTIONS (member);
2541 if (TREE_CODE (functions) == FUNCTION_DECL
2542 && DECL_STATIC_FUNCTION_P (functions))
2543 type = TREE_TYPE (functions);
2544 else
2545 type = unknown_type_node;
2546 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2547 base. That will happen when the function is called. */
2548 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2550 else if (TREE_CODE (member) == CONST_DECL)
2552 /* The member is an enumerator. */
2553 result = member;
2554 /* If OBJECT has side-effects, they are supposed to occur. */
2555 if (TREE_SIDE_EFFECTS (object))
2556 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2557 object, result);
2559 else if ((using_decl = strip_using_decl (member)) != member)
2560 result = build_class_member_access_expr (object,
2561 using_decl,
2562 access_path, preserve_reference,
2563 complain);
2564 else
2566 if (complain & tf_error)
2567 error ("invalid use of %qD", member);
2568 return error_mark_node;
2571 if (!preserve_reference)
2572 /* [expr.ref]
2574 If E2 is declared to have type "reference to T", then ... the
2575 type of E1.E2 is T. */
2576 result = convert_from_reference (result);
2578 return result;
2581 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2582 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2584 static tree
2585 lookup_destructor (tree object, tree scope, tree dtor_name,
2586 tsubst_flags_t complain)
2588 tree object_type = TREE_TYPE (object);
2589 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2590 tree expr;
2592 /* We've already complained about this destructor. */
2593 if (dtor_type == error_mark_node)
2594 return error_mark_node;
2596 if (scope && !check_dtor_name (scope, dtor_type))
2598 if (complain & tf_error)
2599 error ("qualified type %qT does not match destructor name ~%qT",
2600 scope, dtor_type);
2601 return error_mark_node;
2603 if (is_auto (dtor_type))
2604 dtor_type = object_type;
2605 else if (identifier_p (dtor_type))
2607 /* In a template, names we can't find a match for are still accepted
2608 destructor names, and we check them here. */
2609 if (check_dtor_name (object_type, dtor_type))
2610 dtor_type = object_type;
2611 else
2613 if (complain & tf_error)
2614 error ("object type %qT does not match destructor name ~%qT",
2615 object_type, dtor_type);
2616 return error_mark_node;
2620 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2622 if (complain & tf_error)
2623 error ("the type being destroyed is %qT, but the destructor "
2624 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2625 return error_mark_node;
2627 expr = lookup_member (dtor_type, complete_dtor_identifier,
2628 /*protect=*/1, /*want_type=*/false,
2629 tf_warning_or_error);
2630 if (!expr)
2632 if (complain & tf_error)
2633 cxx_incomplete_type_error (dtor_name, dtor_type);
2634 return error_mark_node;
2636 expr = (adjust_result_of_qualified_name_lookup
2637 (expr, dtor_type, object_type));
2638 if (scope == NULL_TREE)
2639 /* We need to call adjust_result_of_qualified_name_lookup in case the
2640 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2641 that we still get virtual function binding. */
2642 BASELINK_QUALIFIED_P (expr) = false;
2643 return expr;
2646 /* An expression of the form "A::template B" has been resolved to
2647 DECL. Issue a diagnostic if B is not a template or template
2648 specialization. */
2650 void
2651 check_template_keyword (tree decl)
2653 /* The standard says:
2655 [temp.names]
2657 If a name prefixed by the keyword template is not a member
2658 template, the program is ill-formed.
2660 DR 228 removed the restriction that the template be a member
2661 template.
2663 DR 96, if accepted would add the further restriction that explicit
2664 template arguments must be provided if the template keyword is
2665 used, but, as of 2005-10-16, that DR is still in "drafting". If
2666 this DR is accepted, then the semantic checks here can be
2667 simplified, as the entity named must in fact be a template
2668 specialization, rather than, as at present, a set of overloaded
2669 functions containing at least one template function. */
2670 if (TREE_CODE (decl) != TEMPLATE_DECL
2671 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2673 if (VAR_P (decl))
2675 if (DECL_USE_TEMPLATE (decl)
2676 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2678 else
2679 permerror (input_location, "%qD is not a template", decl);
2681 else if (!is_overloaded_fn (decl))
2682 permerror (input_location, "%qD is not a template", decl);
2683 else
2685 bool found = false;
2687 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2688 !found && iter; ++iter)
2690 tree fn = *iter;
2691 if (TREE_CODE (fn) == TEMPLATE_DECL
2692 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2693 || (TREE_CODE (fn) == FUNCTION_DECL
2694 && DECL_USE_TEMPLATE (fn)
2695 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2696 found = true;
2698 if (!found)
2699 permerror (input_location, "%qD is not a template", decl);
2704 /* Record that an access failure occurred on BASETYPE_PATH attempting
2705 to access FIELD_DECL. */
2707 void
2708 access_failure_info::record_access_failure (tree basetype_path,
2709 tree field_decl)
2711 m_was_inaccessible = true;
2712 m_basetype_path = basetype_path;
2713 m_field_decl = field_decl;
2716 /* If an access failure was recorded, then attempt to locate an
2717 accessor function for the pertinent field, and if one is
2718 available, add a note and fix-it hint suggesting using it. */
2720 void
2721 access_failure_info::maybe_suggest_accessor (bool const_p) const
2723 if (!m_was_inaccessible)
2724 return;
2726 tree accessor
2727 = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2728 if (!accessor)
2729 return;
2731 /* The accessor must itself be accessible for it to be a reasonable
2732 suggestion. */
2733 if (!accessible_p (m_basetype_path, accessor, true))
2734 return;
2736 rich_location richloc (line_table, input_location);
2737 pretty_printer pp;
2738 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2739 richloc.add_fixit_replace (pp_formatted_text (&pp));
2740 inform (&richloc, "field %q#D can be accessed via %q#D",
2741 m_field_decl, accessor);
2744 /* This function is called by the parser to process a class member
2745 access expression of the form OBJECT.NAME. NAME is a node used by
2746 the parser to represent a name; it is not yet a DECL. It may,
2747 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2748 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2749 there is no reason to do the lookup twice, so the parser keeps the
2750 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2751 be a template via the use of the "A::template B" syntax. */
2753 tree
2754 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2755 tsubst_flags_t complain)
2757 tree expr;
2758 tree object_type;
2759 tree member;
2760 tree access_path = NULL_TREE;
2761 tree orig_object = object;
2762 tree orig_name = name;
2764 if (object == error_mark_node || name == error_mark_node)
2765 return error_mark_node;
2767 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2768 if (!objc_is_public (object, name))
2769 return error_mark_node;
2771 object_type = TREE_TYPE (object);
2773 if (processing_template_decl)
2775 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2776 type_dependent_object_expression_p (object)
2777 /* If NAME is "f<args>", where either 'f' or 'args' is
2778 dependent, then the expression is dependent. */
2779 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2780 && dependent_template_id_p (TREE_OPERAND (name, 0),
2781 TREE_OPERAND (name, 1)))
2782 /* If NAME is "T::X" where "T" is dependent, then the
2783 expression is dependent. */
2784 || (TREE_CODE (name) == SCOPE_REF
2785 && TYPE_P (TREE_OPERAND (name, 0))
2786 && dependent_scope_p (TREE_OPERAND (name, 0))))
2788 dependent:
2789 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2790 orig_object, orig_name, NULL_TREE);
2792 object = build_non_dependent_expr (object);
2794 else if (c_dialect_objc ()
2795 && identifier_p (name)
2796 && (expr = objc_maybe_build_component_ref (object, name)))
2797 return expr;
2799 /* [expr.ref]
2801 The type of the first expression shall be "class object" (of a
2802 complete type). */
2803 if (!currently_open_class (object_type)
2804 && !complete_type_or_maybe_complain (object_type, object, complain))
2805 return error_mark_node;
2806 if (!CLASS_TYPE_P (object_type))
2808 if (complain & tf_error)
2810 if (POINTER_TYPE_P (object_type)
2811 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2812 error ("request for member %qD in %qE, which is of pointer "
2813 "type %qT (maybe you meant to use %<->%> ?)",
2814 name, object.get_value (), object_type);
2815 else
2816 error ("request for member %qD in %qE, which is of non-class "
2817 "type %qT", name, object.get_value (), object_type);
2819 return error_mark_node;
2822 if (BASELINK_P (name))
2823 /* A member function that has already been looked up. */
2824 member = name;
2825 else
2827 bool is_template_id = false;
2828 tree template_args = NULL_TREE;
2829 tree scope = NULL_TREE;
2831 access_path = object_type;
2833 if (TREE_CODE (name) == SCOPE_REF)
2835 /* A qualified name. The qualifying class or namespace `S'
2836 has already been looked up; it is either a TYPE or a
2837 NAMESPACE_DECL. */
2838 scope = TREE_OPERAND (name, 0);
2839 name = TREE_OPERAND (name, 1);
2841 /* If SCOPE is a namespace, then the qualified name does not
2842 name a member of OBJECT_TYPE. */
2843 if (TREE_CODE (scope) == NAMESPACE_DECL)
2845 if (complain & tf_error)
2846 error ("%<%D::%D%> is not a member of %qT",
2847 scope, name, object_type);
2848 return error_mark_node;
2852 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2854 is_template_id = true;
2855 template_args = TREE_OPERAND (name, 1);
2856 name = TREE_OPERAND (name, 0);
2858 if (!identifier_p (name))
2859 name = OVL_NAME (name);
2862 if (scope)
2864 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2866 gcc_assert (!is_template_id);
2867 /* Looking up a member enumerator (c++/56793). */
2868 if (!TYPE_CLASS_SCOPE_P (scope)
2869 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2871 if (complain & tf_error)
2872 error ("%<%D::%D%> is not a member of %qT",
2873 scope, name, object_type);
2874 return error_mark_node;
2876 tree val = lookup_enumerator (scope, name);
2877 if (!val)
2879 if (complain & tf_error)
2880 error ("%qD is not a member of %qD",
2881 name, scope);
2882 return error_mark_node;
2885 if (TREE_SIDE_EFFECTS (object))
2886 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2887 return val;
2890 gcc_assert (CLASS_TYPE_P (scope));
2891 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2893 if (constructor_name_p (name, scope))
2895 if (complain & tf_error)
2896 error ("cannot call constructor %<%T::%D%> directly",
2897 scope, name);
2898 return error_mark_node;
2901 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2902 access_path = lookup_base (object_type, scope, ba_check,
2903 NULL, complain);
2904 if (access_path == error_mark_node)
2905 return error_mark_node;
2906 if (!access_path)
2908 if (any_dependent_bases_p (object_type))
2909 goto dependent;
2910 if (complain & tf_error)
2911 error ("%qT is not a base of %qT", scope, object_type);
2912 return error_mark_node;
2916 if (TREE_CODE (name) == BIT_NOT_EXPR)
2918 if (dependent_type_p (object_type))
2919 /* The destructor isn't declared yet. */
2920 goto dependent;
2921 member = lookup_destructor (object, scope, name, complain);
2923 else
2925 /* Look up the member. */
2926 access_failure_info afi;
2927 member = lookup_member (access_path, name, /*protect=*/1,
2928 /*want_type=*/false, complain,
2929 &afi);
2930 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2931 if (member == NULL_TREE)
2933 if (dependent_type_p (object_type))
2934 /* Try again at instantiation time. */
2935 goto dependent;
2936 if (complain & tf_error)
2938 tree guessed_id = lookup_member_fuzzy (access_path, name,
2939 /*want_type=*/false);
2940 if (guessed_id)
2942 location_t bogus_component_loc = input_location;
2943 gcc_rich_location rich_loc (bogus_component_loc);
2944 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2945 guessed_id);
2946 error_at (&rich_loc,
2947 "%q#T has no member named %qE;"
2948 " did you mean %qE?",
2949 TREE_CODE (access_path) == TREE_BINFO
2950 ? TREE_TYPE (access_path) : object_type,
2951 name, guessed_id);
2953 else
2954 error ("%q#T has no member named %qE",
2955 TREE_CODE (access_path) == TREE_BINFO
2956 ? TREE_TYPE (access_path) : object_type, name);
2958 return error_mark_node;
2960 if (member == error_mark_node)
2961 return error_mark_node;
2962 if (DECL_P (member)
2963 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2964 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2965 wrong, so don't use it. */
2966 goto dependent;
2967 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2968 goto dependent;
2971 if (is_template_id)
2973 tree templ = member;
2975 if (BASELINK_P (templ))
2976 member = lookup_template_function (templ, template_args);
2977 else if (variable_template_p (templ))
2978 member = (lookup_and_finish_template_variable
2979 (templ, template_args, complain));
2980 else
2982 if (complain & tf_error)
2983 error ("%qD is not a member template function", name);
2984 return error_mark_node;
2989 if (TREE_DEPRECATED (member))
2990 warn_deprecated_use (member, NULL_TREE);
2992 if (template_p)
2993 check_template_keyword (member);
2995 expr = build_class_member_access_expr (object, member, access_path,
2996 /*preserve_reference=*/false,
2997 complain);
2998 if (processing_template_decl && expr != error_mark_node)
3000 if (BASELINK_P (member))
3002 if (TREE_CODE (orig_name) == SCOPE_REF)
3003 BASELINK_QUALIFIED_P (member) = 1;
3004 orig_name = member;
3006 return build_min_non_dep (COMPONENT_REF, expr,
3007 orig_object, orig_name,
3008 NULL_TREE);
3011 return expr;
3014 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3015 type. */
3017 tree
3018 build_simple_component_ref (tree object, tree member)
3020 tree type = cp_build_qualified_type (TREE_TYPE (member),
3021 cp_type_quals (TREE_TYPE (object)));
3022 return build3_loc (input_location,
3023 COMPONENT_REF, type,
3024 object, member, NULL_TREE);
3027 /* Return an expression for the MEMBER_NAME field in the internal
3028 representation of PTRMEM, a pointer-to-member function. (Each
3029 pointer-to-member function type gets its own RECORD_TYPE so it is
3030 more convenient to access the fields by name than by FIELD_DECL.)
3031 This routine converts the NAME to a FIELD_DECL and then creates the
3032 node for the complete expression. */
3034 tree
3035 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3037 tree ptrmem_type;
3038 tree member;
3040 /* This code is a stripped down version of
3041 build_class_member_access_expr. It does not work to use that
3042 routine directly because it expects the object to be of class
3043 type. */
3044 ptrmem_type = TREE_TYPE (ptrmem);
3045 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3046 for (member = TYPE_FIELDS (ptrmem_type); member;
3047 member = DECL_CHAIN (member))
3048 if (DECL_NAME (member) == member_name)
3049 break;
3050 tree res = build_simple_component_ref (ptrmem, member);
3052 TREE_NO_WARNING (res) = 1;
3053 return res;
3056 /* Given an expression PTR for a pointer, return an expression
3057 for the value pointed to.
3058 ERRORSTRING is the name of the operator to appear in error messages.
3060 This function may need to overload OPERATOR_FNNAME.
3061 Must also handle REFERENCE_TYPEs for C++. */
3063 tree
3064 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3065 tsubst_flags_t complain)
3067 tree orig_expr = expr;
3068 tree rval;
3069 tree overload = NULL_TREE;
3071 if (processing_template_decl)
3073 /* Retain the type if we know the operand is a pointer. */
3074 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
3075 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3076 if (type_dependent_expression_p (expr))
3077 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3078 expr = build_non_dependent_expr (expr);
3081 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3082 NULL_TREE, NULL_TREE, &overload, complain);
3083 if (!rval)
3084 rval = cp_build_indirect_ref (expr, errorstring, complain);
3086 if (processing_template_decl && rval != error_mark_node)
3088 if (overload != NULL_TREE)
3089 return (build_min_non_dep_op_overload
3090 (INDIRECT_REF, rval, overload, orig_expr));
3092 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3094 else
3095 return rval;
3098 /* The implementation of the above, and of indirection implied by other
3099 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3101 static tree
3102 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3103 tsubst_flags_t complain, bool do_fold)
3105 tree pointer, type;
3107 /* RO_NULL should only be used with the folding entry points below, not
3108 cp_build_indirect_ref. */
3109 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3111 if (ptr == current_class_ptr
3112 || (TREE_CODE (ptr) == NOP_EXPR
3113 && TREE_OPERAND (ptr, 0) == current_class_ptr
3114 && (same_type_ignoring_top_level_qualifiers_p
3115 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3116 return current_class_ref;
3118 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3119 ? ptr : decay_conversion (ptr, complain));
3120 if (pointer == error_mark_node)
3121 return error_mark_node;
3123 type = TREE_TYPE (pointer);
3125 if (POINTER_TYPE_P (type))
3127 /* [expr.unary.op]
3129 If the type of the expression is "pointer to T," the type
3130 of the result is "T." */
3131 tree t = TREE_TYPE (type);
3133 if ((CONVERT_EXPR_P (ptr)
3134 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3135 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3137 /* If a warning is issued, mark it to avoid duplicates from
3138 the backend. This only needs to be done at
3139 warn_strict_aliasing > 2. */
3140 if (warn_strict_aliasing > 2)
3141 if (strict_aliasing_warning (EXPR_LOCATION (ptr),
3142 type, TREE_OPERAND (ptr, 0)))
3143 TREE_NO_WARNING (ptr) = 1;
3146 if (VOID_TYPE_P (t))
3148 /* A pointer to incomplete type (other than cv void) can be
3149 dereferenced [expr.unary.op]/1 */
3150 if (complain & tf_error)
3151 error ("%qT is not a pointer-to-object type", type);
3152 return error_mark_node;
3154 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3155 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3156 /* The POINTER was something like `&x'. We simplify `*&x' to
3157 `x'. */
3158 return TREE_OPERAND (pointer, 0);
3159 else
3161 tree ref = build1 (INDIRECT_REF, t, pointer);
3163 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3164 so that we get the proper error message if the result is used
3165 to assign to. Also, &* is supposed to be a no-op. */
3166 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3167 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3168 TREE_SIDE_EFFECTS (ref)
3169 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3170 return ref;
3173 else if (!(complain & tf_error))
3174 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3176 /* `pointer' won't be an error_mark_node if we were given a
3177 pointer to member, so it's cool to check for this here. */
3178 else if (TYPE_PTRMEM_P (type))
3179 switch (errorstring)
3181 case RO_ARRAY_INDEXING:
3182 error ("invalid use of array indexing on pointer to member");
3183 break;
3184 case RO_UNARY_STAR:
3185 error ("invalid use of unary %<*%> on pointer to member");
3186 break;
3187 case RO_IMPLICIT_CONVERSION:
3188 error ("invalid use of implicit conversion on pointer to member");
3189 break;
3190 case RO_ARROW_STAR:
3191 error ("left hand operand of %<->*%> must be a pointer to class, "
3192 "but is a pointer to member of type %qT", type);
3193 break;
3194 default:
3195 gcc_unreachable ();
3197 else if (pointer != error_mark_node)
3198 invalid_indirection_error (input_location, type, errorstring);
3200 return error_mark_node;
3203 /* Entry point used by c-common, which expects folding. */
3205 tree
3206 build_indirect_ref (location_t /*loc*/,
3207 tree ptr, ref_operator errorstring)
3209 return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3212 /* Entry point used by internal indirection needs that don't correspond to any
3213 syntactic construct. */
3215 tree
3216 cp_build_fold_indirect_ref (tree pointer)
3218 return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3221 /* Entry point used by indirection needs that correspond to some syntactic
3222 construct. */
3224 tree
3225 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3226 tsubst_flags_t complain)
3228 return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3231 /* This handles expressions of the form "a[i]", which denotes
3232 an array reference.
3234 This is logically equivalent in C to *(a+i), but we may do it differently.
3235 If A is a variable or a member, we generate a primitive ARRAY_REF.
3236 This avoids forcing the array out of registers, and can work on
3237 arrays that are not lvalues (for example, members of structures returned
3238 by functions).
3240 If INDEX is of some user-defined type, it must be converted to
3241 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3242 will inherit the type of the array, which will be some pointer type.
3244 LOC is the location to use in building the array reference. */
3246 tree
3247 cp_build_array_ref (location_t loc, tree array, tree idx,
3248 tsubst_flags_t complain)
3250 tree ret;
3252 if (idx == 0)
3254 if (complain & tf_error)
3255 error_at (loc, "subscript missing in array reference");
3256 return error_mark_node;
3259 if (TREE_TYPE (array) == error_mark_node
3260 || TREE_TYPE (idx) == error_mark_node)
3261 return error_mark_node;
3263 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3264 inside it. */
3265 switch (TREE_CODE (array))
3267 case COMPOUND_EXPR:
3269 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3270 complain);
3271 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3272 TREE_OPERAND (array, 0), value);
3273 SET_EXPR_LOCATION (ret, loc);
3274 return ret;
3277 case COND_EXPR:
3278 ret = build_conditional_expr
3279 (loc, TREE_OPERAND (array, 0),
3280 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3281 complain),
3282 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3283 complain),
3284 complain);
3285 protected_set_expr_location (ret, loc);
3286 return ret;
3288 default:
3289 break;
3292 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3294 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3296 tree rval, type;
3298 warn_array_subscript_with_type_char (loc, idx);
3300 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3302 if (complain & tf_error)
3303 error_at (loc, "array subscript is not an integer");
3304 return error_mark_node;
3307 /* Apply integral promotions *after* noticing character types.
3308 (It is unclear why we do these promotions -- the standard
3309 does not say that we should. In fact, the natural thing would
3310 seem to be to convert IDX to ptrdiff_t; we're performing
3311 pointer arithmetic.) */
3312 idx = cp_perform_integral_promotions (idx, complain);
3314 /* An array that is indexed by a non-constant
3315 cannot be stored in a register; we must be able to do
3316 address arithmetic on its address.
3317 Likewise an array of elements of variable size. */
3318 if (TREE_CODE (idx) != INTEGER_CST
3319 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3320 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3321 != INTEGER_CST)))
3323 if (!cxx_mark_addressable (array, true))
3324 return error_mark_node;
3327 /* An array that is indexed by a constant value which is not within
3328 the array bounds cannot be stored in a register either; because we
3329 would get a crash in store_bit_field/extract_bit_field when trying
3330 to access a non-existent part of the register. */
3331 if (TREE_CODE (idx) == INTEGER_CST
3332 && TYPE_DOMAIN (TREE_TYPE (array))
3333 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3335 if (!cxx_mark_addressable (array))
3336 return error_mark_node;
3339 /* Note in C++ it is valid to subscript a `register' array, since
3340 it is valid to take the address of something with that
3341 storage specification. */
3342 if (extra_warnings)
3344 tree foo = array;
3345 while (TREE_CODE (foo) == COMPONENT_REF)
3346 foo = TREE_OPERAND (foo, 0);
3347 if (VAR_P (foo) && DECL_REGISTER (foo)
3348 && (complain & tf_warning))
3349 warning_at (loc, OPT_Wextra,
3350 "subscripting array declared %<register%>");
3353 type = TREE_TYPE (TREE_TYPE (array));
3354 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3355 /* Array ref is const/volatile if the array elements are
3356 or if the array is.. */
3357 TREE_READONLY (rval)
3358 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3359 TREE_SIDE_EFFECTS (rval)
3360 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3361 TREE_THIS_VOLATILE (rval)
3362 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3363 ret = require_complete_type_sfinae (rval, complain);
3364 protected_set_expr_location (ret, loc);
3365 if (non_lvalue)
3366 ret = non_lvalue_loc (loc, ret);
3367 return ret;
3371 tree ar = cp_default_conversion (array, complain);
3372 tree ind = cp_default_conversion (idx, complain);
3374 /* Put the integer in IND to simplify error checking. */
3375 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3376 std::swap (ar, ind);
3378 if (ar == error_mark_node || ind == error_mark_node)
3379 return error_mark_node;
3381 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3383 if (complain & tf_error)
3384 error_at (loc, "subscripted value is neither array nor pointer");
3385 return error_mark_node;
3387 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3389 if (complain & tf_error)
3390 error_at (loc, "array subscript is not an integer");
3391 return error_mark_node;
3394 warn_array_subscript_with_type_char (loc, idx);
3396 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3397 PLUS_EXPR, ar, ind,
3398 complain),
3399 RO_ARRAY_INDEXING,
3400 complain);
3401 protected_set_expr_location (ret, loc);
3402 if (non_lvalue)
3403 ret = non_lvalue_loc (loc, ret);
3404 return ret;
3408 /* Entry point for Obj-C++. */
3410 tree
3411 build_array_ref (location_t loc, tree array, tree idx)
3413 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3416 /* Resolve a pointer to member function. INSTANCE is the object
3417 instance to use, if the member points to a virtual member.
3419 This used to avoid checking for virtual functions if basetype
3420 has no virtual functions, according to an earlier ANSI draft.
3421 With the final ISO C++ rules, such an optimization is
3422 incorrect: A pointer to a derived member can be static_cast
3423 to pointer-to-base-member, as long as the dynamic object
3424 later has the right member. So now we only do this optimization
3425 when we know the dynamic type of the object. */
3427 tree
3428 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3429 tsubst_flags_t complain)
3431 if (TREE_CODE (function) == OFFSET_REF)
3432 function = TREE_OPERAND (function, 1);
3434 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3436 tree idx, delta, e1, e2, e3, vtbl;
3437 bool nonvirtual;
3438 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3439 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3441 tree instance_ptr = *instance_ptrptr;
3442 tree instance_save_expr = 0;
3443 if (instance_ptr == error_mark_node)
3445 if (TREE_CODE (function) == PTRMEM_CST)
3447 /* Extracting the function address from a pmf is only
3448 allowed with -Wno-pmf-conversions. It only works for
3449 pmf constants. */
3450 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3451 e1 = convert (fntype, e1);
3452 return e1;
3454 else
3456 if (complain & tf_error)
3457 error ("object missing in use of %qE", function);
3458 return error_mark_node;
3462 /* True if we know that the dynamic type of the object doesn't have
3463 virtual functions, so we can assume the PFN field is a pointer. */
3464 nonvirtual = (COMPLETE_TYPE_P (basetype)
3465 && !TYPE_POLYMORPHIC_P (basetype)
3466 && resolves_to_fixed_type_p (instance_ptr, 0));
3468 /* If we don't really have an object (i.e. in an ill-formed
3469 conversion from PMF to pointer), we can't resolve virtual
3470 functions anyway. */
3471 if (!nonvirtual && is_dummy_object (instance_ptr))
3472 nonvirtual = true;
3474 if (TREE_SIDE_EFFECTS (instance_ptr))
3475 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3477 if (TREE_SIDE_EFFECTS (function))
3478 function = save_expr (function);
3480 /* Start by extracting all the information from the PMF itself. */
3481 e3 = pfn_from_ptrmemfunc (function);
3482 delta = delta_from_ptrmemfunc (function);
3483 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3484 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3486 int flag_sanitize_save;
3487 case ptrmemfunc_vbit_in_pfn:
3488 e1 = cp_build_binary_op (input_location,
3489 BIT_AND_EXPR, idx, integer_one_node,
3490 complain);
3491 idx = cp_build_binary_op (input_location,
3492 MINUS_EXPR, idx, integer_one_node,
3493 complain);
3494 if (idx == error_mark_node)
3495 return error_mark_node;
3496 break;
3498 case ptrmemfunc_vbit_in_delta:
3499 e1 = cp_build_binary_op (input_location,
3500 BIT_AND_EXPR, delta, integer_one_node,
3501 complain);
3502 /* Don't instrument the RSHIFT_EXPR we're about to create because
3503 we're going to use DELTA number of times, and that wouldn't play
3504 well with SAVE_EXPRs therein. */
3505 flag_sanitize_save = flag_sanitize;
3506 flag_sanitize = 0;
3507 delta = cp_build_binary_op (input_location,
3508 RSHIFT_EXPR, delta, integer_one_node,
3509 complain);
3510 flag_sanitize = flag_sanitize_save;
3511 if (delta == error_mark_node)
3512 return error_mark_node;
3513 break;
3515 default:
3516 gcc_unreachable ();
3519 if (e1 == error_mark_node)
3520 return error_mark_node;
3522 /* Convert down to the right base before using the instance. A
3523 special case is that in a pointer to member of class C, C may
3524 be incomplete. In that case, the function will of course be
3525 a member of C, and no conversion is required. In fact,
3526 lookup_base will fail in that case, because incomplete
3527 classes do not have BINFOs. */
3528 if (!same_type_ignoring_top_level_qualifiers_p
3529 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3531 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3532 basetype, ba_check, NULL, complain);
3533 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3534 1, complain);
3535 if (instance_ptr == error_mark_node)
3536 return error_mark_node;
3538 /* ...and then the delta in the PMF. */
3539 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3541 /* Hand back the adjusted 'this' argument to our caller. */
3542 *instance_ptrptr = instance_ptr;
3544 if (nonvirtual)
3545 /* Now just return the pointer. */
3546 return e3;
3548 /* Next extract the vtable pointer from the object. */
3549 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3550 instance_ptr);
3551 vtbl = cp_build_fold_indirect_ref (vtbl);
3552 if (vtbl == error_mark_node)
3553 return error_mark_node;
3555 /* Finally, extract the function pointer from the vtable. */
3556 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3557 e2 = cp_build_fold_indirect_ref (e2);
3558 if (e2 == error_mark_node)
3559 return error_mark_node;
3560 TREE_CONSTANT (e2) = 1;
3562 /* When using function descriptors, the address of the
3563 vtable entry is treated as a function pointer. */
3564 if (TARGET_VTABLE_USES_DESCRIPTORS)
3565 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3566 cp_build_addr_expr (e2, complain));
3568 e2 = fold_convert (TREE_TYPE (e3), e2);
3569 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3570 if (e1 == error_mark_node)
3571 return error_mark_node;
3573 /* Make sure this doesn't get evaluated first inside one of the
3574 branches of the COND_EXPR. */
3575 if (instance_save_expr)
3576 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3577 instance_save_expr, e1);
3579 function = e1;
3581 return function;
3584 /* Used by the C-common bits. */
3585 tree
3586 build_function_call (location_t /*loc*/,
3587 tree function, tree params)
3589 return cp_build_function_call (function, params, tf_warning_or_error);
3592 /* Used by the C-common bits. */
3593 tree
3594 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3595 tree function, vec<tree, va_gc> *params,
3596 vec<tree, va_gc> * /*origtypes*/)
3598 vec<tree, va_gc> *orig_params = params;
3599 tree ret = cp_build_function_call_vec (function, &params,
3600 tf_warning_or_error);
3602 /* cp_build_function_call_vec can reallocate PARAMS by adding
3603 default arguments. That should never happen here. Verify
3604 that. */
3605 gcc_assert (params == orig_params);
3607 return ret;
3610 /* Build a function call using a tree list of arguments. */
3612 static tree
3613 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3615 vec<tree, va_gc> *vec;
3616 tree ret;
3618 vec = make_tree_vector ();
3619 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3620 vec_safe_push (vec, TREE_VALUE (params));
3621 ret = cp_build_function_call_vec (function, &vec, complain);
3622 release_tree_vector (vec);
3623 return ret;
3626 /* Build a function call using varargs. */
3628 tree
3629 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3631 vec<tree, va_gc> *vec;
3632 va_list args;
3633 tree ret, t;
3635 vec = make_tree_vector ();
3636 va_start (args, complain);
3637 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3638 vec_safe_push (vec, t);
3639 va_end (args);
3640 ret = cp_build_function_call_vec (function, &vec, complain);
3641 release_tree_vector (vec);
3642 return ret;
3645 /* Build a function call using a vector of arguments. PARAMS may be
3646 NULL if there are no parameters. This changes the contents of
3647 PARAMS. */
3649 tree
3650 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3651 tsubst_flags_t complain)
3653 tree fntype, fndecl;
3654 int is_method;
3655 tree original = function;
3656 int nargs;
3657 tree *argarray;
3658 tree parm_types;
3659 vec<tree, va_gc> *allocated = NULL;
3660 tree ret;
3662 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3663 expressions, like those used for ObjC messenger dispatches. */
3664 if (params != NULL && !vec_safe_is_empty (*params))
3665 function = objc_rewrite_function_call (function, (**params)[0]);
3667 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3668 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3669 if (TREE_CODE (function) == NOP_EXPR
3670 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3671 function = TREE_OPERAND (function, 0);
3673 if (TREE_CODE (function) == FUNCTION_DECL)
3675 /* If the function is a non-template member function
3676 or a non-template friend, then we need to check the
3677 constraints.
3679 Note that if overload resolution failed with a single
3680 candidate this function will be used to explicitly diagnose
3681 the failure for the single call expression. The check is
3682 technically redundant since we also would have failed in
3683 add_function_candidate. */
3684 if (flag_concepts
3685 && (complain & tf_error)
3686 && !constraints_satisfied_p (function))
3688 error ("cannot call function %qD", function);
3689 location_t loc = DECL_SOURCE_LOCATION (function);
3690 diagnose_constraints (loc, function, NULL_TREE);
3691 return error_mark_node;
3694 if (!mark_used (function, complain) && !(complain & tf_error))
3695 return error_mark_node;
3696 fndecl = function;
3698 /* Convert anything with function type to a pointer-to-function. */
3699 if (DECL_MAIN_P (function))
3701 if (complain & tf_error)
3702 pedwarn (input_location, OPT_Wpedantic,
3703 "ISO C++ forbids calling %<::main%> from within program");
3704 else
3705 return error_mark_node;
3707 function = build_addr_func (function, complain);
3709 else
3711 fndecl = NULL_TREE;
3713 function = build_addr_func (function, complain);
3716 if (function == error_mark_node)
3717 return error_mark_node;
3719 fntype = TREE_TYPE (function);
3721 if (TYPE_PTRMEMFUNC_P (fntype))
3723 if (complain & tf_error)
3724 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3725 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3726 original, original);
3727 return error_mark_node;
3730 is_method = (TYPE_PTR_P (fntype)
3731 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3733 if (!(TYPE_PTRFN_P (fntype)
3734 || is_method
3735 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3737 if (complain & tf_error)
3739 if (!flag_diagnostics_show_caret)
3740 error_at (input_location,
3741 "%qE cannot be used as a function", original);
3742 else if (DECL_P (original))
3743 error_at (input_location,
3744 "%qD cannot be used as a function", original);
3745 else
3746 error_at (input_location,
3747 "expression cannot be used as a function");
3750 return error_mark_node;
3753 /* fntype now gets the type of function pointed to. */
3754 fntype = TREE_TYPE (fntype);
3755 parm_types = TYPE_ARG_TYPES (fntype);
3757 if (params == NULL)
3759 allocated = make_tree_vector ();
3760 params = &allocated;
3763 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3764 complain);
3765 if (nargs < 0)
3766 return error_mark_node;
3768 argarray = (*params)->address ();
3770 /* Check for errors in format strings and inappropriately
3771 null parameters. */
3772 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3773 nargs, argarray, NULL);
3775 ret = build_cxx_call (function, nargs, argarray, complain);
3777 if (warned_p)
3779 tree c = extract_call_expr (ret);
3780 if (TREE_CODE (c) == CALL_EXPR)
3781 TREE_NO_WARNING (c) = 1;
3784 if (allocated != NULL)
3785 release_tree_vector (allocated);
3787 return ret;
3790 /* Subroutine of convert_arguments.
3791 Print an error message about a wrong number of arguments. */
3793 static void
3794 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3796 if (fndecl)
3798 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3800 if (DECL_NAME (fndecl) == NULL_TREE
3801 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3802 error_at (loc,
3803 too_many_p
3804 ? G_("too many arguments to constructor %q#D")
3805 : G_("too few arguments to constructor %q#D"),
3806 fndecl);
3807 else
3808 error_at (loc,
3809 too_many_p
3810 ? G_("too many arguments to member function %q#D")
3811 : G_("too few arguments to member function %q#D"),
3812 fndecl);
3814 else
3815 error_at (loc,
3816 too_many_p
3817 ? G_("too many arguments to function %q#D")
3818 : G_("too few arguments to function %q#D"),
3819 fndecl);
3820 if (!DECL_IS_BUILTIN (fndecl))
3821 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3823 else
3825 if (c_dialect_objc () && objc_message_selector ())
3826 error_at (loc,
3827 too_many_p
3828 ? G_("too many arguments to method %q#D")
3829 : G_("too few arguments to method %q#D"),
3830 objc_message_selector ());
3831 else
3832 error_at (loc, too_many_p ? G_("too many arguments to function")
3833 : G_("too few arguments to function"));
3837 /* Convert the actual parameter expressions in the list VALUES to the
3838 types in the list TYPELIST. The converted expressions are stored
3839 back in the VALUES vector.
3840 If parmdecls is exhausted, or when an element has NULL as its type,
3841 perform the default conversions.
3843 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3845 This is also where warnings about wrong number of args are generated.
3847 Returns the actual number of arguments processed (which might be less
3848 than the length of the vector), or -1 on error.
3850 In C++, unspecified trailing parameters can be filled in with their
3851 default arguments, if such were specified. Do so here. */
3853 static int
3854 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3855 int flags, tsubst_flags_t complain)
3857 tree typetail;
3858 unsigned int i;
3860 /* Argument passing is always copy-initialization. */
3861 flags |= LOOKUP_ONLYCONVERTING;
3863 for (i = 0, typetail = typelist;
3864 i < vec_safe_length (*values);
3865 i++)
3867 tree type = typetail ? TREE_VALUE (typetail) : 0;
3868 tree val = (**values)[i];
3870 if (val == error_mark_node || type == error_mark_node)
3871 return -1;
3873 if (type == void_type_node)
3875 if (complain & tf_error)
3877 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3878 return i;
3880 else
3881 return -1;
3884 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3885 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3886 if (TREE_CODE (val) == NOP_EXPR
3887 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3888 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3889 val = TREE_OPERAND (val, 0);
3891 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3893 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3894 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3895 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3896 val = decay_conversion (val, complain);
3899 if (val == error_mark_node)
3900 return -1;
3902 if (type != 0)
3904 /* Formal parm type is specified by a function prototype. */
3905 tree parmval;
3907 if (!COMPLETE_TYPE_P (complete_type (type)))
3909 if (complain & tf_error)
3911 if (fndecl)
3912 error ("parameter %P of %qD has incomplete type %qT",
3913 i, fndecl, type);
3914 else
3915 error ("parameter %P has incomplete type %qT", i, type);
3917 parmval = error_mark_node;
3919 else
3921 parmval = convert_for_initialization
3922 (NULL_TREE, type, val, flags,
3923 ICR_ARGPASS, fndecl, i, complain);
3924 parmval = convert_for_arg_passing (type, parmval, complain);
3927 if (parmval == error_mark_node)
3928 return -1;
3930 (**values)[i] = parmval;
3932 else
3934 if (fndecl && magic_varargs_p (fndecl))
3935 /* Don't do ellipsis conversion for __built_in_constant_p
3936 as this will result in spurious errors for non-trivial
3937 types. */
3938 val = require_complete_type_sfinae (val, complain);
3939 else
3940 val = convert_arg_to_ellipsis (val, complain);
3942 (**values)[i] = val;
3945 if (typetail)
3946 typetail = TREE_CHAIN (typetail);
3949 if (typetail != 0 && typetail != void_list_node)
3951 /* See if there are default arguments that can be used. Because
3952 we hold default arguments in the FUNCTION_TYPE (which is so
3953 wrong), we can see default parameters here from deduced
3954 contexts (and via typeof) for indirect function calls.
3955 Fortunately we know whether we have a function decl to
3956 provide default arguments in a language conformant
3957 manner. */
3958 if (fndecl && TREE_PURPOSE (typetail)
3959 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3961 for (; typetail != void_list_node; ++i)
3963 /* After DR777, with explicit template args we can end up with a
3964 default argument followed by no default argument. */
3965 if (!TREE_PURPOSE (typetail))
3966 break;
3967 tree parmval
3968 = convert_default_arg (TREE_VALUE (typetail),
3969 TREE_PURPOSE (typetail),
3970 fndecl, i, complain);
3972 if (parmval == error_mark_node)
3973 return -1;
3975 vec_safe_push (*values, parmval);
3976 typetail = TREE_CHAIN (typetail);
3977 /* ends with `...'. */
3978 if (typetail == NULL_TREE)
3979 break;
3983 if (typetail && typetail != void_list_node)
3985 if (complain & tf_error)
3986 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3987 return -1;
3991 return (int) i;
3994 /* Build a binary-operation expression, after performing default
3995 conversions on the operands. CODE is the kind of expression to
3996 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3997 are the tree codes which correspond to ARG1 and ARG2 when issuing
3998 warnings about possibly misplaced parentheses. They may differ
3999 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4000 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4001 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4002 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4003 ARG2_CODE as ERROR_MARK. */
4005 tree
4006 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4007 enum tree_code arg1_code, tree arg2,
4008 enum tree_code arg2_code, tree *overload_p,
4009 tsubst_flags_t complain)
4011 tree orig_arg1;
4012 tree orig_arg2;
4013 tree expr;
4014 tree overload = NULL_TREE;
4016 orig_arg1 = arg1;
4017 orig_arg2 = arg2;
4019 if (processing_template_decl)
4021 if (type_dependent_expression_p (arg1)
4022 || type_dependent_expression_p (arg2))
4023 return build_min_nt_loc (loc, code, arg1, arg2);
4024 arg1 = build_non_dependent_expr (arg1);
4025 arg2 = build_non_dependent_expr (arg2);
4028 if (code == DOTSTAR_EXPR)
4029 expr = build_m_component_ref (arg1, arg2, complain);
4030 else
4031 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4032 &overload, complain);
4034 if (overload_p != NULL)
4035 *overload_p = overload;
4037 /* Check for cases such as x+y<<z which users are likely to
4038 misinterpret. But don't warn about obj << x + y, since that is a
4039 common idiom for I/O. */
4040 if (warn_parentheses
4041 && (complain & tf_warning)
4042 && !processing_template_decl
4043 && !error_operand_p (arg1)
4044 && !error_operand_p (arg2)
4045 && (code != LSHIFT_EXPR
4046 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4047 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4048 arg2_code, orig_arg2);
4050 if (processing_template_decl && expr != error_mark_node)
4052 if (overload != NULL_TREE)
4053 return (build_min_non_dep_op_overload
4054 (code, expr, overload, orig_arg1, orig_arg2));
4056 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4059 return expr;
4062 /* Build and return an ARRAY_REF expression. */
4064 tree
4065 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4066 tsubst_flags_t complain)
4068 tree orig_arg1 = arg1;
4069 tree orig_arg2 = arg2;
4070 tree expr;
4071 tree overload = NULL_TREE;
4073 if (processing_template_decl)
4075 if (type_dependent_expression_p (arg1)
4076 || type_dependent_expression_p (arg2))
4077 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4078 NULL_TREE, NULL_TREE);
4079 arg1 = build_non_dependent_expr (arg1);
4080 arg2 = build_non_dependent_expr (arg2);
4083 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4084 NULL_TREE, &overload, complain);
4086 if (processing_template_decl && expr != error_mark_node)
4088 if (overload != NULL_TREE)
4089 return (build_min_non_dep_op_overload
4090 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4092 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4093 NULL_TREE, NULL_TREE);
4095 return expr;
4098 /* Return whether OP is an expression of enum type cast to integer
4099 type. In C++ even unsigned enum types are cast to signed integer
4100 types. We do not want to issue warnings about comparisons between
4101 signed and unsigned types when one of the types is an enum type.
4102 Those warnings are always false positives in practice. */
4104 static bool
4105 enum_cast_to_int (tree op)
4107 if (CONVERT_EXPR_P (op)
4108 && TREE_TYPE (op) == integer_type_node
4109 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4110 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4111 return true;
4113 /* The cast may have been pushed into a COND_EXPR. */
4114 if (TREE_CODE (op) == COND_EXPR)
4115 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4116 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4118 return false;
4121 /* For the c-common bits. */
4122 tree
4123 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4124 bool /*convert_p*/)
4126 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4129 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4130 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4132 static tree
4133 build_vec_cmp (tree_code code, tree type,
4134 tree arg0, tree arg1)
4136 tree zero_vec = build_zero_cst (type);
4137 tree minus_one_vec = build_minus_one_cst (type);
4138 tree cmp_type = build_same_sized_truth_vector_type(type);
4139 tree cmp = build2 (code, cmp_type, arg0, arg1);
4140 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4143 /* Possibly warn about an address never being NULL. */
4145 static void
4146 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4148 if (!warn_address
4149 || (complain & tf_warning) == 0
4150 || c_inhibit_evaluation_warnings != 0
4151 || TREE_NO_WARNING (op))
4152 return;
4154 tree cop = fold_non_dependent_expr (op);
4156 if (TREE_CODE (cop) == ADDR_EXPR
4157 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4158 && !TREE_NO_WARNING (cop))
4159 warning_at (location, OPT_Waddress, "the address of %qD will never "
4160 "be NULL", TREE_OPERAND (cop, 0));
4162 if (CONVERT_EXPR_P (op)
4163 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4165 tree inner_op = op;
4166 STRIP_NOPS (inner_op);
4168 if (DECL_P (inner_op))
4169 warning_at (location, OPT_Waddress,
4170 "the compiler can assume that the address of "
4171 "%qD will never be NULL", inner_op);
4175 /* Build a binary-operation expression without default conversions.
4176 CODE is the kind of expression to build.
4177 LOCATION is the location_t of the operator in the source code.
4178 This function differs from `build' in several ways:
4179 the data type of the result is computed and recorded in it,
4180 warnings are generated if arg data types are invalid,
4181 special handling for addition and subtraction of pointers is known,
4182 and some optimization is done (operations on narrow ints
4183 are done in the narrower type when that gives the same result).
4184 Constant folding is also done before the result is returned.
4186 Note that the operands will never have enumeral types
4187 because either they have just had the default conversions performed
4188 or they have both just been converted to some other type in which
4189 the arithmetic is to be done.
4191 C++: must do special pointer arithmetic when implementing
4192 multiple inheritance, and deal with pointer to member functions. */
4194 tree
4195 cp_build_binary_op (location_t location,
4196 enum tree_code code, tree orig_op0, tree orig_op1,
4197 tsubst_flags_t complain)
4199 tree op0, op1;
4200 enum tree_code code0, code1;
4201 tree type0, type1;
4202 const char *invalid_op_diag;
4204 /* Expression code to give to the expression when it is built.
4205 Normally this is CODE, which is what the caller asked for,
4206 but in some special cases we change it. */
4207 enum tree_code resultcode = code;
4209 /* Data type in which the computation is to be performed.
4210 In the simplest cases this is the common type of the arguments. */
4211 tree result_type = NULL_TREE;
4213 /* Nonzero means operands have already been type-converted
4214 in whatever way is necessary.
4215 Zero means they need to be converted to RESULT_TYPE. */
4216 int converted = 0;
4218 /* Nonzero means create the expression with this type, rather than
4219 RESULT_TYPE. */
4220 tree build_type = 0;
4222 /* Nonzero means after finally constructing the expression
4223 convert it to this type. */
4224 tree final_type = 0;
4226 tree result, result_ovl;
4228 /* Nonzero if this is an operation like MIN or MAX which can
4229 safely be computed in short if both args are promoted shorts.
4230 Also implies COMMON.
4231 -1 indicates a bitwise operation; this makes a difference
4232 in the exact conditions for when it is safe to do the operation
4233 in a narrower mode. */
4234 int shorten = 0;
4236 /* Nonzero if this is a comparison operation;
4237 if both args are promoted shorts, compare the original shorts.
4238 Also implies COMMON. */
4239 int short_compare = 0;
4241 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4242 int common = 0;
4244 /* True if both operands have arithmetic type. */
4245 bool arithmetic_types_p;
4247 /* Apply default conversions. */
4248 op0 = orig_op0;
4249 op1 = orig_op1;
4251 /* Remember whether we're doing / or %. */
4252 bool doing_div_or_mod = false;
4254 /* Remember whether we're doing << or >>. */
4255 bool doing_shift = false;
4257 /* Tree holding instrumentation expression. */
4258 tree instrument_expr = NULL_TREE;
4260 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4261 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4262 || code == TRUTH_XOR_EXPR)
4264 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4265 op0 = decay_conversion (op0, complain);
4266 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4267 op1 = decay_conversion (op1, complain);
4269 else
4271 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4272 op0 = cp_default_conversion (op0, complain);
4273 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4274 op1 = cp_default_conversion (op1, complain);
4277 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4278 STRIP_TYPE_NOPS (op0);
4279 STRIP_TYPE_NOPS (op1);
4281 /* DTRT if one side is an overloaded function, but complain about it. */
4282 if (type_unknown_p (op0))
4284 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4285 if (t != error_mark_node)
4287 if (complain & tf_error)
4288 permerror (input_location, "assuming cast to type %qT from overloaded function",
4289 TREE_TYPE (t));
4290 op0 = t;
4293 if (type_unknown_p (op1))
4295 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4296 if (t != error_mark_node)
4298 if (complain & tf_error)
4299 permerror (input_location, "assuming cast to type %qT from overloaded function",
4300 TREE_TYPE (t));
4301 op1 = t;
4305 type0 = TREE_TYPE (op0);
4306 type1 = TREE_TYPE (op1);
4308 /* The expression codes of the data types of the arguments tell us
4309 whether the arguments are integers, floating, pointers, etc. */
4310 code0 = TREE_CODE (type0);
4311 code1 = TREE_CODE (type1);
4313 /* If an error was already reported for one of the arguments,
4314 avoid reporting another error. */
4315 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4316 return error_mark_node;
4318 if ((invalid_op_diag
4319 = targetm.invalid_binary_op (code, type0, type1)))
4321 if (complain & tf_error)
4322 error (invalid_op_diag);
4323 return error_mark_node;
4326 /* Issue warnings about peculiar, but valid, uses of NULL. */
4327 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4328 /* It's reasonable to use pointer values as operands of &&
4329 and ||, so NULL is no exception. */
4330 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4331 && ( /* Both are NULL (or 0) and the operation was not a
4332 comparison or a pointer subtraction. */
4333 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4334 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4335 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4336 || (!null_ptr_cst_p (orig_op0)
4337 && !TYPE_PTR_OR_PTRMEM_P (type0))
4338 || (!null_ptr_cst_p (orig_op1)
4339 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4340 && (complain & tf_warning))
4342 source_location loc =
4343 expansion_point_location_if_in_system_header (input_location);
4345 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4348 /* In case when one of the operands of the binary operation is
4349 a vector and another is a scalar -- convert scalar to vector. */
4350 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4352 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4353 complain & tf_error);
4355 switch (convert_flag)
4357 case stv_error:
4358 return error_mark_node;
4359 case stv_firstarg:
4361 op0 = convert (TREE_TYPE (type1), op0);
4362 op0 = save_expr (op0);
4363 op0 = build_vector_from_val (type1, op0);
4364 type0 = TREE_TYPE (op0);
4365 code0 = TREE_CODE (type0);
4366 converted = 1;
4367 break;
4369 case stv_secondarg:
4371 op1 = convert (TREE_TYPE (type0), op1);
4372 op1 = save_expr (op1);
4373 op1 = build_vector_from_val (type0, op1);
4374 type1 = TREE_TYPE (op1);
4375 code1 = TREE_CODE (type1);
4376 converted = 1;
4377 break;
4379 default:
4380 break;
4384 switch (code)
4386 case MINUS_EXPR:
4387 /* Subtraction of two similar pointers.
4388 We must subtract them as integers, then divide by object size. */
4389 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4390 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4391 TREE_TYPE (type1)))
4393 result = pointer_diff (location, op0, op1,
4394 common_pointer_type (type0, type1), complain,
4395 &instrument_expr);
4396 if (instrument_expr != NULL)
4397 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4398 instrument_expr, result);
4400 return result;
4402 /* In all other cases except pointer - int, the usual arithmetic
4403 rules apply. */
4404 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4406 common = 1;
4407 break;
4409 /* The pointer - int case is just like pointer + int; fall
4410 through. */
4411 gcc_fallthrough ();
4412 case PLUS_EXPR:
4413 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4414 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4416 tree ptr_operand;
4417 tree int_operand;
4418 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4419 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4420 if (processing_template_decl)
4422 result_type = TREE_TYPE (ptr_operand);
4423 break;
4425 return cp_pointer_int_sum (location, code,
4426 ptr_operand,
4427 int_operand,
4428 complain);
4430 common = 1;
4431 break;
4433 case MULT_EXPR:
4434 common = 1;
4435 break;
4437 case TRUNC_DIV_EXPR:
4438 case CEIL_DIV_EXPR:
4439 case FLOOR_DIV_EXPR:
4440 case ROUND_DIV_EXPR:
4441 case EXACT_DIV_EXPR:
4442 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4444 tree type0 = TREE_OPERAND (op0, 0);
4445 tree type1 = TREE_OPERAND (op1, 0);
4446 tree first_arg = type0;
4447 if (!TYPE_P (type0))
4448 type0 = TREE_TYPE (type0);
4449 if (!TYPE_P (type1))
4450 type1 = TREE_TYPE (type1);
4451 if (POINTER_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4452 && !(TREE_CODE (first_arg) == PARM_DECL
4453 && DECL_ARRAY_PARAMETER_P (first_arg)
4454 && warn_sizeof_array_argument)
4455 && (complain & tf_warning))
4456 if (warning_at (location, OPT_Wsizeof_pointer_div,
4457 "division %<sizeof (%T) / sizeof (%T)%> does "
4458 "not compute the number of array elements",
4459 type0, type1))
4460 if (DECL_P (first_arg))
4461 inform (DECL_SOURCE_LOCATION (first_arg),
4462 "first %<sizeof%> operand was declared here");
4465 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4466 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4467 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4468 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4470 enum tree_code tcode0 = code0, tcode1 = code1;
4471 tree cop1 = fold_non_dependent_expr (op1);
4472 doing_div_or_mod = true;
4473 warn_for_div_by_zero (location, cop1);
4475 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4476 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4477 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4478 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4480 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4481 resultcode = RDIV_EXPR;
4482 else
4483 /* When dividing two signed integers, we have to promote to int.
4484 unless we divide by a constant != -1. Note that default
4485 conversion will have been performed on the operands at this
4486 point, so we have to dig out the original type to find out if
4487 it was unsigned. */
4488 shorten = ((TREE_CODE (op0) == NOP_EXPR
4489 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4490 || (TREE_CODE (op1) == INTEGER_CST
4491 && ! integer_all_onesp (op1)));
4493 common = 1;
4495 break;
4497 case BIT_AND_EXPR:
4498 case BIT_IOR_EXPR:
4499 case BIT_XOR_EXPR:
4500 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4501 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4502 && !VECTOR_FLOAT_TYPE_P (type0)
4503 && !VECTOR_FLOAT_TYPE_P (type1)))
4504 shorten = -1;
4505 break;
4507 case TRUNC_MOD_EXPR:
4508 case FLOOR_MOD_EXPR:
4510 tree cop1 = fold_non_dependent_expr (op1);
4511 doing_div_or_mod = true;
4512 warn_for_div_by_zero (location, cop1);
4515 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4516 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4517 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4518 common = 1;
4519 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4521 /* Although it would be tempting to shorten always here, that loses
4522 on some targets, since the modulo instruction is undefined if the
4523 quotient can't be represented in the computation mode. We shorten
4524 only if unsigned or if dividing by something we know != -1. */
4525 shorten = ((TREE_CODE (op0) == NOP_EXPR
4526 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4527 || (TREE_CODE (op1) == INTEGER_CST
4528 && ! integer_all_onesp (op1)));
4529 common = 1;
4531 break;
4533 case TRUTH_ANDIF_EXPR:
4534 case TRUTH_ORIF_EXPR:
4535 case TRUTH_AND_EXPR:
4536 case TRUTH_OR_EXPR:
4537 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4539 if (!COMPARISON_CLASS_P (op1))
4540 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4541 build_zero_cst (type1), complain);
4542 if (code == TRUTH_ANDIF_EXPR)
4544 tree z = build_zero_cst (TREE_TYPE (op1));
4545 return build_conditional_expr (location, op0, op1, z, complain);
4547 else if (code == TRUTH_ORIF_EXPR)
4549 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4550 return build_conditional_expr (location, op0, m1, op1, complain);
4552 else
4553 gcc_unreachable ();
4555 if (VECTOR_TYPE_P (type0))
4557 if (!COMPARISON_CLASS_P (op0))
4558 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4559 build_zero_cst (type0), complain);
4560 if (!VECTOR_TYPE_P (type1))
4562 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4563 tree z = build_zero_cst (TREE_TYPE (op0));
4564 op1 = build_conditional_expr (location, op1, m1, z, complain);
4566 else if (!COMPARISON_CLASS_P (op1))
4567 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4568 build_zero_cst (type1), complain);
4570 if (code == TRUTH_ANDIF_EXPR)
4571 code = BIT_AND_EXPR;
4572 else if (code == TRUTH_ORIF_EXPR)
4573 code = BIT_IOR_EXPR;
4574 else
4575 gcc_unreachable ();
4577 return cp_build_binary_op (location, code, op0, op1, complain);
4580 result_type = boolean_type_node;
4581 break;
4583 /* Shift operations: result has same type as first operand;
4584 always convert second operand to int.
4585 Also set SHORT_SHIFT if shifting rightward. */
4587 case RSHIFT_EXPR:
4588 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4589 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4591 result_type = type0;
4592 converted = 1;
4594 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4595 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4596 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4597 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4598 TYPE_VECTOR_SUBPARTS (type1)))
4600 result_type = type0;
4601 converted = 1;
4603 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4605 tree const_op1 = fold_non_dependent_expr (op1);
4606 if (TREE_CODE (const_op1) != INTEGER_CST)
4607 const_op1 = op1;
4608 result_type = type0;
4609 doing_shift = true;
4610 if (TREE_CODE (const_op1) == INTEGER_CST)
4612 if (tree_int_cst_lt (const_op1, integer_zero_node))
4614 if ((complain & tf_warning)
4615 && c_inhibit_evaluation_warnings == 0)
4616 warning (OPT_Wshift_count_negative,
4617 "right shift count is negative");
4619 else
4621 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4622 && (complain & tf_warning)
4623 && c_inhibit_evaluation_warnings == 0)
4624 warning (OPT_Wshift_count_overflow,
4625 "right shift count >= width of type");
4628 /* Avoid converting op1 to result_type later. */
4629 converted = 1;
4631 break;
4633 case LSHIFT_EXPR:
4634 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4635 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4637 result_type = type0;
4638 converted = 1;
4640 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4641 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4642 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4643 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4644 TYPE_VECTOR_SUBPARTS (type1)))
4646 result_type = type0;
4647 converted = 1;
4649 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4651 tree const_op0 = fold_non_dependent_expr (op0);
4652 if (TREE_CODE (const_op0) != INTEGER_CST)
4653 const_op0 = op0;
4654 tree const_op1 = fold_non_dependent_expr (op1);
4655 if (TREE_CODE (const_op1) != INTEGER_CST)
4656 const_op1 = op1;
4657 result_type = type0;
4658 doing_shift = true;
4659 if (TREE_CODE (const_op0) == INTEGER_CST
4660 && tree_int_cst_sgn (const_op0) < 0
4661 && (complain & tf_warning)
4662 && c_inhibit_evaluation_warnings == 0)
4663 warning (OPT_Wshift_negative_value,
4664 "left shift of negative value");
4665 if (TREE_CODE (const_op1) == INTEGER_CST)
4667 if (tree_int_cst_lt (const_op1, integer_zero_node))
4669 if ((complain & tf_warning)
4670 && c_inhibit_evaluation_warnings == 0)
4671 warning (OPT_Wshift_count_negative,
4672 "left shift count is negative");
4674 else if (compare_tree_int (const_op1,
4675 TYPE_PRECISION (type0)) >= 0)
4677 if ((complain & tf_warning)
4678 && c_inhibit_evaluation_warnings == 0)
4679 warning (OPT_Wshift_count_overflow,
4680 "left shift count >= width of type");
4682 else if (TREE_CODE (const_op0) == INTEGER_CST
4683 && (complain & tf_warning))
4684 maybe_warn_shift_overflow (location, const_op0, const_op1);
4686 /* Avoid converting op1 to result_type later. */
4687 converted = 1;
4689 break;
4691 case RROTATE_EXPR:
4692 case LROTATE_EXPR:
4693 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4695 result_type = type0;
4696 if (TREE_CODE (op1) == INTEGER_CST)
4698 if (tree_int_cst_lt (op1, integer_zero_node))
4700 if (complain & tf_warning)
4701 warning (0, (code == LROTATE_EXPR)
4702 ? G_("left rotate count is negative")
4703 : G_("right rotate count is negative"));
4705 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4707 if (complain & tf_warning)
4708 warning (0, (code == LROTATE_EXPR)
4709 ? G_("left rotate count >= width of type")
4710 : G_("right rotate count >= width of type"));
4713 /* Convert the shift-count to an integer, regardless of
4714 size of value being shifted. */
4715 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4716 op1 = cp_convert (integer_type_node, op1, complain);
4718 break;
4720 case EQ_EXPR:
4721 case NE_EXPR:
4722 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4723 goto vector_compare;
4724 if ((complain & tf_warning)
4725 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4726 warning (OPT_Wfloat_equal,
4727 "comparing floating point with == or != is unsafe");
4728 if ((complain & tf_warning)
4729 && ((TREE_CODE (orig_op0) == STRING_CST
4730 && !integer_zerop (cp_fully_fold (op1)))
4731 || (TREE_CODE (orig_op1) == STRING_CST
4732 && !integer_zerop (cp_fully_fold (op0)))))
4733 warning (OPT_Waddress, "comparison with string literal results "
4734 "in unspecified behavior");
4736 build_type = boolean_type_node;
4737 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4738 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4739 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4740 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4741 short_compare = 1;
4742 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4743 && null_ptr_cst_p (orig_op1))
4744 /* Handle, eg, (void*)0 (c++/43906), and more. */
4745 || (code0 == POINTER_TYPE
4746 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4748 if (TYPE_PTR_P (type1))
4749 result_type = composite_pointer_type (type0, type1, op0, op1,
4750 CPO_COMPARISON, complain);
4751 else
4752 result_type = type0;
4754 if (char_type_p (TREE_TYPE (orig_op1))
4755 && warning (OPT_Wpointer_compare,
4756 "comparison between pointer and zero character "
4757 "constant"))
4758 inform (input_location,
4759 "did you mean to dereference the pointer?");
4760 warn_for_null_address (location, op0, complain);
4762 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4763 && null_ptr_cst_p (orig_op0))
4764 /* Handle, eg, (void*)0 (c++/43906), and more. */
4765 || (code1 == POINTER_TYPE
4766 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4768 if (TYPE_PTR_P (type0))
4769 result_type = composite_pointer_type (type0, type1, op0, op1,
4770 CPO_COMPARISON, complain);
4771 else
4772 result_type = type1;
4774 if (char_type_p (TREE_TYPE (orig_op0))
4775 && warning (OPT_Wpointer_compare,
4776 "comparison between pointer and zero character "
4777 "constant"))
4778 inform (input_location,
4779 "did you mean to dereference the pointer?");
4780 warn_for_null_address (location, op1, complain);
4782 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4783 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4784 result_type = composite_pointer_type (type0, type1, op0, op1,
4785 CPO_COMPARISON, complain);
4786 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4787 /* One of the operands must be of nullptr_t type. */
4788 result_type = TREE_TYPE (nullptr_node);
4789 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4791 result_type = type0;
4792 if (complain & tf_error)
4793 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4794 else
4795 return error_mark_node;
4797 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4799 result_type = type1;
4800 if (complain & tf_error)
4801 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4802 else
4803 return error_mark_node;
4805 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4807 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4808 == ptrmemfunc_vbit_in_delta)
4810 tree pfn0, delta0, e1, e2;
4812 if (TREE_SIDE_EFFECTS (op0))
4813 op0 = save_expr (op0);
4815 pfn0 = pfn_from_ptrmemfunc (op0);
4816 delta0 = delta_from_ptrmemfunc (op0);
4817 e1 = cp_build_binary_op (location,
4818 EQ_EXPR,
4819 pfn0,
4820 build_zero_cst (TREE_TYPE (pfn0)),
4821 complain);
4822 e2 = cp_build_binary_op (location,
4823 BIT_AND_EXPR,
4824 delta0,
4825 integer_one_node,
4826 complain);
4828 if (complain & tf_warning)
4829 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4831 e2 = cp_build_binary_op (location,
4832 EQ_EXPR, e2, integer_zero_node,
4833 complain);
4834 op0 = cp_build_binary_op (location,
4835 TRUTH_ANDIF_EXPR, e1, e2,
4836 complain);
4837 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4839 else
4841 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4842 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4844 result_type = TREE_TYPE (op0);
4846 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4847 return cp_build_binary_op (location, code, op1, op0, complain);
4848 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4850 tree type;
4851 /* E will be the final comparison. */
4852 tree e;
4853 /* E1 and E2 are for scratch. */
4854 tree e1;
4855 tree e2;
4856 tree pfn0;
4857 tree pfn1;
4858 tree delta0;
4859 tree delta1;
4861 type = composite_pointer_type (type0, type1, op0, op1,
4862 CPO_COMPARISON, complain);
4864 if (!same_type_p (TREE_TYPE (op0), type))
4865 op0 = cp_convert_and_check (type, op0, complain);
4866 if (!same_type_p (TREE_TYPE (op1), type))
4867 op1 = cp_convert_and_check (type, op1, complain);
4869 if (op0 == error_mark_node || op1 == error_mark_node)
4870 return error_mark_node;
4872 if (TREE_SIDE_EFFECTS (op0))
4873 op0 = save_expr (op0);
4874 if (TREE_SIDE_EFFECTS (op1))
4875 op1 = save_expr (op1);
4877 pfn0 = pfn_from_ptrmemfunc (op0);
4878 pfn0 = cp_fully_fold (pfn0);
4879 /* Avoid -Waddress warnings (c++/64877). */
4880 if (TREE_CODE (pfn0) == ADDR_EXPR)
4881 TREE_NO_WARNING (pfn0) = 1;
4882 pfn1 = pfn_from_ptrmemfunc (op1);
4883 pfn1 = cp_fully_fold (pfn1);
4884 delta0 = delta_from_ptrmemfunc (op0);
4885 delta1 = delta_from_ptrmemfunc (op1);
4886 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4887 == ptrmemfunc_vbit_in_delta)
4889 /* We generate:
4891 (op0.pfn == op1.pfn
4892 && ((op0.delta == op1.delta)
4893 || (!op0.pfn && op0.delta & 1 == 0
4894 && op1.delta & 1 == 0))
4896 The reason for the `!op0.pfn' bit is that a NULL
4897 pointer-to-member is any member with a zero PFN and
4898 LSB of the DELTA field is 0. */
4900 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4901 delta0,
4902 integer_one_node,
4903 complain);
4904 e1 = cp_build_binary_op (location,
4905 EQ_EXPR, e1, integer_zero_node,
4906 complain);
4907 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4908 delta1,
4909 integer_one_node,
4910 complain);
4911 e2 = cp_build_binary_op (location,
4912 EQ_EXPR, e2, integer_zero_node,
4913 complain);
4914 e1 = cp_build_binary_op (location,
4915 TRUTH_ANDIF_EXPR, e2, e1,
4916 complain);
4917 e2 = cp_build_binary_op (location, EQ_EXPR,
4918 pfn0,
4919 build_zero_cst (TREE_TYPE (pfn0)),
4920 complain);
4921 e2 = cp_build_binary_op (location,
4922 TRUTH_ANDIF_EXPR, e2, e1, complain);
4923 e1 = cp_build_binary_op (location,
4924 EQ_EXPR, delta0, delta1, complain);
4925 e1 = cp_build_binary_op (location,
4926 TRUTH_ORIF_EXPR, e1, e2, complain);
4928 else
4930 /* We generate:
4932 (op0.pfn == op1.pfn
4933 && (!op0.pfn || op0.delta == op1.delta))
4935 The reason for the `!op0.pfn' bit is that a NULL
4936 pointer-to-member is any member with a zero PFN; the
4937 DELTA field is unspecified. */
4939 e1 = cp_build_binary_op (location,
4940 EQ_EXPR, delta0, delta1, complain);
4941 e2 = cp_build_binary_op (location,
4942 EQ_EXPR,
4943 pfn0,
4944 build_zero_cst (TREE_TYPE (pfn0)),
4945 complain);
4946 e1 = cp_build_binary_op (location,
4947 TRUTH_ORIF_EXPR, e1, e2, complain);
4949 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4950 e = cp_build_binary_op (location,
4951 TRUTH_ANDIF_EXPR, e2, e1, complain);
4952 if (code == EQ_EXPR)
4953 return e;
4954 return cp_build_binary_op (location,
4955 EQ_EXPR, e, integer_zero_node, complain);
4957 else
4959 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4960 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4961 type1));
4962 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4963 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4964 type0));
4967 break;
4969 case MAX_EXPR:
4970 case MIN_EXPR:
4971 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4972 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4973 shorten = 1;
4974 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4975 result_type = composite_pointer_type (type0, type1, op0, op1,
4976 CPO_COMPARISON, complain);
4977 break;
4979 case LE_EXPR:
4980 case GE_EXPR:
4981 case LT_EXPR:
4982 case GT_EXPR:
4983 if (TREE_CODE (orig_op0) == STRING_CST
4984 || TREE_CODE (orig_op1) == STRING_CST)
4986 if (complain & tf_warning)
4987 warning (OPT_Waddress, "comparison with string literal results "
4988 "in unspecified behavior");
4991 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4993 vector_compare:
4994 tree intt;
4995 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4996 TREE_TYPE (type1))
4997 && !vector_types_compatible_elements_p (type0, type1))
4999 if (complain & tf_error)
5001 error_at (location, "comparing vectors with different "
5002 "element types");
5003 inform (location, "operand types are %qT and %qT",
5004 type0, type1);
5006 return error_mark_node;
5009 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5010 TYPE_VECTOR_SUBPARTS (type1)))
5012 if (complain & tf_error)
5014 error_at (location, "comparing vectors with different "
5015 "number of elements");
5016 inform (location, "operand types are %qT and %qT",
5017 type0, type1);
5019 return error_mark_node;
5022 /* It's not precisely specified how the usual arithmetic
5023 conversions apply to the vector types. Here, we use
5024 the unsigned type if one of the operands is signed and
5025 the other one is unsigned. */
5026 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5028 if (!TYPE_UNSIGNED (type0))
5029 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5030 else
5031 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5032 warning_at (location, OPT_Wsign_compare, "comparison between "
5033 "types %qT and %qT", type0, type1);
5036 /* Always construct signed integer vector type. */
5037 intt = c_common_type_for_size
5038 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5039 if (!intt)
5041 if (complain & tf_error)
5042 error_at (location, "could not find an integer type "
5043 "of the same size as %qT", TREE_TYPE (type0));
5044 return error_mark_node;
5046 result_type = build_opaque_vector_type (intt,
5047 TYPE_VECTOR_SUBPARTS (type0));
5048 converted = 1;
5049 return build_vec_cmp (resultcode, result_type, op0, op1);
5051 build_type = boolean_type_node;
5052 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5053 || code0 == ENUMERAL_TYPE)
5054 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5055 || code1 == ENUMERAL_TYPE))
5056 short_compare = 1;
5057 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5058 result_type = composite_pointer_type (type0, type1, op0, op1,
5059 CPO_COMPARISON, complain);
5060 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5062 result_type = type0;
5063 if (extra_warnings && (complain & tf_warning))
5064 warning (OPT_Wextra,
5065 "ordered comparison of pointer with integer zero");
5067 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5069 result_type = type1;
5070 if (extra_warnings && (complain & tf_warning))
5071 warning (OPT_Wextra,
5072 "ordered comparison of pointer with integer zero");
5074 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5075 /* One of the operands must be of nullptr_t type. */
5076 result_type = TREE_TYPE (nullptr_node);
5077 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5079 result_type = type0;
5080 if (complain & tf_error)
5081 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5082 else
5083 return error_mark_node;
5085 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5087 result_type = type1;
5088 if (complain & tf_error)
5089 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5090 else
5091 return error_mark_node;
5094 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5095 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5097 op0 = save_expr (op0);
5098 op1 = save_expr (op1);
5100 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5101 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5104 break;
5106 case UNORDERED_EXPR:
5107 case ORDERED_EXPR:
5108 case UNLT_EXPR:
5109 case UNLE_EXPR:
5110 case UNGT_EXPR:
5111 case UNGE_EXPR:
5112 case UNEQ_EXPR:
5113 build_type = integer_type_node;
5114 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5116 if (complain & tf_error)
5117 error ("unordered comparison on non-floating point argument");
5118 return error_mark_node;
5120 common = 1;
5121 break;
5123 default:
5124 break;
5127 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5128 || code0 == ENUMERAL_TYPE)
5129 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5130 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5131 arithmetic_types_p = 1;
5132 else
5134 arithmetic_types_p = 0;
5135 /* Vector arithmetic is only allowed when both sides are vectors. */
5136 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5138 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5139 || !vector_types_compatible_elements_p (type0, type1))
5141 if (complain & tf_error)
5143 /* "location" already embeds the locations of the
5144 operands, so we don't need to add them separately
5145 to richloc. */
5146 rich_location richloc (line_table, location);
5147 binary_op_error (&richloc, code, type0, type1);
5149 return error_mark_node;
5151 arithmetic_types_p = 1;
5154 /* Determine the RESULT_TYPE, if it is not already known. */
5155 if (!result_type
5156 && arithmetic_types_p
5157 && (shorten || common || short_compare))
5159 result_type = cp_common_type (type0, type1);
5160 if (complain & tf_warning)
5161 do_warn_double_promotion (result_type, type0, type1,
5162 "implicit conversion from %qH to %qI "
5163 "to match other operand of binary "
5164 "expression",
5165 location);
5168 if (!result_type)
5170 if (complain & tf_error)
5171 error_at (location,
5172 "invalid operands of types %qT and %qT to binary %qO",
5173 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5174 return error_mark_node;
5177 /* If we're in a template, the only thing we need to know is the
5178 RESULT_TYPE. */
5179 if (processing_template_decl)
5181 /* Since the middle-end checks the type when doing a build2, we
5182 need to build the tree in pieces. This built tree will never
5183 get out of the front-end as we replace it when instantiating
5184 the template. */
5185 tree tmp = build2 (resultcode,
5186 build_type ? build_type : result_type,
5187 NULL_TREE, op1);
5188 TREE_OPERAND (tmp, 0) = op0;
5189 return tmp;
5192 /* Remember the original type; RESULT_TYPE might be changed later on
5193 by shorten_binary_op. */
5194 tree orig_type = result_type;
5196 if (arithmetic_types_p)
5198 bool first_complex = (code0 == COMPLEX_TYPE);
5199 bool second_complex = (code1 == COMPLEX_TYPE);
5200 int none_complex = (!first_complex && !second_complex);
5202 /* Adapted from patch for c/24581. */
5203 if (first_complex != second_complex
5204 && (code == PLUS_EXPR
5205 || code == MINUS_EXPR
5206 || code == MULT_EXPR
5207 || (code == TRUNC_DIV_EXPR && first_complex))
5208 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5209 && flag_signed_zeros)
5211 /* An operation on mixed real/complex operands must be
5212 handled specially, but the language-independent code can
5213 more easily optimize the plain complex arithmetic if
5214 -fno-signed-zeros. */
5215 tree real_type = TREE_TYPE (result_type);
5216 tree real, imag;
5217 if (first_complex)
5219 if (TREE_TYPE (op0) != result_type)
5220 op0 = cp_convert_and_check (result_type, op0, complain);
5221 if (TREE_TYPE (op1) != real_type)
5222 op1 = cp_convert_and_check (real_type, op1, complain);
5224 else
5226 if (TREE_TYPE (op0) != real_type)
5227 op0 = cp_convert_and_check (real_type, op0, complain);
5228 if (TREE_TYPE (op1) != result_type)
5229 op1 = cp_convert_and_check (result_type, op1, complain);
5231 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5232 return error_mark_node;
5233 if (first_complex)
5235 op0 = save_expr (op0);
5236 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5237 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5238 switch (code)
5240 case MULT_EXPR:
5241 case TRUNC_DIV_EXPR:
5242 op1 = save_expr (op1);
5243 imag = build2 (resultcode, real_type, imag, op1);
5244 /* Fall through. */
5245 case PLUS_EXPR:
5246 case MINUS_EXPR:
5247 real = build2 (resultcode, real_type, real, op1);
5248 break;
5249 default:
5250 gcc_unreachable();
5253 else
5255 op1 = save_expr (op1);
5256 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5257 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5258 switch (code)
5260 case MULT_EXPR:
5261 op0 = save_expr (op0);
5262 imag = build2 (resultcode, real_type, op0, imag);
5263 /* Fall through. */
5264 case PLUS_EXPR:
5265 real = build2 (resultcode, real_type, op0, real);
5266 break;
5267 case MINUS_EXPR:
5268 real = build2 (resultcode, real_type, op0, real);
5269 imag = build1 (NEGATE_EXPR, real_type, imag);
5270 break;
5271 default:
5272 gcc_unreachable();
5275 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5276 return result;
5279 /* For certain operations (which identify themselves by shorten != 0)
5280 if both args were extended from the same smaller type,
5281 do the arithmetic in that type and then extend.
5283 shorten !=0 and !=1 indicates a bitwise operation.
5284 For them, this optimization is safe only if
5285 both args are zero-extended or both are sign-extended.
5286 Otherwise, we might change the result.
5287 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5288 but calculated in (unsigned short) it would be (unsigned short)-1. */
5290 if (shorten && none_complex)
5292 final_type = result_type;
5293 result_type = shorten_binary_op (result_type, op0, op1,
5294 shorten == -1);
5297 /* Comparison operations are shortened too but differently.
5298 They identify themselves by setting short_compare = 1. */
5300 if (short_compare)
5302 /* We call shorten_compare only for diagnostic-reason. */
5303 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5304 xresult_type = result_type;
5305 enum tree_code xresultcode = resultcode;
5306 shorten_compare (location, &xop0, &xop1, &xresult_type,
5307 &xresultcode);
5310 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5311 && warn_sign_compare
5312 /* Do not warn until the template is instantiated; we cannot
5313 bound the ranges of the arguments until that point. */
5314 && !processing_template_decl
5315 && (complain & tf_warning)
5316 && c_inhibit_evaluation_warnings == 0
5317 /* Even unsigned enum types promote to signed int. We don't
5318 want to issue -Wsign-compare warnings for this case. */
5319 && !enum_cast_to_int (orig_op0)
5320 && !enum_cast_to_int (orig_op1))
5322 tree oop0 = maybe_constant_value (orig_op0);
5323 tree oop1 = maybe_constant_value (orig_op1);
5325 if (TREE_CODE (oop0) != INTEGER_CST)
5326 oop0 = cp_fully_fold (orig_op0);
5327 if (TREE_CODE (oop1) != INTEGER_CST)
5328 oop1 = cp_fully_fold (orig_op1);
5329 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5330 result_type, resultcode);
5334 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5335 Then the expression will be built.
5336 It will be given type FINAL_TYPE if that is nonzero;
5337 otherwise, it will be given type RESULT_TYPE. */
5338 if (! converted)
5340 if (TREE_TYPE (op0) != result_type)
5341 op0 = cp_convert_and_check (result_type, op0, complain);
5342 if (TREE_TYPE (op1) != result_type)
5343 op1 = cp_convert_and_check (result_type, op1, complain);
5345 if (op0 == error_mark_node || op1 == error_mark_node)
5346 return error_mark_node;
5349 if (build_type == NULL_TREE)
5350 build_type = result_type;
5352 if (sanitize_flags_p ((SANITIZE_SHIFT
5353 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5354 && current_function_decl != NULL_TREE
5355 && !processing_template_decl
5356 && (doing_div_or_mod || doing_shift))
5358 /* OP0 and/or OP1 might have side-effects. */
5359 op0 = cp_save_expr (op0);
5360 op1 = cp_save_expr (op1);
5361 op0 = fold_non_dependent_expr (op0);
5362 op1 = fold_non_dependent_expr (op1);
5363 if (doing_div_or_mod
5364 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5366 /* For diagnostics we want to use the promoted types without
5367 shorten_binary_op. So convert the arguments to the
5368 original result_type. */
5369 tree cop0 = op0;
5370 tree cop1 = op1;
5371 if (TREE_TYPE (cop0) != orig_type)
5372 cop0 = cp_convert (orig_type, op0, complain);
5373 if (TREE_TYPE (cop1) != orig_type)
5374 cop1 = cp_convert (orig_type, op1, complain);
5375 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5377 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5378 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5381 result = build2_loc (location, resultcode, build_type, op0, op1);
5382 if (final_type != 0)
5383 result = cp_convert (final_type, result, complain);
5385 if (instrument_expr != NULL)
5386 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5387 instrument_expr, result);
5389 if (!processing_template_decl)
5391 op0 = cp_fully_fold (op0);
5392 /* Only consider the second argument if the first isn't overflowed. */
5393 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5394 return result;
5395 op1 = cp_fully_fold (op1);
5396 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5397 return result;
5399 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5400 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5401 return result;
5403 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5404 if (TREE_OVERFLOW_P (result_ovl))
5405 overflow_warning (location, result_ovl);
5407 return result;
5410 /* Build a VEC_PERM_EXPR.
5411 This is a simple wrapper for c_build_vec_perm_expr. */
5412 tree
5413 build_x_vec_perm_expr (location_t loc,
5414 tree arg0, tree arg1, tree arg2,
5415 tsubst_flags_t complain)
5417 tree orig_arg0 = arg0;
5418 tree orig_arg1 = arg1;
5419 tree orig_arg2 = arg2;
5420 if (processing_template_decl)
5422 if (type_dependent_expression_p (arg0)
5423 || type_dependent_expression_p (arg1)
5424 || type_dependent_expression_p (arg2))
5425 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5426 arg0 = build_non_dependent_expr (arg0);
5427 if (arg1)
5428 arg1 = build_non_dependent_expr (arg1);
5429 arg2 = build_non_dependent_expr (arg2);
5431 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5432 if (processing_template_decl && exp != error_mark_node)
5433 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5434 orig_arg1, orig_arg2);
5435 return exp;
5438 /* Return a tree for the sum or difference (RESULTCODE says which)
5439 of pointer PTROP and integer INTOP. */
5441 static tree
5442 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5443 tree intop, tsubst_flags_t complain)
5445 tree res_type = TREE_TYPE (ptrop);
5447 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5448 in certain circumstance (when it's valid to do so). So we need
5449 to make sure it's complete. We don't need to check here, if we
5450 can actually complete it at all, as those checks will be done in
5451 pointer_int_sum() anyway. */
5452 complete_type (TREE_TYPE (res_type));
5454 return pointer_int_sum (loc, resultcode, ptrop,
5455 intop, complain & tf_warning_or_error);
5458 /* Return a tree for the difference of pointers OP0 and OP1.
5459 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5460 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5462 static tree
5463 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5464 tsubst_flags_t complain, tree *instrument_expr)
5466 tree result, inttype;
5467 tree restype = ptrdiff_type_node;
5468 tree target_type = TREE_TYPE (ptrtype);
5470 if (!complete_type_or_else (target_type, NULL_TREE))
5471 return error_mark_node;
5473 if (VOID_TYPE_P (target_type))
5475 if (complain & tf_error)
5476 permerror (loc, "ISO C++ forbids using pointer of "
5477 "type %<void *%> in subtraction");
5478 else
5479 return error_mark_node;
5481 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5483 if (complain & tf_error)
5484 permerror (loc, "ISO C++ forbids using pointer to "
5485 "a function in subtraction");
5486 else
5487 return error_mark_node;
5489 if (TREE_CODE (target_type) == METHOD_TYPE)
5491 if (complain & tf_error)
5492 permerror (loc, "ISO C++ forbids using pointer to "
5493 "a method in subtraction");
5494 else
5495 return error_mark_node;
5498 /* Determine integer type result of the subtraction. This will usually
5499 be the same as the result type (ptrdiff_t), but may need to be a wider
5500 type if pointers for the address space are wider than ptrdiff_t. */
5501 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5502 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5503 else
5504 inttype = restype;
5506 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5508 op0 = save_expr (op0);
5509 op1 = save_expr (op1);
5511 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5512 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5515 /* First do the subtraction, then build the divide operator
5516 and only convert at the very end.
5517 Do not do default conversions in case restype is a short type. */
5519 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5520 pointers. If some platform cannot provide that, or has a larger
5521 ptrdiff_type to support differences larger than half the address
5522 space, cast the pointers to some larger integer type and do the
5523 computations in that type. */
5524 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5525 op0 = cp_build_binary_op (loc,
5526 MINUS_EXPR,
5527 cp_convert (inttype, op0, complain),
5528 cp_convert (inttype, op1, complain),
5529 complain);
5530 else
5531 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5533 /* This generates an error if op1 is a pointer to an incomplete type. */
5534 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5536 if (complain & tf_error)
5537 error_at (loc, "invalid use of a pointer to an incomplete type in "
5538 "pointer arithmetic");
5539 else
5540 return error_mark_node;
5543 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5545 if (complain & tf_error)
5546 error_at (loc, "arithmetic on pointer to an empty aggregate");
5547 else
5548 return error_mark_node;
5551 op1 = (TYPE_PTROB_P (ptrtype)
5552 ? size_in_bytes_loc (loc, target_type)
5553 : integer_one_node);
5555 /* Do the division. */
5557 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5558 cp_convert (inttype, op1, complain));
5559 return cp_convert (restype, result, complain);
5562 /* Construct and perhaps optimize a tree representation
5563 for a unary operation. CODE, a tree_code, specifies the operation
5564 and XARG is the operand. */
5566 tree
5567 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5568 tsubst_flags_t complain)
5570 tree orig_expr = xarg;
5571 tree exp;
5572 int ptrmem = 0;
5573 tree overload = NULL_TREE;
5575 if (processing_template_decl)
5577 if (type_dependent_expression_p (xarg))
5578 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5580 xarg = build_non_dependent_expr (xarg);
5583 exp = NULL_TREE;
5585 /* [expr.unary.op] says:
5587 The address of an object of incomplete type can be taken.
5589 (And is just the ordinary address operator, not an overloaded
5590 "operator &".) However, if the type is a template
5591 specialization, we must complete the type at this point so that
5592 an overloaded "operator &" will be available if required. */
5593 if (code == ADDR_EXPR
5594 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5595 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5596 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5597 || (TREE_CODE (xarg) == OFFSET_REF)))
5598 /* Don't look for a function. */;
5599 else
5600 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5601 NULL_TREE, &overload, complain);
5603 if (!exp && code == ADDR_EXPR)
5605 if (is_overloaded_fn (xarg))
5607 tree fn = get_first_fn (xarg);
5608 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5610 if (complain & tf_error)
5611 error (DECL_CONSTRUCTOR_P (fn)
5612 ? G_("taking address of constructor %qD")
5613 : G_("taking address of destructor %qD"),
5614 fn);
5615 return error_mark_node;
5619 /* A pointer to member-function can be formed only by saying
5620 &X::mf. */
5621 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5622 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5624 if (TREE_CODE (xarg) != OFFSET_REF
5625 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5627 if (complain & tf_error)
5629 error ("invalid use of %qE to form a "
5630 "pointer-to-member-function", xarg.get_value ());
5631 if (TREE_CODE (xarg) != OFFSET_REF)
5632 inform (input_location, " a qualified-id is required");
5634 return error_mark_node;
5636 else
5638 if (complain & tf_error)
5639 error ("parentheses around %qE cannot be used to form a"
5640 " pointer-to-member-function",
5641 xarg.get_value ());
5642 else
5643 return error_mark_node;
5644 PTRMEM_OK_P (xarg) = 1;
5648 if (TREE_CODE (xarg) == OFFSET_REF)
5650 ptrmem = PTRMEM_OK_P (xarg);
5652 if (!ptrmem && !flag_ms_extensions
5653 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5655 /* A single non-static member, make sure we don't allow a
5656 pointer-to-member. */
5657 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5658 TREE_OPERAND (xarg, 0),
5659 ovl_make (TREE_OPERAND (xarg, 1)));
5660 PTRMEM_OK_P (xarg) = ptrmem;
5664 exp = cp_build_addr_expr_strict (xarg, complain);
5667 if (processing_template_decl && exp != error_mark_node)
5669 if (overload != NULL_TREE)
5670 return (build_min_non_dep_op_overload
5671 (code, exp, overload, orig_expr, integer_zero_node));
5673 exp = build_min_non_dep (code, exp, orig_expr,
5674 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5676 if (TREE_CODE (exp) == ADDR_EXPR)
5677 PTRMEM_OK_P (exp) = ptrmem;
5678 return exp;
5681 /* Construct and perhaps optimize a tree representation
5682 for __builtin_addressof operation. ARG specifies the operand. */
5684 tree
5685 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5687 tree orig_expr = arg;
5689 if (processing_template_decl)
5691 if (type_dependent_expression_p (arg))
5692 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5694 arg = build_non_dependent_expr (arg);
5697 tree exp = cp_build_addr_expr_strict (arg, complain);
5699 if (processing_template_decl && exp != error_mark_node)
5700 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5701 return exp;
5704 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5705 constants, where a null value is represented by an INTEGER_CST of
5706 -1. */
5708 tree
5709 cp_truthvalue_conversion (tree expr)
5711 tree type = TREE_TYPE (expr);
5712 if (TYPE_PTR_OR_PTRMEM_P (type)
5713 /* Avoid ICE on invalid use of non-static member function. */
5714 || TREE_CODE (expr) == FUNCTION_DECL)
5715 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5716 else
5717 return c_common_truthvalue_conversion (input_location, expr);
5720 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5722 tree
5723 condition_conversion (tree expr)
5725 tree t;
5726 /* Anything that might happen in a template should go through
5727 maybe_convert_cond. */
5728 gcc_assert (!processing_template_decl);
5729 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5730 tf_warning_or_error, LOOKUP_NORMAL);
5731 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5732 return t;
5735 /* Returns the address of T. This function will fold away
5736 ADDR_EXPR of INDIRECT_REF. */
5738 tree
5739 build_address (tree t)
5741 if (error_operand_p (t) || !cxx_mark_addressable (t))
5742 return error_mark_node;
5743 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5744 || processing_template_decl);
5745 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5746 if (TREE_CODE (t) != ADDR_EXPR)
5747 t = rvalue (t);
5748 return t;
5751 /* Return a NOP_EXPR converting EXPR to TYPE. */
5753 tree
5754 build_nop (tree type, tree expr)
5756 if (type == error_mark_node || error_operand_p (expr))
5757 return expr;
5758 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5761 /* Take the address of ARG, whatever that means under C++ semantics.
5762 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5763 and class rvalues as well.
5765 Nothing should call this function directly; instead, callers should use
5766 cp_build_addr_expr or cp_build_addr_expr_strict. */
5768 static tree
5769 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5771 tree argtype;
5772 tree val;
5774 if (!arg || error_operand_p (arg))
5775 return error_mark_node;
5777 arg = mark_lvalue_use (arg);
5778 if (error_operand_p (arg))
5779 return error_mark_node;
5781 argtype = lvalue_type (arg);
5783 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5785 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5786 && !really_overloaded_fn (arg))
5788 /* They're trying to take the address of a unique non-static
5789 member function. This is ill-formed (except in MS-land),
5790 but let's try to DTRT.
5791 Note: We only handle unique functions here because we don't
5792 want to complain if there's a static overload; non-unique
5793 cases will be handled by instantiate_type. But we need to
5794 handle this case here to allow casts on the resulting PMF.
5795 We could defer this in non-MS mode, but it's easier to give
5796 a useful error here. */
5798 /* Inside constant member functions, the `this' pointer
5799 contains an extra const qualifier. TYPE_MAIN_VARIANT
5800 is used here to remove this const from the diagnostics
5801 and the created OFFSET_REF. */
5802 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5803 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5804 if (!mark_used (fn, complain) && !(complain & tf_error))
5805 return error_mark_node;
5807 if (! flag_ms_extensions)
5809 tree name = DECL_NAME (fn);
5810 if (!(complain & tf_error))
5811 return error_mark_node;
5812 else if (current_class_type
5813 && TREE_OPERAND (arg, 0) == current_class_ref)
5814 /* An expression like &memfn. */
5815 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5816 " or parenthesized non-static member function to form"
5817 " a pointer to member function. Say %<&%T::%D%>",
5818 base, name);
5819 else
5820 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5821 " function to form a pointer to member function."
5822 " Say %<&%T::%D%>",
5823 base, name);
5825 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5828 /* Uninstantiated types are all functions. Taking the
5829 address of a function is a no-op, so just return the
5830 argument. */
5831 if (type_unknown_p (arg))
5832 return build1 (ADDR_EXPR, unknown_type_node, arg);
5834 if (TREE_CODE (arg) == OFFSET_REF)
5835 /* We want a pointer to member; bypass all the code for actually taking
5836 the address of something. */
5837 goto offset_ref;
5839 /* Anything not already handled and not a true memory reference
5840 is an error. */
5841 if (TREE_CODE (argtype) != FUNCTION_TYPE
5842 && TREE_CODE (argtype) != METHOD_TYPE)
5844 cp_lvalue_kind kind = lvalue_kind (arg);
5845 if (kind == clk_none)
5847 if (complain & tf_error)
5848 lvalue_error (input_location, lv_addressof);
5849 return error_mark_node;
5851 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5853 if (!(complain & tf_error))
5854 return error_mark_node;
5855 if (kind & clk_class)
5856 /* Make this a permerror because we used to accept it. */
5857 permerror (input_location, "taking address of temporary");
5858 else
5859 error ("taking address of xvalue (rvalue reference)");
5863 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5865 tree type = build_pointer_type (TREE_TYPE (argtype));
5866 arg = build1 (CONVERT_EXPR, type, arg);
5867 return arg;
5869 else if (pedantic && DECL_MAIN_P (arg))
5871 /* ARM $3.4 */
5872 /* Apparently a lot of autoconf scripts for C++ packages do this,
5873 so only complain if -Wpedantic. */
5874 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5875 pedwarn (input_location, OPT_Wpedantic,
5876 "ISO C++ forbids taking address of function %<::main%>");
5877 else if (flag_pedantic_errors)
5878 return error_mark_node;
5881 /* Let &* cancel out to simplify resulting code. */
5882 if (INDIRECT_REF_P (arg))
5884 arg = TREE_OPERAND (arg, 0);
5885 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5887 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5888 arg = build1 (CONVERT_EXPR, type, arg);
5890 else
5891 /* Don't let this be an lvalue. */
5892 arg = rvalue (arg);
5893 return arg;
5896 /* ??? Cope with user tricks that amount to offsetof. */
5897 if (TREE_CODE (argtype) != FUNCTION_TYPE
5898 && TREE_CODE (argtype) != METHOD_TYPE
5899 && argtype != unknown_type_node
5900 && (val = get_base_address (arg))
5901 && COMPLETE_TYPE_P (TREE_TYPE (val))
5902 && INDIRECT_REF_P (val)
5903 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5905 tree type = build_pointer_type (argtype);
5906 return fold_convert (type, fold_offsetof_1 (arg));
5909 /* Handle complex lvalues (when permitted)
5910 by reduction to simpler cases. */
5911 val = unary_complex_lvalue (ADDR_EXPR, arg);
5912 if (val != 0)
5913 return val;
5915 switch (TREE_CODE (arg))
5917 CASE_CONVERT:
5918 case FLOAT_EXPR:
5919 case FIX_TRUNC_EXPR:
5920 /* We should have handled this above in the lvalue_kind check. */
5921 gcc_unreachable ();
5922 break;
5924 case BASELINK:
5925 arg = BASELINK_FUNCTIONS (arg);
5926 /* Fall through. */
5928 case OVERLOAD:
5929 arg = OVL_FIRST (arg);
5930 break;
5932 case OFFSET_REF:
5933 offset_ref:
5934 /* Turn a reference to a non-static data member into a
5935 pointer-to-member. */
5937 tree type;
5938 tree t;
5940 gcc_assert (PTRMEM_OK_P (arg));
5942 t = TREE_OPERAND (arg, 1);
5943 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5945 if (complain & tf_error)
5946 error ("cannot create pointer to reference member %qD", t);
5947 return error_mark_node;
5950 type = build_ptrmem_type (context_for_name_lookup (t),
5951 TREE_TYPE (t));
5952 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5953 return t;
5956 default:
5957 break;
5960 if (argtype != error_mark_node)
5961 argtype = build_pointer_type (argtype);
5963 if (bitfield_p (arg))
5965 if (complain & tf_error)
5966 error ("attempt to take address of bit-field");
5967 return error_mark_node;
5970 /* In a template, we are processing a non-dependent expression
5971 so we can just form an ADDR_EXPR with the correct type. */
5972 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5974 if (TREE_CODE (arg) == FUNCTION_DECL
5975 && !mark_used (arg, complain) && !(complain & tf_error))
5976 return error_mark_node;
5977 val = build_address (arg);
5978 if (TREE_CODE (arg) == OFFSET_REF)
5979 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5981 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5983 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5985 /* We can only get here with a single static member
5986 function. */
5987 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5988 && DECL_STATIC_FUNCTION_P (fn));
5989 if (!mark_used (fn, complain) && !(complain & tf_error))
5990 return error_mark_node;
5991 val = build_address (fn);
5992 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5993 /* Do not lose object's side effects. */
5994 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5995 TREE_OPERAND (arg, 0), val);
5997 else
5999 tree object = TREE_OPERAND (arg, 0);
6000 tree field = TREE_OPERAND (arg, 1);
6001 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6002 (TREE_TYPE (object), decl_type_context (field)));
6003 val = build_address (arg);
6006 if (TYPE_PTR_P (argtype)
6007 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6009 build_ptrmemfunc_type (argtype);
6010 val = build_ptrmemfunc (argtype, val, 0,
6011 /*c_cast_p=*/false,
6012 complain);
6015 return val;
6018 /* Take the address of ARG if it has one, even if it's an rvalue. */
6020 tree
6021 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6023 return cp_build_addr_expr_1 (arg, 0, complain);
6026 /* Take the address of ARG, but only if it's an lvalue. */
6028 static tree
6029 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6031 return cp_build_addr_expr_1 (arg, 1, complain);
6034 /* C++: Must handle pointers to members.
6036 Perhaps type instantiation should be extended to handle conversion
6037 from aggregates to types we don't yet know we want? (Or are those
6038 cases typically errors which should be reported?)
6040 NOCONVERT suppresses the default promotions (such as from short to int). */
6042 tree
6043 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6044 tsubst_flags_t complain)
6046 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6047 tree arg = xarg;
6048 location_t location = EXPR_LOC_OR_LOC (arg, input_location);
6049 tree argtype = 0;
6050 const char *errstring = NULL;
6051 tree val;
6052 const char *invalid_op_diag;
6054 if (!arg || error_operand_p (arg))
6055 return error_mark_node;
6057 if ((invalid_op_diag
6058 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6059 ? CONVERT_EXPR
6060 : code),
6061 TREE_TYPE (xarg))))
6063 if (complain & tf_error)
6064 error (invalid_op_diag);
6065 return error_mark_node;
6068 switch (code)
6070 case UNARY_PLUS_EXPR:
6071 case NEGATE_EXPR:
6073 int flags = WANT_ARITH | WANT_ENUM;
6074 /* Unary plus (but not unary minus) is allowed on pointers. */
6075 if (code == UNARY_PLUS_EXPR)
6076 flags |= WANT_POINTER;
6077 arg = build_expr_type_conversion (flags, arg, true);
6078 if (!arg)
6079 errstring = (code == NEGATE_EXPR
6080 ? _("wrong type argument to unary minus")
6081 : _("wrong type argument to unary plus"));
6082 else
6084 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6085 arg = cp_perform_integral_promotions (arg, complain);
6087 /* Make sure the result is not an lvalue: a unary plus or minus
6088 expression is always a rvalue. */
6089 arg = rvalue (arg);
6092 break;
6094 case BIT_NOT_EXPR:
6095 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6097 code = CONJ_EXPR;
6098 if (!noconvert)
6100 arg = cp_default_conversion (arg, complain);
6101 if (arg == error_mark_node)
6102 return error_mark_node;
6105 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6106 | WANT_VECTOR_OR_COMPLEX,
6107 arg, true)))
6108 errstring = _("wrong type argument to bit-complement");
6109 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6111 /* Warn if the expression has boolean value. */
6112 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6113 && (complain & tf_warning)
6114 && warning_at (location, OPT_Wbool_operation,
6115 "%<~%> on an expression of type bool"))
6116 inform (location, "did you mean to use logical not (%<!%>)?");
6117 arg = cp_perform_integral_promotions (arg, complain);
6119 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6120 arg = mark_rvalue_use (arg);
6121 break;
6123 case ABS_EXPR:
6124 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6125 errstring = _("wrong type argument to abs");
6126 else if (!noconvert)
6128 arg = cp_default_conversion (arg, complain);
6129 if (arg == error_mark_node)
6130 return error_mark_node;
6132 break;
6134 case CONJ_EXPR:
6135 /* Conjugating a real value is a no-op, but allow it anyway. */
6136 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6137 errstring = _("wrong type argument to conjugation");
6138 else if (!noconvert)
6140 arg = cp_default_conversion (arg, complain);
6141 if (arg == error_mark_node)
6142 return error_mark_node;
6144 break;
6146 case TRUTH_NOT_EXPR:
6147 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6148 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6149 build_zero_cst (TREE_TYPE (arg)), complain);
6150 arg = perform_implicit_conversion (boolean_type_node, arg,
6151 complain);
6152 val = invert_truthvalue_loc (input_location, arg);
6153 if (arg != error_mark_node)
6154 return val;
6155 errstring = _("in argument to unary !");
6156 break;
6158 case NOP_EXPR:
6159 break;
6161 case REALPART_EXPR:
6162 case IMAGPART_EXPR:
6163 arg = build_real_imag_expr (input_location, code, arg);
6164 return arg;
6166 case PREINCREMENT_EXPR:
6167 case POSTINCREMENT_EXPR:
6168 case PREDECREMENT_EXPR:
6169 case POSTDECREMENT_EXPR:
6170 /* Handle complex lvalues (when permitted)
6171 by reduction to simpler cases. */
6173 val = unary_complex_lvalue (code, arg);
6174 if (val != 0)
6175 return val;
6177 arg = mark_lvalue_use (arg);
6179 /* Increment or decrement the real part of the value,
6180 and don't change the imaginary part. */
6181 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6183 tree real, imag;
6185 arg = cp_stabilize_reference (arg);
6186 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6187 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6188 real = cp_build_unary_op (code, real, true, complain);
6189 if (real == error_mark_node || imag == error_mark_node)
6190 return error_mark_node;
6191 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6192 real, imag);
6195 /* Report invalid types. */
6197 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6198 arg, true)))
6200 if (code == PREINCREMENT_EXPR)
6201 errstring = _("no pre-increment operator for type");
6202 else if (code == POSTINCREMENT_EXPR)
6203 errstring = _("no post-increment operator for type");
6204 else if (code == PREDECREMENT_EXPR)
6205 errstring = _("no pre-decrement operator for type");
6206 else
6207 errstring = _("no post-decrement operator for type");
6208 break;
6210 else if (arg == error_mark_node)
6211 return error_mark_node;
6213 /* Report something read-only. */
6215 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6216 || TREE_READONLY (arg))
6218 if (complain & tf_error)
6219 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6220 || code == POSTINCREMENT_EXPR)
6221 ? lv_increment : lv_decrement));
6222 else
6223 return error_mark_node;
6227 tree inc;
6228 tree declared_type = unlowered_expr_type (arg);
6230 argtype = TREE_TYPE (arg);
6232 /* ARM $5.2.5 last annotation says this should be forbidden. */
6233 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6235 if (complain & tf_error)
6236 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6237 ? G_("ISO C++ forbids incrementing an enum")
6238 : G_("ISO C++ forbids decrementing an enum"));
6239 else
6240 return error_mark_node;
6243 /* Compute the increment. */
6245 if (TYPE_PTR_P (argtype))
6247 tree type = complete_type (TREE_TYPE (argtype));
6249 if (!COMPLETE_OR_VOID_TYPE_P (type))
6251 if (complain & tf_error)
6252 error (((code == PREINCREMENT_EXPR
6253 || code == POSTINCREMENT_EXPR))
6254 ? G_("cannot increment a pointer to incomplete type %qT")
6255 : G_("cannot decrement a pointer to incomplete type %qT"),
6256 TREE_TYPE (argtype));
6257 else
6258 return error_mark_node;
6260 else if (!TYPE_PTROB_P (argtype))
6262 if (complain & tf_error)
6263 pedwarn (input_location, OPT_Wpointer_arith,
6264 (code == PREINCREMENT_EXPR
6265 || code == POSTINCREMENT_EXPR)
6266 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6267 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6268 argtype);
6269 else
6270 return error_mark_node;
6273 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6275 else
6276 inc = VECTOR_TYPE_P (argtype)
6277 ? build_one_cst (argtype)
6278 : integer_one_node;
6280 inc = cp_convert (argtype, inc, complain);
6282 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6283 need to ask Objective-C to build the increment or decrement
6284 expression for it. */
6285 if (objc_is_property_ref (arg))
6286 return objc_build_incr_expr_for_property_ref (input_location, code,
6287 arg, inc);
6289 /* Complain about anything else that is not a true lvalue. */
6290 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6291 || code == POSTINCREMENT_EXPR)
6292 ? lv_increment : lv_decrement),
6293 complain))
6294 return error_mark_node;
6296 /* Forbid using -- or ++ in C++17 on `bool'. */
6297 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6299 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6301 if (complain & tf_error)
6302 error ("use of an operand of type %qT in %<operator--%> "
6303 "is forbidden", boolean_type_node);
6304 return error_mark_node;
6306 else
6308 if (cxx_dialect >= cxx17)
6310 if (complain & tf_error)
6311 error ("use of an operand of type %qT in "
6312 "%<operator++%> is forbidden in C++17",
6313 boolean_type_node);
6314 return error_mark_node;
6316 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6317 else if (!in_system_header_at (input_location))
6318 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6319 "in %<operator++%> is deprecated",
6320 boolean_type_node);
6322 val = boolean_increment (code, arg);
6324 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6325 /* An rvalue has no cv-qualifiers. */
6326 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6327 else
6328 val = build2 (code, TREE_TYPE (arg), arg, inc);
6330 TREE_SIDE_EFFECTS (val) = 1;
6331 return val;
6334 case ADDR_EXPR:
6335 /* Note that this operation never does default_conversion
6336 regardless of NOCONVERT. */
6337 return cp_build_addr_expr (arg, complain);
6339 default:
6340 break;
6343 if (!errstring)
6345 if (argtype == 0)
6346 argtype = TREE_TYPE (arg);
6347 return build1 (code, argtype, arg);
6350 if (complain & tf_error)
6351 error ("%s", errstring);
6352 return error_mark_node;
6355 /* Hook for the c-common bits that build a unary op. */
6356 tree
6357 build_unary_op (location_t /*location*/,
6358 enum tree_code code, tree xarg, bool noconvert)
6360 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6363 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6364 so that it is a valid lvalue even for GENERIC by replacing
6365 (lhs = rhs) with ((lhs = rhs), lhs)
6366 (--lhs) with ((--lhs), lhs)
6367 (++lhs) with ((++lhs), lhs)
6368 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6369 that it can be evaluated multiple times. */
6371 tree
6372 genericize_compound_lvalue (tree lvalue)
6374 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6375 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6376 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6377 TREE_OPERAND (lvalue, 1));
6378 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6379 lvalue, TREE_OPERAND (lvalue, 0));
6382 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6383 for certain kinds of expressions which are not really lvalues
6384 but which we can accept as lvalues.
6386 If ARG is not a kind of expression we can handle, return
6387 NULL_TREE. */
6389 tree
6390 unary_complex_lvalue (enum tree_code code, tree arg)
6392 /* Inside a template, making these kinds of adjustments is
6393 pointless; we are only concerned with the type of the
6394 expression. */
6395 if (processing_template_decl)
6396 return NULL_TREE;
6398 /* Handle (a, b) used as an "lvalue". */
6399 if (TREE_CODE (arg) == COMPOUND_EXPR)
6401 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6402 tf_warning_or_error);
6403 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6404 TREE_OPERAND (arg, 0), real_result);
6407 /* Handle (a ? b : c) used as an "lvalue". */
6408 if (TREE_CODE (arg) == COND_EXPR
6409 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6410 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6412 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6413 if (TREE_CODE (arg) == MODIFY_EXPR
6414 || TREE_CODE (arg) == PREINCREMENT_EXPR
6415 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6416 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6418 if (code != ADDR_EXPR)
6419 return NULL_TREE;
6421 /* Handle (a = b) used as an "lvalue" for `&'. */
6422 if (TREE_CODE (arg) == MODIFY_EXPR
6423 || TREE_CODE (arg) == INIT_EXPR)
6425 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6426 tf_warning_or_error);
6427 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6428 arg, real_result);
6429 TREE_NO_WARNING (arg) = 1;
6430 return arg;
6433 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6434 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6435 || TREE_CODE (arg) == OFFSET_REF)
6436 return NULL_TREE;
6438 /* We permit compiler to make function calls returning
6439 objects of aggregate type look like lvalues. */
6441 tree targ = arg;
6443 if (TREE_CODE (targ) == SAVE_EXPR)
6444 targ = TREE_OPERAND (targ, 0);
6446 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6448 if (TREE_CODE (arg) == SAVE_EXPR)
6449 targ = arg;
6450 else
6451 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6452 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6455 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6456 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6457 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6460 /* Don't let anything else be handled specially. */
6461 return NULL_TREE;
6464 /* Mark EXP saying that we need to be able to take the
6465 address of it; it should not be allocated in a register.
6466 Value is true if successful. ARRAY_REF_P is true if this
6467 is for ARRAY_REF construction - in that case we don't want
6468 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6469 it is fine to use ARRAY_REFs for vector subscripts on vector
6470 register variables.
6472 C++: we do not allow `current_class_ptr' to be addressable. */
6474 bool
6475 cxx_mark_addressable (tree exp, bool array_ref_p)
6477 tree x = exp;
6479 while (1)
6480 switch (TREE_CODE (x))
6482 case VIEW_CONVERT_EXPR:
6483 if (array_ref_p
6484 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6485 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6486 return true;
6487 /* FALLTHRU */
6488 case ADDR_EXPR:
6489 case COMPONENT_REF:
6490 case ARRAY_REF:
6491 case REALPART_EXPR:
6492 case IMAGPART_EXPR:
6493 x = TREE_OPERAND (x, 0);
6494 break;
6496 case PARM_DECL:
6497 if (x == current_class_ptr)
6499 error ("cannot take the address of %<this%>, which is an rvalue expression");
6500 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6501 return true;
6503 /* Fall through. */
6505 case VAR_DECL:
6506 /* Caller should not be trying to mark initialized
6507 constant fields addressable. */
6508 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6509 || DECL_IN_AGGR_P (x) == 0
6510 || TREE_STATIC (x)
6511 || DECL_EXTERNAL (x));
6512 /* Fall through. */
6514 case RESULT_DECL:
6515 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6516 && !DECL_ARTIFICIAL (x))
6518 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6520 error
6521 ("address of explicit register variable %qD requested", x);
6522 return false;
6524 else if (extra_warnings)
6525 warning
6526 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6528 TREE_ADDRESSABLE (x) = 1;
6529 return true;
6531 case CONST_DECL:
6532 case FUNCTION_DECL:
6533 TREE_ADDRESSABLE (x) = 1;
6534 return true;
6536 case CONSTRUCTOR:
6537 TREE_ADDRESSABLE (x) = 1;
6538 return true;
6540 case TARGET_EXPR:
6541 TREE_ADDRESSABLE (x) = 1;
6542 cxx_mark_addressable (TREE_OPERAND (x, 0));
6543 return true;
6545 default:
6546 return true;
6550 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6552 tree
6553 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6554 tsubst_flags_t complain)
6556 tree orig_ifexp = ifexp;
6557 tree orig_op1 = op1;
6558 tree orig_op2 = op2;
6559 tree expr;
6561 if (processing_template_decl)
6563 /* The standard says that the expression is type-dependent if
6564 IFEXP is type-dependent, even though the eventual type of the
6565 expression doesn't dependent on IFEXP. */
6566 if (type_dependent_expression_p (ifexp)
6567 /* As a GNU extension, the middle operand may be omitted. */
6568 || (op1 && type_dependent_expression_p (op1))
6569 || type_dependent_expression_p (op2))
6570 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6571 ifexp = build_non_dependent_expr (ifexp);
6572 if (op1)
6573 op1 = build_non_dependent_expr (op1);
6574 op2 = build_non_dependent_expr (op2);
6577 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6578 if (processing_template_decl && expr != error_mark_node)
6580 tree min = build_min_non_dep (COND_EXPR, expr,
6581 orig_ifexp, orig_op1, orig_op2);
6582 expr = convert_from_reference (min);
6584 return expr;
6587 /* Given a list of expressions, return a compound expression
6588 that performs them all and returns the value of the last of them. */
6590 tree
6591 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6592 tsubst_flags_t complain)
6594 tree expr = TREE_VALUE (list);
6596 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6597 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6599 if (complain & tf_error)
6600 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6601 "list-initializer for non-class type must not "
6602 "be parenthesized");
6603 else
6604 return error_mark_node;
6607 if (TREE_CHAIN (list))
6609 if (complain & tf_error)
6610 switch (exp)
6612 case ELK_INIT:
6613 permerror (input_location, "expression list treated as compound "
6614 "expression in initializer");
6615 break;
6616 case ELK_MEM_INIT:
6617 permerror (input_location, "expression list treated as compound "
6618 "expression in mem-initializer");
6619 break;
6620 case ELK_FUNC_CAST:
6621 permerror (input_location, "expression list treated as compound "
6622 "expression in functional cast");
6623 break;
6624 default:
6625 gcc_unreachable ();
6627 else
6628 return error_mark_node;
6630 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6631 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6632 expr, TREE_VALUE (list), complain);
6635 return expr;
6638 /* Like build_x_compound_expr_from_list, but using a VEC. */
6640 tree
6641 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6642 tsubst_flags_t complain)
6644 if (vec_safe_is_empty (vec))
6645 return NULL_TREE;
6646 else if (vec->length () == 1)
6647 return (*vec)[0];
6648 else
6650 tree expr;
6651 unsigned int ix;
6652 tree t;
6654 if (msg != NULL)
6656 if (complain & tf_error)
6657 permerror (input_location,
6658 "%s expression list treated as compound expression",
6659 msg);
6660 else
6661 return error_mark_node;
6664 expr = (*vec)[0];
6665 for (ix = 1; vec->iterate (ix, &t); ++ix)
6666 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6667 t, complain);
6669 return expr;
6673 /* Handle overloading of the ',' operator when needed. */
6675 tree
6676 build_x_compound_expr (location_t loc, tree op1, tree op2,
6677 tsubst_flags_t complain)
6679 tree result;
6680 tree orig_op1 = op1;
6681 tree orig_op2 = op2;
6682 tree overload = NULL_TREE;
6684 if (processing_template_decl)
6686 if (type_dependent_expression_p (op1)
6687 || type_dependent_expression_p (op2))
6688 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6689 op1 = build_non_dependent_expr (op1);
6690 op2 = build_non_dependent_expr (op2);
6693 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6694 NULL_TREE, &overload, complain);
6695 if (!result)
6696 result = cp_build_compound_expr (op1, op2, complain);
6698 if (processing_template_decl && result != error_mark_node)
6700 if (overload != NULL_TREE)
6701 return (build_min_non_dep_op_overload
6702 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6704 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6707 return result;
6710 /* Like cp_build_compound_expr, but for the c-common bits. */
6712 tree
6713 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6715 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6718 /* Build a compound expression. */
6720 tree
6721 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6723 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6725 if (lhs == error_mark_node || rhs == error_mark_node)
6726 return error_mark_node;
6728 if (TREE_CODE (rhs) == TARGET_EXPR)
6730 /* If the rhs is a TARGET_EXPR, then build the compound
6731 expression inside the target_expr's initializer. This
6732 helps the compiler to eliminate unnecessary temporaries. */
6733 tree init = TREE_OPERAND (rhs, 1);
6735 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6736 TREE_OPERAND (rhs, 1) = init;
6738 return rhs;
6741 if (type_unknown_p (rhs))
6743 if (complain & tf_error)
6744 error ("no context to resolve type of %qE", rhs);
6745 return error_mark_node;
6748 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6751 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6752 casts away constness. CAST gives the type of cast. Returns true
6753 if the cast is ill-formed, false if it is well-formed.
6755 ??? This function warns for casting away any qualifier not just
6756 const. We would like to specify exactly what qualifiers are casted
6757 away.
6760 static bool
6761 check_for_casting_away_constness (tree src_type, tree dest_type,
6762 enum tree_code cast, tsubst_flags_t complain)
6764 /* C-style casts are allowed to cast away constness. With
6765 WARN_CAST_QUAL, we still want to issue a warning. */
6766 if (cast == CAST_EXPR && !warn_cast_qual)
6767 return false;
6769 if (!casts_away_constness (src_type, dest_type, complain))
6770 return false;
6772 switch (cast)
6774 case CAST_EXPR:
6775 if (complain & tf_warning)
6776 warning (OPT_Wcast_qual,
6777 "cast from type %qT to type %qT casts away qualifiers",
6778 src_type, dest_type);
6779 return false;
6781 case STATIC_CAST_EXPR:
6782 if (complain & tf_error)
6783 error ("static_cast from type %qT to type %qT casts away qualifiers",
6784 src_type, dest_type);
6785 return true;
6787 case REINTERPRET_CAST_EXPR:
6788 if (complain & tf_error)
6789 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6790 src_type, dest_type);
6791 return true;
6793 default:
6794 gcc_unreachable();
6798 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6799 void
6800 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6802 if (warn_useless_cast
6803 && complain & tf_warning)
6805 if ((TREE_CODE (type) == REFERENCE_TYPE
6806 && (TYPE_REF_IS_RVALUE (type)
6807 ? xvalue_p (expr) : lvalue_p (expr))
6808 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6809 || same_type_p (TREE_TYPE (expr), type))
6810 warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6814 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6815 void
6816 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6818 if (warn_ignored_qualifiers
6819 && complain & tf_warning
6820 && !CLASS_TYPE_P (type)
6821 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6823 warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6824 "result type");
6828 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6829 (another pointer-to-member type in the same hierarchy) and return
6830 the converted expression. If ALLOW_INVERSE_P is permitted, a
6831 pointer-to-derived may be converted to pointer-to-base; otherwise,
6832 only the other direction is permitted. If C_CAST_P is true, this
6833 conversion is taking place as part of a C-style cast. */
6835 tree
6836 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6837 bool c_cast_p, tsubst_flags_t complain)
6839 if (same_type_p (type, TREE_TYPE (expr)))
6840 return expr;
6842 if (TYPE_PTRDATAMEM_P (type))
6844 tree delta;
6846 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6847 TYPE_PTRMEM_CLASS_TYPE (type),
6848 allow_inverse_p,
6849 c_cast_p, complain);
6850 if (delta == error_mark_node)
6851 return error_mark_node;
6853 if (!integer_zerop (delta))
6855 tree cond, op1, op2;
6857 if (TREE_CODE (expr) == PTRMEM_CST)
6858 expr = cplus_expand_constant (expr);
6859 cond = cp_build_binary_op (input_location,
6860 EQ_EXPR,
6861 expr,
6862 build_int_cst (TREE_TYPE (expr), -1),
6863 complain);
6864 op1 = build_nop (ptrdiff_type_node, expr);
6865 op2 = cp_build_binary_op (input_location,
6866 PLUS_EXPR, op1, delta,
6867 complain);
6869 expr = fold_build3_loc (input_location,
6870 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6874 return build_nop (type, expr);
6876 else
6877 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6878 allow_inverse_p, c_cast_p, complain);
6881 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6882 this static_cast is being attempted as one of the possible casts
6883 allowed by a C-style cast. (In that case, accessibility of base
6884 classes is not considered, and it is OK to cast away
6885 constness.) Return the result of the cast. *VALID_P is set to
6886 indicate whether or not the cast was valid. */
6888 static tree
6889 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6890 bool *valid_p, tsubst_flags_t complain)
6892 tree intype;
6893 tree result;
6894 cp_lvalue_kind clk;
6896 /* Assume the cast is valid. */
6897 *valid_p = true;
6899 intype = unlowered_expr_type (expr);
6901 /* Save casted types in the function's used types hash table. */
6902 used_types_insert (type);
6904 /* A prvalue of non-class type is cv-unqualified. */
6905 if (!CLASS_TYPE_P (type))
6906 type = cv_unqualified (type);
6908 /* [expr.static.cast]
6910 An lvalue of type "cv1 B", where B is a class type, can be cast
6911 to type "reference to cv2 D", where D is a class derived (clause
6912 _class.derived_) from B, if a valid standard conversion from
6913 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6914 same cv-qualification as, or greater cv-qualification than, cv1,
6915 and B is not a virtual base class of D. */
6916 /* We check this case before checking the validity of "TYPE t =
6917 EXPR;" below because for this case:
6919 struct B {};
6920 struct D : public B { D(const B&); };
6921 extern B& b;
6922 void f() { static_cast<const D&>(b); }
6924 we want to avoid constructing a new D. The standard is not
6925 completely clear about this issue, but our interpretation is
6926 consistent with other compilers. */
6927 if (TREE_CODE (type) == REFERENCE_TYPE
6928 && CLASS_TYPE_P (TREE_TYPE (type))
6929 && CLASS_TYPE_P (intype)
6930 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6931 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6932 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6933 build_pointer_type (TYPE_MAIN_VARIANT
6934 (TREE_TYPE (type))),
6935 complain)
6936 && (c_cast_p
6937 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6939 tree base;
6941 if (processing_template_decl)
6942 return expr;
6944 /* There is a standard conversion from "D*" to "B*" even if "B"
6945 is ambiguous or inaccessible. If this is really a
6946 static_cast, then we check both for inaccessibility and
6947 ambiguity. However, if this is a static_cast being performed
6948 because the user wrote a C-style cast, then accessibility is
6949 not considered. */
6950 base = lookup_base (TREE_TYPE (type), intype,
6951 c_cast_p ? ba_unique : ba_check,
6952 NULL, complain);
6953 expr = build_address (expr);
6955 if (sanitize_flags_p (SANITIZE_VPTR))
6957 tree ubsan_check
6958 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6959 intype, expr);
6960 if (ubsan_check)
6961 expr = ubsan_check;
6964 /* Convert from "B*" to "D*". This function will check that "B"
6965 is not a virtual base of "D". Even if we don't have a guarantee
6966 that expr is NULL, if the static_cast is to a reference type,
6967 it is UB if it would be NULL, so omit the non-NULL check. */
6968 expr = build_base_path (MINUS_EXPR, expr, base,
6969 /*nonnull=*/flag_delete_null_pointer_checks,
6970 complain);
6972 /* Convert the pointer to a reference -- but then remember that
6973 there are no expressions with reference type in C++.
6975 We call rvalue so that there's an actual tree code
6976 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6977 is a variable with the same type, the conversion would get folded
6978 away, leaving just the variable and causing lvalue_kind to give
6979 the wrong answer. */
6980 expr = cp_fold_convert (type, expr);
6982 /* When -fsanitize=null, make sure to diagnose reference binding to
6983 NULL even when the reference is converted to pointer later on. */
6984 if (sanitize_flags_p (SANITIZE_NULL)
6985 && TREE_CODE (expr) == COND_EXPR
6986 && TREE_OPERAND (expr, 2)
6987 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
6988 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
6989 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
6991 return convert_from_reference (rvalue (expr));
6994 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6995 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6996 if (TREE_CODE (type) == REFERENCE_TYPE
6997 && TYPE_REF_IS_RVALUE (type)
6998 && (clk = real_lvalue_p (expr))
6999 && reference_related_p (TREE_TYPE (type), intype)
7000 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
7002 if (processing_template_decl)
7003 return expr;
7004 if (clk == clk_ordinary)
7006 /* Handle the (non-bit-field) lvalue case here by casting to
7007 lvalue reference and then changing it to an rvalue reference.
7008 Casting an xvalue to rvalue reference will be handled by the
7009 main code path. */
7010 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
7011 result = (perform_direct_initialization_if_possible
7012 (lref, expr, c_cast_p, complain));
7013 result = build1 (NON_LVALUE_EXPR, type, result);
7014 return convert_from_reference (result);
7016 else
7017 /* For a bit-field or packed field, bind to a temporary. */
7018 expr = rvalue (expr);
7021 /* Resolve overloaded address here rather than once in
7022 implicit_conversion and again in the inverse code below. */
7023 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7025 expr = instantiate_type (type, expr, complain);
7026 intype = TREE_TYPE (expr);
7029 /* [expr.static.cast]
7031 Any expression can be explicitly converted to type cv void. */
7032 if (VOID_TYPE_P (type))
7033 return convert_to_void (expr, ICV_CAST, complain);
7035 /* [class.abstract]
7036 An abstract class shall not be used ... as the type of an explicit
7037 conversion. */
7038 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7039 return error_mark_node;
7041 /* [expr.static.cast]
7043 An expression e can be explicitly converted to a type T using a
7044 static_cast of the form static_cast<T>(e) if the declaration T
7045 t(e);" is well-formed, for some invented temporary variable
7046 t. */
7047 result = perform_direct_initialization_if_possible (type, expr,
7048 c_cast_p, complain);
7049 if (result)
7051 if (processing_template_decl)
7052 return expr;
7054 result = convert_from_reference (result);
7056 /* [expr.static.cast]
7058 If T is a reference type, the result is an lvalue; otherwise,
7059 the result is an rvalue. */
7060 if (TREE_CODE (type) != REFERENCE_TYPE)
7062 result = rvalue (result);
7064 if (result == expr && SCALAR_TYPE_P (type))
7065 /* Leave some record of the cast. */
7066 result = build_nop (type, expr);
7068 return result;
7071 /* [expr.static.cast]
7073 The inverse of any standard conversion sequence (clause _conv_),
7074 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7075 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7076 (_conv.bool_) conversions, can be performed explicitly using
7077 static_cast subject to the restriction that the explicit
7078 conversion does not cast away constness (_expr.const.cast_), and
7079 the following additional rules for specific cases: */
7080 /* For reference, the conversions not excluded are: integral
7081 promotions, floating point promotion, integral conversions,
7082 floating point conversions, floating-integral conversions,
7083 pointer conversions, and pointer to member conversions. */
7084 /* DR 128
7086 A value of integral _or enumeration_ type can be explicitly
7087 converted to an enumeration type. */
7088 /* The effect of all that is that any conversion between any two
7089 types which are integral, floating, or enumeration types can be
7090 performed. */
7091 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7092 || SCALAR_FLOAT_TYPE_P (type))
7093 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7094 || SCALAR_FLOAT_TYPE_P (intype)))
7096 if (processing_template_decl)
7097 return expr;
7098 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7101 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7102 && CLASS_TYPE_P (TREE_TYPE (type))
7103 && CLASS_TYPE_P (TREE_TYPE (intype))
7104 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7105 (TREE_TYPE (intype))),
7106 build_pointer_type (TYPE_MAIN_VARIANT
7107 (TREE_TYPE (type))),
7108 complain))
7110 tree base;
7112 if (processing_template_decl)
7113 return expr;
7115 if (!c_cast_p
7116 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7117 complain))
7118 return error_mark_node;
7119 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7120 c_cast_p ? ba_unique : ba_check,
7121 NULL, complain);
7122 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7123 complain);
7125 if (sanitize_flags_p (SANITIZE_VPTR))
7127 tree ubsan_check
7128 = cp_ubsan_maybe_instrument_downcast (input_location, type,
7129 intype, expr);
7130 if (ubsan_check)
7131 expr = ubsan_check;
7134 return cp_fold_convert (type, expr);
7137 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7138 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7140 tree c1;
7141 tree c2;
7142 tree t1;
7143 tree t2;
7145 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7146 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7148 if (TYPE_PTRDATAMEM_P (type))
7150 t1 = (build_ptrmem_type
7151 (c1,
7152 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7153 t2 = (build_ptrmem_type
7154 (c2,
7155 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7157 else
7159 t1 = intype;
7160 t2 = type;
7162 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7164 if (!c_cast_p
7165 && check_for_casting_away_constness (intype, type,
7166 STATIC_CAST_EXPR,
7167 complain))
7168 return error_mark_node;
7169 if (processing_template_decl)
7170 return expr;
7171 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7172 c_cast_p, complain);
7176 /* [expr.static.cast]
7178 An rvalue of type "pointer to cv void" can be explicitly
7179 converted to a pointer to object type. A value of type pointer
7180 to object converted to "pointer to cv void" and back to the
7181 original pointer type will have its original value. */
7182 if (TYPE_PTR_P (intype)
7183 && VOID_TYPE_P (TREE_TYPE (intype))
7184 && TYPE_PTROB_P (type))
7186 if (!c_cast_p
7187 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7188 complain))
7189 return error_mark_node;
7190 if (processing_template_decl)
7191 return expr;
7192 return build_nop (type, expr);
7195 *valid_p = false;
7196 return error_mark_node;
7199 /* Return an expression representing static_cast<TYPE>(EXPR). */
7201 tree
7202 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7204 tree expr = oexpr;
7205 tree result;
7206 bool valid_p;
7208 if (type == error_mark_node || expr == error_mark_node)
7209 return error_mark_node;
7211 bool dependent = (dependent_type_p (type)
7212 || type_dependent_expression_p (expr));
7213 if (dependent)
7215 tmpl:
7216 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7217 /* We don't know if it will or will not have side effects. */
7218 TREE_SIDE_EFFECTS (expr) = 1;
7219 return convert_from_reference (expr);
7221 else if (processing_template_decl)
7222 expr = build_non_dependent_expr (expr);
7224 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7225 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7226 if (TREE_CODE (type) != REFERENCE_TYPE
7227 && TREE_CODE (expr) == NOP_EXPR
7228 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7229 expr = TREE_OPERAND (expr, 0);
7231 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7232 complain);
7233 if (valid_p)
7235 if (result != error_mark_node)
7237 maybe_warn_about_useless_cast (type, expr, complain);
7238 maybe_warn_about_cast_ignoring_quals (type, complain);
7240 if (processing_template_decl)
7241 goto tmpl;
7242 return result;
7245 if (complain & tf_error)
7246 error ("invalid static_cast from type %qT to type %qT",
7247 TREE_TYPE (expr), type);
7248 return error_mark_node;
7251 /* EXPR is an expression with member function or pointer-to-member
7252 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7253 not permitted by ISO C++, but we accept it in some modes. If we
7254 are not in one of those modes, issue a diagnostic. Return the
7255 converted expression. */
7257 tree
7258 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7260 tree intype;
7261 tree decl;
7263 intype = TREE_TYPE (expr);
7264 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7265 || TREE_CODE (intype) == METHOD_TYPE);
7267 if (!(complain & tf_warning_or_error))
7268 return error_mark_node;
7270 if (pedantic || warn_pmf2ptr)
7271 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7272 "converting from %qH to %qI", intype, type);
7274 if (TREE_CODE (intype) == METHOD_TYPE)
7275 expr = build_addr_func (expr, complain);
7276 else if (TREE_CODE (expr) == PTRMEM_CST)
7277 expr = build_address (PTRMEM_CST_MEMBER (expr));
7278 else
7280 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7281 decl = build_address (decl);
7282 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7285 if (expr == error_mark_node)
7286 return error_mark_node;
7288 return build_nop (type, expr);
7291 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7292 If C_CAST_P is true, this reinterpret cast is being done as part of
7293 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7294 indicate whether or not reinterpret_cast was valid. */
7296 static tree
7297 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7298 bool *valid_p, tsubst_flags_t complain)
7300 tree intype;
7302 /* Assume the cast is invalid. */
7303 if (valid_p)
7304 *valid_p = true;
7306 if (type == error_mark_node || error_operand_p (expr))
7307 return error_mark_node;
7309 intype = TREE_TYPE (expr);
7311 /* Save casted types in the function's used types hash table. */
7312 used_types_insert (type);
7314 /* A prvalue of non-class type is cv-unqualified. */
7315 if (!CLASS_TYPE_P (type))
7316 type = cv_unqualified (type);
7318 /* [expr.reinterpret.cast]
7319 An lvalue expression of type T1 can be cast to the type
7320 "reference to T2" if an expression of type "pointer to T1" can be
7321 explicitly converted to the type "pointer to T2" using a
7322 reinterpret_cast. */
7323 if (TREE_CODE (type) == REFERENCE_TYPE)
7325 if (! lvalue_p (expr))
7327 if (complain & tf_error)
7328 error ("invalid cast of an rvalue expression of type "
7329 "%qT to type %qT",
7330 intype, type);
7331 return error_mark_node;
7334 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7335 "B" are related class types; the reinterpret_cast does not
7336 adjust the pointer. */
7337 if (TYPE_PTR_P (intype)
7338 && (complain & tf_warning)
7339 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7340 COMPARE_BASE | COMPARE_DERIVED)))
7341 warning (0, "casting %qT to %qT does not dereference pointer",
7342 intype, type);
7344 expr = cp_build_addr_expr (expr, complain);
7346 if (warn_strict_aliasing > 2)
7347 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7349 if (expr != error_mark_node)
7350 expr = build_reinterpret_cast_1
7351 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7352 valid_p, complain);
7353 if (expr != error_mark_node)
7354 /* cp_build_indirect_ref isn't right for rvalue refs. */
7355 expr = convert_from_reference (fold_convert (type, expr));
7356 return expr;
7359 /* As a G++ extension, we consider conversions from member
7360 functions, and pointers to member functions to
7361 pointer-to-function and pointer-to-void types. If
7362 -Wno-pmf-conversions has not been specified,
7363 convert_member_func_to_ptr will issue an error message. */
7364 if ((TYPE_PTRMEMFUNC_P (intype)
7365 || TREE_CODE (intype) == METHOD_TYPE)
7366 && TYPE_PTR_P (type)
7367 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7368 || VOID_TYPE_P (TREE_TYPE (type))))
7369 return convert_member_func_to_ptr (type, expr, complain);
7371 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7372 array-to-pointer, and function-to-pointer conversions are
7373 performed. */
7374 expr = decay_conversion (expr, complain);
7376 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7377 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7378 if (TREE_CODE (expr) == NOP_EXPR
7379 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7380 expr = TREE_OPERAND (expr, 0);
7382 if (error_operand_p (expr))
7383 return error_mark_node;
7385 intype = TREE_TYPE (expr);
7387 /* [expr.reinterpret.cast]
7388 A pointer can be converted to any integral type large enough to
7389 hold it. ... A value of type std::nullptr_t can be converted to
7390 an integral type; the conversion has the same meaning and
7391 validity as a conversion of (void*)0 to the integral type. */
7392 if (CP_INTEGRAL_TYPE_P (type)
7393 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7395 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7397 if (complain & tf_error)
7398 permerror (input_location, "cast from %qH to %qI loses precision",
7399 intype, type);
7400 else
7401 return error_mark_node;
7403 if (NULLPTR_TYPE_P (intype))
7404 return build_int_cst (type, 0);
7406 /* [expr.reinterpret.cast]
7407 A value of integral or enumeration type can be explicitly
7408 converted to a pointer. */
7409 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7410 /* OK */
7412 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7413 || TYPE_PTR_OR_PTRMEM_P (type))
7414 && same_type_p (type, intype))
7415 /* DR 799 */
7416 return rvalue (expr);
7417 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7419 if ((complain & tf_warning)
7420 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7421 TREE_TYPE (intype)))
7422 warning (OPT_Wcast_function_type,
7423 "cast between incompatible function types"
7424 " from %qH to %qI", intype, type);
7425 return build_nop (type, expr);
7427 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7429 if ((complain & tf_warning)
7430 && !cxx_safe_function_type_cast_p
7431 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7432 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7433 warning (OPT_Wcast_function_type,
7434 "cast between incompatible pointer to member types"
7435 " from %qH to %qI", intype, type);
7436 return build_nop (type, expr);
7438 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7439 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7441 if (!c_cast_p
7442 && check_for_casting_away_constness (intype, type,
7443 REINTERPRET_CAST_EXPR,
7444 complain))
7445 return error_mark_node;
7446 /* Warn about possible alignment problems. */
7447 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7448 && (complain & tf_warning)
7449 && !VOID_TYPE_P (type)
7450 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7451 && COMPLETE_TYPE_P (TREE_TYPE (type))
7452 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7453 && min_align_of_type (TREE_TYPE (type))
7454 > min_align_of_type (TREE_TYPE (intype)))
7455 warning (OPT_Wcast_align, "cast from %qH to %qI "
7456 "increases required alignment of target type", intype, type);
7458 if (warn_strict_aliasing <= 2)
7459 /* strict_aliasing_warning STRIP_NOPs its expr. */
7460 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7462 return build_nop (type, expr);
7464 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7465 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7467 if (complain & tf_warning)
7468 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7469 object pointer type or vice versa is conditionally-supported." */
7470 warning (OPT_Wconditionally_supported,
7471 "casting between pointer-to-function and pointer-to-object "
7472 "is conditionally-supported");
7473 return build_nop (type, expr);
7475 else if (VECTOR_TYPE_P (type))
7476 return convert_to_vector (type, expr);
7477 else if (VECTOR_TYPE_P (intype)
7478 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7479 return convert_to_integer_nofold (type, expr);
7480 else
7482 if (valid_p)
7483 *valid_p = false;
7484 if (complain & tf_error)
7485 error ("invalid cast from type %qT to type %qT", intype, type);
7486 return error_mark_node;
7489 return cp_convert (type, expr, complain);
7492 tree
7493 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7495 tree r;
7497 if (type == error_mark_node || expr == error_mark_node)
7498 return error_mark_node;
7500 if (processing_template_decl)
7502 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7504 if (!TREE_SIDE_EFFECTS (t)
7505 && type_dependent_expression_p (expr))
7506 /* There might turn out to be side effects inside expr. */
7507 TREE_SIDE_EFFECTS (t) = 1;
7508 return convert_from_reference (t);
7511 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7512 /*valid_p=*/NULL, complain);
7513 if (r != error_mark_node)
7515 maybe_warn_about_useless_cast (type, expr, complain);
7516 maybe_warn_about_cast_ignoring_quals (type, complain);
7518 return r;
7521 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7522 return an appropriate expression. Otherwise, return
7523 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7524 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7525 performing a C-style cast, its value upon return will indicate
7526 whether or not the conversion succeeded. */
7528 static tree
7529 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7530 bool *valid_p)
7532 tree src_type;
7533 tree reference_type;
7535 /* Callers are responsible for handling error_mark_node as a
7536 destination type. */
7537 gcc_assert (dst_type != error_mark_node);
7538 /* In a template, callers should be building syntactic
7539 representations of casts, not using this machinery. */
7540 gcc_assert (!processing_template_decl);
7542 /* Assume the conversion is invalid. */
7543 if (valid_p)
7544 *valid_p = false;
7546 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7548 if (complain & tf_error)
7549 error ("invalid use of const_cast with type %qT, "
7550 "which is not a pointer, "
7551 "reference, nor a pointer-to-data-member type", dst_type);
7552 return error_mark_node;
7555 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7557 if (complain & tf_error)
7558 error ("invalid use of const_cast with type %qT, which is a pointer "
7559 "or reference to a function type", dst_type);
7560 return error_mark_node;
7563 /* A prvalue of non-class type is cv-unqualified. */
7564 dst_type = cv_unqualified (dst_type);
7566 /* Save casted types in the function's used types hash table. */
7567 used_types_insert (dst_type);
7569 src_type = TREE_TYPE (expr);
7570 /* Expressions do not really have reference types. */
7571 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7572 src_type = TREE_TYPE (src_type);
7574 /* [expr.const.cast]
7576 For two object types T1 and T2, if a pointer to T1 can be explicitly
7577 converted to the type "pointer to T2" using a const_cast, then the
7578 following conversions can also be made:
7580 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7581 type T2 using the cast const_cast<T2&>;
7583 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7584 type T2 using the cast const_cast<T2&&>; and
7586 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7587 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7589 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7591 reference_type = dst_type;
7592 if (!TYPE_REF_IS_RVALUE (dst_type)
7593 ? lvalue_p (expr)
7594 : obvalue_p (expr))
7595 /* OK. */;
7596 else
7598 if (complain & tf_error)
7599 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7600 src_type, dst_type);
7601 return error_mark_node;
7603 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7604 src_type = build_pointer_type (src_type);
7606 else
7608 reference_type = NULL_TREE;
7609 /* If the destination type is not a reference type, the
7610 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7611 conversions are performed. */
7612 src_type = type_decays_to (src_type);
7613 if (src_type == error_mark_node)
7614 return error_mark_node;
7617 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7619 if (comp_ptr_ttypes_const (dst_type, src_type))
7621 if (valid_p)
7623 *valid_p = true;
7624 /* This cast is actually a C-style cast. Issue a warning if
7625 the user is making a potentially unsafe cast. */
7626 check_for_casting_away_constness (src_type, dst_type,
7627 CAST_EXPR, complain);
7628 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7629 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7630 && (complain & tf_warning)
7631 && min_align_of_type (TREE_TYPE (dst_type))
7632 > min_align_of_type (TREE_TYPE (src_type)))
7633 warning (OPT_Wcast_align, "cast from %qH to %qI "
7634 "increases required alignment of target type",
7635 src_type, dst_type);
7637 if (reference_type)
7639 expr = cp_build_addr_expr (expr, complain);
7640 if (expr == error_mark_node)
7641 return error_mark_node;
7642 expr = build_nop (reference_type, expr);
7643 return convert_from_reference (expr);
7645 else
7647 expr = decay_conversion (expr, complain);
7648 if (expr == error_mark_node)
7649 return error_mark_node;
7651 /* build_c_cast puts on a NOP_EXPR to make the result not an
7652 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7653 non-lvalue context. */
7654 if (TREE_CODE (expr) == NOP_EXPR
7655 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7656 expr = TREE_OPERAND (expr, 0);
7657 return build_nop (dst_type, expr);
7660 else if (valid_p
7661 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7662 TREE_TYPE (src_type)))
7663 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7664 complain);
7667 if (complain & tf_error)
7668 error ("invalid const_cast from type %qT to type %qT",
7669 src_type, dst_type);
7670 return error_mark_node;
7673 tree
7674 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7676 tree r;
7678 if (type == error_mark_node || error_operand_p (expr))
7679 return error_mark_node;
7681 if (processing_template_decl)
7683 tree t = build_min (CONST_CAST_EXPR, type, expr);
7685 if (!TREE_SIDE_EFFECTS (t)
7686 && type_dependent_expression_p (expr))
7687 /* There might turn out to be side effects inside expr. */
7688 TREE_SIDE_EFFECTS (t) = 1;
7689 return convert_from_reference (t);
7692 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7693 if (r != error_mark_node)
7695 maybe_warn_about_useless_cast (type, expr, complain);
7696 maybe_warn_about_cast_ignoring_quals (type, complain);
7698 return r;
7701 /* Like cp_build_c_cast, but for the c-common bits. */
7703 tree
7704 build_c_cast (location_t /*loc*/, tree type, tree expr)
7706 return cp_build_c_cast (type, expr, tf_warning_or_error);
7709 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7710 preserve location information even for tree nodes that don't
7711 support it. */
7713 cp_expr
7714 build_c_cast (location_t loc, tree type, cp_expr expr)
7716 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7717 result.set_location (loc);
7718 return result;
7721 /* Build an expression representing an explicit C-style cast to type
7722 TYPE of expression EXPR. */
7724 tree
7725 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7727 tree value = expr;
7728 tree result;
7729 bool valid_p;
7731 if (type == error_mark_node || error_operand_p (expr))
7732 return error_mark_node;
7734 if (processing_template_decl)
7736 tree t = build_min (CAST_EXPR, type,
7737 tree_cons (NULL_TREE, value, NULL_TREE));
7738 /* We don't know if it will or will not have side effects. */
7739 TREE_SIDE_EFFECTS (t) = 1;
7740 return convert_from_reference (t);
7743 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7744 'Class') should always be retained, because this information aids
7745 in method lookup. */
7746 if (objc_is_object_ptr (type)
7747 && objc_is_object_ptr (TREE_TYPE (expr)))
7748 return build_nop (type, expr);
7750 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7751 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7752 if (TREE_CODE (type) != REFERENCE_TYPE
7753 && TREE_CODE (value) == NOP_EXPR
7754 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7755 value = TREE_OPERAND (value, 0);
7757 if (TREE_CODE (type) == ARRAY_TYPE)
7759 /* Allow casting from T1* to T2[] because Cfront allows it.
7760 NIHCL uses it. It is not valid ISO C++ however. */
7761 if (TYPE_PTR_P (TREE_TYPE (expr)))
7763 if (complain & tf_error)
7764 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7765 else
7766 return error_mark_node;
7767 type = build_pointer_type (TREE_TYPE (type));
7769 else
7771 if (complain & tf_error)
7772 error ("ISO C++ forbids casting to an array type %qT", type);
7773 return error_mark_node;
7777 if (TREE_CODE (type) == FUNCTION_TYPE
7778 || TREE_CODE (type) == METHOD_TYPE)
7780 if (complain & tf_error)
7781 error ("invalid cast to function type %qT", type);
7782 return error_mark_node;
7785 if (TYPE_PTR_P (type)
7786 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7787 /* Casting to an integer of smaller size is an error detected elsewhere. */
7788 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7789 /* Don't warn about converting any constant. */
7790 && !TREE_CONSTANT (value))
7791 warning_at (input_location, OPT_Wint_to_pointer_cast,
7792 "cast to pointer from integer of different size");
7794 /* A C-style cast can be a const_cast. */
7795 result = build_const_cast_1 (type, value, complain & tf_warning,
7796 &valid_p);
7797 if (valid_p)
7799 if (result != error_mark_node)
7801 maybe_warn_about_useless_cast (type, value, complain);
7802 maybe_warn_about_cast_ignoring_quals (type, complain);
7804 return result;
7807 /* Or a static cast. */
7808 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7809 &valid_p, complain);
7810 /* Or a reinterpret_cast. */
7811 if (!valid_p)
7812 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7813 &valid_p, complain);
7814 /* The static_cast or reinterpret_cast may be followed by a
7815 const_cast. */
7816 if (valid_p
7817 /* A valid cast may result in errors if, for example, a
7818 conversion to an ambiguous base class is required. */
7819 && !error_operand_p (result))
7821 tree result_type;
7823 maybe_warn_about_useless_cast (type, value, complain);
7824 maybe_warn_about_cast_ignoring_quals (type, complain);
7826 /* Non-class rvalues always have cv-unqualified type. */
7827 if (!CLASS_TYPE_P (type))
7828 type = TYPE_MAIN_VARIANT (type);
7829 result_type = TREE_TYPE (result);
7830 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7831 result_type = TYPE_MAIN_VARIANT (result_type);
7832 /* If the type of RESULT does not match TYPE, perform a
7833 const_cast to make it match. If the static_cast or
7834 reinterpret_cast succeeded, we will differ by at most
7835 cv-qualification, so the follow-on const_cast is guaranteed
7836 to succeed. */
7837 if (!same_type_p (non_reference (type), non_reference (result_type)))
7839 result = build_const_cast_1 (type, result, false, &valid_p);
7840 gcc_assert (valid_p);
7842 return result;
7845 return error_mark_node;
7848 /* For use from the C common bits. */
7849 tree
7850 build_modify_expr (location_t location,
7851 tree lhs, tree /*lhs_origtype*/,
7852 enum tree_code modifycode,
7853 location_t /*rhs_location*/, tree rhs,
7854 tree /*rhs_origtype*/)
7856 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7857 tf_warning_or_error);
7860 /* Build an assignment expression of lvalue LHS from value RHS.
7861 MODIFYCODE is the code for a binary operator that we use
7862 to combine the old value of LHS with RHS to get the new value.
7863 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7865 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7867 tree
7868 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7869 tree rhs, tsubst_flags_t complain)
7871 lhs = mark_lvalue_use_nonread (lhs);
7873 tree result = NULL_TREE;
7874 tree newrhs = rhs;
7875 tree lhstype = TREE_TYPE (lhs);
7876 tree olhs = lhs;
7877 tree olhstype = lhstype;
7878 bool plain_assign = (modifycode == NOP_EXPR);
7879 bool compound_side_effects_p = false;
7880 tree preeval = NULL_TREE;
7882 /* Avoid duplicate error messages from operands that had errors. */
7883 if (error_operand_p (lhs) || error_operand_p (rhs))
7884 return error_mark_node;
7886 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7888 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7889 compound_side_effects_p = true;
7890 lhs = TREE_OPERAND (lhs, 1);
7893 /* Handle control structure constructs used as "lvalues". Note that we
7894 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7895 switch (TREE_CODE (lhs))
7897 /* Handle --foo = 5; as these are valid constructs in C++. */
7898 case PREDECREMENT_EXPR:
7899 case PREINCREMENT_EXPR:
7900 if (compound_side_effects_p)
7901 newrhs = rhs = stabilize_expr (rhs, &preeval);
7902 lhs = genericize_compound_lvalue (lhs);
7903 maybe_add_compound:
7904 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7905 and looked through the COMPOUND_EXPRs, readd them now around
7906 the resulting lhs. */
7907 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7909 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7910 tree *ptr = &TREE_OPERAND (lhs, 1);
7911 for (olhs = TREE_OPERAND (olhs, 1);
7912 TREE_CODE (olhs) == COMPOUND_EXPR;
7913 olhs = TREE_OPERAND (olhs, 1))
7915 *ptr = build2 (COMPOUND_EXPR, lhstype,
7916 TREE_OPERAND (olhs, 0), *ptr);
7917 ptr = &TREE_OPERAND (*ptr, 1);
7920 break;
7922 case MODIFY_EXPR:
7923 if (compound_side_effects_p)
7924 newrhs = rhs = stabilize_expr (rhs, &preeval);
7925 lhs = genericize_compound_lvalue (lhs);
7926 goto maybe_add_compound;
7928 case MIN_EXPR:
7929 case MAX_EXPR:
7930 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7931 when neither operand has side-effects. */
7932 if (!lvalue_or_else (lhs, lv_assign, complain))
7933 return error_mark_node;
7935 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7936 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7938 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7939 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7940 boolean_type_node,
7941 TREE_OPERAND (lhs, 0),
7942 TREE_OPERAND (lhs, 1)),
7943 TREE_OPERAND (lhs, 0),
7944 TREE_OPERAND (lhs, 1));
7945 gcc_fallthrough ();
7947 /* Handle (a ? b : c) used as an "lvalue". */
7948 case COND_EXPR:
7950 /* Produce (a ? (b = rhs) : (c = rhs))
7951 except that the RHS goes through a save-expr
7952 so the code to compute it is only emitted once. */
7953 tree cond;
7955 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7957 if (complain & tf_error)
7958 error ("void value not ignored as it ought to be");
7959 return error_mark_node;
7962 rhs = stabilize_expr (rhs, &preeval);
7964 /* Check this here to avoid odd errors when trying to convert
7965 a throw to the type of the COND_EXPR. */
7966 if (!lvalue_or_else (lhs, lv_assign, complain))
7967 return error_mark_node;
7969 cond = build_conditional_expr
7970 (input_location, TREE_OPERAND (lhs, 0),
7971 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7972 modifycode, rhs, complain),
7973 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7974 modifycode, rhs, complain),
7975 complain);
7977 if (cond == error_mark_node)
7978 return cond;
7979 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7980 and looked through the COMPOUND_EXPRs, readd them now around
7981 the resulting cond before adding the preevaluated rhs. */
7982 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7984 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7985 TREE_OPERAND (olhs, 0), cond);
7986 tree *ptr = &TREE_OPERAND (cond, 1);
7987 for (olhs = TREE_OPERAND (olhs, 1);
7988 TREE_CODE (olhs) == COMPOUND_EXPR;
7989 olhs = TREE_OPERAND (olhs, 1))
7991 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7992 TREE_OPERAND (olhs, 0), *ptr);
7993 ptr = &TREE_OPERAND (*ptr, 1);
7996 /* Make sure the code to compute the rhs comes out
7997 before the split. */
7998 result = cond;
7999 goto ret;
8002 default:
8003 lhs = olhs;
8004 break;
8007 if (modifycode == INIT_EXPR)
8009 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8010 /* Do the default thing. */;
8011 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8013 /* Compound literal. */
8014 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8015 /* Call convert to generate an error; see PR 11063. */
8016 rhs = convert (lhstype, rhs);
8017 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8018 TREE_SIDE_EFFECTS (result) = 1;
8019 goto ret;
8021 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8022 /* Do the default thing. */;
8023 else
8025 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8026 result = build_special_member_call (lhs, complete_ctor_identifier,
8027 &rhs_vec, lhstype, LOOKUP_NORMAL,
8028 complain);
8029 release_tree_vector (rhs_vec);
8030 if (result == NULL_TREE)
8031 return error_mark_node;
8032 goto ret;
8035 else
8037 lhs = require_complete_type_sfinae (lhs, complain);
8038 if (lhs == error_mark_node)
8039 return error_mark_node;
8041 if (modifycode == NOP_EXPR)
8043 if (c_dialect_objc ())
8045 result = objc_maybe_build_modify_expr (lhs, rhs);
8046 if (result)
8047 goto ret;
8050 /* `operator=' is not an inheritable operator. */
8051 if (! MAYBE_CLASS_TYPE_P (lhstype))
8052 /* Do the default thing. */;
8053 else
8055 result = build_new_op (input_location, MODIFY_EXPR,
8056 LOOKUP_NORMAL, lhs, rhs,
8057 make_node (NOP_EXPR), /*overload=*/NULL,
8058 complain);
8059 if (result == NULL_TREE)
8060 return error_mark_node;
8061 goto ret;
8063 lhstype = olhstype;
8065 else
8067 tree init = NULL_TREE;
8069 /* A binary op has been requested. Combine the old LHS
8070 value with the RHS producing the value we should actually
8071 store into the LHS. */
8072 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
8073 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8074 || MAYBE_CLASS_TYPE_P (lhstype)));
8076 /* Preevaluate the RHS to make sure its evaluation is complete
8077 before the lvalue-to-rvalue conversion of the LHS:
8079 [expr.ass] With respect to an indeterminately-sequenced
8080 function call, the operation of a compound assignment is a
8081 single evaluation. [ Note: Therefore, a function call shall
8082 not intervene between the lvalue-to-rvalue conversion and the
8083 side effect associated with any single compound assignment
8084 operator. -- end note ] */
8085 lhs = cp_stabilize_reference (lhs);
8086 rhs = rvalue (rhs);
8087 if (rhs == error_mark_node)
8088 return error_mark_node;
8089 rhs = stabilize_expr (rhs, &init);
8090 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8091 if (newrhs == error_mark_node)
8093 if (complain & tf_error)
8094 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8095 TREE_TYPE (lhs), TREE_TYPE (rhs));
8096 return error_mark_node;
8099 if (init)
8100 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8102 /* Now it looks like a plain assignment. */
8103 modifycode = NOP_EXPR;
8104 if (c_dialect_objc ())
8106 result = objc_maybe_build_modify_expr (lhs, newrhs);
8107 if (result)
8108 goto ret;
8111 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
8112 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
8115 /* The left-hand side must be an lvalue. */
8116 if (!lvalue_or_else (lhs, lv_assign, complain))
8117 return error_mark_node;
8119 /* Warn about modifying something that is `const'. Don't warn if
8120 this is initialization. */
8121 if (modifycode != INIT_EXPR
8122 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8123 /* Functions are not modifiable, even though they are
8124 lvalues. */
8125 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8126 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8127 /* If it's an aggregate and any field is const, then it is
8128 effectively const. */
8129 || (CLASS_TYPE_P (lhstype)
8130 && C_TYPE_FIELDS_READONLY (lhstype))))
8132 if (complain & tf_error)
8133 cxx_readonly_error (lhs, lv_assign);
8134 return error_mark_node;
8137 /* If storing into a structure or union member, it may have been given a
8138 lowered bitfield type. We need to convert to the declared type first,
8139 so retrieve it now. */
8141 olhstype = unlowered_expr_type (lhs);
8143 /* Convert new value to destination type. */
8145 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8147 int from_array;
8149 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8151 if (modifycode != INIT_EXPR)
8153 if (complain & tf_error)
8154 error ("assigning to an array from an initializer list");
8155 return error_mark_node;
8157 if (check_array_initializer (lhs, lhstype, newrhs))
8158 return error_mark_node;
8159 newrhs = digest_init (lhstype, newrhs, complain);
8160 if (newrhs == error_mark_node)
8161 return error_mark_node;
8164 /* C++11 8.5/17: "If the destination type is an array of characters,
8165 an array of char16_t, an array of char32_t, or an array of wchar_t,
8166 and the initializer is a string literal...". */
8167 else if (TREE_CODE (newrhs) == STRING_CST
8168 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8169 && modifycode == INIT_EXPR)
8171 newrhs = digest_init (lhstype, newrhs, complain);
8172 if (newrhs == error_mark_node)
8173 return error_mark_node;
8176 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8177 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8179 if (complain & tf_error)
8180 error ("incompatible types in assignment of %qT to %qT",
8181 TREE_TYPE (rhs), lhstype);
8182 return error_mark_node;
8185 /* Allow array assignment in compiler-generated code. */
8186 else if (!current_function_decl
8187 || !DECL_DEFAULTED_FN (current_function_decl))
8189 /* This routine is used for both initialization and assignment.
8190 Make sure the diagnostic message differentiates the context. */
8191 if (complain & tf_error)
8193 if (modifycode == INIT_EXPR)
8194 error ("array used as initializer");
8195 else
8196 error ("invalid array assignment");
8198 return error_mark_node;
8201 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8202 ? 1 + (modifycode != INIT_EXPR): 0;
8203 result = build_vec_init (lhs, NULL_TREE, newrhs,
8204 /*explicit_value_init_p=*/false,
8205 from_array, complain);
8206 goto ret;
8209 if (modifycode == INIT_EXPR)
8210 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8211 LOOKUP_ONLYCONVERTING. */
8212 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8213 ICR_INIT, NULL_TREE, 0,
8214 complain);
8215 else
8216 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8217 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8219 if (!same_type_p (lhstype, olhstype))
8220 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8222 if (modifycode != INIT_EXPR)
8224 if (TREE_CODE (newrhs) == CALL_EXPR
8225 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8226 newrhs = build_cplus_new (lhstype, newrhs, complain);
8228 /* Can't initialize directly from a TARGET_EXPR, since that would
8229 cause the lhs to be constructed twice, and possibly result in
8230 accidental self-initialization. So we force the TARGET_EXPR to be
8231 expanded without a target. */
8232 if (TREE_CODE (newrhs) == TARGET_EXPR)
8233 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8234 TREE_OPERAND (newrhs, 0));
8237 if (newrhs == error_mark_node)
8238 return error_mark_node;
8240 if (c_dialect_objc () && flag_objc_gc)
8242 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8244 if (result)
8245 goto ret;
8248 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8249 lhstype, lhs, newrhs);
8251 TREE_SIDE_EFFECTS (result) = 1;
8252 if (!plain_assign)
8253 TREE_NO_WARNING (result) = 1;
8255 ret:
8256 if (preeval)
8257 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8258 return result;
8261 cp_expr
8262 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8263 tree rhs, tsubst_flags_t complain)
8265 tree orig_lhs = lhs;
8266 tree orig_rhs = rhs;
8267 tree overload = NULL_TREE;
8268 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8270 if (processing_template_decl)
8272 if (modifycode == NOP_EXPR
8273 || type_dependent_expression_p (lhs)
8274 || type_dependent_expression_p (rhs))
8275 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8276 build_min_nt_loc (loc, modifycode, NULL_TREE,
8277 NULL_TREE), rhs);
8279 lhs = build_non_dependent_expr (lhs);
8280 rhs = build_non_dependent_expr (rhs);
8283 if (modifycode != NOP_EXPR)
8285 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8286 lhs, rhs, op, &overload, complain);
8287 if (rval)
8289 if (rval == error_mark_node)
8290 return rval;
8291 TREE_NO_WARNING (rval) = 1;
8292 if (processing_template_decl)
8294 if (overload != NULL_TREE)
8295 return (build_min_non_dep_op_overload
8296 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8298 return (build_min_non_dep
8299 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8301 return rval;
8304 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8307 /* Helper function for get_delta_difference which assumes FROM is a base
8308 class of TO. Returns a delta for the conversion of pointer-to-member
8309 of FROM to pointer-to-member of TO. If the conversion is invalid and
8310 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8311 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8312 If C_CAST_P is true, this conversion is taking place as part of a
8313 C-style cast. */
8315 static tree
8316 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8317 tsubst_flags_t complain)
8319 tree binfo;
8320 base_kind kind;
8322 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8323 &kind, complain);
8325 if (binfo == error_mark_node)
8327 if (!(complain & tf_error))
8328 return error_mark_node;
8330 error (" in pointer to member function conversion");
8331 return size_zero_node;
8333 else if (binfo)
8335 if (kind != bk_via_virtual)
8336 return BINFO_OFFSET (binfo);
8337 else
8338 /* FROM is a virtual base class of TO. Issue an error or warning
8339 depending on whether or not this is a reinterpret cast. */
8341 if (!(complain & tf_error))
8342 return error_mark_node;
8344 error ("pointer to member conversion via virtual base %qT",
8345 BINFO_TYPE (binfo_from_vbase (binfo)));
8347 return size_zero_node;
8350 else
8351 return NULL_TREE;
8354 /* Get difference in deltas for different pointer to member function
8355 types. If the conversion is invalid and tf_error is not set in
8356 COMPLAIN, returns error_mark_node, otherwise returns an integer
8357 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8358 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8359 conversions as well. If C_CAST_P is true this conversion is taking
8360 place as part of a C-style cast.
8362 Note that the naming of FROM and TO is kind of backwards; the return
8363 value is what we add to a TO in order to get a FROM. They are named
8364 this way because we call this function to find out how to convert from
8365 a pointer to member of FROM to a pointer to member of TO. */
8367 static tree
8368 get_delta_difference (tree from, tree to,
8369 bool allow_inverse_p,
8370 bool c_cast_p, tsubst_flags_t complain)
8372 tree result;
8374 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8375 /* Pointer to member of incomplete class is permitted*/
8376 result = size_zero_node;
8377 else
8378 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8380 if (result == error_mark_node)
8381 return error_mark_node;
8383 if (!result)
8385 if (!allow_inverse_p)
8387 if (!(complain & tf_error))
8388 return error_mark_node;
8390 error_not_base_type (from, to);
8391 error (" in pointer to member conversion");
8392 result = size_zero_node;
8394 else
8396 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8398 if (result == error_mark_node)
8399 return error_mark_node;
8401 if (result)
8402 result = size_diffop_loc (input_location,
8403 size_zero_node, result);
8404 else
8406 if (!(complain & tf_error))
8407 return error_mark_node;
8409 error_not_base_type (from, to);
8410 error (" in pointer to member conversion");
8411 result = size_zero_node;
8416 return convert_to_integer (ptrdiff_type_node, result);
8419 /* Return a constructor for the pointer-to-member-function TYPE using
8420 the other components as specified. */
8422 tree
8423 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8425 tree u = NULL_TREE;
8426 tree delta_field;
8427 tree pfn_field;
8428 vec<constructor_elt, va_gc> *v;
8430 /* Pull the FIELD_DECLs out of the type. */
8431 pfn_field = TYPE_FIELDS (type);
8432 delta_field = DECL_CHAIN (pfn_field);
8434 /* Make sure DELTA has the type we want. */
8435 delta = convert_and_check (input_location, delta_type_node, delta);
8437 /* Convert to the correct target type if necessary. */
8438 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8440 /* Finish creating the initializer. */
8441 vec_alloc (v, 2);
8442 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8443 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8444 u = build_constructor (type, v);
8445 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8446 TREE_STATIC (u) = (TREE_CONSTANT (u)
8447 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8448 != NULL_TREE)
8449 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8450 != NULL_TREE));
8451 return u;
8454 /* Build a constructor for a pointer to member function. It can be
8455 used to initialize global variables, local variable, or used
8456 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8457 want to be.
8459 If FORCE is nonzero, then force this conversion, even if
8460 we would rather not do it. Usually set when using an explicit
8461 cast. A C-style cast is being processed iff C_CAST_P is true.
8463 Return error_mark_node, if something goes wrong. */
8465 tree
8466 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8467 tsubst_flags_t complain)
8469 tree fn;
8470 tree pfn_type;
8471 tree to_type;
8473 if (error_operand_p (pfn))
8474 return error_mark_node;
8476 pfn_type = TREE_TYPE (pfn);
8477 to_type = build_ptrmemfunc_type (type);
8479 /* Handle multiple conversions of pointer to member functions. */
8480 if (TYPE_PTRMEMFUNC_P (pfn_type))
8482 tree delta = NULL_TREE;
8483 tree npfn = NULL_TREE;
8484 tree n;
8486 if (!force
8487 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8488 LOOKUP_NORMAL, complain))
8490 if (complain & tf_error)
8491 error ("invalid conversion to type %qT from type %qT",
8492 to_type, pfn_type);
8493 else
8494 return error_mark_node;
8497 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8498 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8499 force,
8500 c_cast_p, complain);
8501 if (n == error_mark_node)
8502 return error_mark_node;
8504 /* We don't have to do any conversion to convert a
8505 pointer-to-member to its own type. But, we don't want to
8506 just return a PTRMEM_CST if there's an explicit cast; that
8507 cast should make the expression an invalid template argument. */
8508 if (TREE_CODE (pfn) != PTRMEM_CST)
8510 if (same_type_p (to_type, pfn_type))
8511 return pfn;
8512 else if (integer_zerop (n))
8513 return build_reinterpret_cast (to_type, pfn,
8514 complain);
8517 if (TREE_SIDE_EFFECTS (pfn))
8518 pfn = save_expr (pfn);
8520 /* Obtain the function pointer and the current DELTA. */
8521 if (TREE_CODE (pfn) == PTRMEM_CST)
8522 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8523 else
8525 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8526 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8529 /* Just adjust the DELTA field. */
8530 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8531 (TREE_TYPE (delta), ptrdiff_type_node));
8532 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8533 n = cp_build_binary_op (input_location,
8534 LSHIFT_EXPR, n, integer_one_node,
8535 complain);
8536 delta = cp_build_binary_op (input_location,
8537 PLUS_EXPR, delta, n, complain);
8538 return build_ptrmemfunc1 (to_type, delta, npfn);
8541 /* Handle null pointer to member function conversions. */
8542 if (null_ptr_cst_p (pfn))
8544 pfn = cp_build_c_cast (type, pfn, complain);
8545 return build_ptrmemfunc1 (to_type,
8546 integer_zero_node,
8547 pfn);
8550 if (type_unknown_p (pfn))
8551 return instantiate_type (type, pfn, complain);
8553 fn = TREE_OPERAND (pfn, 0);
8554 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8555 /* In a template, we will have preserved the
8556 OFFSET_REF. */
8557 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8558 return make_ptrmem_cst (to_type, fn);
8561 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8562 given by CST.
8564 ??? There is no consistency as to the types returned for the above
8565 values. Some code acts as if it were a sizetype and some as if it were
8566 integer_type_node. */
8568 void
8569 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8571 tree type = TREE_TYPE (cst);
8572 tree fn = PTRMEM_CST_MEMBER (cst);
8573 tree ptr_class, fn_class;
8575 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8577 /* The class that the function belongs to. */
8578 fn_class = DECL_CONTEXT (fn);
8580 /* The class that we're creating a pointer to member of. */
8581 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8583 /* First, calculate the adjustment to the function's class. */
8584 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8585 /*c_cast_p=*/0, tf_warning_or_error);
8587 if (!DECL_VIRTUAL_P (fn))
8588 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8589 build_addr_func (fn, tf_warning_or_error));
8590 else
8592 /* If we're dealing with a virtual function, we have to adjust 'this'
8593 again, to point to the base which provides the vtable entry for
8594 fn; the call will do the opposite adjustment. */
8595 tree orig_class = DECL_CONTEXT (fn);
8596 tree binfo = binfo_or_else (orig_class, fn_class);
8597 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8598 *delta, BINFO_OFFSET (binfo));
8600 /* We set PFN to the vtable offset at which the function can be
8601 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8602 case delta is shifted left, and then incremented). */
8603 *pfn = DECL_VINDEX (fn);
8604 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8605 TYPE_SIZE_UNIT (vtable_entry_type));
8607 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8609 case ptrmemfunc_vbit_in_pfn:
8610 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8611 integer_one_node);
8612 break;
8614 case ptrmemfunc_vbit_in_delta:
8615 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8616 *delta, integer_one_node);
8617 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8618 *delta, integer_one_node);
8619 break;
8621 default:
8622 gcc_unreachable ();
8625 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8629 /* Return an expression for PFN from the pointer-to-member function
8630 given by T. */
8632 static tree
8633 pfn_from_ptrmemfunc (tree t)
8635 if (TREE_CODE (t) == PTRMEM_CST)
8637 tree delta;
8638 tree pfn;
8640 expand_ptrmemfunc_cst (t, &delta, &pfn);
8641 if (pfn)
8642 return pfn;
8645 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8648 /* Return an expression for DELTA from the pointer-to-member function
8649 given by T. */
8651 static tree
8652 delta_from_ptrmemfunc (tree t)
8654 if (TREE_CODE (t) == PTRMEM_CST)
8656 tree delta;
8657 tree pfn;
8659 expand_ptrmemfunc_cst (t, &delta, &pfn);
8660 if (delta)
8661 return delta;
8664 return build_ptrmemfunc_access_expr (t, delta_identifier);
8667 /* Convert value RHS to type TYPE as preparation for an assignment to
8668 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8669 implicit conversion is. If FNDECL is non-NULL, we are doing the
8670 conversion in order to pass the PARMNUMth argument of FNDECL.
8671 If FNDECL is NULL, we are doing the conversion in function pointer
8672 argument passing, conversion in initialization, etc. */
8674 static tree
8675 convert_for_assignment (tree type, tree rhs,
8676 impl_conv_rhs errtype, tree fndecl, int parmnum,
8677 tsubst_flags_t complain, int flags)
8679 tree rhstype;
8680 enum tree_code coder;
8682 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8683 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8684 rhs = TREE_OPERAND (rhs, 0);
8686 /* Handle [dcl.init.list] direct-list-initialization from
8687 single element of enumeration with a fixed underlying type. */
8688 if (is_direct_enum_init (type, rhs))
8690 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8691 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8693 warning_sentinel w (warn_useless_cast);
8694 warning_sentinel w2 (warn_ignored_qualifiers);
8695 rhs = cp_build_c_cast (type, elt, complain);
8697 else
8698 rhs = error_mark_node;
8701 rhstype = TREE_TYPE (rhs);
8702 coder = TREE_CODE (rhstype);
8704 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8705 && vector_types_convertible_p (type, rhstype, true))
8707 rhs = mark_rvalue_use (rhs);
8708 return convert (type, rhs);
8711 if (rhs == error_mark_node || rhstype == error_mark_node)
8712 return error_mark_node;
8713 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8714 return error_mark_node;
8716 /* The RHS of an assignment cannot have void type. */
8717 if (coder == VOID_TYPE)
8719 if (complain & tf_error)
8720 error ("void value not ignored as it ought to be");
8721 return error_mark_node;
8724 if (c_dialect_objc ())
8726 int parmno;
8727 tree selector;
8728 tree rname = fndecl;
8730 switch (errtype)
8732 case ICR_ASSIGN:
8733 parmno = -1;
8734 break;
8735 case ICR_INIT:
8736 parmno = -2;
8737 break;
8738 default:
8739 selector = objc_message_selector ();
8740 parmno = parmnum;
8741 if (selector && parmno > 1)
8743 rname = selector;
8744 parmno -= 1;
8748 if (objc_compare_types (type, rhstype, parmno, rname))
8750 rhs = mark_rvalue_use (rhs);
8751 return convert (type, rhs);
8755 /* [expr.ass]
8757 The expression is implicitly converted (clause _conv_) to the
8758 cv-unqualified type of the left operand.
8760 We allow bad conversions here because by the time we get to this point
8761 we are committed to doing the conversion. If we end up doing a bad
8762 conversion, convert_like will complain. */
8763 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8765 /* When -Wno-pmf-conversions is use, we just silently allow
8766 conversions from pointers-to-members to plain pointers. If
8767 the conversion doesn't work, cp_convert will complain. */
8768 if (!warn_pmf2ptr
8769 && TYPE_PTR_P (type)
8770 && TYPE_PTRMEMFUNC_P (rhstype))
8771 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8772 else
8774 if (complain & tf_error)
8776 /* If the right-hand side has unknown type, then it is an
8777 overloaded function. Call instantiate_type to get error
8778 messages. */
8779 if (rhstype == unknown_type_node)
8781 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8782 /* -fpermissive might allow this; recurse. */
8783 if (!seen_error ())
8784 return convert_for_assignment (type, r, errtype, fndecl,
8785 parmnum, complain, flags);
8787 else if (fndecl)
8788 error_at (EXPR_LOC_OR_LOC (rhs, input_location),
8789 "cannot convert %qH to %qI for argument %qP to %qD",
8790 rhstype, type, parmnum, fndecl);
8791 else
8792 switch (errtype)
8794 case ICR_DEFAULT_ARGUMENT:
8795 error ("cannot convert %qH to %qI in default argument",
8796 rhstype, type);
8797 break;
8798 case ICR_ARGPASS:
8799 error ("cannot convert %qH to %qI in argument passing",
8800 rhstype, type);
8801 break;
8802 case ICR_CONVERTING:
8803 error ("cannot convert %qH to %qI",
8804 rhstype, type);
8805 break;
8806 case ICR_INIT:
8807 error ("cannot convert %qH to %qI in initialization",
8808 rhstype, type);
8809 break;
8810 case ICR_RETURN:
8811 error ("cannot convert %qH to %qI in return",
8812 rhstype, type);
8813 break;
8814 case ICR_ASSIGN:
8815 error ("cannot convert %qH to %qI in assignment",
8816 rhstype, type);
8817 break;
8818 default:
8819 gcc_unreachable();
8821 if (TYPE_PTR_P (rhstype)
8822 && TYPE_PTR_P (type)
8823 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8824 && CLASS_TYPE_P (TREE_TYPE (type))
8825 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8826 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8827 (TREE_TYPE (rhstype))),
8828 "class type %qT is incomplete", TREE_TYPE (rhstype));
8830 return error_mark_node;
8833 if (warn_suggest_attribute_format)
8835 const enum tree_code codel = TREE_CODE (type);
8836 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8837 && coder == codel
8838 && check_missing_format_attribute (type, rhstype)
8839 && (complain & tf_warning))
8840 switch (errtype)
8842 case ICR_ARGPASS:
8843 case ICR_DEFAULT_ARGUMENT:
8844 if (fndecl)
8845 warning (OPT_Wsuggest_attribute_format,
8846 "parameter %qP of %qD might be a candidate "
8847 "for a format attribute", parmnum, fndecl);
8848 else
8849 warning (OPT_Wsuggest_attribute_format,
8850 "parameter might be a candidate "
8851 "for a format attribute");
8852 break;
8853 case ICR_CONVERTING:
8854 warning (OPT_Wsuggest_attribute_format,
8855 "target of conversion might be a candidate "
8856 "for a format attribute");
8857 break;
8858 case ICR_INIT:
8859 warning (OPT_Wsuggest_attribute_format,
8860 "target of initialization might be a candidate "
8861 "for a format attribute");
8862 break;
8863 case ICR_RETURN:
8864 warning (OPT_Wsuggest_attribute_format,
8865 "return type might be a candidate "
8866 "for a format attribute");
8867 break;
8868 case ICR_ASSIGN:
8869 warning (OPT_Wsuggest_attribute_format,
8870 "left-hand side of assignment might be a candidate "
8871 "for a format attribute");
8872 break;
8873 default:
8874 gcc_unreachable();
8878 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8879 does not. */
8880 if (warn_parentheses
8881 && TREE_CODE (type) == BOOLEAN_TYPE
8882 && TREE_CODE (rhs) == MODIFY_EXPR
8883 && !TREE_NO_WARNING (rhs)
8884 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8885 && (complain & tf_warning))
8887 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8889 warning_at (loc, OPT_Wparentheses,
8890 "suggest parentheses around assignment used as truth value");
8891 TREE_NO_WARNING (rhs) = 1;
8894 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8895 complain, flags);
8898 /* Convert RHS to be of type TYPE.
8899 If EXP is nonzero, it is the target of the initialization.
8900 ERRTYPE indicates what kind of error the implicit conversion is.
8902 Two major differences between the behavior of
8903 `convert_for_assignment' and `convert_for_initialization'
8904 are that references are bashed in the former, while
8905 copied in the latter, and aggregates are assigned in
8906 the former (operator=) while initialized in the
8907 latter (X(X&)).
8909 If using constructor make sure no conversion operator exists, if one does
8910 exist, an ambiguity exists. */
8912 tree
8913 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8914 impl_conv_rhs errtype, tree fndecl, int parmnum,
8915 tsubst_flags_t complain)
8917 enum tree_code codel = TREE_CODE (type);
8918 tree rhstype;
8919 enum tree_code coder;
8921 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8922 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8923 if (TREE_CODE (rhs) == NOP_EXPR
8924 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8925 && codel != REFERENCE_TYPE)
8926 rhs = TREE_OPERAND (rhs, 0);
8928 if (type == error_mark_node
8929 || rhs == error_mark_node
8930 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8931 return error_mark_node;
8933 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8935 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8936 && TREE_CODE (type) != ARRAY_TYPE
8937 && (TREE_CODE (type) != REFERENCE_TYPE
8938 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8939 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8940 && !TYPE_REFFN_P (type))
8941 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8942 rhs = decay_conversion (rhs, complain);
8944 rhstype = TREE_TYPE (rhs);
8945 coder = TREE_CODE (rhstype);
8947 if (coder == ERROR_MARK)
8948 return error_mark_node;
8950 /* We accept references to incomplete types, so we can
8951 return here before checking if RHS is of complete type. */
8953 if (codel == REFERENCE_TYPE)
8955 /* This should eventually happen in convert_arguments. */
8956 int savew = 0, savee = 0;
8958 if (fndecl)
8959 savew = warningcount + werrorcount, savee = errorcount;
8960 rhs = initialize_reference (type, rhs, flags, complain);
8962 if (fndecl
8963 && (warningcount + werrorcount > savew || errorcount > savee))
8964 inform (DECL_SOURCE_LOCATION (fndecl),
8965 "in passing argument %P of %qD", parmnum, fndecl);
8967 return rhs;
8970 if (exp != 0)
8971 exp = require_complete_type_sfinae (exp, complain);
8972 if (exp == error_mark_node)
8973 return error_mark_node;
8975 rhstype = non_reference (rhstype);
8977 type = complete_type (type);
8979 if (DIRECT_INIT_EXPR_P (type, rhs))
8980 /* Don't try to do copy-initialization if we already have
8981 direct-initialization. */
8982 return rhs;
8984 if (MAYBE_CLASS_TYPE_P (type))
8985 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8987 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8988 complain, flags);
8991 /* If RETVAL is the address of, or a reference to, a local variable or
8992 temporary give an appropriate warning and return true. */
8994 static bool
8995 maybe_warn_about_returning_address_of_local (tree retval)
8997 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8998 tree whats_returned = fold_for_warn (retval);
9000 for (;;)
9002 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9003 whats_returned = TREE_OPERAND (whats_returned, 1);
9004 else if (CONVERT_EXPR_P (whats_returned)
9005 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9006 whats_returned = TREE_OPERAND (whats_returned, 0);
9007 else
9008 break;
9011 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9012 return false;
9013 whats_returned = TREE_OPERAND (whats_returned, 0);
9015 while (TREE_CODE (whats_returned) == COMPONENT_REF
9016 || TREE_CODE (whats_returned) == ARRAY_REF)
9017 whats_returned = TREE_OPERAND (whats_returned, 0);
9019 if (TREE_CODE (valtype) == REFERENCE_TYPE)
9021 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9022 || TREE_CODE (whats_returned) == TARGET_EXPR)
9024 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
9025 return true;
9027 if (VAR_P (whats_returned)
9028 && DECL_NAME (whats_returned)
9029 && TEMP_NAME_P (DECL_NAME (whats_returned)))
9031 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
9032 return true;
9036 if (DECL_P (whats_returned)
9037 && DECL_NAME (whats_returned)
9038 && DECL_FUNCTION_SCOPE_P (whats_returned)
9039 && !is_capture_proxy (whats_returned)
9040 && !(TREE_STATIC (whats_returned)
9041 || TREE_PUBLIC (whats_returned)))
9043 if (TREE_CODE (valtype) == REFERENCE_TYPE)
9044 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9045 OPT_Wreturn_local_addr,
9046 "reference to local variable %qD returned",
9047 whats_returned);
9048 else if (TREE_CODE (whats_returned) == LABEL_DECL)
9049 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9050 OPT_Wreturn_local_addr, "address of label %qD returned",
9051 whats_returned);
9052 else
9053 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9054 OPT_Wreturn_local_addr, "address of local variable %qD "
9055 "returned", whats_returned);
9056 return true;
9059 return false;
9062 /* Check that returning RETVAL from the current function is valid.
9063 Return an expression explicitly showing all conversions required to
9064 change RETVAL into the function return type, and to assign it to
9065 the DECL_RESULT for the function. Set *NO_WARNING to true if
9066 code reaches end of non-void function warning shouldn't be issued
9067 on this RETURN_EXPR. */
9069 tree
9070 check_return_expr (tree retval, bool *no_warning)
9072 tree result;
9073 /* The type actually returned by the function. */
9074 tree valtype;
9075 /* The type the function is declared to return, or void if
9076 the declared type is incomplete. */
9077 tree functype;
9078 int fn_returns_value_p;
9079 bool named_return_value_okay_p;
9081 *no_warning = false;
9083 /* A `volatile' function is one that isn't supposed to return, ever.
9084 (This is a G++ extension, used to get better code for functions
9085 that call the `volatile' function.) */
9086 if (TREE_THIS_VOLATILE (current_function_decl))
9087 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9089 /* Check for various simple errors. */
9090 if (DECL_DESTRUCTOR_P (current_function_decl))
9092 if (retval)
9093 error ("returning a value from a destructor");
9094 return NULL_TREE;
9096 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9098 if (in_function_try_handler)
9099 /* If a return statement appears in a handler of the
9100 function-try-block of a constructor, the program is ill-formed. */
9101 error ("cannot return from a handler of a function-try-block of a constructor");
9102 else if (retval)
9103 /* You can't return a value from a constructor. */
9104 error ("returning a value from a constructor");
9105 return NULL_TREE;
9108 const tree saved_retval = retval;
9110 if (processing_template_decl)
9112 current_function_returns_value = 1;
9114 if (check_for_bare_parameter_packs (retval))
9115 return error_mark_node;
9117 /* If one of the types might be void, we can't tell whether we're
9118 returning a value. */
9119 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9120 && !current_function_auto_return_pattern)
9121 || (retval != NULL_TREE
9122 && (TREE_TYPE (retval) == NULL_TREE
9123 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9124 goto dependent;
9127 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9129 /* Deduce auto return type from a return statement. */
9130 if (current_function_auto_return_pattern)
9132 tree auto_node;
9133 tree type;
9135 if (!retval && !is_auto (current_function_auto_return_pattern))
9137 /* Give a helpful error message. */
9138 error ("return-statement with no value, in function returning %qT",
9139 current_function_auto_return_pattern);
9140 inform (input_location, "only plain %<auto%> return type can be "
9141 "deduced to %<void%>");
9142 type = error_mark_node;
9144 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9146 error ("returning initializer list");
9147 type = error_mark_node;
9149 else
9151 if (!retval)
9152 retval = void_node;
9153 auto_node = type_uses_auto (current_function_auto_return_pattern);
9154 type = do_auto_deduction (current_function_auto_return_pattern,
9155 retval, auto_node);
9158 if (type == error_mark_node)
9159 /* Leave it. */;
9160 else if (functype == current_function_auto_return_pattern)
9161 apply_deduced_return_type (current_function_decl, type);
9162 else if (!same_type_p (type, functype))
9164 if (LAMBDA_FUNCTION_P (current_function_decl))
9165 error ("inconsistent types %qT and %qT deduced for "
9166 "lambda return type", functype, type);
9167 else
9168 error ("inconsistent deduction for auto return type: "
9169 "%qT and then %qT", functype, type);
9171 functype = type;
9174 result = DECL_RESULT (current_function_decl);
9175 valtype = TREE_TYPE (result);
9176 gcc_assert (valtype != NULL_TREE);
9177 fn_returns_value_p = !VOID_TYPE_P (valtype);
9179 /* Check for a return statement with no return value in a function
9180 that's supposed to return a value. */
9181 if (!retval && fn_returns_value_p)
9183 if (functype != error_mark_node)
9184 permerror (input_location, "return-statement with no value, in "
9185 "function returning %qT", valtype);
9186 /* Remember that this function did return. */
9187 current_function_returns_value = 1;
9188 /* And signal caller that TREE_NO_WARNING should be set on the
9189 RETURN_EXPR to avoid control reaches end of non-void function
9190 warnings in tree-cfg.c. */
9191 *no_warning = true;
9193 /* Check for a return statement with a value in a function that
9194 isn't supposed to return a value. */
9195 else if (retval && !fn_returns_value_p)
9197 if (VOID_TYPE_P (TREE_TYPE (retval)))
9198 /* You can return a `void' value from a function of `void'
9199 type. In that case, we have to evaluate the expression for
9200 its side-effects. */
9201 finish_expr_stmt (retval);
9202 else
9203 permerror (input_location,
9204 "return-statement with a value, in function "
9205 "returning %qT", valtype);
9206 current_function_returns_null = 1;
9208 /* There's really no value to return, after all. */
9209 return NULL_TREE;
9211 else if (!retval)
9212 /* Remember that this function can sometimes return without a
9213 value. */
9214 current_function_returns_null = 1;
9215 else
9216 /* Remember that this function did return a value. */
9217 current_function_returns_value = 1;
9219 /* Check for erroneous operands -- but after giving ourselves a
9220 chance to provide an error about returning a value from a void
9221 function. */
9222 if (error_operand_p (retval))
9224 current_function_return_value = error_mark_node;
9225 return error_mark_node;
9228 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9229 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9230 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9231 && ! flag_check_new
9232 && retval && null_ptr_cst_p (retval))
9233 warning (0, "%<operator new%> must not return NULL unless it is "
9234 "declared %<throw()%> (or -fcheck-new is in effect)");
9236 /* Effective C++ rule 15. See also start_function. */
9237 if (warn_ecpp
9238 && DECL_NAME (current_function_decl) == assign_op_identifier
9239 && !type_dependent_expression_p (retval))
9241 bool warn = true;
9243 /* The function return type must be a reference to the current
9244 class. */
9245 if (TREE_CODE (valtype) == REFERENCE_TYPE
9246 && same_type_ignoring_top_level_qualifiers_p
9247 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9249 /* Returning '*this' is obviously OK. */
9250 if (retval == current_class_ref)
9251 warn = false;
9252 /* If we are calling a function whose return type is the same of
9253 the current class reference, it is ok. */
9254 else if (INDIRECT_REF_P (retval)
9255 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9256 warn = false;
9259 if (warn)
9260 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9263 if (dependent_type_p (functype)
9264 || type_dependent_expression_p (retval))
9266 dependent:
9267 /* We should not have changed the return value. */
9268 gcc_assert (retval == saved_retval);
9269 return retval;
9272 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9274 [...] For a function with a class return type, if the expression
9275 in the return statement is the name of a local object, and the cv-
9276 unqualified type of the local object is the same as the function
9277 return type, an implementation is permitted to omit creating the tem-
9278 porary object to hold the function return value [...]
9280 So, if this is a value-returning function that always returns the same
9281 local variable, remember it.
9283 It might be nice to be more flexible, and choose the first suitable
9284 variable even if the function sometimes returns something else, but
9285 then we run the risk of clobbering the variable we chose if the other
9286 returned expression uses the chosen variable somehow. And people expect
9287 this restriction, anyway. (jason 2000-11-19)
9289 See finish_function and finalize_nrv for the rest of this optimization. */
9291 named_return_value_okay_p =
9292 (retval != NULL_TREE
9293 && !processing_template_decl
9294 /* Must be a local, automatic variable. */
9295 && VAR_P (retval)
9296 && DECL_CONTEXT (retval) == current_function_decl
9297 && ! TREE_STATIC (retval)
9298 /* And not a lambda or anonymous union proxy. */
9299 && !DECL_HAS_VALUE_EXPR_P (retval)
9300 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9301 /* The cv-unqualified type of the returned value must be the
9302 same as the cv-unqualified return type of the
9303 function. */
9304 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9305 (TYPE_MAIN_VARIANT (functype)))
9306 /* And the returned value must be non-volatile. */
9307 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9309 if (fn_returns_value_p && flag_elide_constructors)
9311 if (named_return_value_okay_p
9312 && (current_function_return_value == NULL_TREE
9313 || current_function_return_value == retval))
9314 current_function_return_value = retval;
9315 else
9316 current_function_return_value = error_mark_node;
9319 /* We don't need to do any conversions when there's nothing being
9320 returned. */
9321 if (!retval)
9322 return NULL_TREE;
9324 /* Do any required conversions. */
9325 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9326 /* No conversions are required. */
9328 else
9330 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9332 /* The functype's return type will have been set to void, if it
9333 was an incomplete type. Just treat this as 'return;' */
9334 if (VOID_TYPE_P (functype))
9335 return error_mark_node;
9337 /* If we had an id-expression obfuscated by force_paren_expr, we need
9338 to undo it so we can try to treat it as an rvalue below. */
9339 retval = maybe_undo_parenthesized_ref (retval);
9341 if (processing_template_decl)
9342 retval = build_non_dependent_expr (retval);
9344 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9345 treated as an rvalue for the purposes of overload resolution to
9346 favor move constructors over copy constructors.
9348 Note that these conditions are similar to, but not as strict as,
9349 the conditions for the named return value optimization. */
9350 bool converted = false;
9351 if ((cxx_dialect != cxx98)
9352 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9353 || TREE_CODE (retval) == PARM_DECL)
9354 && DECL_CONTEXT (retval) == current_function_decl
9355 && !TREE_STATIC (retval)
9356 /* This is only interesting for class type. */
9357 && CLASS_TYPE_P (functype))
9359 tree moved = move (retval);
9360 moved = convert_for_initialization
9361 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9362 ICR_RETURN, NULL_TREE, 0, tf_none);
9363 if (moved != error_mark_node)
9365 retval = moved;
9366 converted = true;
9370 /* First convert the value to the function's return type, then
9371 to the type of return value's location to handle the
9372 case that functype is smaller than the valtype. */
9373 if (!converted)
9374 retval = convert_for_initialization
9375 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9376 tf_warning_or_error);
9377 retval = convert (valtype, retval);
9379 /* If the conversion failed, treat this just like `return;'. */
9380 if (retval == error_mark_node)
9381 return retval;
9382 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9383 else if (! cfun->returns_struct
9384 && TREE_CODE (retval) == TARGET_EXPR
9385 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9386 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9387 TREE_OPERAND (retval, 0));
9388 else if (!processing_template_decl
9389 && maybe_warn_about_returning_address_of_local (retval))
9390 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9391 build_zero_cst (TREE_TYPE (retval)));
9394 if (processing_template_decl)
9395 return saved_retval;
9397 /* Actually copy the value returned into the appropriate location. */
9398 if (retval && retval != result)
9399 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9401 return retval;
9405 /* Returns nonzero if the pointer-type FROM can be converted to the
9406 pointer-type TO via a qualification conversion. If CONSTP is -1,
9407 then we return nonzero if the pointers are similar, and the
9408 cv-qualification signature of FROM is a proper subset of that of TO.
9410 If CONSTP is positive, then all outer pointers have been
9411 const-qualified. */
9413 static int
9414 comp_ptr_ttypes_real (tree to, tree from, int constp)
9416 bool to_more_cv_qualified = false;
9417 bool is_opaque_pointer = false;
9419 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9421 if (TREE_CODE (to) != TREE_CODE (from))
9422 return 0;
9424 if (TREE_CODE (from) == OFFSET_TYPE
9425 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9426 TYPE_OFFSET_BASETYPE (to)))
9427 return 0;
9429 /* Const and volatile mean something different for function types,
9430 so the usual checks are not appropriate. */
9431 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9433 if (!at_least_as_qualified_p (to, from))
9434 return 0;
9436 if (!at_least_as_qualified_p (from, to))
9438 if (constp == 0)
9439 return 0;
9440 to_more_cv_qualified = true;
9443 if (constp > 0)
9444 constp &= TYPE_READONLY (to);
9447 if (VECTOR_TYPE_P (to))
9448 is_opaque_pointer = vector_targets_convertible_p (to, from);
9450 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9451 return ((constp >= 0 || to_more_cv_qualified)
9452 && (is_opaque_pointer
9453 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9457 /* When comparing, say, char ** to char const **, this function takes
9458 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9459 types to this function. */
9462 comp_ptr_ttypes (tree to, tree from)
9464 return comp_ptr_ttypes_real (to, from, 1);
9467 /* Returns true iff FNTYPE is a non-class type that involves
9468 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9469 if a parameter type is ill-formed. */
9471 bool
9472 error_type_p (const_tree type)
9474 tree t;
9476 switch (TREE_CODE (type))
9478 case ERROR_MARK:
9479 return true;
9481 case POINTER_TYPE:
9482 case REFERENCE_TYPE:
9483 case OFFSET_TYPE:
9484 return error_type_p (TREE_TYPE (type));
9486 case FUNCTION_TYPE:
9487 case METHOD_TYPE:
9488 if (error_type_p (TREE_TYPE (type)))
9489 return true;
9490 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9491 if (error_type_p (TREE_VALUE (t)))
9492 return true;
9493 return false;
9495 case RECORD_TYPE:
9496 if (TYPE_PTRMEMFUNC_P (type))
9497 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9498 return false;
9500 default:
9501 return false;
9505 /* Returns true if to and from are (possibly multi-level) pointers to the same
9506 type or inheritance-related types, regardless of cv-quals. */
9508 bool
9509 ptr_reasonably_similar (const_tree to, const_tree from)
9511 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9513 /* Any target type is similar enough to void. */
9514 if (VOID_TYPE_P (to))
9515 return !error_type_p (from);
9516 if (VOID_TYPE_P (from))
9517 return !error_type_p (to);
9519 if (TREE_CODE (to) != TREE_CODE (from))
9520 return false;
9522 if (TREE_CODE (from) == OFFSET_TYPE
9523 && comptypes (TYPE_OFFSET_BASETYPE (to),
9524 TYPE_OFFSET_BASETYPE (from),
9525 COMPARE_BASE | COMPARE_DERIVED))
9526 continue;
9528 if (VECTOR_TYPE_P (to)
9529 && vector_types_convertible_p (to, from, false))
9530 return true;
9532 if (TREE_CODE (to) == INTEGER_TYPE
9533 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9534 return true;
9536 if (TREE_CODE (to) == FUNCTION_TYPE)
9537 return !error_type_p (to) && !error_type_p (from);
9539 if (!TYPE_PTR_P (to))
9541 /* When either type is incomplete avoid DERIVED_FROM_P,
9542 which may call complete_type (c++/57942). */
9543 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9544 return comptypes
9545 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9546 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9551 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9552 pointer-to-member types) are the same, ignoring cv-qualification at
9553 all levels. */
9555 bool
9556 comp_ptr_ttypes_const (tree to, tree from)
9558 bool is_opaque_pointer = false;
9560 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9562 if (TREE_CODE (to) != TREE_CODE (from))
9563 return false;
9565 if (TREE_CODE (from) == OFFSET_TYPE
9566 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9567 TYPE_OFFSET_BASETYPE (to)))
9568 continue;
9570 if (VECTOR_TYPE_P (to))
9571 is_opaque_pointer = vector_targets_convertible_p (to, from);
9573 if (!TYPE_PTR_P (to))
9574 return (is_opaque_pointer
9575 || same_type_ignoring_top_level_qualifiers_p (to, from));
9579 /* Returns the type qualifiers for this type, including the qualifiers on the
9580 elements for an array type. */
9583 cp_type_quals (const_tree type)
9585 int quals;
9586 /* This CONST_CAST is okay because strip_array_types returns its
9587 argument unmodified and we assign it to a const_tree. */
9588 type = strip_array_types (CONST_CAST_TREE (type));
9589 if (type == error_mark_node
9590 /* Quals on a FUNCTION_TYPE are memfn quals. */
9591 || TREE_CODE (type) == FUNCTION_TYPE)
9592 return TYPE_UNQUALIFIED;
9593 quals = TYPE_QUALS (type);
9594 /* METHOD and REFERENCE_TYPEs should never have quals. */
9595 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9596 && TREE_CODE (type) != REFERENCE_TYPE)
9597 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9598 == TYPE_UNQUALIFIED));
9599 return quals;
9602 /* Returns the function-ref-qualifier for TYPE */
9604 cp_ref_qualifier
9605 type_memfn_rqual (const_tree type)
9607 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9608 || TREE_CODE (type) == METHOD_TYPE);
9610 if (!FUNCTION_REF_QUALIFIED (type))
9611 return REF_QUAL_NONE;
9612 else if (FUNCTION_RVALUE_QUALIFIED (type))
9613 return REF_QUAL_RVALUE;
9614 else
9615 return REF_QUAL_LVALUE;
9618 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9619 METHOD_TYPE. */
9622 type_memfn_quals (const_tree type)
9624 if (TREE_CODE (type) == FUNCTION_TYPE)
9625 return TYPE_QUALS (type);
9626 else if (TREE_CODE (type) == METHOD_TYPE)
9627 return cp_type_quals (class_of_this_parm (type));
9628 else
9629 gcc_unreachable ();
9632 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9633 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9635 tree
9636 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9638 /* Could handle METHOD_TYPE here if necessary. */
9639 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9640 if (TYPE_QUALS (type) == memfn_quals
9641 && type_memfn_rqual (type) == rqual)
9642 return type;
9644 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9645 complex. */
9646 tree result = build_qualified_type (type, memfn_quals);
9647 return build_ref_qualified_type (result, rqual);
9650 /* Returns nonzero if TYPE is const or volatile. */
9652 bool
9653 cv_qualified_p (const_tree type)
9655 int quals = cp_type_quals (type);
9656 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9659 /* Returns nonzero if the TYPE contains a mutable member. */
9661 bool
9662 cp_has_mutable_p (const_tree type)
9664 /* This CONST_CAST is okay because strip_array_types returns its
9665 argument unmodified and we assign it to a const_tree. */
9666 type = strip_array_types (CONST_CAST_TREE(type));
9668 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9671 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9672 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9673 approximation. In particular, consider:
9675 int f();
9676 struct S { int i; };
9677 const S s = { f(); }
9679 Here, we will make "s" as TREE_READONLY (because it is declared
9680 "const") -- only to reverse ourselves upon seeing that the
9681 initializer is non-constant. */
9683 void
9684 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9686 tree type = TREE_TYPE (decl);
9688 if (type == error_mark_node)
9689 return;
9691 if (TREE_CODE (decl) == TYPE_DECL)
9692 return;
9694 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9695 && type_quals != TYPE_UNQUALIFIED));
9697 /* Avoid setting TREE_READONLY incorrectly. */
9698 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9699 constructor can produce constant init, so rely on cp_finish_decl to
9700 clear TREE_READONLY if the variable has non-constant init. */
9702 /* If the type has (or might have) a mutable component, that component
9703 might be modified. */
9704 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9705 type_quals &= ~TYPE_QUAL_CONST;
9707 c_apply_type_quals_to_decl (type_quals, decl);
9710 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9711 exemplar types such that casting T1 to T2 is casting away constness
9712 if and only if there is no implicit conversion from T1 to T2. */
9714 static void
9715 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9717 int quals1;
9718 int quals2;
9720 /* [expr.const.cast]
9722 For multi-level pointer to members and multi-level mixed pointers
9723 and pointers to members (conv.qual), the "member" aspect of a
9724 pointer to member level is ignored when determining if a const
9725 cv-qualifier has been cast away. */
9726 /* [expr.const.cast]
9728 For two pointer types:
9730 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9731 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9732 K is min(N,M)
9734 casting from X1 to X2 casts away constness if, for a non-pointer
9735 type T there does not exist an implicit conversion (clause
9736 _conv_) from:
9738 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9742 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9743 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9744 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9746 *t1 = cp_build_qualified_type (void_type_node,
9747 cp_type_quals (*t1));
9748 *t2 = cp_build_qualified_type (void_type_node,
9749 cp_type_quals (*t2));
9750 return;
9753 quals1 = cp_type_quals (*t1);
9754 quals2 = cp_type_quals (*t2);
9756 if (TYPE_PTRDATAMEM_P (*t1))
9757 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9758 else
9759 *t1 = TREE_TYPE (*t1);
9760 if (TYPE_PTRDATAMEM_P (*t2))
9761 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9762 else
9763 *t2 = TREE_TYPE (*t2);
9765 casts_away_constness_r (t1, t2, complain);
9766 *t1 = build_pointer_type (*t1);
9767 *t2 = build_pointer_type (*t2);
9768 *t1 = cp_build_qualified_type (*t1, quals1);
9769 *t2 = cp_build_qualified_type (*t2, quals2);
9772 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9773 constness.
9775 ??? This function returns non-zero if casting away qualifiers not
9776 just const. We would like to return to the caller exactly which
9777 qualifiers are casted away to give more accurate diagnostics.
9780 static bool
9781 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9783 if (TREE_CODE (t2) == REFERENCE_TYPE)
9785 /* [expr.const.cast]
9787 Casting from an lvalue of type T1 to an lvalue of type T2
9788 using a reference cast casts away constness if a cast from an
9789 rvalue of type "pointer to T1" to the type "pointer to T2"
9790 casts away constness. */
9791 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9792 return casts_away_constness (build_pointer_type (t1),
9793 build_pointer_type (TREE_TYPE (t2)),
9794 complain);
9797 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9798 /* [expr.const.cast]
9800 Casting from an rvalue of type "pointer to data member of X
9801 of type T1" to the type "pointer to data member of Y of type
9802 T2" casts away constness if a cast from an rvalue of type
9803 "pointer to T1" to the type "pointer to T2" casts away
9804 constness. */
9805 return casts_away_constness
9806 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9807 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9808 complain);
9810 /* Casting away constness is only something that makes sense for
9811 pointer or reference types. */
9812 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9813 return false;
9815 /* Top-level qualifiers don't matter. */
9816 t1 = TYPE_MAIN_VARIANT (t1);
9817 t2 = TYPE_MAIN_VARIANT (t2);
9818 casts_away_constness_r (&t1, &t2, complain);
9819 if (!can_convert (t2, t1, complain))
9820 return true;
9822 return false;
9825 /* If T is a REFERENCE_TYPE return the type to which T refers.
9826 Otherwise, return T itself. */
9828 tree
9829 non_reference (tree t)
9831 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9832 t = TREE_TYPE (t);
9833 return t;
9837 /* Return nonzero if REF is an lvalue valid for this language;
9838 otherwise, print an error message and return zero. USE says
9839 how the lvalue is being used and so selects the error message. */
9842 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9844 cp_lvalue_kind kind = lvalue_kind (ref);
9846 if (kind == clk_none)
9848 if (complain & tf_error)
9849 lvalue_error (input_location, use);
9850 return 0;
9852 else if (kind & (clk_rvalueref|clk_class))
9854 if (!(complain & tf_error))
9855 return 0;
9856 if (kind & clk_class)
9857 /* Make this a permerror because we used to accept it. */
9858 permerror (input_location, "using temporary as lvalue");
9859 else
9860 error ("using xvalue (rvalue reference) as lvalue");
9862 return 1;
9865 /* Return true if a user-defined literal operator is a raw operator. */
9867 bool
9868 check_raw_literal_operator (const_tree decl)
9870 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9871 tree argtype;
9872 int arity;
9873 bool maybe_raw_p = false;
9875 /* Count the number and type of arguments and check for ellipsis. */
9876 for (argtype = argtypes, arity = 0;
9877 argtype && argtype != void_list_node;
9878 ++arity, argtype = TREE_CHAIN (argtype))
9880 tree t = TREE_VALUE (argtype);
9882 if (same_type_p (t, const_string_type_node))
9883 maybe_raw_p = true;
9885 if (!argtype)
9886 return false; /* Found ellipsis. */
9888 if (!maybe_raw_p || arity != 1)
9889 return false;
9891 return true;
9895 /* Return true if a user-defined literal operator has one of the allowed
9896 argument types. */
9898 bool
9899 check_literal_operator_args (const_tree decl,
9900 bool *long_long_unsigned_p, bool *long_double_p)
9902 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9904 *long_long_unsigned_p = false;
9905 *long_double_p = false;
9906 if (processing_template_decl || processing_specialization)
9907 return argtypes == void_list_node;
9908 else
9910 tree argtype;
9911 int arity;
9912 int max_arity = 2;
9914 /* Count the number and type of arguments and check for ellipsis. */
9915 for (argtype = argtypes, arity = 0;
9916 argtype && argtype != void_list_node;
9917 argtype = TREE_CHAIN (argtype))
9919 tree t = TREE_VALUE (argtype);
9920 ++arity;
9922 if (TYPE_PTR_P (t))
9924 bool maybe_raw_p = false;
9925 t = TREE_TYPE (t);
9926 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9927 return false;
9928 t = TYPE_MAIN_VARIANT (t);
9929 if ((maybe_raw_p = same_type_p (t, char_type_node))
9930 || same_type_p (t, wchar_type_node)
9931 || same_type_p (t, char16_type_node)
9932 || same_type_p (t, char32_type_node))
9934 argtype = TREE_CHAIN (argtype);
9935 if (!argtype)
9936 return false;
9937 t = TREE_VALUE (argtype);
9938 if (maybe_raw_p && argtype == void_list_node)
9939 return true;
9940 else if (same_type_p (t, size_type_node))
9942 ++arity;
9943 continue;
9945 else
9946 return false;
9949 else if (same_type_p (t, long_long_unsigned_type_node))
9951 max_arity = 1;
9952 *long_long_unsigned_p = true;
9954 else if (same_type_p (t, long_double_type_node))
9956 max_arity = 1;
9957 *long_double_p = true;
9959 else if (same_type_p (t, char_type_node))
9960 max_arity = 1;
9961 else if (same_type_p (t, wchar_type_node))
9962 max_arity = 1;
9963 else if (same_type_p (t, char16_type_node))
9964 max_arity = 1;
9965 else if (same_type_p (t, char32_type_node))
9966 max_arity = 1;
9967 else
9968 return false;
9970 if (!argtype)
9971 return false; /* Found ellipsis. */
9973 if (arity != max_arity)
9974 return false;
9976 return true;
9980 /* Always returns false since unlike C90, C++ has no concept of implicit
9981 function declarations. */
9983 bool
9984 c_decl_implicit (const_tree)
9986 return false;