[C++ PATCH] cleanup 2
[official-gcc.git] / gcc / cp / typeck.c
blob24a206a2511af370faa15ad352a8416a8e081b06
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "target.h"
31 #include "cp-tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "intl.h"
35 #include "convert.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
38 #include "params.h"
39 #include "gcc-rich-location.h"
40 #include "stringpool.h"
41 #include "attribs.h"
42 #include "asan.h"
44 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46 static tree pfn_from_ptrmemfunc (tree);
47 static tree delta_from_ptrmemfunc (tree);
48 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 tsubst_flags_t, int);
50 static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 tsubst_flags_t);
52 static tree rationalize_conditional_expr (enum tree_code, tree,
53 tsubst_flags_t);
54 static int comp_ptr_ttypes_real (tree, tree, int);
55 static bool comp_except_types (tree, tree, bool);
56 static bool comp_array_types (const_tree, const_tree, bool);
57 static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 static bool maybe_warn_about_returning_address_of_local (tree);
62 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
63 static void error_args_num (location_t, tree, bool);
64 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
65 tsubst_flags_t);
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.)
69 Returns error_mark_node if the VALUE does not have
70 complete type when this function returns. */
72 tree
73 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
75 tree type;
77 if (processing_template_decl || value == error_mark_node)
78 return value;
80 if (TREE_CODE (value) == OVERLOAD)
81 type = unknown_type_node;
82 else
83 type = TREE_TYPE (value);
85 if (type == error_mark_node)
86 return error_mark_node;
88 /* First, detect a valid value with a complete type. */
89 if (COMPLETE_TYPE_P (type))
90 return value;
92 if (complete_type_or_maybe_complain (type, value, complain))
93 return value;
94 else
95 return error_mark_node;
98 tree
99 require_complete_type (tree value)
101 return require_complete_type_sfinae (value, tf_warning_or_error);
104 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
105 a template instantiation, do the instantiation. Returns TYPE,
106 whether or not it could be completed, unless something goes
107 horribly wrong, in which case the error_mark_node is returned. */
109 tree
110 complete_type (tree type)
112 if (type == NULL_TREE)
113 /* Rather than crash, we return something sure to cause an error
114 at some point. */
115 return error_mark_node;
117 if (type == error_mark_node || COMPLETE_TYPE_P (type))
119 else if (TREE_CODE (type) == ARRAY_TYPE)
121 tree t = complete_type (TREE_TYPE (type));
122 unsigned int needs_constructing, has_nontrivial_dtor;
123 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
124 layout_type (type);
125 needs_constructing
126 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
127 has_nontrivial_dtor
128 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
129 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
131 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
132 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
135 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
136 instantiate_class_template (TYPE_MAIN_VARIANT (type));
138 return type;
141 /* Like complete_type, but issue an error if the TYPE cannot be completed.
142 VALUE is used for informative diagnostics.
143 Returns NULL_TREE if the type cannot be made complete. */
145 tree
146 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
148 type = complete_type (type);
149 if (type == error_mark_node)
150 /* We already issued an error. */
151 return NULL_TREE;
152 else if (!COMPLETE_TYPE_P (type))
154 if (complain & tf_error)
155 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
156 return NULL_TREE;
158 else
159 return type;
162 tree
163 complete_type_or_else (tree type, tree value)
165 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
169 /* Return the common type of two parameter lists.
170 We assume that comptypes has already been done and returned 1;
171 if that isn't so, this may crash.
173 As an optimization, free the space we allocate if the parameter
174 lists are already common. */
176 static tree
177 commonparms (tree p1, tree p2)
179 tree oldargs = p1, newargs, n;
180 int i, len;
181 int any_change = 0;
183 len = list_length (p1);
184 newargs = tree_last (p1);
186 if (newargs == void_list_node)
187 i = 1;
188 else
190 i = 0;
191 newargs = 0;
194 for (; i < len; i++)
195 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
197 n = newargs;
199 for (i = 0; p1;
200 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
202 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
204 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
205 any_change = 1;
207 else if (! TREE_PURPOSE (p1))
209 if (TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
212 any_change = 1;
215 else
217 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
218 any_change = 1;
219 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 if (TREE_VALUE (p1) != TREE_VALUE (p2))
223 any_change = 1;
224 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
226 else
227 TREE_VALUE (n) = TREE_VALUE (p1);
229 if (! any_change)
230 return oldargs;
232 return newargs;
235 /* Given a type, perhaps copied for a typedef,
236 find the "original" version of it. */
237 static tree
238 original_type (tree t)
240 int quals = cp_type_quals (t);
241 while (t != error_mark_node
242 && TYPE_NAME (t) != NULL_TREE)
244 tree x = TYPE_NAME (t);
245 if (TREE_CODE (x) != TYPE_DECL)
246 break;
247 x = DECL_ORIGINAL_TYPE (x);
248 if (x == NULL_TREE)
249 break;
250 t = x;
252 return cp_build_qualified_type (t, quals);
255 /* Return the common type for two arithmetic types T1 and T2 under the
256 usual arithmetic conversions. The default conversions have already
257 been applied, and enumerated types converted to their compatible
258 integer types. */
260 static tree
261 cp_common_type (tree t1, tree t2)
263 enum tree_code code1 = TREE_CODE (t1);
264 enum tree_code code2 = TREE_CODE (t2);
265 tree attributes;
266 int i;
269 /* In what follows, we slightly generalize the rules given in [expr] so
270 as to deal with `long long' and `complex'. First, merge the
271 attributes. */
272 attributes = (*targetm.merge_type_attributes) (t1, t2);
274 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
276 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
277 return build_type_attribute_variant (t1, attributes);
278 else
279 return NULL_TREE;
282 /* FIXME: Attributes. */
283 gcc_assert (ARITHMETIC_TYPE_P (t1)
284 || VECTOR_TYPE_P (t1)
285 || UNSCOPED_ENUM_P (t1));
286 gcc_assert (ARITHMETIC_TYPE_P (t2)
287 || VECTOR_TYPE_P (t2)
288 || UNSCOPED_ENUM_P (t2));
290 /* If one type is complex, form the common type of the non-complex
291 components, then make that complex. Use T1 or T2 if it is the
292 required type. */
293 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
295 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
296 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
297 tree subtype
298 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
300 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
301 return build_type_attribute_variant (t1, attributes);
302 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
303 return build_type_attribute_variant (t2, attributes);
304 else
305 return build_type_attribute_variant (build_complex_type (subtype),
306 attributes);
309 if (code1 == VECTOR_TYPE)
311 /* When we get here we should have two vectors of the same size.
312 Just prefer the unsigned one if present. */
313 if (TYPE_UNSIGNED (t1))
314 return build_type_attribute_variant (t1, attributes);
315 else
316 return build_type_attribute_variant (t2, attributes);
319 /* If only one is real, use it as the result. */
320 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
321 return build_type_attribute_variant (t1, attributes);
322 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
323 return build_type_attribute_variant (t2, attributes);
325 /* Both real or both integers; use the one with greater precision. */
326 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
327 return build_type_attribute_variant (t1, attributes);
328 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
329 return build_type_attribute_variant (t2, attributes);
331 /* The types are the same; no need to do anything fancy. */
332 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
333 return build_type_attribute_variant (t1, attributes);
335 if (code1 != REAL_TYPE)
337 /* If one is unsigned long long, then convert the other to unsigned
338 long long. */
339 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
340 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
341 return build_type_attribute_variant (long_long_unsigned_type_node,
342 attributes);
343 /* If one is a long long, and the other is an unsigned long, and
344 long long can represent all the values of an unsigned long, then
345 convert to a long long. Otherwise, convert to an unsigned long
346 long. Otherwise, if either operand is long long, convert the
347 other to long long.
349 Since we're here, we know the TYPE_PRECISION is the same;
350 therefore converting to long long cannot represent all the values
351 of an unsigned long, so we choose unsigned long long in that
352 case. */
353 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
354 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
356 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
357 ? long_long_unsigned_type_node
358 : long_long_integer_type_node);
359 return build_type_attribute_variant (t, attributes);
362 /* Go through the same procedure, but for longs. */
363 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
364 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
365 return build_type_attribute_variant (long_unsigned_type_node,
366 attributes);
367 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
368 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
370 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
371 ? long_unsigned_type_node : long_integer_type_node);
372 return build_type_attribute_variant (t, attributes);
375 /* For __intN types, either the type is __int128 (and is lower
376 priority than the types checked above, but higher than other
377 128-bit types) or it's known to not be the same size as other
378 types (enforced in toplev.c). Prefer the unsigned type. */
379 for (i = 0; i < NUM_INT_N_ENTS; i ++)
381 if (int_n_enabled_p [i]
382 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
383 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
384 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? int_n_trees[i].unsigned_type
389 : int_n_trees[i].signed_type);
390 return build_type_attribute_variant (t, attributes);
394 /* Otherwise prefer the unsigned one. */
395 if (TYPE_UNSIGNED (t1))
396 return build_type_attribute_variant (t1, attributes);
397 else
398 return build_type_attribute_variant (t2, attributes);
400 else
402 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
403 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
404 return build_type_attribute_variant (long_double_type_node,
405 attributes);
406 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
407 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
408 return build_type_attribute_variant (double_type_node,
409 attributes);
410 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
411 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
412 return build_type_attribute_variant (float_type_node,
413 attributes);
415 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
416 the standard C++ floating-point types. Logic earlier in this
417 function has already eliminated the possibility that
418 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
419 compelling reason to choose one or the other. */
420 return build_type_attribute_variant (t1, attributes);
424 /* T1 and T2 are arithmetic or enumeration types. Return the type
425 that will result from the "usual arithmetic conversions" on T1 and
426 T2 as described in [expr]. */
428 tree
429 type_after_usual_arithmetic_conversions (tree t1, tree t2)
431 gcc_assert (ARITHMETIC_TYPE_P (t1)
432 || VECTOR_TYPE_P (t1)
433 || UNSCOPED_ENUM_P (t1));
434 gcc_assert (ARITHMETIC_TYPE_P (t2)
435 || VECTOR_TYPE_P (t2)
436 || UNSCOPED_ENUM_P (t2));
438 /* Perform the integral promotions. We do not promote real types here. */
439 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
440 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
442 t1 = type_promotes_to (t1);
443 t2 = type_promotes_to (t2);
446 return cp_common_type (t1, t2);
449 static void
450 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
451 composite_pointer_operation operation)
453 switch (operation)
455 case CPO_COMPARISON:
456 emit_diagnostic (kind, input_location, 0,
457 "comparison between "
458 "distinct pointer types %qT and %qT lacks a cast",
459 t1, t2);
460 break;
461 case CPO_CONVERSION:
462 emit_diagnostic (kind, input_location, 0,
463 "conversion between "
464 "distinct pointer types %qT and %qT lacks a cast",
465 t1, t2);
466 break;
467 case CPO_CONDITIONAL_EXPR:
468 emit_diagnostic (kind, input_location, 0,
469 "conditional expression between "
470 "distinct pointer types %qT and %qT lacks a cast",
471 t1, t2);
472 break;
473 default:
474 gcc_unreachable ();
478 /* Subroutine of composite_pointer_type to implement the recursive
479 case. See that function for documentation of the parameters. */
481 static tree
482 composite_pointer_type_r (tree t1, tree t2,
483 composite_pointer_operation operation,
484 tsubst_flags_t complain)
486 tree pointee1;
487 tree pointee2;
488 tree result_type;
489 tree attributes;
491 /* Determine the types pointed to by T1 and T2. */
492 if (TYPE_PTR_P (t1))
494 pointee1 = TREE_TYPE (t1);
495 pointee2 = TREE_TYPE (t2);
497 else
499 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
500 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
503 /* [expr.rel]
505 Otherwise, the composite pointer type is a pointer type
506 similar (_conv.qual_) to the type of one of the operands,
507 with a cv-qualification signature (_conv.qual_) that is the
508 union of the cv-qualification signatures of the operand
509 types. */
510 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
511 result_type = pointee1;
512 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
513 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
515 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
516 complain);
517 if (result_type == error_mark_node)
518 return error_mark_node;
520 else
522 if (complain & tf_error)
523 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
524 else
525 return error_mark_node;
526 result_type = void_type_node;
528 result_type = cp_build_qualified_type (result_type,
529 (cp_type_quals (pointee1)
530 | cp_type_quals (pointee2)));
531 /* If the original types were pointers to members, so is the
532 result. */
533 if (TYPE_PTRMEM_P (t1))
535 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
536 TYPE_PTRMEM_CLASS_TYPE (t2)))
538 if (complain & tf_error)
539 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
540 else
541 return error_mark_node;
543 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
544 result_type);
546 else
547 result_type = build_pointer_type (result_type);
549 /* Merge the attributes. */
550 attributes = (*targetm.merge_type_attributes) (t1, t2);
551 return build_type_attribute_variant (result_type, attributes);
554 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
555 ARG1 and ARG2 are the values with those types. The OPERATION is to
556 describe the operation between the pointer types,
557 in case an error occurs.
559 This routine also implements the computation of a common type for
560 pointers-to-members as per [expr.eq]. */
562 tree
563 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
564 composite_pointer_operation operation,
565 tsubst_flags_t complain)
567 tree class1;
568 tree class2;
570 /* [expr.rel]
572 If one operand is a null pointer constant, the composite pointer
573 type is the type of the other operand. */
574 if (null_ptr_cst_p (arg1))
575 return t2;
576 if (null_ptr_cst_p (arg2))
577 return t1;
579 /* We have:
581 [expr.rel]
583 If one of the operands has type "pointer to cv1 void*", then
584 the other has type "pointer to cv2T", and the composite pointer
585 type is "pointer to cv12 void", where cv12 is the union of cv1
586 and cv2.
588 If either type is a pointer to void, make sure it is T1. */
589 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
590 std::swap (t1, t2);
592 /* Now, if T1 is a pointer to void, merge the qualifiers. */
593 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
595 tree attributes;
596 tree result_type;
598 if (TYPE_PTRFN_P (t2))
600 if (complain & tf_error)
602 switch (operation)
604 case CPO_COMPARISON:
605 pedwarn (input_location, OPT_Wpedantic,
606 "ISO C++ forbids comparison between pointer "
607 "of type %<void *%> and pointer-to-function");
608 break;
609 case CPO_CONVERSION:
610 pedwarn (input_location, OPT_Wpedantic,
611 "ISO C++ forbids conversion between pointer "
612 "of type %<void *%> and pointer-to-function");
613 break;
614 case CPO_CONDITIONAL_EXPR:
615 pedwarn (input_location, OPT_Wpedantic,
616 "ISO C++ forbids conditional expression between "
617 "pointer of type %<void *%> and "
618 "pointer-to-function");
619 break;
620 default:
621 gcc_unreachable ();
624 else
625 return error_mark_node;
627 result_type
628 = cp_build_qualified_type (void_type_node,
629 (cp_type_quals (TREE_TYPE (t1))
630 | cp_type_quals (TREE_TYPE (t2))));
631 result_type = build_pointer_type (result_type);
632 /* Merge the attributes. */
633 attributes = (*targetm.merge_type_attributes) (t1, t2);
634 return build_type_attribute_variant (result_type, attributes);
637 if (c_dialect_objc () && TYPE_PTR_P (t1)
638 && TYPE_PTR_P (t2))
640 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
641 return objc_common_type (t1, t2);
644 /* if T1 or T2 is "pointer to noexcept function" and the other type is
645 "pointer to function", where the function types are otherwise the same,
646 "pointer to function" */
647 if (fnptr_conv_p (t1, t2))
648 return t1;
649 if (fnptr_conv_p (t2, t1))
650 return t2;
652 /* [expr.eq] permits the application of a pointer conversion to
653 bring the pointers to a common type. */
654 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
655 && CLASS_TYPE_P (TREE_TYPE (t1))
656 && CLASS_TYPE_P (TREE_TYPE (t2))
657 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
658 TREE_TYPE (t2)))
660 class1 = TREE_TYPE (t1);
661 class2 = TREE_TYPE (t2);
663 if (DERIVED_FROM_P (class1, class2))
664 t2 = (build_pointer_type
665 (cp_build_qualified_type (class1, cp_type_quals (class2))));
666 else if (DERIVED_FROM_P (class2, class1))
667 t1 = (build_pointer_type
668 (cp_build_qualified_type (class2, cp_type_quals (class1))));
669 else
671 if (complain & tf_error)
672 composite_pointer_error (DK_ERROR, t1, t2, operation);
673 return error_mark_node;
676 /* [expr.eq] permits the application of a pointer-to-member
677 conversion to change the class type of one of the types. */
678 else if (TYPE_PTRMEM_P (t1)
679 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
680 TYPE_PTRMEM_CLASS_TYPE (t2)))
682 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
683 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
685 if (DERIVED_FROM_P (class1, class2))
686 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
687 else if (DERIVED_FROM_P (class2, class1))
688 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
689 else
691 if (complain & tf_error)
692 switch (operation)
694 case CPO_COMPARISON:
695 error ("comparison between distinct "
696 "pointer-to-member types %qT and %qT lacks a cast",
697 t1, t2);
698 break;
699 case CPO_CONVERSION:
700 error ("conversion between distinct "
701 "pointer-to-member types %qT and %qT lacks a cast",
702 t1, t2);
703 break;
704 case CPO_CONDITIONAL_EXPR:
705 error ("conditional expression between distinct "
706 "pointer-to-member types %qT and %qT lacks a cast",
707 t1, t2);
708 break;
709 default:
710 gcc_unreachable ();
712 return error_mark_node;
716 return composite_pointer_type_r (t1, t2, operation, complain);
719 /* Return the merged type of two types.
720 We assume that comptypes has already been done and returned 1;
721 if that isn't so, this may crash.
723 This just combines attributes and default arguments; any other
724 differences would cause the two types to compare unalike. */
726 tree
727 merge_types (tree t1, tree t2)
729 enum tree_code code1;
730 enum tree_code code2;
731 tree attributes;
733 /* Save time if the two types are the same. */
734 if (t1 == t2)
735 return t1;
736 if (original_type (t1) == original_type (t2))
737 return t1;
739 /* If one type is nonsense, use the other. */
740 if (t1 == error_mark_node)
741 return t2;
742 if (t2 == error_mark_node)
743 return t1;
745 /* Handle merging an auto redeclaration with a previous deduced
746 return type. */
747 if (is_auto (t1))
748 return t2;
750 /* Merge the attributes. */
751 attributes = (*targetm.merge_type_attributes) (t1, t2);
753 if (TYPE_PTRMEMFUNC_P (t1))
754 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
755 if (TYPE_PTRMEMFUNC_P (t2))
756 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
758 code1 = TREE_CODE (t1);
759 code2 = TREE_CODE (t2);
760 if (code1 != code2)
762 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
763 if (code1 == TYPENAME_TYPE)
765 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
766 code1 = TREE_CODE (t1);
768 else
770 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
771 code2 = TREE_CODE (t2);
775 switch (code1)
777 case POINTER_TYPE:
778 case REFERENCE_TYPE:
779 /* For two pointers, do this recursively on the target type. */
781 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
782 int quals = cp_type_quals (t1);
784 if (code1 == POINTER_TYPE)
786 t1 = build_pointer_type (target);
787 if (TREE_CODE (target) == METHOD_TYPE)
788 t1 = build_ptrmemfunc_type (t1);
790 else
791 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
792 t1 = build_type_attribute_variant (t1, attributes);
793 t1 = cp_build_qualified_type (t1, quals);
795 return t1;
798 case OFFSET_TYPE:
800 int quals;
801 tree pointee;
802 quals = cp_type_quals (t1);
803 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
804 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
805 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
806 pointee);
807 t1 = cp_build_qualified_type (t1, quals);
808 break;
811 case ARRAY_TYPE:
813 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
814 /* Save space: see if the result is identical to one of the args. */
815 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
816 return build_type_attribute_variant (t1, attributes);
817 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
818 return build_type_attribute_variant (t2, attributes);
819 /* Merge the element types, and have a size if either arg has one. */
820 t1 = build_cplus_array_type
821 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
822 break;
825 case FUNCTION_TYPE:
826 /* Function types: prefer the one that specified arg types.
827 If both do, merge the arg types. Also merge the return types. */
829 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
830 tree p1 = TYPE_ARG_TYPES (t1);
831 tree p2 = TYPE_ARG_TYPES (t2);
832 tree parms;
833 tree rval, raises;
834 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
836 /* Save space: see if the result is identical to one of the args. */
837 if (valtype == TREE_TYPE (t1) && ! p2)
838 return cp_build_type_attribute_variant (t1, attributes);
839 if (valtype == TREE_TYPE (t2) && ! p1)
840 return cp_build_type_attribute_variant (t2, attributes);
842 /* Simple way if one arg fails to specify argument types. */
843 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
844 parms = p2;
845 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
846 parms = p1;
847 else
848 parms = commonparms (p1, p2);
850 rval = build_function_type (valtype, parms);
851 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
852 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
853 rval = apply_memfn_quals (rval,
854 type_memfn_quals (t1),
855 type_memfn_rqual (t1));
856 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
857 TYPE_RAISES_EXCEPTIONS (t2));
858 t1 = build_exception_variant (rval, raises);
859 if (late_return_type_p)
860 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
861 break;
864 case METHOD_TYPE:
866 /* Get this value the long way, since TYPE_METHOD_BASETYPE
867 is just the main variant of this. */
868 tree basetype = class_of_this_parm (t2);
869 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
870 TYPE_RAISES_EXCEPTIONS (t2));
871 cp_ref_qualifier rqual = type_memfn_rqual (t1);
872 tree t3;
873 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
874 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
876 /* If this was a member function type, get back to the
877 original type of type member function (i.e., without
878 the class instance variable up front. */
879 t1 = build_function_type (TREE_TYPE (t1),
880 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
881 t2 = build_function_type (TREE_TYPE (t2),
882 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
883 t3 = merge_types (t1, t2);
884 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
885 TYPE_ARG_TYPES (t3));
886 t1 = build_exception_variant (t3, raises);
887 t1 = build_ref_qualified_type (t1, rqual);
888 if (late_return_type_1_p)
889 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
890 if (late_return_type_2_p)
891 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
892 break;
895 case TYPENAME_TYPE:
896 /* There is no need to merge attributes into a TYPENAME_TYPE.
897 When the type is instantiated it will have whatever
898 attributes result from the instantiation. */
899 return t1;
901 default:;
902 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
903 return t1;
904 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
905 return t2;
906 break;
909 return cp_build_type_attribute_variant (t1, attributes);
912 /* Return the ARRAY_TYPE type without its domain. */
914 tree
915 strip_array_domain (tree type)
917 tree t2;
918 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
919 if (TYPE_DOMAIN (type) == NULL_TREE)
920 return type;
921 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
922 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
925 /* Wrapper around cp_common_type that is used by c-common.c and other
926 front end optimizations that remove promotions.
928 Return the common type for two arithmetic types T1 and T2 under the
929 usual arithmetic conversions. The default conversions have already
930 been applied, and enumerated types converted to their compatible
931 integer types. */
933 tree
934 common_type (tree t1, tree t2)
936 /* If one type is nonsense, use the other */
937 if (t1 == error_mark_node)
938 return t2;
939 if (t2 == error_mark_node)
940 return t1;
942 return cp_common_type (t1, t2);
945 /* Return the common type of two pointer types T1 and T2. This is the
946 type for the result of most arithmetic operations if the operands
947 have the given two types.
949 We assume that comp_target_types has already been done and returned
950 nonzero; if that isn't so, this may crash. */
952 tree
953 common_pointer_type (tree t1, tree t2)
955 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
956 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
957 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
959 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
960 CPO_CONVERSION, tf_warning_or_error);
963 /* Compare two exception specifier types for exactness or subsetness, if
964 allowed. Returns false for mismatch, true for match (same, or
965 derived and !exact).
967 [except.spec] "If a class X ... objects of class X or any class publicly
968 and unambiguously derived from X. Similarly, if a pointer type Y * ...
969 exceptions of type Y * or that are pointers to any type publicly and
970 unambiguously derived from Y. Otherwise a function only allows exceptions
971 that have the same type ..."
972 This does not mention cv qualifiers and is different to what throw
973 [except.throw] and catch [except.catch] will do. They will ignore the
974 top level cv qualifiers, and allow qualifiers in the pointer to class
975 example.
977 We implement the letter of the standard. */
979 static bool
980 comp_except_types (tree a, tree b, bool exact)
982 if (same_type_p (a, b))
983 return true;
984 else if (!exact)
986 if (cp_type_quals (a) || cp_type_quals (b))
987 return false;
989 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
991 a = TREE_TYPE (a);
992 b = TREE_TYPE (b);
993 if (cp_type_quals (a) || cp_type_quals (b))
994 return false;
997 if (TREE_CODE (a) != RECORD_TYPE
998 || TREE_CODE (b) != RECORD_TYPE)
999 return false;
1001 if (publicly_uniquely_derived_p (a, b))
1002 return true;
1004 return false;
1007 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1008 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1009 If EXACT is ce_type, the C++17 type compatibility rules apply.
1010 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1011 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1012 are unordered, but we've already filtered out duplicates. Most lists will
1013 be in order, we should try to make use of that. */
1015 bool
1016 comp_except_specs (const_tree t1, const_tree t2, int exact)
1018 const_tree probe;
1019 const_tree base;
1020 int length = 0;
1022 if (t1 == t2)
1023 return true;
1025 /* First handle noexcept. */
1026 if (exact < ce_exact)
1028 if (exact == ce_type
1029 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1030 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1031 return true;
1033 /* noexcept(false) is compatible with no exception-specification,
1034 and less strict than any spec. */
1035 if (t1 == noexcept_false_spec)
1036 return t2 == NULL_TREE || exact == ce_derived;
1037 /* Even a derived noexcept(false) is compatible with no
1038 exception-specification. */
1039 if (t2 == noexcept_false_spec)
1040 return t1 == NULL_TREE;
1042 /* Otherwise, if we aren't looking for an exact match, noexcept is
1043 equivalent to throw(). */
1044 if (t1 == noexcept_true_spec)
1045 t1 = empty_except_spec;
1046 if (t2 == noexcept_true_spec)
1047 t2 = empty_except_spec;
1050 /* If any noexcept is left, it is only comparable to itself;
1051 either we're looking for an exact match or we're redeclaring a
1052 template with dependent noexcept. */
1053 if ((t1 && TREE_PURPOSE (t1))
1054 || (t2 && TREE_PURPOSE (t2)))
1055 return (t1 && t2
1056 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1058 if (t1 == NULL_TREE) /* T1 is ... */
1059 return t2 == NULL_TREE || exact == ce_derived;
1060 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1061 return t2 != NULL_TREE && !TREE_VALUE (t2);
1062 if (t2 == NULL_TREE) /* T2 is ... */
1063 return false;
1064 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1065 return exact == ce_derived;
1067 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1068 Count how many we find, to determine exactness. For exact matching and
1069 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1070 O(nm). */
1071 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1073 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1075 tree a = TREE_VALUE (probe);
1076 tree b = TREE_VALUE (t2);
1078 if (comp_except_types (a, b, exact))
1080 if (probe == base && exact > ce_derived)
1081 base = TREE_CHAIN (probe);
1082 length++;
1083 break;
1086 if (probe == NULL_TREE)
1087 return false;
1089 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1092 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1093 [] can match [size]. */
1095 static bool
1096 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1098 tree d1;
1099 tree d2;
1100 tree max1, max2;
1102 if (t1 == t2)
1103 return true;
1105 /* The type of the array elements must be the same. */
1106 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1107 return false;
1109 d1 = TYPE_DOMAIN (t1);
1110 d2 = TYPE_DOMAIN (t2);
1112 if (d1 == d2)
1113 return true;
1115 /* If one of the arrays is dimensionless, and the other has a
1116 dimension, they are of different types. However, it is valid to
1117 write:
1119 extern int a[];
1120 int a[3];
1122 by [basic.link]:
1124 declarations for an array object can specify
1125 array types that differ by the presence or absence of a major
1126 array bound (_dcl.array_). */
1127 if (!d1 || !d2)
1128 return allow_redeclaration;
1130 /* Check that the dimensions are the same. */
1132 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1133 return false;
1134 max1 = TYPE_MAX_VALUE (d1);
1135 max2 = TYPE_MAX_VALUE (d2);
1137 if (!cp_tree_equal (max1, max2))
1138 return false;
1140 return true;
1143 /* Compare the relative position of T1 and T2 into their respective
1144 template parameter list.
1145 T1 and T2 must be template parameter types.
1146 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1148 static bool
1149 comp_template_parms_position (tree t1, tree t2)
1151 tree index1, index2;
1152 gcc_assert (t1 && t2
1153 && TREE_CODE (t1) == TREE_CODE (t2)
1154 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1155 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1156 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1158 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1159 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1161 /* Then compare their relative position. */
1162 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1163 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1164 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1165 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1166 return false;
1168 /* In C++14 we can end up comparing 'auto' to a normal template
1169 parameter. Don't confuse them. */
1170 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1171 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1173 return true;
1176 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1178 static bool
1179 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1181 t1 = TYPE_MAIN_VARIANT (t1);
1182 t2 = TYPE_MAIN_VARIANT (t2);
1184 if (TREE_CODE (t1) == POINTER_TYPE
1185 && TREE_CODE (t2) == POINTER_TYPE)
1186 return true;
1188 /* The signedness of the parameter matters only when an integral
1189 type smaller than int is promoted to int, otherwise only the
1190 precision of the parameter matters.
1191 This check should make sure that the callee does not see
1192 undefined values in argument registers. */
1193 if (INTEGRAL_TYPE_P (t1)
1194 && INTEGRAL_TYPE_P (t2)
1195 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1196 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1197 || !targetm.calls.promote_prototypes (NULL_TREE)
1198 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1199 return true;
1201 return same_type_p (t1, t2);
1204 /* Check if a type cast between two function types can be considered safe. */
1206 static bool
1207 cxx_safe_function_type_cast_p (tree t1, tree t2)
1209 if (TREE_TYPE (t1) == void_type_node &&
1210 TYPE_ARG_TYPES (t1) == void_list_node)
1211 return true;
1213 if (TREE_TYPE (t2) == void_type_node &&
1214 TYPE_ARG_TYPES (t2) == void_list_node)
1215 return true;
1217 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1218 return false;
1220 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1221 t1 && t2;
1222 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1223 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1224 return false;
1226 return true;
1229 /* Subroutine in comptypes. */
1231 static bool
1232 structural_comptypes (tree t1, tree t2, int strict)
1234 if (t1 == t2)
1235 return true;
1237 /* Suppress errors caused by previously reported errors. */
1238 if (t1 == error_mark_node || t2 == error_mark_node)
1239 return false;
1241 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1243 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1244 current instantiation. */
1245 if (TREE_CODE (t1) == TYPENAME_TYPE)
1246 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1248 if (TREE_CODE (t2) == TYPENAME_TYPE)
1249 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1251 if (TYPE_PTRMEMFUNC_P (t1))
1252 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1253 if (TYPE_PTRMEMFUNC_P (t2))
1254 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1256 /* Different classes of types can't be compatible. */
1257 if (TREE_CODE (t1) != TREE_CODE (t2))
1258 return false;
1260 /* Qualifiers must match. For array types, we will check when we
1261 recur on the array element types. */
1262 if (TREE_CODE (t1) != ARRAY_TYPE
1263 && cp_type_quals (t1) != cp_type_quals (t2))
1264 return false;
1265 if (TREE_CODE (t1) == FUNCTION_TYPE
1266 && type_memfn_quals (t1) != type_memfn_quals (t2))
1267 return false;
1268 /* Need to check this before TYPE_MAIN_VARIANT.
1269 FIXME function qualifiers should really change the main variant. */
1270 if (TREE_CODE (t1) == FUNCTION_TYPE
1271 || TREE_CODE (t1) == METHOD_TYPE)
1273 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1274 return false;
1275 if (flag_noexcept_type
1276 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1277 TYPE_RAISES_EXCEPTIONS (t2),
1278 ce_type))
1279 return false;
1282 /* Allow for two different type nodes which have essentially the same
1283 definition. Note that we already checked for equality of the type
1284 qualifiers (just above). */
1286 if (TREE_CODE (t1) != ARRAY_TYPE
1287 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1288 return true;
1291 /* Compare the types. Break out if they could be the same. */
1292 switch (TREE_CODE (t1))
1294 case VOID_TYPE:
1295 case BOOLEAN_TYPE:
1296 /* All void and bool types are the same. */
1297 break;
1299 case INTEGER_TYPE:
1300 case FIXED_POINT_TYPE:
1301 case REAL_TYPE:
1302 /* With these nodes, we can't determine type equivalence by
1303 looking at what is stored in the nodes themselves, because
1304 two nodes might have different TYPE_MAIN_VARIANTs but still
1305 represent the same type. For example, wchar_t and int could
1306 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1307 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1308 and are distinct types. On the other hand, int and the
1309 following typedef
1311 typedef int INT __attribute((may_alias));
1313 have identical properties, different TYPE_MAIN_VARIANTs, but
1314 represent the same type. The canonical type system keeps
1315 track of equivalence in this case, so we fall back on it. */
1316 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1318 case TEMPLATE_TEMPLATE_PARM:
1319 case BOUND_TEMPLATE_TEMPLATE_PARM:
1320 if (!comp_template_parms_position (t1, t2))
1321 return false;
1322 if (!comp_template_parms
1323 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1324 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1325 return false;
1326 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1327 break;
1328 /* Don't check inheritance. */
1329 strict = COMPARE_STRICT;
1330 /* Fall through. */
1332 case RECORD_TYPE:
1333 case UNION_TYPE:
1334 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1335 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1336 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1337 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1338 break;
1340 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1341 break;
1342 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1343 break;
1345 return false;
1347 case OFFSET_TYPE:
1348 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1349 strict & ~COMPARE_REDECLARATION))
1350 return false;
1351 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1352 return false;
1353 break;
1355 case REFERENCE_TYPE:
1356 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1357 return false;
1358 /* fall through to checks for pointer types */
1359 gcc_fallthrough ();
1361 case POINTER_TYPE:
1362 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1363 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1364 return false;
1365 break;
1367 case METHOD_TYPE:
1368 case FUNCTION_TYPE:
1369 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1370 return false;
1371 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1372 return false;
1373 break;
1375 case ARRAY_TYPE:
1376 /* Target types must match incl. qualifiers. */
1377 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1378 return false;
1379 break;
1381 case TEMPLATE_TYPE_PARM:
1382 /* If T1 and T2 don't have the same relative position in their
1383 template parameters set, they can't be equal. */
1384 if (!comp_template_parms_position (t1, t2))
1385 return false;
1386 /* Constrained 'auto's are distinct from parms that don't have the same
1387 constraints. */
1388 if (!equivalent_placeholder_constraints (t1, t2))
1389 return false;
1390 break;
1392 case TYPENAME_TYPE:
1393 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1394 TYPENAME_TYPE_FULLNAME (t2)))
1395 return false;
1396 /* Qualifiers don't matter on scopes. */
1397 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1398 TYPE_CONTEXT (t2)))
1399 return false;
1400 break;
1402 case UNBOUND_CLASS_TEMPLATE:
1403 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1404 return false;
1405 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1406 return false;
1407 break;
1409 case COMPLEX_TYPE:
1410 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1411 return false;
1412 break;
1414 case VECTOR_TYPE:
1415 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1416 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1417 return false;
1418 break;
1420 case TYPE_PACK_EXPANSION:
1421 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1422 PACK_EXPANSION_PATTERN (t2))
1423 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1424 PACK_EXPANSION_EXTRA_ARGS (t2)));
1426 case DECLTYPE_TYPE:
1427 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1428 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1429 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1430 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1431 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1432 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1433 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1434 DECLTYPE_TYPE_EXPR (t2)))
1435 return false;
1436 break;
1438 case UNDERLYING_TYPE:
1439 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1440 UNDERLYING_TYPE_TYPE (t2));
1442 default:
1443 return false;
1446 /* If we get here, we know that from a target independent POV the
1447 types are the same. Make sure the target attributes are also
1448 the same. */
1449 return comp_type_attributes (t1, t2);
1452 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1453 is a bitwise-or of the COMPARE_* flags. */
1455 bool
1456 comptypes (tree t1, tree t2, int strict)
1458 if (strict == COMPARE_STRICT)
1460 if (t1 == t2)
1461 return true;
1463 if (t1 == error_mark_node || t2 == error_mark_node)
1464 return false;
1466 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1467 /* At least one of the types requires structural equality, so
1468 perform a deep check. */
1469 return structural_comptypes (t1, t2, strict);
1471 if (flag_checking && USE_CANONICAL_TYPES)
1473 bool result = structural_comptypes (t1, t2, strict);
1475 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1476 /* The two types are structurally equivalent, but their
1477 canonical types were different. This is a failure of the
1478 canonical type propagation code.*/
1479 internal_error
1480 ("canonical types differ for identical types %qT and %qT",
1481 t1, t2);
1482 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1483 /* Two types are structurally different, but the canonical
1484 types are the same. This means we were over-eager in
1485 assigning canonical types. */
1486 internal_error
1487 ("same canonical type node for different types %qT and %qT",
1488 t1, t2);
1490 return result;
1492 if (!flag_checking && USE_CANONICAL_TYPES)
1493 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1494 else
1495 return structural_comptypes (t1, t2, strict);
1497 else if (strict == COMPARE_STRUCTURAL)
1498 return structural_comptypes (t1, t2, COMPARE_STRICT);
1499 else
1500 return structural_comptypes (t1, t2, strict);
1503 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1504 top-level qualifiers. */
1506 bool
1507 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1509 if (type1 == error_mark_node || type2 == error_mark_node)
1510 return false;
1512 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1513 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1514 return same_type_p (type1, type2);
1517 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1519 bool
1520 at_least_as_qualified_p (const_tree type1, const_tree type2)
1522 int q1 = cp_type_quals (type1);
1523 int q2 = cp_type_quals (type2);
1525 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1526 return (q1 & q2) == q2;
1529 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1530 more cv-qualified that TYPE1, and 0 otherwise. */
1533 comp_cv_qualification (int q1, int q2)
1535 if (q1 == q2)
1536 return 0;
1538 if ((q1 & q2) == q2)
1539 return 1;
1540 else if ((q1 & q2) == q1)
1541 return -1;
1543 return 0;
1547 comp_cv_qualification (const_tree type1, const_tree type2)
1549 int q1 = cp_type_quals (type1);
1550 int q2 = cp_type_quals (type2);
1551 return comp_cv_qualification (q1, q2);
1554 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1555 subset of the cv-qualification signature of TYPE2, and the types
1556 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1559 comp_cv_qual_signature (tree type1, tree type2)
1561 if (comp_ptr_ttypes_real (type2, type1, -1))
1562 return 1;
1563 else if (comp_ptr_ttypes_real (type1, type2, -1))
1564 return -1;
1565 else
1566 return 0;
1569 /* Subroutines of `comptypes'. */
1571 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1572 equivalent in the sense that functions with those parameter types
1573 can have equivalent types. The two lists must be equivalent,
1574 element by element. */
1576 bool
1577 compparms (const_tree parms1, const_tree parms2)
1579 const_tree t1, t2;
1581 /* An unspecified parmlist matches any specified parmlist
1582 whose argument types don't need default promotions. */
1584 for (t1 = parms1, t2 = parms2;
1585 t1 || t2;
1586 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1588 /* If one parmlist is shorter than the other,
1589 they fail to match. */
1590 if (!t1 || !t2)
1591 return false;
1592 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1593 return false;
1595 return true;
1599 /* Process a sizeof or alignof expression where the operand is a
1600 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1601 or GNU (preferred alignment) semantics; it is ignored if op is
1602 SIZEOF_EXPR. */
1604 tree
1605 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool std_alignof,
1606 bool complain)
1608 tree value;
1609 bool dependent_p;
1611 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1612 if (type == error_mark_node)
1613 return error_mark_node;
1615 type = non_reference (type);
1616 if (TREE_CODE (type) == METHOD_TYPE)
1618 if (complain)
1619 pedwarn (input_location, OPT_Wpointer_arith,
1620 "invalid application of %qs to a member function",
1621 OVL_OP_INFO (false, op)->name);
1622 else
1623 return error_mark_node;
1624 value = size_one_node;
1627 dependent_p = dependent_type_p (type);
1628 if (!dependent_p)
1629 complete_type (type);
1630 if (dependent_p
1631 /* VLA types will have a non-constant size. In the body of an
1632 uninstantiated template, we don't need to try to compute the
1633 value, because the sizeof expression is not an integral
1634 constant expression in that case. And, if we do try to
1635 compute the value, we'll likely end up with SAVE_EXPRs, which
1636 the template substitution machinery does not expect to see. */
1637 || (processing_template_decl
1638 && COMPLETE_TYPE_P (type)
1639 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1641 value = build_min (op, size_type_node, type);
1642 TREE_READONLY (value) = 1;
1643 if (op == ALIGNOF_EXPR && std_alignof)
1644 ALIGNOF_EXPR_STD_P (value) = true;
1645 return value;
1648 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1649 op == SIZEOF_EXPR, std_alignof,
1650 complain);
1653 /* Return the size of the type, without producing any warnings for
1654 types whose size cannot be taken. This routine should be used only
1655 in some other routine that has already produced a diagnostic about
1656 using the size of such a type. */
1657 tree
1658 cxx_sizeof_nowarn (tree type)
1660 if (TREE_CODE (type) == FUNCTION_TYPE
1661 || VOID_TYPE_P (type)
1662 || TREE_CODE (type) == ERROR_MARK)
1663 return size_one_node;
1664 else if (!COMPLETE_TYPE_P (type))
1665 return size_zero_node;
1666 else
1667 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false, false);
1670 /* Process a sizeof expression where the operand is an expression. */
1672 static tree
1673 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1675 if (e == error_mark_node)
1676 return error_mark_node;
1678 if (processing_template_decl)
1680 e = build_min (SIZEOF_EXPR, size_type_node, e);
1681 TREE_SIDE_EFFECTS (e) = 0;
1682 TREE_READONLY (e) = 1;
1684 return e;
1687 /* To get the size of a static data member declared as an array of
1688 unknown bound, we need to instantiate it. */
1689 if (VAR_P (e)
1690 && VAR_HAD_UNKNOWN_BOUND (e)
1691 && DECL_TEMPLATE_INSTANTIATION (e))
1692 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1694 if (TREE_CODE (e) == PARM_DECL
1695 && DECL_ARRAY_PARAMETER_P (e)
1696 && (complain & tf_warning))
1698 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1699 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1700 inform (DECL_SOURCE_LOCATION (e), "declared here");
1703 e = mark_type_use (e);
1705 if (bitfield_p (e))
1707 if (complain & tf_error)
1708 error ("invalid application of %<sizeof%> to a bit-field");
1709 else
1710 return error_mark_node;
1711 e = char_type_node;
1713 else if (is_overloaded_fn (e))
1715 if (complain & tf_error)
1716 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1717 "function type");
1718 else
1719 return error_mark_node;
1720 e = char_type_node;
1722 else if (type_unknown_p (e))
1724 if (complain & tf_error)
1725 cxx_incomplete_type_error (e, TREE_TYPE (e));
1726 else
1727 return error_mark_node;
1728 e = char_type_node;
1730 else
1731 e = TREE_TYPE (e);
1733 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, false, complain & tf_error);
1736 /* Implement the __alignof keyword: Return the minimum required
1737 alignment of E, measured in bytes. For VAR_DECL's and
1738 FIELD_DECL's return DECL_ALIGN (which can be set from an
1739 "aligned" __attribute__ specification). */
1741 static tree
1742 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1744 tree t;
1746 if (e == error_mark_node)
1747 return error_mark_node;
1749 if (processing_template_decl)
1751 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1752 TREE_SIDE_EFFECTS (e) = 0;
1753 TREE_READONLY (e) = 1;
1755 return e;
1758 e = mark_type_use (e);
1760 if (VAR_P (e))
1761 t = size_int (DECL_ALIGN_UNIT (e));
1762 else if (bitfield_p (e))
1764 if (complain & tf_error)
1765 error ("invalid application of %<__alignof%> to a bit-field");
1766 else
1767 return error_mark_node;
1768 t = size_one_node;
1770 else if (TREE_CODE (e) == COMPONENT_REF
1771 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1772 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1773 else if (is_overloaded_fn (e))
1775 if (complain & tf_error)
1776 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1777 "function type");
1778 else
1779 return error_mark_node;
1780 if (TREE_CODE (e) == FUNCTION_DECL)
1781 t = size_int (DECL_ALIGN_UNIT (e));
1782 else
1783 t = size_one_node;
1785 else if (type_unknown_p (e))
1787 if (complain & tf_error)
1788 cxx_incomplete_type_error (e, TREE_TYPE (e));
1789 else
1790 return error_mark_node;
1791 t = size_one_node;
1793 else
1794 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, false,
1795 complain & tf_error);
1797 return fold_convert (size_type_node, t);
1800 /* Process a sizeof or alignof expression E with code OP where the operand
1801 is an expression. */
1803 tree
1804 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1806 if (op == SIZEOF_EXPR)
1807 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1808 else
1809 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1812 /* Build a representation of an expression 'alignas(E).' Return the
1813 folded integer value of E if it is an integral constant expression
1814 that resolves to a valid alignment. If E depends on a template
1815 parameter, return a syntactic representation tree of kind
1816 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1817 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1819 tree
1820 cxx_alignas_expr (tree e)
1822 if (e == NULL_TREE || e == error_mark_node
1823 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1824 return e;
1826 if (TYPE_P (e))
1827 /* [dcl.align]/3:
1829 When the alignment-specifier is of the form
1830 alignas(type-id ), it shall have the same effect as
1831 alignas(alignof(type-id )). */
1833 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, true, false);
1835 /* If we reach this point, it means the alignas expression if of
1836 the form "alignas(assignment-expression)", so we should follow
1837 what is stated by [dcl.align]/2. */
1839 if (value_dependent_expression_p (e))
1840 /* Leave value-dependent expression alone for now. */
1841 return e;
1843 e = instantiate_non_dependent_expr (e);
1844 e = mark_rvalue_use (e);
1846 /* [dcl.align]/2 says:
1848 the assignment-expression shall be an integral constant
1849 expression. */
1851 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1853 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1854 return error_mark_node;
1857 return cxx_constant_value (e);
1861 /* EXPR is being used in a context that is not a function call.
1862 Enforce:
1864 [expr.ref]
1866 The expression can be used only as the left-hand operand of a
1867 member function call.
1869 [expr.mptr.operator]
1871 If the result of .* or ->* is a function, then that result can be
1872 used only as the operand for the function call operator ().
1874 by issuing an error message if appropriate. Returns true iff EXPR
1875 violates these rules. */
1877 bool
1878 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1880 if (expr == NULL_TREE)
1881 return false;
1882 /* Don't enforce this in MS mode. */
1883 if (flag_ms_extensions)
1884 return false;
1885 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1886 expr = get_first_fn (expr);
1887 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1889 if (complain & tf_error)
1891 if (DECL_P (expr))
1893 error_at (loc, "invalid use of non-static member function %qD",
1894 expr);
1895 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1897 else
1898 error_at (loc, "invalid use of non-static member function of "
1899 "type %qT", TREE_TYPE (expr));
1901 return true;
1903 return false;
1906 /* If EXP is a reference to a bitfield, and the type of EXP does not
1907 match the declared type of the bitfield, return the declared type
1908 of the bitfield. Otherwise, return NULL_TREE. */
1910 tree
1911 is_bitfield_expr_with_lowered_type (const_tree exp)
1913 switch (TREE_CODE (exp))
1915 case COND_EXPR:
1916 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1917 ? TREE_OPERAND (exp, 1)
1918 : TREE_OPERAND (exp, 0)))
1919 return NULL_TREE;
1920 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1922 case COMPOUND_EXPR:
1923 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1925 case MODIFY_EXPR:
1926 case SAVE_EXPR:
1927 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1929 case COMPONENT_REF:
1931 tree field;
1933 field = TREE_OPERAND (exp, 1);
1934 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1935 return NULL_TREE;
1936 if (same_type_ignoring_top_level_qualifiers_p
1937 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1938 return NULL_TREE;
1939 return DECL_BIT_FIELD_TYPE (field);
1942 case VAR_DECL:
1943 if (DECL_HAS_VALUE_EXPR_P (exp))
1944 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1945 (CONST_CAST_TREE (exp)));
1946 return NULL_TREE;
1948 CASE_CONVERT:
1949 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1950 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1951 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1952 /* Fallthrough. */
1954 default:
1955 return NULL_TREE;
1959 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1960 bitfield with a lowered type, the type of EXP is returned, rather
1961 than NULL_TREE. */
1963 tree
1964 unlowered_expr_type (const_tree exp)
1966 tree type;
1967 tree etype = TREE_TYPE (exp);
1969 type = is_bitfield_expr_with_lowered_type (exp);
1970 if (type)
1971 type = cp_build_qualified_type (type, cp_type_quals (etype));
1972 else
1973 type = etype;
1975 return type;
1978 /* Perform the conversions in [expr] that apply when an lvalue appears
1979 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1980 function-to-pointer conversions. In addition, bitfield references are
1981 converted to their declared types. Note that this function does not perform
1982 the lvalue-to-rvalue conversion for class types. If you need that conversion
1983 for class types, then you probably need to use force_rvalue.
1985 Although the returned value is being used as an rvalue, this
1986 function does not wrap the returned expression in a
1987 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1988 that the return value is no longer an lvalue. */
1990 tree
1991 decay_conversion (tree exp,
1992 tsubst_flags_t complain,
1993 bool reject_builtin /* = true */)
1995 tree type;
1996 enum tree_code code;
1997 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1999 type = TREE_TYPE (exp);
2000 if (type == error_mark_node)
2001 return error_mark_node;
2003 exp = resolve_nondeduced_context (exp, complain);
2004 if (type_unknown_p (exp))
2006 if (complain & tf_error)
2007 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
2008 return error_mark_node;
2011 code = TREE_CODE (type);
2013 if (error_operand_p (exp))
2014 return error_mark_node;
2016 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2018 mark_rvalue_use (exp, loc, reject_builtin);
2019 return nullptr_node;
2022 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2023 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2024 if (code == VOID_TYPE)
2026 if (complain & tf_error)
2027 error_at (loc, "void value not ignored as it ought to be");
2028 return error_mark_node;
2030 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2031 return error_mark_node;
2032 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2034 exp = mark_lvalue_use (exp);
2035 if (reject_builtin && reject_gcc_builtin (exp, loc))
2036 return error_mark_node;
2037 return cp_build_addr_expr (exp, complain);
2039 if (code == ARRAY_TYPE)
2041 tree adr;
2042 tree ptrtype;
2044 exp = mark_lvalue_use (exp);
2046 if (INDIRECT_REF_P (exp))
2047 return build_nop (build_pointer_type (TREE_TYPE (type)),
2048 TREE_OPERAND (exp, 0));
2050 if (TREE_CODE (exp) == COMPOUND_EXPR)
2052 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2053 if (op1 == error_mark_node)
2054 return error_mark_node;
2055 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2056 TREE_OPERAND (exp, 0), op1);
2059 if (!obvalue_p (exp)
2060 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2062 if (complain & tf_error)
2063 error_at (loc, "invalid use of non-lvalue array");
2064 return error_mark_node;
2067 /* Don't let an array compound literal decay to a pointer. It can
2068 still be used to initialize an array or bind to a reference. */
2069 if (TREE_CODE (exp) == TARGET_EXPR)
2071 if (complain & tf_error)
2072 error_at (loc, "taking address of temporary array");
2073 return error_mark_node;
2076 ptrtype = build_pointer_type (TREE_TYPE (type));
2078 if (VAR_P (exp))
2080 if (!cxx_mark_addressable (exp))
2081 return error_mark_node;
2082 adr = build_nop (ptrtype, build_address (exp));
2083 return adr;
2085 /* This way is better for a COMPONENT_REF since it can
2086 simplify the offset for a component. */
2087 adr = cp_build_addr_expr (exp, complain);
2088 return cp_convert (ptrtype, adr, complain);
2091 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2092 exp = mark_rvalue_use (exp, loc, reject_builtin);
2094 /* If a bitfield is used in a context where integral promotion
2095 applies, then the caller is expected to have used
2096 default_conversion. That function promotes bitfields correctly
2097 before calling this function. At this point, if we have a
2098 bitfield referenced, we may assume that is not subject to
2099 promotion, and that, therefore, the type of the resulting rvalue
2100 is the declared type of the bitfield. */
2101 exp = convert_bitfield_to_declared_type (exp);
2103 /* We do not call rvalue() here because we do not want to wrap EXP
2104 in a NON_LVALUE_EXPR. */
2106 /* [basic.lval]
2108 Non-class rvalues always have cv-unqualified types. */
2109 type = TREE_TYPE (exp);
2110 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2111 exp = build_nop (cv_unqualified (type), exp);
2113 if (!complete_type_or_maybe_complain (type, exp, complain))
2114 return error_mark_node;
2116 return exp;
2119 /* Perform preparatory conversions, as part of the "usual arithmetic
2120 conversions". In particular, as per [expr]:
2122 Whenever an lvalue expression appears as an operand of an
2123 operator that expects the rvalue for that operand, the
2124 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2125 standard conversions are applied to convert the expression to an
2126 rvalue.
2128 In addition, we perform integral promotions here, as those are
2129 applied to both operands to a binary operator before determining
2130 what additional conversions should apply. */
2132 static tree
2133 cp_default_conversion (tree exp, tsubst_flags_t complain)
2135 /* Check for target-specific promotions. */
2136 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2137 if (promoted_type)
2138 exp = cp_convert (promoted_type, exp, complain);
2139 /* Perform the integral promotions first so that bitfield
2140 expressions (which may promote to "int", even if the bitfield is
2141 declared "unsigned") are promoted correctly. */
2142 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2143 exp = cp_perform_integral_promotions (exp, complain);
2144 /* Perform the other conversions. */
2145 exp = decay_conversion (exp, complain);
2147 return exp;
2150 /* C version. */
2152 tree
2153 default_conversion (tree exp)
2155 return cp_default_conversion (exp, tf_warning_or_error);
2158 /* EXPR is an expression with an integral or enumeration type.
2159 Perform the integral promotions in [conv.prom], and return the
2160 converted value. */
2162 tree
2163 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2165 tree type;
2166 tree promoted_type;
2168 expr = mark_rvalue_use (expr);
2169 if (error_operand_p (expr))
2170 return error_mark_node;
2172 /* [conv.prom]
2174 If the bitfield has an enumerated type, it is treated as any
2175 other value of that type for promotion purposes. */
2176 type = is_bitfield_expr_with_lowered_type (expr);
2177 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2178 type = TREE_TYPE (expr);
2179 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2180 /* Scoped enums don't promote. */
2181 if (SCOPED_ENUM_P (type))
2182 return expr;
2183 promoted_type = type_promotes_to (type);
2184 if (type != promoted_type)
2185 expr = cp_convert (promoted_type, expr, complain);
2186 return expr;
2189 /* C version. */
2191 tree
2192 perform_integral_promotions (tree expr)
2194 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2197 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2198 decay_conversion to one. */
2201 string_conv_p (const_tree totype, const_tree exp, int warn)
2203 tree t;
2205 if (!TYPE_PTR_P (totype))
2206 return 0;
2208 t = TREE_TYPE (totype);
2209 if (!same_type_p (t, char_type_node)
2210 && !same_type_p (t, char16_type_node)
2211 && !same_type_p (t, char32_type_node)
2212 && !same_type_p (t, wchar_type_node))
2213 return 0;
2215 STRIP_ANY_LOCATION_WRAPPER (exp);
2217 if (TREE_CODE (exp) == STRING_CST)
2219 /* Make sure that we don't try to convert between char and wide chars. */
2220 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2221 return 0;
2223 else
2225 /* Is this a string constant which has decayed to 'const char *'? */
2226 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2227 if (!same_type_p (TREE_TYPE (exp), t))
2228 return 0;
2229 STRIP_NOPS (exp);
2230 if (TREE_CODE (exp) != ADDR_EXPR
2231 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2232 return 0;
2234 if (warn)
2236 if (cxx_dialect >= cxx11)
2237 pedwarn (input_location, OPT_Wwrite_strings,
2238 "ISO C++ forbids converting a string constant to %qT",
2239 totype);
2240 else
2241 warning (OPT_Wwrite_strings,
2242 "deprecated conversion from string constant to %qT",
2243 totype);
2246 return 1;
2249 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2250 can, for example, use as an lvalue. This code used to be in
2251 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2252 expressions, where we're dealing with aggregates. But now it's again only
2253 called from unary_complex_lvalue. The case (in particular) that led to
2254 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2255 get it there. */
2257 static tree
2258 rationalize_conditional_expr (enum tree_code code, tree t,
2259 tsubst_flags_t complain)
2261 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2263 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2264 the first operand is always the one to be used if both operands
2265 are equal, so we know what conditional expression this used to be. */
2266 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2268 tree op0 = TREE_OPERAND (t, 0);
2269 tree op1 = TREE_OPERAND (t, 1);
2271 /* The following code is incorrect if either operand side-effects. */
2272 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2273 && !TREE_SIDE_EFFECTS (op1));
2274 return
2275 build_conditional_expr (loc,
2276 build_x_binary_op (loc,
2277 (TREE_CODE (t) == MIN_EXPR
2278 ? LE_EXPR : GE_EXPR),
2279 op0, TREE_CODE (op0),
2280 op1, TREE_CODE (op1),
2281 /*overload=*/NULL,
2282 complain),
2283 cp_build_unary_op (code, op0, false, complain),
2284 cp_build_unary_op (code, op1, false, complain),
2285 complain);
2288 return
2289 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2290 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2291 complain),
2292 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2293 complain),
2294 complain);
2297 /* Given the TYPE of an anonymous union field inside T, return the
2298 FIELD_DECL for the field. If not found return NULL_TREE. Because
2299 anonymous unions can nest, we must also search all anonymous unions
2300 that are directly reachable. */
2302 tree
2303 lookup_anon_field (tree t, tree type)
2305 tree field;
2307 t = TYPE_MAIN_VARIANT (t);
2309 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2311 if (TREE_STATIC (field))
2312 continue;
2313 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2314 continue;
2316 /* If we find it directly, return the field. */
2317 if (DECL_NAME (field) == NULL_TREE
2318 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2320 return field;
2323 /* Otherwise, it could be nested, search harder. */
2324 if (DECL_NAME (field) == NULL_TREE
2325 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2327 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2328 if (subfield)
2329 return subfield;
2332 return NULL_TREE;
2335 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2336 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2337 non-NULL, it indicates the path to the base used to name MEMBER.
2338 If PRESERVE_REFERENCE is true, the expression returned will have
2339 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2340 returned will have the type referred to by the reference.
2342 This function does not perform access control; that is either done
2343 earlier by the parser when the name of MEMBER is resolved to MEMBER
2344 itself, or later when overload resolution selects one of the
2345 functions indicated by MEMBER. */
2347 tree
2348 build_class_member_access_expr (cp_expr object, tree member,
2349 tree access_path, bool preserve_reference,
2350 tsubst_flags_t complain)
2352 tree object_type;
2353 tree member_scope;
2354 tree result = NULL_TREE;
2355 tree using_decl = NULL_TREE;
2357 if (error_operand_p (object) || error_operand_p (member))
2358 return error_mark_node;
2360 gcc_assert (DECL_P (member) || BASELINK_P (member));
2362 /* [expr.ref]
2364 The type of the first expression shall be "class object" (of a
2365 complete type). */
2366 object_type = TREE_TYPE (object);
2367 if (!currently_open_class (object_type)
2368 && !complete_type_or_maybe_complain (object_type, object, complain))
2369 return error_mark_node;
2370 if (!CLASS_TYPE_P (object_type))
2372 if (complain & tf_error)
2374 if (POINTER_TYPE_P (object_type)
2375 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2376 error ("request for member %qD in %qE, which is of pointer "
2377 "type %qT (maybe you meant to use %<->%> ?)",
2378 member, object.get_value (), object_type);
2379 else
2380 error ("request for member %qD in %qE, which is of non-class "
2381 "type %qT", member, object.get_value (), object_type);
2383 return error_mark_node;
2386 /* The standard does not seem to actually say that MEMBER must be a
2387 member of OBJECT_TYPE. However, that is clearly what is
2388 intended. */
2389 if (DECL_P (member))
2391 member_scope = DECL_CLASS_CONTEXT (member);
2392 if (!mark_used (member, complain) && !(complain & tf_error))
2393 return error_mark_node;
2394 if (TREE_DEPRECATED (member))
2395 warn_deprecated_use (member, NULL_TREE);
2397 else
2398 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2399 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2400 presently be the anonymous union. Go outwards until we find a
2401 type related to OBJECT_TYPE. */
2402 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2403 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2404 object_type))
2405 member_scope = TYPE_CONTEXT (member_scope);
2406 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2408 if (complain & tf_error)
2410 if (TREE_CODE (member) == FIELD_DECL)
2411 error ("invalid use of nonstatic data member %qE", member);
2412 else
2413 error ("%qD is not a member of %qT", member, object_type);
2415 return error_mark_node;
2418 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2419 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2420 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2422 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2423 if (temp)
2424 object = cp_build_fold_indirect_ref (temp);
2427 /* In [expr.ref], there is an explicit list of the valid choices for
2428 MEMBER. We check for each of those cases here. */
2429 if (VAR_P (member))
2431 /* A static data member. */
2432 result = member;
2433 mark_exp_read (object);
2434 /* If OBJECT has side-effects, they are supposed to occur. */
2435 if (TREE_SIDE_EFFECTS (object))
2436 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2438 else if (TREE_CODE (member) == FIELD_DECL)
2440 /* A non-static data member. */
2441 bool null_object_p;
2442 int type_quals;
2443 tree member_type;
2445 if (INDIRECT_REF_P (object))
2446 null_object_p =
2447 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2448 else
2449 null_object_p = false;
2451 /* Convert OBJECT to the type of MEMBER. */
2452 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2453 TYPE_MAIN_VARIANT (member_scope)))
2455 tree binfo;
2456 base_kind kind;
2458 binfo = lookup_base (access_path ? access_path : object_type,
2459 member_scope, ba_unique, &kind, complain);
2460 if (binfo == error_mark_node)
2461 return error_mark_node;
2463 /* It is invalid to try to get to a virtual base of a
2464 NULL object. The most common cause is invalid use of
2465 offsetof macro. */
2466 if (null_object_p && kind == bk_via_virtual)
2468 if (complain & tf_error)
2470 error ("invalid access to non-static data member %qD in "
2471 "virtual base of NULL object", member);
2473 return error_mark_node;
2476 /* Convert to the base. */
2477 object = build_base_path (PLUS_EXPR, object, binfo,
2478 /*nonnull=*/1, complain);
2479 /* If we found the base successfully then we should be able
2480 to convert to it successfully. */
2481 gcc_assert (object != error_mark_node);
2484 /* If MEMBER is from an anonymous aggregate, we have converted
2485 OBJECT so that it refers to the class containing the
2486 anonymous union. Generate a reference to the anonymous union
2487 itself, and recur to find MEMBER. */
2488 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2489 /* When this code is called from build_field_call, the
2490 object already has the type of the anonymous union.
2491 That is because the COMPONENT_REF was already
2492 constructed, and was then disassembled before calling
2493 build_field_call. After the function-call code is
2494 cleaned up, this waste can be eliminated. */
2495 && (!same_type_ignoring_top_level_qualifiers_p
2496 (TREE_TYPE (object), DECL_CONTEXT (member))))
2498 tree anonymous_union;
2500 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2501 DECL_CONTEXT (member));
2502 object = build_class_member_access_expr (object,
2503 anonymous_union,
2504 /*access_path=*/NULL_TREE,
2505 preserve_reference,
2506 complain);
2509 /* Compute the type of the field, as described in [expr.ref]. */
2510 type_quals = TYPE_UNQUALIFIED;
2511 member_type = TREE_TYPE (member);
2512 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2514 type_quals = (cp_type_quals (member_type)
2515 | cp_type_quals (object_type));
2517 /* A field is const (volatile) if the enclosing object, or the
2518 field itself, is const (volatile). But, a mutable field is
2519 not const, even within a const object. */
2520 if (DECL_MUTABLE_P (member))
2521 type_quals &= ~TYPE_QUAL_CONST;
2522 member_type = cp_build_qualified_type (member_type, type_quals);
2525 result = build3_loc (input_location, COMPONENT_REF, member_type,
2526 object, member, NULL_TREE);
2528 /* Mark the expression const or volatile, as appropriate. Even
2529 though we've dealt with the type above, we still have to mark the
2530 expression itself. */
2531 if (type_quals & TYPE_QUAL_CONST)
2532 TREE_READONLY (result) = 1;
2533 if (type_quals & TYPE_QUAL_VOLATILE)
2534 TREE_THIS_VOLATILE (result) = 1;
2536 else if (BASELINK_P (member))
2538 /* The member is a (possibly overloaded) member function. */
2539 tree functions;
2540 tree type;
2542 /* If the MEMBER is exactly one static member function, then we
2543 know the type of the expression. Otherwise, we must wait
2544 until overload resolution has been performed. */
2545 functions = BASELINK_FUNCTIONS (member);
2546 if (TREE_CODE (functions) == FUNCTION_DECL
2547 && DECL_STATIC_FUNCTION_P (functions))
2548 type = TREE_TYPE (functions);
2549 else
2550 type = unknown_type_node;
2551 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2552 base. That will happen when the function is called. */
2553 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2555 else if (TREE_CODE (member) == CONST_DECL)
2557 /* The member is an enumerator. */
2558 result = member;
2559 /* If OBJECT has side-effects, they are supposed to occur. */
2560 if (TREE_SIDE_EFFECTS (object))
2561 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2562 object, result);
2564 else if ((using_decl = strip_using_decl (member)) != member)
2565 result = build_class_member_access_expr (object,
2566 using_decl,
2567 access_path, preserve_reference,
2568 complain);
2569 else
2571 if (complain & tf_error)
2572 error ("invalid use of %qD", member);
2573 return error_mark_node;
2576 if (!preserve_reference)
2577 /* [expr.ref]
2579 If E2 is declared to have type "reference to T", then ... the
2580 type of E1.E2 is T. */
2581 result = convert_from_reference (result);
2583 return result;
2586 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2587 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2589 static tree
2590 lookup_destructor (tree object, tree scope, tree dtor_name,
2591 tsubst_flags_t complain)
2593 tree object_type = TREE_TYPE (object);
2594 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2595 tree expr;
2597 /* We've already complained about this destructor. */
2598 if (dtor_type == error_mark_node)
2599 return error_mark_node;
2601 if (scope && !check_dtor_name (scope, dtor_type))
2603 if (complain & tf_error)
2604 error ("qualified type %qT does not match destructor name ~%qT",
2605 scope, dtor_type);
2606 return error_mark_node;
2608 if (is_auto (dtor_type))
2609 dtor_type = object_type;
2610 else if (identifier_p (dtor_type))
2612 /* In a template, names we can't find a match for are still accepted
2613 destructor names, and we check them here. */
2614 if (check_dtor_name (object_type, dtor_type))
2615 dtor_type = object_type;
2616 else
2618 if (complain & tf_error)
2619 error ("object type %qT does not match destructor name ~%qT",
2620 object_type, dtor_type);
2621 return error_mark_node;
2625 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2627 if (complain & tf_error)
2628 error ("the type being destroyed is %qT, but the destructor "
2629 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2630 return error_mark_node;
2632 expr = lookup_member (dtor_type, complete_dtor_identifier,
2633 /*protect=*/1, /*want_type=*/false,
2634 tf_warning_or_error);
2635 if (!expr)
2637 if (complain & tf_error)
2638 cxx_incomplete_type_error (dtor_name, dtor_type);
2639 return error_mark_node;
2641 expr = (adjust_result_of_qualified_name_lookup
2642 (expr, dtor_type, object_type));
2643 if (scope == NULL_TREE)
2644 /* We need to call adjust_result_of_qualified_name_lookup in case the
2645 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2646 that we still get virtual function binding. */
2647 BASELINK_QUALIFIED_P (expr) = false;
2648 return expr;
2651 /* An expression of the form "A::template B" has been resolved to
2652 DECL. Issue a diagnostic if B is not a template or template
2653 specialization. */
2655 void
2656 check_template_keyword (tree decl)
2658 /* The standard says:
2660 [temp.names]
2662 If a name prefixed by the keyword template is not a member
2663 template, the program is ill-formed.
2665 DR 228 removed the restriction that the template be a member
2666 template.
2668 DR 96, if accepted would add the further restriction that explicit
2669 template arguments must be provided if the template keyword is
2670 used, but, as of 2005-10-16, that DR is still in "drafting". If
2671 this DR is accepted, then the semantic checks here can be
2672 simplified, as the entity named must in fact be a template
2673 specialization, rather than, as at present, a set of overloaded
2674 functions containing at least one template function. */
2675 if (TREE_CODE (decl) != TEMPLATE_DECL
2676 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2678 if (VAR_P (decl))
2680 if (DECL_USE_TEMPLATE (decl)
2681 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2683 else
2684 permerror (input_location, "%qD is not a template", decl);
2686 else if (!is_overloaded_fn (decl))
2687 permerror (input_location, "%qD is not a template", decl);
2688 else
2690 bool found = false;
2692 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2693 !found && iter; ++iter)
2695 tree fn = *iter;
2696 if (TREE_CODE (fn) == TEMPLATE_DECL
2697 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2698 || (TREE_CODE (fn) == FUNCTION_DECL
2699 && DECL_USE_TEMPLATE (fn)
2700 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2701 found = true;
2703 if (!found)
2704 permerror (input_location, "%qD is not a template", decl);
2709 /* Record that an access failure occurred on BASETYPE_PATH attempting
2710 to access FIELD_DECL. */
2712 void
2713 access_failure_info::record_access_failure (tree basetype_path,
2714 tree field_decl)
2716 m_was_inaccessible = true;
2717 m_basetype_path = basetype_path;
2718 m_field_decl = field_decl;
2721 /* If an access failure was recorded, then attempt to locate an
2722 accessor function for the pertinent field, and if one is
2723 available, add a note and fix-it hint suggesting using it. */
2725 void
2726 access_failure_info::maybe_suggest_accessor (bool const_p) const
2728 if (!m_was_inaccessible)
2729 return;
2731 tree accessor
2732 = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2733 if (!accessor)
2734 return;
2736 /* The accessor must itself be accessible for it to be a reasonable
2737 suggestion. */
2738 if (!accessible_p (m_basetype_path, accessor, true))
2739 return;
2741 rich_location richloc (line_table, input_location);
2742 pretty_printer pp;
2743 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2744 richloc.add_fixit_replace (pp_formatted_text (&pp));
2745 inform (&richloc, "field %q#D can be accessed via %q#D",
2746 m_field_decl, accessor);
2749 /* This function is called by the parser to process a class member
2750 access expression of the form OBJECT.NAME. NAME is a node used by
2751 the parser to represent a name; it is not yet a DECL. It may,
2752 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2753 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2754 there is no reason to do the lookup twice, so the parser keeps the
2755 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2756 be a template via the use of the "A::template B" syntax. */
2758 tree
2759 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2760 tsubst_flags_t complain)
2762 tree expr;
2763 tree object_type;
2764 tree member;
2765 tree access_path = NULL_TREE;
2766 tree orig_object = object;
2767 tree orig_name = name;
2769 if (object == error_mark_node || name == error_mark_node)
2770 return error_mark_node;
2772 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2773 if (!objc_is_public (object, name))
2774 return error_mark_node;
2776 object_type = TREE_TYPE (object);
2778 if (processing_template_decl)
2780 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2781 type_dependent_object_expression_p (object)
2782 /* If NAME is "f<args>", where either 'f' or 'args' is
2783 dependent, then the expression is dependent. */
2784 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2785 && dependent_template_id_p (TREE_OPERAND (name, 0),
2786 TREE_OPERAND (name, 1)))
2787 /* If NAME is "T::X" where "T" is dependent, then the
2788 expression is dependent. */
2789 || (TREE_CODE (name) == SCOPE_REF
2790 && TYPE_P (TREE_OPERAND (name, 0))
2791 && dependent_scope_p (TREE_OPERAND (name, 0))))
2793 dependent:
2794 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2795 orig_object, orig_name, NULL_TREE);
2797 object = build_non_dependent_expr (object);
2799 else if (c_dialect_objc ()
2800 && identifier_p (name)
2801 && (expr = objc_maybe_build_component_ref (object, name)))
2802 return expr;
2804 /* [expr.ref]
2806 The type of the first expression shall be "class object" (of a
2807 complete type). */
2808 if (!currently_open_class (object_type)
2809 && !complete_type_or_maybe_complain (object_type, object, complain))
2810 return error_mark_node;
2811 if (!CLASS_TYPE_P (object_type))
2813 if (complain & tf_error)
2815 if (POINTER_TYPE_P (object_type)
2816 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2817 error ("request for member %qD in %qE, which is of pointer "
2818 "type %qT (maybe you meant to use %<->%> ?)",
2819 name, object.get_value (), object_type);
2820 else
2821 error ("request for member %qD in %qE, which is of non-class "
2822 "type %qT", name, object.get_value (), object_type);
2824 return error_mark_node;
2827 if (BASELINK_P (name))
2828 /* A member function that has already been looked up. */
2829 member = name;
2830 else
2832 bool is_template_id = false;
2833 tree template_args = NULL_TREE;
2834 tree scope = NULL_TREE;
2836 access_path = object_type;
2838 if (TREE_CODE (name) == SCOPE_REF)
2840 /* A qualified name. The qualifying class or namespace `S'
2841 has already been looked up; it is either a TYPE or a
2842 NAMESPACE_DECL. */
2843 scope = TREE_OPERAND (name, 0);
2844 name = TREE_OPERAND (name, 1);
2846 /* If SCOPE is a namespace, then the qualified name does not
2847 name a member of OBJECT_TYPE. */
2848 if (TREE_CODE (scope) == NAMESPACE_DECL)
2850 if (complain & tf_error)
2851 error ("%<%D::%D%> is not a member of %qT",
2852 scope, name, object_type);
2853 return error_mark_node;
2857 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2859 is_template_id = true;
2860 template_args = TREE_OPERAND (name, 1);
2861 name = TREE_OPERAND (name, 0);
2863 if (!identifier_p (name))
2864 name = OVL_NAME (name);
2867 if (scope)
2869 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2871 gcc_assert (!is_template_id);
2872 /* Looking up a member enumerator (c++/56793). */
2873 if (!TYPE_CLASS_SCOPE_P (scope)
2874 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2876 if (complain & tf_error)
2877 error ("%<%D::%D%> is not a member of %qT",
2878 scope, name, object_type);
2879 return error_mark_node;
2881 tree val = lookup_enumerator (scope, name);
2882 if (!val)
2884 if (complain & tf_error)
2885 error ("%qD is not a member of %qD",
2886 name, scope);
2887 return error_mark_node;
2890 if (TREE_SIDE_EFFECTS (object))
2891 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2892 return val;
2895 gcc_assert (CLASS_TYPE_P (scope));
2896 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2898 if (constructor_name_p (name, scope))
2900 if (complain & tf_error)
2901 error ("cannot call constructor %<%T::%D%> directly",
2902 scope, name);
2903 return error_mark_node;
2906 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2907 access_path = lookup_base (object_type, scope, ba_check,
2908 NULL, complain);
2909 if (access_path == error_mark_node)
2910 return error_mark_node;
2911 if (!access_path)
2913 if (any_dependent_bases_p (object_type))
2914 goto dependent;
2915 if (complain & tf_error)
2916 error ("%qT is not a base of %qT", scope, object_type);
2917 return error_mark_node;
2921 if (TREE_CODE (name) == BIT_NOT_EXPR)
2923 if (dependent_type_p (object_type))
2924 /* The destructor isn't declared yet. */
2925 goto dependent;
2926 member = lookup_destructor (object, scope, name, complain);
2928 else
2930 /* Look up the member. */
2931 access_failure_info afi;
2932 member = lookup_member (access_path, name, /*protect=*/1,
2933 /*want_type=*/false, complain,
2934 &afi);
2935 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2936 if (member == NULL_TREE)
2938 if (dependent_type_p (object_type))
2939 /* Try again at instantiation time. */
2940 goto dependent;
2941 if (complain & tf_error)
2943 tree guessed_id = lookup_member_fuzzy (access_path, name,
2944 /*want_type=*/false);
2945 if (guessed_id)
2947 location_t bogus_component_loc = input_location;
2948 gcc_rich_location rich_loc (bogus_component_loc);
2949 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2950 guessed_id);
2951 error_at (&rich_loc,
2952 "%q#T has no member named %qE;"
2953 " did you mean %qE?",
2954 TREE_CODE (access_path) == TREE_BINFO
2955 ? TREE_TYPE (access_path) : object_type,
2956 name, guessed_id);
2958 else
2959 error ("%q#T has no member named %qE",
2960 TREE_CODE (access_path) == TREE_BINFO
2961 ? TREE_TYPE (access_path) : object_type, name);
2963 return error_mark_node;
2965 if (member == error_mark_node)
2966 return error_mark_node;
2967 if (DECL_P (member)
2968 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2969 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2970 wrong, so don't use it. */
2971 goto dependent;
2972 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2973 goto dependent;
2976 if (is_template_id)
2978 tree templ = member;
2980 if (BASELINK_P (templ))
2981 member = lookup_template_function (templ, template_args);
2982 else if (variable_template_p (templ))
2983 member = (lookup_and_finish_template_variable
2984 (templ, template_args, complain));
2985 else
2987 if (complain & tf_error)
2988 error ("%qD is not a member template function", name);
2989 return error_mark_node;
2994 if (TREE_DEPRECATED (member))
2995 warn_deprecated_use (member, NULL_TREE);
2997 if (template_p)
2998 check_template_keyword (member);
3000 expr = build_class_member_access_expr (object, member, access_path,
3001 /*preserve_reference=*/false,
3002 complain);
3003 if (processing_template_decl && expr != error_mark_node)
3005 if (BASELINK_P (member))
3007 if (TREE_CODE (orig_name) == SCOPE_REF)
3008 BASELINK_QUALIFIED_P (member) = 1;
3009 orig_name = member;
3011 return build_min_non_dep (COMPONENT_REF, expr,
3012 orig_object, orig_name,
3013 NULL_TREE);
3016 return expr;
3019 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3020 type. */
3022 tree
3023 build_simple_component_ref (tree object, tree member)
3025 tree type = cp_build_qualified_type (TREE_TYPE (member),
3026 cp_type_quals (TREE_TYPE (object)));
3027 return build3_loc (input_location,
3028 COMPONENT_REF, type,
3029 object, member, NULL_TREE);
3032 /* Return an expression for the MEMBER_NAME field in the internal
3033 representation of PTRMEM, a pointer-to-member function. (Each
3034 pointer-to-member function type gets its own RECORD_TYPE so it is
3035 more convenient to access the fields by name than by FIELD_DECL.)
3036 This routine converts the NAME to a FIELD_DECL and then creates the
3037 node for the complete expression. */
3039 tree
3040 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3042 tree ptrmem_type;
3043 tree member;
3045 /* This code is a stripped down version of
3046 build_class_member_access_expr. It does not work to use that
3047 routine directly because it expects the object to be of class
3048 type. */
3049 ptrmem_type = TREE_TYPE (ptrmem);
3050 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3051 for (member = TYPE_FIELDS (ptrmem_type); member;
3052 member = DECL_CHAIN (member))
3053 if (DECL_NAME (member) == member_name)
3054 break;
3055 tree res = build_simple_component_ref (ptrmem, member);
3057 TREE_NO_WARNING (res) = 1;
3058 return res;
3061 /* Given an expression PTR for a pointer, return an expression
3062 for the value pointed to.
3063 ERRORSTRING is the name of the operator to appear in error messages.
3065 This function may need to overload OPERATOR_FNNAME.
3066 Must also handle REFERENCE_TYPEs for C++. */
3068 tree
3069 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3070 tsubst_flags_t complain)
3072 tree orig_expr = expr;
3073 tree rval;
3074 tree overload = NULL_TREE;
3076 if (processing_template_decl)
3078 /* Retain the type if we know the operand is a pointer. */
3079 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
3080 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3081 if (type_dependent_expression_p (expr))
3082 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3083 expr = build_non_dependent_expr (expr);
3086 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3087 NULL_TREE, NULL_TREE, &overload, complain);
3088 if (!rval)
3089 rval = cp_build_indirect_ref (expr, errorstring, complain);
3091 if (processing_template_decl && rval != error_mark_node)
3093 if (overload != NULL_TREE)
3094 return (build_min_non_dep_op_overload
3095 (INDIRECT_REF, rval, overload, orig_expr));
3097 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3099 else
3100 return rval;
3103 /* The implementation of the above, and of indirection implied by other
3104 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3106 static tree
3107 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3108 tsubst_flags_t complain, bool do_fold)
3110 tree pointer, type;
3112 /* RO_NULL should only be used with the folding entry points below, not
3113 cp_build_indirect_ref. */
3114 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3116 if (ptr == current_class_ptr
3117 || (TREE_CODE (ptr) == NOP_EXPR
3118 && TREE_OPERAND (ptr, 0) == current_class_ptr
3119 && (same_type_ignoring_top_level_qualifiers_p
3120 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3121 return current_class_ref;
3123 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3124 ? ptr : decay_conversion (ptr, complain));
3125 if (pointer == error_mark_node)
3126 return error_mark_node;
3128 type = TREE_TYPE (pointer);
3130 if (POINTER_TYPE_P (type))
3132 /* [expr.unary.op]
3134 If the type of the expression is "pointer to T," the type
3135 of the result is "T." */
3136 tree t = TREE_TYPE (type);
3138 if ((CONVERT_EXPR_P (ptr)
3139 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3140 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3142 /* If a warning is issued, mark it to avoid duplicates from
3143 the backend. This only needs to be done at
3144 warn_strict_aliasing > 2. */
3145 if (warn_strict_aliasing > 2)
3146 if (strict_aliasing_warning (EXPR_LOCATION (ptr),
3147 type, TREE_OPERAND (ptr, 0)))
3148 TREE_NO_WARNING (ptr) = 1;
3151 if (VOID_TYPE_P (t))
3153 /* A pointer to incomplete type (other than cv void) can be
3154 dereferenced [expr.unary.op]/1 */
3155 if (complain & tf_error)
3156 error ("%qT is not a pointer-to-object type", type);
3157 return error_mark_node;
3159 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3160 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3161 /* The POINTER was something like `&x'. We simplify `*&x' to
3162 `x'. */
3163 return TREE_OPERAND (pointer, 0);
3164 else
3166 tree ref = build1 (INDIRECT_REF, t, pointer);
3168 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3169 so that we get the proper error message if the result is used
3170 to assign to. Also, &* is supposed to be a no-op. */
3171 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3172 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3173 TREE_SIDE_EFFECTS (ref)
3174 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3175 return ref;
3178 else if (!(complain & tf_error))
3179 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3181 /* `pointer' won't be an error_mark_node if we were given a
3182 pointer to member, so it's cool to check for this here. */
3183 else if (TYPE_PTRMEM_P (type))
3184 switch (errorstring)
3186 case RO_ARRAY_INDEXING:
3187 error ("invalid use of array indexing on pointer to member");
3188 break;
3189 case RO_UNARY_STAR:
3190 error ("invalid use of unary %<*%> on pointer to member");
3191 break;
3192 case RO_IMPLICIT_CONVERSION:
3193 error ("invalid use of implicit conversion on pointer to member");
3194 break;
3195 case RO_ARROW_STAR:
3196 error ("left hand operand of %<->*%> must be a pointer to class, "
3197 "but is a pointer to member of type %qT", type);
3198 break;
3199 default:
3200 gcc_unreachable ();
3202 else if (pointer != error_mark_node)
3203 invalid_indirection_error (input_location, type, errorstring);
3205 return error_mark_node;
3208 /* Entry point used by c-common, which expects folding. */
3210 tree
3211 build_indirect_ref (location_t /*loc*/,
3212 tree ptr, ref_operator errorstring)
3214 return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3217 /* Entry point used by internal indirection needs that don't correspond to any
3218 syntactic construct. */
3220 tree
3221 cp_build_fold_indirect_ref (tree pointer)
3223 return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3226 /* Entry point used by indirection needs that correspond to some syntactic
3227 construct. */
3229 tree
3230 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3231 tsubst_flags_t complain)
3233 return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3236 /* This handles expressions of the form "a[i]", which denotes
3237 an array reference.
3239 This is logically equivalent in C to *(a+i), but we may do it differently.
3240 If A is a variable or a member, we generate a primitive ARRAY_REF.
3241 This avoids forcing the array out of registers, and can work on
3242 arrays that are not lvalues (for example, members of structures returned
3243 by functions).
3245 If INDEX is of some user-defined type, it must be converted to
3246 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3247 will inherit the type of the array, which will be some pointer type.
3249 LOC is the location to use in building the array reference. */
3251 tree
3252 cp_build_array_ref (location_t loc, tree array, tree idx,
3253 tsubst_flags_t complain)
3255 tree ret;
3257 if (idx == 0)
3259 if (complain & tf_error)
3260 error_at (loc, "subscript missing in array reference");
3261 return error_mark_node;
3264 if (TREE_TYPE (array) == error_mark_node
3265 || TREE_TYPE (idx) == error_mark_node)
3266 return error_mark_node;
3268 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3269 inside it. */
3270 switch (TREE_CODE (array))
3272 case COMPOUND_EXPR:
3274 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3275 complain);
3276 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3277 TREE_OPERAND (array, 0), value);
3278 SET_EXPR_LOCATION (ret, loc);
3279 return ret;
3282 case COND_EXPR:
3283 ret = build_conditional_expr
3284 (loc, TREE_OPERAND (array, 0),
3285 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3286 complain),
3287 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3288 complain),
3289 complain);
3290 protected_set_expr_location (ret, loc);
3291 return ret;
3293 default:
3294 break;
3297 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3299 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3301 tree rval, type;
3303 warn_array_subscript_with_type_char (loc, idx);
3305 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3307 if (complain & tf_error)
3308 error_at (loc, "array subscript is not an integer");
3309 return error_mark_node;
3312 /* Apply integral promotions *after* noticing character types.
3313 (It is unclear why we do these promotions -- the standard
3314 does not say that we should. In fact, the natural thing would
3315 seem to be to convert IDX to ptrdiff_t; we're performing
3316 pointer arithmetic.) */
3317 idx = cp_perform_integral_promotions (idx, complain);
3319 /* An array that is indexed by a non-constant
3320 cannot be stored in a register; we must be able to do
3321 address arithmetic on its address.
3322 Likewise an array of elements of variable size. */
3323 if (TREE_CODE (idx) != INTEGER_CST
3324 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3325 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3326 != INTEGER_CST)))
3328 if (!cxx_mark_addressable (array, true))
3329 return error_mark_node;
3332 /* An array that is indexed by a constant value which is not within
3333 the array bounds cannot be stored in a register either; because we
3334 would get a crash in store_bit_field/extract_bit_field when trying
3335 to access a non-existent part of the register. */
3336 if (TREE_CODE (idx) == INTEGER_CST
3337 && TYPE_DOMAIN (TREE_TYPE (array))
3338 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3340 if (!cxx_mark_addressable (array))
3341 return error_mark_node;
3344 /* Note in C++ it is valid to subscript a `register' array, since
3345 it is valid to take the address of something with that
3346 storage specification. */
3347 if (extra_warnings)
3349 tree foo = array;
3350 while (TREE_CODE (foo) == COMPONENT_REF)
3351 foo = TREE_OPERAND (foo, 0);
3352 if (VAR_P (foo) && DECL_REGISTER (foo)
3353 && (complain & tf_warning))
3354 warning_at (loc, OPT_Wextra,
3355 "subscripting array declared %<register%>");
3358 type = TREE_TYPE (TREE_TYPE (array));
3359 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3360 /* Array ref is const/volatile if the array elements are
3361 or if the array is.. */
3362 TREE_READONLY (rval)
3363 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3364 TREE_SIDE_EFFECTS (rval)
3365 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3366 TREE_THIS_VOLATILE (rval)
3367 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3368 ret = require_complete_type_sfinae (rval, complain);
3369 protected_set_expr_location (ret, loc);
3370 if (non_lvalue)
3371 ret = non_lvalue_loc (loc, ret);
3372 return ret;
3376 tree ar = cp_default_conversion (array, complain);
3377 tree ind = cp_default_conversion (idx, complain);
3379 /* Put the integer in IND to simplify error checking. */
3380 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3381 std::swap (ar, ind);
3383 if (ar == error_mark_node || ind == error_mark_node)
3384 return error_mark_node;
3386 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3388 if (complain & tf_error)
3389 error_at (loc, "subscripted value is neither array nor pointer");
3390 return error_mark_node;
3392 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3394 if (complain & tf_error)
3395 error_at (loc, "array subscript is not an integer");
3396 return error_mark_node;
3399 warn_array_subscript_with_type_char (loc, idx);
3401 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3402 PLUS_EXPR, ar, ind,
3403 complain),
3404 RO_ARRAY_INDEXING,
3405 complain);
3406 protected_set_expr_location (ret, loc);
3407 if (non_lvalue)
3408 ret = non_lvalue_loc (loc, ret);
3409 return ret;
3413 /* Entry point for Obj-C++. */
3415 tree
3416 build_array_ref (location_t loc, tree array, tree idx)
3418 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3421 /* Resolve a pointer to member function. INSTANCE is the object
3422 instance to use, if the member points to a virtual member.
3424 This used to avoid checking for virtual functions if basetype
3425 has no virtual functions, according to an earlier ANSI draft.
3426 With the final ISO C++ rules, such an optimization is
3427 incorrect: A pointer to a derived member can be static_cast
3428 to pointer-to-base-member, as long as the dynamic object
3429 later has the right member. So now we only do this optimization
3430 when we know the dynamic type of the object. */
3432 tree
3433 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3434 tsubst_flags_t complain)
3436 if (TREE_CODE (function) == OFFSET_REF)
3437 function = TREE_OPERAND (function, 1);
3439 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3441 tree idx, delta, e1, e2, e3, vtbl;
3442 bool nonvirtual;
3443 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3444 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3446 tree instance_ptr = *instance_ptrptr;
3447 tree instance_save_expr = 0;
3448 if (instance_ptr == error_mark_node)
3450 if (TREE_CODE (function) == PTRMEM_CST)
3452 /* Extracting the function address from a pmf is only
3453 allowed with -Wno-pmf-conversions. It only works for
3454 pmf constants. */
3455 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3456 e1 = convert (fntype, e1);
3457 return e1;
3459 else
3461 if (complain & tf_error)
3462 error ("object missing in use of %qE", function);
3463 return error_mark_node;
3467 /* True if we know that the dynamic type of the object doesn't have
3468 virtual functions, so we can assume the PFN field is a pointer. */
3469 nonvirtual = (COMPLETE_TYPE_P (basetype)
3470 && !TYPE_POLYMORPHIC_P (basetype)
3471 && resolves_to_fixed_type_p (instance_ptr, 0));
3473 /* If we don't really have an object (i.e. in an ill-formed
3474 conversion from PMF to pointer), we can't resolve virtual
3475 functions anyway. */
3476 if (!nonvirtual && is_dummy_object (instance_ptr))
3477 nonvirtual = true;
3479 if (TREE_SIDE_EFFECTS (instance_ptr))
3480 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3482 if (TREE_SIDE_EFFECTS (function))
3483 function = save_expr (function);
3485 /* Start by extracting all the information from the PMF itself. */
3486 e3 = pfn_from_ptrmemfunc (function);
3487 delta = delta_from_ptrmemfunc (function);
3488 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3489 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3491 int flag_sanitize_save;
3492 case ptrmemfunc_vbit_in_pfn:
3493 e1 = cp_build_binary_op (input_location,
3494 BIT_AND_EXPR, idx, integer_one_node,
3495 complain);
3496 idx = cp_build_binary_op (input_location,
3497 MINUS_EXPR, idx, integer_one_node,
3498 complain);
3499 if (idx == error_mark_node)
3500 return error_mark_node;
3501 break;
3503 case ptrmemfunc_vbit_in_delta:
3504 e1 = cp_build_binary_op (input_location,
3505 BIT_AND_EXPR, delta, integer_one_node,
3506 complain);
3507 /* Don't instrument the RSHIFT_EXPR we're about to create because
3508 we're going to use DELTA number of times, and that wouldn't play
3509 well with SAVE_EXPRs therein. */
3510 flag_sanitize_save = flag_sanitize;
3511 flag_sanitize = 0;
3512 delta = cp_build_binary_op (input_location,
3513 RSHIFT_EXPR, delta, integer_one_node,
3514 complain);
3515 flag_sanitize = flag_sanitize_save;
3516 if (delta == error_mark_node)
3517 return error_mark_node;
3518 break;
3520 default:
3521 gcc_unreachable ();
3524 if (e1 == error_mark_node)
3525 return error_mark_node;
3527 /* Convert down to the right base before using the instance. A
3528 special case is that in a pointer to member of class C, C may
3529 be incomplete. In that case, the function will of course be
3530 a member of C, and no conversion is required. In fact,
3531 lookup_base will fail in that case, because incomplete
3532 classes do not have BINFOs. */
3533 if (!same_type_ignoring_top_level_qualifiers_p
3534 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3536 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3537 basetype, ba_check, NULL, complain);
3538 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3539 1, complain);
3540 if (instance_ptr == error_mark_node)
3541 return error_mark_node;
3543 /* ...and then the delta in the PMF. */
3544 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3546 /* Hand back the adjusted 'this' argument to our caller. */
3547 *instance_ptrptr = instance_ptr;
3549 if (nonvirtual)
3550 /* Now just return the pointer. */
3551 return e3;
3553 /* Next extract the vtable pointer from the object. */
3554 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3555 instance_ptr);
3556 vtbl = cp_build_fold_indirect_ref (vtbl);
3557 if (vtbl == error_mark_node)
3558 return error_mark_node;
3560 /* Finally, extract the function pointer from the vtable. */
3561 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3562 e2 = cp_build_fold_indirect_ref (e2);
3563 if (e2 == error_mark_node)
3564 return error_mark_node;
3565 TREE_CONSTANT (e2) = 1;
3567 /* When using function descriptors, the address of the
3568 vtable entry is treated as a function pointer. */
3569 if (TARGET_VTABLE_USES_DESCRIPTORS)
3570 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3571 cp_build_addr_expr (e2, complain));
3573 e2 = fold_convert (TREE_TYPE (e3), e2);
3574 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3575 if (e1 == error_mark_node)
3576 return error_mark_node;
3578 /* Make sure this doesn't get evaluated first inside one of the
3579 branches of the COND_EXPR. */
3580 if (instance_save_expr)
3581 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3582 instance_save_expr, e1);
3584 function = e1;
3586 return function;
3589 /* Used by the C-common bits. */
3590 tree
3591 build_function_call (location_t /*loc*/,
3592 tree function, tree params)
3594 return cp_build_function_call (function, params, tf_warning_or_error);
3597 /* Used by the C-common bits. */
3598 tree
3599 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3600 tree function, vec<tree, va_gc> *params,
3601 vec<tree, va_gc> * /*origtypes*/)
3603 vec<tree, va_gc> *orig_params = params;
3604 tree ret = cp_build_function_call_vec (function, &params,
3605 tf_warning_or_error);
3607 /* cp_build_function_call_vec can reallocate PARAMS by adding
3608 default arguments. That should never happen here. Verify
3609 that. */
3610 gcc_assert (params == orig_params);
3612 return ret;
3615 /* Build a function call using a tree list of arguments. */
3617 static tree
3618 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3620 vec<tree, va_gc> *vec;
3621 tree ret;
3623 vec = make_tree_vector ();
3624 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3625 vec_safe_push (vec, TREE_VALUE (params));
3626 ret = cp_build_function_call_vec (function, &vec, complain);
3627 release_tree_vector (vec);
3628 return ret;
3631 /* Build a function call using varargs. */
3633 tree
3634 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3636 vec<tree, va_gc> *vec;
3637 va_list args;
3638 tree ret, t;
3640 vec = make_tree_vector ();
3641 va_start (args, complain);
3642 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3643 vec_safe_push (vec, t);
3644 va_end (args);
3645 ret = cp_build_function_call_vec (function, &vec, complain);
3646 release_tree_vector (vec);
3647 return ret;
3650 /* Build a function call using a vector of arguments. PARAMS may be
3651 NULL if there are no parameters. This changes the contents of
3652 PARAMS. */
3654 tree
3655 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3656 tsubst_flags_t complain)
3658 tree fntype, fndecl;
3659 int is_method;
3660 tree original = function;
3661 int nargs;
3662 tree *argarray;
3663 tree parm_types;
3664 vec<tree, va_gc> *allocated = NULL;
3665 tree ret;
3667 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3668 expressions, like those used for ObjC messenger dispatches. */
3669 if (params != NULL && !vec_safe_is_empty (*params))
3670 function = objc_rewrite_function_call (function, (**params)[0]);
3672 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3673 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3674 if (TREE_CODE (function) == NOP_EXPR
3675 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3676 function = TREE_OPERAND (function, 0);
3678 if (TREE_CODE (function) == FUNCTION_DECL)
3680 /* If the function is a non-template member function
3681 or a non-template friend, then we need to check the
3682 constraints.
3684 Note that if overload resolution failed with a single
3685 candidate this function will be used to explicitly diagnose
3686 the failure for the single call expression. The check is
3687 technically redundant since we also would have failed in
3688 add_function_candidate. */
3689 if (flag_concepts
3690 && (complain & tf_error)
3691 && !constraints_satisfied_p (function))
3693 error ("cannot call function %qD", function);
3694 location_t loc = DECL_SOURCE_LOCATION (function);
3695 diagnose_constraints (loc, function, NULL_TREE);
3696 return error_mark_node;
3699 if (!mark_used (function, complain) && !(complain & tf_error))
3700 return error_mark_node;
3701 fndecl = function;
3703 /* Convert anything with function type to a pointer-to-function. */
3704 if (DECL_MAIN_P (function))
3706 if (complain & tf_error)
3707 pedwarn (input_location, OPT_Wpedantic,
3708 "ISO C++ forbids calling %<::main%> from within program");
3709 else
3710 return error_mark_node;
3712 function = build_addr_func (function, complain);
3714 else
3716 fndecl = NULL_TREE;
3718 function = build_addr_func (function, complain);
3721 if (function == error_mark_node)
3722 return error_mark_node;
3724 fntype = TREE_TYPE (function);
3726 if (TYPE_PTRMEMFUNC_P (fntype))
3728 if (complain & tf_error)
3729 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3730 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3731 original, original);
3732 return error_mark_node;
3735 is_method = (TYPE_PTR_P (fntype)
3736 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3738 if (!(TYPE_PTRFN_P (fntype)
3739 || is_method
3740 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3742 if (complain & tf_error)
3744 if (!flag_diagnostics_show_caret)
3745 error_at (input_location,
3746 "%qE cannot be used as a function", original);
3747 else if (DECL_P (original))
3748 error_at (input_location,
3749 "%qD cannot be used as a function", original);
3750 else
3751 error_at (input_location,
3752 "expression cannot be used as a function");
3755 return error_mark_node;
3758 /* fntype now gets the type of function pointed to. */
3759 fntype = TREE_TYPE (fntype);
3760 parm_types = TYPE_ARG_TYPES (fntype);
3762 if (params == NULL)
3764 allocated = make_tree_vector ();
3765 params = &allocated;
3768 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3769 complain);
3770 if (nargs < 0)
3771 return error_mark_node;
3773 argarray = (*params)->address ();
3775 /* Check for errors in format strings and inappropriately
3776 null parameters. */
3777 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3778 nargs, argarray, NULL);
3780 ret = build_cxx_call (function, nargs, argarray, complain);
3782 if (warned_p)
3784 tree c = extract_call_expr (ret);
3785 if (TREE_CODE (c) == CALL_EXPR)
3786 TREE_NO_WARNING (c) = 1;
3789 if (allocated != NULL)
3790 release_tree_vector (allocated);
3792 return ret;
3795 /* Subroutine of convert_arguments.
3796 Print an error message about a wrong number of arguments. */
3798 static void
3799 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3801 if (fndecl)
3803 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3805 if (DECL_NAME (fndecl) == NULL_TREE
3806 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3807 error_at (loc,
3808 too_many_p
3809 ? G_("too many arguments to constructor %q#D")
3810 : G_("too few arguments to constructor %q#D"),
3811 fndecl);
3812 else
3813 error_at (loc,
3814 too_many_p
3815 ? G_("too many arguments to member function %q#D")
3816 : G_("too few arguments to member function %q#D"),
3817 fndecl);
3819 else
3820 error_at (loc,
3821 too_many_p
3822 ? G_("too many arguments to function %q#D")
3823 : G_("too few arguments to function %q#D"),
3824 fndecl);
3825 if (!DECL_IS_BUILTIN (fndecl))
3826 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3828 else
3830 if (c_dialect_objc () && objc_message_selector ())
3831 error_at (loc,
3832 too_many_p
3833 ? G_("too many arguments to method %q#D")
3834 : G_("too few arguments to method %q#D"),
3835 objc_message_selector ());
3836 else
3837 error_at (loc, too_many_p ? G_("too many arguments to function")
3838 : G_("too few arguments to function"));
3842 /* Convert the actual parameter expressions in the list VALUES to the
3843 types in the list TYPELIST. The converted expressions are stored
3844 back in the VALUES vector.
3845 If parmdecls is exhausted, or when an element has NULL as its type,
3846 perform the default conversions.
3848 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3850 This is also where warnings about wrong number of args are generated.
3852 Returns the actual number of arguments processed (which might be less
3853 than the length of the vector), or -1 on error.
3855 In C++, unspecified trailing parameters can be filled in with their
3856 default arguments, if such were specified. Do so here. */
3858 static int
3859 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3860 int flags, tsubst_flags_t complain)
3862 tree typetail;
3863 unsigned int i;
3865 /* Argument passing is always copy-initialization. */
3866 flags |= LOOKUP_ONLYCONVERTING;
3868 for (i = 0, typetail = typelist;
3869 i < vec_safe_length (*values);
3870 i++)
3872 tree type = typetail ? TREE_VALUE (typetail) : 0;
3873 tree val = (**values)[i];
3875 if (val == error_mark_node || type == error_mark_node)
3876 return -1;
3878 if (type == void_type_node)
3880 if (complain & tf_error)
3882 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3883 return i;
3885 else
3886 return -1;
3889 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3890 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3891 if (TREE_CODE (val) == NOP_EXPR
3892 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3893 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3894 val = TREE_OPERAND (val, 0);
3896 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3898 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3899 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3900 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3901 val = decay_conversion (val, complain);
3904 if (val == error_mark_node)
3905 return -1;
3907 if (type != 0)
3909 /* Formal parm type is specified by a function prototype. */
3910 tree parmval;
3912 if (!COMPLETE_TYPE_P (complete_type (type)))
3914 if (complain & tf_error)
3916 if (fndecl)
3917 error ("parameter %P of %qD has incomplete type %qT",
3918 i, fndecl, type);
3919 else
3920 error ("parameter %P has incomplete type %qT", i, type);
3922 parmval = error_mark_node;
3924 else
3926 parmval = convert_for_initialization
3927 (NULL_TREE, type, val, flags,
3928 ICR_ARGPASS, fndecl, i, complain);
3929 parmval = convert_for_arg_passing (type, parmval, complain);
3932 if (parmval == error_mark_node)
3933 return -1;
3935 (**values)[i] = parmval;
3937 else
3939 if (fndecl && magic_varargs_p (fndecl))
3940 /* Don't do ellipsis conversion for __built_in_constant_p
3941 as this will result in spurious errors for non-trivial
3942 types. */
3943 val = require_complete_type_sfinae (val, complain);
3944 else
3945 val = convert_arg_to_ellipsis (val, complain);
3947 (**values)[i] = val;
3950 if (typetail)
3951 typetail = TREE_CHAIN (typetail);
3954 if (typetail != 0 && typetail != void_list_node)
3956 /* See if there are default arguments that can be used. Because
3957 we hold default arguments in the FUNCTION_TYPE (which is so
3958 wrong), we can see default parameters here from deduced
3959 contexts (and via typeof) for indirect function calls.
3960 Fortunately we know whether we have a function decl to
3961 provide default arguments in a language conformant
3962 manner. */
3963 if (fndecl && TREE_PURPOSE (typetail)
3964 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3966 for (; typetail != void_list_node; ++i)
3968 /* After DR777, with explicit template args we can end up with a
3969 default argument followed by no default argument. */
3970 if (!TREE_PURPOSE (typetail))
3971 break;
3972 tree parmval
3973 = convert_default_arg (TREE_VALUE (typetail),
3974 TREE_PURPOSE (typetail),
3975 fndecl, i, complain);
3977 if (parmval == error_mark_node)
3978 return -1;
3980 vec_safe_push (*values, parmval);
3981 typetail = TREE_CHAIN (typetail);
3982 /* ends with `...'. */
3983 if (typetail == NULL_TREE)
3984 break;
3988 if (typetail && typetail != void_list_node)
3990 if (complain & tf_error)
3991 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3992 return -1;
3996 return (int) i;
3999 /* Build a binary-operation expression, after performing default
4000 conversions on the operands. CODE is the kind of expression to
4001 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4002 are the tree codes which correspond to ARG1 and ARG2 when issuing
4003 warnings about possibly misplaced parentheses. They may differ
4004 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4005 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4006 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4007 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4008 ARG2_CODE as ERROR_MARK. */
4010 tree
4011 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4012 enum tree_code arg1_code, tree arg2,
4013 enum tree_code arg2_code, tree *overload_p,
4014 tsubst_flags_t complain)
4016 tree orig_arg1;
4017 tree orig_arg2;
4018 tree expr;
4019 tree overload = NULL_TREE;
4021 orig_arg1 = arg1;
4022 orig_arg2 = arg2;
4024 if (processing_template_decl)
4026 if (type_dependent_expression_p (arg1)
4027 || type_dependent_expression_p (arg2))
4028 return build_min_nt_loc (loc, code, arg1, arg2);
4029 arg1 = build_non_dependent_expr (arg1);
4030 arg2 = build_non_dependent_expr (arg2);
4033 if (code == DOTSTAR_EXPR)
4034 expr = build_m_component_ref (arg1, arg2, complain);
4035 else
4036 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4037 &overload, complain);
4039 if (overload_p != NULL)
4040 *overload_p = overload;
4042 /* Check for cases such as x+y<<z which users are likely to
4043 misinterpret. But don't warn about obj << x + y, since that is a
4044 common idiom for I/O. */
4045 if (warn_parentheses
4046 && (complain & tf_warning)
4047 && !processing_template_decl
4048 && !error_operand_p (arg1)
4049 && !error_operand_p (arg2)
4050 && (code != LSHIFT_EXPR
4051 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4052 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4053 arg2_code, orig_arg2);
4055 if (processing_template_decl && expr != error_mark_node)
4057 if (overload != NULL_TREE)
4058 return (build_min_non_dep_op_overload
4059 (code, expr, overload, orig_arg1, orig_arg2));
4061 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4064 return expr;
4067 /* Build and return an ARRAY_REF expression. */
4069 tree
4070 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4071 tsubst_flags_t complain)
4073 tree orig_arg1 = arg1;
4074 tree orig_arg2 = arg2;
4075 tree expr;
4076 tree overload = NULL_TREE;
4078 if (processing_template_decl)
4080 if (type_dependent_expression_p (arg1)
4081 || type_dependent_expression_p (arg2))
4082 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4083 NULL_TREE, NULL_TREE);
4084 arg1 = build_non_dependent_expr (arg1);
4085 arg2 = build_non_dependent_expr (arg2);
4088 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4089 NULL_TREE, &overload, complain);
4091 if (processing_template_decl && expr != error_mark_node)
4093 if (overload != NULL_TREE)
4094 return (build_min_non_dep_op_overload
4095 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4097 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4098 NULL_TREE, NULL_TREE);
4100 return expr;
4103 /* Return whether OP is an expression of enum type cast to integer
4104 type. In C++ even unsigned enum types are cast to signed integer
4105 types. We do not want to issue warnings about comparisons between
4106 signed and unsigned types when one of the types is an enum type.
4107 Those warnings are always false positives in practice. */
4109 static bool
4110 enum_cast_to_int (tree op)
4112 if (CONVERT_EXPR_P (op)
4113 && TREE_TYPE (op) == integer_type_node
4114 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4115 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4116 return true;
4118 /* The cast may have been pushed into a COND_EXPR. */
4119 if (TREE_CODE (op) == COND_EXPR)
4120 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4121 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4123 return false;
4126 /* For the c-common bits. */
4127 tree
4128 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4129 bool /*convert_p*/)
4131 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4134 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4135 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4137 static tree
4138 build_vec_cmp (tree_code code, tree type,
4139 tree arg0, tree arg1)
4141 tree zero_vec = build_zero_cst (type);
4142 tree minus_one_vec = build_minus_one_cst (type);
4143 tree cmp_type = build_same_sized_truth_vector_type(type);
4144 tree cmp = build2 (code, cmp_type, arg0, arg1);
4145 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4148 /* Possibly warn about an address never being NULL. */
4150 static void
4151 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4153 if (!warn_address
4154 || (complain & tf_warning) == 0
4155 || c_inhibit_evaluation_warnings != 0
4156 || TREE_NO_WARNING (op))
4157 return;
4159 tree cop = fold_non_dependent_expr (op);
4161 if (TREE_CODE (cop) == ADDR_EXPR
4162 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4163 && !TREE_NO_WARNING (cop))
4164 warning_at (location, OPT_Waddress, "the address of %qD will never "
4165 "be NULL", TREE_OPERAND (cop, 0));
4167 if (CONVERT_EXPR_P (op)
4168 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4170 tree inner_op = op;
4171 STRIP_NOPS (inner_op);
4173 if (DECL_P (inner_op))
4174 warning_at (location, OPT_Waddress,
4175 "the compiler can assume that the address of "
4176 "%qD will never be NULL", inner_op);
4180 /* Build a binary-operation expression without default conversions.
4181 CODE is the kind of expression to build.
4182 LOCATION is the location_t of the operator in the source code.
4183 This function differs from `build' in several ways:
4184 the data type of the result is computed and recorded in it,
4185 warnings are generated if arg data types are invalid,
4186 special handling for addition and subtraction of pointers is known,
4187 and some optimization is done (operations on narrow ints
4188 are done in the narrower type when that gives the same result).
4189 Constant folding is also done before the result is returned.
4191 Note that the operands will never have enumeral types
4192 because either they have just had the default conversions performed
4193 or they have both just been converted to some other type in which
4194 the arithmetic is to be done.
4196 C++: must do special pointer arithmetic when implementing
4197 multiple inheritance, and deal with pointer to member functions. */
4199 tree
4200 cp_build_binary_op (location_t location,
4201 enum tree_code code, tree orig_op0, tree orig_op1,
4202 tsubst_flags_t complain)
4204 tree op0, op1;
4205 enum tree_code code0, code1;
4206 tree type0, type1;
4207 const char *invalid_op_diag;
4209 /* Expression code to give to the expression when it is built.
4210 Normally this is CODE, which is what the caller asked for,
4211 but in some special cases we change it. */
4212 enum tree_code resultcode = code;
4214 /* Data type in which the computation is to be performed.
4215 In the simplest cases this is the common type of the arguments. */
4216 tree result_type = NULL_TREE;
4218 /* Nonzero means operands have already been type-converted
4219 in whatever way is necessary.
4220 Zero means they need to be converted to RESULT_TYPE. */
4221 int converted = 0;
4223 /* Nonzero means create the expression with this type, rather than
4224 RESULT_TYPE. */
4225 tree build_type = 0;
4227 /* Nonzero means after finally constructing the expression
4228 convert it to this type. */
4229 tree final_type = 0;
4231 tree result, result_ovl;
4233 /* Nonzero if this is an operation like MIN or MAX which can
4234 safely be computed in short if both args are promoted shorts.
4235 Also implies COMMON.
4236 -1 indicates a bitwise operation; this makes a difference
4237 in the exact conditions for when it is safe to do the operation
4238 in a narrower mode. */
4239 int shorten = 0;
4241 /* Nonzero if this is a comparison operation;
4242 if both args are promoted shorts, compare the original shorts.
4243 Also implies COMMON. */
4244 int short_compare = 0;
4246 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4247 int common = 0;
4249 /* True if both operands have arithmetic type. */
4250 bool arithmetic_types_p;
4252 /* Apply default conversions. */
4253 op0 = orig_op0;
4254 op1 = orig_op1;
4256 /* Remember whether we're doing / or %. */
4257 bool doing_div_or_mod = false;
4259 /* Remember whether we're doing << or >>. */
4260 bool doing_shift = false;
4262 /* Tree holding instrumentation expression. */
4263 tree instrument_expr = NULL_TREE;
4265 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4266 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4267 || code == TRUTH_XOR_EXPR)
4269 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4270 op0 = decay_conversion (op0, complain);
4271 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4272 op1 = decay_conversion (op1, complain);
4274 else
4276 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4277 op0 = cp_default_conversion (op0, complain);
4278 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4279 op1 = cp_default_conversion (op1, complain);
4282 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4283 STRIP_TYPE_NOPS (op0);
4284 STRIP_TYPE_NOPS (op1);
4286 /* DTRT if one side is an overloaded function, but complain about it. */
4287 if (type_unknown_p (op0))
4289 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4290 if (t != error_mark_node)
4292 if (complain & tf_error)
4293 permerror (input_location, "assuming cast to type %qT from overloaded function",
4294 TREE_TYPE (t));
4295 op0 = t;
4298 if (type_unknown_p (op1))
4300 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4301 if (t != error_mark_node)
4303 if (complain & tf_error)
4304 permerror (input_location, "assuming cast to type %qT from overloaded function",
4305 TREE_TYPE (t));
4306 op1 = t;
4310 type0 = TREE_TYPE (op0);
4311 type1 = TREE_TYPE (op1);
4313 /* The expression codes of the data types of the arguments tell us
4314 whether the arguments are integers, floating, pointers, etc. */
4315 code0 = TREE_CODE (type0);
4316 code1 = TREE_CODE (type1);
4318 /* If an error was already reported for one of the arguments,
4319 avoid reporting another error. */
4320 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4321 return error_mark_node;
4323 if ((invalid_op_diag
4324 = targetm.invalid_binary_op (code, type0, type1)))
4326 if (complain & tf_error)
4327 error (invalid_op_diag);
4328 return error_mark_node;
4331 /* Issue warnings about peculiar, but valid, uses of NULL. */
4332 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4333 /* It's reasonable to use pointer values as operands of &&
4334 and ||, so NULL is no exception. */
4335 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4336 && ( /* Both are NULL (or 0) and the operation was not a
4337 comparison or a pointer subtraction. */
4338 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4339 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4340 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4341 || (!null_ptr_cst_p (orig_op0)
4342 && !TYPE_PTR_OR_PTRMEM_P (type0))
4343 || (!null_ptr_cst_p (orig_op1)
4344 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4345 && (complain & tf_warning))
4347 source_location loc =
4348 expansion_point_location_if_in_system_header (input_location);
4350 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4353 /* In case when one of the operands of the binary operation is
4354 a vector and another is a scalar -- convert scalar to vector. */
4355 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4357 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4358 complain & tf_error);
4360 switch (convert_flag)
4362 case stv_error:
4363 return error_mark_node;
4364 case stv_firstarg:
4366 op0 = convert (TREE_TYPE (type1), op0);
4367 op0 = save_expr (op0);
4368 op0 = build_vector_from_val (type1, op0);
4369 type0 = TREE_TYPE (op0);
4370 code0 = TREE_CODE (type0);
4371 converted = 1;
4372 break;
4374 case stv_secondarg:
4376 op1 = convert (TREE_TYPE (type0), op1);
4377 op1 = save_expr (op1);
4378 op1 = build_vector_from_val (type0, op1);
4379 type1 = TREE_TYPE (op1);
4380 code1 = TREE_CODE (type1);
4381 converted = 1;
4382 break;
4384 default:
4385 break;
4389 switch (code)
4391 case MINUS_EXPR:
4392 /* Subtraction of two similar pointers.
4393 We must subtract them as integers, then divide by object size. */
4394 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4395 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4396 TREE_TYPE (type1)))
4398 result = pointer_diff (location, op0, op1,
4399 common_pointer_type (type0, type1), complain,
4400 &instrument_expr);
4401 if (instrument_expr != NULL)
4402 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4403 instrument_expr, result);
4405 return result;
4407 /* In all other cases except pointer - int, the usual arithmetic
4408 rules apply. */
4409 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4411 common = 1;
4412 break;
4414 /* The pointer - int case is just like pointer + int; fall
4415 through. */
4416 gcc_fallthrough ();
4417 case PLUS_EXPR:
4418 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4419 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4421 tree ptr_operand;
4422 tree int_operand;
4423 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4424 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4425 if (processing_template_decl)
4427 result_type = TREE_TYPE (ptr_operand);
4428 break;
4430 return cp_pointer_int_sum (location, code,
4431 ptr_operand,
4432 int_operand,
4433 complain);
4435 common = 1;
4436 break;
4438 case MULT_EXPR:
4439 common = 1;
4440 break;
4442 case TRUNC_DIV_EXPR:
4443 case CEIL_DIV_EXPR:
4444 case FLOOR_DIV_EXPR:
4445 case ROUND_DIV_EXPR:
4446 case EXACT_DIV_EXPR:
4447 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4449 tree type0 = TREE_OPERAND (op0, 0);
4450 tree type1 = TREE_OPERAND (op1, 0);
4451 tree first_arg = type0;
4452 if (!TYPE_P (type0))
4453 type0 = TREE_TYPE (type0);
4454 if (!TYPE_P (type1))
4455 type1 = TREE_TYPE (type1);
4456 if (POINTER_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4457 && !(TREE_CODE (first_arg) == PARM_DECL
4458 && DECL_ARRAY_PARAMETER_P (first_arg)
4459 && warn_sizeof_array_argument)
4460 && (complain & tf_warning))
4461 if (warning_at (location, OPT_Wsizeof_pointer_div,
4462 "division %<sizeof (%T) / sizeof (%T)%> does "
4463 "not compute the number of array elements",
4464 type0, type1))
4465 if (DECL_P (first_arg))
4466 inform (DECL_SOURCE_LOCATION (first_arg),
4467 "first %<sizeof%> operand was declared here");
4470 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4471 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4472 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4473 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4475 enum tree_code tcode0 = code0, tcode1 = code1;
4476 tree cop1 = fold_non_dependent_expr (op1);
4477 doing_div_or_mod = true;
4478 warn_for_div_by_zero (location, cop1);
4480 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4481 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4482 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4483 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4485 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4486 resultcode = RDIV_EXPR;
4487 else
4488 /* When dividing two signed integers, we have to promote to int.
4489 unless we divide by a constant != -1. Note that default
4490 conversion will have been performed on the operands at this
4491 point, so we have to dig out the original type to find out if
4492 it was unsigned. */
4493 shorten = ((TREE_CODE (op0) == NOP_EXPR
4494 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4495 || (TREE_CODE (op1) == INTEGER_CST
4496 && ! integer_all_onesp (op1)));
4498 common = 1;
4500 break;
4502 case BIT_AND_EXPR:
4503 case BIT_IOR_EXPR:
4504 case BIT_XOR_EXPR:
4505 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4506 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4507 && !VECTOR_FLOAT_TYPE_P (type0)
4508 && !VECTOR_FLOAT_TYPE_P (type1)))
4509 shorten = -1;
4510 break;
4512 case TRUNC_MOD_EXPR:
4513 case FLOOR_MOD_EXPR:
4515 tree cop1 = fold_non_dependent_expr (op1);
4516 doing_div_or_mod = true;
4517 warn_for_div_by_zero (location, cop1);
4520 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4521 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4522 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4523 common = 1;
4524 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4526 /* Although it would be tempting to shorten always here, that loses
4527 on some targets, since the modulo instruction is undefined if the
4528 quotient can't be represented in the computation mode. We shorten
4529 only if unsigned or if dividing by something we know != -1. */
4530 shorten = ((TREE_CODE (op0) == NOP_EXPR
4531 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4532 || (TREE_CODE (op1) == INTEGER_CST
4533 && ! integer_all_onesp (op1)));
4534 common = 1;
4536 break;
4538 case TRUTH_ANDIF_EXPR:
4539 case TRUTH_ORIF_EXPR:
4540 case TRUTH_AND_EXPR:
4541 case TRUTH_OR_EXPR:
4542 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4544 if (!COMPARISON_CLASS_P (op1))
4545 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4546 build_zero_cst (type1), complain);
4547 if (code == TRUTH_ANDIF_EXPR)
4549 tree z = build_zero_cst (TREE_TYPE (op1));
4550 return build_conditional_expr (location, op0, op1, z, complain);
4552 else if (code == TRUTH_ORIF_EXPR)
4554 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4555 return build_conditional_expr (location, op0, m1, op1, complain);
4557 else
4558 gcc_unreachable ();
4560 if (VECTOR_TYPE_P (type0))
4562 if (!COMPARISON_CLASS_P (op0))
4563 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4564 build_zero_cst (type0), complain);
4565 if (!VECTOR_TYPE_P (type1))
4567 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4568 tree z = build_zero_cst (TREE_TYPE (op0));
4569 op1 = build_conditional_expr (location, op1, m1, z, complain);
4571 else if (!COMPARISON_CLASS_P (op1))
4572 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4573 build_zero_cst (type1), complain);
4575 if (code == TRUTH_ANDIF_EXPR)
4576 code = BIT_AND_EXPR;
4577 else if (code == TRUTH_ORIF_EXPR)
4578 code = BIT_IOR_EXPR;
4579 else
4580 gcc_unreachable ();
4582 return cp_build_binary_op (location, code, op0, op1, complain);
4585 result_type = boolean_type_node;
4586 break;
4588 /* Shift operations: result has same type as first operand;
4589 always convert second operand to int.
4590 Also set SHORT_SHIFT if shifting rightward. */
4592 case RSHIFT_EXPR:
4593 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4594 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4596 result_type = type0;
4597 converted = 1;
4599 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4600 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4601 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4602 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4603 TYPE_VECTOR_SUBPARTS (type1)))
4605 result_type = type0;
4606 converted = 1;
4608 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4610 tree const_op1 = fold_non_dependent_expr (op1);
4611 if (TREE_CODE (const_op1) != INTEGER_CST)
4612 const_op1 = op1;
4613 result_type = type0;
4614 doing_shift = true;
4615 if (TREE_CODE (const_op1) == INTEGER_CST)
4617 if (tree_int_cst_lt (const_op1, integer_zero_node))
4619 if ((complain & tf_warning)
4620 && c_inhibit_evaluation_warnings == 0)
4621 warning (OPT_Wshift_count_negative,
4622 "right shift count is negative");
4624 else
4626 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4627 && (complain & tf_warning)
4628 && c_inhibit_evaluation_warnings == 0)
4629 warning (OPT_Wshift_count_overflow,
4630 "right shift count >= width of type");
4633 /* Avoid converting op1 to result_type later. */
4634 converted = 1;
4636 break;
4638 case LSHIFT_EXPR:
4639 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4640 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4642 result_type = type0;
4643 converted = 1;
4645 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4646 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4647 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4648 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4649 TYPE_VECTOR_SUBPARTS (type1)))
4651 result_type = type0;
4652 converted = 1;
4654 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4656 tree const_op0 = fold_non_dependent_expr (op0);
4657 if (TREE_CODE (const_op0) != INTEGER_CST)
4658 const_op0 = op0;
4659 tree const_op1 = fold_non_dependent_expr (op1);
4660 if (TREE_CODE (const_op1) != INTEGER_CST)
4661 const_op1 = op1;
4662 result_type = type0;
4663 doing_shift = true;
4664 if (TREE_CODE (const_op0) == INTEGER_CST
4665 && tree_int_cst_sgn (const_op0) < 0
4666 && (complain & tf_warning)
4667 && c_inhibit_evaluation_warnings == 0)
4668 warning (OPT_Wshift_negative_value,
4669 "left shift of negative value");
4670 if (TREE_CODE (const_op1) == INTEGER_CST)
4672 if (tree_int_cst_lt (const_op1, integer_zero_node))
4674 if ((complain & tf_warning)
4675 && c_inhibit_evaluation_warnings == 0)
4676 warning (OPT_Wshift_count_negative,
4677 "left shift count is negative");
4679 else if (compare_tree_int (const_op1,
4680 TYPE_PRECISION (type0)) >= 0)
4682 if ((complain & tf_warning)
4683 && c_inhibit_evaluation_warnings == 0)
4684 warning (OPT_Wshift_count_overflow,
4685 "left shift count >= width of type");
4687 else if (TREE_CODE (const_op0) == INTEGER_CST
4688 && (complain & tf_warning))
4689 maybe_warn_shift_overflow (location, const_op0, const_op1);
4691 /* Avoid converting op1 to result_type later. */
4692 converted = 1;
4694 break;
4696 case RROTATE_EXPR:
4697 case LROTATE_EXPR:
4698 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4700 result_type = type0;
4701 if (TREE_CODE (op1) == INTEGER_CST)
4703 if (tree_int_cst_lt (op1, integer_zero_node))
4705 if (complain & tf_warning)
4706 warning (0, (code == LROTATE_EXPR)
4707 ? G_("left rotate count is negative")
4708 : G_("right rotate count is negative"));
4710 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4712 if (complain & tf_warning)
4713 warning (0, (code == LROTATE_EXPR)
4714 ? G_("left rotate count >= width of type")
4715 : G_("right rotate count >= width of type"));
4718 /* Convert the shift-count to an integer, regardless of
4719 size of value being shifted. */
4720 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4721 op1 = cp_convert (integer_type_node, op1, complain);
4723 break;
4725 case EQ_EXPR:
4726 case NE_EXPR:
4727 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4728 goto vector_compare;
4729 if ((complain & tf_warning)
4730 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4731 warning (OPT_Wfloat_equal,
4732 "comparing floating point with == or != is unsafe");
4733 if ((complain & tf_warning)
4734 && ((TREE_CODE (orig_op0) == STRING_CST
4735 && !integer_zerop (cp_fully_fold (op1)))
4736 || (TREE_CODE (orig_op1) == STRING_CST
4737 && !integer_zerop (cp_fully_fold (op0)))))
4738 warning (OPT_Waddress, "comparison with string literal results "
4739 "in unspecified behavior");
4741 build_type = boolean_type_node;
4742 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4743 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4744 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4745 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4746 short_compare = 1;
4747 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4748 && null_ptr_cst_p (orig_op1))
4749 /* Handle, eg, (void*)0 (c++/43906), and more. */
4750 || (code0 == POINTER_TYPE
4751 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4753 if (TYPE_PTR_P (type1))
4754 result_type = composite_pointer_type (type0, type1, op0, op1,
4755 CPO_COMPARISON, complain);
4756 else
4757 result_type = type0;
4759 if (char_type_p (TREE_TYPE (orig_op1))
4760 && warning (OPT_Wpointer_compare,
4761 "comparison between pointer and zero character "
4762 "constant"))
4763 inform (input_location,
4764 "did you mean to dereference the pointer?");
4765 warn_for_null_address (location, op0, complain);
4767 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4768 && null_ptr_cst_p (orig_op0))
4769 /* Handle, eg, (void*)0 (c++/43906), and more. */
4770 || (code1 == POINTER_TYPE
4771 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4773 if (TYPE_PTR_P (type0))
4774 result_type = composite_pointer_type (type0, type1, op0, op1,
4775 CPO_COMPARISON, complain);
4776 else
4777 result_type = type1;
4779 if (char_type_p (TREE_TYPE (orig_op0))
4780 && warning (OPT_Wpointer_compare,
4781 "comparison between pointer and zero character "
4782 "constant"))
4783 inform (input_location,
4784 "did you mean to dereference the pointer?");
4785 warn_for_null_address (location, op1, complain);
4787 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4788 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4789 result_type = composite_pointer_type (type0, type1, op0, op1,
4790 CPO_COMPARISON, complain);
4791 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4792 /* One of the operands must be of nullptr_t type. */
4793 result_type = TREE_TYPE (nullptr_node);
4794 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4796 result_type = type0;
4797 if (complain & tf_error)
4798 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4799 else
4800 return error_mark_node;
4802 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4804 result_type = type1;
4805 if (complain & tf_error)
4806 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4807 else
4808 return error_mark_node;
4810 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4812 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4813 == ptrmemfunc_vbit_in_delta)
4815 tree pfn0, delta0, e1, e2;
4817 if (TREE_SIDE_EFFECTS (op0))
4818 op0 = save_expr (op0);
4820 pfn0 = pfn_from_ptrmemfunc (op0);
4821 delta0 = delta_from_ptrmemfunc (op0);
4822 e1 = cp_build_binary_op (location,
4823 EQ_EXPR,
4824 pfn0,
4825 build_zero_cst (TREE_TYPE (pfn0)),
4826 complain);
4827 e2 = cp_build_binary_op (location,
4828 BIT_AND_EXPR,
4829 delta0,
4830 integer_one_node,
4831 complain);
4833 if (complain & tf_warning)
4834 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4836 e2 = cp_build_binary_op (location,
4837 EQ_EXPR, e2, integer_zero_node,
4838 complain);
4839 op0 = cp_build_binary_op (location,
4840 TRUTH_ANDIF_EXPR, e1, e2,
4841 complain);
4842 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4844 else
4846 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4847 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4849 result_type = TREE_TYPE (op0);
4851 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4852 return cp_build_binary_op (location, code, op1, op0, complain);
4853 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4855 tree type;
4856 /* E will be the final comparison. */
4857 tree e;
4858 /* E1 and E2 are for scratch. */
4859 tree e1;
4860 tree e2;
4861 tree pfn0;
4862 tree pfn1;
4863 tree delta0;
4864 tree delta1;
4866 type = composite_pointer_type (type0, type1, op0, op1,
4867 CPO_COMPARISON, complain);
4869 if (!same_type_p (TREE_TYPE (op0), type))
4870 op0 = cp_convert_and_check (type, op0, complain);
4871 if (!same_type_p (TREE_TYPE (op1), type))
4872 op1 = cp_convert_and_check (type, op1, complain);
4874 if (op0 == error_mark_node || op1 == error_mark_node)
4875 return error_mark_node;
4877 if (TREE_SIDE_EFFECTS (op0))
4878 op0 = save_expr (op0);
4879 if (TREE_SIDE_EFFECTS (op1))
4880 op1 = save_expr (op1);
4882 pfn0 = pfn_from_ptrmemfunc (op0);
4883 pfn0 = cp_fully_fold (pfn0);
4884 /* Avoid -Waddress warnings (c++/64877). */
4885 if (TREE_CODE (pfn0) == ADDR_EXPR)
4886 TREE_NO_WARNING (pfn0) = 1;
4887 pfn1 = pfn_from_ptrmemfunc (op1);
4888 pfn1 = cp_fully_fold (pfn1);
4889 delta0 = delta_from_ptrmemfunc (op0);
4890 delta1 = delta_from_ptrmemfunc (op1);
4891 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4892 == ptrmemfunc_vbit_in_delta)
4894 /* We generate:
4896 (op0.pfn == op1.pfn
4897 && ((op0.delta == op1.delta)
4898 || (!op0.pfn && op0.delta & 1 == 0
4899 && op1.delta & 1 == 0))
4901 The reason for the `!op0.pfn' bit is that a NULL
4902 pointer-to-member is any member with a zero PFN and
4903 LSB of the DELTA field is 0. */
4905 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4906 delta0,
4907 integer_one_node,
4908 complain);
4909 e1 = cp_build_binary_op (location,
4910 EQ_EXPR, e1, integer_zero_node,
4911 complain);
4912 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4913 delta1,
4914 integer_one_node,
4915 complain);
4916 e2 = cp_build_binary_op (location,
4917 EQ_EXPR, e2, integer_zero_node,
4918 complain);
4919 e1 = cp_build_binary_op (location,
4920 TRUTH_ANDIF_EXPR, e2, e1,
4921 complain);
4922 e2 = cp_build_binary_op (location, EQ_EXPR,
4923 pfn0,
4924 build_zero_cst (TREE_TYPE (pfn0)),
4925 complain);
4926 e2 = cp_build_binary_op (location,
4927 TRUTH_ANDIF_EXPR, e2, e1, complain);
4928 e1 = cp_build_binary_op (location,
4929 EQ_EXPR, delta0, delta1, complain);
4930 e1 = cp_build_binary_op (location,
4931 TRUTH_ORIF_EXPR, e1, e2, complain);
4933 else
4935 /* We generate:
4937 (op0.pfn == op1.pfn
4938 && (!op0.pfn || op0.delta == op1.delta))
4940 The reason for the `!op0.pfn' bit is that a NULL
4941 pointer-to-member is any member with a zero PFN; the
4942 DELTA field is unspecified. */
4944 e1 = cp_build_binary_op (location,
4945 EQ_EXPR, delta0, delta1, complain);
4946 e2 = cp_build_binary_op (location,
4947 EQ_EXPR,
4948 pfn0,
4949 build_zero_cst (TREE_TYPE (pfn0)),
4950 complain);
4951 e1 = cp_build_binary_op (location,
4952 TRUTH_ORIF_EXPR, e1, e2, complain);
4954 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4955 e = cp_build_binary_op (location,
4956 TRUTH_ANDIF_EXPR, e2, e1, complain);
4957 if (code == EQ_EXPR)
4958 return e;
4959 return cp_build_binary_op (location,
4960 EQ_EXPR, e, integer_zero_node, complain);
4962 else
4964 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4965 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4966 type1));
4967 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4968 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4969 type0));
4972 break;
4974 case MAX_EXPR:
4975 case MIN_EXPR:
4976 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4977 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4978 shorten = 1;
4979 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4980 result_type = composite_pointer_type (type0, type1, op0, op1,
4981 CPO_COMPARISON, complain);
4982 break;
4984 case LE_EXPR:
4985 case GE_EXPR:
4986 case LT_EXPR:
4987 case GT_EXPR:
4988 if (TREE_CODE (orig_op0) == STRING_CST
4989 || TREE_CODE (orig_op1) == STRING_CST)
4991 if (complain & tf_warning)
4992 warning (OPT_Waddress, "comparison with string literal results "
4993 "in unspecified behavior");
4996 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4998 vector_compare:
4999 tree intt;
5000 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5001 TREE_TYPE (type1))
5002 && !vector_types_compatible_elements_p (type0, type1))
5004 if (complain & tf_error)
5006 error_at (location, "comparing vectors with different "
5007 "element types");
5008 inform (location, "operand types are %qT and %qT",
5009 type0, type1);
5011 return error_mark_node;
5014 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5015 TYPE_VECTOR_SUBPARTS (type1)))
5017 if (complain & tf_error)
5019 error_at (location, "comparing vectors with different "
5020 "number of elements");
5021 inform (location, "operand types are %qT and %qT",
5022 type0, type1);
5024 return error_mark_node;
5027 /* It's not precisely specified how the usual arithmetic
5028 conversions apply to the vector types. Here, we use
5029 the unsigned type if one of the operands is signed and
5030 the other one is unsigned. */
5031 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5033 if (!TYPE_UNSIGNED (type0))
5034 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5035 else
5036 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5037 warning_at (location, OPT_Wsign_compare, "comparison between "
5038 "types %qT and %qT", type0, type1);
5041 /* Always construct signed integer vector type. */
5042 intt = c_common_type_for_size
5043 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5044 if (!intt)
5046 if (complain & tf_error)
5047 error_at (location, "could not find an integer type "
5048 "of the same size as %qT", TREE_TYPE (type0));
5049 return error_mark_node;
5051 result_type = build_opaque_vector_type (intt,
5052 TYPE_VECTOR_SUBPARTS (type0));
5053 converted = 1;
5054 return build_vec_cmp (resultcode, result_type, op0, op1);
5056 build_type = boolean_type_node;
5057 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5058 || code0 == ENUMERAL_TYPE)
5059 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5060 || code1 == ENUMERAL_TYPE))
5061 short_compare = 1;
5062 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5063 result_type = composite_pointer_type (type0, type1, op0, op1,
5064 CPO_COMPARISON, complain);
5065 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5067 result_type = type0;
5068 if (extra_warnings && (complain & tf_warning))
5069 warning (OPT_Wextra,
5070 "ordered comparison of pointer with integer zero");
5072 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5074 result_type = type1;
5075 if (extra_warnings && (complain & tf_warning))
5076 warning (OPT_Wextra,
5077 "ordered comparison of pointer with integer zero");
5079 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5080 /* One of the operands must be of nullptr_t type. */
5081 result_type = TREE_TYPE (nullptr_node);
5082 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5084 result_type = type0;
5085 if (complain & tf_error)
5086 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5087 else
5088 return error_mark_node;
5090 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5092 result_type = type1;
5093 if (complain & tf_error)
5094 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5095 else
5096 return error_mark_node;
5099 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5100 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5102 op0 = save_expr (op0);
5103 op1 = save_expr (op1);
5105 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5106 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5109 break;
5111 case UNORDERED_EXPR:
5112 case ORDERED_EXPR:
5113 case UNLT_EXPR:
5114 case UNLE_EXPR:
5115 case UNGT_EXPR:
5116 case UNGE_EXPR:
5117 case UNEQ_EXPR:
5118 build_type = integer_type_node;
5119 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5121 if (complain & tf_error)
5122 error ("unordered comparison on non-floating point argument");
5123 return error_mark_node;
5125 common = 1;
5126 break;
5128 default:
5129 break;
5132 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5133 || code0 == ENUMERAL_TYPE)
5134 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5135 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5136 arithmetic_types_p = 1;
5137 else
5139 arithmetic_types_p = 0;
5140 /* Vector arithmetic is only allowed when both sides are vectors. */
5141 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5143 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5144 || !vector_types_compatible_elements_p (type0, type1))
5146 if (complain & tf_error)
5148 /* "location" already embeds the locations of the
5149 operands, so we don't need to add them separately
5150 to richloc. */
5151 rich_location richloc (line_table, location);
5152 binary_op_error (&richloc, code, type0, type1);
5154 return error_mark_node;
5156 arithmetic_types_p = 1;
5159 /* Determine the RESULT_TYPE, if it is not already known. */
5160 if (!result_type
5161 && arithmetic_types_p
5162 && (shorten || common || short_compare))
5164 result_type = cp_common_type (type0, type1);
5165 if (complain & tf_warning)
5166 do_warn_double_promotion (result_type, type0, type1,
5167 "implicit conversion from %qH to %qI "
5168 "to match other operand of binary "
5169 "expression",
5170 location);
5173 if (!result_type)
5175 if (complain & tf_error)
5176 error_at (location,
5177 "invalid operands of types %qT and %qT to binary %qO",
5178 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5179 return error_mark_node;
5182 /* If we're in a template, the only thing we need to know is the
5183 RESULT_TYPE. */
5184 if (processing_template_decl)
5186 /* Since the middle-end checks the type when doing a build2, we
5187 need to build the tree in pieces. This built tree will never
5188 get out of the front-end as we replace it when instantiating
5189 the template. */
5190 tree tmp = build2 (resultcode,
5191 build_type ? build_type : result_type,
5192 NULL_TREE, op1);
5193 TREE_OPERAND (tmp, 0) = op0;
5194 return tmp;
5197 /* Remember the original type; RESULT_TYPE might be changed later on
5198 by shorten_binary_op. */
5199 tree orig_type = result_type;
5201 if (arithmetic_types_p)
5203 bool first_complex = (code0 == COMPLEX_TYPE);
5204 bool second_complex = (code1 == COMPLEX_TYPE);
5205 int none_complex = (!first_complex && !second_complex);
5207 /* Adapted from patch for c/24581. */
5208 if (first_complex != second_complex
5209 && (code == PLUS_EXPR
5210 || code == MINUS_EXPR
5211 || code == MULT_EXPR
5212 || (code == TRUNC_DIV_EXPR && first_complex))
5213 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5214 && flag_signed_zeros)
5216 /* An operation on mixed real/complex operands must be
5217 handled specially, but the language-independent code can
5218 more easily optimize the plain complex arithmetic if
5219 -fno-signed-zeros. */
5220 tree real_type = TREE_TYPE (result_type);
5221 tree real, imag;
5222 if (first_complex)
5224 if (TREE_TYPE (op0) != result_type)
5225 op0 = cp_convert_and_check (result_type, op0, complain);
5226 if (TREE_TYPE (op1) != real_type)
5227 op1 = cp_convert_and_check (real_type, op1, complain);
5229 else
5231 if (TREE_TYPE (op0) != real_type)
5232 op0 = cp_convert_and_check (real_type, op0, complain);
5233 if (TREE_TYPE (op1) != result_type)
5234 op1 = cp_convert_and_check (result_type, op1, complain);
5236 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5237 return error_mark_node;
5238 if (first_complex)
5240 op0 = save_expr (op0);
5241 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5242 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5243 switch (code)
5245 case MULT_EXPR:
5246 case TRUNC_DIV_EXPR:
5247 op1 = save_expr (op1);
5248 imag = build2 (resultcode, real_type, imag, op1);
5249 /* Fall through. */
5250 case PLUS_EXPR:
5251 case MINUS_EXPR:
5252 real = build2 (resultcode, real_type, real, op1);
5253 break;
5254 default:
5255 gcc_unreachable();
5258 else
5260 op1 = save_expr (op1);
5261 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5262 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5263 switch (code)
5265 case MULT_EXPR:
5266 op0 = save_expr (op0);
5267 imag = build2 (resultcode, real_type, op0, imag);
5268 /* Fall through. */
5269 case PLUS_EXPR:
5270 real = build2 (resultcode, real_type, op0, real);
5271 break;
5272 case MINUS_EXPR:
5273 real = build2 (resultcode, real_type, op0, real);
5274 imag = build1 (NEGATE_EXPR, real_type, imag);
5275 break;
5276 default:
5277 gcc_unreachable();
5280 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5281 return result;
5284 /* For certain operations (which identify themselves by shorten != 0)
5285 if both args were extended from the same smaller type,
5286 do the arithmetic in that type and then extend.
5288 shorten !=0 and !=1 indicates a bitwise operation.
5289 For them, this optimization is safe only if
5290 both args are zero-extended or both are sign-extended.
5291 Otherwise, we might change the result.
5292 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5293 but calculated in (unsigned short) it would be (unsigned short)-1. */
5295 if (shorten && none_complex)
5297 final_type = result_type;
5298 result_type = shorten_binary_op (result_type, op0, op1,
5299 shorten == -1);
5302 /* Comparison operations are shortened too but differently.
5303 They identify themselves by setting short_compare = 1. */
5305 if (short_compare)
5307 /* We call shorten_compare only for diagnostic-reason. */
5308 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5309 xresult_type = result_type;
5310 enum tree_code xresultcode = resultcode;
5311 shorten_compare (location, &xop0, &xop1, &xresult_type,
5312 &xresultcode);
5315 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5316 && warn_sign_compare
5317 /* Do not warn until the template is instantiated; we cannot
5318 bound the ranges of the arguments until that point. */
5319 && !processing_template_decl
5320 && (complain & tf_warning)
5321 && c_inhibit_evaluation_warnings == 0
5322 /* Even unsigned enum types promote to signed int. We don't
5323 want to issue -Wsign-compare warnings for this case. */
5324 && !enum_cast_to_int (orig_op0)
5325 && !enum_cast_to_int (orig_op1))
5327 tree oop0 = maybe_constant_value (orig_op0);
5328 tree oop1 = maybe_constant_value (orig_op1);
5330 if (TREE_CODE (oop0) != INTEGER_CST)
5331 oop0 = cp_fully_fold (orig_op0);
5332 if (TREE_CODE (oop1) != INTEGER_CST)
5333 oop1 = cp_fully_fold (orig_op1);
5334 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5335 result_type, resultcode);
5339 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5340 Then the expression will be built.
5341 It will be given type FINAL_TYPE if that is nonzero;
5342 otherwise, it will be given type RESULT_TYPE. */
5343 if (! converted)
5345 if (TREE_TYPE (op0) != result_type)
5346 op0 = cp_convert_and_check (result_type, op0, complain);
5347 if (TREE_TYPE (op1) != result_type)
5348 op1 = cp_convert_and_check (result_type, op1, complain);
5350 if (op0 == error_mark_node || op1 == error_mark_node)
5351 return error_mark_node;
5354 if (build_type == NULL_TREE)
5355 build_type = result_type;
5357 if (sanitize_flags_p ((SANITIZE_SHIFT
5358 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5359 && current_function_decl != NULL_TREE
5360 && !processing_template_decl
5361 && (doing_div_or_mod || doing_shift))
5363 /* OP0 and/or OP1 might have side-effects. */
5364 op0 = cp_save_expr (op0);
5365 op1 = cp_save_expr (op1);
5366 op0 = fold_non_dependent_expr (op0);
5367 op1 = fold_non_dependent_expr (op1);
5368 if (doing_div_or_mod
5369 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5371 /* For diagnostics we want to use the promoted types without
5372 shorten_binary_op. So convert the arguments to the
5373 original result_type. */
5374 tree cop0 = op0;
5375 tree cop1 = op1;
5376 if (TREE_TYPE (cop0) != orig_type)
5377 cop0 = cp_convert (orig_type, op0, complain);
5378 if (TREE_TYPE (cop1) != orig_type)
5379 cop1 = cp_convert (orig_type, op1, complain);
5380 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5382 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5383 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5386 result = build2_loc (location, resultcode, build_type, op0, op1);
5387 if (final_type != 0)
5388 result = cp_convert (final_type, result, complain);
5390 if (instrument_expr != NULL)
5391 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5392 instrument_expr, result);
5394 if (!processing_template_decl)
5396 op0 = cp_fully_fold (op0);
5397 /* Only consider the second argument if the first isn't overflowed. */
5398 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5399 return result;
5400 op1 = cp_fully_fold (op1);
5401 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5402 return result;
5404 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5405 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5406 return result;
5408 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5409 if (TREE_OVERFLOW_P (result_ovl))
5410 overflow_warning (location, result_ovl);
5412 return result;
5415 /* Build a VEC_PERM_EXPR.
5416 This is a simple wrapper for c_build_vec_perm_expr. */
5417 tree
5418 build_x_vec_perm_expr (location_t loc,
5419 tree arg0, tree arg1, tree arg2,
5420 tsubst_flags_t complain)
5422 tree orig_arg0 = arg0;
5423 tree orig_arg1 = arg1;
5424 tree orig_arg2 = arg2;
5425 if (processing_template_decl)
5427 if (type_dependent_expression_p (arg0)
5428 || type_dependent_expression_p (arg1)
5429 || type_dependent_expression_p (arg2))
5430 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5431 arg0 = build_non_dependent_expr (arg0);
5432 if (arg1)
5433 arg1 = build_non_dependent_expr (arg1);
5434 arg2 = build_non_dependent_expr (arg2);
5436 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5437 if (processing_template_decl && exp != error_mark_node)
5438 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5439 orig_arg1, orig_arg2);
5440 return exp;
5443 /* Return a tree for the sum or difference (RESULTCODE says which)
5444 of pointer PTROP and integer INTOP. */
5446 static tree
5447 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5448 tree intop, tsubst_flags_t complain)
5450 tree res_type = TREE_TYPE (ptrop);
5452 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5453 in certain circumstance (when it's valid to do so). So we need
5454 to make sure it's complete. We don't need to check here, if we
5455 can actually complete it at all, as those checks will be done in
5456 pointer_int_sum() anyway. */
5457 complete_type (TREE_TYPE (res_type));
5459 return pointer_int_sum (loc, resultcode, ptrop,
5460 intop, complain & tf_warning_or_error);
5463 /* Return a tree for the difference of pointers OP0 and OP1.
5464 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5465 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5467 static tree
5468 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5469 tsubst_flags_t complain, tree *instrument_expr)
5471 tree result, inttype;
5472 tree restype = ptrdiff_type_node;
5473 tree target_type = TREE_TYPE (ptrtype);
5475 if (!complete_type_or_else (target_type, NULL_TREE))
5476 return error_mark_node;
5478 if (VOID_TYPE_P (target_type))
5480 if (complain & tf_error)
5481 permerror (loc, "ISO C++ forbids using pointer of "
5482 "type %<void *%> in subtraction");
5483 else
5484 return error_mark_node;
5486 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5488 if (complain & tf_error)
5489 permerror (loc, "ISO C++ forbids using pointer to "
5490 "a function in subtraction");
5491 else
5492 return error_mark_node;
5494 if (TREE_CODE (target_type) == METHOD_TYPE)
5496 if (complain & tf_error)
5497 permerror (loc, "ISO C++ forbids using pointer to "
5498 "a method in subtraction");
5499 else
5500 return error_mark_node;
5503 /* Determine integer type result of the subtraction. This will usually
5504 be the same as the result type (ptrdiff_t), but may need to be a wider
5505 type if pointers for the address space are wider than ptrdiff_t. */
5506 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5507 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5508 else
5509 inttype = restype;
5511 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5513 op0 = save_expr (op0);
5514 op1 = save_expr (op1);
5516 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5517 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5520 /* First do the subtraction, then build the divide operator
5521 and only convert at the very end.
5522 Do not do default conversions in case restype is a short type. */
5524 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5525 pointers. If some platform cannot provide that, or has a larger
5526 ptrdiff_type to support differences larger than half the address
5527 space, cast the pointers to some larger integer type and do the
5528 computations in that type. */
5529 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5530 op0 = cp_build_binary_op (loc,
5531 MINUS_EXPR,
5532 cp_convert (inttype, op0, complain),
5533 cp_convert (inttype, op1, complain),
5534 complain);
5535 else
5536 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5538 /* This generates an error if op1 is a pointer to an incomplete type. */
5539 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5541 if (complain & tf_error)
5542 error_at (loc, "invalid use of a pointer to an incomplete type in "
5543 "pointer arithmetic");
5544 else
5545 return error_mark_node;
5548 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5550 if (complain & tf_error)
5551 error_at (loc, "arithmetic on pointer to an empty aggregate");
5552 else
5553 return error_mark_node;
5556 op1 = (TYPE_PTROB_P (ptrtype)
5557 ? size_in_bytes_loc (loc, target_type)
5558 : integer_one_node);
5560 /* Do the division. */
5562 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5563 cp_convert (inttype, op1, complain));
5564 return cp_convert (restype, result, complain);
5567 /* Construct and perhaps optimize a tree representation
5568 for a unary operation. CODE, a tree_code, specifies the operation
5569 and XARG is the operand. */
5571 tree
5572 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5573 tsubst_flags_t complain)
5575 tree orig_expr = xarg;
5576 tree exp;
5577 int ptrmem = 0;
5578 tree overload = NULL_TREE;
5580 if (processing_template_decl)
5582 if (type_dependent_expression_p (xarg))
5583 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5585 xarg = build_non_dependent_expr (xarg);
5588 exp = NULL_TREE;
5590 /* [expr.unary.op] says:
5592 The address of an object of incomplete type can be taken.
5594 (And is just the ordinary address operator, not an overloaded
5595 "operator &".) However, if the type is a template
5596 specialization, we must complete the type at this point so that
5597 an overloaded "operator &" will be available if required. */
5598 if (code == ADDR_EXPR
5599 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5600 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5601 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5602 || (TREE_CODE (xarg) == OFFSET_REF)))
5603 /* Don't look for a function. */;
5604 else
5605 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5606 NULL_TREE, &overload, complain);
5608 if (!exp && code == ADDR_EXPR)
5610 if (is_overloaded_fn (xarg))
5612 tree fn = get_first_fn (xarg);
5613 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5615 if (complain & tf_error)
5616 error (DECL_CONSTRUCTOR_P (fn)
5617 ? G_("taking address of constructor %qD")
5618 : G_("taking address of destructor %qD"),
5619 fn);
5620 return error_mark_node;
5624 /* A pointer to member-function can be formed only by saying
5625 &X::mf. */
5626 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5627 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5629 if (TREE_CODE (xarg) != OFFSET_REF
5630 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5632 if (complain & tf_error)
5634 error ("invalid use of %qE to form a "
5635 "pointer-to-member-function", xarg.get_value ());
5636 if (TREE_CODE (xarg) != OFFSET_REF)
5637 inform (input_location, " a qualified-id is required");
5639 return error_mark_node;
5641 else
5643 if (complain & tf_error)
5644 error ("parentheses around %qE cannot be used to form a"
5645 " pointer-to-member-function",
5646 xarg.get_value ());
5647 else
5648 return error_mark_node;
5649 PTRMEM_OK_P (xarg) = 1;
5653 if (TREE_CODE (xarg) == OFFSET_REF)
5655 ptrmem = PTRMEM_OK_P (xarg);
5657 if (!ptrmem && !flag_ms_extensions
5658 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5660 /* A single non-static member, make sure we don't allow a
5661 pointer-to-member. */
5662 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5663 TREE_OPERAND (xarg, 0),
5664 ovl_make (TREE_OPERAND (xarg, 1)));
5665 PTRMEM_OK_P (xarg) = ptrmem;
5669 exp = cp_build_addr_expr_strict (xarg, complain);
5672 if (processing_template_decl && exp != error_mark_node)
5674 if (overload != NULL_TREE)
5675 return (build_min_non_dep_op_overload
5676 (code, exp, overload, orig_expr, integer_zero_node));
5678 exp = build_min_non_dep (code, exp, orig_expr,
5679 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5681 if (TREE_CODE (exp) == ADDR_EXPR)
5682 PTRMEM_OK_P (exp) = ptrmem;
5683 return exp;
5686 /* Construct and perhaps optimize a tree representation
5687 for __builtin_addressof operation. ARG specifies the operand. */
5689 tree
5690 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5692 tree orig_expr = arg;
5694 if (processing_template_decl)
5696 if (type_dependent_expression_p (arg))
5697 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5699 arg = build_non_dependent_expr (arg);
5702 tree exp = cp_build_addr_expr_strict (arg, complain);
5704 if (processing_template_decl && exp != error_mark_node)
5705 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5706 return exp;
5709 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5710 constants, where a null value is represented by an INTEGER_CST of
5711 -1. */
5713 tree
5714 cp_truthvalue_conversion (tree expr)
5716 tree type = TREE_TYPE (expr);
5717 if (TYPE_PTR_OR_PTRMEM_P (type)
5718 /* Avoid ICE on invalid use of non-static member function. */
5719 || TREE_CODE (expr) == FUNCTION_DECL)
5720 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5721 else
5722 return c_common_truthvalue_conversion (input_location, expr);
5725 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5727 tree
5728 condition_conversion (tree expr)
5730 tree t;
5731 /* Anything that might happen in a template should go through
5732 maybe_convert_cond. */
5733 gcc_assert (!processing_template_decl);
5734 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5735 tf_warning_or_error, LOOKUP_NORMAL);
5736 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5737 return t;
5740 /* Returns the address of T. This function will fold away
5741 ADDR_EXPR of INDIRECT_REF. */
5743 tree
5744 build_address (tree t)
5746 if (error_operand_p (t) || !cxx_mark_addressable (t))
5747 return error_mark_node;
5748 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5749 || processing_template_decl);
5750 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5751 if (TREE_CODE (t) != ADDR_EXPR)
5752 t = rvalue (t);
5753 return t;
5756 /* Return a NOP_EXPR converting EXPR to TYPE. */
5758 tree
5759 build_nop (tree type, tree expr)
5761 if (type == error_mark_node || error_operand_p (expr))
5762 return expr;
5763 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5766 /* Take the address of ARG, whatever that means under C++ semantics.
5767 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5768 and class rvalues as well.
5770 Nothing should call this function directly; instead, callers should use
5771 cp_build_addr_expr or cp_build_addr_expr_strict. */
5773 static tree
5774 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5776 tree argtype;
5777 tree val;
5779 if (!arg || error_operand_p (arg))
5780 return error_mark_node;
5782 arg = mark_lvalue_use (arg);
5783 if (error_operand_p (arg))
5784 return error_mark_node;
5786 argtype = lvalue_type (arg);
5788 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5790 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5791 && !really_overloaded_fn (arg))
5793 /* They're trying to take the address of a unique non-static
5794 member function. This is ill-formed (except in MS-land),
5795 but let's try to DTRT.
5796 Note: We only handle unique functions here because we don't
5797 want to complain if there's a static overload; non-unique
5798 cases will be handled by instantiate_type. But we need to
5799 handle this case here to allow casts on the resulting PMF.
5800 We could defer this in non-MS mode, but it's easier to give
5801 a useful error here. */
5803 /* Inside constant member functions, the `this' pointer
5804 contains an extra const qualifier. TYPE_MAIN_VARIANT
5805 is used here to remove this const from the diagnostics
5806 and the created OFFSET_REF. */
5807 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5808 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5809 if (!mark_used (fn, complain) && !(complain & tf_error))
5810 return error_mark_node;
5812 if (! flag_ms_extensions)
5814 tree name = DECL_NAME (fn);
5815 if (!(complain & tf_error))
5816 return error_mark_node;
5817 else if (current_class_type
5818 && TREE_OPERAND (arg, 0) == current_class_ref)
5819 /* An expression like &memfn. */
5820 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5821 " or parenthesized non-static member function to form"
5822 " a pointer to member function. Say %<&%T::%D%>",
5823 base, name);
5824 else
5825 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5826 " function to form a pointer to member function."
5827 " Say %<&%T::%D%>",
5828 base, name);
5830 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5833 /* Uninstantiated types are all functions. Taking the
5834 address of a function is a no-op, so just return the
5835 argument. */
5836 if (type_unknown_p (arg))
5837 return build1 (ADDR_EXPR, unknown_type_node, arg);
5839 if (TREE_CODE (arg) == OFFSET_REF)
5840 /* We want a pointer to member; bypass all the code for actually taking
5841 the address of something. */
5842 goto offset_ref;
5844 /* Anything not already handled and not a true memory reference
5845 is an error. */
5846 if (TREE_CODE (argtype) != FUNCTION_TYPE
5847 && TREE_CODE (argtype) != METHOD_TYPE)
5849 cp_lvalue_kind kind = lvalue_kind (arg);
5850 if (kind == clk_none)
5852 if (complain & tf_error)
5853 lvalue_error (input_location, lv_addressof);
5854 return error_mark_node;
5856 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5858 if (!(complain & tf_error))
5859 return error_mark_node;
5860 if (kind & clk_class)
5861 /* Make this a permerror because we used to accept it. */
5862 permerror (input_location, "taking address of temporary");
5863 else
5864 error ("taking address of xvalue (rvalue reference)");
5868 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5870 tree type = build_pointer_type (TREE_TYPE (argtype));
5871 arg = build1 (CONVERT_EXPR, type, arg);
5872 return arg;
5874 else if (pedantic && DECL_MAIN_P (arg))
5876 /* ARM $3.4 */
5877 /* Apparently a lot of autoconf scripts for C++ packages do this,
5878 so only complain if -Wpedantic. */
5879 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5880 pedwarn (input_location, OPT_Wpedantic,
5881 "ISO C++ forbids taking address of function %<::main%>");
5882 else if (flag_pedantic_errors)
5883 return error_mark_node;
5886 /* Let &* cancel out to simplify resulting code. */
5887 if (INDIRECT_REF_P (arg))
5889 arg = TREE_OPERAND (arg, 0);
5890 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5892 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5893 arg = build1 (CONVERT_EXPR, type, arg);
5895 else
5896 /* Don't let this be an lvalue. */
5897 arg = rvalue (arg);
5898 return arg;
5901 /* Handle complex lvalues (when permitted)
5902 by reduction to simpler cases. */
5903 val = unary_complex_lvalue (ADDR_EXPR, arg);
5904 if (val != 0)
5905 return val;
5907 switch (TREE_CODE (arg))
5909 CASE_CONVERT:
5910 case FLOAT_EXPR:
5911 case FIX_TRUNC_EXPR:
5912 /* We should have handled this above in the lvalue_kind check. */
5913 gcc_unreachable ();
5914 break;
5916 case BASELINK:
5917 arg = BASELINK_FUNCTIONS (arg);
5918 /* Fall through. */
5920 case OVERLOAD:
5921 arg = OVL_FIRST (arg);
5922 break;
5924 case OFFSET_REF:
5925 offset_ref:
5926 /* Turn a reference to a non-static data member into a
5927 pointer-to-member. */
5929 tree type;
5930 tree t;
5932 gcc_assert (PTRMEM_OK_P (arg));
5934 t = TREE_OPERAND (arg, 1);
5935 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5937 if (complain & tf_error)
5938 error ("cannot create pointer to reference member %qD", t);
5939 return error_mark_node;
5942 type = build_ptrmem_type (context_for_name_lookup (t),
5943 TREE_TYPE (t));
5944 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5945 return t;
5948 default:
5949 break;
5952 if (argtype != error_mark_node)
5953 argtype = build_pointer_type (argtype);
5955 if (bitfield_p (arg))
5957 if (complain & tf_error)
5958 error ("attempt to take address of bit-field");
5959 return error_mark_node;
5962 /* In a template, we are processing a non-dependent expression
5963 so we can just form an ADDR_EXPR with the correct type. */
5964 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5966 if (TREE_CODE (arg) == FUNCTION_DECL
5967 && !mark_used (arg, complain) && !(complain & tf_error))
5968 return error_mark_node;
5969 val = build_address (arg);
5970 if (TREE_CODE (arg) == OFFSET_REF)
5971 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5973 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5975 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5977 /* We can only get here with a single static member
5978 function. */
5979 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5980 && DECL_STATIC_FUNCTION_P (fn));
5981 if (!mark_used (fn, complain) && !(complain & tf_error))
5982 return error_mark_node;
5983 val = build_address (fn);
5984 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5985 /* Do not lose object's side effects. */
5986 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5987 TREE_OPERAND (arg, 0), val);
5989 else
5991 tree object = TREE_OPERAND (arg, 0);
5992 tree field = TREE_OPERAND (arg, 1);
5993 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5994 (TREE_TYPE (object), decl_type_context (field)));
5995 val = build_address (arg);
5998 if (TYPE_PTR_P (argtype)
5999 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
6001 build_ptrmemfunc_type (argtype);
6002 val = build_ptrmemfunc (argtype, val, 0,
6003 /*c_cast_p=*/false,
6004 complain);
6007 return val;
6010 /* Take the address of ARG if it has one, even if it's an rvalue. */
6012 tree
6013 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
6015 return cp_build_addr_expr_1 (arg, 0, complain);
6018 /* Take the address of ARG, but only if it's an lvalue. */
6020 static tree
6021 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
6023 return cp_build_addr_expr_1 (arg, 1, complain);
6026 /* C++: Must handle pointers to members.
6028 Perhaps type instantiation should be extended to handle conversion
6029 from aggregates to types we don't yet know we want? (Or are those
6030 cases typically errors which should be reported?)
6032 NOCONVERT suppresses the default promotions (such as from short to int). */
6034 tree
6035 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
6036 tsubst_flags_t complain)
6038 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6039 tree arg = xarg;
6040 location_t location = EXPR_LOC_OR_LOC (arg, input_location);
6041 tree argtype = 0;
6042 const char *errstring = NULL;
6043 tree val;
6044 const char *invalid_op_diag;
6046 if (!arg || error_operand_p (arg))
6047 return error_mark_node;
6049 if ((invalid_op_diag
6050 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
6051 ? CONVERT_EXPR
6052 : code),
6053 TREE_TYPE (xarg))))
6055 if (complain & tf_error)
6056 error (invalid_op_diag);
6057 return error_mark_node;
6060 switch (code)
6062 case UNARY_PLUS_EXPR:
6063 case NEGATE_EXPR:
6065 int flags = WANT_ARITH | WANT_ENUM;
6066 /* Unary plus (but not unary minus) is allowed on pointers. */
6067 if (code == UNARY_PLUS_EXPR)
6068 flags |= WANT_POINTER;
6069 arg = build_expr_type_conversion (flags, arg, true);
6070 if (!arg)
6071 errstring = (code == NEGATE_EXPR
6072 ? _("wrong type argument to unary minus")
6073 : _("wrong type argument to unary plus"));
6074 else
6076 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6077 arg = cp_perform_integral_promotions (arg, complain);
6079 /* Make sure the result is not an lvalue: a unary plus or minus
6080 expression is always a rvalue. */
6081 arg = rvalue (arg);
6084 break;
6086 case BIT_NOT_EXPR:
6087 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6089 code = CONJ_EXPR;
6090 if (!noconvert)
6092 arg = cp_default_conversion (arg, complain);
6093 if (arg == error_mark_node)
6094 return error_mark_node;
6097 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
6098 | WANT_VECTOR_OR_COMPLEX,
6099 arg, true)))
6100 errstring = _("wrong type argument to bit-complement");
6101 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
6103 /* Warn if the expression has boolean value. */
6104 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
6105 && (complain & tf_warning)
6106 && warning_at (location, OPT_Wbool_operation,
6107 "%<~%> on an expression of type bool"))
6108 inform (location, "did you mean to use logical not (%<!%>)?");
6109 arg = cp_perform_integral_promotions (arg, complain);
6111 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
6112 arg = mark_rvalue_use (arg);
6113 break;
6115 case ABS_EXPR:
6116 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6117 errstring = _("wrong type argument to abs");
6118 else if (!noconvert)
6120 arg = cp_default_conversion (arg, complain);
6121 if (arg == error_mark_node)
6122 return error_mark_node;
6124 break;
6126 case CONJ_EXPR:
6127 /* Conjugating a real value is a no-op, but allow it anyway. */
6128 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6129 errstring = _("wrong type argument to conjugation");
6130 else if (!noconvert)
6132 arg = cp_default_conversion (arg, complain);
6133 if (arg == error_mark_node)
6134 return error_mark_node;
6136 break;
6138 case TRUTH_NOT_EXPR:
6139 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6140 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6141 build_zero_cst (TREE_TYPE (arg)), complain);
6142 arg = perform_implicit_conversion (boolean_type_node, arg,
6143 complain);
6144 val = invert_truthvalue_loc (input_location, arg);
6145 if (arg != error_mark_node)
6146 return val;
6147 errstring = _("in argument to unary !");
6148 break;
6150 case NOP_EXPR:
6151 break;
6153 case REALPART_EXPR:
6154 case IMAGPART_EXPR:
6155 arg = build_real_imag_expr (input_location, code, arg);
6156 return arg;
6158 case PREINCREMENT_EXPR:
6159 case POSTINCREMENT_EXPR:
6160 case PREDECREMENT_EXPR:
6161 case POSTDECREMENT_EXPR:
6162 /* Handle complex lvalues (when permitted)
6163 by reduction to simpler cases. */
6165 val = unary_complex_lvalue (code, arg);
6166 if (val != 0)
6167 return val;
6169 arg = mark_lvalue_use (arg);
6171 /* Increment or decrement the real part of the value,
6172 and don't change the imaginary part. */
6173 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6175 tree real, imag;
6177 arg = cp_stabilize_reference (arg);
6178 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6179 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6180 real = cp_build_unary_op (code, real, true, complain);
6181 if (real == error_mark_node || imag == error_mark_node)
6182 return error_mark_node;
6183 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6184 real, imag);
6187 /* Report invalid types. */
6189 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6190 arg, true)))
6192 if (code == PREINCREMENT_EXPR)
6193 errstring = _("no pre-increment operator for type");
6194 else if (code == POSTINCREMENT_EXPR)
6195 errstring = _("no post-increment operator for type");
6196 else if (code == PREDECREMENT_EXPR)
6197 errstring = _("no pre-decrement operator for type");
6198 else
6199 errstring = _("no post-decrement operator for type");
6200 break;
6202 else if (arg == error_mark_node)
6203 return error_mark_node;
6205 /* Report something read-only. */
6207 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6208 || TREE_READONLY (arg))
6210 if (complain & tf_error)
6211 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6212 || code == POSTINCREMENT_EXPR)
6213 ? lv_increment : lv_decrement));
6214 else
6215 return error_mark_node;
6219 tree inc;
6220 tree declared_type = unlowered_expr_type (arg);
6222 argtype = TREE_TYPE (arg);
6224 /* ARM $5.2.5 last annotation says this should be forbidden. */
6225 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6227 if (complain & tf_error)
6228 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6229 ? G_("ISO C++ forbids incrementing an enum")
6230 : G_("ISO C++ forbids decrementing an enum"));
6231 else
6232 return error_mark_node;
6235 /* Compute the increment. */
6237 if (TYPE_PTR_P (argtype))
6239 tree type = complete_type (TREE_TYPE (argtype));
6241 if (!COMPLETE_OR_VOID_TYPE_P (type))
6243 if (complain & tf_error)
6244 error (((code == PREINCREMENT_EXPR
6245 || code == POSTINCREMENT_EXPR))
6246 ? G_("cannot increment a pointer to incomplete type %qT")
6247 : G_("cannot decrement a pointer to incomplete type %qT"),
6248 TREE_TYPE (argtype));
6249 else
6250 return error_mark_node;
6252 else if (!TYPE_PTROB_P (argtype))
6254 if (complain & tf_error)
6255 pedwarn (input_location, OPT_Wpointer_arith,
6256 (code == PREINCREMENT_EXPR
6257 || code == POSTINCREMENT_EXPR)
6258 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6259 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6260 argtype);
6261 else
6262 return error_mark_node;
6265 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6267 else
6268 inc = VECTOR_TYPE_P (argtype)
6269 ? build_one_cst (argtype)
6270 : integer_one_node;
6272 inc = cp_convert (argtype, inc, complain);
6274 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6275 need to ask Objective-C to build the increment or decrement
6276 expression for it. */
6277 if (objc_is_property_ref (arg))
6278 return objc_build_incr_expr_for_property_ref (input_location, code,
6279 arg, inc);
6281 /* Complain about anything else that is not a true lvalue. */
6282 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6283 || code == POSTINCREMENT_EXPR)
6284 ? lv_increment : lv_decrement),
6285 complain))
6286 return error_mark_node;
6288 /* Forbid using -- or ++ in C++17 on `bool'. */
6289 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6291 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6293 if (complain & tf_error)
6294 error ("use of an operand of type %qT in %<operator--%> "
6295 "is forbidden", boolean_type_node);
6296 return error_mark_node;
6298 else
6300 if (cxx_dialect >= cxx17)
6302 if (complain & tf_error)
6303 error ("use of an operand of type %qT in "
6304 "%<operator++%> is forbidden in C++17",
6305 boolean_type_node);
6306 return error_mark_node;
6308 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6309 else if (!in_system_header_at (input_location))
6310 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6311 "in %<operator++%> is deprecated",
6312 boolean_type_node);
6314 val = boolean_increment (code, arg);
6316 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6317 /* An rvalue has no cv-qualifiers. */
6318 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6319 else
6320 val = build2 (code, TREE_TYPE (arg), arg, inc);
6322 TREE_SIDE_EFFECTS (val) = 1;
6323 return val;
6326 case ADDR_EXPR:
6327 /* Note that this operation never does default_conversion
6328 regardless of NOCONVERT. */
6329 return cp_build_addr_expr (arg, complain);
6331 default:
6332 break;
6335 if (!errstring)
6337 if (argtype == 0)
6338 argtype = TREE_TYPE (arg);
6339 return build1 (code, argtype, arg);
6342 if (complain & tf_error)
6343 error ("%s", errstring);
6344 return error_mark_node;
6347 /* Hook for the c-common bits that build a unary op. */
6348 tree
6349 build_unary_op (location_t /*location*/,
6350 enum tree_code code, tree xarg, bool noconvert)
6352 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6355 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6356 so that it is a valid lvalue even for GENERIC by replacing
6357 (lhs = rhs) with ((lhs = rhs), lhs)
6358 (--lhs) with ((--lhs), lhs)
6359 (++lhs) with ((++lhs), lhs)
6360 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6361 that it can be evaluated multiple times. */
6363 tree
6364 genericize_compound_lvalue (tree lvalue)
6366 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
6367 lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
6368 cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
6369 TREE_OPERAND (lvalue, 1));
6370 return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
6371 lvalue, TREE_OPERAND (lvalue, 0));
6374 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6375 for certain kinds of expressions which are not really lvalues
6376 but which we can accept as lvalues.
6378 If ARG is not a kind of expression we can handle, return
6379 NULL_TREE. */
6381 tree
6382 unary_complex_lvalue (enum tree_code code, tree arg)
6384 /* Inside a template, making these kinds of adjustments is
6385 pointless; we are only concerned with the type of the
6386 expression. */
6387 if (processing_template_decl)
6388 return NULL_TREE;
6390 /* Handle (a, b) used as an "lvalue". */
6391 if (TREE_CODE (arg) == COMPOUND_EXPR)
6393 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6394 tf_warning_or_error);
6395 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6396 TREE_OPERAND (arg, 0), real_result);
6399 /* Handle (a ? b : c) used as an "lvalue". */
6400 if (TREE_CODE (arg) == COND_EXPR
6401 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6402 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6404 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6405 if (TREE_CODE (arg) == MODIFY_EXPR
6406 || TREE_CODE (arg) == PREINCREMENT_EXPR
6407 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6408 return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
6410 if (code != ADDR_EXPR)
6411 return NULL_TREE;
6413 /* Handle (a = b) used as an "lvalue" for `&'. */
6414 if (TREE_CODE (arg) == MODIFY_EXPR
6415 || TREE_CODE (arg) == INIT_EXPR)
6417 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6418 tf_warning_or_error);
6419 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6420 arg, real_result);
6421 TREE_NO_WARNING (arg) = 1;
6422 return arg;
6425 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6426 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6427 || TREE_CODE (arg) == OFFSET_REF)
6428 return NULL_TREE;
6430 /* We permit compiler to make function calls returning
6431 objects of aggregate type look like lvalues. */
6433 tree targ = arg;
6435 if (TREE_CODE (targ) == SAVE_EXPR)
6436 targ = TREE_OPERAND (targ, 0);
6438 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6440 if (TREE_CODE (arg) == SAVE_EXPR)
6441 targ = arg;
6442 else
6443 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6444 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6447 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6448 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6449 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6452 /* Don't let anything else be handled specially. */
6453 return NULL_TREE;
6456 /* Mark EXP saying that we need to be able to take the
6457 address of it; it should not be allocated in a register.
6458 Value is true if successful. ARRAY_REF_P is true if this
6459 is for ARRAY_REF construction - in that case we don't want
6460 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6461 it is fine to use ARRAY_REFs for vector subscripts on vector
6462 register variables.
6464 C++: we do not allow `current_class_ptr' to be addressable. */
6466 bool
6467 cxx_mark_addressable (tree exp, bool array_ref_p)
6469 tree x = exp;
6471 while (1)
6472 switch (TREE_CODE (x))
6474 case VIEW_CONVERT_EXPR:
6475 if (array_ref_p
6476 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6477 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6478 return true;
6479 /* FALLTHRU */
6480 case ADDR_EXPR:
6481 case COMPONENT_REF:
6482 case ARRAY_REF:
6483 case REALPART_EXPR:
6484 case IMAGPART_EXPR:
6485 x = TREE_OPERAND (x, 0);
6486 break;
6488 case PARM_DECL:
6489 if (x == current_class_ptr)
6491 error ("cannot take the address of %<this%>, which is an rvalue expression");
6492 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6493 return true;
6495 /* Fall through. */
6497 case VAR_DECL:
6498 /* Caller should not be trying to mark initialized
6499 constant fields addressable. */
6500 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6501 || DECL_IN_AGGR_P (x) == 0
6502 || TREE_STATIC (x)
6503 || DECL_EXTERNAL (x));
6504 /* Fall through. */
6506 case RESULT_DECL:
6507 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6508 && !DECL_ARTIFICIAL (x))
6510 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6512 error
6513 ("address of explicit register variable %qD requested", x);
6514 return false;
6516 else if (extra_warnings)
6517 warning
6518 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6520 TREE_ADDRESSABLE (x) = 1;
6521 return true;
6523 case CONST_DECL:
6524 case FUNCTION_DECL:
6525 TREE_ADDRESSABLE (x) = 1;
6526 return true;
6528 case CONSTRUCTOR:
6529 TREE_ADDRESSABLE (x) = 1;
6530 return true;
6532 case TARGET_EXPR:
6533 TREE_ADDRESSABLE (x) = 1;
6534 cxx_mark_addressable (TREE_OPERAND (x, 0));
6535 return true;
6537 default:
6538 return true;
6542 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6544 tree
6545 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6546 tsubst_flags_t complain)
6548 tree orig_ifexp = ifexp;
6549 tree orig_op1 = op1;
6550 tree orig_op2 = op2;
6551 tree expr;
6553 if (processing_template_decl)
6555 /* The standard says that the expression is type-dependent if
6556 IFEXP is type-dependent, even though the eventual type of the
6557 expression doesn't dependent on IFEXP. */
6558 if (type_dependent_expression_p (ifexp)
6559 /* As a GNU extension, the middle operand may be omitted. */
6560 || (op1 && type_dependent_expression_p (op1))
6561 || type_dependent_expression_p (op2))
6562 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6563 ifexp = build_non_dependent_expr (ifexp);
6564 if (op1)
6565 op1 = build_non_dependent_expr (op1);
6566 op2 = build_non_dependent_expr (op2);
6569 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6570 if (processing_template_decl && expr != error_mark_node)
6572 tree min = build_min_non_dep (COND_EXPR, expr,
6573 orig_ifexp, orig_op1, orig_op2);
6574 expr = convert_from_reference (min);
6576 return expr;
6579 /* Given a list of expressions, return a compound expression
6580 that performs them all and returns the value of the last of them. */
6582 tree
6583 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6584 tsubst_flags_t complain)
6586 tree expr = TREE_VALUE (list);
6588 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6589 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6591 if (complain & tf_error)
6592 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6593 "list-initializer for non-class type must not "
6594 "be parenthesized");
6595 else
6596 return error_mark_node;
6599 if (TREE_CHAIN (list))
6601 if (complain & tf_error)
6602 switch (exp)
6604 case ELK_INIT:
6605 permerror (input_location, "expression list treated as compound "
6606 "expression in initializer");
6607 break;
6608 case ELK_MEM_INIT:
6609 permerror (input_location, "expression list treated as compound "
6610 "expression in mem-initializer");
6611 break;
6612 case ELK_FUNC_CAST:
6613 permerror (input_location, "expression list treated as compound "
6614 "expression in functional cast");
6615 break;
6616 default:
6617 gcc_unreachable ();
6619 else
6620 return error_mark_node;
6622 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6623 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6624 expr, TREE_VALUE (list), complain);
6627 return expr;
6630 /* Like build_x_compound_expr_from_list, but using a VEC. */
6632 tree
6633 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6634 tsubst_flags_t complain)
6636 if (vec_safe_is_empty (vec))
6637 return NULL_TREE;
6638 else if (vec->length () == 1)
6639 return (*vec)[0];
6640 else
6642 tree expr;
6643 unsigned int ix;
6644 tree t;
6646 if (msg != NULL)
6648 if (complain & tf_error)
6649 permerror (input_location,
6650 "%s expression list treated as compound expression",
6651 msg);
6652 else
6653 return error_mark_node;
6656 expr = (*vec)[0];
6657 for (ix = 1; vec->iterate (ix, &t); ++ix)
6658 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6659 t, complain);
6661 return expr;
6665 /* Handle overloading of the ',' operator when needed. */
6667 tree
6668 build_x_compound_expr (location_t loc, tree op1, tree op2,
6669 tsubst_flags_t complain)
6671 tree result;
6672 tree orig_op1 = op1;
6673 tree orig_op2 = op2;
6674 tree overload = NULL_TREE;
6676 if (processing_template_decl)
6678 if (type_dependent_expression_p (op1)
6679 || type_dependent_expression_p (op2))
6680 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6681 op1 = build_non_dependent_expr (op1);
6682 op2 = build_non_dependent_expr (op2);
6685 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6686 NULL_TREE, &overload, complain);
6687 if (!result)
6688 result = cp_build_compound_expr (op1, op2, complain);
6690 if (processing_template_decl && result != error_mark_node)
6692 if (overload != NULL_TREE)
6693 return (build_min_non_dep_op_overload
6694 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6696 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6699 return result;
6702 /* Like cp_build_compound_expr, but for the c-common bits. */
6704 tree
6705 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6707 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6710 /* Build a compound expression. */
6712 tree
6713 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6715 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6717 if (lhs == error_mark_node || rhs == error_mark_node)
6718 return error_mark_node;
6720 if (TREE_CODE (rhs) == TARGET_EXPR)
6722 /* If the rhs is a TARGET_EXPR, then build the compound
6723 expression inside the target_expr's initializer. This
6724 helps the compiler to eliminate unnecessary temporaries. */
6725 tree init = TREE_OPERAND (rhs, 1);
6727 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6728 TREE_OPERAND (rhs, 1) = init;
6730 return rhs;
6733 if (type_unknown_p (rhs))
6735 if (complain & tf_error)
6736 error ("no context to resolve type of %qE", rhs);
6737 return error_mark_node;
6740 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6743 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6744 casts away constness. CAST gives the type of cast. Returns true
6745 if the cast is ill-formed, false if it is well-formed.
6747 ??? This function warns for casting away any qualifier not just
6748 const. We would like to specify exactly what qualifiers are casted
6749 away.
6752 static bool
6753 check_for_casting_away_constness (tree src_type, tree dest_type,
6754 enum tree_code cast, tsubst_flags_t complain)
6756 /* C-style casts are allowed to cast away constness. With
6757 WARN_CAST_QUAL, we still want to issue a warning. */
6758 if (cast == CAST_EXPR && !warn_cast_qual)
6759 return false;
6761 if (!casts_away_constness (src_type, dest_type, complain))
6762 return false;
6764 switch (cast)
6766 case CAST_EXPR:
6767 if (complain & tf_warning)
6768 warning (OPT_Wcast_qual,
6769 "cast from type %qT to type %qT casts away qualifiers",
6770 src_type, dest_type);
6771 return false;
6773 case STATIC_CAST_EXPR:
6774 if (complain & tf_error)
6775 error ("static_cast from type %qT to type %qT casts away qualifiers",
6776 src_type, dest_type);
6777 return true;
6779 case REINTERPRET_CAST_EXPR:
6780 if (complain & tf_error)
6781 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6782 src_type, dest_type);
6783 return true;
6785 default:
6786 gcc_unreachable();
6790 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6791 void
6792 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6794 if (warn_useless_cast
6795 && complain & tf_warning)
6797 if ((TREE_CODE (type) == REFERENCE_TYPE
6798 && (TYPE_REF_IS_RVALUE (type)
6799 ? xvalue_p (expr) : lvalue_p (expr))
6800 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6801 || same_type_p (TREE_TYPE (expr), type))
6802 warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6806 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6807 void
6808 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6810 if (warn_ignored_qualifiers
6811 && complain & tf_warning
6812 && !CLASS_TYPE_P (type)
6813 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6815 warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6816 "result type");
6820 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6821 (another pointer-to-member type in the same hierarchy) and return
6822 the converted expression. If ALLOW_INVERSE_P is permitted, a
6823 pointer-to-derived may be converted to pointer-to-base; otherwise,
6824 only the other direction is permitted. If C_CAST_P is true, this
6825 conversion is taking place as part of a C-style cast. */
6827 tree
6828 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6829 bool c_cast_p, tsubst_flags_t complain)
6831 if (same_type_p (type, TREE_TYPE (expr)))
6832 return expr;
6834 if (TYPE_PTRDATAMEM_P (type))
6836 tree delta = (get_delta_difference
6837 (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6838 TYPE_PTRMEM_CLASS_TYPE (type),
6839 allow_inverse_p, c_cast_p, complain));
6841 if (delta == error_mark_node)
6842 return error_mark_node;
6844 if (!integer_zerop (delta))
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 (TREE_CODE (type) == REFERENCE_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 (TREE_CODE (type) == REFERENCE_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 (TREE_CODE (type) != REFERENCE_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 (TREE_CODE (type) != REFERENCE_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 An lvalue 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 (TREE_CODE (type) == REFERENCE_TYPE)
7323 if (! lvalue_p (expr))
7325 if (complain & tf_error)
7326 error ("invalid cast of an rvalue expression of type "
7327 "%qT to type %qT",
7328 intype, type);
7329 return error_mark_node;
7332 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7333 "B" are related class types; the reinterpret_cast does not
7334 adjust the pointer. */
7335 if (TYPE_PTR_P (intype)
7336 && (complain & tf_warning)
7337 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7338 COMPARE_BASE | COMPARE_DERIVED)))
7339 warning (0, "casting %qT to %qT does not dereference pointer",
7340 intype, type);
7342 expr = cp_build_addr_expr (expr, complain);
7344 if (warn_strict_aliasing > 2)
7345 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7347 if (expr != error_mark_node)
7348 expr = build_reinterpret_cast_1
7349 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7350 valid_p, complain);
7351 if (expr != error_mark_node)
7352 /* cp_build_indirect_ref isn't right for rvalue refs. */
7353 expr = convert_from_reference (fold_convert (type, expr));
7354 return expr;
7357 /* As a G++ extension, we consider conversions from member
7358 functions, and pointers to member functions to
7359 pointer-to-function and pointer-to-void types. If
7360 -Wno-pmf-conversions has not been specified,
7361 convert_member_func_to_ptr will issue an error message. */
7362 if ((TYPE_PTRMEMFUNC_P (intype)
7363 || TREE_CODE (intype) == METHOD_TYPE)
7364 && TYPE_PTR_P (type)
7365 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7366 || VOID_TYPE_P (TREE_TYPE (type))))
7367 return convert_member_func_to_ptr (type, expr, complain);
7369 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7370 array-to-pointer, and function-to-pointer conversions are
7371 performed. */
7372 expr = decay_conversion (expr, complain);
7374 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7375 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7376 if (TREE_CODE (expr) == NOP_EXPR
7377 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7378 expr = TREE_OPERAND (expr, 0);
7380 if (error_operand_p (expr))
7381 return error_mark_node;
7383 intype = TREE_TYPE (expr);
7385 /* [expr.reinterpret.cast]
7386 A pointer can be converted to any integral type large enough to
7387 hold it. ... A value of type std::nullptr_t can be converted to
7388 an integral type; the conversion has the same meaning and
7389 validity as a conversion of (void*)0 to the integral type. */
7390 if (CP_INTEGRAL_TYPE_P (type)
7391 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7393 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7395 if (complain & tf_error)
7396 permerror (input_location, "cast from %qH to %qI loses precision",
7397 intype, type);
7398 else
7399 return error_mark_node;
7401 if (NULLPTR_TYPE_P (intype))
7402 return build_int_cst (type, 0);
7404 /* [expr.reinterpret.cast]
7405 A value of integral or enumeration type can be explicitly
7406 converted to a pointer. */
7407 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7408 /* OK */
7410 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7411 || TYPE_PTR_OR_PTRMEM_P (type))
7412 && same_type_p (type, intype))
7413 /* DR 799 */
7414 return rvalue (expr);
7415 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7417 if ((complain & tf_warning)
7418 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7419 TREE_TYPE (intype)))
7420 warning (OPT_Wcast_function_type,
7421 "cast between incompatible function types"
7422 " from %qH to %qI", intype, type);
7423 return build_nop_reinterpret (type, expr);
7425 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7427 if ((complain & tf_warning)
7428 && !cxx_safe_function_type_cast_p
7429 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7430 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7431 warning (OPT_Wcast_function_type,
7432 "cast between incompatible pointer to member types"
7433 " from %qH to %qI", intype, type);
7434 return build_nop_reinterpret (type, expr);
7436 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7437 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7439 if (!c_cast_p
7440 && check_for_casting_away_constness (intype, type,
7441 REINTERPRET_CAST_EXPR,
7442 complain))
7443 return error_mark_node;
7444 /* Warn about possible alignment problems. */
7445 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7446 && (complain & tf_warning)
7447 && !VOID_TYPE_P (type)
7448 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7449 && COMPLETE_TYPE_P (TREE_TYPE (type))
7450 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7451 && min_align_of_type (TREE_TYPE (type))
7452 > min_align_of_type (TREE_TYPE (intype)))
7453 warning (OPT_Wcast_align, "cast from %qH to %qI "
7454 "increases required alignment of target type", intype, type);
7456 if (warn_strict_aliasing <= 2)
7457 /* strict_aliasing_warning STRIP_NOPs its expr. */
7458 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7460 return build_nop_reinterpret (type, expr);
7462 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7463 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7465 if (complain & tf_warning)
7466 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7467 object pointer type or vice versa is conditionally-supported." */
7468 warning (OPT_Wconditionally_supported,
7469 "casting between pointer-to-function and pointer-to-object "
7470 "is conditionally-supported");
7471 return build_nop_reinterpret (type, expr);
7473 else if (VECTOR_TYPE_P (type))
7474 return convert_to_vector (type, expr);
7475 else if (VECTOR_TYPE_P (intype)
7476 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7477 return convert_to_integer_nofold (type, expr);
7478 else
7480 if (valid_p)
7481 *valid_p = false;
7482 if (complain & tf_error)
7483 error ("invalid cast from type %qT to type %qT", intype, type);
7484 return error_mark_node;
7487 expr = cp_convert (type, expr, complain);
7488 if (TREE_CODE (expr) == NOP_EXPR)
7489 /* Mark any nop_expr that created as a reintepret_cast. */
7490 REINTERPRET_CAST_P (expr) = true;
7491 return expr;
7494 tree
7495 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7497 tree r;
7499 if (type == error_mark_node || expr == error_mark_node)
7500 return error_mark_node;
7502 if (processing_template_decl)
7504 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7506 if (!TREE_SIDE_EFFECTS (t)
7507 && type_dependent_expression_p (expr))
7508 /* There might turn out to be side effects inside expr. */
7509 TREE_SIDE_EFFECTS (t) = 1;
7510 return convert_from_reference (t);
7513 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7514 /*valid_p=*/NULL, complain);
7515 if (r != error_mark_node)
7517 maybe_warn_about_useless_cast (type, expr, complain);
7518 maybe_warn_about_cast_ignoring_quals (type, complain);
7520 return r;
7523 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7524 return an appropriate expression. Otherwise, return
7525 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7526 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7527 performing a C-style cast, its value upon return will indicate
7528 whether or not the conversion succeeded. */
7530 static tree
7531 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7532 bool *valid_p)
7534 tree src_type;
7535 tree reference_type;
7537 /* Callers are responsible for handling error_mark_node as a
7538 destination type. */
7539 gcc_assert (dst_type != error_mark_node);
7540 /* In a template, callers should be building syntactic
7541 representations of casts, not using this machinery. */
7542 gcc_assert (!processing_template_decl);
7544 /* Assume the conversion is invalid. */
7545 if (valid_p)
7546 *valid_p = false;
7548 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7550 if (complain & tf_error)
7551 error ("invalid use of const_cast with type %qT, "
7552 "which is not a pointer, "
7553 "reference, nor a pointer-to-data-member type", dst_type);
7554 return error_mark_node;
7557 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7559 if (complain & tf_error)
7560 error ("invalid use of const_cast with type %qT, which is a pointer "
7561 "or reference to a function type", dst_type);
7562 return error_mark_node;
7565 /* A prvalue of non-class type is cv-unqualified. */
7566 dst_type = cv_unqualified (dst_type);
7568 /* Save casted types in the function's used types hash table. */
7569 used_types_insert (dst_type);
7571 src_type = TREE_TYPE (expr);
7572 /* Expressions do not really have reference types. */
7573 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7574 src_type = TREE_TYPE (src_type);
7576 /* [expr.const.cast]
7578 For two object types T1 and T2, if a pointer to T1 can be explicitly
7579 converted to the type "pointer to T2" using a const_cast, then the
7580 following conversions can also be made:
7582 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7583 type T2 using the cast const_cast<T2&>;
7585 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7586 type T2 using the cast const_cast<T2&&>; and
7588 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7589 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7591 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7593 reference_type = dst_type;
7594 if (!TYPE_REF_IS_RVALUE (dst_type)
7595 ? lvalue_p (expr)
7596 : obvalue_p (expr))
7597 /* OK. */;
7598 else
7600 if (complain & tf_error)
7601 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7602 src_type, dst_type);
7603 return error_mark_node;
7605 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7606 src_type = build_pointer_type (src_type);
7608 else
7610 reference_type = NULL_TREE;
7611 /* If the destination type is not a reference type, the
7612 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7613 conversions are performed. */
7614 src_type = type_decays_to (src_type);
7615 if (src_type == error_mark_node)
7616 return error_mark_node;
7619 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7621 if (comp_ptr_ttypes_const (dst_type, src_type))
7623 if (valid_p)
7625 *valid_p = true;
7626 /* This cast is actually a C-style cast. Issue a warning if
7627 the user is making a potentially unsafe cast. */
7628 check_for_casting_away_constness (src_type, dst_type,
7629 CAST_EXPR, complain);
7630 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7631 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7632 && (complain & tf_warning)
7633 && min_align_of_type (TREE_TYPE (dst_type))
7634 > min_align_of_type (TREE_TYPE (src_type)))
7635 warning (OPT_Wcast_align, "cast from %qH to %qI "
7636 "increases required alignment of target type",
7637 src_type, dst_type);
7639 if (reference_type)
7641 expr = cp_build_addr_expr (expr, complain);
7642 if (expr == error_mark_node)
7643 return error_mark_node;
7644 expr = build_nop (reference_type, expr);
7645 return convert_from_reference (expr);
7647 else
7649 expr = decay_conversion (expr, complain);
7650 if (expr == error_mark_node)
7651 return error_mark_node;
7653 /* build_c_cast puts on a NOP_EXPR to make the result not an
7654 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7655 non-lvalue context. */
7656 if (TREE_CODE (expr) == NOP_EXPR
7657 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7658 expr = TREE_OPERAND (expr, 0);
7659 return build_nop (dst_type, expr);
7662 else if (valid_p
7663 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7664 TREE_TYPE (src_type)))
7665 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7666 complain);
7669 if (complain & tf_error)
7670 error ("invalid const_cast from type %qT to type %qT",
7671 src_type, dst_type);
7672 return error_mark_node;
7675 tree
7676 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7678 tree r;
7680 if (type == error_mark_node || error_operand_p (expr))
7681 return error_mark_node;
7683 if (processing_template_decl)
7685 tree t = build_min (CONST_CAST_EXPR, type, expr);
7687 if (!TREE_SIDE_EFFECTS (t)
7688 && type_dependent_expression_p (expr))
7689 /* There might turn out to be side effects inside expr. */
7690 TREE_SIDE_EFFECTS (t) = 1;
7691 return convert_from_reference (t);
7694 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7695 if (r != error_mark_node)
7697 maybe_warn_about_useless_cast (type, expr, complain);
7698 maybe_warn_about_cast_ignoring_quals (type, complain);
7700 return r;
7703 /* Like cp_build_c_cast, but for the c-common bits. */
7705 tree
7706 build_c_cast (location_t /*loc*/, tree type, tree expr)
7708 return cp_build_c_cast (type, expr, tf_warning_or_error);
7711 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7712 preserve location information even for tree nodes that don't
7713 support it. */
7715 cp_expr
7716 build_c_cast (location_t loc, tree type, cp_expr expr)
7718 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7719 result.set_location (loc);
7720 return result;
7723 /* Build an expression representing an explicit C-style cast to type
7724 TYPE of expression EXPR. */
7726 tree
7727 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7729 tree value = expr;
7730 tree result;
7731 bool valid_p;
7733 if (type == error_mark_node || error_operand_p (expr))
7734 return error_mark_node;
7736 if (processing_template_decl)
7738 tree t = build_min (CAST_EXPR, type,
7739 tree_cons (NULL_TREE, value, NULL_TREE));
7740 /* We don't know if it will or will not have side effects. */
7741 TREE_SIDE_EFFECTS (t) = 1;
7742 return convert_from_reference (t);
7745 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7746 'Class') should always be retained, because this information aids
7747 in method lookup. */
7748 if (objc_is_object_ptr (type)
7749 && objc_is_object_ptr (TREE_TYPE (expr)))
7750 return build_nop (type, expr);
7752 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7753 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7754 if (TREE_CODE (type) != REFERENCE_TYPE
7755 && TREE_CODE (value) == NOP_EXPR
7756 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7757 value = TREE_OPERAND (value, 0);
7759 if (TREE_CODE (type) == ARRAY_TYPE)
7761 /* Allow casting from T1* to T2[] because Cfront allows it.
7762 NIHCL uses it. It is not valid ISO C++ however. */
7763 if (TYPE_PTR_P (TREE_TYPE (expr)))
7765 if (complain & tf_error)
7766 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7767 else
7768 return error_mark_node;
7769 type = build_pointer_type (TREE_TYPE (type));
7771 else
7773 if (complain & tf_error)
7774 error ("ISO C++ forbids casting to an array type %qT", type);
7775 return error_mark_node;
7779 if (TREE_CODE (type) == FUNCTION_TYPE
7780 || TREE_CODE (type) == METHOD_TYPE)
7782 if (complain & tf_error)
7783 error ("invalid cast to function type %qT", type);
7784 return error_mark_node;
7787 if (TYPE_PTR_P (type)
7788 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7789 /* Casting to an integer of smaller size is an error detected elsewhere. */
7790 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7791 /* Don't warn about converting any constant. */
7792 && !TREE_CONSTANT (value))
7793 warning_at (input_location, OPT_Wint_to_pointer_cast,
7794 "cast to pointer from integer of different size");
7796 /* A C-style cast can be a const_cast. */
7797 result = build_const_cast_1 (type, value, complain & tf_warning,
7798 &valid_p);
7799 if (valid_p)
7801 if (result != error_mark_node)
7803 maybe_warn_about_useless_cast (type, value, complain);
7804 maybe_warn_about_cast_ignoring_quals (type, complain);
7806 return result;
7809 /* Or a static cast. */
7810 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7811 &valid_p, complain);
7812 /* Or a reinterpret_cast. */
7813 if (!valid_p)
7814 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7815 &valid_p, complain);
7816 /* The static_cast or reinterpret_cast may be followed by a
7817 const_cast. */
7818 if (valid_p
7819 /* A valid cast may result in errors if, for example, a
7820 conversion to an ambiguous base class is required. */
7821 && !error_operand_p (result))
7823 tree result_type;
7825 maybe_warn_about_useless_cast (type, value, complain);
7826 maybe_warn_about_cast_ignoring_quals (type, complain);
7828 /* Non-class rvalues always have cv-unqualified type. */
7829 if (!CLASS_TYPE_P (type))
7830 type = TYPE_MAIN_VARIANT (type);
7831 result_type = TREE_TYPE (result);
7832 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7833 result_type = TYPE_MAIN_VARIANT (result_type);
7834 /* If the type of RESULT does not match TYPE, perform a
7835 const_cast to make it match. If the static_cast or
7836 reinterpret_cast succeeded, we will differ by at most
7837 cv-qualification, so the follow-on const_cast is guaranteed
7838 to succeed. */
7839 if (!same_type_p (non_reference (type), non_reference (result_type)))
7841 result = build_const_cast_1 (type, result, false, &valid_p);
7842 gcc_assert (valid_p);
7844 return result;
7847 return error_mark_node;
7850 /* For use from the C common bits. */
7851 tree
7852 build_modify_expr (location_t location,
7853 tree lhs, tree /*lhs_origtype*/,
7854 enum tree_code modifycode,
7855 location_t /*rhs_location*/, tree rhs,
7856 tree /*rhs_origtype*/)
7858 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7859 tf_warning_or_error);
7862 /* Build an assignment expression of lvalue LHS from value RHS.
7863 MODIFYCODE is the code for a binary operator that we use
7864 to combine the old value of LHS with RHS to get the new value.
7865 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7867 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7869 tree
7870 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7871 tree rhs, tsubst_flags_t complain)
7873 lhs = mark_lvalue_use_nonread (lhs);
7875 tree result = NULL_TREE;
7876 tree newrhs = rhs;
7877 tree lhstype = TREE_TYPE (lhs);
7878 tree olhs = lhs;
7879 tree olhstype = lhstype;
7880 bool plain_assign = (modifycode == NOP_EXPR);
7881 bool compound_side_effects_p = false;
7882 tree preeval = NULL_TREE;
7884 /* Avoid duplicate error messages from operands that had errors. */
7885 if (error_operand_p (lhs) || error_operand_p (rhs))
7886 return error_mark_node;
7888 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7890 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7891 compound_side_effects_p = true;
7892 lhs = TREE_OPERAND (lhs, 1);
7895 /* Handle control structure constructs used as "lvalues". Note that we
7896 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7897 switch (TREE_CODE (lhs))
7899 /* Handle --foo = 5; as these are valid constructs in C++. */
7900 case PREDECREMENT_EXPR:
7901 case PREINCREMENT_EXPR:
7902 if (compound_side_effects_p)
7903 newrhs = rhs = stabilize_expr (rhs, &preeval);
7904 lhs = genericize_compound_lvalue (lhs);
7905 maybe_add_compound:
7906 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7907 and looked through the COMPOUND_EXPRs, readd them now around
7908 the resulting lhs. */
7909 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7911 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7912 tree *ptr = &TREE_OPERAND (lhs, 1);
7913 for (olhs = TREE_OPERAND (olhs, 1);
7914 TREE_CODE (olhs) == COMPOUND_EXPR;
7915 olhs = TREE_OPERAND (olhs, 1))
7917 *ptr = build2 (COMPOUND_EXPR, lhstype,
7918 TREE_OPERAND (olhs, 0), *ptr);
7919 ptr = &TREE_OPERAND (*ptr, 1);
7922 break;
7924 case MODIFY_EXPR:
7925 if (compound_side_effects_p)
7926 newrhs = rhs = stabilize_expr (rhs, &preeval);
7927 lhs = genericize_compound_lvalue (lhs);
7928 goto maybe_add_compound;
7930 case MIN_EXPR:
7931 case MAX_EXPR:
7932 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7933 when neither operand has side-effects. */
7934 if (!lvalue_or_else (lhs, lv_assign, complain))
7935 return error_mark_node;
7937 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7938 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7940 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7941 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7942 boolean_type_node,
7943 TREE_OPERAND (lhs, 0),
7944 TREE_OPERAND (lhs, 1)),
7945 TREE_OPERAND (lhs, 0),
7946 TREE_OPERAND (lhs, 1));
7947 gcc_fallthrough ();
7949 /* Handle (a ? b : c) used as an "lvalue". */
7950 case COND_EXPR:
7952 /* Produce (a ? (b = rhs) : (c = rhs))
7953 except that the RHS goes through a save-expr
7954 so the code to compute it is only emitted once. */
7955 tree cond;
7957 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7959 if (complain & tf_error)
7960 error ("void value not ignored as it ought to be");
7961 return error_mark_node;
7964 rhs = stabilize_expr (rhs, &preeval);
7966 /* Check this here to avoid odd errors when trying to convert
7967 a throw to the type of the COND_EXPR. */
7968 if (!lvalue_or_else (lhs, lv_assign, complain))
7969 return error_mark_node;
7971 cond = build_conditional_expr
7972 (input_location, TREE_OPERAND (lhs, 0),
7973 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7974 modifycode, rhs, complain),
7975 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7976 modifycode, rhs, complain),
7977 complain);
7979 if (cond == error_mark_node)
7980 return cond;
7981 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7982 and looked through the COMPOUND_EXPRs, readd them now around
7983 the resulting cond before adding the preevaluated rhs. */
7984 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7986 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7987 TREE_OPERAND (olhs, 0), cond);
7988 tree *ptr = &TREE_OPERAND (cond, 1);
7989 for (olhs = TREE_OPERAND (olhs, 1);
7990 TREE_CODE (olhs) == COMPOUND_EXPR;
7991 olhs = TREE_OPERAND (olhs, 1))
7993 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7994 TREE_OPERAND (olhs, 0), *ptr);
7995 ptr = &TREE_OPERAND (*ptr, 1);
7998 /* Make sure the code to compute the rhs comes out
7999 before the split. */
8000 result = cond;
8001 goto ret;
8004 default:
8005 lhs = olhs;
8006 break;
8009 if (modifycode == INIT_EXPR)
8011 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8012 /* Do the default thing. */;
8013 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8015 /* Compound literal. */
8016 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8017 /* Call convert to generate an error; see PR 11063. */
8018 rhs = convert (lhstype, rhs);
8019 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8020 TREE_SIDE_EFFECTS (result) = 1;
8021 goto ret;
8023 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8024 /* Do the default thing. */;
8025 else
8027 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8028 result = build_special_member_call (lhs, complete_ctor_identifier,
8029 &rhs_vec, lhstype, LOOKUP_NORMAL,
8030 complain);
8031 release_tree_vector (rhs_vec);
8032 if (result == NULL_TREE)
8033 return error_mark_node;
8034 goto ret;
8037 else
8039 lhs = require_complete_type_sfinae (lhs, complain);
8040 if (lhs == error_mark_node)
8041 return error_mark_node;
8043 if (modifycode == NOP_EXPR)
8045 if (c_dialect_objc ())
8047 result = objc_maybe_build_modify_expr (lhs, rhs);
8048 if (result)
8049 goto ret;
8052 /* `operator=' is not an inheritable operator. */
8053 if (! MAYBE_CLASS_TYPE_P (lhstype))
8054 /* Do the default thing. */;
8055 else
8057 result = build_new_op (input_location, MODIFY_EXPR,
8058 LOOKUP_NORMAL, lhs, rhs,
8059 make_node (NOP_EXPR), /*overload=*/NULL,
8060 complain);
8061 if (result == NULL_TREE)
8062 return error_mark_node;
8063 goto ret;
8065 lhstype = olhstype;
8067 else
8069 tree init = NULL_TREE;
8071 /* A binary op has been requested. Combine the old LHS
8072 value with the RHS producing the value we should actually
8073 store into the LHS. */
8074 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
8075 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8076 || MAYBE_CLASS_TYPE_P (lhstype)));
8078 /* Preevaluate the RHS to make sure its evaluation is complete
8079 before the lvalue-to-rvalue conversion of the LHS:
8081 [expr.ass] With respect to an indeterminately-sequenced
8082 function call, the operation of a compound assignment is a
8083 single evaluation. [ Note: Therefore, a function call shall
8084 not intervene between the lvalue-to-rvalue conversion and the
8085 side effect associated with any single compound assignment
8086 operator. -- end note ] */
8087 lhs = cp_stabilize_reference (lhs);
8088 rhs = decay_conversion (rhs, complain);
8089 if (rhs == error_mark_node)
8090 return error_mark_node;
8091 rhs = stabilize_expr (rhs, &init);
8092 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8093 if (newrhs == error_mark_node)
8095 if (complain & tf_error)
8096 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8097 TREE_TYPE (lhs), TREE_TYPE (rhs));
8098 return error_mark_node;
8101 if (init)
8102 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8104 /* Now it looks like a plain assignment. */
8105 modifycode = NOP_EXPR;
8106 if (c_dialect_objc ())
8108 result = objc_maybe_build_modify_expr (lhs, newrhs);
8109 if (result)
8110 goto ret;
8113 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
8114 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
8117 /* The left-hand side must be an lvalue. */
8118 if (!lvalue_or_else (lhs, lv_assign, complain))
8119 return error_mark_node;
8121 /* Warn about modifying something that is `const'. Don't warn if
8122 this is initialization. */
8123 if (modifycode != INIT_EXPR
8124 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8125 /* Functions are not modifiable, even though they are
8126 lvalues. */
8127 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8128 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8129 /* If it's an aggregate and any field is const, then it is
8130 effectively const. */
8131 || (CLASS_TYPE_P (lhstype)
8132 && C_TYPE_FIELDS_READONLY (lhstype))))
8134 if (complain & tf_error)
8135 cxx_readonly_error (lhs, lv_assign);
8136 return error_mark_node;
8139 /* If storing into a structure or union member, it may have been given a
8140 lowered bitfield type. We need to convert to the declared type first,
8141 so retrieve it now. */
8143 olhstype = unlowered_expr_type (lhs);
8145 /* Convert new value to destination type. */
8147 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8149 int from_array;
8151 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8153 if (modifycode != INIT_EXPR)
8155 if (complain & tf_error)
8156 error ("assigning to an array from an initializer list");
8157 return error_mark_node;
8159 if (check_array_initializer (lhs, lhstype, newrhs))
8160 return error_mark_node;
8161 newrhs = digest_init (lhstype, newrhs, complain);
8162 if (newrhs == error_mark_node)
8163 return error_mark_node;
8166 /* C++11 8.5/17: "If the destination type is an array of characters,
8167 an array of char16_t, an array of char32_t, or an array of wchar_t,
8168 and the initializer is a string literal...". */
8169 else if (TREE_CODE (newrhs) == STRING_CST
8170 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8171 && modifycode == INIT_EXPR)
8173 newrhs = digest_init (lhstype, newrhs, complain);
8174 if (newrhs == error_mark_node)
8175 return error_mark_node;
8178 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8179 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8181 if (complain & tf_error)
8182 error ("incompatible types in assignment of %qT to %qT",
8183 TREE_TYPE (rhs), lhstype);
8184 return error_mark_node;
8187 /* Allow array assignment in compiler-generated code. */
8188 else if (!current_function_decl
8189 || !DECL_DEFAULTED_FN (current_function_decl))
8191 /* This routine is used for both initialization and assignment.
8192 Make sure the diagnostic message differentiates the context. */
8193 if (complain & tf_error)
8195 if (modifycode == INIT_EXPR)
8196 error ("array used as initializer");
8197 else
8198 error ("invalid array assignment");
8200 return error_mark_node;
8203 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8204 ? 1 + (modifycode != INIT_EXPR): 0;
8205 result = build_vec_init (lhs, NULL_TREE, newrhs,
8206 /*explicit_value_init_p=*/false,
8207 from_array, complain);
8208 goto ret;
8211 if (modifycode == INIT_EXPR)
8212 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8213 LOOKUP_ONLYCONVERTING. */
8214 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8215 ICR_INIT, NULL_TREE, 0,
8216 complain);
8217 else
8218 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8219 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8221 if (!same_type_p (lhstype, olhstype))
8222 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8224 if (modifycode != INIT_EXPR)
8226 if (TREE_CODE (newrhs) == CALL_EXPR
8227 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8228 newrhs = build_cplus_new (lhstype, newrhs, complain);
8230 /* Can't initialize directly from a TARGET_EXPR, since that would
8231 cause the lhs to be constructed twice, and possibly result in
8232 accidental self-initialization. So we force the TARGET_EXPR to be
8233 expanded without a target. */
8234 if (TREE_CODE (newrhs) == TARGET_EXPR)
8235 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8236 TREE_OPERAND (newrhs, 0));
8239 if (newrhs == error_mark_node)
8240 return error_mark_node;
8242 if (c_dialect_objc () && flag_objc_gc)
8244 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8246 if (result)
8247 goto ret;
8250 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8251 lhstype, lhs, newrhs);
8253 TREE_SIDE_EFFECTS (result) = 1;
8254 if (!plain_assign)
8255 TREE_NO_WARNING (result) = 1;
8257 ret:
8258 if (preeval)
8259 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8260 return result;
8263 cp_expr
8264 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8265 tree rhs, tsubst_flags_t complain)
8267 tree orig_lhs = lhs;
8268 tree orig_rhs = rhs;
8269 tree overload = NULL_TREE;
8270 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8272 if (processing_template_decl)
8274 if (modifycode == NOP_EXPR
8275 || type_dependent_expression_p (lhs)
8276 || type_dependent_expression_p (rhs))
8277 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8278 build_min_nt_loc (loc, modifycode, NULL_TREE,
8279 NULL_TREE), rhs);
8281 lhs = build_non_dependent_expr (lhs);
8282 rhs = build_non_dependent_expr (rhs);
8285 if (modifycode != NOP_EXPR)
8287 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8288 lhs, rhs, op, &overload, complain);
8289 if (rval)
8291 if (rval == error_mark_node)
8292 return rval;
8293 TREE_NO_WARNING (rval) = 1;
8294 if (processing_template_decl)
8296 if (overload != NULL_TREE)
8297 return (build_min_non_dep_op_overload
8298 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8300 return (build_min_non_dep
8301 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8303 return rval;
8306 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8309 /* Helper function for get_delta_difference which assumes FROM is a base
8310 class of TO. Returns a delta for the conversion of pointer-to-member
8311 of FROM to pointer-to-member of TO. If the conversion is invalid and
8312 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8313 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8314 If C_CAST_P is true, this conversion is taking place as part of a
8315 C-style cast. */
8317 static tree
8318 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8319 tsubst_flags_t complain)
8321 tree binfo;
8322 base_kind kind;
8324 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8325 &kind, complain);
8327 if (binfo == error_mark_node)
8329 if (!(complain & tf_error))
8330 return error_mark_node;
8332 error (" in pointer to member function conversion");
8333 return size_zero_node;
8335 else if (binfo)
8337 if (kind != bk_via_virtual)
8338 return BINFO_OFFSET (binfo);
8339 else
8340 /* FROM is a virtual base class of TO. Issue an error or warning
8341 depending on whether or not this is a reinterpret cast. */
8343 if (!(complain & tf_error))
8344 return error_mark_node;
8346 error ("pointer to member conversion via virtual base %qT",
8347 BINFO_TYPE (binfo_from_vbase (binfo)));
8349 return size_zero_node;
8352 else
8353 return NULL_TREE;
8356 /* Get difference in deltas for different pointer to member function
8357 types. If the conversion is invalid and tf_error is not set in
8358 COMPLAIN, returns error_mark_node, otherwise returns an integer
8359 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8360 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8361 conversions as well. If C_CAST_P is true this conversion is taking
8362 place as part of a C-style cast.
8364 Note that the naming of FROM and TO is kind of backwards; the return
8365 value is what we add to a TO in order to get a FROM. They are named
8366 this way because we call this function to find out how to convert from
8367 a pointer to member of FROM to a pointer to member of TO. */
8369 static tree
8370 get_delta_difference (tree from, tree to,
8371 bool allow_inverse_p,
8372 bool c_cast_p, tsubst_flags_t complain)
8374 tree result;
8376 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8377 /* Pointer to member of incomplete class is permitted*/
8378 result = size_zero_node;
8379 else
8380 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8382 if (result == error_mark_node)
8383 return error_mark_node;
8385 if (!result)
8387 if (!allow_inverse_p)
8389 if (!(complain & tf_error))
8390 return error_mark_node;
8392 error_not_base_type (from, to);
8393 error (" in pointer to member conversion");
8394 result = size_zero_node;
8396 else
8398 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8400 if (result == error_mark_node)
8401 return error_mark_node;
8403 if (result)
8404 result = size_diffop_loc (input_location,
8405 size_zero_node, result);
8406 else
8408 if (!(complain & tf_error))
8409 return error_mark_node;
8411 error_not_base_type (from, to);
8412 error (" in pointer to member conversion");
8413 result = size_zero_node;
8418 return convert_to_integer (ptrdiff_type_node, result);
8421 /* Return a constructor for the pointer-to-member-function TYPE using
8422 the other components as specified. */
8424 tree
8425 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8427 tree u = NULL_TREE;
8428 tree delta_field;
8429 tree pfn_field;
8430 vec<constructor_elt, va_gc> *v;
8432 /* Pull the FIELD_DECLs out of the type. */
8433 pfn_field = TYPE_FIELDS (type);
8434 delta_field = DECL_CHAIN (pfn_field);
8436 /* Make sure DELTA has the type we want. */
8437 delta = convert_and_check (input_location, delta_type_node, delta);
8439 /* Convert to the correct target type if necessary. */
8440 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8442 /* Finish creating the initializer. */
8443 vec_alloc (v, 2);
8444 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8445 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8446 u = build_constructor (type, v);
8447 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8448 TREE_STATIC (u) = (TREE_CONSTANT (u)
8449 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8450 != NULL_TREE)
8451 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8452 != NULL_TREE));
8453 return u;
8456 /* Build a constructor for a pointer to member function. It can be
8457 used to initialize global variables, local variable, or used
8458 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8459 want to be.
8461 If FORCE is nonzero, then force this conversion, even if
8462 we would rather not do it. Usually set when using an explicit
8463 cast. A C-style cast is being processed iff C_CAST_P is true.
8465 Return error_mark_node, if something goes wrong. */
8467 tree
8468 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8469 tsubst_flags_t complain)
8471 tree fn;
8472 tree pfn_type;
8473 tree to_type;
8475 if (error_operand_p (pfn))
8476 return error_mark_node;
8478 pfn_type = TREE_TYPE (pfn);
8479 to_type = build_ptrmemfunc_type (type);
8481 /* Handle multiple conversions of pointer to member functions. */
8482 if (TYPE_PTRMEMFUNC_P (pfn_type))
8484 tree delta = NULL_TREE;
8485 tree npfn = NULL_TREE;
8486 tree n;
8488 if (!force
8489 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8490 LOOKUP_NORMAL, complain))
8492 if (complain & tf_error)
8493 error ("invalid conversion to type %qT from type %qT",
8494 to_type, pfn_type);
8495 else
8496 return error_mark_node;
8499 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8500 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8501 force,
8502 c_cast_p, complain);
8503 if (n == error_mark_node)
8504 return error_mark_node;
8506 /* We don't have to do any conversion to convert a
8507 pointer-to-member to its own type. But, we don't want to
8508 just return a PTRMEM_CST if there's an explicit cast; that
8509 cast should make the expression an invalid template argument. */
8510 if (TREE_CODE (pfn) != PTRMEM_CST)
8512 if (same_type_p (to_type, pfn_type))
8513 return pfn;
8514 else if (integer_zerop (n))
8515 return build_reinterpret_cast (to_type, pfn,
8516 complain);
8519 if (TREE_SIDE_EFFECTS (pfn))
8520 pfn = save_expr (pfn);
8522 /* Obtain the function pointer and the current DELTA. */
8523 if (TREE_CODE (pfn) == PTRMEM_CST)
8524 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8525 else
8527 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8528 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8531 /* Just adjust the DELTA field. */
8532 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8533 (TREE_TYPE (delta), ptrdiff_type_node));
8534 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8535 n = cp_build_binary_op (input_location,
8536 LSHIFT_EXPR, n, integer_one_node,
8537 complain);
8538 delta = cp_build_binary_op (input_location,
8539 PLUS_EXPR, delta, n, complain);
8540 return build_ptrmemfunc1 (to_type, delta, npfn);
8543 /* Handle null pointer to member function conversions. */
8544 if (null_ptr_cst_p (pfn))
8546 pfn = cp_build_c_cast (type, pfn, complain);
8547 return build_ptrmemfunc1 (to_type,
8548 integer_zero_node,
8549 pfn);
8552 if (type_unknown_p (pfn))
8553 return instantiate_type (type, pfn, complain);
8555 fn = TREE_OPERAND (pfn, 0);
8556 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8557 /* In a template, we will have preserved the
8558 OFFSET_REF. */
8559 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8560 return make_ptrmem_cst (to_type, fn);
8563 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8564 given by CST.
8566 ??? There is no consistency as to the types returned for the above
8567 values. Some code acts as if it were a sizetype and some as if it were
8568 integer_type_node. */
8570 void
8571 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8573 tree type = TREE_TYPE (cst);
8574 tree fn = PTRMEM_CST_MEMBER (cst);
8575 tree ptr_class, fn_class;
8577 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8579 /* The class that the function belongs to. */
8580 fn_class = DECL_CONTEXT (fn);
8582 /* The class that we're creating a pointer to member of. */
8583 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8585 /* First, calculate the adjustment to the function's class. */
8586 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8587 /*c_cast_p=*/0, tf_warning_or_error);
8589 if (!DECL_VIRTUAL_P (fn))
8590 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8591 build_addr_func (fn, tf_warning_or_error));
8592 else
8594 /* If we're dealing with a virtual function, we have to adjust 'this'
8595 again, to point to the base which provides the vtable entry for
8596 fn; the call will do the opposite adjustment. */
8597 tree orig_class = DECL_CONTEXT (fn);
8598 tree binfo = binfo_or_else (orig_class, fn_class);
8599 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8600 *delta, BINFO_OFFSET (binfo));
8602 /* We set PFN to the vtable offset at which the function can be
8603 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8604 case delta is shifted left, and then incremented). */
8605 *pfn = DECL_VINDEX (fn);
8606 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8607 TYPE_SIZE_UNIT (vtable_entry_type));
8609 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8611 case ptrmemfunc_vbit_in_pfn:
8612 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8613 integer_one_node);
8614 break;
8616 case ptrmemfunc_vbit_in_delta:
8617 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8618 *delta, integer_one_node);
8619 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8620 *delta, integer_one_node);
8621 break;
8623 default:
8624 gcc_unreachable ();
8627 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8631 /* Return an expression for PFN from the pointer-to-member function
8632 given by T. */
8634 static tree
8635 pfn_from_ptrmemfunc (tree t)
8637 if (TREE_CODE (t) == PTRMEM_CST)
8639 tree delta;
8640 tree pfn;
8642 expand_ptrmemfunc_cst (t, &delta, &pfn);
8643 if (pfn)
8644 return pfn;
8647 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8650 /* Return an expression for DELTA from the pointer-to-member function
8651 given by T. */
8653 static tree
8654 delta_from_ptrmemfunc (tree t)
8656 if (TREE_CODE (t) == PTRMEM_CST)
8658 tree delta;
8659 tree pfn;
8661 expand_ptrmemfunc_cst (t, &delta, &pfn);
8662 if (delta)
8663 return delta;
8666 return build_ptrmemfunc_access_expr (t, delta_identifier);
8669 /* Convert value RHS to type TYPE as preparation for an assignment to
8670 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8671 implicit conversion is. If FNDECL is non-NULL, we are doing the
8672 conversion in order to pass the PARMNUMth argument of FNDECL.
8673 If FNDECL is NULL, we are doing the conversion in function pointer
8674 argument passing, conversion in initialization, etc. */
8676 static tree
8677 convert_for_assignment (tree type, tree rhs,
8678 impl_conv_rhs errtype, tree fndecl, int parmnum,
8679 tsubst_flags_t complain, int flags)
8681 tree rhstype;
8682 enum tree_code coder;
8684 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8685 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8686 rhs = TREE_OPERAND (rhs, 0);
8688 /* Handle [dcl.init.list] direct-list-initialization from
8689 single element of enumeration with a fixed underlying type. */
8690 if (is_direct_enum_init (type, rhs))
8692 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8693 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8695 warning_sentinel w (warn_useless_cast);
8696 warning_sentinel w2 (warn_ignored_qualifiers);
8697 rhs = cp_build_c_cast (type, elt, complain);
8699 else
8700 rhs = error_mark_node;
8703 rhstype = TREE_TYPE (rhs);
8704 coder = TREE_CODE (rhstype);
8706 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8707 && vector_types_convertible_p (type, rhstype, true))
8709 rhs = mark_rvalue_use (rhs);
8710 return convert (type, rhs);
8713 if (rhs == error_mark_node || rhstype == error_mark_node)
8714 return error_mark_node;
8715 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8716 return error_mark_node;
8718 /* The RHS of an assignment cannot have void type. */
8719 if (coder == VOID_TYPE)
8721 if (complain & tf_error)
8722 error ("void value not ignored as it ought to be");
8723 return error_mark_node;
8726 if (c_dialect_objc ())
8728 int parmno;
8729 tree selector;
8730 tree rname = fndecl;
8732 switch (errtype)
8734 case ICR_ASSIGN:
8735 parmno = -1;
8736 break;
8737 case ICR_INIT:
8738 parmno = -2;
8739 break;
8740 default:
8741 selector = objc_message_selector ();
8742 parmno = parmnum;
8743 if (selector && parmno > 1)
8745 rname = selector;
8746 parmno -= 1;
8750 if (objc_compare_types (type, rhstype, parmno, rname))
8752 rhs = mark_rvalue_use (rhs);
8753 return convert (type, rhs);
8757 /* [expr.ass]
8759 The expression is implicitly converted (clause _conv_) to the
8760 cv-unqualified type of the left operand.
8762 We allow bad conversions here because by the time we get to this point
8763 we are committed to doing the conversion. If we end up doing a bad
8764 conversion, convert_like will complain. */
8765 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8767 /* When -Wno-pmf-conversions is use, we just silently allow
8768 conversions from pointers-to-members to plain pointers. If
8769 the conversion doesn't work, cp_convert will complain. */
8770 if (!warn_pmf2ptr
8771 && TYPE_PTR_P (type)
8772 && TYPE_PTRMEMFUNC_P (rhstype))
8773 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8774 else
8776 if (complain & tf_error)
8778 /* If the right-hand side has unknown type, then it is an
8779 overloaded function. Call instantiate_type to get error
8780 messages. */
8781 if (rhstype == unknown_type_node)
8783 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8784 /* -fpermissive might allow this; recurse. */
8785 if (!seen_error ())
8786 return convert_for_assignment (type, r, errtype, fndecl,
8787 parmnum, complain, flags);
8789 else if (fndecl)
8791 error_at (EXPR_LOC_OR_LOC (rhs, input_location),
8792 "cannot convert %qH to %qI",
8793 rhstype, type);
8794 inform (get_fndecl_argument_location (fndecl, parmnum),
8795 " initializing argument %P of %qD", parmnum, fndecl);
8797 else
8798 switch (errtype)
8800 case ICR_DEFAULT_ARGUMENT:
8801 error ("cannot convert %qH to %qI in default argument",
8802 rhstype, type);
8803 break;
8804 case ICR_ARGPASS:
8805 error ("cannot convert %qH to %qI in argument passing",
8806 rhstype, type);
8807 break;
8808 case ICR_CONVERTING:
8809 error ("cannot convert %qH to %qI",
8810 rhstype, type);
8811 break;
8812 case ICR_INIT:
8813 error ("cannot convert %qH to %qI in initialization",
8814 rhstype, type);
8815 break;
8816 case ICR_RETURN:
8817 error ("cannot convert %qH to %qI in return",
8818 rhstype, type);
8819 break;
8820 case ICR_ASSIGN:
8821 error ("cannot convert %qH to %qI in assignment",
8822 rhstype, type);
8823 break;
8824 default:
8825 gcc_unreachable();
8827 if (TYPE_PTR_P (rhstype)
8828 && TYPE_PTR_P (type)
8829 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8830 && CLASS_TYPE_P (TREE_TYPE (type))
8831 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8832 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8833 (TREE_TYPE (rhstype))),
8834 "class type %qT is incomplete", TREE_TYPE (rhstype));
8836 return error_mark_node;
8839 if (warn_suggest_attribute_format)
8841 const enum tree_code codel = TREE_CODE (type);
8842 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8843 && coder == codel
8844 && check_missing_format_attribute (type, rhstype)
8845 && (complain & tf_warning))
8846 switch (errtype)
8848 case ICR_ARGPASS:
8849 case ICR_DEFAULT_ARGUMENT:
8850 if (fndecl)
8851 warning (OPT_Wsuggest_attribute_format,
8852 "parameter %qP of %qD might be a candidate "
8853 "for a format attribute", parmnum, fndecl);
8854 else
8855 warning (OPT_Wsuggest_attribute_format,
8856 "parameter might be a candidate "
8857 "for a format attribute");
8858 break;
8859 case ICR_CONVERTING:
8860 warning (OPT_Wsuggest_attribute_format,
8861 "target of conversion might be a candidate "
8862 "for a format attribute");
8863 break;
8864 case ICR_INIT:
8865 warning (OPT_Wsuggest_attribute_format,
8866 "target of initialization might be a candidate "
8867 "for a format attribute");
8868 break;
8869 case ICR_RETURN:
8870 warning (OPT_Wsuggest_attribute_format,
8871 "return type might be a candidate "
8872 "for a format attribute");
8873 break;
8874 case ICR_ASSIGN:
8875 warning (OPT_Wsuggest_attribute_format,
8876 "left-hand side of assignment might be a candidate "
8877 "for a format attribute");
8878 break;
8879 default:
8880 gcc_unreachable();
8884 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8885 does not. */
8886 if (warn_parentheses
8887 && TREE_CODE (type) == BOOLEAN_TYPE
8888 && TREE_CODE (rhs) == MODIFY_EXPR
8889 && !TREE_NO_WARNING (rhs)
8890 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8891 && (complain & tf_warning))
8893 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8895 warning_at (loc, OPT_Wparentheses,
8896 "suggest parentheses around assignment used as truth value");
8897 TREE_NO_WARNING (rhs) = 1;
8900 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8901 complain, flags);
8904 /* Convert RHS to be of type TYPE.
8905 If EXP is nonzero, it is the target of the initialization.
8906 ERRTYPE indicates what kind of error the implicit conversion is.
8908 Two major differences between the behavior of
8909 `convert_for_assignment' and `convert_for_initialization'
8910 are that references are bashed in the former, while
8911 copied in the latter, and aggregates are assigned in
8912 the former (operator=) while initialized in the
8913 latter (X(X&)).
8915 If using constructor make sure no conversion operator exists, if one does
8916 exist, an ambiguity exists. */
8918 tree
8919 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8920 impl_conv_rhs errtype, tree fndecl, int parmnum,
8921 tsubst_flags_t complain)
8923 enum tree_code codel = TREE_CODE (type);
8924 tree rhstype;
8925 enum tree_code coder;
8927 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8928 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8929 if (TREE_CODE (rhs) == NOP_EXPR
8930 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8931 && codel != REFERENCE_TYPE)
8932 rhs = TREE_OPERAND (rhs, 0);
8934 if (type == error_mark_node
8935 || rhs == error_mark_node
8936 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8937 return error_mark_node;
8939 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8941 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8942 && TREE_CODE (type) != ARRAY_TYPE
8943 && (TREE_CODE (type) != REFERENCE_TYPE
8944 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8945 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8946 && !TYPE_REFFN_P (type))
8947 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8948 rhs = decay_conversion (rhs, complain);
8950 rhstype = TREE_TYPE (rhs);
8951 coder = TREE_CODE (rhstype);
8953 if (coder == ERROR_MARK)
8954 return error_mark_node;
8956 /* We accept references to incomplete types, so we can
8957 return here before checking if RHS is of complete type. */
8959 if (codel == REFERENCE_TYPE)
8961 /* This should eventually happen in convert_arguments. */
8962 int savew = 0, savee = 0;
8964 if (fndecl)
8965 savew = warningcount + werrorcount, savee = errorcount;
8966 rhs = initialize_reference (type, rhs, flags, complain);
8968 if (fndecl
8969 && (warningcount + werrorcount > savew || errorcount > savee))
8970 inform (DECL_SOURCE_LOCATION (fndecl),
8971 "in passing argument %P of %qD", parmnum, fndecl);
8973 return rhs;
8976 if (exp != 0)
8977 exp = require_complete_type_sfinae (exp, complain);
8978 if (exp == error_mark_node)
8979 return error_mark_node;
8981 rhstype = non_reference (rhstype);
8983 type = complete_type (type);
8985 if (DIRECT_INIT_EXPR_P (type, rhs))
8986 /* Don't try to do copy-initialization if we already have
8987 direct-initialization. */
8988 return rhs;
8990 if (MAYBE_CLASS_TYPE_P (type))
8991 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8993 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8994 complain, flags);
8997 /* If RETVAL is the address of, or a reference to, a local variable or
8998 temporary give an appropriate warning and return true. */
9000 static bool
9001 maybe_warn_about_returning_address_of_local (tree retval)
9003 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9004 tree whats_returned = fold_for_warn (retval);
9006 for (;;)
9008 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9009 whats_returned = TREE_OPERAND (whats_returned, 1);
9010 else if (CONVERT_EXPR_P (whats_returned)
9011 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9012 whats_returned = TREE_OPERAND (whats_returned, 0);
9013 else
9014 break;
9017 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9018 return false;
9019 whats_returned = TREE_OPERAND (whats_returned, 0);
9021 while (TREE_CODE (whats_returned) == COMPONENT_REF
9022 || TREE_CODE (whats_returned) == ARRAY_REF)
9023 whats_returned = TREE_OPERAND (whats_returned, 0);
9025 if (TREE_CODE (valtype) == REFERENCE_TYPE)
9027 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9028 || TREE_CODE (whats_returned) == TARGET_EXPR)
9030 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
9031 return true;
9033 if (VAR_P (whats_returned)
9034 && DECL_NAME (whats_returned)
9035 && TEMP_NAME_P (DECL_NAME (whats_returned)))
9037 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
9038 return true;
9042 if (DECL_P (whats_returned)
9043 && DECL_NAME (whats_returned)
9044 && DECL_FUNCTION_SCOPE_P (whats_returned)
9045 && !is_capture_proxy (whats_returned)
9046 && !(TREE_STATIC (whats_returned)
9047 || TREE_PUBLIC (whats_returned)))
9049 if (TREE_CODE (valtype) == REFERENCE_TYPE)
9050 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9051 OPT_Wreturn_local_addr,
9052 "reference to local variable %qD returned",
9053 whats_returned);
9054 else if (TREE_CODE (whats_returned) == LABEL_DECL)
9055 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9056 OPT_Wreturn_local_addr, "address of label %qD returned",
9057 whats_returned);
9058 else
9059 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9060 OPT_Wreturn_local_addr, "address of local variable %qD "
9061 "returned", whats_returned);
9062 return true;
9065 return false;
9068 /* Check that returning RETVAL from the current function is valid.
9069 Return an expression explicitly showing all conversions required to
9070 change RETVAL into the function return type, and to assign it to
9071 the DECL_RESULT for the function. Set *NO_WARNING to true if
9072 code reaches end of non-void function warning shouldn't be issued
9073 on this RETURN_EXPR. */
9075 tree
9076 check_return_expr (tree retval, bool *no_warning)
9078 tree result;
9079 /* The type actually returned by the function. */
9080 tree valtype;
9081 /* The type the function is declared to return, or void if
9082 the declared type is incomplete. */
9083 tree functype;
9084 int fn_returns_value_p;
9085 bool named_return_value_okay_p;
9087 *no_warning = false;
9089 /* A `volatile' function is one that isn't supposed to return, ever.
9090 (This is a G++ extension, used to get better code for functions
9091 that call the `volatile' function.) */
9092 if (TREE_THIS_VOLATILE (current_function_decl))
9093 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9095 /* Check for various simple errors. */
9096 if (DECL_DESTRUCTOR_P (current_function_decl))
9098 if (retval)
9099 error ("returning a value from a destructor");
9100 return NULL_TREE;
9102 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9104 if (in_function_try_handler)
9105 /* If a return statement appears in a handler of the
9106 function-try-block of a constructor, the program is ill-formed. */
9107 error ("cannot return from a handler of a function-try-block of a constructor");
9108 else if (retval)
9109 /* You can't return a value from a constructor. */
9110 error ("returning a value from a constructor");
9111 return NULL_TREE;
9114 const tree saved_retval = retval;
9116 if (processing_template_decl)
9118 current_function_returns_value = 1;
9120 if (check_for_bare_parameter_packs (retval))
9121 return error_mark_node;
9123 /* If one of the types might be void, we can't tell whether we're
9124 returning a value. */
9125 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9126 && !current_function_auto_return_pattern)
9127 || (retval != NULL_TREE
9128 && (TREE_TYPE (retval) == NULL_TREE
9129 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9130 goto dependent;
9133 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9135 /* Deduce auto return type from a return statement. */
9136 if (current_function_auto_return_pattern)
9138 tree auto_node;
9139 tree type;
9141 if (!retval && !is_auto (current_function_auto_return_pattern))
9143 /* Give a helpful error message. */
9144 error ("return-statement with no value, in function returning %qT",
9145 current_function_auto_return_pattern);
9146 inform (input_location, "only plain %<auto%> return type can be "
9147 "deduced to %<void%>");
9148 type = error_mark_node;
9150 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9152 error ("returning initializer list");
9153 type = error_mark_node;
9155 else
9157 if (!retval)
9158 retval = void_node;
9159 auto_node = type_uses_auto (current_function_auto_return_pattern);
9160 type = do_auto_deduction (current_function_auto_return_pattern,
9161 retval, auto_node);
9164 if (type == error_mark_node)
9165 /* Leave it. */;
9166 else if (functype == current_function_auto_return_pattern)
9167 apply_deduced_return_type (current_function_decl, type);
9168 else if (!same_type_p (type, functype))
9170 if (LAMBDA_FUNCTION_P (current_function_decl))
9171 error ("inconsistent types %qT and %qT deduced for "
9172 "lambda return type", functype, type);
9173 else
9174 error ("inconsistent deduction for auto return type: "
9175 "%qT and then %qT", functype, type);
9177 functype = type;
9180 result = DECL_RESULT (current_function_decl);
9181 valtype = TREE_TYPE (result);
9182 gcc_assert (valtype != NULL_TREE);
9183 fn_returns_value_p = !VOID_TYPE_P (valtype);
9185 /* Check for a return statement with no return value in a function
9186 that's supposed to return a value. */
9187 if (!retval && fn_returns_value_p)
9189 if (functype != error_mark_node)
9190 permerror (input_location, "return-statement with no value, in "
9191 "function returning %qT", valtype);
9192 /* Remember that this function did return. */
9193 current_function_returns_value = 1;
9194 /* And signal caller that TREE_NO_WARNING should be set on the
9195 RETURN_EXPR to avoid control reaches end of non-void function
9196 warnings in tree-cfg.c. */
9197 *no_warning = true;
9199 /* Check for a return statement with a value in a function that
9200 isn't supposed to return a value. */
9201 else if (retval && !fn_returns_value_p)
9203 if (VOID_TYPE_P (TREE_TYPE (retval)))
9204 /* You can return a `void' value from a function of `void'
9205 type. In that case, we have to evaluate the expression for
9206 its side-effects. */
9207 finish_expr_stmt (retval);
9208 else
9209 permerror (input_location,
9210 "return-statement with a value, in function "
9211 "returning %qT", valtype);
9212 current_function_returns_null = 1;
9214 /* There's really no value to return, after all. */
9215 return NULL_TREE;
9217 else if (!retval)
9218 /* Remember that this function can sometimes return without a
9219 value. */
9220 current_function_returns_null = 1;
9221 else
9222 /* Remember that this function did return a value. */
9223 current_function_returns_value = 1;
9225 /* Check for erroneous operands -- but after giving ourselves a
9226 chance to provide an error about returning a value from a void
9227 function. */
9228 if (error_operand_p (retval))
9230 current_function_return_value = error_mark_node;
9231 return error_mark_node;
9234 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9235 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9236 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9237 && ! flag_check_new
9238 && retval && null_ptr_cst_p (retval))
9239 warning (0, "%<operator new%> must not return NULL unless it is "
9240 "declared %<throw()%> (or -fcheck-new is in effect)");
9242 /* Effective C++ rule 15. See also start_function. */
9243 if (warn_ecpp
9244 && DECL_NAME (current_function_decl) == assign_op_identifier
9245 && !type_dependent_expression_p (retval))
9247 bool warn = true;
9249 /* The function return type must be a reference to the current
9250 class. */
9251 if (TREE_CODE (valtype) == REFERENCE_TYPE
9252 && same_type_ignoring_top_level_qualifiers_p
9253 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9255 /* Returning '*this' is obviously OK. */
9256 if (retval == current_class_ref)
9257 warn = false;
9258 /* If we are calling a function whose return type is the same of
9259 the current class reference, it is ok. */
9260 else if (INDIRECT_REF_P (retval)
9261 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9262 warn = false;
9265 if (warn)
9266 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9269 if (dependent_type_p (functype)
9270 || type_dependent_expression_p (retval))
9272 dependent:
9273 /* We should not have changed the return value. */
9274 gcc_assert (retval == saved_retval);
9275 return retval;
9278 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9280 [...] For a function with a class return type, if the expression
9281 in the return statement is the name of a local object, and the cv-
9282 unqualified type of the local object is the same as the function
9283 return type, an implementation is permitted to omit creating the tem-
9284 porary object to hold the function return value [...]
9286 So, if this is a value-returning function that always returns the same
9287 local variable, remember it.
9289 It might be nice to be more flexible, and choose the first suitable
9290 variable even if the function sometimes returns something else, but
9291 then we run the risk of clobbering the variable we chose if the other
9292 returned expression uses the chosen variable somehow. And people expect
9293 this restriction, anyway. (jason 2000-11-19)
9295 See finish_function and finalize_nrv for the rest of this optimization. */
9297 named_return_value_okay_p =
9298 (retval != NULL_TREE
9299 && !processing_template_decl
9300 /* Must be a local, automatic variable. */
9301 && VAR_P (retval)
9302 && DECL_CONTEXT (retval) == current_function_decl
9303 && ! TREE_STATIC (retval)
9304 /* And not a lambda or anonymous union proxy. */
9305 && !DECL_HAS_VALUE_EXPR_P (retval)
9306 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9307 /* The cv-unqualified type of the returned value must be the
9308 same as the cv-unqualified return type of the
9309 function. */
9310 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9311 (TYPE_MAIN_VARIANT (functype)))
9312 /* And the returned value must be non-volatile. */
9313 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9315 if (fn_returns_value_p && flag_elide_constructors)
9317 if (named_return_value_okay_p
9318 && (current_function_return_value == NULL_TREE
9319 || current_function_return_value == retval))
9320 current_function_return_value = retval;
9321 else
9322 current_function_return_value = error_mark_node;
9325 /* We don't need to do any conversions when there's nothing being
9326 returned. */
9327 if (!retval)
9328 return NULL_TREE;
9330 /* Do any required conversions. */
9331 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9332 /* No conversions are required. */
9334 else
9336 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9338 /* The functype's return type will have been set to void, if it
9339 was an incomplete type. Just treat this as 'return;' */
9340 if (VOID_TYPE_P (functype))
9341 return error_mark_node;
9343 /* If we had an id-expression obfuscated by force_paren_expr, we need
9344 to undo it so we can try to treat it as an rvalue below. */
9345 retval = maybe_undo_parenthesized_ref (retval);
9347 if (processing_template_decl)
9348 retval = build_non_dependent_expr (retval);
9350 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9351 treated as an rvalue for the purposes of overload resolution to
9352 favor move constructors over copy constructors.
9354 Note that these conditions are similar to, but not as strict as,
9355 the conditions for the named return value optimization. */
9356 bool converted = false;
9357 if ((cxx_dialect != cxx98)
9358 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9359 || TREE_CODE (retval) == PARM_DECL)
9360 && DECL_CONTEXT (retval) == current_function_decl
9361 && !TREE_STATIC (retval)
9362 /* This is only interesting for class type. */
9363 && CLASS_TYPE_P (functype))
9365 tree moved = move (retval);
9366 moved = convert_for_initialization
9367 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9368 ICR_RETURN, NULL_TREE, 0, tf_none);
9369 if (moved != error_mark_node)
9371 retval = moved;
9372 converted = true;
9376 /* First convert the value to the function's return type, then
9377 to the type of return value's location to handle the
9378 case that functype is smaller than the valtype. */
9379 if (!converted)
9380 retval = convert_for_initialization
9381 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9382 tf_warning_or_error);
9383 retval = convert (valtype, retval);
9385 /* If the conversion failed, treat this just like `return;'. */
9386 if (retval == error_mark_node)
9387 return retval;
9388 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9389 else if (! cfun->returns_struct
9390 && TREE_CODE (retval) == TARGET_EXPR
9391 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9392 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9393 TREE_OPERAND (retval, 0));
9394 else if (!processing_template_decl
9395 && maybe_warn_about_returning_address_of_local (retval))
9396 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9397 build_zero_cst (TREE_TYPE (retval)));
9400 if (processing_template_decl)
9401 return saved_retval;
9403 /* Actually copy the value returned into the appropriate location. */
9404 if (retval && retval != result)
9405 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9407 return retval;
9411 /* Returns nonzero if the pointer-type FROM can be converted to the
9412 pointer-type TO via a qualification conversion. If CONSTP is -1,
9413 then we return nonzero if the pointers are similar, and the
9414 cv-qualification signature of FROM is a proper subset of that of TO.
9416 If CONSTP is positive, then all outer pointers have been
9417 const-qualified. */
9419 static int
9420 comp_ptr_ttypes_real (tree to, tree from, int constp)
9422 bool to_more_cv_qualified = false;
9423 bool is_opaque_pointer = false;
9425 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9427 if (TREE_CODE (to) != TREE_CODE (from))
9428 return 0;
9430 if (TREE_CODE (from) == OFFSET_TYPE
9431 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9432 TYPE_OFFSET_BASETYPE (to)))
9433 return 0;
9435 /* Const and volatile mean something different for function types,
9436 so the usual checks are not appropriate. */
9437 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9439 if (!at_least_as_qualified_p (to, from))
9440 return 0;
9442 if (!at_least_as_qualified_p (from, to))
9444 if (constp == 0)
9445 return 0;
9446 to_more_cv_qualified = true;
9449 if (constp > 0)
9450 constp &= TYPE_READONLY (to);
9453 if (VECTOR_TYPE_P (to))
9454 is_opaque_pointer = vector_targets_convertible_p (to, from);
9456 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9457 return ((constp >= 0 || to_more_cv_qualified)
9458 && (is_opaque_pointer
9459 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9463 /* When comparing, say, char ** to char const **, this function takes
9464 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9465 types to this function. */
9468 comp_ptr_ttypes (tree to, tree from)
9470 return comp_ptr_ttypes_real (to, from, 1);
9473 /* Returns true iff FNTYPE is a non-class type that involves
9474 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9475 if a parameter type is ill-formed. */
9477 bool
9478 error_type_p (const_tree type)
9480 tree t;
9482 switch (TREE_CODE (type))
9484 case ERROR_MARK:
9485 return true;
9487 case POINTER_TYPE:
9488 case REFERENCE_TYPE:
9489 case OFFSET_TYPE:
9490 return error_type_p (TREE_TYPE (type));
9492 case FUNCTION_TYPE:
9493 case METHOD_TYPE:
9494 if (error_type_p (TREE_TYPE (type)))
9495 return true;
9496 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9497 if (error_type_p (TREE_VALUE (t)))
9498 return true;
9499 return false;
9501 case RECORD_TYPE:
9502 if (TYPE_PTRMEMFUNC_P (type))
9503 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9504 return false;
9506 default:
9507 return false;
9511 /* Returns true if to and from are (possibly multi-level) pointers to the same
9512 type or inheritance-related types, regardless of cv-quals. */
9514 bool
9515 ptr_reasonably_similar (const_tree to, const_tree from)
9517 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9519 /* Any target type is similar enough to void. */
9520 if (VOID_TYPE_P (to))
9521 return !error_type_p (from);
9522 if (VOID_TYPE_P (from))
9523 return !error_type_p (to);
9525 if (TREE_CODE (to) != TREE_CODE (from))
9526 return false;
9528 if (TREE_CODE (from) == OFFSET_TYPE
9529 && comptypes (TYPE_OFFSET_BASETYPE (to),
9530 TYPE_OFFSET_BASETYPE (from),
9531 COMPARE_BASE | COMPARE_DERIVED))
9532 continue;
9534 if (VECTOR_TYPE_P (to)
9535 && vector_types_convertible_p (to, from, false))
9536 return true;
9538 if (TREE_CODE (to) == INTEGER_TYPE
9539 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9540 return true;
9542 if (TREE_CODE (to) == FUNCTION_TYPE)
9543 return !error_type_p (to) && !error_type_p (from);
9545 if (!TYPE_PTR_P (to))
9547 /* When either type is incomplete avoid DERIVED_FROM_P,
9548 which may call complete_type (c++/57942). */
9549 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9550 return comptypes
9551 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9552 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9557 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9558 pointer-to-member types) are the same, ignoring cv-qualification at
9559 all levels. */
9561 bool
9562 comp_ptr_ttypes_const (tree to, tree from)
9564 bool is_opaque_pointer = false;
9566 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9568 if (TREE_CODE (to) != TREE_CODE (from))
9569 return false;
9571 if (TREE_CODE (from) == OFFSET_TYPE
9572 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9573 TYPE_OFFSET_BASETYPE (to)))
9574 continue;
9576 if (VECTOR_TYPE_P (to))
9577 is_opaque_pointer = vector_targets_convertible_p (to, from);
9579 if (!TYPE_PTR_P (to))
9580 return (is_opaque_pointer
9581 || same_type_ignoring_top_level_qualifiers_p (to, from));
9585 /* Returns the type qualifiers for this type, including the qualifiers on the
9586 elements for an array type. */
9589 cp_type_quals (const_tree type)
9591 int quals;
9592 /* This CONST_CAST is okay because strip_array_types returns its
9593 argument unmodified and we assign it to a const_tree. */
9594 type = strip_array_types (CONST_CAST_TREE (type));
9595 if (type == error_mark_node
9596 /* Quals on a FUNCTION_TYPE are memfn quals. */
9597 || TREE_CODE (type) == FUNCTION_TYPE)
9598 return TYPE_UNQUALIFIED;
9599 quals = TYPE_QUALS (type);
9600 /* METHOD and REFERENCE_TYPEs should never have quals. */
9601 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9602 && TREE_CODE (type) != REFERENCE_TYPE)
9603 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9604 == TYPE_UNQUALIFIED));
9605 return quals;
9608 /* Returns the function-ref-qualifier for TYPE */
9610 cp_ref_qualifier
9611 type_memfn_rqual (const_tree type)
9613 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9614 || TREE_CODE (type) == METHOD_TYPE);
9616 if (!FUNCTION_REF_QUALIFIED (type))
9617 return REF_QUAL_NONE;
9618 else if (FUNCTION_RVALUE_QUALIFIED (type))
9619 return REF_QUAL_RVALUE;
9620 else
9621 return REF_QUAL_LVALUE;
9624 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9625 METHOD_TYPE. */
9628 type_memfn_quals (const_tree type)
9630 if (TREE_CODE (type) == FUNCTION_TYPE)
9631 return TYPE_QUALS (type);
9632 else if (TREE_CODE (type) == METHOD_TYPE)
9633 return cp_type_quals (class_of_this_parm (type));
9634 else
9635 gcc_unreachable ();
9638 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9639 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9641 tree
9642 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9644 /* Could handle METHOD_TYPE here if necessary. */
9645 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9646 if (TYPE_QUALS (type) == memfn_quals
9647 && type_memfn_rqual (type) == rqual)
9648 return type;
9650 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9651 complex. */
9652 tree result = build_qualified_type (type, memfn_quals);
9653 return build_ref_qualified_type (result, rqual);
9656 /* Returns nonzero if TYPE is const or volatile. */
9658 bool
9659 cv_qualified_p (const_tree type)
9661 int quals = cp_type_quals (type);
9662 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9665 /* Returns nonzero if the TYPE contains a mutable member. */
9667 bool
9668 cp_has_mutable_p (const_tree type)
9670 /* This CONST_CAST is okay because strip_array_types returns its
9671 argument unmodified and we assign it to a const_tree. */
9672 type = strip_array_types (CONST_CAST_TREE(type));
9674 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9677 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9678 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9679 approximation. In particular, consider:
9681 int f();
9682 struct S { int i; };
9683 const S s = { f(); }
9685 Here, we will make "s" as TREE_READONLY (because it is declared
9686 "const") -- only to reverse ourselves upon seeing that the
9687 initializer is non-constant. */
9689 void
9690 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9692 tree type = TREE_TYPE (decl);
9694 if (type == error_mark_node)
9695 return;
9697 if (TREE_CODE (decl) == TYPE_DECL)
9698 return;
9700 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9701 && type_quals != TYPE_UNQUALIFIED));
9703 /* Avoid setting TREE_READONLY incorrectly. */
9704 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9705 constructor can produce constant init, so rely on cp_finish_decl to
9706 clear TREE_READONLY if the variable has non-constant init. */
9708 /* If the type has (or might have) a mutable component, that component
9709 might be modified. */
9710 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9711 type_quals &= ~TYPE_QUAL_CONST;
9713 c_apply_type_quals_to_decl (type_quals, decl);
9716 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9717 exemplar types such that casting T1 to T2 is casting away constness
9718 if and only if there is no implicit conversion from T1 to T2. */
9720 static void
9721 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9723 int quals1;
9724 int quals2;
9726 /* [expr.const.cast]
9728 For multi-level pointer to members and multi-level mixed pointers
9729 and pointers to members (conv.qual), the "member" aspect of a
9730 pointer to member level is ignored when determining if a const
9731 cv-qualifier has been cast away. */
9732 /* [expr.const.cast]
9734 For two pointer types:
9736 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9737 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9738 K is min(N,M)
9740 casting from X1 to X2 casts away constness if, for a non-pointer
9741 type T there does not exist an implicit conversion (clause
9742 _conv_) from:
9744 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9748 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9749 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9750 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9752 *t1 = cp_build_qualified_type (void_type_node,
9753 cp_type_quals (*t1));
9754 *t2 = cp_build_qualified_type (void_type_node,
9755 cp_type_quals (*t2));
9756 return;
9759 quals1 = cp_type_quals (*t1);
9760 quals2 = cp_type_quals (*t2);
9762 if (TYPE_PTRDATAMEM_P (*t1))
9763 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9764 else
9765 *t1 = TREE_TYPE (*t1);
9766 if (TYPE_PTRDATAMEM_P (*t2))
9767 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9768 else
9769 *t2 = TREE_TYPE (*t2);
9771 casts_away_constness_r (t1, t2, complain);
9772 *t1 = build_pointer_type (*t1);
9773 *t2 = build_pointer_type (*t2);
9774 *t1 = cp_build_qualified_type (*t1, quals1);
9775 *t2 = cp_build_qualified_type (*t2, quals2);
9778 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9779 constness.
9781 ??? This function returns non-zero if casting away qualifiers not
9782 just const. We would like to return to the caller exactly which
9783 qualifiers are casted away to give more accurate diagnostics.
9786 static bool
9787 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9789 if (TREE_CODE (t2) == REFERENCE_TYPE)
9791 /* [expr.const.cast]
9793 Casting from an lvalue of type T1 to an lvalue of type T2
9794 using a reference cast casts away constness if a cast from an
9795 rvalue of type "pointer to T1" to the type "pointer to T2"
9796 casts away constness. */
9797 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9798 return casts_away_constness (build_pointer_type (t1),
9799 build_pointer_type (TREE_TYPE (t2)),
9800 complain);
9803 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9804 /* [expr.const.cast]
9806 Casting from an rvalue of type "pointer to data member of X
9807 of type T1" to the type "pointer to data member of Y of type
9808 T2" casts away constness if a cast from an rvalue of type
9809 "pointer to T1" to the type "pointer to T2" casts away
9810 constness. */
9811 return casts_away_constness
9812 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9813 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9814 complain);
9816 /* Casting away constness is only something that makes sense for
9817 pointer or reference types. */
9818 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9819 return false;
9821 /* Top-level qualifiers don't matter. */
9822 t1 = TYPE_MAIN_VARIANT (t1);
9823 t2 = TYPE_MAIN_VARIANT (t2);
9824 casts_away_constness_r (&t1, &t2, complain);
9825 if (!can_convert (t2, t1, complain))
9826 return true;
9828 return false;
9831 /* If T is a REFERENCE_TYPE return the type to which T refers.
9832 Otherwise, return T itself. */
9834 tree
9835 non_reference (tree t)
9837 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9838 t = TREE_TYPE (t);
9839 return t;
9843 /* Return nonzero if REF is an lvalue valid for this language;
9844 otherwise, print an error message and return zero. USE says
9845 how the lvalue is being used and so selects the error message. */
9848 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9850 cp_lvalue_kind kind = lvalue_kind (ref);
9852 if (kind == clk_none)
9854 if (complain & tf_error)
9855 lvalue_error (input_location, use);
9856 return 0;
9858 else if (kind & (clk_rvalueref|clk_class))
9860 if (!(complain & tf_error))
9861 return 0;
9862 if (kind & clk_class)
9863 /* Make this a permerror because we used to accept it. */
9864 permerror (input_location, "using temporary as lvalue");
9865 else
9866 error ("using xvalue (rvalue reference) as lvalue");
9868 return 1;
9871 /* Return true if a user-defined literal operator is a raw operator. */
9873 bool
9874 check_raw_literal_operator (const_tree decl)
9876 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9877 tree argtype;
9878 int arity;
9879 bool maybe_raw_p = false;
9881 /* Count the number and type of arguments and check for ellipsis. */
9882 for (argtype = argtypes, arity = 0;
9883 argtype && argtype != void_list_node;
9884 ++arity, argtype = TREE_CHAIN (argtype))
9886 tree t = TREE_VALUE (argtype);
9888 if (same_type_p (t, const_string_type_node))
9889 maybe_raw_p = true;
9891 if (!argtype)
9892 return false; /* Found ellipsis. */
9894 if (!maybe_raw_p || arity != 1)
9895 return false;
9897 return true;
9901 /* Return true if a user-defined literal operator has one of the allowed
9902 argument types. */
9904 bool
9905 check_literal_operator_args (const_tree decl,
9906 bool *long_long_unsigned_p, bool *long_double_p)
9908 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9910 *long_long_unsigned_p = false;
9911 *long_double_p = false;
9912 if (processing_template_decl || processing_specialization)
9913 return argtypes == void_list_node;
9914 else
9916 tree argtype;
9917 int arity;
9918 int max_arity = 2;
9920 /* Count the number and type of arguments and check for ellipsis. */
9921 for (argtype = argtypes, arity = 0;
9922 argtype && argtype != void_list_node;
9923 argtype = TREE_CHAIN (argtype))
9925 tree t = TREE_VALUE (argtype);
9926 ++arity;
9928 if (TYPE_PTR_P (t))
9930 bool maybe_raw_p = false;
9931 t = TREE_TYPE (t);
9932 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9933 return false;
9934 t = TYPE_MAIN_VARIANT (t);
9935 if ((maybe_raw_p = same_type_p (t, char_type_node))
9936 || same_type_p (t, wchar_type_node)
9937 || same_type_p (t, char16_type_node)
9938 || same_type_p (t, char32_type_node))
9940 argtype = TREE_CHAIN (argtype);
9941 if (!argtype)
9942 return false;
9943 t = TREE_VALUE (argtype);
9944 if (maybe_raw_p && argtype == void_list_node)
9945 return true;
9946 else if (same_type_p (t, size_type_node))
9948 ++arity;
9949 continue;
9951 else
9952 return false;
9955 else if (same_type_p (t, long_long_unsigned_type_node))
9957 max_arity = 1;
9958 *long_long_unsigned_p = true;
9960 else if (same_type_p (t, long_double_type_node))
9962 max_arity = 1;
9963 *long_double_p = true;
9965 else if (same_type_p (t, char_type_node))
9966 max_arity = 1;
9967 else if (same_type_p (t, wchar_type_node))
9968 max_arity = 1;
9969 else if (same_type_p (t, char16_type_node))
9970 max_arity = 1;
9971 else if (same_type_p (t, char32_type_node))
9972 max_arity = 1;
9973 else
9974 return false;
9976 if (!argtype)
9977 return false; /* Found ellipsis. */
9979 if (arity != max_arity)
9980 return false;
9982 return true;
9986 /* Always returns false since unlike C90, C++ has no concept of implicit
9987 function declarations. */
9989 bool
9990 c_decl_implicit (const_tree)
9992 return false;