ChangeLog fix
[official-gcc.git] / gcc / cp / typeck.c
blob9febdb908ae9220e745097791fe7743d1d3cfe2b
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;
834 /* Save space: see if the result is identical to one of the args. */
835 if (valtype == TREE_TYPE (t1) && ! p2)
836 return cp_build_type_attribute_variant (t1, attributes);
837 if (valtype == TREE_TYPE (t2) && ! p1)
838 return cp_build_type_attribute_variant (t2, attributes);
840 /* Simple way if one arg fails to specify argument types. */
841 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
842 parms = p2;
843 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
844 parms = p1;
845 else
846 parms = commonparms (p1, p2);
848 cp_cv_quals quals = type_memfn_quals (t1);
849 cp_ref_qualifier rqual = type_memfn_rqual (t1);
850 gcc_assert (quals == type_memfn_quals (t2));
851 gcc_assert (rqual == type_memfn_rqual (t2));
853 tree rval = build_function_type (valtype, parms);
854 rval = apply_memfn_quals (rval, quals);
855 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
856 TYPE_RAISES_EXCEPTIONS (t2));
857 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
858 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
859 break;
862 case METHOD_TYPE:
864 /* Get this value the long way, since TYPE_METHOD_BASETYPE
865 is just the main variant of this. */
866 tree basetype = class_of_this_parm (t2);
867 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
868 TYPE_RAISES_EXCEPTIONS (t2));
869 cp_ref_qualifier rqual = type_memfn_rqual (t1);
870 tree t3;
871 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
873 /* If this was a member function type, get back to the
874 original type of type member function (i.e., without
875 the class instance variable up front. */
876 t1 = build_function_type (TREE_TYPE (t1),
877 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
878 t2 = build_function_type (TREE_TYPE (t2),
879 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
880 t3 = merge_types (t1, t2);
881 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
882 TYPE_ARG_TYPES (t3));
883 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
884 break;
887 case TYPENAME_TYPE:
888 /* There is no need to merge attributes into a TYPENAME_TYPE.
889 When the type is instantiated it will have whatever
890 attributes result from the instantiation. */
891 return t1;
893 default:;
894 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
895 return t1;
896 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
897 return t2;
898 break;
901 return cp_build_type_attribute_variant (t1, attributes);
904 /* Return the ARRAY_TYPE type without its domain. */
906 tree
907 strip_array_domain (tree type)
909 tree t2;
910 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
911 if (TYPE_DOMAIN (type) == NULL_TREE)
912 return type;
913 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
914 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
917 /* Wrapper around cp_common_type that is used by c-common.c and other
918 front end optimizations that remove promotions.
920 Return the common type for two arithmetic types T1 and T2 under the
921 usual arithmetic conversions. The default conversions have already
922 been applied, and enumerated types converted to their compatible
923 integer types. */
925 tree
926 common_type (tree t1, tree t2)
928 /* If one type is nonsense, use the other */
929 if (t1 == error_mark_node)
930 return t2;
931 if (t2 == error_mark_node)
932 return t1;
934 return cp_common_type (t1, t2);
937 /* Return the common type of two pointer types T1 and T2. This is the
938 type for the result of most arithmetic operations if the operands
939 have the given two types.
941 We assume that comp_target_types has already been done and returned
942 nonzero; if that isn't so, this may crash. */
944 tree
945 common_pointer_type (tree t1, tree t2)
947 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
948 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
949 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
951 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
952 CPO_CONVERSION, tf_warning_or_error);
955 /* Compare two exception specifier types for exactness or subsetness, if
956 allowed. Returns false for mismatch, true for match (same, or
957 derived and !exact).
959 [except.spec] "If a class X ... objects of class X or any class publicly
960 and unambiguously derived from X. Similarly, if a pointer type Y * ...
961 exceptions of type Y * or that are pointers to any type publicly and
962 unambiguously derived from Y. Otherwise a function only allows exceptions
963 that have the same type ..."
964 This does not mention cv qualifiers and is different to what throw
965 [except.throw] and catch [except.catch] will do. They will ignore the
966 top level cv qualifiers, and allow qualifiers in the pointer to class
967 example.
969 We implement the letter of the standard. */
971 static bool
972 comp_except_types (tree a, tree b, bool exact)
974 if (same_type_p (a, b))
975 return true;
976 else if (!exact)
978 if (cp_type_quals (a) || cp_type_quals (b))
979 return false;
981 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
983 a = TREE_TYPE (a);
984 b = TREE_TYPE (b);
985 if (cp_type_quals (a) || cp_type_quals (b))
986 return false;
989 if (TREE_CODE (a) != RECORD_TYPE
990 || TREE_CODE (b) != RECORD_TYPE)
991 return false;
993 if (publicly_uniquely_derived_p (a, b))
994 return true;
996 return false;
999 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1000 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1001 If EXACT is ce_type, the C++17 type compatibility rules apply.
1002 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1003 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1004 are unordered, but we've already filtered out duplicates. Most lists will
1005 be in order, we should try to make use of that. */
1007 bool
1008 comp_except_specs (const_tree t1, const_tree t2, int exact)
1010 const_tree probe;
1011 const_tree base;
1012 int length = 0;
1014 if (t1 == t2)
1015 return true;
1017 /* First handle noexcept. */
1018 if (exact < ce_exact)
1020 if (exact == ce_type
1021 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1022 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1023 return true;
1025 /* noexcept(false) is compatible with no exception-specification,
1026 and less strict than any spec. */
1027 if (t1 == noexcept_false_spec)
1028 return t2 == NULL_TREE || exact == ce_derived;
1029 /* Even a derived noexcept(false) is compatible with no
1030 exception-specification. */
1031 if (t2 == noexcept_false_spec)
1032 return t1 == NULL_TREE;
1034 /* Otherwise, if we aren't looking for an exact match, noexcept is
1035 equivalent to throw(). */
1036 if (t1 == noexcept_true_spec)
1037 t1 = empty_except_spec;
1038 if (t2 == noexcept_true_spec)
1039 t2 = empty_except_spec;
1042 /* If any noexcept is left, it is only comparable to itself;
1043 either we're looking for an exact match or we're redeclaring a
1044 template with dependent noexcept. */
1045 if ((t1 && TREE_PURPOSE (t1))
1046 || (t2 && TREE_PURPOSE (t2)))
1047 return (t1 && t2
1048 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1050 if (t1 == NULL_TREE) /* T1 is ... */
1051 return t2 == NULL_TREE || exact == ce_derived;
1052 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1053 return t2 != NULL_TREE && !TREE_VALUE (t2);
1054 if (t2 == NULL_TREE) /* T2 is ... */
1055 return false;
1056 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1057 return exact == ce_derived;
1059 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1060 Count how many we find, to determine exactness. For exact matching and
1061 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1062 O(nm). */
1063 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1065 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1067 tree a = TREE_VALUE (probe);
1068 tree b = TREE_VALUE (t2);
1070 if (comp_except_types (a, b, exact))
1072 if (probe == base && exact > ce_derived)
1073 base = TREE_CHAIN (probe);
1074 length++;
1075 break;
1078 if (probe == NULL_TREE)
1079 return false;
1081 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1084 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1085 [] can match [size]. */
1087 static bool
1088 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1090 tree d1;
1091 tree d2;
1092 tree max1, max2;
1094 if (t1 == t2)
1095 return true;
1097 /* The type of the array elements must be the same. */
1098 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1099 return false;
1101 d1 = TYPE_DOMAIN (t1);
1102 d2 = TYPE_DOMAIN (t2);
1104 if (d1 == d2)
1105 return true;
1107 /* If one of the arrays is dimensionless, and the other has a
1108 dimension, they are of different types. However, it is valid to
1109 write:
1111 extern int a[];
1112 int a[3];
1114 by [basic.link]:
1116 declarations for an array object can specify
1117 array types that differ by the presence or absence of a major
1118 array bound (_dcl.array_). */
1119 if (!d1 || !d2)
1120 return allow_redeclaration;
1122 /* Check that the dimensions are the same. */
1124 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1125 return false;
1126 max1 = TYPE_MAX_VALUE (d1);
1127 max2 = TYPE_MAX_VALUE (d2);
1129 if (!cp_tree_equal (max1, max2))
1130 return false;
1132 return true;
1135 /* Compare the relative position of T1 and T2 into their respective
1136 template parameter list.
1137 T1 and T2 must be template parameter types.
1138 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1140 static bool
1141 comp_template_parms_position (tree t1, tree t2)
1143 tree index1, index2;
1144 gcc_assert (t1 && t2
1145 && TREE_CODE (t1) == TREE_CODE (t2)
1146 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1147 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1148 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1150 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1151 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1153 /* Then compare their relative position. */
1154 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1155 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1156 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1157 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1158 return false;
1160 /* In C++14 we can end up comparing 'auto' to a normal template
1161 parameter. Don't confuse them. */
1162 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1163 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1165 return true;
1168 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1170 static bool
1171 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1173 t1 = TYPE_MAIN_VARIANT (t1);
1174 t2 = TYPE_MAIN_VARIANT (t2);
1176 if (TYPE_PTR_P (t1)
1177 && TYPE_PTR_P (t2))
1178 return true;
1180 /* The signedness of the parameter matters only when an integral
1181 type smaller than int is promoted to int, otherwise only the
1182 precision of the parameter matters.
1183 This check should make sure that the callee does not see
1184 undefined values in argument registers. */
1185 if (INTEGRAL_TYPE_P (t1)
1186 && INTEGRAL_TYPE_P (t2)
1187 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1188 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1189 || !targetm.calls.promote_prototypes (NULL_TREE)
1190 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1191 return true;
1193 return same_type_p (t1, t2);
1196 /* Check if a type cast between two function types can be considered safe. */
1198 static bool
1199 cxx_safe_function_type_cast_p (tree t1, tree t2)
1201 if (TREE_TYPE (t1) == void_type_node &&
1202 TYPE_ARG_TYPES (t1) == void_list_node)
1203 return true;
1205 if (TREE_TYPE (t2) == void_type_node &&
1206 TYPE_ARG_TYPES (t2) == void_list_node)
1207 return true;
1209 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1210 return false;
1212 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1213 t1 && t2;
1214 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1215 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1216 return false;
1218 return true;
1221 /* Subroutine in comptypes. */
1223 static bool
1224 structural_comptypes (tree t1, tree t2, int strict)
1226 if (t1 == t2)
1227 return true;
1229 /* Suppress errors caused by previously reported errors. */
1230 if (t1 == error_mark_node || t2 == error_mark_node)
1231 return false;
1233 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1235 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1236 current instantiation. */
1237 if (TREE_CODE (t1) == TYPENAME_TYPE)
1238 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1240 if (TREE_CODE (t2) == TYPENAME_TYPE)
1241 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1243 if (TYPE_PTRMEMFUNC_P (t1))
1244 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1245 if (TYPE_PTRMEMFUNC_P (t2))
1246 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1248 /* Different classes of types can't be compatible. */
1249 if (TREE_CODE (t1) != TREE_CODE (t2))
1250 return false;
1252 /* Qualifiers must match. For array types, we will check when we
1253 recur on the array element types. */
1254 if (TREE_CODE (t1) != ARRAY_TYPE
1255 && cp_type_quals (t1) != cp_type_quals (t2))
1256 return false;
1257 if (TREE_CODE (t1) == FUNCTION_TYPE
1258 && type_memfn_quals (t1) != type_memfn_quals (t2))
1259 return false;
1260 /* Need to check this before TYPE_MAIN_VARIANT.
1261 FIXME function qualifiers should really change the main variant. */
1262 if (TREE_CODE (t1) == FUNCTION_TYPE
1263 || TREE_CODE (t1) == METHOD_TYPE)
1265 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1266 return false;
1267 if (flag_noexcept_type
1268 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1269 TYPE_RAISES_EXCEPTIONS (t2),
1270 ce_type))
1271 return false;
1274 /* Allow for two different type nodes which have essentially the same
1275 definition. Note that we already checked for equality of the type
1276 qualifiers (just above). */
1278 if (TREE_CODE (t1) != ARRAY_TYPE
1279 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1280 return true;
1283 /* Compare the types. Break out if they could be the same. */
1284 switch (TREE_CODE (t1))
1286 case VOID_TYPE:
1287 case BOOLEAN_TYPE:
1288 /* All void and bool types are the same. */
1289 break;
1291 case INTEGER_TYPE:
1292 case FIXED_POINT_TYPE:
1293 case REAL_TYPE:
1294 /* With these nodes, we can't determine type equivalence by
1295 looking at what is stored in the nodes themselves, because
1296 two nodes might have different TYPE_MAIN_VARIANTs but still
1297 represent the same type. For example, wchar_t and int could
1298 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1299 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1300 and are distinct types. On the other hand, int and the
1301 following typedef
1303 typedef int INT __attribute((may_alias));
1305 have identical properties, different TYPE_MAIN_VARIANTs, but
1306 represent the same type. The canonical type system keeps
1307 track of equivalence in this case, so we fall back on it. */
1308 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1310 case TEMPLATE_TEMPLATE_PARM:
1311 case BOUND_TEMPLATE_TEMPLATE_PARM:
1312 if (!comp_template_parms_position (t1, t2))
1313 return false;
1314 if (!comp_template_parms
1315 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1316 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1317 return false;
1318 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1319 break;
1320 /* Don't check inheritance. */
1321 strict = COMPARE_STRICT;
1322 /* Fall through. */
1324 case RECORD_TYPE:
1325 case UNION_TYPE:
1326 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1327 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1328 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1329 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1330 break;
1332 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1333 break;
1334 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1335 break;
1337 return false;
1339 case OFFSET_TYPE:
1340 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1341 strict & ~COMPARE_REDECLARATION))
1342 return false;
1343 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1344 return false;
1345 break;
1347 case REFERENCE_TYPE:
1348 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1349 return false;
1350 /* fall through to checks for pointer types */
1351 gcc_fallthrough ();
1353 case POINTER_TYPE:
1354 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1355 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1356 return false;
1357 break;
1359 case METHOD_TYPE:
1360 case FUNCTION_TYPE:
1361 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1362 return false;
1363 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1364 return false;
1365 break;
1367 case ARRAY_TYPE:
1368 /* Target types must match incl. qualifiers. */
1369 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1370 return false;
1371 break;
1373 case TEMPLATE_TYPE_PARM:
1374 /* If T1 and T2 don't have the same relative position in their
1375 template parameters set, they can't be equal. */
1376 if (!comp_template_parms_position (t1, t2))
1377 return false;
1378 /* Constrained 'auto's are distinct from parms that don't have the same
1379 constraints. */
1380 if (!equivalent_placeholder_constraints (t1, t2))
1381 return false;
1382 break;
1384 case TYPENAME_TYPE:
1385 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1386 TYPENAME_TYPE_FULLNAME (t2)))
1387 return false;
1388 /* Qualifiers don't matter on scopes. */
1389 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1390 TYPE_CONTEXT (t2)))
1391 return false;
1392 break;
1394 case UNBOUND_CLASS_TEMPLATE:
1395 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1396 return false;
1397 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1398 return false;
1399 break;
1401 case COMPLEX_TYPE:
1402 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1403 return false;
1404 break;
1406 case VECTOR_TYPE:
1407 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1408 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1409 return false;
1410 break;
1412 case TYPE_PACK_EXPANSION:
1413 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1414 PACK_EXPANSION_PATTERN (t2))
1415 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1416 PACK_EXPANSION_EXTRA_ARGS (t2)));
1418 case DECLTYPE_TYPE:
1419 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1420 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1421 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1422 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1423 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1424 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1425 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1426 DECLTYPE_TYPE_EXPR (t2)))
1427 return false;
1428 break;
1430 case UNDERLYING_TYPE:
1431 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1432 UNDERLYING_TYPE_TYPE (t2));
1434 default:
1435 return false;
1438 /* If we get here, we know that from a target independent POV the
1439 types are the same. Make sure the target attributes are also
1440 the same. */
1441 return comp_type_attributes (t1, t2);
1444 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1445 is a bitwise-or of the COMPARE_* flags. */
1447 bool
1448 comptypes (tree t1, tree t2, int strict)
1450 if (strict == COMPARE_STRICT)
1452 if (t1 == t2)
1453 return true;
1455 if (t1 == error_mark_node || t2 == error_mark_node)
1456 return false;
1458 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1459 /* At least one of the types requires structural equality, so
1460 perform a deep check. */
1461 return structural_comptypes (t1, t2, strict);
1463 if (flag_checking && USE_CANONICAL_TYPES)
1465 bool result = structural_comptypes (t1, t2, strict);
1467 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1468 /* The two types are structurally equivalent, but their
1469 canonical types were different. This is a failure of the
1470 canonical type propagation code.*/
1471 internal_error
1472 ("canonical types differ for identical types %qT and %qT",
1473 t1, t2);
1474 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1475 /* Two types are structurally different, but the canonical
1476 types are the same. This means we were over-eager in
1477 assigning canonical types. */
1478 internal_error
1479 ("same canonical type node for different types %qT and %qT",
1480 t1, t2);
1482 return result;
1484 if (!flag_checking && USE_CANONICAL_TYPES)
1485 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1486 else
1487 return structural_comptypes (t1, t2, strict);
1489 else if (strict == COMPARE_STRUCTURAL)
1490 return structural_comptypes (t1, t2, COMPARE_STRICT);
1491 else
1492 return structural_comptypes (t1, t2, strict);
1495 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1496 top-level qualifiers. */
1498 bool
1499 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1501 if (type1 == error_mark_node || type2 == error_mark_node)
1502 return false;
1504 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1505 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1506 return same_type_p (type1, type2);
1509 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1511 bool
1512 at_least_as_qualified_p (const_tree type1, const_tree type2)
1514 int q1 = cp_type_quals (type1);
1515 int q2 = cp_type_quals (type2);
1517 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1518 return (q1 & q2) == q2;
1521 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1522 more cv-qualified that TYPE1, and 0 otherwise. */
1525 comp_cv_qualification (int q1, int q2)
1527 if (q1 == q2)
1528 return 0;
1530 if ((q1 & q2) == q2)
1531 return 1;
1532 else if ((q1 & q2) == q1)
1533 return -1;
1535 return 0;
1539 comp_cv_qualification (const_tree type1, const_tree type2)
1541 int q1 = cp_type_quals (type1);
1542 int q2 = cp_type_quals (type2);
1543 return comp_cv_qualification (q1, q2);
1546 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1547 subset of the cv-qualification signature of TYPE2, and the types
1548 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1551 comp_cv_qual_signature (tree type1, tree type2)
1553 if (comp_ptr_ttypes_real (type2, type1, -1))
1554 return 1;
1555 else if (comp_ptr_ttypes_real (type1, type2, -1))
1556 return -1;
1557 else
1558 return 0;
1561 /* Subroutines of `comptypes'. */
1563 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1564 equivalent in the sense that functions with those parameter types
1565 can have equivalent types. The two lists must be equivalent,
1566 element by element. */
1568 bool
1569 compparms (const_tree parms1, const_tree parms2)
1571 const_tree t1, t2;
1573 /* An unspecified parmlist matches any specified parmlist
1574 whose argument types don't need default promotions. */
1576 for (t1 = parms1, t2 = parms2;
1577 t1 || t2;
1578 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1580 /* If one parmlist is shorter than the other,
1581 they fail to match. */
1582 if (!t1 || !t2)
1583 return false;
1584 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1585 return false;
1587 return true;
1591 /* Process a sizeof or alignof expression where the operand is a
1592 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1593 or GNU (preferred alignment) semantics; it is ignored if op is
1594 SIZEOF_EXPR. */
1596 tree
1597 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool std_alignof,
1598 bool complain)
1600 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1601 if (type == error_mark_node)
1602 return error_mark_node;
1604 type = non_reference (type);
1605 if (TREE_CODE (type) == METHOD_TYPE)
1607 if (complain)
1609 pedwarn (input_location, OPT_Wpointer_arith,
1610 "invalid application of %qs to a member function",
1611 OVL_OP_INFO (false, op)->name);
1612 return size_one_node;
1614 else
1615 return error_mark_node;
1618 bool dependent_p = dependent_type_p (type);
1619 if (!dependent_p)
1620 complete_type (type);
1621 if (dependent_p
1622 /* VLA types will have a non-constant size. In the body of an
1623 uninstantiated template, we don't need to try to compute the
1624 value, because the sizeof expression is not an integral
1625 constant expression in that case. And, if we do try to
1626 compute the value, we'll likely end up with SAVE_EXPRs, which
1627 the template substitution machinery does not expect to see. */
1628 || (processing_template_decl
1629 && COMPLETE_TYPE_P (type)
1630 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1632 tree value = build_min (op, size_type_node, type);
1633 TREE_READONLY (value) = 1;
1634 if (op == ALIGNOF_EXPR && std_alignof)
1635 ALIGNOF_EXPR_STD_P (value) = true;
1636 return value;
1639 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1640 op == SIZEOF_EXPR, std_alignof,
1641 complain);
1644 /* Return the size of the type, without producing any warnings for
1645 types whose size cannot be taken. This routine should be used only
1646 in some other routine that has already produced a diagnostic about
1647 using the size of such a type. */
1648 tree
1649 cxx_sizeof_nowarn (tree type)
1651 if (TREE_CODE (type) == FUNCTION_TYPE
1652 || VOID_TYPE_P (type)
1653 || TREE_CODE (type) == ERROR_MARK)
1654 return size_one_node;
1655 else if (!COMPLETE_TYPE_P (type))
1656 return size_zero_node;
1657 else
1658 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false, false);
1661 /* Process a sizeof expression where the operand is an expression. */
1663 static tree
1664 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1666 if (e == error_mark_node)
1667 return error_mark_node;
1669 if (processing_template_decl)
1671 e = build_min (SIZEOF_EXPR, size_type_node, e);
1672 TREE_SIDE_EFFECTS (e) = 0;
1673 TREE_READONLY (e) = 1;
1675 return e;
1678 /* To get the size of a static data member declared as an array of
1679 unknown bound, we need to instantiate it. */
1680 if (VAR_P (e)
1681 && VAR_HAD_UNKNOWN_BOUND (e)
1682 && DECL_TEMPLATE_INSTANTIATION (e))
1683 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1685 if (TREE_CODE (e) == PARM_DECL
1686 && DECL_ARRAY_PARAMETER_P (e)
1687 && (complain & tf_warning))
1689 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1690 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1691 inform (DECL_SOURCE_LOCATION (e), "declared here");
1694 e = mark_type_use (e);
1696 if (bitfield_p (e))
1698 if (complain & tf_error)
1699 error ("invalid application of %<sizeof%> to a bit-field");
1700 else
1701 return error_mark_node;
1702 e = char_type_node;
1704 else if (is_overloaded_fn (e))
1706 if (complain & tf_error)
1707 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1708 "function type");
1709 else
1710 return error_mark_node;
1711 e = char_type_node;
1713 else if (type_unknown_p (e))
1715 if (complain & tf_error)
1716 cxx_incomplete_type_error (e, TREE_TYPE (e));
1717 else
1718 return error_mark_node;
1719 e = char_type_node;
1721 else
1722 e = TREE_TYPE (e);
1724 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, false, complain & tf_error);
1727 /* Implement the __alignof keyword: Return the minimum required
1728 alignment of E, measured in bytes. For VAR_DECL's and
1729 FIELD_DECL's return DECL_ALIGN (which can be set from an
1730 "aligned" __attribute__ specification). */
1732 static tree
1733 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1735 tree t;
1737 if (e == error_mark_node)
1738 return error_mark_node;
1740 if (processing_template_decl)
1742 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1743 TREE_SIDE_EFFECTS (e) = 0;
1744 TREE_READONLY (e) = 1;
1746 return e;
1749 e = mark_type_use (e);
1751 if (VAR_P (e))
1752 t = size_int (DECL_ALIGN_UNIT (e));
1753 else if (bitfield_p (e))
1755 if (complain & tf_error)
1756 error ("invalid application of %<__alignof%> to a bit-field");
1757 else
1758 return error_mark_node;
1759 t = size_one_node;
1761 else if (TREE_CODE (e) == COMPONENT_REF
1762 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1763 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1764 else if (is_overloaded_fn (e))
1766 if (complain & tf_error)
1767 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1768 "function type");
1769 else
1770 return error_mark_node;
1771 if (TREE_CODE (e) == FUNCTION_DECL)
1772 t = size_int (DECL_ALIGN_UNIT (e));
1773 else
1774 t = size_one_node;
1776 else if (type_unknown_p (e))
1778 if (complain & tf_error)
1779 cxx_incomplete_type_error (e, TREE_TYPE (e));
1780 else
1781 return error_mark_node;
1782 t = size_one_node;
1784 else
1785 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, false,
1786 complain & tf_error);
1788 return fold_convert (size_type_node, t);
1791 /* Process a sizeof or alignof expression E with code OP where the operand
1792 is an expression. */
1794 tree
1795 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1797 if (op == SIZEOF_EXPR)
1798 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1799 else
1800 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1803 /* Build a representation of an expression 'alignas(E).' Return the
1804 folded integer value of E if it is an integral constant expression
1805 that resolves to a valid alignment. If E depends on a template
1806 parameter, return a syntactic representation tree of kind
1807 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1808 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1810 tree
1811 cxx_alignas_expr (tree e)
1813 if (e == NULL_TREE || e == error_mark_node
1814 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1815 return e;
1817 if (TYPE_P (e))
1818 /* [dcl.align]/3:
1820 When the alignment-specifier is of the form
1821 alignas(type-id ), it shall have the same effect as
1822 alignas(alignof(type-id )). */
1824 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, true, false);
1826 /* If we reach this point, it means the alignas expression if of
1827 the form "alignas(assignment-expression)", so we should follow
1828 what is stated by [dcl.align]/2. */
1830 if (value_dependent_expression_p (e))
1831 /* Leave value-dependent expression alone for now. */
1832 return e;
1834 e = instantiate_non_dependent_expr (e);
1835 e = mark_rvalue_use (e);
1837 /* [dcl.align]/2 says:
1839 the assignment-expression shall be an integral constant
1840 expression. */
1842 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1844 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1845 return error_mark_node;
1848 return cxx_constant_value (e);
1852 /* EXPR is being used in a context that is not a function call.
1853 Enforce:
1855 [expr.ref]
1857 The expression can be used only as the left-hand operand of a
1858 member function call.
1860 [expr.mptr.operator]
1862 If the result of .* or ->* is a function, then that result can be
1863 used only as the operand for the function call operator ().
1865 by issuing an error message if appropriate. Returns true iff EXPR
1866 violates these rules. */
1868 bool
1869 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1871 if (expr == NULL_TREE)
1872 return false;
1873 /* Don't enforce this in MS mode. */
1874 if (flag_ms_extensions)
1875 return false;
1876 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1877 expr = get_first_fn (expr);
1878 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1880 if (complain & tf_error)
1882 if (DECL_P (expr))
1884 error_at (loc, "invalid use of non-static member function %qD",
1885 expr);
1886 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1888 else
1889 error_at (loc, "invalid use of non-static member function of "
1890 "type %qT", TREE_TYPE (expr));
1892 return true;
1894 return false;
1897 /* If EXP is a reference to a bitfield, and the type of EXP does not
1898 match the declared type of the bitfield, return the declared type
1899 of the bitfield. Otherwise, return NULL_TREE. */
1901 tree
1902 is_bitfield_expr_with_lowered_type (const_tree exp)
1904 switch (TREE_CODE (exp))
1906 case COND_EXPR:
1907 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1908 ? TREE_OPERAND (exp, 1)
1909 : TREE_OPERAND (exp, 0)))
1910 return NULL_TREE;
1911 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1913 case COMPOUND_EXPR:
1914 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1916 case MODIFY_EXPR:
1917 case SAVE_EXPR:
1918 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1920 case COMPONENT_REF:
1922 tree field;
1924 field = TREE_OPERAND (exp, 1);
1925 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1926 return NULL_TREE;
1927 if (same_type_ignoring_top_level_qualifiers_p
1928 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1929 return NULL_TREE;
1930 return DECL_BIT_FIELD_TYPE (field);
1933 case VAR_DECL:
1934 if (DECL_HAS_VALUE_EXPR_P (exp))
1935 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1936 (CONST_CAST_TREE (exp)));
1937 return NULL_TREE;
1939 CASE_CONVERT:
1940 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1941 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1942 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1943 /* Fallthrough. */
1945 default:
1946 return NULL_TREE;
1950 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1951 bitfield with a lowered type, the type of EXP is returned, rather
1952 than NULL_TREE. */
1954 tree
1955 unlowered_expr_type (const_tree exp)
1957 tree type;
1958 tree etype = TREE_TYPE (exp);
1960 type = is_bitfield_expr_with_lowered_type (exp);
1961 if (type)
1962 type = cp_build_qualified_type (type, cp_type_quals (etype));
1963 else
1964 type = etype;
1966 return type;
1969 /* Perform the conversions in [expr] that apply when an lvalue appears
1970 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1971 function-to-pointer conversions. In addition, bitfield references are
1972 converted to their declared types. Note that this function does not perform
1973 the lvalue-to-rvalue conversion for class types. If you need that conversion
1974 for class types, then you probably need to use force_rvalue.
1976 Although the returned value is being used as an rvalue, this
1977 function does not wrap the returned expression in a
1978 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1979 that the return value is no longer an lvalue. */
1981 tree
1982 decay_conversion (tree exp,
1983 tsubst_flags_t complain,
1984 bool reject_builtin /* = true */)
1986 tree type;
1987 enum tree_code code;
1988 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1990 type = TREE_TYPE (exp);
1991 if (type == error_mark_node)
1992 return error_mark_node;
1994 exp = resolve_nondeduced_context (exp, complain);
1995 if (type_unknown_p (exp))
1997 if (complain & tf_error)
1998 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1999 return error_mark_node;
2002 code = TREE_CODE (type);
2004 if (error_operand_p (exp))
2005 return error_mark_node;
2007 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2009 mark_rvalue_use (exp, loc, reject_builtin);
2010 return nullptr_node;
2013 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2014 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2015 if (code == VOID_TYPE)
2017 if (complain & tf_error)
2018 error_at (loc, "void value not ignored as it ought to be");
2019 return error_mark_node;
2021 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2022 return error_mark_node;
2023 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2025 exp = mark_lvalue_use (exp);
2026 if (reject_builtin && reject_gcc_builtin (exp, loc))
2027 return error_mark_node;
2028 return cp_build_addr_expr (exp, complain);
2030 if (code == ARRAY_TYPE)
2032 tree adr;
2033 tree ptrtype;
2035 exp = mark_lvalue_use (exp);
2037 if (INDIRECT_REF_P (exp))
2038 return build_nop (build_pointer_type (TREE_TYPE (type)),
2039 TREE_OPERAND (exp, 0));
2041 if (TREE_CODE (exp) == COMPOUND_EXPR)
2043 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2044 if (op1 == error_mark_node)
2045 return error_mark_node;
2046 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2047 TREE_OPERAND (exp, 0), op1);
2050 if (!obvalue_p (exp)
2051 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2053 if (complain & tf_error)
2054 error_at (loc, "invalid use of non-lvalue array");
2055 return error_mark_node;
2058 /* Don't let an array compound literal decay to a pointer. It can
2059 still be used to initialize an array or bind to a reference. */
2060 if (TREE_CODE (exp) == TARGET_EXPR)
2062 if (complain & tf_error)
2063 error_at (loc, "taking address of temporary array");
2064 return error_mark_node;
2067 ptrtype = build_pointer_type (TREE_TYPE (type));
2069 if (VAR_P (exp))
2071 if (!cxx_mark_addressable (exp))
2072 return error_mark_node;
2073 adr = build_nop (ptrtype, build_address (exp));
2074 return adr;
2076 /* This way is better for a COMPONENT_REF since it can
2077 simplify the offset for a component. */
2078 adr = cp_build_addr_expr (exp, complain);
2079 return cp_convert (ptrtype, adr, complain);
2082 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2083 exp = mark_rvalue_use (exp, loc, reject_builtin);
2085 /* If a bitfield is used in a context where integral promotion
2086 applies, then the caller is expected to have used
2087 default_conversion. That function promotes bitfields correctly
2088 before calling this function. At this point, if we have a
2089 bitfield referenced, we may assume that is not subject to
2090 promotion, and that, therefore, the type of the resulting rvalue
2091 is the declared type of the bitfield. */
2092 exp = convert_bitfield_to_declared_type (exp);
2094 /* We do not call rvalue() here because we do not want to wrap EXP
2095 in a NON_LVALUE_EXPR. */
2097 /* [basic.lval]
2099 Non-class rvalues always have cv-unqualified types. */
2100 type = TREE_TYPE (exp);
2101 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2102 exp = build_nop (cv_unqualified (type), exp);
2104 if (!complete_type_or_maybe_complain (type, exp, complain))
2105 return error_mark_node;
2107 return exp;
2110 /* Perform preparatory conversions, as part of the "usual arithmetic
2111 conversions". In particular, as per [expr]:
2113 Whenever an lvalue expression appears as an operand of an
2114 operator that expects the rvalue for that operand, the
2115 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2116 standard conversions are applied to convert the expression to an
2117 rvalue.
2119 In addition, we perform integral promotions here, as those are
2120 applied to both operands to a binary operator before determining
2121 what additional conversions should apply. */
2123 static tree
2124 cp_default_conversion (tree exp, tsubst_flags_t complain)
2126 /* Check for target-specific promotions. */
2127 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2128 if (promoted_type)
2129 exp = cp_convert (promoted_type, exp, complain);
2130 /* Perform the integral promotions first so that bitfield
2131 expressions (which may promote to "int", even if the bitfield is
2132 declared "unsigned") are promoted correctly. */
2133 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2134 exp = cp_perform_integral_promotions (exp, complain);
2135 /* Perform the other conversions. */
2136 exp = decay_conversion (exp, complain);
2138 return exp;
2141 /* C version. */
2143 tree
2144 default_conversion (tree exp)
2146 return cp_default_conversion (exp, tf_warning_or_error);
2149 /* EXPR is an expression with an integral or enumeration type.
2150 Perform the integral promotions in [conv.prom], and return the
2151 converted value. */
2153 tree
2154 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2156 tree type;
2157 tree promoted_type;
2159 expr = mark_rvalue_use (expr);
2160 if (error_operand_p (expr))
2161 return error_mark_node;
2163 /* [conv.prom]
2165 If the bitfield has an enumerated type, it is treated as any
2166 other value of that type for promotion purposes. */
2167 type = is_bitfield_expr_with_lowered_type (expr);
2168 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2169 type = TREE_TYPE (expr);
2170 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2171 /* Scoped enums don't promote. */
2172 if (SCOPED_ENUM_P (type))
2173 return expr;
2174 promoted_type = type_promotes_to (type);
2175 if (type != promoted_type)
2176 expr = cp_convert (promoted_type, expr, complain);
2177 return expr;
2180 /* C version. */
2182 tree
2183 perform_integral_promotions (tree expr)
2185 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2188 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2189 decay_conversion to one. */
2192 string_conv_p (const_tree totype, const_tree exp, int warn)
2194 tree t;
2196 if (!TYPE_PTR_P (totype))
2197 return 0;
2199 t = TREE_TYPE (totype);
2200 if (!same_type_p (t, char_type_node)
2201 && !same_type_p (t, char16_type_node)
2202 && !same_type_p (t, char32_type_node)
2203 && !same_type_p (t, wchar_type_node))
2204 return 0;
2206 STRIP_ANY_LOCATION_WRAPPER (exp);
2208 if (TREE_CODE (exp) == STRING_CST)
2210 /* Make sure that we don't try to convert between char and wide chars. */
2211 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2212 return 0;
2214 else
2216 /* Is this a string constant which has decayed to 'const char *'? */
2217 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2218 if (!same_type_p (TREE_TYPE (exp), t))
2219 return 0;
2220 STRIP_NOPS (exp);
2221 if (TREE_CODE (exp) != ADDR_EXPR
2222 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2223 return 0;
2225 if (warn)
2227 if (cxx_dialect >= cxx11)
2228 pedwarn (input_location, OPT_Wwrite_strings,
2229 "ISO C++ forbids converting a string constant to %qT",
2230 totype);
2231 else
2232 warning (OPT_Wwrite_strings,
2233 "deprecated conversion from string constant to %qT",
2234 totype);
2237 return 1;
2240 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2241 can, for example, use as an lvalue. This code used to be in
2242 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2243 expressions, where we're dealing with aggregates. But now it's again only
2244 called from unary_complex_lvalue. The case (in particular) that led to
2245 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2246 get it there. */
2248 static tree
2249 rationalize_conditional_expr (enum tree_code code, tree t,
2250 tsubst_flags_t complain)
2252 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2254 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2255 the first operand is always the one to be used if both operands
2256 are equal, so we know what conditional expression this used to be. */
2257 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2259 tree op0 = TREE_OPERAND (t, 0);
2260 tree op1 = TREE_OPERAND (t, 1);
2262 /* The following code is incorrect if either operand side-effects. */
2263 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2264 && !TREE_SIDE_EFFECTS (op1));
2265 return
2266 build_conditional_expr (loc,
2267 build_x_binary_op (loc,
2268 (TREE_CODE (t) == MIN_EXPR
2269 ? LE_EXPR : GE_EXPR),
2270 op0, TREE_CODE (op0),
2271 op1, TREE_CODE (op1),
2272 /*overload=*/NULL,
2273 complain),
2274 cp_build_unary_op (code, op0, false, complain),
2275 cp_build_unary_op (code, op1, false, complain),
2276 complain);
2279 return
2280 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2281 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2282 complain),
2283 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2284 complain),
2285 complain);
2288 /* Given the TYPE of an anonymous union field inside T, return the
2289 FIELD_DECL for the field. If not found return NULL_TREE. Because
2290 anonymous unions can nest, we must also search all anonymous unions
2291 that are directly reachable. */
2293 tree
2294 lookup_anon_field (tree t, tree type)
2296 tree field;
2298 t = TYPE_MAIN_VARIANT (t);
2300 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2302 if (TREE_STATIC (field))
2303 continue;
2304 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2305 continue;
2307 /* If we find it directly, return the field. */
2308 if (DECL_NAME (field) == NULL_TREE
2309 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2311 return field;
2314 /* Otherwise, it could be nested, search harder. */
2315 if (DECL_NAME (field) == NULL_TREE
2316 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2318 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2319 if (subfield)
2320 return subfield;
2323 return NULL_TREE;
2326 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2327 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2328 non-NULL, it indicates the path to the base used to name MEMBER.
2329 If PRESERVE_REFERENCE is true, the expression returned will have
2330 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2331 returned will have the type referred to by the reference.
2333 This function does not perform access control; that is either done
2334 earlier by the parser when the name of MEMBER is resolved to MEMBER
2335 itself, or later when overload resolution selects one of the
2336 functions indicated by MEMBER. */
2338 tree
2339 build_class_member_access_expr (cp_expr object, tree member,
2340 tree access_path, bool preserve_reference,
2341 tsubst_flags_t complain)
2343 tree object_type;
2344 tree member_scope;
2345 tree result = NULL_TREE;
2346 tree using_decl = NULL_TREE;
2348 if (error_operand_p (object) || error_operand_p (member))
2349 return error_mark_node;
2351 gcc_assert (DECL_P (member) || BASELINK_P (member));
2353 /* [expr.ref]
2355 The type of the first expression shall be "class object" (of a
2356 complete type). */
2357 object_type = TREE_TYPE (object);
2358 if (!currently_open_class (object_type)
2359 && !complete_type_or_maybe_complain (object_type, object, complain))
2360 return error_mark_node;
2361 if (!CLASS_TYPE_P (object_type))
2363 if (complain & tf_error)
2365 if (INDIRECT_TYPE_P (object_type)
2366 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2367 error ("request for member %qD in %qE, which is of pointer "
2368 "type %qT (maybe you meant to use %<->%> ?)",
2369 member, object.get_value (), object_type);
2370 else
2371 error ("request for member %qD in %qE, which is of non-class "
2372 "type %qT", member, object.get_value (), object_type);
2374 return error_mark_node;
2377 /* The standard does not seem to actually say that MEMBER must be a
2378 member of OBJECT_TYPE. However, that is clearly what is
2379 intended. */
2380 if (DECL_P (member))
2382 member_scope = DECL_CLASS_CONTEXT (member);
2383 if (!mark_used (member, complain) && !(complain & tf_error))
2384 return error_mark_node;
2385 if (TREE_DEPRECATED (member))
2386 warn_deprecated_use (member, NULL_TREE);
2388 else
2389 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2390 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2391 presently be the anonymous union. Go outwards until we find a
2392 type related to OBJECT_TYPE. */
2393 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2394 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2395 object_type))
2396 member_scope = TYPE_CONTEXT (member_scope);
2397 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2399 if (complain & tf_error)
2401 if (TREE_CODE (member) == FIELD_DECL)
2402 error ("invalid use of nonstatic data member %qE", member);
2403 else
2404 error ("%qD is not a member of %qT", member, object_type);
2406 return error_mark_node;
2409 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2410 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2411 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2413 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2414 if (temp)
2415 object = cp_build_fold_indirect_ref (temp);
2418 /* In [expr.ref], there is an explicit list of the valid choices for
2419 MEMBER. We check for each of those cases here. */
2420 if (VAR_P (member))
2422 /* A static data member. */
2423 result = member;
2424 mark_exp_read (object);
2425 /* If OBJECT has side-effects, they are supposed to occur. */
2426 if (TREE_SIDE_EFFECTS (object))
2427 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2429 else if (TREE_CODE (member) == FIELD_DECL)
2431 /* A non-static data member. */
2432 bool null_object_p;
2433 int type_quals;
2434 tree member_type;
2436 if (INDIRECT_REF_P (object))
2437 null_object_p =
2438 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2439 else
2440 null_object_p = false;
2442 /* Convert OBJECT to the type of MEMBER. */
2443 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2444 TYPE_MAIN_VARIANT (member_scope)))
2446 tree binfo;
2447 base_kind kind;
2449 binfo = lookup_base (access_path ? access_path : object_type,
2450 member_scope, ba_unique, &kind, complain);
2451 if (binfo == error_mark_node)
2452 return error_mark_node;
2454 /* It is invalid to try to get to a virtual base of a
2455 NULL object. The most common cause is invalid use of
2456 offsetof macro. */
2457 if (null_object_p && kind == bk_via_virtual)
2459 if (complain & tf_error)
2461 error ("invalid access to non-static data member %qD in "
2462 "virtual base of NULL object", member);
2464 return error_mark_node;
2467 /* Convert to the base. */
2468 object = build_base_path (PLUS_EXPR, object, binfo,
2469 /*nonnull=*/1, complain);
2470 /* If we found the base successfully then we should be able
2471 to convert to it successfully. */
2472 gcc_assert (object != error_mark_node);
2475 /* If MEMBER is from an anonymous aggregate, we have converted
2476 OBJECT so that it refers to the class containing the
2477 anonymous union. Generate a reference to the anonymous union
2478 itself, and recur to find MEMBER. */
2479 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2480 /* When this code is called from build_field_call, the
2481 object already has the type of the anonymous union.
2482 That is because the COMPONENT_REF was already
2483 constructed, and was then disassembled before calling
2484 build_field_call. After the function-call code is
2485 cleaned up, this waste can be eliminated. */
2486 && (!same_type_ignoring_top_level_qualifiers_p
2487 (TREE_TYPE (object), DECL_CONTEXT (member))))
2489 tree anonymous_union;
2491 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2492 DECL_CONTEXT (member));
2493 object = build_class_member_access_expr (object,
2494 anonymous_union,
2495 /*access_path=*/NULL_TREE,
2496 preserve_reference,
2497 complain);
2500 /* Compute the type of the field, as described in [expr.ref]. */
2501 type_quals = TYPE_UNQUALIFIED;
2502 member_type = TREE_TYPE (member);
2503 if (!TYPE_REF_P (member_type))
2505 type_quals = (cp_type_quals (member_type)
2506 | cp_type_quals (object_type));
2508 /* A field is const (volatile) if the enclosing object, or the
2509 field itself, is const (volatile). But, a mutable field is
2510 not const, even within a const object. */
2511 if (DECL_MUTABLE_P (member))
2512 type_quals &= ~TYPE_QUAL_CONST;
2513 member_type = cp_build_qualified_type (member_type, type_quals);
2516 result = build3_loc (input_location, COMPONENT_REF, member_type,
2517 object, member, NULL_TREE);
2519 /* Mark the expression const or volatile, as appropriate. Even
2520 though we've dealt with the type above, we still have to mark the
2521 expression itself. */
2522 if (type_quals & TYPE_QUAL_CONST)
2523 TREE_READONLY (result) = 1;
2524 if (type_quals & TYPE_QUAL_VOLATILE)
2525 TREE_THIS_VOLATILE (result) = 1;
2527 else if (BASELINK_P (member))
2529 /* The member is a (possibly overloaded) member function. */
2530 tree functions;
2531 tree type;
2533 /* If the MEMBER is exactly one static member function, then we
2534 know the type of the expression. Otherwise, we must wait
2535 until overload resolution has been performed. */
2536 functions = BASELINK_FUNCTIONS (member);
2537 if (TREE_CODE (functions) == FUNCTION_DECL
2538 && DECL_STATIC_FUNCTION_P (functions))
2539 type = TREE_TYPE (functions);
2540 else
2541 type = unknown_type_node;
2542 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2543 base. That will happen when the function is called. */
2544 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2546 else if (TREE_CODE (member) == CONST_DECL)
2548 /* The member is an enumerator. */
2549 result = member;
2550 /* If OBJECT has side-effects, they are supposed to occur. */
2551 if (TREE_SIDE_EFFECTS (object))
2552 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2553 object, result);
2555 else if ((using_decl = strip_using_decl (member)) != member)
2556 result = build_class_member_access_expr (object,
2557 using_decl,
2558 access_path, preserve_reference,
2559 complain);
2560 else
2562 if (complain & tf_error)
2563 error ("invalid use of %qD", member);
2564 return error_mark_node;
2567 if (!preserve_reference)
2568 /* [expr.ref]
2570 If E2 is declared to have type "reference to T", then ... the
2571 type of E1.E2 is T. */
2572 result = convert_from_reference (result);
2574 return result;
2577 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2578 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2580 static tree
2581 lookup_destructor (tree object, tree scope, tree dtor_name,
2582 tsubst_flags_t complain)
2584 tree object_type = TREE_TYPE (object);
2585 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2586 tree expr;
2588 /* We've already complained about this destructor. */
2589 if (dtor_type == error_mark_node)
2590 return error_mark_node;
2592 if (scope && !check_dtor_name (scope, dtor_type))
2594 if (complain & tf_error)
2595 error ("qualified type %qT does not match destructor name ~%qT",
2596 scope, dtor_type);
2597 return error_mark_node;
2599 if (is_auto (dtor_type))
2600 dtor_type = object_type;
2601 else if (identifier_p (dtor_type))
2603 /* In a template, names we can't find a match for are still accepted
2604 destructor names, and we check them here. */
2605 if (check_dtor_name (object_type, dtor_type))
2606 dtor_type = object_type;
2607 else
2609 if (complain & tf_error)
2610 error ("object type %qT does not match destructor name ~%qT",
2611 object_type, dtor_type);
2612 return error_mark_node;
2616 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2618 if (complain & tf_error)
2619 error ("the type being destroyed is %qT, but the destructor "
2620 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2621 return error_mark_node;
2623 expr = lookup_member (dtor_type, complete_dtor_identifier,
2624 /*protect=*/1, /*want_type=*/false,
2625 tf_warning_or_error);
2626 if (!expr)
2628 if (complain & tf_error)
2629 cxx_incomplete_type_error (dtor_name, dtor_type);
2630 return error_mark_node;
2632 expr = (adjust_result_of_qualified_name_lookup
2633 (expr, dtor_type, object_type));
2634 if (scope == NULL_TREE)
2635 /* We need to call adjust_result_of_qualified_name_lookup in case the
2636 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2637 that we still get virtual function binding. */
2638 BASELINK_QUALIFIED_P (expr) = false;
2639 return expr;
2642 /* An expression of the form "A::template B" has been resolved to
2643 DECL. Issue a diagnostic if B is not a template or template
2644 specialization. */
2646 void
2647 check_template_keyword (tree decl)
2649 /* The standard says:
2651 [temp.names]
2653 If a name prefixed by the keyword template is not a member
2654 template, the program is ill-formed.
2656 DR 228 removed the restriction that the template be a member
2657 template.
2659 DR 96, if accepted would add the further restriction that explicit
2660 template arguments must be provided if the template keyword is
2661 used, but, as of 2005-10-16, that DR is still in "drafting". If
2662 this DR is accepted, then the semantic checks here can be
2663 simplified, as the entity named must in fact be a template
2664 specialization, rather than, as at present, a set of overloaded
2665 functions containing at least one template function. */
2666 if (TREE_CODE (decl) != TEMPLATE_DECL
2667 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2669 if (VAR_P (decl))
2671 if (DECL_USE_TEMPLATE (decl)
2672 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2674 else
2675 permerror (input_location, "%qD is not a template", decl);
2677 else if (!is_overloaded_fn (decl))
2678 permerror (input_location, "%qD is not a template", decl);
2679 else
2681 bool found = false;
2683 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2684 !found && iter; ++iter)
2686 tree fn = *iter;
2687 if (TREE_CODE (fn) == TEMPLATE_DECL
2688 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2689 || (TREE_CODE (fn) == FUNCTION_DECL
2690 && DECL_USE_TEMPLATE (fn)
2691 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2692 found = true;
2694 if (!found)
2695 permerror (input_location, "%qD is not a template", decl);
2700 /* Record that an access failure occurred on BASETYPE_PATH attempting
2701 to access FIELD_DECL. */
2703 void
2704 access_failure_info::record_access_failure (tree basetype_path,
2705 tree field_decl)
2707 m_was_inaccessible = true;
2708 m_basetype_path = basetype_path;
2709 m_field_decl = field_decl;
2712 /* If an access failure was recorded, then attempt to locate an
2713 accessor function for the pertinent field, and if one is
2714 available, add a note and fix-it hint suggesting using it. */
2716 void
2717 access_failure_info::maybe_suggest_accessor (bool const_p) const
2719 if (!m_was_inaccessible)
2720 return;
2722 tree accessor
2723 = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2724 if (!accessor)
2725 return;
2727 /* The accessor must itself be accessible for it to be a reasonable
2728 suggestion. */
2729 if (!accessible_p (m_basetype_path, accessor, true))
2730 return;
2732 rich_location richloc (line_table, input_location);
2733 pretty_printer pp;
2734 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2735 richloc.add_fixit_replace (pp_formatted_text (&pp));
2736 inform (&richloc, "field %q#D can be accessed via %q#D",
2737 m_field_decl, accessor);
2740 /* This function is called by the parser to process a class member
2741 access expression of the form OBJECT.NAME. NAME is a node used by
2742 the parser to represent a name; it is not yet a DECL. It may,
2743 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2744 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2745 there is no reason to do the lookup twice, so the parser keeps the
2746 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2747 be a template via the use of the "A::template B" syntax. */
2749 tree
2750 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2751 tsubst_flags_t complain)
2753 tree expr;
2754 tree object_type;
2755 tree member;
2756 tree access_path = NULL_TREE;
2757 tree orig_object = object;
2758 tree orig_name = name;
2760 if (object == error_mark_node || name == error_mark_node)
2761 return error_mark_node;
2763 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2764 if (!objc_is_public (object, name))
2765 return error_mark_node;
2767 object_type = TREE_TYPE (object);
2769 if (processing_template_decl)
2771 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2772 type_dependent_object_expression_p (object)
2773 /* If NAME is "f<args>", where either 'f' or 'args' is
2774 dependent, then the expression is dependent. */
2775 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2776 && dependent_template_id_p (TREE_OPERAND (name, 0),
2777 TREE_OPERAND (name, 1)))
2778 /* If NAME is "T::X" where "T" is dependent, then the
2779 expression is dependent. */
2780 || (TREE_CODE (name) == SCOPE_REF
2781 && TYPE_P (TREE_OPERAND (name, 0))
2782 && dependent_scope_p (TREE_OPERAND (name, 0))))
2784 dependent:
2785 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2786 orig_object, orig_name, NULL_TREE);
2788 object = build_non_dependent_expr (object);
2790 else if (c_dialect_objc ()
2791 && identifier_p (name)
2792 && (expr = objc_maybe_build_component_ref (object, name)))
2793 return expr;
2795 /* [expr.ref]
2797 The type of the first expression shall be "class object" (of a
2798 complete type). */
2799 if (!currently_open_class (object_type)
2800 && !complete_type_or_maybe_complain (object_type, object, complain))
2801 return error_mark_node;
2802 if (!CLASS_TYPE_P (object_type))
2804 if (complain & tf_error)
2806 if (INDIRECT_TYPE_P (object_type)
2807 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2808 error ("request for member %qD in %qE, which is of pointer "
2809 "type %qT (maybe you meant to use %<->%> ?)",
2810 name, object.get_value (), object_type);
2811 else
2812 error ("request for member %qD in %qE, which is of non-class "
2813 "type %qT", name, object.get_value (), object_type);
2815 return error_mark_node;
2818 if (BASELINK_P (name))
2819 /* A member function that has already been looked up. */
2820 member = name;
2821 else
2823 bool is_template_id = false;
2824 tree template_args = NULL_TREE;
2825 tree scope = NULL_TREE;
2827 access_path = object_type;
2829 if (TREE_CODE (name) == SCOPE_REF)
2831 /* A qualified name. The qualifying class or namespace `S'
2832 has already been looked up; it is either a TYPE or a
2833 NAMESPACE_DECL. */
2834 scope = TREE_OPERAND (name, 0);
2835 name = TREE_OPERAND (name, 1);
2837 /* If SCOPE is a namespace, then the qualified name does not
2838 name a member of OBJECT_TYPE. */
2839 if (TREE_CODE (scope) == NAMESPACE_DECL)
2841 if (complain & tf_error)
2842 error ("%<%D::%D%> is not a member of %qT",
2843 scope, name, object_type);
2844 return error_mark_node;
2848 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2850 is_template_id = true;
2851 template_args = TREE_OPERAND (name, 1);
2852 name = TREE_OPERAND (name, 0);
2854 if (!identifier_p (name))
2855 name = OVL_NAME (name);
2858 if (scope)
2860 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2862 gcc_assert (!is_template_id);
2863 /* Looking up a member enumerator (c++/56793). */
2864 if (!TYPE_CLASS_SCOPE_P (scope)
2865 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2867 if (complain & tf_error)
2868 error ("%<%D::%D%> is not a member of %qT",
2869 scope, name, object_type);
2870 return error_mark_node;
2872 tree val = lookup_enumerator (scope, name);
2873 if (!val)
2875 if (complain & tf_error)
2876 error ("%qD is not a member of %qD",
2877 name, scope);
2878 return error_mark_node;
2881 if (TREE_SIDE_EFFECTS (object))
2882 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2883 return val;
2886 gcc_assert (CLASS_TYPE_P (scope));
2887 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2889 if (constructor_name_p (name, scope))
2891 if (complain & tf_error)
2892 error ("cannot call constructor %<%T::%D%> directly",
2893 scope, name);
2894 return error_mark_node;
2897 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2898 access_path = lookup_base (object_type, scope, ba_check,
2899 NULL, complain);
2900 if (access_path == error_mark_node)
2901 return error_mark_node;
2902 if (!access_path)
2904 if (any_dependent_bases_p (object_type))
2905 goto dependent;
2906 if (complain & tf_error)
2907 error ("%qT is not a base of %qT", scope, object_type);
2908 return error_mark_node;
2912 if (TREE_CODE (name) == BIT_NOT_EXPR)
2914 if (dependent_type_p (object_type))
2915 /* The destructor isn't declared yet. */
2916 goto dependent;
2917 member = lookup_destructor (object, scope, name, complain);
2919 else
2921 /* Look up the member. */
2922 access_failure_info afi;
2923 member = lookup_member (access_path, name, /*protect=*/1,
2924 /*want_type=*/false, complain,
2925 &afi);
2926 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2927 if (member == NULL_TREE)
2929 if (dependent_type_p (object_type))
2930 /* Try again at instantiation time. */
2931 goto dependent;
2932 if (complain & tf_error)
2934 tree guessed_id = lookup_member_fuzzy (access_path, name,
2935 /*want_type=*/false);
2936 if (guessed_id)
2938 location_t bogus_component_loc = input_location;
2939 gcc_rich_location rich_loc (bogus_component_loc);
2940 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2941 guessed_id);
2942 error_at (&rich_loc,
2943 "%q#T has no member named %qE;"
2944 " did you mean %qE?",
2945 TREE_CODE (access_path) == TREE_BINFO
2946 ? TREE_TYPE (access_path) : object_type,
2947 name, guessed_id);
2949 else
2950 error ("%q#T has no member named %qE",
2951 TREE_CODE (access_path) == TREE_BINFO
2952 ? TREE_TYPE (access_path) : object_type, name);
2954 return error_mark_node;
2956 if (member == error_mark_node)
2957 return error_mark_node;
2958 if (DECL_P (member)
2959 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2960 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2961 wrong, so don't use it. */
2962 goto dependent;
2963 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2964 goto dependent;
2967 if (is_template_id)
2969 tree templ = member;
2971 if (BASELINK_P (templ))
2972 member = lookup_template_function (templ, template_args);
2973 else if (variable_template_p (templ))
2974 member = (lookup_and_finish_template_variable
2975 (templ, template_args, complain));
2976 else
2978 if (complain & tf_error)
2979 error ("%qD is not a member template function", name);
2980 return error_mark_node;
2985 if (TREE_DEPRECATED (member))
2986 warn_deprecated_use (member, NULL_TREE);
2988 if (template_p)
2989 check_template_keyword (member);
2991 expr = build_class_member_access_expr (object, member, access_path,
2992 /*preserve_reference=*/false,
2993 complain);
2994 if (processing_template_decl && expr != error_mark_node)
2996 if (BASELINK_P (member))
2998 if (TREE_CODE (orig_name) == SCOPE_REF)
2999 BASELINK_QUALIFIED_P (member) = 1;
3000 orig_name = member;
3002 return build_min_non_dep (COMPONENT_REF, expr,
3003 orig_object, orig_name,
3004 NULL_TREE);
3007 return expr;
3010 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3011 type. */
3013 tree
3014 build_simple_component_ref (tree object, tree member)
3016 tree type = cp_build_qualified_type (TREE_TYPE (member),
3017 cp_type_quals (TREE_TYPE (object)));
3018 return build3_loc (input_location,
3019 COMPONENT_REF, type,
3020 object, member, NULL_TREE);
3023 /* Return an expression for the MEMBER_NAME field in the internal
3024 representation of PTRMEM, a pointer-to-member function. (Each
3025 pointer-to-member function type gets its own RECORD_TYPE so it is
3026 more convenient to access the fields by name than by FIELD_DECL.)
3027 This routine converts the NAME to a FIELD_DECL and then creates the
3028 node for the complete expression. */
3030 tree
3031 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3033 tree ptrmem_type;
3034 tree member;
3036 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3038 unsigned int ix;
3039 tree index, value;
3040 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem),
3041 ix, index, value)
3042 if (index && DECL_P (index) && DECL_NAME (index) == member_name)
3043 return value;
3044 gcc_unreachable ();
3047 /* This code is a stripped down version of
3048 build_class_member_access_expr. It does not work to use that
3049 routine directly because it expects the object to be of class
3050 type. */
3051 ptrmem_type = TREE_TYPE (ptrmem);
3052 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3053 for (member = TYPE_FIELDS (ptrmem_type); member;
3054 member = DECL_CHAIN (member))
3055 if (DECL_NAME (member) == member_name)
3056 break;
3057 tree res = build_simple_component_ref (ptrmem, member);
3059 TREE_NO_WARNING (res) = 1;
3060 return res;
3063 /* Given an expression PTR for a pointer, return an expression
3064 for the value pointed to.
3065 ERRORSTRING is the name of the operator to appear in error messages.
3067 This function may need to overload OPERATOR_FNNAME.
3068 Must also handle REFERENCE_TYPEs for C++. */
3070 tree
3071 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3072 tsubst_flags_t complain)
3074 tree orig_expr = expr;
3075 tree rval;
3076 tree overload = NULL_TREE;
3078 if (processing_template_decl)
3080 /* Retain the type if we know the operand is a pointer. */
3081 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3082 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3083 if (type_dependent_expression_p (expr))
3084 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3085 expr = build_non_dependent_expr (expr);
3088 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3089 NULL_TREE, NULL_TREE, &overload, complain);
3090 if (!rval)
3091 rval = cp_build_indirect_ref (expr, errorstring, complain);
3093 if (processing_template_decl && rval != error_mark_node)
3095 if (overload != NULL_TREE)
3096 return (build_min_non_dep_op_overload
3097 (INDIRECT_REF, rval, overload, orig_expr));
3099 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3101 else
3102 return rval;
3105 /* The implementation of the above, and of indirection implied by other
3106 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3108 static tree
3109 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3110 tsubst_flags_t complain, bool do_fold)
3112 tree pointer, type;
3114 /* RO_NULL should only be used with the folding entry points below, not
3115 cp_build_indirect_ref. */
3116 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3118 if (ptr == current_class_ptr
3119 || (TREE_CODE (ptr) == NOP_EXPR
3120 && TREE_OPERAND (ptr, 0) == current_class_ptr
3121 && (same_type_ignoring_top_level_qualifiers_p
3122 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3123 return current_class_ref;
3125 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3126 ? ptr : decay_conversion (ptr, complain));
3127 if (pointer == error_mark_node)
3128 return error_mark_node;
3130 type = TREE_TYPE (pointer);
3132 if (INDIRECT_TYPE_P (type))
3134 /* [expr.unary.op]
3136 If the type of the expression is "pointer to T," the type
3137 of the result is "T." */
3138 tree t = TREE_TYPE (type);
3140 if ((CONVERT_EXPR_P (ptr)
3141 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3142 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3144 /* If a warning is issued, mark it to avoid duplicates from
3145 the backend. This only needs to be done at
3146 warn_strict_aliasing > 2. */
3147 if (warn_strict_aliasing > 2)
3148 if (strict_aliasing_warning (EXPR_LOCATION (ptr),
3149 type, TREE_OPERAND (ptr, 0)))
3150 TREE_NO_WARNING (ptr) = 1;
3153 if (VOID_TYPE_P (t))
3155 /* A pointer to incomplete type (other than cv void) can be
3156 dereferenced [expr.unary.op]/1 */
3157 if (complain & tf_error)
3158 error ("%qT is not a pointer-to-object type", type);
3159 return error_mark_node;
3161 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3162 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3163 /* The POINTER was something like `&x'. We simplify `*&x' to
3164 `x'. */
3165 return TREE_OPERAND (pointer, 0);
3166 else
3168 tree ref = build1 (INDIRECT_REF, t, pointer);
3170 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3171 so that we get the proper error message if the result is used
3172 to assign to. Also, &* is supposed to be a no-op. */
3173 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3174 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3175 TREE_SIDE_EFFECTS (ref)
3176 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3177 return ref;
3180 else if (!(complain & tf_error))
3181 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3183 /* `pointer' won't be an error_mark_node if we were given a
3184 pointer to member, so it's cool to check for this here. */
3185 else if (TYPE_PTRMEM_P (type))
3186 switch (errorstring)
3188 case RO_ARRAY_INDEXING:
3189 error ("invalid use of array indexing on pointer to member");
3190 break;
3191 case RO_UNARY_STAR:
3192 error ("invalid use of unary %<*%> on pointer to member");
3193 break;
3194 case RO_IMPLICIT_CONVERSION:
3195 error ("invalid use of implicit conversion on pointer to member");
3196 break;
3197 case RO_ARROW_STAR:
3198 error ("left hand operand of %<->*%> must be a pointer to class, "
3199 "but is a pointer to member of type %qT", type);
3200 break;
3201 default:
3202 gcc_unreachable ();
3204 else if (pointer != error_mark_node)
3205 invalid_indirection_error (input_location, type, errorstring);
3207 return error_mark_node;
3210 /* Entry point used by c-common, which expects folding. */
3212 tree
3213 build_indirect_ref (location_t /*loc*/,
3214 tree ptr, ref_operator errorstring)
3216 return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3219 /* Entry point used by internal indirection needs that don't correspond to any
3220 syntactic construct. */
3222 tree
3223 cp_build_fold_indirect_ref (tree pointer)
3225 return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3228 /* Entry point used by indirection needs that correspond to some syntactic
3229 construct. */
3231 tree
3232 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3233 tsubst_flags_t complain)
3235 return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3238 /* This handles expressions of the form "a[i]", which denotes
3239 an array reference.
3241 This is logically equivalent in C to *(a+i), but we may do it differently.
3242 If A is a variable or a member, we generate a primitive ARRAY_REF.
3243 This avoids forcing the array out of registers, and can work on
3244 arrays that are not lvalues (for example, members of structures returned
3245 by functions).
3247 If INDEX is of some user-defined type, it must be converted to
3248 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3249 will inherit the type of the array, which will be some pointer type.
3251 LOC is the location to use in building the array reference. */
3253 tree
3254 cp_build_array_ref (location_t loc, tree array, tree idx,
3255 tsubst_flags_t complain)
3257 tree ret;
3259 if (idx == 0)
3261 if (complain & tf_error)
3262 error_at (loc, "subscript missing in array reference");
3263 return error_mark_node;
3266 if (TREE_TYPE (array) == error_mark_node
3267 || TREE_TYPE (idx) == error_mark_node)
3268 return error_mark_node;
3270 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3271 inside it. */
3272 switch (TREE_CODE (array))
3274 case COMPOUND_EXPR:
3276 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3277 complain);
3278 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3279 TREE_OPERAND (array, 0), value);
3280 SET_EXPR_LOCATION (ret, loc);
3281 return ret;
3284 case COND_EXPR:
3285 ret = build_conditional_expr
3286 (loc, TREE_OPERAND (array, 0),
3287 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3288 complain),
3289 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3290 complain),
3291 complain);
3292 protected_set_expr_location (ret, loc);
3293 return ret;
3295 default:
3296 break;
3299 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3301 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3303 tree rval, type;
3305 warn_array_subscript_with_type_char (loc, idx);
3307 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3309 if (complain & tf_error)
3310 error_at (loc, "array subscript is not an integer");
3311 return error_mark_node;
3314 /* Apply integral promotions *after* noticing character types.
3315 (It is unclear why we do these promotions -- the standard
3316 does not say that we should. In fact, the natural thing would
3317 seem to be to convert IDX to ptrdiff_t; we're performing
3318 pointer arithmetic.) */
3319 idx = cp_perform_integral_promotions (idx, complain);
3321 /* An array that is indexed by a non-constant
3322 cannot be stored in a register; we must be able to do
3323 address arithmetic on its address.
3324 Likewise an array of elements of variable size. */
3325 if (TREE_CODE (idx) != INTEGER_CST
3326 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3327 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3328 != INTEGER_CST)))
3330 if (!cxx_mark_addressable (array, true))
3331 return error_mark_node;
3334 /* An array that is indexed by a constant value which is not within
3335 the array bounds cannot be stored in a register either; because we
3336 would get a crash in store_bit_field/extract_bit_field when trying
3337 to access a non-existent part of the register. */
3338 if (TREE_CODE (idx) == INTEGER_CST
3339 && TYPE_DOMAIN (TREE_TYPE (array))
3340 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3342 if (!cxx_mark_addressable (array))
3343 return error_mark_node;
3346 /* Note in C++ it is valid to subscript a `register' array, since
3347 it is valid to take the address of something with that
3348 storage specification. */
3349 if (extra_warnings)
3351 tree foo = array;
3352 while (TREE_CODE (foo) == COMPONENT_REF)
3353 foo = TREE_OPERAND (foo, 0);
3354 if (VAR_P (foo) && DECL_REGISTER (foo)
3355 && (complain & tf_warning))
3356 warning_at (loc, OPT_Wextra,
3357 "subscripting array declared %<register%>");
3360 type = TREE_TYPE (TREE_TYPE (array));
3361 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3362 /* Array ref is const/volatile if the array elements are
3363 or if the array is.. */
3364 TREE_READONLY (rval)
3365 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3366 TREE_SIDE_EFFECTS (rval)
3367 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3368 TREE_THIS_VOLATILE (rval)
3369 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3370 ret = require_complete_type_sfinae (rval, complain);
3371 protected_set_expr_location (ret, loc);
3372 if (non_lvalue)
3373 ret = non_lvalue_loc (loc, ret);
3374 return ret;
3378 tree ar = cp_default_conversion (array, complain);
3379 tree ind = cp_default_conversion (idx, complain);
3381 /* Put the integer in IND to simplify error checking. */
3382 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3383 std::swap (ar, ind);
3385 if (ar == error_mark_node || ind == error_mark_node)
3386 return error_mark_node;
3388 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3390 if (complain & tf_error)
3391 error_at (loc, "subscripted value is neither array nor pointer");
3392 return error_mark_node;
3394 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3396 if (complain & tf_error)
3397 error_at (loc, "array subscript is not an integer");
3398 return error_mark_node;
3401 warn_array_subscript_with_type_char (loc, idx);
3403 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3404 PLUS_EXPR, ar, ind,
3405 complain),
3406 RO_ARRAY_INDEXING,
3407 complain);
3408 protected_set_expr_location (ret, loc);
3409 if (non_lvalue)
3410 ret = non_lvalue_loc (loc, ret);
3411 return ret;
3415 /* Entry point for Obj-C++. */
3417 tree
3418 build_array_ref (location_t loc, tree array, tree idx)
3420 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3423 /* Resolve a pointer to member function. INSTANCE is the object
3424 instance to use, if the member points to a virtual member.
3426 This used to avoid checking for virtual functions if basetype
3427 has no virtual functions, according to an earlier ANSI draft.
3428 With the final ISO C++ rules, such an optimization is
3429 incorrect: A pointer to a derived member can be static_cast
3430 to pointer-to-base-member, as long as the dynamic object
3431 later has the right member. So now we only do this optimization
3432 when we know the dynamic type of the object. */
3434 tree
3435 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3436 tsubst_flags_t complain)
3438 if (TREE_CODE (function) == OFFSET_REF)
3439 function = TREE_OPERAND (function, 1);
3441 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3443 tree idx, delta, e1, e2, e3, vtbl;
3444 bool nonvirtual;
3445 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3446 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3448 tree instance_ptr = *instance_ptrptr;
3449 tree instance_save_expr = 0;
3450 if (instance_ptr == error_mark_node)
3452 if (TREE_CODE (function) == PTRMEM_CST)
3454 /* Extracting the function address from a pmf is only
3455 allowed with -Wno-pmf-conversions. It only works for
3456 pmf constants. */
3457 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3458 e1 = convert (fntype, e1);
3459 return e1;
3461 else
3463 if (complain & tf_error)
3464 error ("object missing in use of %qE", function);
3465 return error_mark_node;
3469 /* True if we know that the dynamic type of the object doesn't have
3470 virtual functions, so we can assume the PFN field is a pointer. */
3471 nonvirtual = (COMPLETE_TYPE_P (basetype)
3472 && !TYPE_POLYMORPHIC_P (basetype)
3473 && resolves_to_fixed_type_p (instance_ptr, 0));
3475 /* If we don't really have an object (i.e. in an ill-formed
3476 conversion from PMF to pointer), we can't resolve virtual
3477 functions anyway. */
3478 if (!nonvirtual && is_dummy_object (instance_ptr))
3479 nonvirtual = true;
3481 if (TREE_SIDE_EFFECTS (instance_ptr))
3482 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3484 if (TREE_SIDE_EFFECTS (function))
3485 function = save_expr (function);
3487 /* Start by extracting all the information from the PMF itself. */
3488 e3 = pfn_from_ptrmemfunc (function);
3489 delta = delta_from_ptrmemfunc (function);
3490 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3491 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3493 int flag_sanitize_save;
3494 case ptrmemfunc_vbit_in_pfn:
3495 e1 = cp_build_binary_op (input_location,
3496 BIT_AND_EXPR, idx, integer_one_node,
3497 complain);
3498 idx = cp_build_binary_op (input_location,
3499 MINUS_EXPR, idx, integer_one_node,
3500 complain);
3501 if (idx == error_mark_node)
3502 return error_mark_node;
3503 break;
3505 case ptrmemfunc_vbit_in_delta:
3506 e1 = cp_build_binary_op (input_location,
3507 BIT_AND_EXPR, delta, integer_one_node,
3508 complain);
3509 /* Don't instrument the RSHIFT_EXPR we're about to create because
3510 we're going to use DELTA number of times, and that wouldn't play
3511 well with SAVE_EXPRs therein. */
3512 flag_sanitize_save = flag_sanitize;
3513 flag_sanitize = 0;
3514 delta = cp_build_binary_op (input_location,
3515 RSHIFT_EXPR, delta, integer_one_node,
3516 complain);
3517 flag_sanitize = flag_sanitize_save;
3518 if (delta == error_mark_node)
3519 return error_mark_node;
3520 break;
3522 default:
3523 gcc_unreachable ();
3526 if (e1 == error_mark_node)
3527 return error_mark_node;
3529 /* Convert down to the right base before using the instance. A
3530 special case is that in a pointer to member of class C, C may
3531 be incomplete. In that case, the function will of course be
3532 a member of C, and no conversion is required. In fact,
3533 lookup_base will fail in that case, because incomplete
3534 classes do not have BINFOs. */
3535 if (!same_type_ignoring_top_level_qualifiers_p
3536 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3538 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3539 basetype, ba_check, NULL, complain);
3540 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3541 1, complain);
3542 if (instance_ptr == error_mark_node)
3543 return error_mark_node;
3545 /* ...and then the delta in the PMF. */
3546 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3548 /* Hand back the adjusted 'this' argument to our caller. */
3549 *instance_ptrptr = instance_ptr;
3551 if (nonvirtual)
3552 /* Now just return the pointer. */
3553 return e3;
3555 /* Next extract the vtable pointer from the object. */
3556 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3557 instance_ptr);
3558 vtbl = cp_build_fold_indirect_ref (vtbl);
3559 if (vtbl == error_mark_node)
3560 return error_mark_node;
3562 /* Finally, extract the function pointer from the vtable. */
3563 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3564 e2 = cp_build_fold_indirect_ref (e2);
3565 if (e2 == error_mark_node)
3566 return error_mark_node;
3567 TREE_CONSTANT (e2) = 1;
3569 /* When using function descriptors, the address of the
3570 vtable entry is treated as a function pointer. */
3571 if (TARGET_VTABLE_USES_DESCRIPTORS)
3572 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3573 cp_build_addr_expr (e2, complain));
3575 e2 = fold_convert (TREE_TYPE (e3), e2);
3576 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3577 if (e1 == error_mark_node)
3578 return error_mark_node;
3580 /* Make sure this doesn't get evaluated first inside one of the
3581 branches of the COND_EXPR. */
3582 if (instance_save_expr)
3583 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3584 instance_save_expr, e1);
3586 function = e1;
3588 return function;
3591 /* Used by the C-common bits. */
3592 tree
3593 build_function_call (location_t /*loc*/,
3594 tree function, tree params)
3596 return cp_build_function_call (function, params, tf_warning_or_error);
3599 /* Used by the C-common bits. */
3600 tree
3601 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3602 tree function, vec<tree, va_gc> *params,
3603 vec<tree, va_gc> * /*origtypes*/)
3605 vec<tree, va_gc> *orig_params = params;
3606 tree ret = cp_build_function_call_vec (function, &params,
3607 tf_warning_or_error);
3609 /* cp_build_function_call_vec can reallocate PARAMS by adding
3610 default arguments. That should never happen here. Verify
3611 that. */
3612 gcc_assert (params == orig_params);
3614 return ret;
3617 /* Build a function call using a tree list of arguments. */
3619 static tree
3620 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3622 vec<tree, va_gc> *vec;
3623 tree ret;
3625 vec = make_tree_vector ();
3626 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3627 vec_safe_push (vec, TREE_VALUE (params));
3628 ret = cp_build_function_call_vec (function, &vec, complain);
3629 release_tree_vector (vec);
3630 return ret;
3633 /* Build a function call using varargs. */
3635 tree
3636 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3638 vec<tree, va_gc> *vec;
3639 va_list args;
3640 tree ret, t;
3642 vec = make_tree_vector ();
3643 va_start (args, complain);
3644 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3645 vec_safe_push (vec, t);
3646 va_end (args);
3647 ret = cp_build_function_call_vec (function, &vec, complain);
3648 release_tree_vector (vec);
3649 return ret;
3652 /* Build a function call using a vector of arguments. PARAMS may be
3653 NULL if there are no parameters. This changes the contents of
3654 PARAMS. */
3656 tree
3657 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3658 tsubst_flags_t complain)
3660 tree fntype, fndecl;
3661 int is_method;
3662 tree original = function;
3663 int nargs;
3664 tree *argarray;
3665 tree parm_types;
3666 vec<tree, va_gc> *allocated = NULL;
3667 tree ret;
3669 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3670 expressions, like those used for ObjC messenger dispatches. */
3671 if (params != NULL && !vec_safe_is_empty (*params))
3672 function = objc_rewrite_function_call (function, (**params)[0]);
3674 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3675 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3676 if (TREE_CODE (function) == NOP_EXPR
3677 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3678 function = TREE_OPERAND (function, 0);
3680 if (TREE_CODE (function) == FUNCTION_DECL)
3682 /* If the function is a non-template member function
3683 or a non-template friend, then we need to check the
3684 constraints.
3686 Note that if overload resolution failed with a single
3687 candidate this function will be used to explicitly diagnose
3688 the failure for the single call expression. The check is
3689 technically redundant since we also would have failed in
3690 add_function_candidate. */
3691 if (flag_concepts
3692 && (complain & tf_error)
3693 && !constraints_satisfied_p (function))
3695 error ("cannot call function %qD", function);
3696 location_t loc = DECL_SOURCE_LOCATION (function);
3697 diagnose_constraints (loc, function, NULL_TREE);
3698 return error_mark_node;
3701 if (!mark_used (function, complain) && !(complain & tf_error))
3702 return error_mark_node;
3703 fndecl = function;
3705 /* Convert anything with function type to a pointer-to-function. */
3706 if (DECL_MAIN_P (function))
3708 if (complain & tf_error)
3709 pedwarn (input_location, OPT_Wpedantic,
3710 "ISO C++ forbids calling %<::main%> from within program");
3711 else
3712 return error_mark_node;
3714 function = build_addr_func (function, complain);
3716 else
3718 fndecl = NULL_TREE;
3720 function = build_addr_func (function, complain);
3723 if (function == error_mark_node)
3724 return error_mark_node;
3726 fntype = TREE_TYPE (function);
3728 if (TYPE_PTRMEMFUNC_P (fntype))
3730 if (complain & tf_error)
3731 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3732 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3733 original, original);
3734 return error_mark_node;
3737 is_method = (TYPE_PTR_P (fntype)
3738 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3740 if (!(TYPE_PTRFN_P (fntype)
3741 || is_method
3742 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3744 if (complain & tf_error)
3746 if (!flag_diagnostics_show_caret)
3747 error_at (input_location,
3748 "%qE cannot be used as a function", original);
3749 else if (DECL_P (original))
3750 error_at (input_location,
3751 "%qD cannot be used as a function", original);
3752 else
3753 error_at (input_location,
3754 "expression cannot be used as a function");
3757 return error_mark_node;
3760 /* fntype now gets the type of function pointed to. */
3761 fntype = TREE_TYPE (fntype);
3762 parm_types = TYPE_ARG_TYPES (fntype);
3764 if (params == NULL)
3766 allocated = make_tree_vector ();
3767 params = &allocated;
3770 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3771 complain);
3772 if (nargs < 0)
3773 return error_mark_node;
3775 argarray = (*params)->address ();
3777 /* Check for errors in format strings and inappropriately
3778 null parameters. */
3779 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3780 nargs, argarray, NULL);
3782 ret = build_cxx_call (function, nargs, argarray, complain);
3784 if (warned_p)
3786 tree c = extract_call_expr (ret);
3787 if (TREE_CODE (c) == CALL_EXPR)
3788 TREE_NO_WARNING (c) = 1;
3791 if (allocated != NULL)
3792 release_tree_vector (allocated);
3794 return ret;
3797 /* Subroutine of convert_arguments.
3798 Print an error message about a wrong number of arguments. */
3800 static void
3801 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3803 if (fndecl)
3805 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3807 if (DECL_NAME (fndecl) == NULL_TREE
3808 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3809 error_at (loc,
3810 too_many_p
3811 ? G_("too many arguments to constructor %q#D")
3812 : G_("too few arguments to constructor %q#D"),
3813 fndecl);
3814 else
3815 error_at (loc,
3816 too_many_p
3817 ? G_("too many arguments to member function %q#D")
3818 : G_("too few arguments to member function %q#D"),
3819 fndecl);
3821 else
3822 error_at (loc,
3823 too_many_p
3824 ? G_("too many arguments to function %q#D")
3825 : G_("too few arguments to function %q#D"),
3826 fndecl);
3827 if (!DECL_IS_BUILTIN (fndecl))
3828 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3830 else
3832 if (c_dialect_objc () && objc_message_selector ())
3833 error_at (loc,
3834 too_many_p
3835 ? G_("too many arguments to method %q#D")
3836 : G_("too few arguments to method %q#D"),
3837 objc_message_selector ());
3838 else
3839 error_at (loc, too_many_p ? G_("too many arguments to function")
3840 : G_("too few arguments to function"));
3844 /* Convert the actual parameter expressions in the list VALUES to the
3845 types in the list TYPELIST. The converted expressions are stored
3846 back in the VALUES vector.
3847 If parmdecls is exhausted, or when an element has NULL as its type,
3848 perform the default conversions.
3850 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3852 This is also where warnings about wrong number of args are generated.
3854 Returns the actual number of arguments processed (which might be less
3855 than the length of the vector), or -1 on error.
3857 In C++, unspecified trailing parameters can be filled in with their
3858 default arguments, if such were specified. Do so here. */
3860 static int
3861 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3862 int flags, tsubst_flags_t complain)
3864 tree typetail;
3865 unsigned int i;
3867 /* Argument passing is always copy-initialization. */
3868 flags |= LOOKUP_ONLYCONVERTING;
3870 for (i = 0, typetail = typelist;
3871 i < vec_safe_length (*values);
3872 i++)
3874 tree type = typetail ? TREE_VALUE (typetail) : 0;
3875 tree val = (**values)[i];
3877 if (val == error_mark_node || type == error_mark_node)
3878 return -1;
3880 if (type == void_type_node)
3882 if (complain & tf_error)
3884 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3885 return i;
3887 else
3888 return -1;
3891 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3892 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3893 if (TREE_CODE (val) == NOP_EXPR
3894 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3895 && (type == 0 || !TYPE_REF_P (type)))
3896 val = TREE_OPERAND (val, 0);
3898 if (type == 0 || !TYPE_REF_P (type))
3900 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3901 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3902 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3903 val = decay_conversion (val, complain);
3906 if (val == error_mark_node)
3907 return -1;
3909 if (type != 0)
3911 /* Formal parm type is specified by a function prototype. */
3912 tree parmval;
3914 if (!COMPLETE_TYPE_P (complete_type (type)))
3916 if (complain & tf_error)
3918 if (fndecl)
3919 error ("parameter %P of %qD has incomplete type %qT",
3920 i, fndecl, type);
3921 else
3922 error ("parameter %P has incomplete type %qT", i, type);
3924 parmval = error_mark_node;
3926 else
3928 parmval = convert_for_initialization
3929 (NULL_TREE, type, val, flags,
3930 ICR_ARGPASS, fndecl, i, complain);
3931 parmval = convert_for_arg_passing (type, parmval, complain);
3934 if (parmval == error_mark_node)
3935 return -1;
3937 (**values)[i] = parmval;
3939 else
3941 if (fndecl && magic_varargs_p (fndecl))
3942 /* Don't do ellipsis conversion for __built_in_constant_p
3943 as this will result in spurious errors for non-trivial
3944 types. */
3945 val = require_complete_type_sfinae (val, complain);
3946 else
3947 val = convert_arg_to_ellipsis (val, complain);
3949 (**values)[i] = val;
3952 if (typetail)
3953 typetail = TREE_CHAIN (typetail);
3956 if (typetail != 0 && typetail != void_list_node)
3958 /* See if there are default arguments that can be used. Because
3959 we hold default arguments in the FUNCTION_TYPE (which is so
3960 wrong), we can see default parameters here from deduced
3961 contexts (and via typeof) for indirect function calls.
3962 Fortunately we know whether we have a function decl to
3963 provide default arguments in a language conformant
3964 manner. */
3965 if (fndecl && TREE_PURPOSE (typetail)
3966 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3968 for (; typetail != void_list_node; ++i)
3970 /* After DR777, with explicit template args we can end up with a
3971 default argument followed by no default argument. */
3972 if (!TREE_PURPOSE (typetail))
3973 break;
3974 tree parmval
3975 = convert_default_arg (TREE_VALUE (typetail),
3976 TREE_PURPOSE (typetail),
3977 fndecl, i, complain);
3979 if (parmval == error_mark_node)
3980 return -1;
3982 vec_safe_push (*values, parmval);
3983 typetail = TREE_CHAIN (typetail);
3984 /* ends with `...'. */
3985 if (typetail == NULL_TREE)
3986 break;
3990 if (typetail && typetail != void_list_node)
3992 if (complain & tf_error)
3993 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3994 return -1;
3998 return (int) i;
4001 /* Build a binary-operation expression, after performing default
4002 conversions on the operands. CODE is the kind of expression to
4003 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4004 are the tree codes which correspond to ARG1 and ARG2 when issuing
4005 warnings about possibly misplaced parentheses. They may differ
4006 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4007 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4008 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4009 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4010 ARG2_CODE as ERROR_MARK. */
4012 tree
4013 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4014 enum tree_code arg1_code, tree arg2,
4015 enum tree_code arg2_code, tree *overload_p,
4016 tsubst_flags_t complain)
4018 tree orig_arg1;
4019 tree orig_arg2;
4020 tree expr;
4021 tree overload = NULL_TREE;
4023 orig_arg1 = arg1;
4024 orig_arg2 = arg2;
4026 if (processing_template_decl)
4028 if (type_dependent_expression_p (arg1)
4029 || type_dependent_expression_p (arg2))
4030 return build_min_nt_loc (loc, code, arg1, arg2);
4031 arg1 = build_non_dependent_expr (arg1);
4032 arg2 = build_non_dependent_expr (arg2);
4035 if (code == DOTSTAR_EXPR)
4036 expr = build_m_component_ref (arg1, arg2, complain);
4037 else
4038 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4039 &overload, complain);
4041 if (overload_p != NULL)
4042 *overload_p = overload;
4044 /* Check for cases such as x+y<<z which users are likely to
4045 misinterpret. But don't warn about obj << x + y, since that is a
4046 common idiom for I/O. */
4047 if (warn_parentheses
4048 && (complain & tf_warning)
4049 && !processing_template_decl
4050 && !error_operand_p (arg1)
4051 && !error_operand_p (arg2)
4052 && (code != LSHIFT_EXPR
4053 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4054 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4055 arg2_code, orig_arg2);
4057 if (processing_template_decl && expr != error_mark_node)
4059 if (overload != NULL_TREE)
4060 return (build_min_non_dep_op_overload
4061 (code, expr, overload, orig_arg1, orig_arg2));
4063 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4066 return expr;
4069 /* Build and return an ARRAY_REF expression. */
4071 tree
4072 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4073 tsubst_flags_t complain)
4075 tree orig_arg1 = arg1;
4076 tree orig_arg2 = arg2;
4077 tree expr;
4078 tree overload = NULL_TREE;
4080 if (processing_template_decl)
4082 if (type_dependent_expression_p (arg1)
4083 || type_dependent_expression_p (arg2))
4084 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4085 NULL_TREE, NULL_TREE);
4086 arg1 = build_non_dependent_expr (arg1);
4087 arg2 = build_non_dependent_expr (arg2);
4090 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4091 NULL_TREE, &overload, complain);
4093 if (processing_template_decl && expr != error_mark_node)
4095 if (overload != NULL_TREE)
4096 return (build_min_non_dep_op_overload
4097 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4099 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4100 NULL_TREE, NULL_TREE);
4102 return expr;
4105 /* Return whether OP is an expression of enum type cast to integer
4106 type. In C++ even unsigned enum types are cast to signed integer
4107 types. We do not want to issue warnings about comparisons between
4108 signed and unsigned types when one of the types is an enum type.
4109 Those warnings are always false positives in practice. */
4111 static bool
4112 enum_cast_to_int (tree op)
4114 if (CONVERT_EXPR_P (op)
4115 && TREE_TYPE (op) == integer_type_node
4116 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4117 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4118 return true;
4120 /* The cast may have been pushed into a COND_EXPR. */
4121 if (TREE_CODE (op) == COND_EXPR)
4122 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4123 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4125 return false;
4128 /* For the c-common bits. */
4129 tree
4130 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4131 bool /*convert_p*/)
4133 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4136 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4137 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4139 static tree
4140 build_vec_cmp (tree_code code, tree type,
4141 tree arg0, tree arg1)
4143 tree zero_vec = build_zero_cst (type);
4144 tree minus_one_vec = build_minus_one_cst (type);
4145 tree cmp_type = build_same_sized_truth_vector_type(type);
4146 tree cmp = build2 (code, cmp_type, arg0, arg1);
4147 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4150 /* Possibly warn about an address never being NULL. */
4152 static void
4153 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4155 if (!warn_address
4156 || (complain & tf_warning) == 0
4157 || c_inhibit_evaluation_warnings != 0
4158 || TREE_NO_WARNING (op))
4159 return;
4161 tree cop = fold_non_dependent_expr (op);
4163 if (TREE_CODE (cop) == ADDR_EXPR
4164 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4165 && !TREE_NO_WARNING (cop))
4166 warning_at (location, OPT_Waddress, "the address of %qD will never "
4167 "be NULL", TREE_OPERAND (cop, 0));
4169 if (CONVERT_EXPR_P (op)
4170 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4172 tree inner_op = op;
4173 STRIP_NOPS (inner_op);
4175 if (DECL_P (inner_op))
4176 warning_at (location, OPT_Waddress,
4177 "the compiler can assume that the address of "
4178 "%qD will never be NULL", inner_op);
4182 /* Build a binary-operation expression without default conversions.
4183 CODE is the kind of expression to build.
4184 LOCATION is the location_t of the operator in the source code.
4185 This function differs from `build' in several ways:
4186 the data type of the result is computed and recorded in it,
4187 warnings are generated if arg data types are invalid,
4188 special handling for addition and subtraction of pointers is known,
4189 and some optimization is done (operations on narrow ints
4190 are done in the narrower type when that gives the same result).
4191 Constant folding is also done before the result is returned.
4193 Note that the operands will never have enumeral types
4194 because either they have just had the default conversions performed
4195 or they have both just been converted to some other type in which
4196 the arithmetic is to be done.
4198 C++: must do special pointer arithmetic when implementing
4199 multiple inheritance, and deal with pointer to member functions. */
4201 tree
4202 cp_build_binary_op (location_t location,
4203 enum tree_code code, tree orig_op0, tree orig_op1,
4204 tsubst_flags_t complain)
4206 tree op0, op1;
4207 enum tree_code code0, code1;
4208 tree type0, type1;
4209 const char *invalid_op_diag;
4211 /* Expression code to give to the expression when it is built.
4212 Normally this is CODE, which is what the caller asked for,
4213 but in some special cases we change it. */
4214 enum tree_code resultcode = code;
4216 /* Data type in which the computation is to be performed.
4217 In the simplest cases this is the common type of the arguments. */
4218 tree result_type = NULL_TREE;
4220 /* Nonzero means operands have already been type-converted
4221 in whatever way is necessary.
4222 Zero means they need to be converted to RESULT_TYPE. */
4223 int converted = 0;
4225 /* Nonzero means create the expression with this type, rather than
4226 RESULT_TYPE. */
4227 tree build_type = 0;
4229 /* Nonzero means after finally constructing the expression
4230 convert it to this type. */
4231 tree final_type = 0;
4233 tree result, result_ovl;
4235 /* Nonzero if this is an operation like MIN or MAX which can
4236 safely be computed in short if both args are promoted shorts.
4237 Also implies COMMON.
4238 -1 indicates a bitwise operation; this makes a difference
4239 in the exact conditions for when it is safe to do the operation
4240 in a narrower mode. */
4241 int shorten = 0;
4243 /* Nonzero if this is a comparison operation;
4244 if both args are promoted shorts, compare the original shorts.
4245 Also implies COMMON. */
4246 int short_compare = 0;
4248 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4249 int common = 0;
4251 /* True if both operands have arithmetic type. */
4252 bool arithmetic_types_p;
4254 /* Apply default conversions. */
4255 op0 = orig_op0;
4256 op1 = orig_op1;
4258 /* Remember whether we're doing / or %. */
4259 bool doing_div_or_mod = false;
4261 /* Remember whether we're doing << or >>. */
4262 bool doing_shift = false;
4264 /* Tree holding instrumentation expression. */
4265 tree instrument_expr = NULL_TREE;
4267 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4268 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4269 || code == TRUTH_XOR_EXPR)
4271 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4272 op0 = decay_conversion (op0, complain);
4273 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4274 op1 = decay_conversion (op1, complain);
4276 else
4278 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4279 op0 = cp_default_conversion (op0, complain);
4280 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4281 op1 = cp_default_conversion (op1, complain);
4284 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4285 STRIP_TYPE_NOPS (op0);
4286 STRIP_TYPE_NOPS (op1);
4288 /* DTRT if one side is an overloaded function, but complain about it. */
4289 if (type_unknown_p (op0))
4291 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4292 if (t != error_mark_node)
4294 if (complain & tf_error)
4295 permerror (input_location, "assuming cast to type %qT from overloaded function",
4296 TREE_TYPE (t));
4297 op0 = t;
4300 if (type_unknown_p (op1))
4302 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4303 if (t != error_mark_node)
4305 if (complain & tf_error)
4306 permerror (input_location, "assuming cast to type %qT from overloaded function",
4307 TREE_TYPE (t));
4308 op1 = t;
4312 type0 = TREE_TYPE (op0);
4313 type1 = TREE_TYPE (op1);
4315 /* The expression codes of the data types of the arguments tell us
4316 whether the arguments are integers, floating, pointers, etc. */
4317 code0 = TREE_CODE (type0);
4318 code1 = TREE_CODE (type1);
4320 /* If an error was already reported for one of the arguments,
4321 avoid reporting another error. */
4322 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4323 return error_mark_node;
4325 if ((invalid_op_diag
4326 = targetm.invalid_binary_op (code, type0, type1)))
4328 if (complain & tf_error)
4329 error (invalid_op_diag);
4330 return error_mark_node;
4333 /* Issue warnings about peculiar, but valid, uses of NULL. */
4334 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4335 /* It's reasonable to use pointer values as operands of &&
4336 and ||, so NULL is no exception. */
4337 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4338 && ( /* Both are NULL (or 0) and the operation was not a
4339 comparison or a pointer subtraction. */
4340 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4341 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4342 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4343 || (!null_ptr_cst_p (orig_op0)
4344 && !TYPE_PTR_OR_PTRMEM_P (type0))
4345 || (!null_ptr_cst_p (orig_op1)
4346 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4347 && (complain & tf_warning))
4349 source_location loc =
4350 expansion_point_location_if_in_system_header (input_location);
4352 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4355 /* In case when one of the operands of the binary operation is
4356 a vector and another is a scalar -- convert scalar to vector. */
4357 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4359 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4360 complain & tf_error);
4362 switch (convert_flag)
4364 case stv_error:
4365 return error_mark_node;
4366 case stv_firstarg:
4368 op0 = convert (TREE_TYPE (type1), op0);
4369 op0 = save_expr (op0);
4370 op0 = build_vector_from_val (type1, op0);
4371 type0 = TREE_TYPE (op0);
4372 code0 = TREE_CODE (type0);
4373 converted = 1;
4374 break;
4376 case stv_secondarg:
4378 op1 = convert (TREE_TYPE (type0), op1);
4379 op1 = save_expr (op1);
4380 op1 = build_vector_from_val (type0, op1);
4381 type1 = TREE_TYPE (op1);
4382 code1 = TREE_CODE (type1);
4383 converted = 1;
4384 break;
4386 default:
4387 break;
4391 switch (code)
4393 case MINUS_EXPR:
4394 /* Subtraction of two similar pointers.
4395 We must subtract them as integers, then divide by object size. */
4396 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4397 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4398 TREE_TYPE (type1)))
4400 result = pointer_diff (location, op0, op1,
4401 common_pointer_type (type0, type1), complain,
4402 &instrument_expr);
4403 if (instrument_expr != NULL)
4404 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4405 instrument_expr, result);
4407 return result;
4409 /* In all other cases except pointer - int, the usual arithmetic
4410 rules apply. */
4411 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4413 common = 1;
4414 break;
4416 /* The pointer - int case is just like pointer + int; fall
4417 through. */
4418 gcc_fallthrough ();
4419 case PLUS_EXPR:
4420 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4421 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4423 tree ptr_operand;
4424 tree int_operand;
4425 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4426 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4427 if (processing_template_decl)
4429 result_type = TREE_TYPE (ptr_operand);
4430 break;
4432 return cp_pointer_int_sum (location, code,
4433 ptr_operand,
4434 int_operand,
4435 complain);
4437 common = 1;
4438 break;
4440 case MULT_EXPR:
4441 common = 1;
4442 break;
4444 case TRUNC_DIV_EXPR:
4445 case CEIL_DIV_EXPR:
4446 case FLOOR_DIV_EXPR:
4447 case ROUND_DIV_EXPR:
4448 case EXACT_DIV_EXPR:
4449 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4451 tree type0 = TREE_OPERAND (op0, 0);
4452 tree type1 = TREE_OPERAND (op1, 0);
4453 tree first_arg = type0;
4454 if (!TYPE_P (type0))
4455 type0 = TREE_TYPE (type0);
4456 if (!TYPE_P (type1))
4457 type1 = TREE_TYPE (type1);
4458 if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4459 && !(TREE_CODE (first_arg) == PARM_DECL
4460 && DECL_ARRAY_PARAMETER_P (first_arg)
4461 && warn_sizeof_array_argument)
4462 && (complain & tf_warning))
4463 if (warning_at (location, OPT_Wsizeof_pointer_div,
4464 "division %<sizeof (%T) / sizeof (%T)%> does "
4465 "not compute the number of array elements",
4466 type0, type1))
4467 if (DECL_P (first_arg))
4468 inform (DECL_SOURCE_LOCATION (first_arg),
4469 "first %<sizeof%> operand was declared here");
4472 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4473 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4474 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4475 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4477 enum tree_code tcode0 = code0, tcode1 = code1;
4478 tree cop1 = fold_non_dependent_expr (op1);
4479 doing_div_or_mod = true;
4480 warn_for_div_by_zero (location, cop1);
4482 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4483 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4484 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4485 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4487 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4488 resultcode = RDIV_EXPR;
4489 else
4490 /* When dividing two signed integers, we have to promote to int.
4491 unless we divide by a constant != -1. Note that default
4492 conversion will have been performed on the operands at this
4493 point, so we have to dig out the original type to find out if
4494 it was unsigned. */
4495 shorten = ((TREE_CODE (op0) == NOP_EXPR
4496 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4497 || (TREE_CODE (op1) == INTEGER_CST
4498 && ! integer_all_onesp (op1)));
4500 common = 1;
4502 break;
4504 case BIT_AND_EXPR:
4505 case BIT_IOR_EXPR:
4506 case BIT_XOR_EXPR:
4507 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4508 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4509 && !VECTOR_FLOAT_TYPE_P (type0)
4510 && !VECTOR_FLOAT_TYPE_P (type1)))
4511 shorten = -1;
4512 break;
4514 case TRUNC_MOD_EXPR:
4515 case FLOOR_MOD_EXPR:
4517 tree cop1 = fold_non_dependent_expr (op1);
4518 doing_div_or_mod = true;
4519 warn_for_div_by_zero (location, cop1);
4522 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4523 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4524 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4525 common = 1;
4526 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4528 /* Although it would be tempting to shorten always here, that loses
4529 on some targets, since the modulo instruction is undefined if the
4530 quotient can't be represented in the computation mode. We shorten
4531 only if unsigned or if dividing by something we know != -1. */
4532 shorten = ((TREE_CODE (op0) == NOP_EXPR
4533 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4534 || (TREE_CODE (op1) == INTEGER_CST
4535 && ! integer_all_onesp (op1)));
4536 common = 1;
4538 break;
4540 case TRUTH_ANDIF_EXPR:
4541 case TRUTH_ORIF_EXPR:
4542 case TRUTH_AND_EXPR:
4543 case TRUTH_OR_EXPR:
4544 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4546 if (!COMPARISON_CLASS_P (op1))
4547 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4548 build_zero_cst (type1), complain);
4549 if (code == TRUTH_ANDIF_EXPR)
4551 tree z = build_zero_cst (TREE_TYPE (op1));
4552 return build_conditional_expr (location, op0, op1, z, complain);
4554 else if (code == TRUTH_ORIF_EXPR)
4556 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4557 return build_conditional_expr (location, op0, m1, op1, complain);
4559 else
4560 gcc_unreachable ();
4562 if (VECTOR_TYPE_P (type0))
4564 if (!COMPARISON_CLASS_P (op0))
4565 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4566 build_zero_cst (type0), complain);
4567 if (!VECTOR_TYPE_P (type1))
4569 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4570 tree z = build_zero_cst (TREE_TYPE (op0));
4571 op1 = build_conditional_expr (location, op1, m1, z, complain);
4573 else if (!COMPARISON_CLASS_P (op1))
4574 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4575 build_zero_cst (type1), complain);
4577 if (code == TRUTH_ANDIF_EXPR)
4578 code = BIT_AND_EXPR;
4579 else if (code == TRUTH_ORIF_EXPR)
4580 code = BIT_IOR_EXPR;
4581 else
4582 gcc_unreachable ();
4584 return cp_build_binary_op (location, code, op0, op1, complain);
4587 result_type = boolean_type_node;
4588 break;
4590 /* Shift operations: result has same type as first operand;
4591 always convert second operand to int.
4592 Also set SHORT_SHIFT if shifting rightward. */
4594 case RSHIFT_EXPR:
4595 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4596 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4598 result_type = type0;
4599 converted = 1;
4601 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4602 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4603 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4604 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4605 TYPE_VECTOR_SUBPARTS (type1)))
4607 result_type = type0;
4608 converted = 1;
4610 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4612 tree const_op1 = fold_non_dependent_expr (op1);
4613 if (TREE_CODE (const_op1) != INTEGER_CST)
4614 const_op1 = op1;
4615 result_type = type0;
4616 doing_shift = true;
4617 if (TREE_CODE (const_op1) == INTEGER_CST)
4619 if (tree_int_cst_lt (const_op1, integer_zero_node))
4621 if ((complain & tf_warning)
4622 && c_inhibit_evaluation_warnings == 0)
4623 warning (OPT_Wshift_count_negative,
4624 "right shift count is negative");
4626 else
4628 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4629 && (complain & tf_warning)
4630 && c_inhibit_evaluation_warnings == 0)
4631 warning (OPT_Wshift_count_overflow,
4632 "right shift count >= width of type");
4635 /* Avoid converting op1 to result_type later. */
4636 converted = 1;
4638 break;
4640 case LSHIFT_EXPR:
4641 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4642 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4644 result_type = type0;
4645 converted = 1;
4647 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4648 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4649 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4650 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4651 TYPE_VECTOR_SUBPARTS (type1)))
4653 result_type = type0;
4654 converted = 1;
4656 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4658 tree const_op0 = fold_non_dependent_expr (op0);
4659 if (TREE_CODE (const_op0) != INTEGER_CST)
4660 const_op0 = op0;
4661 tree const_op1 = fold_non_dependent_expr (op1);
4662 if (TREE_CODE (const_op1) != INTEGER_CST)
4663 const_op1 = op1;
4664 result_type = type0;
4665 doing_shift = true;
4666 if (TREE_CODE (const_op0) == INTEGER_CST
4667 && tree_int_cst_sgn (const_op0) < 0
4668 && (complain & tf_warning)
4669 && c_inhibit_evaluation_warnings == 0)
4670 warning (OPT_Wshift_negative_value,
4671 "left shift of negative value");
4672 if (TREE_CODE (const_op1) == INTEGER_CST)
4674 if (tree_int_cst_lt (const_op1, integer_zero_node))
4676 if ((complain & tf_warning)
4677 && c_inhibit_evaluation_warnings == 0)
4678 warning (OPT_Wshift_count_negative,
4679 "left shift count is negative");
4681 else if (compare_tree_int (const_op1,
4682 TYPE_PRECISION (type0)) >= 0)
4684 if ((complain & tf_warning)
4685 && c_inhibit_evaluation_warnings == 0)
4686 warning (OPT_Wshift_count_overflow,
4687 "left shift count >= width of type");
4689 else if (TREE_CODE (const_op0) == INTEGER_CST
4690 && (complain & tf_warning))
4691 maybe_warn_shift_overflow (location, const_op0, const_op1);
4693 /* Avoid converting op1 to result_type later. */
4694 converted = 1;
4696 break;
4698 case RROTATE_EXPR:
4699 case LROTATE_EXPR:
4700 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4702 result_type = type0;
4703 if (TREE_CODE (op1) == INTEGER_CST)
4705 if (tree_int_cst_lt (op1, integer_zero_node))
4707 if (complain & tf_warning)
4708 warning (0, (code == LROTATE_EXPR)
4709 ? G_("left rotate count is negative")
4710 : G_("right rotate count is negative"));
4712 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4714 if (complain & tf_warning)
4715 warning (0, (code == LROTATE_EXPR)
4716 ? G_("left rotate count >= width of type")
4717 : G_("right rotate count >= width of type"));
4720 /* Convert the shift-count to an integer, regardless of
4721 size of value being shifted. */
4722 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4723 op1 = cp_convert (integer_type_node, op1, complain);
4725 break;
4727 case EQ_EXPR:
4728 case NE_EXPR:
4729 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4730 goto vector_compare;
4731 if ((complain & tf_warning)
4732 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4733 warning (OPT_Wfloat_equal,
4734 "comparing floating point with == or != is unsafe");
4735 if ((complain & tf_warning)
4736 && ((TREE_CODE (orig_op0) == STRING_CST
4737 && !integer_zerop (cp_fully_fold (op1)))
4738 || (TREE_CODE (orig_op1) == STRING_CST
4739 && !integer_zerop (cp_fully_fold (op0)))))
4740 warning (OPT_Waddress, "comparison with string literal results "
4741 "in unspecified behavior");
4743 build_type = boolean_type_node;
4744 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4745 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4746 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4747 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4748 short_compare = 1;
4749 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4750 && null_ptr_cst_p (orig_op1))
4751 /* Handle, eg, (void*)0 (c++/43906), and more. */
4752 || (code0 == POINTER_TYPE
4753 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4755 if (TYPE_PTR_P (type1))
4756 result_type = composite_pointer_type (type0, type1, op0, op1,
4757 CPO_COMPARISON, complain);
4758 else
4759 result_type = type0;
4761 if (char_type_p (TREE_TYPE (orig_op1))
4762 && warning (OPT_Wpointer_compare,
4763 "comparison between pointer and zero character "
4764 "constant"))
4765 inform (input_location,
4766 "did you mean to dereference the pointer?");
4767 warn_for_null_address (location, op0, complain);
4769 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4770 && null_ptr_cst_p (orig_op0))
4771 /* Handle, eg, (void*)0 (c++/43906), and more. */
4772 || (code1 == POINTER_TYPE
4773 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4775 if (TYPE_PTR_P (type0))
4776 result_type = composite_pointer_type (type0, type1, op0, op1,
4777 CPO_COMPARISON, complain);
4778 else
4779 result_type = type1;
4781 if (char_type_p (TREE_TYPE (orig_op0))
4782 && warning (OPT_Wpointer_compare,
4783 "comparison between pointer and zero character "
4784 "constant"))
4785 inform (input_location,
4786 "did you mean to dereference the pointer?");
4787 warn_for_null_address (location, op1, complain);
4789 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4790 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4791 result_type = composite_pointer_type (type0, type1, op0, op1,
4792 CPO_COMPARISON, complain);
4793 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4794 /* One of the operands must be of nullptr_t type. */
4795 result_type = TREE_TYPE (nullptr_node);
4796 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4798 result_type = type0;
4799 if (complain & tf_error)
4800 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4801 else
4802 return error_mark_node;
4804 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4806 result_type = type1;
4807 if (complain & tf_error)
4808 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4809 else
4810 return error_mark_node;
4812 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4814 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4815 == ptrmemfunc_vbit_in_delta)
4817 tree pfn0, delta0, e1, e2;
4819 if (TREE_SIDE_EFFECTS (op0))
4820 op0 = save_expr (op0);
4822 pfn0 = pfn_from_ptrmemfunc (op0);
4823 delta0 = delta_from_ptrmemfunc (op0);
4824 e1 = cp_build_binary_op (location,
4825 EQ_EXPR,
4826 pfn0,
4827 build_zero_cst (TREE_TYPE (pfn0)),
4828 complain);
4829 e2 = cp_build_binary_op (location,
4830 BIT_AND_EXPR,
4831 delta0,
4832 integer_one_node,
4833 complain);
4835 if (complain & tf_warning)
4836 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4838 e2 = cp_build_binary_op (location,
4839 EQ_EXPR, e2, integer_zero_node,
4840 complain);
4841 op0 = cp_build_binary_op (location,
4842 TRUTH_ANDIF_EXPR, e1, e2,
4843 complain);
4844 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4846 else
4848 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4849 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4851 result_type = TREE_TYPE (op0);
4853 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4854 return cp_build_binary_op (location, code, op1, op0, complain);
4855 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4857 tree type;
4858 /* E will be the final comparison. */
4859 tree e;
4860 /* E1 and E2 are for scratch. */
4861 tree e1;
4862 tree e2;
4863 tree pfn0;
4864 tree pfn1;
4865 tree delta0;
4866 tree delta1;
4868 type = composite_pointer_type (type0, type1, op0, op1,
4869 CPO_COMPARISON, complain);
4871 if (!same_type_p (TREE_TYPE (op0), type))
4872 op0 = cp_convert_and_check (type, op0, complain);
4873 if (!same_type_p (TREE_TYPE (op1), type))
4874 op1 = cp_convert_and_check (type, op1, complain);
4876 if (op0 == error_mark_node || op1 == error_mark_node)
4877 return error_mark_node;
4879 if (TREE_SIDE_EFFECTS (op0))
4880 op0 = save_expr (op0);
4881 if (TREE_SIDE_EFFECTS (op1))
4882 op1 = save_expr (op1);
4884 pfn0 = pfn_from_ptrmemfunc (op0);
4885 pfn0 = cp_fully_fold (pfn0);
4886 /* Avoid -Waddress warnings (c++/64877). */
4887 if (TREE_CODE (pfn0) == ADDR_EXPR)
4888 TREE_NO_WARNING (pfn0) = 1;
4889 pfn1 = pfn_from_ptrmemfunc (op1);
4890 pfn1 = cp_fully_fold (pfn1);
4891 delta0 = delta_from_ptrmemfunc (op0);
4892 delta1 = delta_from_ptrmemfunc (op1);
4893 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4894 == ptrmemfunc_vbit_in_delta)
4896 /* We generate:
4898 (op0.pfn == op1.pfn
4899 && ((op0.delta == op1.delta)
4900 || (!op0.pfn && op0.delta & 1 == 0
4901 && op1.delta & 1 == 0))
4903 The reason for the `!op0.pfn' bit is that a NULL
4904 pointer-to-member is any member with a zero PFN and
4905 LSB of the DELTA field is 0. */
4907 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4908 delta0,
4909 integer_one_node,
4910 complain);
4911 e1 = cp_build_binary_op (location,
4912 EQ_EXPR, e1, integer_zero_node,
4913 complain);
4914 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4915 delta1,
4916 integer_one_node,
4917 complain);
4918 e2 = cp_build_binary_op (location,
4919 EQ_EXPR, e2, integer_zero_node,
4920 complain);
4921 e1 = cp_build_binary_op (location,
4922 TRUTH_ANDIF_EXPR, e2, e1,
4923 complain);
4924 e2 = cp_build_binary_op (location, EQ_EXPR,
4925 pfn0,
4926 build_zero_cst (TREE_TYPE (pfn0)),
4927 complain);
4928 e2 = cp_build_binary_op (location,
4929 TRUTH_ANDIF_EXPR, e2, e1, complain);
4930 e1 = cp_build_binary_op (location,
4931 EQ_EXPR, delta0, delta1, complain);
4932 e1 = cp_build_binary_op (location,
4933 TRUTH_ORIF_EXPR, e1, e2, complain);
4935 else
4937 /* We generate:
4939 (op0.pfn == op1.pfn
4940 && (!op0.pfn || op0.delta == op1.delta))
4942 The reason for the `!op0.pfn' bit is that a NULL
4943 pointer-to-member is any member with a zero PFN; the
4944 DELTA field is unspecified. */
4946 e1 = cp_build_binary_op (location,
4947 EQ_EXPR, delta0, delta1, complain);
4948 e2 = cp_build_binary_op (location,
4949 EQ_EXPR,
4950 pfn0,
4951 build_zero_cst (TREE_TYPE (pfn0)),
4952 complain);
4953 e1 = cp_build_binary_op (location,
4954 TRUTH_ORIF_EXPR, e1, e2, complain);
4956 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4957 e = cp_build_binary_op (location,
4958 TRUTH_ANDIF_EXPR, e2, e1, complain);
4959 if (code == EQ_EXPR)
4960 return e;
4961 return cp_build_binary_op (location,
4962 EQ_EXPR, e, integer_zero_node, complain);
4964 else
4966 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4967 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4968 type1));
4969 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4970 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4971 type0));
4974 break;
4976 case MAX_EXPR:
4977 case MIN_EXPR:
4978 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4979 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4980 shorten = 1;
4981 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4982 result_type = composite_pointer_type (type0, type1, op0, op1,
4983 CPO_COMPARISON, complain);
4984 break;
4986 case LE_EXPR:
4987 case GE_EXPR:
4988 case LT_EXPR:
4989 case GT_EXPR:
4990 if (TREE_CODE (orig_op0) == STRING_CST
4991 || TREE_CODE (orig_op1) == STRING_CST)
4993 if (complain & tf_warning)
4994 warning (OPT_Waddress, "comparison with string literal results "
4995 "in unspecified behavior");
4998 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5000 vector_compare:
5001 tree intt;
5002 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5003 TREE_TYPE (type1))
5004 && !vector_types_compatible_elements_p (type0, type1))
5006 if (complain & tf_error)
5008 error_at (location, "comparing vectors with different "
5009 "element types");
5010 inform (location, "operand types are %qT and %qT",
5011 type0, type1);
5013 return error_mark_node;
5016 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5017 TYPE_VECTOR_SUBPARTS (type1)))
5019 if (complain & tf_error)
5021 error_at (location, "comparing vectors with different "
5022 "number of elements");
5023 inform (location, "operand types are %qT and %qT",
5024 type0, type1);
5026 return error_mark_node;
5029 /* It's not precisely specified how the usual arithmetic
5030 conversions apply to the vector types. Here, we use
5031 the unsigned type if one of the operands is signed and
5032 the other one is unsigned. */
5033 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5035 if (!TYPE_UNSIGNED (type0))
5036 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5037 else
5038 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5039 warning_at (location, OPT_Wsign_compare, "comparison between "
5040 "types %qT and %qT", type0, type1);
5043 /* Always construct signed integer vector type. */
5044 intt = c_common_type_for_size
5045 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5046 if (!intt)
5048 if (complain & tf_error)
5049 error_at (location, "could not find an integer type "
5050 "of the same size as %qT", TREE_TYPE (type0));
5051 return error_mark_node;
5053 result_type = build_opaque_vector_type (intt,
5054 TYPE_VECTOR_SUBPARTS (type0));
5055 converted = 1;
5056 return build_vec_cmp (resultcode, result_type, op0, op1);
5058 build_type = boolean_type_node;
5059 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5060 || code0 == ENUMERAL_TYPE)
5061 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5062 || code1 == ENUMERAL_TYPE))
5063 short_compare = 1;
5064 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5065 result_type = composite_pointer_type (type0, type1, op0, op1,
5066 CPO_COMPARISON, complain);
5067 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5069 result_type = type0;
5070 if (extra_warnings && (complain & tf_warning))
5071 warning (OPT_Wextra,
5072 "ordered comparison of pointer with integer zero");
5074 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5076 result_type = type1;
5077 if (extra_warnings && (complain & tf_warning))
5078 warning (OPT_Wextra,
5079 "ordered comparison of pointer with integer zero");
5081 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5082 /* One of the operands must be of nullptr_t type. */
5083 result_type = TREE_TYPE (nullptr_node);
5084 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5086 result_type = type0;
5087 if (complain & tf_error)
5088 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5089 else
5090 return error_mark_node;
5092 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5094 result_type = type1;
5095 if (complain & tf_error)
5096 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5097 else
5098 return error_mark_node;
5101 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5102 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5104 op0 = save_expr (op0);
5105 op1 = save_expr (op1);
5107 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5108 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5111 break;
5113 case UNORDERED_EXPR:
5114 case ORDERED_EXPR:
5115 case UNLT_EXPR:
5116 case UNLE_EXPR:
5117 case UNGT_EXPR:
5118 case UNGE_EXPR:
5119 case UNEQ_EXPR:
5120 build_type = integer_type_node;
5121 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5123 if (complain & tf_error)
5124 error ("unordered comparison on non-floating point argument");
5125 return error_mark_node;
5127 common = 1;
5128 break;
5130 default:
5131 break;
5134 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5135 || code0 == ENUMERAL_TYPE)
5136 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5137 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5138 arithmetic_types_p = 1;
5139 else
5141 arithmetic_types_p = 0;
5142 /* Vector arithmetic is only allowed when both sides are vectors. */
5143 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5145 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5146 || !vector_types_compatible_elements_p (type0, type1))
5148 if (complain & tf_error)
5150 /* "location" already embeds the locations of the
5151 operands, so we don't need to add them separately
5152 to richloc. */
5153 rich_location richloc (line_table, location);
5154 binary_op_error (&richloc, code, type0, type1);
5156 return error_mark_node;
5158 arithmetic_types_p = 1;
5161 /* Determine the RESULT_TYPE, if it is not already known. */
5162 if (!result_type
5163 && arithmetic_types_p
5164 && (shorten || common || short_compare))
5166 result_type = cp_common_type (type0, type1);
5167 if (complain & tf_warning)
5168 do_warn_double_promotion (result_type, type0, type1,
5169 "implicit conversion from %qH to %qI "
5170 "to match other operand of binary "
5171 "expression",
5172 location);
5175 if (!result_type)
5177 if (complain & tf_error)
5178 error_at (location,
5179 "invalid operands of types %qT and %qT to binary %qO",
5180 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5181 return error_mark_node;
5184 /* If we're in a template, the only thing we need to know is the
5185 RESULT_TYPE. */
5186 if (processing_template_decl)
5188 /* Since the middle-end checks the type when doing a build2, we
5189 need to build the tree in pieces. This built tree will never
5190 get out of the front-end as we replace it when instantiating
5191 the template. */
5192 tree tmp = build2 (resultcode,
5193 build_type ? build_type : result_type,
5194 NULL_TREE, op1);
5195 TREE_OPERAND (tmp, 0) = op0;
5196 return tmp;
5199 /* Remember the original type; RESULT_TYPE might be changed later on
5200 by shorten_binary_op. */
5201 tree orig_type = result_type;
5203 if (arithmetic_types_p)
5205 bool first_complex = (code0 == COMPLEX_TYPE);
5206 bool second_complex = (code1 == COMPLEX_TYPE);
5207 int none_complex = (!first_complex && !second_complex);
5209 /* Adapted from patch for c/24581. */
5210 if (first_complex != second_complex
5211 && (code == PLUS_EXPR
5212 || code == MINUS_EXPR
5213 || code == MULT_EXPR
5214 || (code == TRUNC_DIV_EXPR && first_complex))
5215 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5216 && flag_signed_zeros)
5218 /* An operation on mixed real/complex operands must be
5219 handled specially, but the language-independent code can
5220 more easily optimize the plain complex arithmetic if
5221 -fno-signed-zeros. */
5222 tree real_type = TREE_TYPE (result_type);
5223 tree real, imag;
5224 if (first_complex)
5226 if (TREE_TYPE (op0) != result_type)
5227 op0 = cp_convert_and_check (result_type, op0, complain);
5228 if (TREE_TYPE (op1) != real_type)
5229 op1 = cp_convert_and_check (real_type, op1, complain);
5231 else
5233 if (TREE_TYPE (op0) != real_type)
5234 op0 = cp_convert_and_check (real_type, op0, complain);
5235 if (TREE_TYPE (op1) != result_type)
5236 op1 = cp_convert_and_check (result_type, op1, complain);
5238 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5239 return error_mark_node;
5240 if (first_complex)
5242 op0 = save_expr (op0);
5243 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5244 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5245 switch (code)
5247 case MULT_EXPR:
5248 case TRUNC_DIV_EXPR:
5249 op1 = save_expr (op1);
5250 imag = build2 (resultcode, real_type, imag, op1);
5251 /* Fall through. */
5252 case PLUS_EXPR:
5253 case MINUS_EXPR:
5254 real = build2 (resultcode, real_type, real, op1);
5255 break;
5256 default:
5257 gcc_unreachable();
5260 else
5262 op1 = save_expr (op1);
5263 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5264 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5265 switch (code)
5267 case MULT_EXPR:
5268 op0 = save_expr (op0);
5269 imag = build2 (resultcode, real_type, op0, imag);
5270 /* Fall through. */
5271 case PLUS_EXPR:
5272 real = build2 (resultcode, real_type, op0, real);
5273 break;
5274 case MINUS_EXPR:
5275 real = build2 (resultcode, real_type, op0, real);
5276 imag = build1 (NEGATE_EXPR, real_type, imag);
5277 break;
5278 default:
5279 gcc_unreachable();
5282 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5283 return result;
5286 /* For certain operations (which identify themselves by shorten != 0)
5287 if both args were extended from the same smaller type,
5288 do the arithmetic in that type and then extend.
5290 shorten !=0 and !=1 indicates a bitwise operation.
5291 For them, this optimization is safe only if
5292 both args are zero-extended or both are sign-extended.
5293 Otherwise, we might change the result.
5294 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5295 but calculated in (unsigned short) it would be (unsigned short)-1. */
5297 if (shorten && none_complex)
5299 final_type = result_type;
5300 result_type = shorten_binary_op (result_type, op0, op1,
5301 shorten == -1);
5304 /* Comparison operations are shortened too but differently.
5305 They identify themselves by setting short_compare = 1. */
5307 if (short_compare)
5309 /* We call shorten_compare only for diagnostic-reason. */
5310 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5311 xresult_type = result_type;
5312 enum tree_code xresultcode = resultcode;
5313 shorten_compare (location, &xop0, &xop1, &xresult_type,
5314 &xresultcode);
5317 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5318 && warn_sign_compare
5319 /* Do not warn until the template is instantiated; we cannot
5320 bound the ranges of the arguments until that point. */
5321 && !processing_template_decl
5322 && (complain & tf_warning)
5323 && c_inhibit_evaluation_warnings == 0
5324 /* Even unsigned enum types promote to signed int. We don't
5325 want to issue -Wsign-compare warnings for this case. */
5326 && !enum_cast_to_int (orig_op0)
5327 && !enum_cast_to_int (orig_op1))
5329 tree oop0 = maybe_constant_value (orig_op0);
5330 tree oop1 = maybe_constant_value (orig_op1);
5332 if (TREE_CODE (oop0) != INTEGER_CST)
5333 oop0 = cp_fully_fold (orig_op0);
5334 if (TREE_CODE (oop1) != INTEGER_CST)
5335 oop1 = cp_fully_fold (orig_op1);
5336 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5337 result_type, resultcode);
5341 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5342 Then the expression will be built.
5343 It will be given type FINAL_TYPE if that is nonzero;
5344 otherwise, it will be given type RESULT_TYPE. */
5345 if (! converted)
5347 if (TREE_TYPE (op0) != result_type)
5348 op0 = cp_convert_and_check (result_type, op0, complain);
5349 if (TREE_TYPE (op1) != result_type)
5350 op1 = cp_convert_and_check (result_type, op1, complain);
5352 if (op0 == error_mark_node || op1 == error_mark_node)
5353 return error_mark_node;
5356 if (build_type == NULL_TREE)
5357 build_type = result_type;
5359 if (sanitize_flags_p ((SANITIZE_SHIFT
5360 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5361 && current_function_decl != NULL_TREE
5362 && !processing_template_decl
5363 && (doing_div_or_mod || doing_shift))
5365 /* OP0 and/or OP1 might have side-effects. */
5366 op0 = cp_save_expr (op0);
5367 op1 = cp_save_expr (op1);
5368 op0 = fold_non_dependent_expr (op0);
5369 op1 = fold_non_dependent_expr (op1);
5370 if (doing_div_or_mod
5371 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5373 /* For diagnostics we want to use the promoted types without
5374 shorten_binary_op. So convert the arguments to the
5375 original result_type. */
5376 tree cop0 = op0;
5377 tree cop1 = op1;
5378 if (TREE_TYPE (cop0) != orig_type)
5379 cop0 = cp_convert (orig_type, op0, complain);
5380 if (TREE_TYPE (cop1) != orig_type)
5381 cop1 = cp_convert (orig_type, op1, complain);
5382 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5384 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5385 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5388 result = build2_loc (location, resultcode, build_type, op0, op1);
5389 if (final_type != 0)
5390 result = cp_convert (final_type, result, complain);
5392 if (instrument_expr != NULL)
5393 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5394 instrument_expr, result);
5396 if (!processing_template_decl)
5398 op0 = cp_fully_fold (op0);
5399 /* Only consider the second argument if the first isn't overflowed. */
5400 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5401 return result;
5402 op1 = cp_fully_fold (op1);
5403 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5404 return result;
5406 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5407 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5408 return result;
5410 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5411 if (TREE_OVERFLOW_P (result_ovl))
5412 overflow_warning (location, result_ovl);
5414 return result;
5417 /* Build a VEC_PERM_EXPR.
5418 This is a simple wrapper for c_build_vec_perm_expr. */
5419 tree
5420 build_x_vec_perm_expr (location_t loc,
5421 tree arg0, tree arg1, tree arg2,
5422 tsubst_flags_t complain)
5424 tree orig_arg0 = arg0;
5425 tree orig_arg1 = arg1;
5426 tree orig_arg2 = arg2;
5427 if (processing_template_decl)
5429 if (type_dependent_expression_p (arg0)
5430 || type_dependent_expression_p (arg1)
5431 || type_dependent_expression_p (arg2))
5432 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5433 arg0 = build_non_dependent_expr (arg0);
5434 if (arg1)
5435 arg1 = build_non_dependent_expr (arg1);
5436 arg2 = build_non_dependent_expr (arg2);
5438 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5439 if (processing_template_decl && exp != error_mark_node)
5440 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5441 orig_arg1, orig_arg2);
5442 return exp;
5445 /* Return a tree for the sum or difference (RESULTCODE says which)
5446 of pointer PTROP and integer INTOP. */
5448 static tree
5449 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5450 tree intop, tsubst_flags_t complain)
5452 tree res_type = TREE_TYPE (ptrop);
5454 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5455 in certain circumstance (when it's valid to do so). So we need
5456 to make sure it's complete. We don't need to check here, if we
5457 can actually complete it at all, as those checks will be done in
5458 pointer_int_sum() anyway. */
5459 complete_type (TREE_TYPE (res_type));
5461 return pointer_int_sum (loc, resultcode, ptrop,
5462 intop, complain & tf_warning_or_error);
5465 /* Return a tree for the difference of pointers OP0 and OP1.
5466 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5467 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5469 static tree
5470 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5471 tsubst_flags_t complain, tree *instrument_expr)
5473 tree result, inttype;
5474 tree restype = ptrdiff_type_node;
5475 tree target_type = TREE_TYPE (ptrtype);
5477 if (!complete_type_or_else (target_type, NULL_TREE))
5478 return error_mark_node;
5480 if (VOID_TYPE_P (target_type))
5482 if (complain & tf_error)
5483 permerror (loc, "ISO C++ forbids using pointer of "
5484 "type %<void *%> in subtraction");
5485 else
5486 return error_mark_node;
5488 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5490 if (complain & tf_error)
5491 permerror (loc, "ISO C++ forbids using pointer to "
5492 "a function in subtraction");
5493 else
5494 return error_mark_node;
5496 if (TREE_CODE (target_type) == METHOD_TYPE)
5498 if (complain & tf_error)
5499 permerror (loc, "ISO C++ forbids using pointer to "
5500 "a method in subtraction");
5501 else
5502 return error_mark_node;
5505 /* Determine integer type result of the subtraction. This will usually
5506 be the same as the result type (ptrdiff_t), but may need to be a wider
5507 type if pointers for the address space are wider than ptrdiff_t. */
5508 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5509 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5510 else
5511 inttype = restype;
5513 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5515 op0 = save_expr (op0);
5516 op1 = save_expr (op1);
5518 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5519 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5522 /* First do the subtraction, then build the divide operator
5523 and only convert at the very end.
5524 Do not do default conversions in case restype is a short type. */
5526 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5527 pointers. If some platform cannot provide that, or has a larger
5528 ptrdiff_type to support differences larger than half the address
5529 space, cast the pointers to some larger integer type and do the
5530 computations in that type. */
5531 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5532 op0 = cp_build_binary_op (loc,
5533 MINUS_EXPR,
5534 cp_convert (inttype, op0, complain),
5535 cp_convert (inttype, op1, complain),
5536 complain);
5537 else
5538 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5540 /* This generates an error if op1 is a pointer to an incomplete type. */
5541 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5543 if (complain & tf_error)
5544 error_at (loc, "invalid use of a pointer to an incomplete type in "
5545 "pointer arithmetic");
5546 else
5547 return error_mark_node;
5550 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5552 if (complain & tf_error)
5553 error_at (loc, "arithmetic on pointer to an empty aggregate");
5554 else
5555 return error_mark_node;
5558 op1 = (TYPE_PTROB_P (ptrtype)
5559 ? size_in_bytes_loc (loc, target_type)
5560 : integer_one_node);
5562 /* Do the division. */
5564 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5565 cp_convert (inttype, op1, complain));
5566 return cp_convert (restype, result, complain);
5569 /* Construct and perhaps optimize a tree representation
5570 for a unary operation. CODE, a tree_code, specifies the operation
5571 and XARG is the operand. */
5573 tree
5574 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5575 tsubst_flags_t complain)
5577 tree orig_expr = xarg;
5578 tree exp;
5579 int ptrmem = 0;
5580 tree overload = NULL_TREE;
5582 if (processing_template_decl)
5584 if (type_dependent_expression_p (xarg))
5585 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5587 xarg = build_non_dependent_expr (xarg);
5590 exp = NULL_TREE;
5592 /* [expr.unary.op] says:
5594 The address of an object of incomplete type can be taken.
5596 (And is just the ordinary address operator, not an overloaded
5597 "operator &".) However, if the type is a template
5598 specialization, we must complete the type at this point so that
5599 an overloaded "operator &" will be available if required. */
5600 if (code == ADDR_EXPR
5601 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5602 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5603 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5604 || (TREE_CODE (xarg) == OFFSET_REF)))
5605 /* Don't look for a function. */;
5606 else
5607 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5608 NULL_TREE, &overload, complain);
5610 if (!exp && code == ADDR_EXPR)
5612 if (is_overloaded_fn (xarg))
5614 tree fn = get_first_fn (xarg);
5615 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5617 if (complain & tf_error)
5618 error (DECL_CONSTRUCTOR_P (fn)
5619 ? G_("taking address of constructor %qD")
5620 : G_("taking address of destructor %qD"),
5621 fn);
5622 return error_mark_node;
5626 /* A pointer to member-function can be formed only by saying
5627 &X::mf. */
5628 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5629 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5631 if (TREE_CODE (xarg) != OFFSET_REF
5632 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5634 if (complain & tf_error)
5636 error ("invalid use of %qE to form a "
5637 "pointer-to-member-function", xarg.get_value ());
5638 if (TREE_CODE (xarg) != OFFSET_REF)
5639 inform (input_location, " a qualified-id is required");
5641 return error_mark_node;
5643 else
5645 if (complain & tf_error)
5646 error ("parentheses around %qE cannot be used to form a"
5647 " pointer-to-member-function",
5648 xarg.get_value ());
5649 else
5650 return error_mark_node;
5651 PTRMEM_OK_P (xarg) = 1;
5655 if (TREE_CODE (xarg) == OFFSET_REF)
5657 ptrmem = PTRMEM_OK_P (xarg);
5659 if (!ptrmem && !flag_ms_extensions
5660 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5662 /* A single non-static member, make sure we don't allow a
5663 pointer-to-member. */
5664 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5665 TREE_OPERAND (xarg, 0),
5666 ovl_make (TREE_OPERAND (xarg, 1)));
5667 PTRMEM_OK_P (xarg) = ptrmem;
5671 exp = cp_build_addr_expr_strict (xarg, complain);
5674 if (processing_template_decl && exp != error_mark_node)
5676 if (overload != NULL_TREE)
5677 return (build_min_non_dep_op_overload
5678 (code, exp, overload, orig_expr, integer_zero_node));
5680 exp = build_min_non_dep (code, exp, orig_expr,
5681 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5683 if (TREE_CODE (exp) == ADDR_EXPR)
5684 PTRMEM_OK_P (exp) = ptrmem;
5685 return exp;
5688 /* Construct and perhaps optimize a tree representation
5689 for __builtin_addressof operation. ARG specifies the operand. */
5691 tree
5692 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5694 tree orig_expr = arg;
5696 if (processing_template_decl)
5698 if (type_dependent_expression_p (arg))
5699 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5701 arg = build_non_dependent_expr (arg);
5704 tree exp = cp_build_addr_expr_strict (arg, complain);
5706 if (processing_template_decl && exp != error_mark_node)
5707 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5708 return exp;
5711 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5712 constants, where a null value is represented by an INTEGER_CST of
5713 -1. */
5715 tree
5716 cp_truthvalue_conversion (tree expr)
5718 tree type = TREE_TYPE (expr);
5719 if (TYPE_PTR_OR_PTRMEM_P (type)
5720 /* Avoid ICE on invalid use of non-static member function. */
5721 || TREE_CODE (expr) == FUNCTION_DECL)
5722 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5723 else
5724 return c_common_truthvalue_conversion (input_location, expr);
5727 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5729 tree
5730 condition_conversion (tree expr)
5732 tree t;
5733 /* Anything that might happen in a template should go through
5734 maybe_convert_cond. */
5735 gcc_assert (!processing_template_decl);
5736 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5737 tf_warning_or_error, LOOKUP_NORMAL);
5738 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5739 return t;
5742 /* Returns the address of T. This function will fold away
5743 ADDR_EXPR of INDIRECT_REF. */
5745 tree
5746 build_address (tree t)
5748 if (error_operand_p (t) || !cxx_mark_addressable (t))
5749 return error_mark_node;
5750 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5751 || processing_template_decl);
5752 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5753 if (TREE_CODE (t) != ADDR_EXPR)
5754 t = rvalue (t);
5755 return t;
5758 /* Return a NOP_EXPR converting EXPR to TYPE. */
5760 tree
5761 build_nop (tree type, tree expr)
5763 if (type == error_mark_node || error_operand_p (expr))
5764 return expr;
5765 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5768 /* Take the address of ARG, whatever that means under C++ semantics.
5769 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5770 and class rvalues as well.
5772 Nothing should call this function directly; instead, callers should use
5773 cp_build_addr_expr or cp_build_addr_expr_strict. */
5775 static tree
5776 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5778 tree argtype;
5779 tree val;
5781 if (!arg || error_operand_p (arg))
5782 return error_mark_node;
5784 arg = mark_lvalue_use (arg);
5785 if (error_operand_p (arg))
5786 return error_mark_node;
5788 argtype = lvalue_type (arg);
5790 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5792 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5793 && !really_overloaded_fn (arg))
5795 /* They're trying to take the address of a unique non-static
5796 member function. This is ill-formed (except in MS-land),
5797 but let's try to DTRT.
5798 Note: We only handle unique functions here because we don't
5799 want to complain if there's a static overload; non-unique
5800 cases will be handled by instantiate_type. But we need to
5801 handle this case here to allow casts on the resulting PMF.
5802 We could defer this in non-MS mode, but it's easier to give
5803 a useful error here. */
5805 /* Inside constant member functions, the `this' pointer
5806 contains an extra const qualifier. TYPE_MAIN_VARIANT
5807 is used here to remove this const from the diagnostics
5808 and the created OFFSET_REF. */
5809 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5810 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5811 if (!mark_used (fn, complain) && !(complain & tf_error))
5812 return error_mark_node;
5814 if (! flag_ms_extensions)
5816 tree name = DECL_NAME (fn);
5817 if (!(complain & tf_error))
5818 return error_mark_node;
5819 else if (current_class_type
5820 && TREE_OPERAND (arg, 0) == current_class_ref)
5821 /* An expression like &memfn. */
5822 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5823 " or parenthesized non-static member function to form"
5824 " a pointer to member function. Say %<&%T::%D%>",
5825 base, name);
5826 else
5827 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5828 " function to form a pointer to member function."
5829 " Say %<&%T::%D%>",
5830 base, name);
5832 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5835 /* Uninstantiated types are all functions. Taking the
5836 address of a function is a no-op, so just return the
5837 argument. */
5838 if (type_unknown_p (arg))
5839 return build1 (ADDR_EXPR, unknown_type_node, arg);
5841 if (TREE_CODE (arg) == OFFSET_REF)
5842 /* We want a pointer to member; bypass all the code for actually taking
5843 the address of something. */
5844 goto offset_ref;
5846 /* Anything not already handled and not a true memory reference
5847 is an error. */
5848 if (TREE_CODE (argtype) != FUNCTION_TYPE
5849 && TREE_CODE (argtype) != METHOD_TYPE)
5851 cp_lvalue_kind kind = lvalue_kind (arg);
5852 if (kind == clk_none)
5854 if (complain & tf_error)
5855 lvalue_error (input_location, lv_addressof);
5856 return error_mark_node;
5858 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5860 if (!(complain & tf_error))
5861 return error_mark_node;
5862 /* Make this a permerror because we used to accept it. */
5863 permerror (input_location, "taking address of rvalue");
5867 if (TYPE_REF_P (argtype))
5869 tree type = build_pointer_type (TREE_TYPE (argtype));
5870 arg = build1 (CONVERT_EXPR, type, arg);
5871 return arg;
5873 else if (pedantic && DECL_MAIN_P (arg))
5875 /* ARM $3.4 */
5876 /* Apparently a lot of autoconf scripts for C++ packages do this,
5877 so only complain if -Wpedantic. */
5878 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5879 pedwarn (input_location, OPT_Wpedantic,
5880 "ISO C++ forbids taking address of function %<::main%>");
5881 else if (flag_pedantic_errors)
5882 return error_mark_node;
5885 /* Let &* cancel out to simplify resulting code. */
5886 if (INDIRECT_REF_P (arg))
5888 arg = TREE_OPERAND (arg, 0);
5889 if (TYPE_REF_P (TREE_TYPE (arg)))
5891 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5892 arg = build1 (CONVERT_EXPR, type, arg);
5894 else
5895 /* Don't let this be an lvalue. */
5896 arg = rvalue (arg);
5897 return arg;
5900 /* Handle complex lvalues (when permitted)
5901 by reduction to simpler cases. */
5902 val = unary_complex_lvalue (ADDR_EXPR, arg);
5903 if (val != 0)
5904 return val;
5906 switch (TREE_CODE (arg))
5908 CASE_CONVERT:
5909 case FLOAT_EXPR:
5910 case FIX_TRUNC_EXPR:
5911 /* We should have handled this above in the lvalue_kind check. */
5912 gcc_unreachable ();
5913 break;
5915 case BASELINK:
5916 arg = BASELINK_FUNCTIONS (arg);
5917 /* Fall through. */
5919 case OVERLOAD:
5920 arg = OVL_FIRST (arg);
5921 break;
5923 case OFFSET_REF:
5924 offset_ref:
5925 /* Turn a reference to a non-static data member into a
5926 pointer-to-member. */
5928 tree type;
5929 tree t;
5931 gcc_assert (PTRMEM_OK_P (arg));
5933 t = TREE_OPERAND (arg, 1);
5934 if (TYPE_REF_P (TREE_TYPE (t)))
5936 if (complain & tf_error)
5937 error ("cannot create pointer to reference member %qD", t);
5938 return error_mark_node;
5941 type = build_ptrmem_type (context_for_name_lookup (t),
5942 TREE_TYPE (t));
5943 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5944 return t;
5947 default:
5948 break;
5951 if (argtype != error_mark_node)
5952 argtype = build_pointer_type (argtype);
5954 if (bitfield_p (arg))
5956 if (complain & tf_error)
5957 error ("attempt to take address of bit-field");
5958 return error_mark_node;
5961 /* In a template, we are processing a non-dependent expression
5962 so we can just form an ADDR_EXPR with the correct type. */
5963 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5965 if (TREE_CODE (arg) == FUNCTION_DECL
5966 && !mark_used (arg, complain) && !(complain & tf_error))
5967 return error_mark_node;
5968 val = build_address (arg);
5969 if (TREE_CODE (arg) == OFFSET_REF)
5970 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5972 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5974 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5976 /* We can only get here with a single static member
5977 function. */
5978 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5979 && DECL_STATIC_FUNCTION_P (fn));
5980 if (!mark_used (fn, complain) && !(complain & tf_error))
5981 return error_mark_node;
5982 val = build_address (fn);
5983 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5984 /* Do not lose object's side effects. */
5985 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5986 TREE_OPERAND (arg, 0), val);
5988 else
5990 tree object = TREE_OPERAND (arg, 0);
5991 tree field = TREE_OPERAND (arg, 1);
5992 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5993 (TREE_TYPE (object), decl_type_context (field)));
5994 val = build_address (arg);
5997 if (TYPE_PTR_P (argtype)
5998 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6000 build_ptrmemfunc_type (argtype);
6001 val = build_ptrmemfunc (argtype, val, 0,
6002 /*c_cast_p=*/false,
6003 complain);
6006 return val;
6009 /* Take the address of ARG if it has one, even if it's an rvalue. */
6011 tree
6012 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6014 return cp_build_addr_expr_1 (arg, 0, complain);
6017 /* Take the address of ARG, but only if it's an lvalue. */
6019 static tree
6020 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6022 return cp_build_addr_expr_1 (arg, 1, complain);
6025 /* C++: Must handle pointers to members.
6027 Perhaps type instantiation should be extended to handle conversion
6028 from aggregates to types we don't yet know we want? (Or are those
6029 cases typically errors which should be reported?)
6031 NOCONVERT suppresses the default promotions (such as from short to int). */
6033 tree
6034 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6035 tsubst_flags_t complain)
6037 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6038 tree arg = xarg;
6039 location_t location = EXPR_LOC_OR_LOC (arg, input_location);
6040 tree argtype = 0;
6041 const char *errstring = NULL;
6042 tree val;
6043 const char *invalid_op_diag;
6045 if (!arg || error_operand_p (arg))
6046 return error_mark_node;
6048 if ((invalid_op_diag
6049 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6050 ? CONVERT_EXPR
6051 : code),
6052 TREE_TYPE (xarg))))
6054 if (complain & tf_error)
6055 error (invalid_op_diag);
6056 return error_mark_node;
6059 switch (code)
6061 case UNARY_PLUS_EXPR:
6062 case NEGATE_EXPR:
6064 int flags = WANT_ARITH | WANT_ENUM;
6065 /* Unary plus (but not unary minus) is allowed on pointers. */
6066 if (code == UNARY_PLUS_EXPR)
6067 flags |= WANT_POINTER;
6068 arg = build_expr_type_conversion (flags, arg, true);
6069 if (!arg)
6070 errstring = (code == NEGATE_EXPR
6071 ? _("wrong type argument to unary minus")
6072 : _("wrong type argument to unary plus"));
6073 else
6075 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6076 arg = cp_perform_integral_promotions (arg, complain);
6078 /* Make sure the result is not an lvalue: a unary plus or minus
6079 expression is always a rvalue. */
6080 arg = rvalue (arg);
6083 break;
6085 case BIT_NOT_EXPR:
6086 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6088 code = CONJ_EXPR;
6089 if (!noconvert)
6091 arg = cp_default_conversion (arg, complain);
6092 if (arg == error_mark_node)
6093 return error_mark_node;
6096 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6097 | WANT_VECTOR_OR_COMPLEX,
6098 arg, true)))
6099 errstring = _("wrong type argument to bit-complement");
6100 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6102 /* Warn if the expression has boolean value. */
6103 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6104 && (complain & tf_warning)
6105 && warning_at (location, OPT_Wbool_operation,
6106 "%<~%> on an expression of type bool"))
6107 inform (location, "did you mean to use logical not (%<!%>)?");
6108 arg = cp_perform_integral_promotions (arg, complain);
6110 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6111 arg = mark_rvalue_use (arg);
6112 break;
6114 case ABS_EXPR:
6115 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6116 errstring = _("wrong type argument to abs");
6117 else if (!noconvert)
6119 arg = cp_default_conversion (arg, complain);
6120 if (arg == error_mark_node)
6121 return error_mark_node;
6123 break;
6125 case CONJ_EXPR:
6126 /* Conjugating a real value is a no-op, but allow it anyway. */
6127 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6128 errstring = _("wrong type argument to conjugation");
6129 else if (!noconvert)
6131 arg = cp_default_conversion (arg, complain);
6132 if (arg == error_mark_node)
6133 return error_mark_node;
6135 break;
6137 case TRUTH_NOT_EXPR:
6138 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6139 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6140 build_zero_cst (TREE_TYPE (arg)), complain);
6141 arg = perform_implicit_conversion (boolean_type_node, arg,
6142 complain);
6143 val = invert_truthvalue_loc (input_location, arg);
6144 if (arg != error_mark_node)
6145 return val;
6146 errstring = _("in argument to unary !");
6147 break;
6149 case NOP_EXPR:
6150 break;
6152 case REALPART_EXPR:
6153 case IMAGPART_EXPR:
6154 arg = build_real_imag_expr (input_location, code, arg);
6155 return arg;
6157 case PREINCREMENT_EXPR:
6158 case POSTINCREMENT_EXPR:
6159 case PREDECREMENT_EXPR:
6160 case POSTDECREMENT_EXPR:
6161 /* Handle complex lvalues (when permitted)
6162 by reduction to simpler cases. */
6164 val = unary_complex_lvalue (code, arg);
6165 if (val != 0)
6166 return val;
6168 arg = mark_lvalue_use (arg);
6170 /* Increment or decrement the real part of the value,
6171 and don't change the imaginary part. */
6172 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6174 tree real, imag;
6176 arg = cp_stabilize_reference (arg);
6177 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6178 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6179 real = cp_build_unary_op (code, real, true, complain);
6180 if (real == error_mark_node || imag == error_mark_node)
6181 return error_mark_node;
6182 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6183 real, imag);
6186 /* Report invalid types. */
6188 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6189 arg, true)))
6191 if (code == PREINCREMENT_EXPR)
6192 errstring = _("no pre-increment operator for type");
6193 else if (code == POSTINCREMENT_EXPR)
6194 errstring = _("no post-increment operator for type");
6195 else if (code == PREDECREMENT_EXPR)
6196 errstring = _("no pre-decrement operator for type");
6197 else
6198 errstring = _("no post-decrement operator for type");
6199 break;
6201 else if (arg == error_mark_node)
6202 return error_mark_node;
6204 /* Report something read-only. */
6206 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6207 || TREE_READONLY (arg))
6209 if (complain & tf_error)
6210 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6211 || code == POSTINCREMENT_EXPR)
6212 ? lv_increment : lv_decrement));
6213 else
6214 return error_mark_node;
6218 tree inc;
6219 tree declared_type = unlowered_expr_type (arg);
6221 argtype = TREE_TYPE (arg);
6223 /* ARM $5.2.5 last annotation says this should be forbidden. */
6224 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6226 if (complain & tf_error)
6227 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6228 ? G_("ISO C++ forbids incrementing an enum")
6229 : G_("ISO C++ forbids decrementing an enum"));
6230 else
6231 return error_mark_node;
6234 /* Compute the increment. */
6236 if (TYPE_PTR_P (argtype))
6238 tree type = complete_type (TREE_TYPE (argtype));
6240 if (!COMPLETE_OR_VOID_TYPE_P (type))
6242 if (complain & tf_error)
6243 error (((code == PREINCREMENT_EXPR
6244 || code == POSTINCREMENT_EXPR))
6245 ? G_("cannot increment a pointer to incomplete type %qT")
6246 : G_("cannot decrement a pointer to incomplete type %qT"),
6247 TREE_TYPE (argtype));
6248 else
6249 return error_mark_node;
6251 else if (!TYPE_PTROB_P (argtype))
6253 if (complain & tf_error)
6254 pedwarn (input_location, OPT_Wpointer_arith,
6255 (code == PREINCREMENT_EXPR
6256 || code == POSTINCREMENT_EXPR)
6257 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6258 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6259 argtype);
6260 else
6261 return error_mark_node;
6264 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6266 else
6267 inc = VECTOR_TYPE_P (argtype)
6268 ? build_one_cst (argtype)
6269 : integer_one_node;
6271 inc = cp_convert (argtype, inc, complain);
6273 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6274 need to ask Objective-C to build the increment or decrement
6275 expression for it. */
6276 if (objc_is_property_ref (arg))
6277 return objc_build_incr_expr_for_property_ref (input_location, code,
6278 arg, inc);
6280 /* Complain about anything else that is not a true lvalue. */
6281 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6282 || code == POSTINCREMENT_EXPR)
6283 ? lv_increment : lv_decrement),
6284 complain))
6285 return error_mark_node;
6287 /* Forbid using -- or ++ in C++17 on `bool'. */
6288 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6290 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6292 if (complain & tf_error)
6293 error ("use of an operand of type %qT in %<operator--%> "
6294 "is forbidden", boolean_type_node);
6295 return error_mark_node;
6297 else
6299 if (cxx_dialect >= cxx17)
6301 if (complain & tf_error)
6302 error ("use of an operand of type %qT in "
6303 "%<operator++%> is forbidden in C++17",
6304 boolean_type_node);
6305 return error_mark_node;
6307 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6308 else if (!in_system_header_at (input_location))
6309 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6310 "in %<operator++%> is deprecated",
6311 boolean_type_node);
6313 val = boolean_increment (code, arg);
6315 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6316 /* An rvalue has no cv-qualifiers. */
6317 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6318 else
6319 val = build2 (code, TREE_TYPE (arg), arg, inc);
6321 TREE_SIDE_EFFECTS (val) = 1;
6322 return val;
6325 case ADDR_EXPR:
6326 /* Note that this operation never does default_conversion
6327 regardless of NOCONVERT. */
6328 return cp_build_addr_expr (arg, complain);
6330 default:
6331 break;
6334 if (!errstring)
6336 if (argtype == 0)
6337 argtype = TREE_TYPE (arg);
6338 return build1 (code, argtype, arg);
6341 if (complain & tf_error)
6342 error ("%s", errstring);
6343 return error_mark_node;
6346 /* Hook for the c-common bits that build a unary op. */
6347 tree
6348 build_unary_op (location_t /*location*/,
6349 enum tree_code code, tree xarg, bool noconvert)
6351 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6354 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6355 so that it is a valid lvalue even for GENERIC by replacing
6356 (lhs = rhs) with ((lhs = rhs), lhs)
6357 (--lhs) with ((--lhs), lhs)
6358 (++lhs) with ((++lhs), lhs)
6359 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6360 that it can be evaluated multiple times. */
6362 tree
6363 genericize_compound_lvalue (tree lvalue)
6365 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6366 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6367 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6368 TREE_OPERAND (lvalue, 1));
6369 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6370 lvalue, TREE_OPERAND (lvalue, 0));
6373 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6374 for certain kinds of expressions which are not really lvalues
6375 but which we can accept as lvalues.
6377 If ARG is not a kind of expression we can handle, return
6378 NULL_TREE. */
6380 tree
6381 unary_complex_lvalue (enum tree_code code, tree arg)
6383 /* Inside a template, making these kinds of adjustments is
6384 pointless; we are only concerned with the type of the
6385 expression. */
6386 if (processing_template_decl)
6387 return NULL_TREE;
6389 /* Handle (a, b) used as an "lvalue". */
6390 if (TREE_CODE (arg) == COMPOUND_EXPR)
6392 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6393 tf_warning_or_error);
6394 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6395 TREE_OPERAND (arg, 0), real_result);
6398 /* Handle (a ? b : c) used as an "lvalue". */
6399 if (TREE_CODE (arg) == COND_EXPR
6400 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6401 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6403 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6404 if (TREE_CODE (arg) == MODIFY_EXPR
6405 || TREE_CODE (arg) == PREINCREMENT_EXPR
6406 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6407 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6409 if (code != ADDR_EXPR)
6410 return NULL_TREE;
6412 /* Handle (a = b) used as an "lvalue" for `&'. */
6413 if (TREE_CODE (arg) == MODIFY_EXPR
6414 || TREE_CODE (arg) == INIT_EXPR)
6416 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6417 tf_warning_or_error);
6418 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6419 arg, real_result);
6420 TREE_NO_WARNING (arg) = 1;
6421 return arg;
6424 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6425 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6426 || TREE_CODE (arg) == OFFSET_REF)
6427 return NULL_TREE;
6429 /* We permit compiler to make function calls returning
6430 objects of aggregate type look like lvalues. */
6432 tree targ = arg;
6434 if (TREE_CODE (targ) == SAVE_EXPR)
6435 targ = TREE_OPERAND (targ, 0);
6437 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6439 if (TREE_CODE (arg) == SAVE_EXPR)
6440 targ = arg;
6441 else
6442 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6443 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6446 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6447 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6448 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6451 /* Don't let anything else be handled specially. */
6452 return NULL_TREE;
6455 /* Mark EXP saying that we need to be able to take the
6456 address of it; it should not be allocated in a register.
6457 Value is true if successful. ARRAY_REF_P is true if this
6458 is for ARRAY_REF construction - in that case we don't want
6459 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6460 it is fine to use ARRAY_REFs for vector subscripts on vector
6461 register variables.
6463 C++: we do not allow `current_class_ptr' to be addressable. */
6465 bool
6466 cxx_mark_addressable (tree exp, bool array_ref_p)
6468 tree x = exp;
6470 while (1)
6471 switch (TREE_CODE (x))
6473 case VIEW_CONVERT_EXPR:
6474 if (array_ref_p
6475 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6476 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6477 return true;
6478 /* FALLTHRU */
6479 case ADDR_EXPR:
6480 case COMPONENT_REF:
6481 case ARRAY_REF:
6482 case REALPART_EXPR:
6483 case IMAGPART_EXPR:
6484 x = TREE_OPERAND (x, 0);
6485 break;
6487 case PARM_DECL:
6488 if (x == current_class_ptr)
6490 error ("cannot take the address of %<this%>, which is an rvalue expression");
6491 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6492 return true;
6494 /* Fall through. */
6496 case VAR_DECL:
6497 /* Caller should not be trying to mark initialized
6498 constant fields addressable. */
6499 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6500 || DECL_IN_AGGR_P (x) == 0
6501 || TREE_STATIC (x)
6502 || DECL_EXTERNAL (x));
6503 /* Fall through. */
6505 case RESULT_DECL:
6506 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6507 && !DECL_ARTIFICIAL (x))
6509 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6511 error
6512 ("address of explicit register variable %qD requested", x);
6513 return false;
6515 else if (extra_warnings)
6516 warning
6517 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6519 TREE_ADDRESSABLE (x) = 1;
6520 return true;
6522 case CONST_DECL:
6523 case FUNCTION_DECL:
6524 TREE_ADDRESSABLE (x) = 1;
6525 return true;
6527 case CONSTRUCTOR:
6528 TREE_ADDRESSABLE (x) = 1;
6529 return true;
6531 case TARGET_EXPR:
6532 TREE_ADDRESSABLE (x) = 1;
6533 cxx_mark_addressable (TREE_OPERAND (x, 0));
6534 return true;
6536 default:
6537 return true;
6541 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6543 tree
6544 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6545 tsubst_flags_t complain)
6547 tree orig_ifexp = ifexp;
6548 tree orig_op1 = op1;
6549 tree orig_op2 = op2;
6550 tree expr;
6552 if (processing_template_decl)
6554 /* The standard says that the expression is type-dependent if
6555 IFEXP is type-dependent, even though the eventual type of the
6556 expression doesn't dependent on IFEXP. */
6557 if (type_dependent_expression_p (ifexp)
6558 /* As a GNU extension, the middle operand may be omitted. */
6559 || (op1 && type_dependent_expression_p (op1))
6560 || type_dependent_expression_p (op2))
6561 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6562 ifexp = build_non_dependent_expr (ifexp);
6563 if (op1)
6564 op1 = build_non_dependent_expr (op1);
6565 op2 = build_non_dependent_expr (op2);
6568 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6569 if (processing_template_decl && expr != error_mark_node)
6571 tree min = build_min_non_dep (COND_EXPR, expr,
6572 orig_ifexp, orig_op1, orig_op2);
6573 expr = convert_from_reference (min);
6575 return expr;
6578 /* Given a list of expressions, return a compound expression
6579 that performs them all and returns the value of the last of them. */
6581 tree
6582 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6583 tsubst_flags_t complain)
6585 tree expr = TREE_VALUE (list);
6587 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6588 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6590 if (complain & tf_error)
6591 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6592 "list-initializer for non-class type must not "
6593 "be parenthesized");
6594 else
6595 return error_mark_node;
6598 if (TREE_CHAIN (list))
6600 if (complain & tf_error)
6601 switch (exp)
6603 case ELK_INIT:
6604 permerror (input_location, "expression list treated as compound "
6605 "expression in initializer");
6606 break;
6607 case ELK_MEM_INIT:
6608 permerror (input_location, "expression list treated as compound "
6609 "expression in mem-initializer");
6610 break;
6611 case ELK_FUNC_CAST:
6612 permerror (input_location, "expression list treated as compound "
6613 "expression in functional cast");
6614 break;
6615 default:
6616 gcc_unreachable ();
6618 else
6619 return error_mark_node;
6621 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6622 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6623 expr, TREE_VALUE (list), complain);
6626 return expr;
6629 /* Like build_x_compound_expr_from_list, but using a VEC. */
6631 tree
6632 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6633 tsubst_flags_t complain)
6635 if (vec_safe_is_empty (vec))
6636 return NULL_TREE;
6637 else if (vec->length () == 1)
6638 return (*vec)[0];
6639 else
6641 tree expr;
6642 unsigned int ix;
6643 tree t;
6645 if (msg != NULL)
6647 if (complain & tf_error)
6648 permerror (input_location,
6649 "%s expression list treated as compound expression",
6650 msg);
6651 else
6652 return error_mark_node;
6655 expr = (*vec)[0];
6656 for (ix = 1; vec->iterate (ix, &t); ++ix)
6657 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6658 t, complain);
6660 return expr;
6664 /* Handle overloading of the ',' operator when needed. */
6666 tree
6667 build_x_compound_expr (location_t loc, tree op1, tree op2,
6668 tsubst_flags_t complain)
6670 tree result;
6671 tree orig_op1 = op1;
6672 tree orig_op2 = op2;
6673 tree overload = NULL_TREE;
6675 if (processing_template_decl)
6677 if (type_dependent_expression_p (op1)
6678 || type_dependent_expression_p (op2))
6679 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6680 op1 = build_non_dependent_expr (op1);
6681 op2 = build_non_dependent_expr (op2);
6684 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6685 NULL_TREE, &overload, complain);
6686 if (!result)
6687 result = cp_build_compound_expr (op1, op2, complain);
6689 if (processing_template_decl && result != error_mark_node)
6691 if (overload != NULL_TREE)
6692 return (build_min_non_dep_op_overload
6693 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6695 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6698 return result;
6701 /* Like cp_build_compound_expr, but for the c-common bits. */
6703 tree
6704 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6706 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6709 /* Build a compound expression. */
6711 tree
6712 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6714 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6716 if (lhs == error_mark_node || rhs == error_mark_node)
6717 return error_mark_node;
6719 if (TREE_CODE (rhs) == TARGET_EXPR)
6721 /* If the rhs is a TARGET_EXPR, then build the compound
6722 expression inside the target_expr's initializer. This
6723 helps the compiler to eliminate unnecessary temporaries. */
6724 tree init = TREE_OPERAND (rhs, 1);
6726 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6727 TREE_OPERAND (rhs, 1) = init;
6729 return rhs;
6732 if (type_unknown_p (rhs))
6734 if (complain & tf_error)
6735 error ("no context to resolve type of %qE", rhs);
6736 return error_mark_node;
6739 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6742 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6743 casts away constness. CAST gives the type of cast. Returns true
6744 if the cast is ill-formed, false if it is well-formed.
6746 ??? This function warns for casting away any qualifier not just
6747 const. We would like to specify exactly what qualifiers are casted
6748 away.
6751 static bool
6752 check_for_casting_away_constness (tree src_type, tree dest_type,
6753 enum tree_code cast, tsubst_flags_t complain)
6755 /* C-style casts are allowed to cast away constness. With
6756 WARN_CAST_QUAL, we still want to issue a warning. */
6757 if (cast == CAST_EXPR && !warn_cast_qual)
6758 return false;
6760 if (!casts_away_constness (src_type, dest_type, complain))
6761 return false;
6763 switch (cast)
6765 case CAST_EXPR:
6766 if (complain & tf_warning)
6767 warning (OPT_Wcast_qual,
6768 "cast from type %qT to type %qT casts away qualifiers",
6769 src_type, dest_type);
6770 return false;
6772 case STATIC_CAST_EXPR:
6773 if (complain & tf_error)
6774 error ("static_cast from type %qT to type %qT casts away qualifiers",
6775 src_type, dest_type);
6776 return true;
6778 case REINTERPRET_CAST_EXPR:
6779 if (complain & tf_error)
6780 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6781 src_type, dest_type);
6782 return true;
6784 default:
6785 gcc_unreachable();
6789 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6790 void
6791 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6793 if (warn_useless_cast
6794 && complain & tf_warning)
6796 if ((TYPE_REF_P (type)
6797 && (TYPE_REF_IS_RVALUE (type)
6798 ? xvalue_p (expr) : lvalue_p (expr))
6799 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6800 || same_type_p (TREE_TYPE (expr), type))
6801 warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6805 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6806 void
6807 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6809 if (warn_ignored_qualifiers
6810 && complain & tf_warning
6811 && !CLASS_TYPE_P (type)
6812 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6814 warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6815 "result type");
6819 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6820 (another pointer-to-member type in the same hierarchy) and return
6821 the converted expression. If ALLOW_INVERSE_P is permitted, a
6822 pointer-to-derived may be converted to pointer-to-base; otherwise,
6823 only the other direction is permitted. If C_CAST_P is true, this
6824 conversion is taking place as part of a C-style cast. */
6826 tree
6827 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6828 bool c_cast_p, tsubst_flags_t complain)
6830 if (same_type_p (type, TREE_TYPE (expr)))
6831 return expr;
6833 if (TYPE_PTRDATAMEM_P (type))
6835 tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
6836 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
6837 tree delta = (get_delta_difference
6838 (obase, nbase,
6839 allow_inverse_p, c_cast_p, complain));
6841 if (delta == error_mark_node)
6842 return error_mark_node;
6844 if (!same_type_p (obase, nbase))
6846 if (TREE_CODE (expr) == PTRMEM_CST)
6847 expr = cplus_expand_constant (expr);
6849 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
6850 build_int_cst (TREE_TYPE (expr), -1),
6851 complain);
6852 tree op1 = build_nop (ptrdiff_type_node, expr);
6853 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
6854 complain);
6856 expr = fold_build3_loc (input_location,
6857 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6860 return build_nop (type, expr);
6862 else
6863 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6864 allow_inverse_p, c_cast_p, complain);
6867 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6868 this static_cast is being attempted as one of the possible casts
6869 allowed by a C-style cast. (In that case, accessibility of base
6870 classes is not considered, and it is OK to cast away
6871 constness.) Return the result of the cast. *VALID_P is set to
6872 indicate whether or not the cast was valid. */
6874 static tree
6875 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6876 bool *valid_p, tsubst_flags_t complain)
6878 tree intype;
6879 tree result;
6880 cp_lvalue_kind clk;
6882 /* Assume the cast is valid. */
6883 *valid_p = true;
6885 intype = unlowered_expr_type (expr);
6887 /* Save casted types in the function's used types hash table. */
6888 used_types_insert (type);
6890 /* A prvalue of non-class type is cv-unqualified. */
6891 if (!CLASS_TYPE_P (type))
6892 type = cv_unqualified (type);
6894 /* [expr.static.cast]
6896 An lvalue of type "cv1 B", where B is a class type, can be cast
6897 to type "reference to cv2 D", where D is a class derived (clause
6898 _class.derived_) from B, if a valid standard conversion from
6899 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6900 same cv-qualification as, or greater cv-qualification than, cv1,
6901 and B is not a virtual base class of D. */
6902 /* We check this case before checking the validity of "TYPE t =
6903 EXPR;" below because for this case:
6905 struct B {};
6906 struct D : public B { D(const B&); };
6907 extern B& b;
6908 void f() { static_cast<const D&>(b); }
6910 we want to avoid constructing a new D. The standard is not
6911 completely clear about this issue, but our interpretation is
6912 consistent with other compilers. */
6913 if (TYPE_REF_P (type)
6914 && CLASS_TYPE_P (TREE_TYPE (type))
6915 && CLASS_TYPE_P (intype)
6916 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6917 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6918 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6919 build_pointer_type (TYPE_MAIN_VARIANT
6920 (TREE_TYPE (type))),
6921 complain)
6922 && (c_cast_p
6923 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6925 tree base;
6927 if (processing_template_decl)
6928 return expr;
6930 /* There is a standard conversion from "D*" to "B*" even if "B"
6931 is ambiguous or inaccessible. If this is really a
6932 static_cast, then we check both for inaccessibility and
6933 ambiguity. However, if this is a static_cast being performed
6934 because the user wrote a C-style cast, then accessibility is
6935 not considered. */
6936 base = lookup_base (TREE_TYPE (type), intype,
6937 c_cast_p ? ba_unique : ba_check,
6938 NULL, complain);
6939 expr = build_address (expr);
6941 if (sanitize_flags_p (SANITIZE_VPTR))
6943 tree ubsan_check
6944 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6945 intype, expr);
6946 if (ubsan_check)
6947 expr = ubsan_check;
6950 /* Convert from "B*" to "D*". This function will check that "B"
6951 is not a virtual base of "D". Even if we don't have a guarantee
6952 that expr is NULL, if the static_cast is to a reference type,
6953 it is UB if it would be NULL, so omit the non-NULL check. */
6954 expr = build_base_path (MINUS_EXPR, expr, base,
6955 /*nonnull=*/flag_delete_null_pointer_checks,
6956 complain);
6958 /* Convert the pointer to a reference -- but then remember that
6959 there are no expressions with reference type in C++.
6961 We call rvalue so that there's an actual tree code
6962 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6963 is a variable with the same type, the conversion would get folded
6964 away, leaving just the variable and causing lvalue_kind to give
6965 the wrong answer. */
6966 expr = cp_fold_convert (type, expr);
6968 /* When -fsanitize=null, make sure to diagnose reference binding to
6969 NULL even when the reference is converted to pointer later on. */
6970 if (sanitize_flags_p (SANITIZE_NULL)
6971 && TREE_CODE (expr) == COND_EXPR
6972 && TREE_OPERAND (expr, 2)
6973 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
6974 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
6975 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
6977 return convert_from_reference (rvalue (expr));
6980 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6981 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6982 if (TYPE_REF_P (type)
6983 && TYPE_REF_IS_RVALUE (type)
6984 && (clk = real_lvalue_p (expr))
6985 && reference_related_p (TREE_TYPE (type), intype)
6986 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6988 if (processing_template_decl)
6989 return expr;
6990 if (clk == clk_ordinary)
6992 /* Handle the (non-bit-field) lvalue case here by casting to
6993 lvalue reference and then changing it to an rvalue reference.
6994 Casting an xvalue to rvalue reference will be handled by the
6995 main code path. */
6996 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6997 result = (perform_direct_initialization_if_possible
6998 (lref, expr, c_cast_p, complain));
6999 result = build1 (NON_LVALUE_EXPR, type, result);
7000 return convert_from_reference (result);
7002 else
7003 /* For a bit-field or packed field, bind to a temporary. */
7004 expr = rvalue (expr);
7007 /* Resolve overloaded address here rather than once in
7008 implicit_conversion and again in the inverse code below. */
7009 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7011 expr = instantiate_type (type, expr, complain);
7012 intype = TREE_TYPE (expr);
7015 /* [expr.static.cast]
7017 Any expression can be explicitly converted to type cv void. */
7018 if (VOID_TYPE_P (type))
7019 return convert_to_void (expr, ICV_CAST, complain);
7021 /* [class.abstract]
7022 An abstract class shall not be used ... as the type of an explicit
7023 conversion. */
7024 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7025 return error_mark_node;
7027 /* [expr.static.cast]
7029 An expression e can be explicitly converted to a type T using a
7030 static_cast of the form static_cast<T>(e) if the declaration T
7031 t(e);" is well-formed, for some invented temporary variable
7032 t. */
7033 result = perform_direct_initialization_if_possible (type, expr,
7034 c_cast_p, complain);
7035 if (result)
7037 if (processing_template_decl)
7038 return expr;
7040 result = convert_from_reference (result);
7042 /* [expr.static.cast]
7044 If T is a reference type, the result is an lvalue; otherwise,
7045 the result is an rvalue. */
7046 if (!TYPE_REF_P (type))
7048 result = rvalue (result);
7050 if (result == expr && SCALAR_TYPE_P (type))
7051 /* Leave some record of the cast. */
7052 result = build_nop (type, expr);
7054 return result;
7057 /* [expr.static.cast]
7059 The inverse of any standard conversion sequence (clause _conv_),
7060 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7061 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7062 (_conv.bool_) conversions, can be performed explicitly using
7063 static_cast subject to the restriction that the explicit
7064 conversion does not cast away constness (_expr.const.cast_), and
7065 the following additional rules for specific cases: */
7066 /* For reference, the conversions not excluded are: integral
7067 promotions, floating point promotion, integral conversions,
7068 floating point conversions, floating-integral conversions,
7069 pointer conversions, and pointer to member conversions. */
7070 /* DR 128
7072 A value of integral _or enumeration_ type can be explicitly
7073 converted to an enumeration type. */
7074 /* The effect of all that is that any conversion between any two
7075 types which are integral, floating, or enumeration types can be
7076 performed. */
7077 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7078 || SCALAR_FLOAT_TYPE_P (type))
7079 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7080 || SCALAR_FLOAT_TYPE_P (intype)))
7082 if (processing_template_decl)
7083 return expr;
7084 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7087 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7088 && CLASS_TYPE_P (TREE_TYPE (type))
7089 && CLASS_TYPE_P (TREE_TYPE (intype))
7090 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7091 (TREE_TYPE (intype))),
7092 build_pointer_type (TYPE_MAIN_VARIANT
7093 (TREE_TYPE (type))),
7094 complain))
7096 tree base;
7098 if (processing_template_decl)
7099 return expr;
7101 if (!c_cast_p
7102 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7103 complain))
7104 return error_mark_node;
7105 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7106 c_cast_p ? ba_unique : ba_check,
7107 NULL, complain);
7108 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7109 complain);
7111 if (sanitize_flags_p (SANITIZE_VPTR))
7113 tree ubsan_check
7114 = cp_ubsan_maybe_instrument_downcast (input_location, type,
7115 intype, expr);
7116 if (ubsan_check)
7117 expr = ubsan_check;
7120 return cp_fold_convert (type, expr);
7123 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7124 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7126 tree c1;
7127 tree c2;
7128 tree t1;
7129 tree t2;
7131 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7132 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7134 if (TYPE_PTRDATAMEM_P (type))
7136 t1 = (build_ptrmem_type
7137 (c1,
7138 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7139 t2 = (build_ptrmem_type
7140 (c2,
7141 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7143 else
7145 t1 = intype;
7146 t2 = type;
7148 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7150 if (!c_cast_p
7151 && check_for_casting_away_constness (intype, type,
7152 STATIC_CAST_EXPR,
7153 complain))
7154 return error_mark_node;
7155 if (processing_template_decl)
7156 return expr;
7157 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7158 c_cast_p, complain);
7162 /* [expr.static.cast]
7164 An rvalue of type "pointer to cv void" can be explicitly
7165 converted to a pointer to object type. A value of type pointer
7166 to object converted to "pointer to cv void" and back to the
7167 original pointer type will have its original value. */
7168 if (TYPE_PTR_P (intype)
7169 && VOID_TYPE_P (TREE_TYPE (intype))
7170 && TYPE_PTROB_P (type))
7172 if (!c_cast_p
7173 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7174 complain))
7175 return error_mark_node;
7176 if (processing_template_decl)
7177 return expr;
7178 return build_nop (type, expr);
7181 *valid_p = false;
7182 return error_mark_node;
7185 /* Return an expression representing static_cast<TYPE>(EXPR). */
7187 tree
7188 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7190 tree expr = oexpr;
7191 tree result;
7192 bool valid_p;
7194 if (type == error_mark_node || expr == error_mark_node)
7195 return error_mark_node;
7197 bool dependent = (dependent_type_p (type)
7198 || type_dependent_expression_p (expr));
7199 if (dependent)
7201 tmpl:
7202 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7203 /* We don't know if it will or will not have side effects. */
7204 TREE_SIDE_EFFECTS (expr) = 1;
7205 return convert_from_reference (expr);
7207 else if (processing_template_decl)
7208 expr = build_non_dependent_expr (expr);
7210 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7211 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7212 if (!TYPE_REF_P (type)
7213 && TREE_CODE (expr) == NOP_EXPR
7214 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7215 expr = TREE_OPERAND (expr, 0);
7217 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7218 complain);
7219 if (valid_p)
7221 if (result != error_mark_node)
7223 maybe_warn_about_useless_cast (type, expr, complain);
7224 maybe_warn_about_cast_ignoring_quals (type, complain);
7226 if (processing_template_decl)
7227 goto tmpl;
7228 return result;
7231 if (complain & tf_error)
7232 error ("invalid static_cast from type %qT to type %qT",
7233 TREE_TYPE (expr), type);
7234 return error_mark_node;
7237 /* EXPR is an expression with member function or pointer-to-member
7238 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7239 not permitted by ISO C++, but we accept it in some modes. If we
7240 are not in one of those modes, issue a diagnostic. Return the
7241 converted expression. */
7243 tree
7244 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7246 tree intype;
7247 tree decl;
7249 intype = TREE_TYPE (expr);
7250 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7251 || TREE_CODE (intype) == METHOD_TYPE);
7253 if (!(complain & tf_warning_or_error))
7254 return error_mark_node;
7256 if (pedantic || warn_pmf2ptr)
7257 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7258 "converting from %qH to %qI", intype, type);
7260 if (TREE_CODE (intype) == METHOD_TYPE)
7261 expr = build_addr_func (expr, complain);
7262 else if (TREE_CODE (expr) == PTRMEM_CST)
7263 expr = build_address (PTRMEM_CST_MEMBER (expr));
7264 else
7266 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7267 decl = build_address (decl);
7268 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7271 if (expr == error_mark_node)
7272 return error_mark_node;
7274 return build_nop (type, expr);
7277 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7278 constexpr evaluation knows to reject it. */
7280 static tree
7281 build_nop_reinterpret (tree type, tree expr)
7283 tree ret = build_nop (type, expr);
7284 if (ret != expr)
7285 REINTERPRET_CAST_P (ret) = true;
7286 return ret;
7289 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7290 If C_CAST_P is true, this reinterpret cast is being done as part of
7291 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7292 indicate whether or not reinterpret_cast was valid. */
7294 static tree
7295 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7296 bool *valid_p, tsubst_flags_t complain)
7298 tree intype;
7300 /* Assume the cast is invalid. */
7301 if (valid_p)
7302 *valid_p = true;
7304 if (type == error_mark_node || error_operand_p (expr))
7305 return error_mark_node;
7307 intype = TREE_TYPE (expr);
7309 /* Save casted types in the function's used types hash table. */
7310 used_types_insert (type);
7312 /* A prvalue of non-class type is cv-unqualified. */
7313 if (!CLASS_TYPE_P (type))
7314 type = cv_unqualified (type);
7316 /* [expr.reinterpret.cast]
7317 A glvalue expression of type T1 can be cast to the type
7318 "reference to T2" if an expression of type "pointer to T1" can be
7319 explicitly converted to the type "pointer to T2" using a
7320 reinterpret_cast. */
7321 if (TYPE_REF_P (type))
7323 if (TYPE_REF_IS_RVALUE (type))
7325 if (!obvalue_p (expr))
7326 /* Perform the temporary materialization conversion. */
7327 expr = get_target_expr_sfinae (expr, complain);
7329 else if (!lvalue_p (expr))
7331 if (complain & tf_error)
7332 error ("invalid cast of an rvalue expression of type "
7333 "%qT to type %qT",
7334 intype, type);
7335 return error_mark_node;
7338 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7339 "B" are related class types; the reinterpret_cast does not
7340 adjust the pointer. */
7341 if (TYPE_PTR_P (intype)
7342 && (complain & tf_warning)
7343 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7344 COMPARE_BASE | COMPARE_DERIVED)))
7345 warning (0, "casting %qT to %qT does not dereference pointer",
7346 intype, type);
7348 expr = cp_build_addr_expr (expr, complain);
7350 if (warn_strict_aliasing > 2)
7351 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7353 if (expr != error_mark_node)
7354 expr = build_reinterpret_cast_1
7355 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7356 valid_p, complain);
7357 if (expr != error_mark_node)
7358 /* cp_build_indirect_ref isn't right for rvalue refs. */
7359 expr = convert_from_reference (fold_convert (type, expr));
7360 return expr;
7363 /* As a G++ extension, we consider conversions from member
7364 functions, and pointers to member functions to
7365 pointer-to-function and pointer-to-void types. If
7366 -Wno-pmf-conversions has not been specified,
7367 convert_member_func_to_ptr will issue an error message. */
7368 if ((TYPE_PTRMEMFUNC_P (intype)
7369 || TREE_CODE (intype) == METHOD_TYPE)
7370 && TYPE_PTR_P (type)
7371 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7372 || VOID_TYPE_P (TREE_TYPE (type))))
7373 return convert_member_func_to_ptr (type, expr, complain);
7375 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7376 array-to-pointer, and function-to-pointer conversions are
7377 performed. */
7378 expr = decay_conversion (expr, complain);
7380 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7381 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7382 if (TREE_CODE (expr) == NOP_EXPR
7383 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7384 expr = TREE_OPERAND (expr, 0);
7386 if (error_operand_p (expr))
7387 return error_mark_node;
7389 intype = TREE_TYPE (expr);
7391 /* [expr.reinterpret.cast]
7392 A pointer can be converted to any integral type large enough to
7393 hold it. ... A value of type std::nullptr_t can be converted to
7394 an integral type; the conversion has the same meaning and
7395 validity as a conversion of (void*)0 to the integral type. */
7396 if (CP_INTEGRAL_TYPE_P (type)
7397 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7399 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7401 if (complain & tf_error)
7402 permerror (input_location, "cast from %qH to %qI loses precision",
7403 intype, type);
7404 else
7405 return error_mark_node;
7407 if (NULLPTR_TYPE_P (intype))
7408 return build_int_cst (type, 0);
7410 /* [expr.reinterpret.cast]
7411 A value of integral or enumeration type can be explicitly
7412 converted to a pointer. */
7413 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7414 /* OK */
7416 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7417 || TYPE_PTR_OR_PTRMEM_P (type))
7418 && same_type_p (type, intype))
7419 /* DR 799 */
7420 return rvalue (expr);
7421 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7423 if ((complain & tf_warning)
7424 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7425 TREE_TYPE (intype)))
7426 warning (OPT_Wcast_function_type,
7427 "cast between incompatible function types"
7428 " from %qH to %qI", intype, type);
7429 return build_nop_reinterpret (type, expr);
7431 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7433 if ((complain & tf_warning)
7434 && !cxx_safe_function_type_cast_p
7435 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7436 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7437 warning (OPT_Wcast_function_type,
7438 "cast between incompatible pointer to member types"
7439 " from %qH to %qI", intype, type);
7440 return build_nop_reinterpret (type, expr);
7442 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7443 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7445 if (!c_cast_p
7446 && check_for_casting_away_constness (intype, type,
7447 REINTERPRET_CAST_EXPR,
7448 complain))
7449 return error_mark_node;
7450 /* Warn about possible alignment problems. */
7451 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7452 && (complain & tf_warning)
7453 && !VOID_TYPE_P (type)
7454 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7455 && COMPLETE_TYPE_P (TREE_TYPE (type))
7456 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7457 && min_align_of_type (TREE_TYPE (type))
7458 > min_align_of_type (TREE_TYPE (intype)))
7459 warning (OPT_Wcast_align, "cast from %qH to %qI "
7460 "increases required alignment of target type", intype, type);
7462 if (warn_strict_aliasing <= 2)
7463 /* strict_aliasing_warning STRIP_NOPs its expr. */
7464 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7466 return build_nop_reinterpret (type, expr);
7468 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7469 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7471 if (complain & tf_warning)
7472 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7473 object pointer type or vice versa is conditionally-supported." */
7474 warning (OPT_Wconditionally_supported,
7475 "casting between pointer-to-function and pointer-to-object "
7476 "is conditionally-supported");
7477 return build_nop_reinterpret (type, expr);
7479 else if (VECTOR_TYPE_P (type))
7480 return convert_to_vector (type, expr);
7481 else if (VECTOR_TYPE_P (intype)
7482 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7483 return convert_to_integer_nofold (type, expr);
7484 else
7486 if (valid_p)
7487 *valid_p = false;
7488 if (complain & tf_error)
7489 error ("invalid cast from type %qT to type %qT", intype, type);
7490 return error_mark_node;
7493 expr = cp_convert (type, expr, complain);
7494 if (TREE_CODE (expr) == NOP_EXPR)
7495 /* Mark any nop_expr that created as a reintepret_cast. */
7496 REINTERPRET_CAST_P (expr) = true;
7497 return expr;
7500 tree
7501 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7503 tree r;
7505 if (type == error_mark_node || expr == error_mark_node)
7506 return error_mark_node;
7508 if (processing_template_decl)
7510 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7512 if (!TREE_SIDE_EFFECTS (t)
7513 && type_dependent_expression_p (expr))
7514 /* There might turn out to be side effects inside expr. */
7515 TREE_SIDE_EFFECTS (t) = 1;
7516 return convert_from_reference (t);
7519 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7520 /*valid_p=*/NULL, complain);
7521 if (r != error_mark_node)
7523 maybe_warn_about_useless_cast (type, expr, complain);
7524 maybe_warn_about_cast_ignoring_quals (type, complain);
7526 return r;
7529 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7530 return an appropriate expression. Otherwise, return
7531 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7532 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7533 performing a C-style cast, its value upon return will indicate
7534 whether or not the conversion succeeded. */
7536 static tree
7537 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7538 bool *valid_p)
7540 tree src_type;
7541 tree reference_type;
7543 /* Callers are responsible for handling error_mark_node as a
7544 destination type. */
7545 gcc_assert (dst_type != error_mark_node);
7546 /* In a template, callers should be building syntactic
7547 representations of casts, not using this machinery. */
7548 gcc_assert (!processing_template_decl);
7550 /* Assume the conversion is invalid. */
7551 if (valid_p)
7552 *valid_p = false;
7554 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7556 if (complain & tf_error)
7557 error ("invalid use of const_cast with type %qT, "
7558 "which is not a pointer, "
7559 "reference, nor a pointer-to-data-member type", dst_type);
7560 return error_mark_node;
7563 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7565 if (complain & tf_error)
7566 error ("invalid use of const_cast with type %qT, which is a pointer "
7567 "or reference to a function type", dst_type);
7568 return error_mark_node;
7571 /* A prvalue of non-class type is cv-unqualified. */
7572 dst_type = cv_unqualified (dst_type);
7574 /* Save casted types in the function's used types hash table. */
7575 used_types_insert (dst_type);
7577 src_type = TREE_TYPE (expr);
7578 /* Expressions do not really have reference types. */
7579 if (TYPE_REF_P (src_type))
7580 src_type = TREE_TYPE (src_type);
7582 /* [expr.const.cast]
7584 For two object types T1 and T2, if a pointer to T1 can be explicitly
7585 converted to the type "pointer to T2" using a const_cast, then the
7586 following conversions can also be made:
7588 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7589 type T2 using the cast const_cast<T2&>;
7591 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7592 type T2 using the cast const_cast<T2&&>; and
7594 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7595 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7597 if (TYPE_REF_P (dst_type))
7599 reference_type = dst_type;
7600 if (!TYPE_REF_IS_RVALUE (dst_type)
7601 ? lvalue_p (expr)
7602 : obvalue_p (expr))
7603 /* OK. */;
7604 else
7606 if (complain & tf_error)
7607 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7608 src_type, dst_type);
7609 return error_mark_node;
7611 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7612 src_type = build_pointer_type (src_type);
7614 else
7616 reference_type = NULL_TREE;
7617 /* If the destination type is not a reference type, the
7618 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7619 conversions are performed. */
7620 src_type = type_decays_to (src_type);
7621 if (src_type == error_mark_node)
7622 return error_mark_node;
7625 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7627 if (comp_ptr_ttypes_const (dst_type, src_type))
7629 if (valid_p)
7631 *valid_p = true;
7632 /* This cast is actually a C-style cast. Issue a warning if
7633 the user is making a potentially unsafe cast. */
7634 check_for_casting_away_constness (src_type, dst_type,
7635 CAST_EXPR, complain);
7636 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7637 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7638 && (complain & tf_warning)
7639 && min_align_of_type (TREE_TYPE (dst_type))
7640 > min_align_of_type (TREE_TYPE (src_type)))
7641 warning (OPT_Wcast_align, "cast from %qH to %qI "
7642 "increases required alignment of target type",
7643 src_type, dst_type);
7645 if (reference_type)
7647 expr = cp_build_addr_expr (expr, complain);
7648 if (expr == error_mark_node)
7649 return error_mark_node;
7650 expr = build_nop (reference_type, expr);
7651 return convert_from_reference (expr);
7653 else
7655 expr = decay_conversion (expr, complain);
7656 if (expr == error_mark_node)
7657 return error_mark_node;
7659 /* build_c_cast puts on a NOP_EXPR to make the result not an
7660 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7661 non-lvalue context. */
7662 if (TREE_CODE (expr) == NOP_EXPR
7663 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7664 expr = TREE_OPERAND (expr, 0);
7665 return build_nop (dst_type, expr);
7668 else if (valid_p
7669 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7670 TREE_TYPE (src_type)))
7671 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7672 complain);
7675 if (complain & tf_error)
7676 error ("invalid const_cast from type %qT to type %qT",
7677 src_type, dst_type);
7678 return error_mark_node;
7681 tree
7682 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7684 tree r;
7686 if (type == error_mark_node || error_operand_p (expr))
7687 return error_mark_node;
7689 if (processing_template_decl)
7691 tree t = build_min (CONST_CAST_EXPR, type, expr);
7693 if (!TREE_SIDE_EFFECTS (t)
7694 && type_dependent_expression_p (expr))
7695 /* There might turn out to be side effects inside expr. */
7696 TREE_SIDE_EFFECTS (t) = 1;
7697 return convert_from_reference (t);
7700 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7701 if (r != error_mark_node)
7703 maybe_warn_about_useless_cast (type, expr, complain);
7704 maybe_warn_about_cast_ignoring_quals (type, complain);
7706 return r;
7709 /* Like cp_build_c_cast, but for the c-common bits. */
7711 tree
7712 build_c_cast (location_t /*loc*/, tree type, tree expr)
7714 return cp_build_c_cast (type, expr, tf_warning_or_error);
7717 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7718 preserve location information even for tree nodes that don't
7719 support it. */
7721 cp_expr
7722 build_c_cast (location_t loc, tree type, cp_expr expr)
7724 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7725 result.set_location (loc);
7726 return result;
7729 /* Build an expression representing an explicit C-style cast to type
7730 TYPE of expression EXPR. */
7732 tree
7733 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7735 tree value = expr;
7736 tree result;
7737 bool valid_p;
7739 if (type == error_mark_node || error_operand_p (expr))
7740 return error_mark_node;
7742 if (processing_template_decl)
7744 tree t = build_min (CAST_EXPR, type,
7745 tree_cons (NULL_TREE, value, NULL_TREE));
7746 /* We don't know if it will or will not have side effects. */
7747 TREE_SIDE_EFFECTS (t) = 1;
7748 return convert_from_reference (t);
7751 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7752 'Class') should always be retained, because this information aids
7753 in method lookup. */
7754 if (objc_is_object_ptr (type)
7755 && objc_is_object_ptr (TREE_TYPE (expr)))
7756 return build_nop (type, expr);
7758 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7759 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7760 if (!TYPE_REF_P (type)
7761 && TREE_CODE (value) == NOP_EXPR
7762 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7763 value = TREE_OPERAND (value, 0);
7765 if (TREE_CODE (type) == ARRAY_TYPE)
7767 /* Allow casting from T1* to T2[] because Cfront allows it.
7768 NIHCL uses it. It is not valid ISO C++ however. */
7769 if (TYPE_PTR_P (TREE_TYPE (expr)))
7771 if (complain & tf_error)
7772 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7773 else
7774 return error_mark_node;
7775 type = build_pointer_type (TREE_TYPE (type));
7777 else
7779 if (complain & tf_error)
7780 error ("ISO C++ forbids casting to an array type %qT", type);
7781 return error_mark_node;
7785 if (TREE_CODE (type) == FUNCTION_TYPE
7786 || TREE_CODE (type) == METHOD_TYPE)
7788 if (complain & tf_error)
7789 error ("invalid cast to function type %qT", type);
7790 return error_mark_node;
7793 if (TYPE_PTR_P (type)
7794 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7795 /* Casting to an integer of smaller size is an error detected elsewhere. */
7796 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7797 /* Don't warn about converting any constant. */
7798 && !TREE_CONSTANT (value))
7799 warning_at (input_location, OPT_Wint_to_pointer_cast,
7800 "cast to pointer from integer of different size");
7802 /* A C-style cast can be a const_cast. */
7803 result = build_const_cast_1 (type, value, complain & tf_warning,
7804 &valid_p);
7805 if (valid_p)
7807 if (result != error_mark_node)
7809 maybe_warn_about_useless_cast (type, value, complain);
7810 maybe_warn_about_cast_ignoring_quals (type, complain);
7812 return result;
7815 /* Or a static cast. */
7816 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7817 &valid_p, complain);
7818 /* Or a reinterpret_cast. */
7819 if (!valid_p)
7820 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7821 &valid_p, complain);
7822 /* The static_cast or reinterpret_cast may be followed by a
7823 const_cast. */
7824 if (valid_p
7825 /* A valid cast may result in errors if, for example, a
7826 conversion to an ambiguous base class is required. */
7827 && !error_operand_p (result))
7829 tree result_type;
7831 maybe_warn_about_useless_cast (type, value, complain);
7832 maybe_warn_about_cast_ignoring_quals (type, complain);
7834 /* Non-class rvalues always have cv-unqualified type. */
7835 if (!CLASS_TYPE_P (type))
7836 type = TYPE_MAIN_VARIANT (type);
7837 result_type = TREE_TYPE (result);
7838 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
7839 result_type = TYPE_MAIN_VARIANT (result_type);
7840 /* If the type of RESULT does not match TYPE, perform a
7841 const_cast to make it match. If the static_cast or
7842 reinterpret_cast succeeded, we will differ by at most
7843 cv-qualification, so the follow-on const_cast is guaranteed
7844 to succeed. */
7845 if (!same_type_p (non_reference (type), non_reference (result_type)))
7847 result = build_const_cast_1 (type, result, false, &valid_p);
7848 gcc_assert (valid_p);
7850 return result;
7853 return error_mark_node;
7856 /* For use from the C common bits. */
7857 tree
7858 build_modify_expr (location_t location,
7859 tree lhs, tree /*lhs_origtype*/,
7860 enum tree_code modifycode,
7861 location_t /*rhs_location*/, tree rhs,
7862 tree /*rhs_origtype*/)
7864 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7865 tf_warning_or_error);
7868 /* Build an assignment expression of lvalue LHS from value RHS.
7869 MODIFYCODE is the code for a binary operator that we use
7870 to combine the old value of LHS with RHS to get the new value.
7871 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7873 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7875 tree
7876 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7877 tree rhs, tsubst_flags_t complain)
7879 lhs = mark_lvalue_use_nonread (lhs);
7881 tree result = NULL_TREE;
7882 tree newrhs = rhs;
7883 tree lhstype = TREE_TYPE (lhs);
7884 tree olhs = lhs;
7885 tree olhstype = lhstype;
7886 bool plain_assign = (modifycode == NOP_EXPR);
7887 bool compound_side_effects_p = false;
7888 tree preeval = NULL_TREE;
7890 /* Avoid duplicate error messages from operands that had errors. */
7891 if (error_operand_p (lhs) || error_operand_p (rhs))
7892 return error_mark_node;
7894 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7896 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7897 compound_side_effects_p = true;
7898 lhs = TREE_OPERAND (lhs, 1);
7901 /* Handle control structure constructs used as "lvalues". Note that we
7902 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7903 switch (TREE_CODE (lhs))
7905 /* Handle --foo = 5; as these are valid constructs in C++. */
7906 case PREDECREMENT_EXPR:
7907 case PREINCREMENT_EXPR:
7908 if (compound_side_effects_p)
7909 newrhs = rhs = stabilize_expr (rhs, &preeval);
7910 lhs = genericize_compound_lvalue (lhs);
7911 maybe_add_compound:
7912 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7913 and looked through the COMPOUND_EXPRs, readd them now around
7914 the resulting lhs. */
7915 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7917 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7918 tree *ptr = &TREE_OPERAND (lhs, 1);
7919 for (olhs = TREE_OPERAND (olhs, 1);
7920 TREE_CODE (olhs) == COMPOUND_EXPR;
7921 olhs = TREE_OPERAND (olhs, 1))
7923 *ptr = build2 (COMPOUND_EXPR, lhstype,
7924 TREE_OPERAND (olhs, 0), *ptr);
7925 ptr = &TREE_OPERAND (*ptr, 1);
7928 break;
7930 case MODIFY_EXPR:
7931 if (compound_side_effects_p)
7932 newrhs = rhs = stabilize_expr (rhs, &preeval);
7933 lhs = genericize_compound_lvalue (lhs);
7934 goto maybe_add_compound;
7936 case MIN_EXPR:
7937 case MAX_EXPR:
7938 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7939 when neither operand has side-effects. */
7940 if (!lvalue_or_else (lhs, lv_assign, complain))
7941 return error_mark_node;
7943 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7944 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7946 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7947 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7948 boolean_type_node,
7949 TREE_OPERAND (lhs, 0),
7950 TREE_OPERAND (lhs, 1)),
7951 TREE_OPERAND (lhs, 0),
7952 TREE_OPERAND (lhs, 1));
7953 gcc_fallthrough ();
7955 /* Handle (a ? b : c) used as an "lvalue". */
7956 case COND_EXPR:
7958 /* Produce (a ? (b = rhs) : (c = rhs))
7959 except that the RHS goes through a save-expr
7960 so the code to compute it is only emitted once. */
7961 tree cond;
7963 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7965 if (complain & tf_error)
7966 error ("void value not ignored as it ought to be");
7967 return error_mark_node;
7970 rhs = stabilize_expr (rhs, &preeval);
7972 /* Check this here to avoid odd errors when trying to convert
7973 a throw to the type of the COND_EXPR. */
7974 if (!lvalue_or_else (lhs, lv_assign, complain))
7975 return error_mark_node;
7977 cond = build_conditional_expr
7978 (input_location, TREE_OPERAND (lhs, 0),
7979 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7980 modifycode, rhs, complain),
7981 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7982 modifycode, rhs, complain),
7983 complain);
7985 if (cond == error_mark_node)
7986 return cond;
7987 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7988 and looked through the COMPOUND_EXPRs, readd them now around
7989 the resulting cond before adding the preevaluated rhs. */
7990 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7992 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7993 TREE_OPERAND (olhs, 0), cond);
7994 tree *ptr = &TREE_OPERAND (cond, 1);
7995 for (olhs = TREE_OPERAND (olhs, 1);
7996 TREE_CODE (olhs) == COMPOUND_EXPR;
7997 olhs = TREE_OPERAND (olhs, 1))
7999 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8000 TREE_OPERAND (olhs, 0), *ptr);
8001 ptr = &TREE_OPERAND (*ptr, 1);
8004 /* Make sure the code to compute the rhs comes out
8005 before the split. */
8006 result = cond;
8007 goto ret;
8010 default:
8011 lhs = olhs;
8012 break;
8015 if (modifycode == INIT_EXPR)
8017 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8018 /* Do the default thing. */;
8019 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8021 /* Compound literal. */
8022 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8023 /* Call convert to generate an error; see PR 11063. */
8024 rhs = convert (lhstype, rhs);
8025 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8026 TREE_SIDE_EFFECTS (result) = 1;
8027 goto ret;
8029 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8030 /* Do the default thing. */;
8031 else
8033 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8034 result = build_special_member_call (lhs, complete_ctor_identifier,
8035 &rhs_vec, lhstype, LOOKUP_NORMAL,
8036 complain);
8037 release_tree_vector (rhs_vec);
8038 if (result == NULL_TREE)
8039 return error_mark_node;
8040 goto ret;
8043 else
8045 lhs = require_complete_type_sfinae (lhs, complain);
8046 if (lhs == error_mark_node)
8047 return error_mark_node;
8049 if (modifycode == NOP_EXPR)
8051 if (c_dialect_objc ())
8053 result = objc_maybe_build_modify_expr (lhs, rhs);
8054 if (result)
8055 goto ret;
8058 /* `operator=' is not an inheritable operator. */
8059 if (! MAYBE_CLASS_TYPE_P (lhstype))
8060 /* Do the default thing. */;
8061 else
8063 result = build_new_op (input_location, MODIFY_EXPR,
8064 LOOKUP_NORMAL, lhs, rhs,
8065 make_node (NOP_EXPR), /*overload=*/NULL,
8066 complain);
8067 if (result == NULL_TREE)
8068 return error_mark_node;
8069 goto ret;
8071 lhstype = olhstype;
8073 else
8075 tree init = NULL_TREE;
8077 /* A binary op has been requested. Combine the old LHS
8078 value with the RHS producing the value we should actually
8079 store into the LHS. */
8080 gcc_assert (!((TYPE_REF_P (lhstype)
8081 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8082 || MAYBE_CLASS_TYPE_P (lhstype)));
8084 /* Preevaluate the RHS to make sure its evaluation is complete
8085 before the lvalue-to-rvalue conversion of the LHS:
8087 [expr.ass] With respect to an indeterminately-sequenced
8088 function call, the operation of a compound assignment is a
8089 single evaluation. [ Note: Therefore, a function call shall
8090 not intervene between the lvalue-to-rvalue conversion and the
8091 side effect associated with any single compound assignment
8092 operator. -- end note ] */
8093 lhs = cp_stabilize_reference (lhs);
8094 rhs = decay_conversion (rhs, complain);
8095 if (rhs == error_mark_node)
8096 return error_mark_node;
8097 rhs = stabilize_expr (rhs, &init);
8098 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8099 if (newrhs == error_mark_node)
8101 if (complain & tf_error)
8102 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8103 TREE_TYPE (lhs), TREE_TYPE (rhs));
8104 return error_mark_node;
8107 if (init)
8108 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8110 /* Now it looks like a plain assignment. */
8111 modifycode = NOP_EXPR;
8112 if (c_dialect_objc ())
8114 result = objc_maybe_build_modify_expr (lhs, newrhs);
8115 if (result)
8116 goto ret;
8119 gcc_assert (!TYPE_REF_P (lhstype));
8120 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
8123 /* The left-hand side must be an lvalue. */
8124 if (!lvalue_or_else (lhs, lv_assign, complain))
8125 return error_mark_node;
8127 /* Warn about modifying something that is `const'. Don't warn if
8128 this is initialization. */
8129 if (modifycode != INIT_EXPR
8130 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8131 /* Functions are not modifiable, even though they are
8132 lvalues. */
8133 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8134 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8135 /* If it's an aggregate and any field is const, then it is
8136 effectively const. */
8137 || (CLASS_TYPE_P (lhstype)
8138 && C_TYPE_FIELDS_READONLY (lhstype))))
8140 if (complain & tf_error)
8141 cxx_readonly_error (lhs, lv_assign);
8142 return error_mark_node;
8145 /* If storing into a structure or union member, it may have been given a
8146 lowered bitfield type. We need to convert to the declared type first,
8147 so retrieve it now. */
8149 olhstype = unlowered_expr_type (lhs);
8151 /* Convert new value to destination type. */
8153 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8155 int from_array;
8157 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8159 if (modifycode != INIT_EXPR)
8161 if (complain & tf_error)
8162 error ("assigning to an array from an initializer list");
8163 return error_mark_node;
8165 if (check_array_initializer (lhs, lhstype, newrhs))
8166 return error_mark_node;
8167 newrhs = digest_init (lhstype, newrhs, complain);
8168 if (newrhs == error_mark_node)
8169 return error_mark_node;
8172 /* C++11 8.5/17: "If the destination type is an array of characters,
8173 an array of char16_t, an array of char32_t, or an array of wchar_t,
8174 and the initializer is a string literal...". */
8175 else if (TREE_CODE (newrhs) == STRING_CST
8176 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8177 && modifycode == INIT_EXPR)
8179 newrhs = digest_init (lhstype, newrhs, complain);
8180 if (newrhs == error_mark_node)
8181 return error_mark_node;
8184 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8185 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8187 if (complain & tf_error)
8188 error ("incompatible types in assignment of %qT to %qT",
8189 TREE_TYPE (rhs), lhstype);
8190 return error_mark_node;
8193 /* Allow array assignment in compiler-generated code. */
8194 else if (!current_function_decl
8195 || !DECL_DEFAULTED_FN (current_function_decl))
8197 /* This routine is used for both initialization and assignment.
8198 Make sure the diagnostic message differentiates the context. */
8199 if (complain & tf_error)
8201 if (modifycode == INIT_EXPR)
8202 error ("array used as initializer");
8203 else
8204 error ("invalid array assignment");
8206 return error_mark_node;
8209 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8210 ? 1 + (modifycode != INIT_EXPR): 0;
8211 result = build_vec_init (lhs, NULL_TREE, newrhs,
8212 /*explicit_value_init_p=*/false,
8213 from_array, complain);
8214 goto ret;
8217 if (modifycode == INIT_EXPR)
8218 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8219 LOOKUP_ONLYCONVERTING. */
8220 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8221 ICR_INIT, NULL_TREE, 0,
8222 complain);
8223 else
8224 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8225 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8227 if (!same_type_p (lhstype, olhstype))
8228 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8230 if (modifycode != INIT_EXPR)
8232 if (TREE_CODE (newrhs) == CALL_EXPR
8233 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8234 newrhs = build_cplus_new (lhstype, newrhs, complain);
8236 /* Can't initialize directly from a TARGET_EXPR, since that would
8237 cause the lhs to be constructed twice, and possibly result in
8238 accidental self-initialization. So we force the TARGET_EXPR to be
8239 expanded without a target. */
8240 if (TREE_CODE (newrhs) == TARGET_EXPR)
8241 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8242 TREE_OPERAND (newrhs, 0));
8245 if (newrhs == error_mark_node)
8246 return error_mark_node;
8248 if (c_dialect_objc () && flag_objc_gc)
8250 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8252 if (result)
8253 goto ret;
8256 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8257 lhstype, lhs, newrhs);
8259 TREE_SIDE_EFFECTS (result) = 1;
8260 if (!plain_assign)
8261 TREE_NO_WARNING (result) = 1;
8263 ret:
8264 if (preeval)
8265 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8266 return result;
8269 cp_expr
8270 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8271 tree rhs, tsubst_flags_t complain)
8273 tree orig_lhs = lhs;
8274 tree orig_rhs = rhs;
8275 tree overload = NULL_TREE;
8276 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8278 if (processing_template_decl)
8280 if (modifycode == NOP_EXPR
8281 || type_dependent_expression_p (lhs)
8282 || type_dependent_expression_p (rhs))
8283 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8284 build_min_nt_loc (loc, modifycode, NULL_TREE,
8285 NULL_TREE), rhs);
8287 lhs = build_non_dependent_expr (lhs);
8288 rhs = build_non_dependent_expr (rhs);
8291 if (modifycode != NOP_EXPR)
8293 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8294 lhs, rhs, op, &overload, complain);
8295 if (rval)
8297 if (rval == error_mark_node)
8298 return rval;
8299 TREE_NO_WARNING (rval) = 1;
8300 if (processing_template_decl)
8302 if (overload != NULL_TREE)
8303 return (build_min_non_dep_op_overload
8304 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8306 return (build_min_non_dep
8307 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8309 return rval;
8312 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8315 /* Helper function for get_delta_difference which assumes FROM is a base
8316 class of TO. Returns a delta for the conversion of pointer-to-member
8317 of FROM to pointer-to-member of TO. If the conversion is invalid and
8318 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8319 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8320 If C_CAST_P is true, this conversion is taking place as part of a
8321 C-style cast. */
8323 static tree
8324 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8325 tsubst_flags_t complain)
8327 tree binfo;
8328 base_kind kind;
8330 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8331 &kind, complain);
8333 if (binfo == error_mark_node)
8335 if (!(complain & tf_error))
8336 return error_mark_node;
8338 error (" in pointer to member function conversion");
8339 return size_zero_node;
8341 else if (binfo)
8343 if (kind != bk_via_virtual)
8344 return BINFO_OFFSET (binfo);
8345 else
8346 /* FROM is a virtual base class of TO. Issue an error or warning
8347 depending on whether or not this is a reinterpret cast. */
8349 if (!(complain & tf_error))
8350 return error_mark_node;
8352 error ("pointer to member conversion via virtual base %qT",
8353 BINFO_TYPE (binfo_from_vbase (binfo)));
8355 return size_zero_node;
8358 else
8359 return NULL_TREE;
8362 /* Get difference in deltas for different pointer to member function
8363 types. If the conversion is invalid and tf_error is not set in
8364 COMPLAIN, returns error_mark_node, otherwise returns an integer
8365 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8366 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8367 conversions as well. If C_CAST_P is true this conversion is taking
8368 place as part of a C-style cast.
8370 Note that the naming of FROM and TO is kind of backwards; the return
8371 value is what we add to a TO in order to get a FROM. They are named
8372 this way because we call this function to find out how to convert from
8373 a pointer to member of FROM to a pointer to member of TO. */
8375 static tree
8376 get_delta_difference (tree from, tree to,
8377 bool allow_inverse_p,
8378 bool c_cast_p, tsubst_flags_t complain)
8380 tree result;
8382 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8383 /* Pointer to member of incomplete class is permitted*/
8384 result = size_zero_node;
8385 else
8386 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8388 if (result == error_mark_node)
8389 return error_mark_node;
8391 if (!result)
8393 if (!allow_inverse_p)
8395 if (!(complain & tf_error))
8396 return error_mark_node;
8398 error_not_base_type (from, to);
8399 error (" in pointer to member conversion");
8400 result = size_zero_node;
8402 else
8404 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8406 if (result == error_mark_node)
8407 return error_mark_node;
8409 if (result)
8410 result = size_diffop_loc (input_location,
8411 size_zero_node, result);
8412 else
8414 if (!(complain & tf_error))
8415 return error_mark_node;
8417 error_not_base_type (from, to);
8418 error (" in pointer to member conversion");
8419 result = size_zero_node;
8424 return convert_to_integer (ptrdiff_type_node, result);
8427 /* Return a constructor for the pointer-to-member-function TYPE using
8428 the other components as specified. */
8430 tree
8431 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8433 tree u = NULL_TREE;
8434 tree delta_field;
8435 tree pfn_field;
8436 vec<constructor_elt, va_gc> *v;
8438 /* Pull the FIELD_DECLs out of the type. */
8439 pfn_field = TYPE_FIELDS (type);
8440 delta_field = DECL_CHAIN (pfn_field);
8442 /* Make sure DELTA has the type we want. */
8443 delta = convert_and_check (input_location, delta_type_node, delta);
8445 /* Convert to the correct target type if necessary. */
8446 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8448 /* Finish creating the initializer. */
8449 vec_alloc (v, 2);
8450 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8451 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8452 u = build_constructor (type, v);
8453 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8454 TREE_STATIC (u) = (TREE_CONSTANT (u)
8455 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8456 != NULL_TREE)
8457 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8458 != NULL_TREE));
8459 return u;
8462 /* Build a constructor for a pointer to member function. It can be
8463 used to initialize global variables, local variable, or used
8464 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8465 want to be.
8467 If FORCE is nonzero, then force this conversion, even if
8468 we would rather not do it. Usually set when using an explicit
8469 cast. A C-style cast is being processed iff C_CAST_P is true.
8471 Return error_mark_node, if something goes wrong. */
8473 tree
8474 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8475 tsubst_flags_t complain)
8477 tree fn;
8478 tree pfn_type;
8479 tree to_type;
8481 if (error_operand_p (pfn))
8482 return error_mark_node;
8484 pfn_type = TREE_TYPE (pfn);
8485 to_type = build_ptrmemfunc_type (type);
8487 /* Handle multiple conversions of pointer to member functions. */
8488 if (TYPE_PTRMEMFUNC_P (pfn_type))
8490 tree delta = NULL_TREE;
8491 tree npfn = NULL_TREE;
8492 tree n;
8494 if (!force
8495 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8496 LOOKUP_NORMAL, complain))
8498 if (complain & tf_error)
8499 error ("invalid conversion to type %qT from type %qT",
8500 to_type, pfn_type);
8501 else
8502 return error_mark_node;
8505 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8506 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8507 force,
8508 c_cast_p, complain);
8509 if (n == error_mark_node)
8510 return error_mark_node;
8512 /* We don't have to do any conversion to convert a
8513 pointer-to-member to its own type. But, we don't want to
8514 just return a PTRMEM_CST if there's an explicit cast; that
8515 cast should make the expression an invalid template argument. */
8516 if (TREE_CODE (pfn) != PTRMEM_CST)
8518 if (same_type_p (to_type, pfn_type))
8519 return pfn;
8520 else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
8521 return build_reinterpret_cast (to_type, pfn,
8522 complain);
8525 if (TREE_SIDE_EFFECTS (pfn))
8526 pfn = save_expr (pfn);
8528 /* Obtain the function pointer and the current DELTA. */
8529 if (TREE_CODE (pfn) == PTRMEM_CST)
8530 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8531 else
8533 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8534 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8537 /* Just adjust the DELTA field. */
8538 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8539 (TREE_TYPE (delta), ptrdiff_type_node));
8540 if (!integer_zerop (n))
8542 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8543 n = cp_build_binary_op (input_location,
8544 LSHIFT_EXPR, n, integer_one_node,
8545 complain);
8546 delta = cp_build_binary_op (input_location,
8547 PLUS_EXPR, delta, n, complain);
8549 return build_ptrmemfunc1 (to_type, delta, npfn);
8552 /* Handle null pointer to member function conversions. */
8553 if (null_ptr_cst_p (pfn))
8555 pfn = cp_build_c_cast (type, pfn, complain);
8556 return build_ptrmemfunc1 (to_type,
8557 integer_zero_node,
8558 pfn);
8561 if (type_unknown_p (pfn))
8562 return instantiate_type (type, pfn, complain);
8564 fn = TREE_OPERAND (pfn, 0);
8565 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8566 /* In a template, we will have preserved the
8567 OFFSET_REF. */
8568 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8569 return make_ptrmem_cst (to_type, fn);
8572 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8573 given by CST.
8575 ??? There is no consistency as to the types returned for the above
8576 values. Some code acts as if it were a sizetype and some as if it were
8577 integer_type_node. */
8579 void
8580 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8582 tree type = TREE_TYPE (cst);
8583 tree fn = PTRMEM_CST_MEMBER (cst);
8584 tree ptr_class, fn_class;
8586 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8588 /* The class that the function belongs to. */
8589 fn_class = DECL_CONTEXT (fn);
8591 /* The class that we're creating a pointer to member of. */
8592 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8594 /* First, calculate the adjustment to the function's class. */
8595 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8596 /*c_cast_p=*/0, tf_warning_or_error);
8598 if (!DECL_VIRTUAL_P (fn))
8599 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8600 build_addr_func (fn, tf_warning_or_error));
8601 else
8603 /* If we're dealing with a virtual function, we have to adjust 'this'
8604 again, to point to the base which provides the vtable entry for
8605 fn; the call will do the opposite adjustment. */
8606 tree orig_class = DECL_CONTEXT (fn);
8607 tree binfo = binfo_or_else (orig_class, fn_class);
8608 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8609 *delta, BINFO_OFFSET (binfo));
8611 /* We set PFN to the vtable offset at which the function can be
8612 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8613 case delta is shifted left, and then incremented). */
8614 *pfn = DECL_VINDEX (fn);
8615 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8616 TYPE_SIZE_UNIT (vtable_entry_type));
8618 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8620 case ptrmemfunc_vbit_in_pfn:
8621 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8622 integer_one_node);
8623 break;
8625 case ptrmemfunc_vbit_in_delta:
8626 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8627 *delta, integer_one_node);
8628 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8629 *delta, integer_one_node);
8630 break;
8632 default:
8633 gcc_unreachable ();
8636 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8640 /* Return an expression for PFN from the pointer-to-member function
8641 given by T. */
8643 static tree
8644 pfn_from_ptrmemfunc (tree t)
8646 if (TREE_CODE (t) == PTRMEM_CST)
8648 tree delta;
8649 tree pfn;
8651 expand_ptrmemfunc_cst (t, &delta, &pfn);
8652 if (pfn)
8653 return pfn;
8656 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8659 /* Return an expression for DELTA from the pointer-to-member function
8660 given by T. */
8662 static tree
8663 delta_from_ptrmemfunc (tree t)
8665 if (TREE_CODE (t) == PTRMEM_CST)
8667 tree delta;
8668 tree pfn;
8670 expand_ptrmemfunc_cst (t, &delta, &pfn);
8671 if (delta)
8672 return delta;
8675 return build_ptrmemfunc_access_expr (t, delta_identifier);
8678 /* Convert value RHS to type TYPE as preparation for an assignment to
8679 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8680 implicit conversion is. If FNDECL is non-NULL, we are doing the
8681 conversion in order to pass the PARMNUMth argument of FNDECL.
8682 If FNDECL is NULL, we are doing the conversion in function pointer
8683 argument passing, conversion in initialization, etc. */
8685 static tree
8686 convert_for_assignment (tree type, tree rhs,
8687 impl_conv_rhs errtype, tree fndecl, int parmnum,
8688 tsubst_flags_t complain, int flags)
8690 tree rhstype;
8691 enum tree_code coder;
8693 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8694 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8695 rhs = TREE_OPERAND (rhs, 0);
8697 /* Handle [dcl.init.list] direct-list-initialization from
8698 single element of enumeration with a fixed underlying type. */
8699 if (is_direct_enum_init (type, rhs))
8701 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8702 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8704 warning_sentinel w (warn_useless_cast);
8705 warning_sentinel w2 (warn_ignored_qualifiers);
8706 rhs = cp_build_c_cast (type, elt, complain);
8708 else
8709 rhs = error_mark_node;
8712 rhstype = TREE_TYPE (rhs);
8713 coder = TREE_CODE (rhstype);
8715 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8716 && vector_types_convertible_p (type, rhstype, true))
8718 rhs = mark_rvalue_use (rhs);
8719 return convert (type, rhs);
8722 if (rhs == error_mark_node || rhstype == error_mark_node)
8723 return error_mark_node;
8724 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8725 return error_mark_node;
8727 /* The RHS of an assignment cannot have void type. */
8728 if (coder == VOID_TYPE)
8730 if (complain & tf_error)
8731 error ("void value not ignored as it ought to be");
8732 return error_mark_node;
8735 if (c_dialect_objc ())
8737 int parmno;
8738 tree selector;
8739 tree rname = fndecl;
8741 switch (errtype)
8743 case ICR_ASSIGN:
8744 parmno = -1;
8745 break;
8746 case ICR_INIT:
8747 parmno = -2;
8748 break;
8749 default:
8750 selector = objc_message_selector ();
8751 parmno = parmnum;
8752 if (selector && parmno > 1)
8754 rname = selector;
8755 parmno -= 1;
8759 if (objc_compare_types (type, rhstype, parmno, rname))
8761 rhs = mark_rvalue_use (rhs);
8762 return convert (type, rhs);
8766 /* [expr.ass]
8768 The expression is implicitly converted (clause _conv_) to the
8769 cv-unqualified type of the left operand.
8771 We allow bad conversions here because by the time we get to this point
8772 we are committed to doing the conversion. If we end up doing a bad
8773 conversion, convert_like will complain. */
8774 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8776 /* When -Wno-pmf-conversions is use, we just silently allow
8777 conversions from pointers-to-members to plain pointers. If
8778 the conversion doesn't work, cp_convert will complain. */
8779 if (!warn_pmf2ptr
8780 && TYPE_PTR_P (type)
8781 && TYPE_PTRMEMFUNC_P (rhstype))
8782 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8783 else
8785 if (complain & tf_error)
8787 /* If the right-hand side has unknown type, then it is an
8788 overloaded function. Call instantiate_type to get error
8789 messages. */
8790 if (rhstype == unknown_type_node)
8792 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8793 /* -fpermissive might allow this; recurse. */
8794 if (!seen_error ())
8795 return convert_for_assignment (type, r, errtype, fndecl,
8796 parmnum, complain, flags);
8798 else if (fndecl)
8800 error_at (EXPR_LOC_OR_LOC (rhs, input_location),
8801 "cannot convert %qH to %qI",
8802 rhstype, type);
8803 inform (get_fndecl_argument_location (fndecl, parmnum),
8804 " initializing argument %P of %qD", parmnum, fndecl);
8806 else
8807 switch (errtype)
8809 case ICR_DEFAULT_ARGUMENT:
8810 error ("cannot convert %qH to %qI in default argument",
8811 rhstype, type);
8812 break;
8813 case ICR_ARGPASS:
8814 error ("cannot convert %qH to %qI in argument passing",
8815 rhstype, type);
8816 break;
8817 case ICR_CONVERTING:
8818 error ("cannot convert %qH to %qI",
8819 rhstype, type);
8820 break;
8821 case ICR_INIT:
8822 error ("cannot convert %qH to %qI in initialization",
8823 rhstype, type);
8824 break;
8825 case ICR_RETURN:
8826 error ("cannot convert %qH to %qI in return",
8827 rhstype, type);
8828 break;
8829 case ICR_ASSIGN:
8830 error ("cannot convert %qH to %qI in assignment",
8831 rhstype, type);
8832 break;
8833 default:
8834 gcc_unreachable();
8836 if (TYPE_PTR_P (rhstype)
8837 && TYPE_PTR_P (type)
8838 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8839 && CLASS_TYPE_P (TREE_TYPE (type))
8840 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8841 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8842 (TREE_TYPE (rhstype))),
8843 "class type %qT is incomplete", TREE_TYPE (rhstype));
8845 return error_mark_node;
8848 if (warn_suggest_attribute_format)
8850 const enum tree_code codel = TREE_CODE (type);
8851 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8852 && coder == codel
8853 && check_missing_format_attribute (type, rhstype)
8854 && (complain & tf_warning))
8855 switch (errtype)
8857 case ICR_ARGPASS:
8858 case ICR_DEFAULT_ARGUMENT:
8859 if (fndecl)
8860 warning (OPT_Wsuggest_attribute_format,
8861 "parameter %qP of %qD might be a candidate "
8862 "for a format attribute", parmnum, fndecl);
8863 else
8864 warning (OPT_Wsuggest_attribute_format,
8865 "parameter might be a candidate "
8866 "for a format attribute");
8867 break;
8868 case ICR_CONVERTING:
8869 warning (OPT_Wsuggest_attribute_format,
8870 "target of conversion might be a candidate "
8871 "for a format attribute");
8872 break;
8873 case ICR_INIT:
8874 warning (OPT_Wsuggest_attribute_format,
8875 "target of initialization might be a candidate "
8876 "for a format attribute");
8877 break;
8878 case ICR_RETURN:
8879 warning (OPT_Wsuggest_attribute_format,
8880 "return type might be a candidate "
8881 "for a format attribute");
8882 break;
8883 case ICR_ASSIGN:
8884 warning (OPT_Wsuggest_attribute_format,
8885 "left-hand side of assignment might be a candidate "
8886 "for a format attribute");
8887 break;
8888 default:
8889 gcc_unreachable();
8893 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8894 does not. */
8895 if (warn_parentheses
8896 && TREE_CODE (type) == BOOLEAN_TYPE
8897 && TREE_CODE (rhs) == MODIFY_EXPR
8898 && !TREE_NO_WARNING (rhs)
8899 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8900 && (complain & tf_warning))
8902 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8904 warning_at (loc, OPT_Wparentheses,
8905 "suggest parentheses around assignment used as truth value");
8906 TREE_NO_WARNING (rhs) = 1;
8909 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8910 complain, flags);
8913 /* Convert RHS to be of type TYPE.
8914 If EXP is nonzero, it is the target of the initialization.
8915 ERRTYPE indicates what kind of error the implicit conversion is.
8917 Two major differences between the behavior of
8918 `convert_for_assignment' and `convert_for_initialization'
8919 are that references are bashed in the former, while
8920 copied in the latter, and aggregates are assigned in
8921 the former (operator=) while initialized in the
8922 latter (X(X&)).
8924 If using constructor make sure no conversion operator exists, if one does
8925 exist, an ambiguity exists. */
8927 tree
8928 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8929 impl_conv_rhs errtype, tree fndecl, int parmnum,
8930 tsubst_flags_t complain)
8932 enum tree_code codel = TREE_CODE (type);
8933 tree rhstype;
8934 enum tree_code coder;
8936 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8937 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8938 if (TREE_CODE (rhs) == NOP_EXPR
8939 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8940 && codel != REFERENCE_TYPE)
8941 rhs = TREE_OPERAND (rhs, 0);
8943 if (type == error_mark_node
8944 || rhs == error_mark_node
8945 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8946 return error_mark_node;
8948 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8950 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8951 && TREE_CODE (type) != ARRAY_TYPE
8952 && (!TYPE_REF_P (type)
8953 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8954 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8955 && !TYPE_REFFN_P (type))
8956 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8957 rhs = decay_conversion (rhs, complain);
8959 rhstype = TREE_TYPE (rhs);
8960 coder = TREE_CODE (rhstype);
8962 if (coder == ERROR_MARK)
8963 return error_mark_node;
8965 /* We accept references to incomplete types, so we can
8966 return here before checking if RHS is of complete type. */
8968 if (codel == REFERENCE_TYPE)
8970 /* This should eventually happen in convert_arguments. */
8971 int savew = 0, savee = 0;
8973 if (fndecl)
8974 savew = warningcount + werrorcount, savee = errorcount;
8975 rhs = initialize_reference (type, rhs, flags, complain);
8977 if (fndecl
8978 && (warningcount + werrorcount > savew || errorcount > savee))
8979 inform (DECL_SOURCE_LOCATION (fndecl),
8980 "in passing argument %P of %qD", parmnum, fndecl);
8982 return rhs;
8985 if (exp != 0)
8986 exp = require_complete_type_sfinae (exp, complain);
8987 if (exp == error_mark_node)
8988 return error_mark_node;
8990 rhstype = non_reference (rhstype);
8992 type = complete_type (type);
8994 if (DIRECT_INIT_EXPR_P (type, rhs))
8995 /* Don't try to do copy-initialization if we already have
8996 direct-initialization. */
8997 return rhs;
8999 if (MAYBE_CLASS_TYPE_P (type))
9000 return perform_implicit_conversion_flags (type, rhs, complain, flags);
9002 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
9003 complain, flags);
9006 /* If RETVAL is the address of, or a reference to, a local variable or
9007 temporary give an appropriate warning and return true. */
9009 static bool
9010 maybe_warn_about_returning_address_of_local (tree retval)
9012 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9013 tree whats_returned = fold_for_warn (retval);
9014 location_t loc = EXPR_LOC_OR_LOC (retval, input_location);
9016 for (;;)
9018 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9019 whats_returned = TREE_OPERAND (whats_returned, 1);
9020 else if (CONVERT_EXPR_P (whats_returned)
9021 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9022 whats_returned = TREE_OPERAND (whats_returned, 0);
9023 else
9024 break;
9027 if (TREE_CODE (whats_returned) == TARGET_EXPR
9028 && is_std_init_list (TREE_TYPE (whats_returned)))
9030 tree init = TARGET_EXPR_INITIAL (whats_returned);
9031 if (TREE_CODE (init) == CONSTRUCTOR)
9032 /* Pull out the array address. */
9033 whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
9034 else if (TREE_CODE (init) == INDIRECT_REF)
9035 /* The source of a trivial copy looks like *(T*)&var. */
9036 whats_returned = TREE_OPERAND (init, 0);
9037 else
9038 return false;
9039 STRIP_NOPS (whats_returned);
9042 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9043 return false;
9044 whats_returned = TREE_OPERAND (whats_returned, 0);
9046 while (TREE_CODE (whats_returned) == COMPONENT_REF
9047 || TREE_CODE (whats_returned) == ARRAY_REF)
9048 whats_returned = TREE_OPERAND (whats_returned, 0);
9050 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9051 || TREE_CODE (whats_returned) == TARGET_EXPR)
9053 if (TYPE_REF_P (valtype))
9054 warning_at (loc, OPT_Wreturn_local_addr,
9055 "returning reference to temporary");
9056 else if (is_std_init_list (valtype))
9057 warning_at (loc, OPT_Winit_list_lifetime,
9058 "returning temporary initializer_list does not extend "
9059 "the lifetime of the underlying array");
9060 return true;
9063 if (DECL_P (whats_returned)
9064 && DECL_NAME (whats_returned)
9065 && DECL_FUNCTION_SCOPE_P (whats_returned)
9066 && !is_capture_proxy (whats_returned)
9067 && !(TREE_STATIC (whats_returned)
9068 || TREE_PUBLIC (whats_returned)))
9070 bool w = false;
9071 if (TYPE_REF_P (valtype))
9072 w = warning_at (loc, OPT_Wreturn_local_addr,
9073 "reference to local variable %qD returned",
9074 whats_returned);
9075 else if (is_std_init_list (valtype))
9076 w = warning_at (loc, OPT_Winit_list_lifetime,
9077 "returning local initializer_list variable %qD "
9078 "does not extend the lifetime of the underlying array",
9079 whats_returned);
9080 else if (TREE_CODE (whats_returned) == LABEL_DECL)
9081 w = warning_at (loc, OPT_Wreturn_local_addr,
9082 "address of label %qD returned",
9083 whats_returned);
9084 else
9085 w = warning_at (loc, OPT_Wreturn_local_addr,
9086 "address of local variable %qD returned",
9087 whats_returned);
9088 if (w)
9089 inform (DECL_SOURCE_LOCATION (whats_returned),
9090 "declared here");
9091 return true;
9094 return false;
9097 /* Check that returning RETVAL from the current function is valid.
9098 Return an expression explicitly showing all conversions required to
9099 change RETVAL into the function return type, and to assign it to
9100 the DECL_RESULT for the function. Set *NO_WARNING to true if
9101 code reaches end of non-void function warning shouldn't be issued
9102 on this RETURN_EXPR. */
9104 tree
9105 check_return_expr (tree retval, bool *no_warning)
9107 tree result;
9108 /* The type actually returned by the function. */
9109 tree valtype;
9110 /* The type the function is declared to return, or void if
9111 the declared type is incomplete. */
9112 tree functype;
9113 int fn_returns_value_p;
9114 bool named_return_value_okay_p;
9116 *no_warning = false;
9118 /* A `volatile' function is one that isn't supposed to return, ever.
9119 (This is a G++ extension, used to get better code for functions
9120 that call the `volatile' function.) */
9121 if (TREE_THIS_VOLATILE (current_function_decl))
9122 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9124 /* Check for various simple errors. */
9125 if (DECL_DESTRUCTOR_P (current_function_decl))
9127 if (retval)
9128 error ("returning a value from a destructor");
9129 return NULL_TREE;
9131 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9133 if (in_function_try_handler)
9134 /* If a return statement appears in a handler of the
9135 function-try-block of a constructor, the program is ill-formed. */
9136 error ("cannot return from a handler of a function-try-block of a constructor");
9137 else if (retval)
9138 /* You can't return a value from a constructor. */
9139 error ("returning a value from a constructor");
9140 return NULL_TREE;
9143 const tree saved_retval = retval;
9145 if (processing_template_decl)
9147 current_function_returns_value = 1;
9149 if (check_for_bare_parameter_packs (retval))
9150 return error_mark_node;
9152 /* If one of the types might be void, we can't tell whether we're
9153 returning a value. */
9154 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9155 && !current_function_auto_return_pattern)
9156 || (retval != NULL_TREE
9157 && (TREE_TYPE (retval) == NULL_TREE
9158 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9159 goto dependent;
9162 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9164 /* Deduce auto return type from a return statement. */
9165 if (current_function_auto_return_pattern)
9167 tree auto_node;
9168 tree type;
9170 if (!retval && !is_auto (current_function_auto_return_pattern))
9172 /* Give a helpful error message. */
9173 error ("return-statement with no value, in function returning %qT",
9174 current_function_auto_return_pattern);
9175 inform (input_location, "only plain %<auto%> return type can be "
9176 "deduced to %<void%>");
9177 type = error_mark_node;
9179 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9181 error ("returning initializer list");
9182 type = error_mark_node;
9184 else
9186 if (!retval)
9187 retval = void_node;
9188 auto_node = type_uses_auto (current_function_auto_return_pattern);
9189 type = do_auto_deduction (current_function_auto_return_pattern,
9190 retval, auto_node);
9193 if (type == error_mark_node)
9194 /* Leave it. */;
9195 else if (functype == current_function_auto_return_pattern)
9196 apply_deduced_return_type (current_function_decl, type);
9197 else if (!same_type_p (type, functype))
9199 if (LAMBDA_FUNCTION_P (current_function_decl))
9200 error ("inconsistent types %qT and %qT deduced for "
9201 "lambda return type", functype, type);
9202 else
9203 error ("inconsistent deduction for auto return type: "
9204 "%qT and then %qT", functype, type);
9206 functype = type;
9209 result = DECL_RESULT (current_function_decl);
9210 valtype = TREE_TYPE (result);
9211 gcc_assert (valtype != NULL_TREE);
9212 fn_returns_value_p = !VOID_TYPE_P (valtype);
9214 /* Check for a return statement with no return value in a function
9215 that's supposed to return a value. */
9216 if (!retval && fn_returns_value_p)
9218 if (functype != error_mark_node)
9219 permerror (input_location, "return-statement with no value, in "
9220 "function returning %qT", valtype);
9221 /* Remember that this function did return. */
9222 current_function_returns_value = 1;
9223 /* And signal caller that TREE_NO_WARNING should be set on the
9224 RETURN_EXPR to avoid control reaches end of non-void function
9225 warnings in tree-cfg.c. */
9226 *no_warning = true;
9228 /* Check for a return statement with a value in a function that
9229 isn't supposed to return a value. */
9230 else if (retval && !fn_returns_value_p)
9232 if (VOID_TYPE_P (TREE_TYPE (retval)))
9233 /* You can return a `void' value from a function of `void'
9234 type. In that case, we have to evaluate the expression for
9235 its side-effects. */
9236 finish_expr_stmt (retval);
9237 else
9238 permerror (input_location,
9239 "return-statement with a value, in function "
9240 "returning %qT", valtype);
9241 current_function_returns_null = 1;
9243 /* There's really no value to return, after all. */
9244 return NULL_TREE;
9246 else if (!retval)
9247 /* Remember that this function can sometimes return without a
9248 value. */
9249 current_function_returns_null = 1;
9250 else
9251 /* Remember that this function did return a value. */
9252 current_function_returns_value = 1;
9254 /* Check for erroneous operands -- but after giving ourselves a
9255 chance to provide an error about returning a value from a void
9256 function. */
9257 if (error_operand_p (retval))
9259 current_function_return_value = error_mark_node;
9260 return error_mark_node;
9263 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9264 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9265 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9266 && ! flag_check_new
9267 && retval && null_ptr_cst_p (retval))
9268 warning (0, "%<operator new%> must not return NULL unless it is "
9269 "declared %<throw()%> (or -fcheck-new is in effect)");
9271 /* Effective C++ rule 15. See also start_function. */
9272 if (warn_ecpp
9273 && DECL_NAME (current_function_decl) == assign_op_identifier
9274 && !type_dependent_expression_p (retval))
9276 bool warn = true;
9278 /* The function return type must be a reference to the current
9279 class. */
9280 if (TYPE_REF_P (valtype)
9281 && same_type_ignoring_top_level_qualifiers_p
9282 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9284 /* Returning '*this' is obviously OK. */
9285 if (retval == current_class_ref)
9286 warn = false;
9287 /* If we are calling a function whose return type is the same of
9288 the current class reference, it is ok. */
9289 else if (INDIRECT_REF_P (retval)
9290 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9291 warn = false;
9294 if (warn)
9295 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9298 if (dependent_type_p (functype)
9299 || type_dependent_expression_p (retval))
9301 dependent:
9302 /* We should not have changed the return value. */
9303 gcc_assert (retval == saved_retval);
9304 return retval;
9307 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9309 [...] For a function with a class return type, if the expression
9310 in the return statement is the name of a local object, and the cv-
9311 unqualified type of the local object is the same as the function
9312 return type, an implementation is permitted to omit creating the tem-
9313 porary object to hold the function return value [...]
9315 So, if this is a value-returning function that always returns the same
9316 local variable, remember it.
9318 It might be nice to be more flexible, and choose the first suitable
9319 variable even if the function sometimes returns something else, but
9320 then we run the risk of clobbering the variable we chose if the other
9321 returned expression uses the chosen variable somehow. And people expect
9322 this restriction, anyway. (jason 2000-11-19)
9324 See finish_function and finalize_nrv for the rest of this optimization. */
9326 named_return_value_okay_p =
9327 (retval != NULL_TREE
9328 && !processing_template_decl
9329 /* Must be a local, automatic variable. */
9330 && VAR_P (retval)
9331 && DECL_CONTEXT (retval) == current_function_decl
9332 && ! TREE_STATIC (retval)
9333 /* And not a lambda or anonymous union proxy. */
9334 && !DECL_HAS_VALUE_EXPR_P (retval)
9335 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9336 /* The cv-unqualified type of the returned value must be the
9337 same as the cv-unqualified return type of the
9338 function. */
9339 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9340 (TYPE_MAIN_VARIANT (functype)))
9341 /* And the returned value must be non-volatile. */
9342 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9344 if (fn_returns_value_p && flag_elide_constructors)
9346 if (named_return_value_okay_p
9347 && (current_function_return_value == NULL_TREE
9348 || current_function_return_value == retval))
9349 current_function_return_value = retval;
9350 else
9351 current_function_return_value = error_mark_node;
9354 /* We don't need to do any conversions when there's nothing being
9355 returned. */
9356 if (!retval)
9357 return NULL_TREE;
9359 /* Do any required conversions. */
9360 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9361 /* No conversions are required. */
9363 else
9365 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9367 /* The functype's return type will have been set to void, if it
9368 was an incomplete type. Just treat this as 'return;' */
9369 if (VOID_TYPE_P (functype))
9370 return error_mark_node;
9372 /* If we had an id-expression obfuscated by force_paren_expr, we need
9373 to undo it so we can try to treat it as an rvalue below. */
9374 retval = maybe_undo_parenthesized_ref (retval);
9376 if (processing_template_decl)
9377 retval = build_non_dependent_expr (retval);
9379 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9380 treated as an rvalue for the purposes of overload resolution to
9381 favor move constructors over copy constructors.
9383 Note that these conditions are similar to, but not as strict as,
9384 the conditions for the named return value optimization. */
9385 bool converted = false;
9386 if ((cxx_dialect != cxx98)
9387 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9388 || TREE_CODE (retval) == PARM_DECL)
9389 && DECL_CONTEXT (retval) == current_function_decl
9390 && !TREE_STATIC (retval)
9391 /* This is only interesting for class type. */
9392 && CLASS_TYPE_P (functype))
9394 tree moved = move (retval);
9395 moved = convert_for_initialization
9396 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9397 ICR_RETURN, NULL_TREE, 0, tf_none);
9398 if (moved != error_mark_node)
9400 retval = moved;
9401 converted = true;
9405 /* First convert the value to the function's return type, then
9406 to the type of return value's location to handle the
9407 case that functype is smaller than the valtype. */
9408 if (!converted)
9409 retval = convert_for_initialization
9410 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9411 tf_warning_or_error);
9412 retval = convert (valtype, retval);
9414 /* If the conversion failed, treat this just like `return;'. */
9415 if (retval == error_mark_node)
9416 return retval;
9417 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9418 else if (! cfun->returns_struct
9419 && TREE_CODE (retval) == TARGET_EXPR
9420 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9421 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9422 TREE_OPERAND (retval, 0));
9423 else if (!processing_template_decl
9424 && maybe_warn_about_returning_address_of_local (retval)
9425 && INDIRECT_TYPE_P (valtype))
9426 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9427 build_zero_cst (TREE_TYPE (retval)));
9430 if (processing_template_decl)
9431 return saved_retval;
9433 /* Actually copy the value returned into the appropriate location. */
9434 if (retval && retval != result)
9435 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9437 return retval;
9441 /* Returns nonzero if the pointer-type FROM can be converted to the
9442 pointer-type TO via a qualification conversion. If CONSTP is -1,
9443 then we return nonzero if the pointers are similar, and the
9444 cv-qualification signature of FROM is a proper subset of that of TO.
9446 If CONSTP is positive, then all outer pointers have been
9447 const-qualified. */
9449 static int
9450 comp_ptr_ttypes_real (tree to, tree from, int constp)
9452 bool to_more_cv_qualified = false;
9453 bool is_opaque_pointer = false;
9455 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9457 if (TREE_CODE (to) != TREE_CODE (from))
9458 return 0;
9460 if (TREE_CODE (from) == OFFSET_TYPE
9461 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9462 TYPE_OFFSET_BASETYPE (to)))
9463 return 0;
9465 /* Const and volatile mean something different for function types,
9466 so the usual checks are not appropriate. */
9467 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9469 if (!at_least_as_qualified_p (to, from))
9470 return 0;
9472 if (!at_least_as_qualified_p (from, to))
9474 if (constp == 0)
9475 return 0;
9476 to_more_cv_qualified = true;
9479 if (constp > 0)
9480 constp &= TYPE_READONLY (to);
9483 if (VECTOR_TYPE_P (to))
9484 is_opaque_pointer = vector_targets_convertible_p (to, from);
9486 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9487 return ((constp >= 0 || to_more_cv_qualified)
9488 && (is_opaque_pointer
9489 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9493 /* When comparing, say, char ** to char const **, this function takes
9494 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9495 types to this function. */
9498 comp_ptr_ttypes (tree to, tree from)
9500 return comp_ptr_ttypes_real (to, from, 1);
9503 /* Returns true iff FNTYPE is a non-class type that involves
9504 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9505 if a parameter type is ill-formed. */
9507 bool
9508 error_type_p (const_tree type)
9510 tree t;
9512 switch (TREE_CODE (type))
9514 case ERROR_MARK:
9515 return true;
9517 case POINTER_TYPE:
9518 case REFERENCE_TYPE:
9519 case OFFSET_TYPE:
9520 return error_type_p (TREE_TYPE (type));
9522 case FUNCTION_TYPE:
9523 case METHOD_TYPE:
9524 if (error_type_p (TREE_TYPE (type)))
9525 return true;
9526 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9527 if (error_type_p (TREE_VALUE (t)))
9528 return true;
9529 return false;
9531 case RECORD_TYPE:
9532 if (TYPE_PTRMEMFUNC_P (type))
9533 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9534 return false;
9536 default:
9537 return false;
9541 /* Returns true if to and from are (possibly multi-level) pointers to the same
9542 type or inheritance-related types, regardless of cv-quals. */
9544 bool
9545 ptr_reasonably_similar (const_tree to, const_tree from)
9547 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9549 /* Any target type is similar enough to void. */
9550 if (VOID_TYPE_P (to))
9551 return !error_type_p (from);
9552 if (VOID_TYPE_P (from))
9553 return !error_type_p (to);
9555 if (TREE_CODE (to) != TREE_CODE (from))
9556 return false;
9558 if (TREE_CODE (from) == OFFSET_TYPE
9559 && comptypes (TYPE_OFFSET_BASETYPE (to),
9560 TYPE_OFFSET_BASETYPE (from),
9561 COMPARE_BASE | COMPARE_DERIVED))
9562 continue;
9564 if (VECTOR_TYPE_P (to)
9565 && vector_types_convertible_p (to, from, false))
9566 return true;
9568 if (TREE_CODE (to) == INTEGER_TYPE
9569 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9570 return true;
9572 if (TREE_CODE (to) == FUNCTION_TYPE)
9573 return !error_type_p (to) && !error_type_p (from);
9575 if (!TYPE_PTR_P (to))
9577 /* When either type is incomplete avoid DERIVED_FROM_P,
9578 which may call complete_type (c++/57942). */
9579 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9580 return comptypes
9581 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9582 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9587 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9588 pointer-to-member types) are the same, ignoring cv-qualification at
9589 all levels. */
9591 bool
9592 comp_ptr_ttypes_const (tree to, tree from)
9594 bool is_opaque_pointer = false;
9596 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9598 if (TREE_CODE (to) != TREE_CODE (from))
9599 return false;
9601 if (TREE_CODE (from) == OFFSET_TYPE
9602 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9603 TYPE_OFFSET_BASETYPE (to)))
9604 continue;
9606 if (VECTOR_TYPE_P (to))
9607 is_opaque_pointer = vector_targets_convertible_p (to, from);
9609 if (!TYPE_PTR_P (to))
9610 return (is_opaque_pointer
9611 || same_type_ignoring_top_level_qualifiers_p (to, from));
9615 /* Returns the type qualifiers for this type, including the qualifiers on the
9616 elements for an array type. */
9619 cp_type_quals (const_tree type)
9621 int quals;
9622 /* This CONST_CAST is okay because strip_array_types returns its
9623 argument unmodified and we assign it to a const_tree. */
9624 type = strip_array_types (CONST_CAST_TREE (type));
9625 if (type == error_mark_node
9626 /* Quals on a FUNCTION_TYPE are memfn quals. */
9627 || TREE_CODE (type) == FUNCTION_TYPE)
9628 return TYPE_UNQUALIFIED;
9629 quals = TYPE_QUALS (type);
9630 /* METHOD and REFERENCE_TYPEs should never have quals. */
9631 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9632 && !TYPE_REF_P (type))
9633 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9634 == TYPE_UNQUALIFIED));
9635 return quals;
9638 /* Returns the function-ref-qualifier for TYPE */
9640 cp_ref_qualifier
9641 type_memfn_rqual (const_tree type)
9643 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9644 || TREE_CODE (type) == METHOD_TYPE);
9646 if (!FUNCTION_REF_QUALIFIED (type))
9647 return REF_QUAL_NONE;
9648 else if (FUNCTION_RVALUE_QUALIFIED (type))
9649 return REF_QUAL_RVALUE;
9650 else
9651 return REF_QUAL_LVALUE;
9654 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9655 METHOD_TYPE. */
9658 type_memfn_quals (const_tree type)
9660 if (TREE_CODE (type) == FUNCTION_TYPE)
9661 return TYPE_QUALS (type);
9662 else if (TREE_CODE (type) == METHOD_TYPE)
9663 return cp_type_quals (class_of_this_parm (type));
9664 else
9665 gcc_unreachable ();
9668 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9669 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9671 tree
9672 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9674 /* Could handle METHOD_TYPE here if necessary. */
9675 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9676 if (TYPE_QUALS (type) == memfn_quals
9677 && type_memfn_rqual (type) == rqual)
9678 return type;
9680 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9681 complex. */
9682 tree result = build_qualified_type (type, memfn_quals);
9683 return build_ref_qualified_type (result, rqual);
9686 /* Returns nonzero if TYPE is const or volatile. */
9688 bool
9689 cv_qualified_p (const_tree type)
9691 int quals = cp_type_quals (type);
9692 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9695 /* Returns nonzero if the TYPE contains a mutable member. */
9697 bool
9698 cp_has_mutable_p (const_tree type)
9700 /* This CONST_CAST is okay because strip_array_types returns its
9701 argument unmodified and we assign it to a const_tree. */
9702 type = strip_array_types (CONST_CAST_TREE(type));
9704 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9707 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9708 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9709 approximation. In particular, consider:
9711 int f();
9712 struct S { int i; };
9713 const S s = { f(); }
9715 Here, we will make "s" as TREE_READONLY (because it is declared
9716 "const") -- only to reverse ourselves upon seeing that the
9717 initializer is non-constant. */
9719 void
9720 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9722 tree type = TREE_TYPE (decl);
9724 if (type == error_mark_node)
9725 return;
9727 if (TREE_CODE (decl) == TYPE_DECL)
9728 return;
9730 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9731 && type_quals != TYPE_UNQUALIFIED));
9733 /* Avoid setting TREE_READONLY incorrectly. */
9734 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9735 constructor can produce constant init, so rely on cp_finish_decl to
9736 clear TREE_READONLY if the variable has non-constant init. */
9738 /* If the type has (or might have) a mutable component, that component
9739 might be modified. */
9740 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9741 type_quals &= ~TYPE_QUAL_CONST;
9743 c_apply_type_quals_to_decl (type_quals, decl);
9746 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9747 exemplar types such that casting T1 to T2 is casting away constness
9748 if and only if there is no implicit conversion from T1 to T2. */
9750 static void
9751 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9753 int quals1;
9754 int quals2;
9756 /* [expr.const.cast]
9758 For multi-level pointer to members and multi-level mixed pointers
9759 and pointers to members (conv.qual), the "member" aspect of a
9760 pointer to member level is ignored when determining if a const
9761 cv-qualifier has been cast away. */
9762 /* [expr.const.cast]
9764 For two pointer types:
9766 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9767 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9768 K is min(N,M)
9770 casting from X1 to X2 casts away constness if, for a non-pointer
9771 type T there does not exist an implicit conversion (clause
9772 _conv_) from:
9774 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9778 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9779 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9780 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9782 *t1 = cp_build_qualified_type (void_type_node,
9783 cp_type_quals (*t1));
9784 *t2 = cp_build_qualified_type (void_type_node,
9785 cp_type_quals (*t2));
9786 return;
9789 quals1 = cp_type_quals (*t1);
9790 quals2 = cp_type_quals (*t2);
9792 if (TYPE_PTRDATAMEM_P (*t1))
9793 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9794 else
9795 *t1 = TREE_TYPE (*t1);
9796 if (TYPE_PTRDATAMEM_P (*t2))
9797 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9798 else
9799 *t2 = TREE_TYPE (*t2);
9801 casts_away_constness_r (t1, t2, complain);
9802 *t1 = build_pointer_type (*t1);
9803 *t2 = build_pointer_type (*t2);
9804 *t1 = cp_build_qualified_type (*t1, quals1);
9805 *t2 = cp_build_qualified_type (*t2, quals2);
9808 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9809 constness.
9811 ??? This function returns non-zero if casting away qualifiers not
9812 just const. We would like to return to the caller exactly which
9813 qualifiers are casted away to give more accurate diagnostics.
9816 static bool
9817 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9819 if (TYPE_REF_P (t2))
9821 /* [expr.const.cast]
9823 Casting from an lvalue of type T1 to an lvalue of type T2
9824 using a reference cast casts away constness if a cast from an
9825 rvalue of type "pointer to T1" to the type "pointer to T2"
9826 casts away constness. */
9827 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
9828 return casts_away_constness (build_pointer_type (t1),
9829 build_pointer_type (TREE_TYPE (t2)),
9830 complain);
9833 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9834 /* [expr.const.cast]
9836 Casting from an rvalue of type "pointer to data member of X
9837 of type T1" to the type "pointer to data member of Y of type
9838 T2" casts away constness if a cast from an rvalue of type
9839 "pointer to T1" to the type "pointer to T2" casts away
9840 constness. */
9841 return casts_away_constness
9842 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9843 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9844 complain);
9846 /* Casting away constness is only something that makes sense for
9847 pointer or reference types. */
9848 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9849 return false;
9851 /* Top-level qualifiers don't matter. */
9852 t1 = TYPE_MAIN_VARIANT (t1);
9853 t2 = TYPE_MAIN_VARIANT (t2);
9854 casts_away_constness_r (&t1, &t2, complain);
9855 if (!can_convert (t2, t1, complain))
9856 return true;
9858 return false;
9861 /* If T is a REFERENCE_TYPE return the type to which T refers.
9862 Otherwise, return T itself. */
9864 tree
9865 non_reference (tree t)
9867 if (t && TYPE_REF_P (t))
9868 t = TREE_TYPE (t);
9869 return t;
9873 /* Return nonzero if REF is an lvalue valid for this language;
9874 otherwise, print an error message and return zero. USE says
9875 how the lvalue is being used and so selects the error message. */
9878 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9880 cp_lvalue_kind kind = lvalue_kind (ref);
9882 if (kind == clk_none)
9884 if (complain & tf_error)
9885 lvalue_error (input_location, use);
9886 return 0;
9888 else if (kind & (clk_rvalueref|clk_class))
9890 if (!(complain & tf_error))
9891 return 0;
9892 /* Make this a permerror because we used to accept it. */
9893 permerror (input_location, "using rvalue as lvalue");
9895 return 1;
9898 /* Return true if a user-defined literal operator is a raw operator. */
9900 bool
9901 check_raw_literal_operator (const_tree decl)
9903 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9904 tree argtype;
9905 int arity;
9906 bool maybe_raw_p = false;
9908 /* Count the number and type of arguments and check for ellipsis. */
9909 for (argtype = argtypes, arity = 0;
9910 argtype && argtype != void_list_node;
9911 ++arity, argtype = TREE_CHAIN (argtype))
9913 tree t = TREE_VALUE (argtype);
9915 if (same_type_p (t, const_string_type_node))
9916 maybe_raw_p = true;
9918 if (!argtype)
9919 return false; /* Found ellipsis. */
9921 if (!maybe_raw_p || arity != 1)
9922 return false;
9924 return true;
9928 /* Return true if a user-defined literal operator has one of the allowed
9929 argument types. */
9931 bool
9932 check_literal_operator_args (const_tree decl,
9933 bool *long_long_unsigned_p, bool *long_double_p)
9935 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9937 *long_long_unsigned_p = false;
9938 *long_double_p = false;
9939 if (processing_template_decl || processing_specialization)
9940 return argtypes == void_list_node;
9941 else
9943 tree argtype;
9944 int arity;
9945 int max_arity = 2;
9947 /* Count the number and type of arguments and check for ellipsis. */
9948 for (argtype = argtypes, arity = 0;
9949 argtype && argtype != void_list_node;
9950 argtype = TREE_CHAIN (argtype))
9952 tree t = TREE_VALUE (argtype);
9953 ++arity;
9955 if (TYPE_PTR_P (t))
9957 bool maybe_raw_p = false;
9958 t = TREE_TYPE (t);
9959 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9960 return false;
9961 t = TYPE_MAIN_VARIANT (t);
9962 if ((maybe_raw_p = same_type_p (t, char_type_node))
9963 || same_type_p (t, wchar_type_node)
9964 || same_type_p (t, char16_type_node)
9965 || same_type_p (t, char32_type_node))
9967 argtype = TREE_CHAIN (argtype);
9968 if (!argtype)
9969 return false;
9970 t = TREE_VALUE (argtype);
9971 if (maybe_raw_p && argtype == void_list_node)
9972 return true;
9973 else if (same_type_p (t, size_type_node))
9975 ++arity;
9976 continue;
9978 else
9979 return false;
9982 else if (same_type_p (t, long_long_unsigned_type_node))
9984 max_arity = 1;
9985 *long_long_unsigned_p = true;
9987 else if (same_type_p (t, long_double_type_node))
9989 max_arity = 1;
9990 *long_double_p = true;
9992 else if (same_type_p (t, char_type_node))
9993 max_arity = 1;
9994 else if (same_type_p (t, wchar_type_node))
9995 max_arity = 1;
9996 else if (same_type_p (t, char16_type_node))
9997 max_arity = 1;
9998 else if (same_type_p (t, char32_type_node))
9999 max_arity = 1;
10000 else
10001 return false;
10003 if (!argtype)
10004 return false; /* Found ellipsis. */
10006 if (arity != max_arity)
10007 return false;
10009 return true;
10013 /* Always returns false since unlike C90, C++ has no concept of implicit
10014 function declarations. */
10016 bool
10017 c_decl_implicit (const_tree)
10019 return false;