[NDS32] new attribute no_prologue and new option -mret-in-naked-func.
[official-gcc.git] / gcc / cp / typeck.c
blob3df043e2ab4a1b3b7f0c41118126d1a3490ead31
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "target.h"
31 #include "cp-tree.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "intl.h"
35 #include "convert.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
38 #include "params.h"
39 #include "gcc-rich-location.h"
40 #include "stringpool.h"
41 #include "attribs.h"
42 #include "asan.h"
44 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
45 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
46 static tree pfn_from_ptrmemfunc (tree);
47 static tree delta_from_ptrmemfunc (tree);
48 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
49 tsubst_flags_t, int);
50 static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
51 tsubst_flags_t);
52 static tree rationalize_conditional_expr (enum tree_code, tree,
53 tsubst_flags_t);
54 static int comp_ptr_ttypes_real (tree, tree, int);
55 static bool comp_except_types (tree, tree, bool);
56 static bool comp_array_types (const_tree, const_tree, bool);
57 static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
58 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
59 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
60 static bool casts_away_constness (tree, tree, tsubst_flags_t);
61 static bool maybe_warn_about_returning_address_of_local (tree);
62 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
63 static void error_args_num (location_t, tree, bool);
64 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
65 tsubst_flags_t);
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.)
69 Returns error_mark_node if the VALUE does not have
70 complete type when this function returns. */
72 tree
73 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
75 tree type;
77 if (processing_template_decl || value == error_mark_node)
78 return value;
80 if (TREE_CODE (value) == OVERLOAD)
81 type = unknown_type_node;
82 else
83 type = TREE_TYPE (value);
85 if (type == error_mark_node)
86 return error_mark_node;
88 /* First, detect a valid value with a complete type. */
89 if (COMPLETE_TYPE_P (type))
90 return value;
92 if (complete_type_or_maybe_complain (type, value, complain))
93 return value;
94 else
95 return error_mark_node;
98 tree
99 require_complete_type (tree value)
101 return require_complete_type_sfinae (value, tf_warning_or_error);
104 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
105 a template instantiation, do the instantiation. Returns TYPE,
106 whether or not it could be completed, unless something goes
107 horribly wrong, in which case the error_mark_node is returned. */
109 tree
110 complete_type (tree type)
112 if (type == NULL_TREE)
113 /* Rather than crash, we return something sure to cause an error
114 at some point. */
115 return error_mark_node;
117 if (type == error_mark_node || COMPLETE_TYPE_P (type))
119 else if (TREE_CODE (type) == ARRAY_TYPE)
121 tree t = complete_type (TREE_TYPE (type));
122 unsigned int needs_constructing, has_nontrivial_dtor;
123 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
124 layout_type (type);
125 needs_constructing
126 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
127 has_nontrivial_dtor
128 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
129 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
131 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
132 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
135 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
136 instantiate_class_template (TYPE_MAIN_VARIANT (type));
138 return type;
141 /* Like complete_type, but issue an error if the TYPE cannot be completed.
142 VALUE is used for informative diagnostics.
143 Returns NULL_TREE if the type cannot be made complete. */
145 tree
146 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
148 type = complete_type (type);
149 if (type == error_mark_node)
150 /* We already issued an error. */
151 return NULL_TREE;
152 else if (!COMPLETE_TYPE_P (type))
154 if (complain & tf_error)
155 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
156 return NULL_TREE;
158 else
159 return type;
162 tree
163 complete_type_or_else (tree type, tree value)
165 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
169 /* Return the common type of two parameter lists.
170 We assume that comptypes has already been done and returned 1;
171 if that isn't so, this may crash.
173 As an optimization, free the space we allocate if the parameter
174 lists are already common. */
176 static tree
177 commonparms (tree p1, tree p2)
179 tree oldargs = p1, newargs, n;
180 int i, len;
181 int any_change = 0;
183 len = list_length (p1);
184 newargs = tree_last (p1);
186 if (newargs == void_list_node)
187 i = 1;
188 else
190 i = 0;
191 newargs = 0;
194 for (; i < len; i++)
195 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
197 n = newargs;
199 for (i = 0; p1;
200 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
202 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
204 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
205 any_change = 1;
207 else if (! TREE_PURPOSE (p1))
209 if (TREE_PURPOSE (p2))
211 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
212 any_change = 1;
215 else
217 if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
218 any_change = 1;
219 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 if (TREE_VALUE (p1) != TREE_VALUE (p2))
223 any_change = 1;
224 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
226 else
227 TREE_VALUE (n) = TREE_VALUE (p1);
229 if (! any_change)
230 return oldargs;
232 return newargs;
235 /* Given a type, perhaps copied for a typedef,
236 find the "original" version of it. */
237 static tree
238 original_type (tree t)
240 int quals = cp_type_quals (t);
241 while (t != error_mark_node
242 && TYPE_NAME (t) != NULL_TREE)
244 tree x = TYPE_NAME (t);
245 if (TREE_CODE (x) != TYPE_DECL)
246 break;
247 x = DECL_ORIGINAL_TYPE (x);
248 if (x == NULL_TREE)
249 break;
250 t = x;
252 return cp_build_qualified_type (t, quals);
255 /* Return the common type for two arithmetic types T1 and T2 under the
256 usual arithmetic conversions. The default conversions have already
257 been applied, and enumerated types converted to their compatible
258 integer types. */
260 static tree
261 cp_common_type (tree t1, tree t2)
263 enum tree_code code1 = TREE_CODE (t1);
264 enum tree_code code2 = TREE_CODE (t2);
265 tree attributes;
266 int i;
269 /* In what follows, we slightly generalize the rules given in [expr] so
270 as to deal with `long long' and `complex'. First, merge the
271 attributes. */
272 attributes = (*targetm.merge_type_attributes) (t1, t2);
274 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
276 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
277 return build_type_attribute_variant (t1, attributes);
278 else
279 return NULL_TREE;
282 /* FIXME: Attributes. */
283 gcc_assert (ARITHMETIC_TYPE_P (t1)
284 || VECTOR_TYPE_P (t1)
285 || UNSCOPED_ENUM_P (t1));
286 gcc_assert (ARITHMETIC_TYPE_P (t2)
287 || VECTOR_TYPE_P (t2)
288 || UNSCOPED_ENUM_P (t2));
290 /* If one type is complex, form the common type of the non-complex
291 components, then make that complex. Use T1 or T2 if it is the
292 required type. */
293 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
295 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
296 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
297 tree subtype
298 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
300 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
301 return build_type_attribute_variant (t1, attributes);
302 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
303 return build_type_attribute_variant (t2, attributes);
304 else
305 return build_type_attribute_variant (build_complex_type (subtype),
306 attributes);
309 if (code1 == VECTOR_TYPE)
311 /* When we get here we should have two vectors of the same size.
312 Just prefer the unsigned one if present. */
313 if (TYPE_UNSIGNED (t1))
314 return build_type_attribute_variant (t1, attributes);
315 else
316 return build_type_attribute_variant (t2, attributes);
319 /* If only one is real, use it as the result. */
320 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
321 return build_type_attribute_variant (t1, attributes);
322 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
323 return build_type_attribute_variant (t2, attributes);
325 /* Both real or both integers; use the one with greater precision. */
326 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
327 return build_type_attribute_variant (t1, attributes);
328 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
329 return build_type_attribute_variant (t2, attributes);
331 /* The types are the same; no need to do anything fancy. */
332 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
333 return build_type_attribute_variant (t1, attributes);
335 if (code1 != REAL_TYPE)
337 /* If one is unsigned long long, then convert the other to unsigned
338 long long. */
339 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
340 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
341 return build_type_attribute_variant (long_long_unsigned_type_node,
342 attributes);
343 /* If one is a long long, and the other is an unsigned long, and
344 long long can represent all the values of an unsigned long, then
345 convert to a long long. Otherwise, convert to an unsigned long
346 long. Otherwise, if either operand is long long, convert the
347 other to long long.
349 Since we're here, we know the TYPE_PRECISION is the same;
350 therefore converting to long long cannot represent all the values
351 of an unsigned long, so we choose unsigned long long in that
352 case. */
353 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
354 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
356 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
357 ? long_long_unsigned_type_node
358 : long_long_integer_type_node);
359 return build_type_attribute_variant (t, attributes);
362 /* Go through the same procedure, but for longs. */
363 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
364 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
365 return build_type_attribute_variant (long_unsigned_type_node,
366 attributes);
367 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
368 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
370 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
371 ? long_unsigned_type_node : long_integer_type_node);
372 return build_type_attribute_variant (t, attributes);
375 /* For __intN types, either the type is __int128 (and is lower
376 priority than the types checked above, but higher than other
377 128-bit types) or it's known to not be the same size as other
378 types (enforced in toplev.c). Prefer the unsigned type. */
379 for (i = 0; i < NUM_INT_N_ENTS; i ++)
381 if (int_n_enabled_p [i]
382 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
383 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
384 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
385 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
387 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
388 ? int_n_trees[i].unsigned_type
389 : int_n_trees[i].signed_type);
390 return build_type_attribute_variant (t, attributes);
394 /* Otherwise prefer the unsigned one. */
395 if (TYPE_UNSIGNED (t1))
396 return build_type_attribute_variant (t1, attributes);
397 else
398 return build_type_attribute_variant (t2, attributes);
400 else
402 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
403 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
404 return build_type_attribute_variant (long_double_type_node,
405 attributes);
406 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
407 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
408 return build_type_attribute_variant (double_type_node,
409 attributes);
410 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
411 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
412 return build_type_attribute_variant (float_type_node,
413 attributes);
415 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
416 the standard C++ floating-point types. Logic earlier in this
417 function has already eliminated the possibility that
418 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
419 compelling reason to choose one or the other. */
420 return build_type_attribute_variant (t1, attributes);
424 /* T1 and T2 are arithmetic or enumeration types. Return the type
425 that will result from the "usual arithmetic conversions" on T1 and
426 T2 as described in [expr]. */
428 tree
429 type_after_usual_arithmetic_conversions (tree t1, tree t2)
431 gcc_assert (ARITHMETIC_TYPE_P (t1)
432 || VECTOR_TYPE_P (t1)
433 || UNSCOPED_ENUM_P (t1));
434 gcc_assert (ARITHMETIC_TYPE_P (t2)
435 || VECTOR_TYPE_P (t2)
436 || UNSCOPED_ENUM_P (t2));
438 /* Perform the integral promotions. We do not promote real types here. */
439 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
440 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
442 t1 = type_promotes_to (t1);
443 t2 = type_promotes_to (t2);
446 return cp_common_type (t1, t2);
449 static void
450 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
451 composite_pointer_operation operation)
453 switch (operation)
455 case CPO_COMPARISON:
456 emit_diagnostic (kind, input_location, 0,
457 "comparison between "
458 "distinct pointer types %qT and %qT lacks a cast",
459 t1, t2);
460 break;
461 case CPO_CONVERSION:
462 emit_diagnostic (kind, input_location, 0,
463 "conversion between "
464 "distinct pointer types %qT and %qT lacks a cast",
465 t1, t2);
466 break;
467 case CPO_CONDITIONAL_EXPR:
468 emit_diagnostic (kind, input_location, 0,
469 "conditional expression between "
470 "distinct pointer types %qT and %qT lacks a cast",
471 t1, t2);
472 break;
473 default:
474 gcc_unreachable ();
478 /* Subroutine of composite_pointer_type to implement the recursive
479 case. See that function for documentation of the parameters. */
481 static tree
482 composite_pointer_type_r (tree t1, tree t2,
483 composite_pointer_operation operation,
484 tsubst_flags_t complain)
486 tree pointee1;
487 tree pointee2;
488 tree result_type;
489 tree attributes;
491 /* Determine the types pointed to by T1 and T2. */
492 if (TYPE_PTR_P (t1))
494 pointee1 = TREE_TYPE (t1);
495 pointee2 = TREE_TYPE (t2);
497 else
499 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
500 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
503 /* [expr.rel]
505 Otherwise, the composite pointer type is a pointer type
506 similar (_conv.qual_) to the type of one of the operands,
507 with a cv-qualification signature (_conv.qual_) that is the
508 union of the cv-qualification signatures of the operand
509 types. */
510 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
511 result_type = pointee1;
512 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
513 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
515 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
516 complain);
517 if (result_type == error_mark_node)
518 return error_mark_node;
520 else
522 if (complain & tf_error)
523 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
524 else
525 return error_mark_node;
526 result_type = void_type_node;
528 result_type = cp_build_qualified_type (result_type,
529 (cp_type_quals (pointee1)
530 | cp_type_quals (pointee2)));
531 /* If the original types were pointers to members, so is the
532 result. */
533 if (TYPE_PTRMEM_P (t1))
535 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
536 TYPE_PTRMEM_CLASS_TYPE (t2)))
538 if (complain & tf_error)
539 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
540 else
541 return error_mark_node;
543 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
544 result_type);
546 else
547 result_type = build_pointer_type (result_type);
549 /* Merge the attributes. */
550 attributes = (*targetm.merge_type_attributes) (t1, t2);
551 return build_type_attribute_variant (result_type, attributes);
554 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
555 ARG1 and ARG2 are the values with those types. The OPERATION is to
556 describe the operation between the pointer types,
557 in case an error occurs.
559 This routine also implements the computation of a common type for
560 pointers-to-members as per [expr.eq]. */
562 tree
563 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
564 composite_pointer_operation operation,
565 tsubst_flags_t complain)
567 tree class1;
568 tree class2;
570 /* [expr.rel]
572 If one operand is a null pointer constant, the composite pointer
573 type is the type of the other operand. */
574 if (null_ptr_cst_p (arg1))
575 return t2;
576 if (null_ptr_cst_p (arg2))
577 return t1;
579 /* We have:
581 [expr.rel]
583 If one of the operands has type "pointer to cv1 void*", then
584 the other has type "pointer to cv2T", and the composite pointer
585 type is "pointer to cv12 void", where cv12 is the union of cv1
586 and cv2.
588 If either type is a pointer to void, make sure it is T1. */
589 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
590 std::swap (t1, t2);
592 /* Now, if T1 is a pointer to void, merge the qualifiers. */
593 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
595 tree attributes;
596 tree result_type;
598 if (TYPE_PTRFN_P (t2))
600 if (complain & tf_error)
602 switch (operation)
604 case CPO_COMPARISON:
605 pedwarn (input_location, OPT_Wpedantic,
606 "ISO C++ forbids comparison between pointer "
607 "of type %<void *%> and pointer-to-function");
608 break;
609 case CPO_CONVERSION:
610 pedwarn (input_location, OPT_Wpedantic,
611 "ISO C++ forbids conversion between pointer "
612 "of type %<void *%> and pointer-to-function");
613 break;
614 case CPO_CONDITIONAL_EXPR:
615 pedwarn (input_location, OPT_Wpedantic,
616 "ISO C++ forbids conditional expression between "
617 "pointer of type %<void *%> and "
618 "pointer-to-function");
619 break;
620 default:
621 gcc_unreachable ();
624 else
625 return error_mark_node;
627 result_type
628 = cp_build_qualified_type (void_type_node,
629 (cp_type_quals (TREE_TYPE (t1))
630 | cp_type_quals (TREE_TYPE (t2))));
631 result_type = build_pointer_type (result_type);
632 /* Merge the attributes. */
633 attributes = (*targetm.merge_type_attributes) (t1, t2);
634 return build_type_attribute_variant (result_type, attributes);
637 if (c_dialect_objc () && TYPE_PTR_P (t1)
638 && TYPE_PTR_P (t2))
640 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
641 return objc_common_type (t1, t2);
644 /* if T1 or T2 is "pointer to noexcept function" and the other type is
645 "pointer to function", where the function types are otherwise the same,
646 "pointer to function" */
647 if (fnptr_conv_p (t1, t2))
648 return t1;
649 if (fnptr_conv_p (t2, t1))
650 return t2;
652 /* [expr.eq] permits the application of a pointer conversion to
653 bring the pointers to a common type. */
654 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
655 && CLASS_TYPE_P (TREE_TYPE (t1))
656 && CLASS_TYPE_P (TREE_TYPE (t2))
657 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
658 TREE_TYPE (t2)))
660 class1 = TREE_TYPE (t1);
661 class2 = TREE_TYPE (t2);
663 if (DERIVED_FROM_P (class1, class2))
664 t2 = (build_pointer_type
665 (cp_build_qualified_type (class1, cp_type_quals (class2))));
666 else if (DERIVED_FROM_P (class2, class1))
667 t1 = (build_pointer_type
668 (cp_build_qualified_type (class2, cp_type_quals (class1))));
669 else
671 if (complain & tf_error)
672 composite_pointer_error (DK_ERROR, t1, t2, operation);
673 return error_mark_node;
676 /* [expr.eq] permits the application of a pointer-to-member
677 conversion to change the class type of one of the types. */
678 else if (TYPE_PTRMEM_P (t1)
679 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
680 TYPE_PTRMEM_CLASS_TYPE (t2)))
682 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
683 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
685 if (DERIVED_FROM_P (class1, class2))
686 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
687 else if (DERIVED_FROM_P (class2, class1))
688 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
689 else
691 if (complain & tf_error)
692 switch (operation)
694 case CPO_COMPARISON:
695 error ("comparison between distinct "
696 "pointer-to-member types %qT and %qT lacks a cast",
697 t1, t2);
698 break;
699 case CPO_CONVERSION:
700 error ("conversion between distinct "
701 "pointer-to-member types %qT and %qT lacks a cast",
702 t1, t2);
703 break;
704 case CPO_CONDITIONAL_EXPR:
705 error ("conditional expression between distinct "
706 "pointer-to-member types %qT and %qT lacks a cast",
707 t1, t2);
708 break;
709 default:
710 gcc_unreachable ();
712 return error_mark_node;
716 return composite_pointer_type_r (t1, t2, operation, complain);
719 /* Return the merged type of two types.
720 We assume that comptypes has already been done and returned 1;
721 if that isn't so, this may crash.
723 This just combines attributes and default arguments; any other
724 differences would cause the two types to compare unalike. */
726 tree
727 merge_types (tree t1, tree t2)
729 enum tree_code code1;
730 enum tree_code code2;
731 tree attributes;
733 /* Save time if the two types are the same. */
734 if (t1 == t2)
735 return t1;
736 if (original_type (t1) == original_type (t2))
737 return t1;
739 /* If one type is nonsense, use the other. */
740 if (t1 == error_mark_node)
741 return t2;
742 if (t2 == error_mark_node)
743 return t1;
745 /* Handle merging an auto redeclaration with a previous deduced
746 return type. */
747 if (is_auto (t1))
748 return t2;
750 /* Merge the attributes. */
751 attributes = (*targetm.merge_type_attributes) (t1, t2);
753 if (TYPE_PTRMEMFUNC_P (t1))
754 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
755 if (TYPE_PTRMEMFUNC_P (t2))
756 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
758 code1 = TREE_CODE (t1);
759 code2 = TREE_CODE (t2);
760 if (code1 != code2)
762 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
763 if (code1 == TYPENAME_TYPE)
765 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
766 code1 = TREE_CODE (t1);
768 else
770 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
771 code2 = TREE_CODE (t2);
775 switch (code1)
777 case POINTER_TYPE:
778 case REFERENCE_TYPE:
779 /* For two pointers, do this recursively on the target type. */
781 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
782 int quals = cp_type_quals (t1);
784 if (code1 == POINTER_TYPE)
786 t1 = build_pointer_type (target);
787 if (TREE_CODE (target) == METHOD_TYPE)
788 t1 = build_ptrmemfunc_type (t1);
790 else
791 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
792 t1 = build_type_attribute_variant (t1, attributes);
793 t1 = cp_build_qualified_type (t1, quals);
795 return t1;
798 case OFFSET_TYPE:
800 int quals;
801 tree pointee;
802 quals = cp_type_quals (t1);
803 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
804 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
805 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
806 pointee);
807 t1 = cp_build_qualified_type (t1, quals);
808 break;
811 case ARRAY_TYPE:
813 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
814 /* Save space: see if the result is identical to one of the args. */
815 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
816 return build_type_attribute_variant (t1, attributes);
817 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
818 return build_type_attribute_variant (t2, attributes);
819 /* Merge the element types, and have a size if either arg has one. */
820 t1 = build_cplus_array_type
821 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
822 break;
825 case FUNCTION_TYPE:
826 /* Function types: prefer the one that specified arg types.
827 If both do, merge the arg types. Also merge the return types. */
829 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
830 tree p1 = TYPE_ARG_TYPES (t1);
831 tree p2 = TYPE_ARG_TYPES (t2);
832 tree parms;
834 /* Save space: see if the result is identical to one of the args. */
835 if (valtype == TREE_TYPE (t1) && ! p2)
836 return cp_build_type_attribute_variant (t1, attributes);
837 if (valtype == TREE_TYPE (t2) && ! p1)
838 return cp_build_type_attribute_variant (t2, attributes);
840 /* Simple way if one arg fails to specify argument types. */
841 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
842 parms = p2;
843 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
844 parms = p1;
845 else
846 parms = commonparms (p1, p2);
848 cp_cv_quals quals = type_memfn_quals (t1);
849 cp_ref_qualifier rqual = type_memfn_rqual (t1);
850 gcc_assert (quals == type_memfn_quals (t2));
851 gcc_assert (rqual == type_memfn_rqual (t2));
853 tree rval = build_function_type (valtype, parms);
854 rval = apply_memfn_quals (rval, quals);
855 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
856 TYPE_RAISES_EXCEPTIONS (t2));
857 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
858 t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
859 break;
862 case METHOD_TYPE:
864 /* Get this value the long way, since TYPE_METHOD_BASETYPE
865 is just the main variant of this. */
866 tree basetype = class_of_this_parm (t2);
867 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
868 TYPE_RAISES_EXCEPTIONS (t2));
869 cp_ref_qualifier rqual = type_memfn_rqual (t1);
870 tree t3;
871 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
873 /* If this was a member function type, get back to the
874 original type of type member function (i.e., without
875 the class instance variable up front. */
876 t1 = build_function_type (TREE_TYPE (t1),
877 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
878 t2 = build_function_type (TREE_TYPE (t2),
879 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
880 t3 = merge_types (t1, t2);
881 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
882 TYPE_ARG_TYPES (t3));
883 t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
884 break;
887 case TYPENAME_TYPE:
888 /* There is no need to merge attributes into a TYPENAME_TYPE.
889 When the type is instantiated it will have whatever
890 attributes result from the instantiation. */
891 return t1;
893 default:;
894 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
895 return t1;
896 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
897 return t2;
898 break;
901 return cp_build_type_attribute_variant (t1, attributes);
904 /* Return the ARRAY_TYPE type without its domain. */
906 tree
907 strip_array_domain (tree type)
909 tree t2;
910 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
911 if (TYPE_DOMAIN (type) == NULL_TREE)
912 return type;
913 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
914 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
917 /* Wrapper around cp_common_type that is used by c-common.c and other
918 front end optimizations that remove promotions.
920 Return the common type for two arithmetic types T1 and T2 under the
921 usual arithmetic conversions. The default conversions have already
922 been applied, and enumerated types converted to their compatible
923 integer types. */
925 tree
926 common_type (tree t1, tree t2)
928 /* If one type is nonsense, use the other */
929 if (t1 == error_mark_node)
930 return t2;
931 if (t2 == error_mark_node)
932 return t1;
934 return cp_common_type (t1, t2);
937 /* Return the common type of two pointer types T1 and T2. This is the
938 type for the result of most arithmetic operations if the operands
939 have the given two types.
941 We assume that comp_target_types has already been done and returned
942 nonzero; if that isn't so, this may crash. */
944 tree
945 common_pointer_type (tree t1, tree t2)
947 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
948 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
949 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
951 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
952 CPO_CONVERSION, tf_warning_or_error);
955 /* Compare two exception specifier types for exactness or subsetness, if
956 allowed. Returns false for mismatch, true for match (same, or
957 derived and !exact).
959 [except.spec] "If a class X ... objects of class X or any class publicly
960 and unambiguously derived from X. Similarly, if a pointer type Y * ...
961 exceptions of type Y * or that are pointers to any type publicly and
962 unambiguously derived from Y. Otherwise a function only allows exceptions
963 that have the same type ..."
964 This does not mention cv qualifiers and is different to what throw
965 [except.throw] and catch [except.catch] will do. They will ignore the
966 top level cv qualifiers, and allow qualifiers in the pointer to class
967 example.
969 We implement the letter of the standard. */
971 static bool
972 comp_except_types (tree a, tree b, bool exact)
974 if (same_type_p (a, b))
975 return true;
976 else if (!exact)
978 if (cp_type_quals (a) || cp_type_quals (b))
979 return false;
981 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
983 a = TREE_TYPE (a);
984 b = TREE_TYPE (b);
985 if (cp_type_quals (a) || cp_type_quals (b))
986 return false;
989 if (TREE_CODE (a) != RECORD_TYPE
990 || TREE_CODE (b) != RECORD_TYPE)
991 return false;
993 if (publicly_uniquely_derived_p (a, b))
994 return true;
996 return false;
999 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1000 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1001 If EXACT is ce_type, the C++17 type compatibility rules apply.
1002 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1003 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1004 are unordered, but we've already filtered out duplicates. Most lists will
1005 be in order, we should try to make use of that. */
1007 bool
1008 comp_except_specs (const_tree t1, const_tree t2, int exact)
1010 const_tree probe;
1011 const_tree base;
1012 int length = 0;
1014 if (t1 == t2)
1015 return true;
1017 /* First handle noexcept. */
1018 if (exact < ce_exact)
1020 if (exact == ce_type
1021 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1022 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1023 return true;
1025 /* noexcept(false) is compatible with no exception-specification,
1026 and less strict than any spec. */
1027 if (t1 == noexcept_false_spec)
1028 return t2 == NULL_TREE || exact == ce_derived;
1029 /* Even a derived noexcept(false) is compatible with no
1030 exception-specification. */
1031 if (t2 == noexcept_false_spec)
1032 return t1 == NULL_TREE;
1034 /* Otherwise, if we aren't looking for an exact match, noexcept is
1035 equivalent to throw(). */
1036 if (t1 == noexcept_true_spec)
1037 t1 = empty_except_spec;
1038 if (t2 == noexcept_true_spec)
1039 t2 = empty_except_spec;
1042 /* If any noexcept is left, it is only comparable to itself;
1043 either we're looking for an exact match or we're redeclaring a
1044 template with dependent noexcept. */
1045 if ((t1 && TREE_PURPOSE (t1))
1046 || (t2 && TREE_PURPOSE (t2)))
1047 return (t1 && t2
1048 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1050 if (t1 == NULL_TREE) /* T1 is ... */
1051 return t2 == NULL_TREE || exact == ce_derived;
1052 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1053 return t2 != NULL_TREE && !TREE_VALUE (t2);
1054 if (t2 == NULL_TREE) /* T2 is ... */
1055 return false;
1056 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1057 return exact == ce_derived;
1059 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1060 Count how many we find, to determine exactness. For exact matching and
1061 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1062 O(nm). */
1063 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1065 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1067 tree a = TREE_VALUE (probe);
1068 tree b = TREE_VALUE (t2);
1070 if (comp_except_types (a, b, exact))
1072 if (probe == base && exact > ce_derived)
1073 base = TREE_CHAIN (probe);
1074 length++;
1075 break;
1078 if (probe == NULL_TREE)
1079 return false;
1081 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1084 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1085 [] can match [size]. */
1087 static bool
1088 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1090 tree d1;
1091 tree d2;
1092 tree max1, max2;
1094 if (t1 == t2)
1095 return true;
1097 /* The type of the array elements must be the same. */
1098 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1099 return false;
1101 d1 = TYPE_DOMAIN (t1);
1102 d2 = TYPE_DOMAIN (t2);
1104 if (d1 == d2)
1105 return true;
1107 /* If one of the arrays is dimensionless, and the other has a
1108 dimension, they are of different types. However, it is valid to
1109 write:
1111 extern int a[];
1112 int a[3];
1114 by [basic.link]:
1116 declarations for an array object can specify
1117 array types that differ by the presence or absence of a major
1118 array bound (_dcl.array_). */
1119 if (!d1 || !d2)
1120 return allow_redeclaration;
1122 /* Check that the dimensions are the same. */
1124 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1125 return false;
1126 max1 = TYPE_MAX_VALUE (d1);
1127 max2 = TYPE_MAX_VALUE (d2);
1129 if (!cp_tree_equal (max1, max2))
1130 return false;
1132 return true;
1135 /* Compare the relative position of T1 and T2 into their respective
1136 template parameter list.
1137 T1 and T2 must be template parameter types.
1138 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1140 static bool
1141 comp_template_parms_position (tree t1, tree t2)
1143 tree index1, index2;
1144 gcc_assert (t1 && t2
1145 && TREE_CODE (t1) == TREE_CODE (t2)
1146 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1147 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1148 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1150 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1151 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1153 /* Then compare their relative position. */
1154 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1155 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1156 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1157 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1158 return false;
1160 /* In C++14 we can end up comparing 'auto' to a normal template
1161 parameter. Don't confuse them. */
1162 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1163 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1165 return true;
1168 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1170 static bool
1171 cxx_safe_arg_type_equiv_p (tree t1, tree t2)
1173 t1 = TYPE_MAIN_VARIANT (t1);
1174 t2 = TYPE_MAIN_VARIANT (t2);
1176 if (TYPE_PTR_P (t1)
1177 && TYPE_PTR_P (t2))
1178 return true;
1180 /* The signedness of the parameter matters only when an integral
1181 type smaller than int is promoted to int, otherwise only the
1182 precision of the parameter matters.
1183 This check should make sure that the callee does not see
1184 undefined values in argument registers. */
1185 if (INTEGRAL_TYPE_P (t1)
1186 && INTEGRAL_TYPE_P (t2)
1187 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
1188 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
1189 || !targetm.calls.promote_prototypes (NULL_TREE)
1190 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
1191 return true;
1193 return same_type_p (t1, t2);
1196 /* Check if a type cast between two function types can be considered safe. */
1198 static bool
1199 cxx_safe_function_type_cast_p (tree t1, tree t2)
1201 if (TREE_TYPE (t1) == void_type_node &&
1202 TYPE_ARG_TYPES (t1) == void_list_node)
1203 return true;
1205 if (TREE_TYPE (t2) == void_type_node &&
1206 TYPE_ARG_TYPES (t2) == void_list_node)
1207 return true;
1209 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1210 return false;
1212 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
1213 t1 && t2;
1214 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1215 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1216 return false;
1218 return true;
1221 /* Subroutine in comptypes. */
1223 static bool
1224 structural_comptypes (tree t1, tree t2, int strict)
1226 if (t1 == t2)
1227 return true;
1229 /* Suppress errors caused by previously reported errors. */
1230 if (t1 == error_mark_node || t2 == error_mark_node)
1231 return false;
1233 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1235 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1236 current instantiation. */
1237 if (TREE_CODE (t1) == TYPENAME_TYPE)
1238 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1240 if (TREE_CODE (t2) == TYPENAME_TYPE)
1241 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1243 if (TYPE_PTRMEMFUNC_P (t1))
1244 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1245 if (TYPE_PTRMEMFUNC_P (t2))
1246 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1248 /* Different classes of types can't be compatible. */
1249 if (TREE_CODE (t1) != TREE_CODE (t2))
1250 return false;
1252 /* Qualifiers must match. For array types, we will check when we
1253 recur on the array element types. */
1254 if (TREE_CODE (t1) != ARRAY_TYPE
1255 && cp_type_quals (t1) != cp_type_quals (t2))
1256 return false;
1257 if (TREE_CODE (t1) == FUNCTION_TYPE
1258 && type_memfn_quals (t1) != type_memfn_quals (t2))
1259 return false;
1260 /* Need to check this before TYPE_MAIN_VARIANT.
1261 FIXME function qualifiers should really change the main variant. */
1262 if (TREE_CODE (t1) == FUNCTION_TYPE
1263 || TREE_CODE (t1) == METHOD_TYPE)
1265 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1266 return false;
1267 if (flag_noexcept_type
1268 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1269 TYPE_RAISES_EXCEPTIONS (t2),
1270 ce_type))
1271 return false;
1274 /* Allow for two different type nodes which have essentially the same
1275 definition. Note that we already checked for equality of the type
1276 qualifiers (just above). */
1278 if (TREE_CODE (t1) != ARRAY_TYPE
1279 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1280 return true;
1283 /* Compare the types. Break out if they could be the same. */
1284 switch (TREE_CODE (t1))
1286 case VOID_TYPE:
1287 case BOOLEAN_TYPE:
1288 /* All void and bool types are the same. */
1289 break;
1291 case INTEGER_TYPE:
1292 case FIXED_POINT_TYPE:
1293 case REAL_TYPE:
1294 /* With these nodes, we can't determine type equivalence by
1295 looking at what is stored in the nodes themselves, because
1296 two nodes might have different TYPE_MAIN_VARIANTs but still
1297 represent the same type. For example, wchar_t and int could
1298 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1299 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1300 and are distinct types. On the other hand, int and the
1301 following typedef
1303 typedef int INT __attribute((may_alias));
1305 have identical properties, different TYPE_MAIN_VARIANTs, but
1306 represent the same type. The canonical type system keeps
1307 track of equivalence in this case, so we fall back on it. */
1308 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1310 case TEMPLATE_TEMPLATE_PARM:
1311 case BOUND_TEMPLATE_TEMPLATE_PARM:
1312 if (!comp_template_parms_position (t1, t2))
1313 return false;
1314 if (!comp_template_parms
1315 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1316 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1317 return false;
1318 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1319 break;
1320 /* Don't check inheritance. */
1321 strict = COMPARE_STRICT;
1322 /* Fall through. */
1324 case RECORD_TYPE:
1325 case UNION_TYPE:
1326 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1327 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1328 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1329 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1330 break;
1332 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1333 break;
1334 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1335 break;
1337 return false;
1339 case OFFSET_TYPE:
1340 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1341 strict & ~COMPARE_REDECLARATION))
1342 return false;
1343 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1344 return false;
1345 break;
1347 case REFERENCE_TYPE:
1348 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1349 return false;
1350 /* fall through to checks for pointer types */
1351 gcc_fallthrough ();
1353 case POINTER_TYPE:
1354 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1355 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1356 return false;
1357 break;
1359 case METHOD_TYPE:
1360 case FUNCTION_TYPE:
1361 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1362 return false;
1363 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1364 return false;
1365 break;
1367 case ARRAY_TYPE:
1368 /* Target types must match incl. qualifiers. */
1369 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1370 return false;
1371 break;
1373 case TEMPLATE_TYPE_PARM:
1374 /* If T1 and T2 don't have the same relative position in their
1375 template parameters set, they can't be equal. */
1376 if (!comp_template_parms_position (t1, t2))
1377 return false;
1378 /* Constrained 'auto's are distinct from parms that don't have the same
1379 constraints. */
1380 if (!equivalent_placeholder_constraints (t1, t2))
1381 return false;
1382 break;
1384 case TYPENAME_TYPE:
1385 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1386 TYPENAME_TYPE_FULLNAME (t2)))
1387 return false;
1388 /* Qualifiers don't matter on scopes. */
1389 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1390 TYPE_CONTEXT (t2)))
1391 return false;
1392 break;
1394 case UNBOUND_CLASS_TEMPLATE:
1395 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1396 return false;
1397 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1398 return false;
1399 break;
1401 case COMPLEX_TYPE:
1402 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1403 return false;
1404 break;
1406 case VECTOR_TYPE:
1407 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1408 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1409 return false;
1410 break;
1412 case TYPE_PACK_EXPANSION:
1413 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1414 PACK_EXPANSION_PATTERN (t2))
1415 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1416 PACK_EXPANSION_EXTRA_ARGS (t2)));
1418 case DECLTYPE_TYPE:
1419 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1420 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1421 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1422 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1423 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1424 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1425 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1426 DECLTYPE_TYPE_EXPR (t2)))
1427 return false;
1428 break;
1430 case UNDERLYING_TYPE:
1431 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1432 UNDERLYING_TYPE_TYPE (t2));
1434 default:
1435 return false;
1438 /* If we get here, we know that from a target independent POV the
1439 types are the same. Make sure the target attributes are also
1440 the same. */
1441 return comp_type_attributes (t1, t2);
1444 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1445 is a bitwise-or of the COMPARE_* flags. */
1447 bool
1448 comptypes (tree t1, tree t2, int strict)
1450 if (strict == COMPARE_STRICT)
1452 if (t1 == t2)
1453 return true;
1455 if (t1 == error_mark_node || t2 == error_mark_node)
1456 return false;
1458 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1459 /* At least one of the types requires structural equality, so
1460 perform a deep check. */
1461 return structural_comptypes (t1, t2, strict);
1463 if (flag_checking && USE_CANONICAL_TYPES)
1465 bool result = structural_comptypes (t1, t2, strict);
1467 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1468 /* The two types are structurally equivalent, but their
1469 canonical types were different. This is a failure of the
1470 canonical type propagation code.*/
1471 internal_error
1472 ("canonical types differ for identical types %qT and %qT",
1473 t1, t2);
1474 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1475 /* Two types are structurally different, but the canonical
1476 types are the same. This means we were over-eager in
1477 assigning canonical types. */
1478 internal_error
1479 ("same canonical type node for different types %qT and %qT",
1480 t1, t2);
1482 return result;
1484 if (!flag_checking && USE_CANONICAL_TYPES)
1485 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1486 else
1487 return structural_comptypes (t1, t2, strict);
1489 else if (strict == COMPARE_STRUCTURAL)
1490 return structural_comptypes (t1, t2, COMPARE_STRICT);
1491 else
1492 return structural_comptypes (t1, t2, strict);
1495 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1496 top-level qualifiers. */
1498 bool
1499 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1501 if (type1 == error_mark_node || type2 == error_mark_node)
1502 return false;
1504 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1505 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1506 return same_type_p (type1, type2);
1509 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1511 bool
1512 at_least_as_qualified_p (const_tree type1, const_tree type2)
1514 int q1 = cp_type_quals (type1);
1515 int q2 = cp_type_quals (type2);
1517 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1518 return (q1 & q2) == q2;
1521 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1522 more cv-qualified that TYPE1, and 0 otherwise. */
1525 comp_cv_qualification (int q1, int q2)
1527 if (q1 == q2)
1528 return 0;
1530 if ((q1 & q2) == q2)
1531 return 1;
1532 else if ((q1 & q2) == q1)
1533 return -1;
1535 return 0;
1539 comp_cv_qualification (const_tree type1, const_tree type2)
1541 int q1 = cp_type_quals (type1);
1542 int q2 = cp_type_quals (type2);
1543 return comp_cv_qualification (q1, q2);
1546 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1547 subset of the cv-qualification signature of TYPE2, and the types
1548 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1551 comp_cv_qual_signature (tree type1, tree type2)
1553 if (comp_ptr_ttypes_real (type2, type1, -1))
1554 return 1;
1555 else if (comp_ptr_ttypes_real (type1, type2, -1))
1556 return -1;
1557 else
1558 return 0;
1561 /* Subroutines of `comptypes'. */
1563 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1564 equivalent in the sense that functions with those parameter types
1565 can have equivalent types. The two lists must be equivalent,
1566 element by element. */
1568 bool
1569 compparms (const_tree parms1, const_tree parms2)
1571 const_tree t1, t2;
1573 /* An unspecified parmlist matches any specified parmlist
1574 whose argument types don't need default promotions. */
1576 for (t1 = parms1, t2 = parms2;
1577 t1 || t2;
1578 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1580 /* If one parmlist is shorter than the other,
1581 they fail to match. */
1582 if (!t1 || !t2)
1583 return false;
1584 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1585 return false;
1587 return true;
1591 /* Process a sizeof or alignof expression where the operand is a
1592 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1593 or GNU (preferred alignment) semantics; it is ignored if op is
1594 SIZEOF_EXPR. */
1596 tree
1597 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool std_alignof,
1598 bool complain)
1600 tree value;
1601 bool dependent_p;
1603 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1604 if (type == error_mark_node)
1605 return error_mark_node;
1607 type = non_reference (type);
1608 if (TREE_CODE (type) == METHOD_TYPE)
1610 if (complain)
1611 pedwarn (input_location, OPT_Wpointer_arith,
1612 "invalid application of %qs to a member function",
1613 OVL_OP_INFO (false, op)->name);
1614 else
1615 return error_mark_node;
1616 value = size_one_node;
1619 dependent_p = dependent_type_p (type);
1620 if (!dependent_p)
1621 complete_type (type);
1622 if (dependent_p
1623 /* VLA types will have a non-constant size. In the body of an
1624 uninstantiated template, we don't need to try to compute the
1625 value, because the sizeof expression is not an integral
1626 constant expression in that case. And, if we do try to
1627 compute the value, we'll likely end up with SAVE_EXPRs, which
1628 the template substitution machinery does not expect to see. */
1629 || (processing_template_decl
1630 && COMPLETE_TYPE_P (type)
1631 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1633 value = build_min (op, size_type_node, type);
1634 TREE_READONLY (value) = 1;
1635 if (op == ALIGNOF_EXPR && std_alignof)
1636 ALIGNOF_EXPR_STD_P (value) = true;
1637 return value;
1640 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1641 op == SIZEOF_EXPR, std_alignof,
1642 complain);
1645 /* Return the size of the type, without producing any warnings for
1646 types whose size cannot be taken. This routine should be used only
1647 in some other routine that has already produced a diagnostic about
1648 using the size of such a type. */
1649 tree
1650 cxx_sizeof_nowarn (tree type)
1652 if (TREE_CODE (type) == FUNCTION_TYPE
1653 || VOID_TYPE_P (type)
1654 || TREE_CODE (type) == ERROR_MARK)
1655 return size_one_node;
1656 else if (!COMPLETE_TYPE_P (type))
1657 return size_zero_node;
1658 else
1659 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false, false);
1662 /* Process a sizeof expression where the operand is an expression. */
1664 static tree
1665 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1667 if (e == error_mark_node)
1668 return error_mark_node;
1670 if (processing_template_decl)
1672 e = build_min (SIZEOF_EXPR, size_type_node, e);
1673 TREE_SIDE_EFFECTS (e) = 0;
1674 TREE_READONLY (e) = 1;
1676 return e;
1679 /* To get the size of a static data member declared as an array of
1680 unknown bound, we need to instantiate it. */
1681 if (VAR_P (e)
1682 && VAR_HAD_UNKNOWN_BOUND (e)
1683 && DECL_TEMPLATE_INSTANTIATION (e))
1684 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1686 if (TREE_CODE (e) == PARM_DECL
1687 && DECL_ARRAY_PARAMETER_P (e)
1688 && (complain & tf_warning))
1690 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1691 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1692 inform (DECL_SOURCE_LOCATION (e), "declared here");
1695 e = mark_type_use (e);
1697 if (bitfield_p (e))
1699 if (complain & tf_error)
1700 error ("invalid application of %<sizeof%> to a bit-field");
1701 else
1702 return error_mark_node;
1703 e = char_type_node;
1705 else if (is_overloaded_fn (e))
1707 if (complain & tf_error)
1708 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1709 "function type");
1710 else
1711 return error_mark_node;
1712 e = char_type_node;
1714 else if (type_unknown_p (e))
1716 if (complain & tf_error)
1717 cxx_incomplete_type_error (e, TREE_TYPE (e));
1718 else
1719 return error_mark_node;
1720 e = char_type_node;
1722 else
1723 e = TREE_TYPE (e);
1725 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, false, complain & tf_error);
1728 /* Implement the __alignof keyword: Return the minimum required
1729 alignment of E, measured in bytes. For VAR_DECL's and
1730 FIELD_DECL's return DECL_ALIGN (which can be set from an
1731 "aligned" __attribute__ specification). */
1733 static tree
1734 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1736 tree t;
1738 if (e == error_mark_node)
1739 return error_mark_node;
1741 if (processing_template_decl)
1743 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1744 TREE_SIDE_EFFECTS (e) = 0;
1745 TREE_READONLY (e) = 1;
1747 return e;
1750 e = mark_type_use (e);
1752 if (VAR_P (e))
1753 t = size_int (DECL_ALIGN_UNIT (e));
1754 else if (bitfield_p (e))
1756 if (complain & tf_error)
1757 error ("invalid application of %<__alignof%> to a bit-field");
1758 else
1759 return error_mark_node;
1760 t = size_one_node;
1762 else if (TREE_CODE (e) == COMPONENT_REF
1763 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1764 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1765 else if (is_overloaded_fn (e))
1767 if (complain & tf_error)
1768 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1769 "function type");
1770 else
1771 return error_mark_node;
1772 if (TREE_CODE (e) == FUNCTION_DECL)
1773 t = size_int (DECL_ALIGN_UNIT (e));
1774 else
1775 t = size_one_node;
1777 else if (type_unknown_p (e))
1779 if (complain & tf_error)
1780 cxx_incomplete_type_error (e, TREE_TYPE (e));
1781 else
1782 return error_mark_node;
1783 t = size_one_node;
1785 else
1786 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR, false,
1787 complain & tf_error);
1789 return fold_convert (size_type_node, t);
1792 /* Process a sizeof or alignof expression E with code OP where the operand
1793 is an expression. */
1795 tree
1796 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1798 if (op == SIZEOF_EXPR)
1799 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1800 else
1801 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1804 /* Build a representation of an expression 'alignas(E).' Return the
1805 folded integer value of E if it is an integral constant expression
1806 that resolves to a valid alignment. If E depends on a template
1807 parameter, return a syntactic representation tree of kind
1808 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1809 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1811 tree
1812 cxx_alignas_expr (tree e)
1814 if (e == NULL_TREE || e == error_mark_node
1815 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1816 return e;
1818 if (TYPE_P (e))
1819 /* [dcl.align]/3:
1821 When the alignment-specifier is of the form
1822 alignas(type-id ), it shall have the same effect as
1823 alignas(alignof(type-id )). */
1825 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, true, false);
1827 /* If we reach this point, it means the alignas expression if of
1828 the form "alignas(assignment-expression)", so we should follow
1829 what is stated by [dcl.align]/2. */
1831 if (value_dependent_expression_p (e))
1832 /* Leave value-dependent expression alone for now. */
1833 return e;
1835 e = instantiate_non_dependent_expr (e);
1836 e = mark_rvalue_use (e);
1838 /* [dcl.align]/2 says:
1840 the assignment-expression shall be an integral constant
1841 expression. */
1843 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1845 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1846 return error_mark_node;
1849 return cxx_constant_value (e);
1853 /* EXPR is being used in a context that is not a function call.
1854 Enforce:
1856 [expr.ref]
1858 The expression can be used only as the left-hand operand of a
1859 member function call.
1861 [expr.mptr.operator]
1863 If the result of .* or ->* is a function, then that result can be
1864 used only as the operand for the function call operator ().
1866 by issuing an error message if appropriate. Returns true iff EXPR
1867 violates these rules. */
1869 bool
1870 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1872 if (expr == NULL_TREE)
1873 return false;
1874 /* Don't enforce this in MS mode. */
1875 if (flag_ms_extensions)
1876 return false;
1877 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1878 expr = get_first_fn (expr);
1879 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1881 if (complain & tf_error)
1883 if (DECL_P (expr))
1885 error_at (loc, "invalid use of non-static member function %qD",
1886 expr);
1887 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1889 else
1890 error_at (loc, "invalid use of non-static member function of "
1891 "type %qT", TREE_TYPE (expr));
1893 return true;
1895 return false;
1898 /* If EXP is a reference to a bitfield, and the type of EXP does not
1899 match the declared type of the bitfield, return the declared type
1900 of the bitfield. Otherwise, return NULL_TREE. */
1902 tree
1903 is_bitfield_expr_with_lowered_type (const_tree exp)
1905 switch (TREE_CODE (exp))
1907 case COND_EXPR:
1908 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1909 ? TREE_OPERAND (exp, 1)
1910 : TREE_OPERAND (exp, 0)))
1911 return NULL_TREE;
1912 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1914 case COMPOUND_EXPR:
1915 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1917 case MODIFY_EXPR:
1918 case SAVE_EXPR:
1919 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1921 case COMPONENT_REF:
1923 tree field;
1925 field = TREE_OPERAND (exp, 1);
1926 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1927 return NULL_TREE;
1928 if (same_type_ignoring_top_level_qualifiers_p
1929 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1930 return NULL_TREE;
1931 return DECL_BIT_FIELD_TYPE (field);
1934 case VAR_DECL:
1935 if (DECL_HAS_VALUE_EXPR_P (exp))
1936 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1937 (CONST_CAST_TREE (exp)));
1938 return NULL_TREE;
1940 CASE_CONVERT:
1941 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1942 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1943 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1944 /* Fallthrough. */
1946 default:
1947 return NULL_TREE;
1951 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1952 bitfield with a lowered type, the type of EXP is returned, rather
1953 than NULL_TREE. */
1955 tree
1956 unlowered_expr_type (const_tree exp)
1958 tree type;
1959 tree etype = TREE_TYPE (exp);
1961 type = is_bitfield_expr_with_lowered_type (exp);
1962 if (type)
1963 type = cp_build_qualified_type (type, cp_type_quals (etype));
1964 else
1965 type = etype;
1967 return type;
1970 /* Perform the conversions in [expr] that apply when an lvalue appears
1971 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1972 function-to-pointer conversions. In addition, bitfield references are
1973 converted to their declared types. Note that this function does not perform
1974 the lvalue-to-rvalue conversion for class types. If you need that conversion
1975 for class types, then you probably need to use force_rvalue.
1977 Although the returned value is being used as an rvalue, this
1978 function does not wrap the returned expression in a
1979 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1980 that the return value is no longer an lvalue. */
1982 tree
1983 decay_conversion (tree exp,
1984 tsubst_flags_t complain,
1985 bool reject_builtin /* = true */)
1987 tree type;
1988 enum tree_code code;
1989 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1991 type = TREE_TYPE (exp);
1992 if (type == error_mark_node)
1993 return error_mark_node;
1995 exp = resolve_nondeduced_context (exp, complain);
1996 if (type_unknown_p (exp))
1998 if (complain & tf_error)
1999 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
2000 return error_mark_node;
2003 code = TREE_CODE (type);
2005 if (error_operand_p (exp))
2006 return error_mark_node;
2008 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
2010 mark_rvalue_use (exp, loc, reject_builtin);
2011 return nullptr_node;
2014 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2015 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2016 if (code == VOID_TYPE)
2018 if (complain & tf_error)
2019 error_at (loc, "void value not ignored as it ought to be");
2020 return error_mark_node;
2022 if (invalid_nonstatic_memfn_p (loc, exp, complain))
2023 return error_mark_node;
2024 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
2026 exp = mark_lvalue_use (exp);
2027 if (reject_builtin && reject_gcc_builtin (exp, loc))
2028 return error_mark_node;
2029 return cp_build_addr_expr (exp, complain);
2031 if (code == ARRAY_TYPE)
2033 tree adr;
2034 tree ptrtype;
2036 exp = mark_lvalue_use (exp);
2038 if (INDIRECT_REF_P (exp))
2039 return build_nop (build_pointer_type (TREE_TYPE (type)),
2040 TREE_OPERAND (exp, 0));
2042 if (TREE_CODE (exp) == COMPOUND_EXPR)
2044 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
2045 if (op1 == error_mark_node)
2046 return error_mark_node;
2047 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2048 TREE_OPERAND (exp, 0), op1);
2051 if (!obvalue_p (exp)
2052 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2054 if (complain & tf_error)
2055 error_at (loc, "invalid use of non-lvalue array");
2056 return error_mark_node;
2059 /* Don't let an array compound literal decay to a pointer. It can
2060 still be used to initialize an array or bind to a reference. */
2061 if (TREE_CODE (exp) == TARGET_EXPR)
2063 if (complain & tf_error)
2064 error_at (loc, "taking address of temporary array");
2065 return error_mark_node;
2068 ptrtype = build_pointer_type (TREE_TYPE (type));
2070 if (VAR_P (exp))
2072 if (!cxx_mark_addressable (exp))
2073 return error_mark_node;
2074 adr = build_nop (ptrtype, build_address (exp));
2075 return adr;
2077 /* This way is better for a COMPONENT_REF since it can
2078 simplify the offset for a component. */
2079 adr = cp_build_addr_expr (exp, complain);
2080 return cp_convert (ptrtype, adr, complain);
2083 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2084 exp = mark_rvalue_use (exp, loc, reject_builtin);
2086 /* If a bitfield is used in a context where integral promotion
2087 applies, then the caller is expected to have used
2088 default_conversion. That function promotes bitfields correctly
2089 before calling this function. At this point, if we have a
2090 bitfield referenced, we may assume that is not subject to
2091 promotion, and that, therefore, the type of the resulting rvalue
2092 is the declared type of the bitfield. */
2093 exp = convert_bitfield_to_declared_type (exp);
2095 /* We do not call rvalue() here because we do not want to wrap EXP
2096 in a NON_LVALUE_EXPR. */
2098 /* [basic.lval]
2100 Non-class rvalues always have cv-unqualified types. */
2101 type = TREE_TYPE (exp);
2102 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2103 exp = build_nop (cv_unqualified (type), exp);
2105 if (!complete_type_or_maybe_complain (type, exp, complain))
2106 return error_mark_node;
2108 return exp;
2111 /* Perform preparatory conversions, as part of the "usual arithmetic
2112 conversions". In particular, as per [expr]:
2114 Whenever an lvalue expression appears as an operand of an
2115 operator that expects the rvalue for that operand, the
2116 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2117 standard conversions are applied to convert the expression to an
2118 rvalue.
2120 In addition, we perform integral promotions here, as those are
2121 applied to both operands to a binary operator before determining
2122 what additional conversions should apply. */
2124 static tree
2125 cp_default_conversion (tree exp, tsubst_flags_t complain)
2127 /* Check for target-specific promotions. */
2128 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2129 if (promoted_type)
2130 exp = cp_convert (promoted_type, exp, complain);
2131 /* Perform the integral promotions first so that bitfield
2132 expressions (which may promote to "int", even if the bitfield is
2133 declared "unsigned") are promoted correctly. */
2134 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2135 exp = cp_perform_integral_promotions (exp, complain);
2136 /* Perform the other conversions. */
2137 exp = decay_conversion (exp, complain);
2139 return exp;
2142 /* C version. */
2144 tree
2145 default_conversion (tree exp)
2147 return cp_default_conversion (exp, tf_warning_or_error);
2150 /* EXPR is an expression with an integral or enumeration type.
2151 Perform the integral promotions in [conv.prom], and return the
2152 converted value. */
2154 tree
2155 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2157 tree type;
2158 tree promoted_type;
2160 expr = mark_rvalue_use (expr);
2161 if (error_operand_p (expr))
2162 return error_mark_node;
2164 /* [conv.prom]
2166 If the bitfield has an enumerated type, it is treated as any
2167 other value of that type for promotion purposes. */
2168 type = is_bitfield_expr_with_lowered_type (expr);
2169 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2170 type = TREE_TYPE (expr);
2171 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2172 /* Scoped enums don't promote. */
2173 if (SCOPED_ENUM_P (type))
2174 return expr;
2175 promoted_type = type_promotes_to (type);
2176 if (type != promoted_type)
2177 expr = cp_convert (promoted_type, expr, complain);
2178 return expr;
2181 /* C version. */
2183 tree
2184 perform_integral_promotions (tree expr)
2186 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2189 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2190 decay_conversion to one. */
2193 string_conv_p (const_tree totype, const_tree exp, int warn)
2195 tree t;
2197 if (!TYPE_PTR_P (totype))
2198 return 0;
2200 t = TREE_TYPE (totype);
2201 if (!same_type_p (t, char_type_node)
2202 && !same_type_p (t, char16_type_node)
2203 && !same_type_p (t, char32_type_node)
2204 && !same_type_p (t, wchar_type_node))
2205 return 0;
2207 STRIP_ANY_LOCATION_WRAPPER (exp);
2209 if (TREE_CODE (exp) == STRING_CST)
2211 /* Make sure that we don't try to convert between char and wide chars. */
2212 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2213 return 0;
2215 else
2217 /* Is this a string constant which has decayed to 'const char *'? */
2218 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2219 if (!same_type_p (TREE_TYPE (exp), t))
2220 return 0;
2221 STRIP_NOPS (exp);
2222 if (TREE_CODE (exp) != ADDR_EXPR
2223 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2224 return 0;
2226 if (warn)
2228 if (cxx_dialect >= cxx11)
2229 pedwarn (input_location, OPT_Wwrite_strings,
2230 "ISO C++ forbids converting a string constant to %qT",
2231 totype);
2232 else
2233 warning (OPT_Wwrite_strings,
2234 "deprecated conversion from string constant to %qT",
2235 totype);
2238 return 1;
2241 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2242 can, for example, use as an lvalue. This code used to be in
2243 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2244 expressions, where we're dealing with aggregates. But now it's again only
2245 called from unary_complex_lvalue. The case (in particular) that led to
2246 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2247 get it there. */
2249 static tree
2250 rationalize_conditional_expr (enum tree_code code, tree t,
2251 tsubst_flags_t complain)
2253 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2255 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2256 the first operand is always the one to be used if both operands
2257 are equal, so we know what conditional expression this used to be. */
2258 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2260 tree op0 = TREE_OPERAND (t, 0);
2261 tree op1 = TREE_OPERAND (t, 1);
2263 /* The following code is incorrect if either operand side-effects. */
2264 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2265 && !TREE_SIDE_EFFECTS (op1));
2266 return
2267 build_conditional_expr (loc,
2268 build_x_binary_op (loc,
2269 (TREE_CODE (t) == MIN_EXPR
2270 ? LE_EXPR : GE_EXPR),
2271 op0, TREE_CODE (op0),
2272 op1, TREE_CODE (op1),
2273 /*overload=*/NULL,
2274 complain),
2275 cp_build_unary_op (code, op0, false, complain),
2276 cp_build_unary_op (code, op1, false, complain),
2277 complain);
2280 return
2281 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2282 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2283 complain),
2284 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2285 complain),
2286 complain);
2289 /* Given the TYPE of an anonymous union field inside T, return the
2290 FIELD_DECL for the field. If not found return NULL_TREE. Because
2291 anonymous unions can nest, we must also search all anonymous unions
2292 that are directly reachable. */
2294 tree
2295 lookup_anon_field (tree t, tree type)
2297 tree field;
2299 t = TYPE_MAIN_VARIANT (t);
2301 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2303 if (TREE_STATIC (field))
2304 continue;
2305 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2306 continue;
2308 /* If we find it directly, return the field. */
2309 if (DECL_NAME (field) == NULL_TREE
2310 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2312 return field;
2315 /* Otherwise, it could be nested, search harder. */
2316 if (DECL_NAME (field) == NULL_TREE
2317 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2319 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2320 if (subfield)
2321 return subfield;
2324 return NULL_TREE;
2327 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2328 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2329 non-NULL, it indicates the path to the base used to name MEMBER.
2330 If PRESERVE_REFERENCE is true, the expression returned will have
2331 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2332 returned will have the type referred to by the reference.
2334 This function does not perform access control; that is either done
2335 earlier by the parser when the name of MEMBER is resolved to MEMBER
2336 itself, or later when overload resolution selects one of the
2337 functions indicated by MEMBER. */
2339 tree
2340 build_class_member_access_expr (cp_expr object, tree member,
2341 tree access_path, bool preserve_reference,
2342 tsubst_flags_t complain)
2344 tree object_type;
2345 tree member_scope;
2346 tree result = NULL_TREE;
2347 tree using_decl = NULL_TREE;
2349 if (error_operand_p (object) || error_operand_p (member))
2350 return error_mark_node;
2352 gcc_assert (DECL_P (member) || BASELINK_P (member));
2354 /* [expr.ref]
2356 The type of the first expression shall be "class object" (of a
2357 complete type). */
2358 object_type = TREE_TYPE (object);
2359 if (!currently_open_class (object_type)
2360 && !complete_type_or_maybe_complain (object_type, object, complain))
2361 return error_mark_node;
2362 if (!CLASS_TYPE_P (object_type))
2364 if (complain & tf_error)
2366 if (INDIRECT_TYPE_P (object_type)
2367 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2368 error ("request for member %qD in %qE, which is of pointer "
2369 "type %qT (maybe you meant to use %<->%> ?)",
2370 member, object.get_value (), object_type);
2371 else
2372 error ("request for member %qD in %qE, which is of non-class "
2373 "type %qT", member, object.get_value (), object_type);
2375 return error_mark_node;
2378 /* The standard does not seem to actually say that MEMBER must be a
2379 member of OBJECT_TYPE. However, that is clearly what is
2380 intended. */
2381 if (DECL_P (member))
2383 member_scope = DECL_CLASS_CONTEXT (member);
2384 if (!mark_used (member, complain) && !(complain & tf_error))
2385 return error_mark_node;
2386 if (TREE_DEPRECATED (member))
2387 warn_deprecated_use (member, NULL_TREE);
2389 else
2390 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2391 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2392 presently be the anonymous union. Go outwards until we find a
2393 type related to OBJECT_TYPE. */
2394 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2395 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2396 object_type))
2397 member_scope = TYPE_CONTEXT (member_scope);
2398 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2400 if (complain & tf_error)
2402 if (TREE_CODE (member) == FIELD_DECL)
2403 error ("invalid use of nonstatic data member %qE", member);
2404 else
2405 error ("%qD is not a member of %qT", member, object_type);
2407 return error_mark_node;
2410 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2411 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2412 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2414 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2415 if (temp)
2416 object = cp_build_fold_indirect_ref (temp);
2419 /* In [expr.ref], there is an explicit list of the valid choices for
2420 MEMBER. We check for each of those cases here. */
2421 if (VAR_P (member))
2423 /* A static data member. */
2424 result = member;
2425 mark_exp_read (object);
2426 /* If OBJECT has side-effects, they are supposed to occur. */
2427 if (TREE_SIDE_EFFECTS (object))
2428 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2430 else if (TREE_CODE (member) == FIELD_DECL)
2432 /* A non-static data member. */
2433 bool null_object_p;
2434 int type_quals;
2435 tree member_type;
2437 if (INDIRECT_REF_P (object))
2438 null_object_p =
2439 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2440 else
2441 null_object_p = false;
2443 /* Convert OBJECT to the type of MEMBER. */
2444 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2445 TYPE_MAIN_VARIANT (member_scope)))
2447 tree binfo;
2448 base_kind kind;
2450 binfo = lookup_base (access_path ? access_path : object_type,
2451 member_scope, ba_unique, &kind, complain);
2452 if (binfo == error_mark_node)
2453 return error_mark_node;
2455 /* It is invalid to try to get to a virtual base of a
2456 NULL object. The most common cause is invalid use of
2457 offsetof macro. */
2458 if (null_object_p && kind == bk_via_virtual)
2460 if (complain & tf_error)
2462 error ("invalid access to non-static data member %qD in "
2463 "virtual base of NULL object", member);
2465 return error_mark_node;
2468 /* Convert to the base. */
2469 object = build_base_path (PLUS_EXPR, object, binfo,
2470 /*nonnull=*/1, complain);
2471 /* If we found the base successfully then we should be able
2472 to convert to it successfully. */
2473 gcc_assert (object != error_mark_node);
2476 /* If MEMBER is from an anonymous aggregate, we have converted
2477 OBJECT so that it refers to the class containing the
2478 anonymous union. Generate a reference to the anonymous union
2479 itself, and recur to find MEMBER. */
2480 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2481 /* When this code is called from build_field_call, the
2482 object already has the type of the anonymous union.
2483 That is because the COMPONENT_REF was already
2484 constructed, and was then disassembled before calling
2485 build_field_call. After the function-call code is
2486 cleaned up, this waste can be eliminated. */
2487 && (!same_type_ignoring_top_level_qualifiers_p
2488 (TREE_TYPE (object), DECL_CONTEXT (member))))
2490 tree anonymous_union;
2492 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2493 DECL_CONTEXT (member));
2494 object = build_class_member_access_expr (object,
2495 anonymous_union,
2496 /*access_path=*/NULL_TREE,
2497 preserve_reference,
2498 complain);
2501 /* Compute the type of the field, as described in [expr.ref]. */
2502 type_quals = TYPE_UNQUALIFIED;
2503 member_type = TREE_TYPE (member);
2504 if (!TYPE_REF_P (member_type))
2506 type_quals = (cp_type_quals (member_type)
2507 | cp_type_quals (object_type));
2509 /* A field is const (volatile) if the enclosing object, or the
2510 field itself, is const (volatile). But, a mutable field is
2511 not const, even within a const object. */
2512 if (DECL_MUTABLE_P (member))
2513 type_quals &= ~TYPE_QUAL_CONST;
2514 member_type = cp_build_qualified_type (member_type, type_quals);
2517 result = build3_loc (input_location, COMPONENT_REF, member_type,
2518 object, member, NULL_TREE);
2520 /* Mark the expression const or volatile, as appropriate. Even
2521 though we've dealt with the type above, we still have to mark the
2522 expression itself. */
2523 if (type_quals & TYPE_QUAL_CONST)
2524 TREE_READONLY (result) = 1;
2525 if (type_quals & TYPE_QUAL_VOLATILE)
2526 TREE_THIS_VOLATILE (result) = 1;
2528 else if (BASELINK_P (member))
2530 /* The member is a (possibly overloaded) member function. */
2531 tree functions;
2532 tree type;
2534 /* If the MEMBER is exactly one static member function, then we
2535 know the type of the expression. Otherwise, we must wait
2536 until overload resolution has been performed. */
2537 functions = BASELINK_FUNCTIONS (member);
2538 if (TREE_CODE (functions) == FUNCTION_DECL
2539 && DECL_STATIC_FUNCTION_P (functions))
2540 type = TREE_TYPE (functions);
2541 else
2542 type = unknown_type_node;
2543 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2544 base. That will happen when the function is called. */
2545 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2547 else if (TREE_CODE (member) == CONST_DECL)
2549 /* The member is an enumerator. */
2550 result = member;
2551 /* If OBJECT has side-effects, they are supposed to occur. */
2552 if (TREE_SIDE_EFFECTS (object))
2553 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2554 object, result);
2556 else if ((using_decl = strip_using_decl (member)) != member)
2557 result = build_class_member_access_expr (object,
2558 using_decl,
2559 access_path, preserve_reference,
2560 complain);
2561 else
2563 if (complain & tf_error)
2564 error ("invalid use of %qD", member);
2565 return error_mark_node;
2568 if (!preserve_reference)
2569 /* [expr.ref]
2571 If E2 is declared to have type "reference to T", then ... the
2572 type of E1.E2 is T. */
2573 result = convert_from_reference (result);
2575 return result;
2578 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2579 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2581 static tree
2582 lookup_destructor (tree object, tree scope, tree dtor_name,
2583 tsubst_flags_t complain)
2585 tree object_type = TREE_TYPE (object);
2586 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2587 tree expr;
2589 /* We've already complained about this destructor. */
2590 if (dtor_type == error_mark_node)
2591 return error_mark_node;
2593 if (scope && !check_dtor_name (scope, dtor_type))
2595 if (complain & tf_error)
2596 error ("qualified type %qT does not match destructor name ~%qT",
2597 scope, dtor_type);
2598 return error_mark_node;
2600 if (is_auto (dtor_type))
2601 dtor_type = object_type;
2602 else if (identifier_p (dtor_type))
2604 /* In a template, names we can't find a match for are still accepted
2605 destructor names, and we check them here. */
2606 if (check_dtor_name (object_type, dtor_type))
2607 dtor_type = object_type;
2608 else
2610 if (complain & tf_error)
2611 error ("object type %qT does not match destructor name ~%qT",
2612 object_type, dtor_type);
2613 return error_mark_node;
2617 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2619 if (complain & tf_error)
2620 error ("the type being destroyed is %qT, but the destructor "
2621 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2622 return error_mark_node;
2624 expr = lookup_member (dtor_type, complete_dtor_identifier,
2625 /*protect=*/1, /*want_type=*/false,
2626 tf_warning_or_error);
2627 if (!expr)
2629 if (complain & tf_error)
2630 cxx_incomplete_type_error (dtor_name, dtor_type);
2631 return error_mark_node;
2633 expr = (adjust_result_of_qualified_name_lookup
2634 (expr, dtor_type, object_type));
2635 if (scope == NULL_TREE)
2636 /* We need to call adjust_result_of_qualified_name_lookup in case the
2637 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2638 that we still get virtual function binding. */
2639 BASELINK_QUALIFIED_P (expr) = false;
2640 return expr;
2643 /* An expression of the form "A::template B" has been resolved to
2644 DECL. Issue a diagnostic if B is not a template or template
2645 specialization. */
2647 void
2648 check_template_keyword (tree decl)
2650 /* The standard says:
2652 [temp.names]
2654 If a name prefixed by the keyword template is not a member
2655 template, the program is ill-formed.
2657 DR 228 removed the restriction that the template be a member
2658 template.
2660 DR 96, if accepted would add the further restriction that explicit
2661 template arguments must be provided if the template keyword is
2662 used, but, as of 2005-10-16, that DR is still in "drafting". If
2663 this DR is accepted, then the semantic checks here can be
2664 simplified, as the entity named must in fact be a template
2665 specialization, rather than, as at present, a set of overloaded
2666 functions containing at least one template function. */
2667 if (TREE_CODE (decl) != TEMPLATE_DECL
2668 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2670 if (VAR_P (decl))
2672 if (DECL_USE_TEMPLATE (decl)
2673 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2675 else
2676 permerror (input_location, "%qD is not a template", decl);
2678 else if (!is_overloaded_fn (decl))
2679 permerror (input_location, "%qD is not a template", decl);
2680 else
2682 bool found = false;
2684 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2685 !found && iter; ++iter)
2687 tree fn = *iter;
2688 if (TREE_CODE (fn) == TEMPLATE_DECL
2689 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2690 || (TREE_CODE (fn) == FUNCTION_DECL
2691 && DECL_USE_TEMPLATE (fn)
2692 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2693 found = true;
2695 if (!found)
2696 permerror (input_location, "%qD is not a template", decl);
2701 /* Record that an access failure occurred on BASETYPE_PATH attempting
2702 to access FIELD_DECL. */
2704 void
2705 access_failure_info::record_access_failure (tree basetype_path,
2706 tree field_decl)
2708 m_was_inaccessible = true;
2709 m_basetype_path = basetype_path;
2710 m_field_decl = field_decl;
2713 /* If an access failure was recorded, then attempt to locate an
2714 accessor function for the pertinent field, and if one is
2715 available, add a note and fix-it hint suggesting using it. */
2717 void
2718 access_failure_info::maybe_suggest_accessor (bool const_p) const
2720 if (!m_was_inaccessible)
2721 return;
2723 tree accessor
2724 = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2725 if (!accessor)
2726 return;
2728 /* The accessor must itself be accessible for it to be a reasonable
2729 suggestion. */
2730 if (!accessible_p (m_basetype_path, accessor, true))
2731 return;
2733 rich_location richloc (line_table, input_location);
2734 pretty_printer pp;
2735 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2736 richloc.add_fixit_replace (pp_formatted_text (&pp));
2737 inform (&richloc, "field %q#D can be accessed via %q#D",
2738 m_field_decl, accessor);
2741 /* This function is called by the parser to process a class member
2742 access expression of the form OBJECT.NAME. NAME is a node used by
2743 the parser to represent a name; it is not yet a DECL. It may,
2744 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2745 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2746 there is no reason to do the lookup twice, so the parser keeps the
2747 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2748 be a template via the use of the "A::template B" syntax. */
2750 tree
2751 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2752 tsubst_flags_t complain)
2754 tree expr;
2755 tree object_type;
2756 tree member;
2757 tree access_path = NULL_TREE;
2758 tree orig_object = object;
2759 tree orig_name = name;
2761 if (object == error_mark_node || name == error_mark_node)
2762 return error_mark_node;
2764 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2765 if (!objc_is_public (object, name))
2766 return error_mark_node;
2768 object_type = TREE_TYPE (object);
2770 if (processing_template_decl)
2772 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2773 type_dependent_object_expression_p (object)
2774 /* If NAME is "f<args>", where either 'f' or 'args' is
2775 dependent, then the expression is dependent. */
2776 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2777 && dependent_template_id_p (TREE_OPERAND (name, 0),
2778 TREE_OPERAND (name, 1)))
2779 /* If NAME is "T::X" where "T" is dependent, then the
2780 expression is dependent. */
2781 || (TREE_CODE (name) == SCOPE_REF
2782 && TYPE_P (TREE_OPERAND (name, 0))
2783 && dependent_scope_p (TREE_OPERAND (name, 0))))
2785 dependent:
2786 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2787 orig_object, orig_name, NULL_TREE);
2789 object = build_non_dependent_expr (object);
2791 else if (c_dialect_objc ()
2792 && identifier_p (name)
2793 && (expr = objc_maybe_build_component_ref (object, name)))
2794 return expr;
2796 /* [expr.ref]
2798 The type of the first expression shall be "class object" (of a
2799 complete type). */
2800 if (!currently_open_class (object_type)
2801 && !complete_type_or_maybe_complain (object_type, object, complain))
2802 return error_mark_node;
2803 if (!CLASS_TYPE_P (object_type))
2805 if (complain & tf_error)
2807 if (INDIRECT_TYPE_P (object_type)
2808 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2809 error ("request for member %qD in %qE, which is of pointer "
2810 "type %qT (maybe you meant to use %<->%> ?)",
2811 name, object.get_value (), object_type);
2812 else
2813 error ("request for member %qD in %qE, which is of non-class "
2814 "type %qT", name, object.get_value (), object_type);
2816 return error_mark_node;
2819 if (BASELINK_P (name))
2820 /* A member function that has already been looked up. */
2821 member = name;
2822 else
2824 bool is_template_id = false;
2825 tree template_args = NULL_TREE;
2826 tree scope = NULL_TREE;
2828 access_path = object_type;
2830 if (TREE_CODE (name) == SCOPE_REF)
2832 /* A qualified name. The qualifying class or namespace `S'
2833 has already been looked up; it is either a TYPE or a
2834 NAMESPACE_DECL. */
2835 scope = TREE_OPERAND (name, 0);
2836 name = TREE_OPERAND (name, 1);
2838 /* If SCOPE is a namespace, then the qualified name does not
2839 name a member of OBJECT_TYPE. */
2840 if (TREE_CODE (scope) == NAMESPACE_DECL)
2842 if (complain & tf_error)
2843 error ("%<%D::%D%> is not a member of %qT",
2844 scope, name, object_type);
2845 return error_mark_node;
2849 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2851 is_template_id = true;
2852 template_args = TREE_OPERAND (name, 1);
2853 name = TREE_OPERAND (name, 0);
2855 if (!identifier_p (name))
2856 name = OVL_NAME (name);
2859 if (scope)
2861 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2863 gcc_assert (!is_template_id);
2864 /* Looking up a member enumerator (c++/56793). */
2865 if (!TYPE_CLASS_SCOPE_P (scope)
2866 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2868 if (complain & tf_error)
2869 error ("%<%D::%D%> is not a member of %qT",
2870 scope, name, object_type);
2871 return error_mark_node;
2873 tree val = lookup_enumerator (scope, name);
2874 if (!val)
2876 if (complain & tf_error)
2877 error ("%qD is not a member of %qD",
2878 name, scope);
2879 return error_mark_node;
2882 if (TREE_SIDE_EFFECTS (object))
2883 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2884 return val;
2887 gcc_assert (CLASS_TYPE_P (scope));
2888 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2890 if (constructor_name_p (name, scope))
2892 if (complain & tf_error)
2893 error ("cannot call constructor %<%T::%D%> directly",
2894 scope, name);
2895 return error_mark_node;
2898 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2899 access_path = lookup_base (object_type, scope, ba_check,
2900 NULL, complain);
2901 if (access_path == error_mark_node)
2902 return error_mark_node;
2903 if (!access_path)
2905 if (any_dependent_bases_p (object_type))
2906 goto dependent;
2907 if (complain & tf_error)
2908 error ("%qT is not a base of %qT", scope, object_type);
2909 return error_mark_node;
2913 if (TREE_CODE (name) == BIT_NOT_EXPR)
2915 if (dependent_type_p (object_type))
2916 /* The destructor isn't declared yet. */
2917 goto dependent;
2918 member = lookup_destructor (object, scope, name, complain);
2920 else
2922 /* Look up the member. */
2923 access_failure_info afi;
2924 member = lookup_member (access_path, name, /*protect=*/1,
2925 /*want_type=*/false, complain,
2926 &afi);
2927 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2928 if (member == NULL_TREE)
2930 if (dependent_type_p (object_type))
2931 /* Try again at instantiation time. */
2932 goto dependent;
2933 if (complain & tf_error)
2935 tree guessed_id = lookup_member_fuzzy (access_path, name,
2936 /*want_type=*/false);
2937 if (guessed_id)
2939 location_t bogus_component_loc = input_location;
2940 gcc_rich_location rich_loc (bogus_component_loc);
2941 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2942 guessed_id);
2943 error_at (&rich_loc,
2944 "%q#T has no member named %qE;"
2945 " did you mean %qE?",
2946 TREE_CODE (access_path) == TREE_BINFO
2947 ? TREE_TYPE (access_path) : object_type,
2948 name, guessed_id);
2950 else
2951 error ("%q#T has no member named %qE",
2952 TREE_CODE (access_path) == TREE_BINFO
2953 ? TREE_TYPE (access_path) : object_type, name);
2955 return error_mark_node;
2957 if (member == error_mark_node)
2958 return error_mark_node;
2959 if (DECL_P (member)
2960 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2961 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2962 wrong, so don't use it. */
2963 goto dependent;
2964 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2965 goto dependent;
2968 if (is_template_id)
2970 tree templ = member;
2972 if (BASELINK_P (templ))
2973 member = lookup_template_function (templ, template_args);
2974 else if (variable_template_p (templ))
2975 member = (lookup_and_finish_template_variable
2976 (templ, template_args, complain));
2977 else
2979 if (complain & tf_error)
2980 error ("%qD is not a member template function", name);
2981 return error_mark_node;
2986 if (TREE_DEPRECATED (member))
2987 warn_deprecated_use (member, NULL_TREE);
2989 if (template_p)
2990 check_template_keyword (member);
2992 expr = build_class_member_access_expr (object, member, access_path,
2993 /*preserve_reference=*/false,
2994 complain);
2995 if (processing_template_decl && expr != error_mark_node)
2997 if (BASELINK_P (member))
2999 if (TREE_CODE (orig_name) == SCOPE_REF)
3000 BASELINK_QUALIFIED_P (member) = 1;
3001 orig_name = member;
3003 return build_min_non_dep (COMPONENT_REF, expr,
3004 orig_object, orig_name,
3005 NULL_TREE);
3008 return expr;
3011 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3012 type. */
3014 tree
3015 build_simple_component_ref (tree object, tree member)
3017 tree type = cp_build_qualified_type (TREE_TYPE (member),
3018 cp_type_quals (TREE_TYPE (object)));
3019 return build3_loc (input_location,
3020 COMPONENT_REF, type,
3021 object, member, NULL_TREE);
3024 /* Return an expression for the MEMBER_NAME field in the internal
3025 representation of PTRMEM, a pointer-to-member function. (Each
3026 pointer-to-member function type gets its own RECORD_TYPE so it is
3027 more convenient to access the fields by name than by FIELD_DECL.)
3028 This routine converts the NAME to a FIELD_DECL and then creates the
3029 node for the complete expression. */
3031 tree
3032 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
3034 tree ptrmem_type;
3035 tree member;
3037 if (TREE_CODE (ptrmem) == CONSTRUCTOR)
3039 unsigned int ix;
3040 tree index, value;
3041 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem),
3042 ix, index, value)
3043 if (index && DECL_P (index) && DECL_NAME (index) == member_name)
3044 return value;
3045 gcc_unreachable ();
3048 /* This code is a stripped down version of
3049 build_class_member_access_expr. It does not work to use that
3050 routine directly because it expects the object to be of class
3051 type. */
3052 ptrmem_type = TREE_TYPE (ptrmem);
3053 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
3054 for (member = TYPE_FIELDS (ptrmem_type); member;
3055 member = DECL_CHAIN (member))
3056 if (DECL_NAME (member) == member_name)
3057 break;
3058 tree res = build_simple_component_ref (ptrmem, member);
3060 TREE_NO_WARNING (res) = 1;
3061 return res;
3064 /* Given an expression PTR for a pointer, return an expression
3065 for the value pointed to.
3066 ERRORSTRING is the name of the operator to appear in error messages.
3068 This function may need to overload OPERATOR_FNNAME.
3069 Must also handle REFERENCE_TYPEs for C++. */
3071 tree
3072 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3073 tsubst_flags_t complain)
3075 tree orig_expr = expr;
3076 tree rval;
3077 tree overload = NULL_TREE;
3079 if (processing_template_decl)
3081 /* Retain the type if we know the operand is a pointer. */
3082 if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
3083 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3084 if (type_dependent_expression_p (expr))
3085 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3086 expr = build_non_dependent_expr (expr);
3089 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3090 NULL_TREE, NULL_TREE, &overload, complain);
3091 if (!rval)
3092 rval = cp_build_indirect_ref (expr, errorstring, complain);
3094 if (processing_template_decl && rval != error_mark_node)
3096 if (overload != NULL_TREE)
3097 return (build_min_non_dep_op_overload
3098 (INDIRECT_REF, rval, overload, orig_expr));
3100 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3102 else
3103 return rval;
3106 /* The implementation of the above, and of indirection implied by other
3107 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3109 static tree
3110 cp_build_indirect_ref_1 (tree ptr, ref_operator errorstring,
3111 tsubst_flags_t complain, bool do_fold)
3113 tree pointer, type;
3115 /* RO_NULL should only be used with the folding entry points below, not
3116 cp_build_indirect_ref. */
3117 gcc_checking_assert (errorstring != RO_NULL || do_fold);
3119 if (ptr == current_class_ptr
3120 || (TREE_CODE (ptr) == NOP_EXPR
3121 && TREE_OPERAND (ptr, 0) == current_class_ptr
3122 && (same_type_ignoring_top_level_qualifiers_p
3123 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3124 return current_class_ref;
3126 pointer = (TYPE_REF_P (TREE_TYPE (ptr))
3127 ? ptr : decay_conversion (ptr, complain));
3128 if (pointer == error_mark_node)
3129 return error_mark_node;
3131 type = TREE_TYPE (pointer);
3133 if (INDIRECT_TYPE_P (type))
3135 /* [expr.unary.op]
3137 If the type of the expression is "pointer to T," the type
3138 of the result is "T." */
3139 tree t = TREE_TYPE (type);
3141 if ((CONVERT_EXPR_P (ptr)
3142 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3143 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3145 /* If a warning is issued, mark it to avoid duplicates from
3146 the backend. This only needs to be done at
3147 warn_strict_aliasing > 2. */
3148 if (warn_strict_aliasing > 2)
3149 if (strict_aliasing_warning (EXPR_LOCATION (ptr),
3150 type, TREE_OPERAND (ptr, 0)))
3151 TREE_NO_WARNING (ptr) = 1;
3154 if (VOID_TYPE_P (t))
3156 /* A pointer to incomplete type (other than cv void) can be
3157 dereferenced [expr.unary.op]/1 */
3158 if (complain & tf_error)
3159 error ("%qT is not a pointer-to-object type", type);
3160 return error_mark_node;
3162 else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
3163 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3164 /* The POINTER was something like `&x'. We simplify `*&x' to
3165 `x'. */
3166 return TREE_OPERAND (pointer, 0);
3167 else
3169 tree ref = build1 (INDIRECT_REF, t, pointer);
3171 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3172 so that we get the proper error message if the result is used
3173 to assign to. Also, &* is supposed to be a no-op. */
3174 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3175 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3176 TREE_SIDE_EFFECTS (ref)
3177 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3178 return ref;
3181 else if (!(complain & tf_error))
3182 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3184 /* `pointer' won't be an error_mark_node if we were given a
3185 pointer to member, so it's cool to check for this here. */
3186 else if (TYPE_PTRMEM_P (type))
3187 switch (errorstring)
3189 case RO_ARRAY_INDEXING:
3190 error ("invalid use of array indexing on pointer to member");
3191 break;
3192 case RO_UNARY_STAR:
3193 error ("invalid use of unary %<*%> on pointer to member");
3194 break;
3195 case RO_IMPLICIT_CONVERSION:
3196 error ("invalid use of implicit conversion on pointer to member");
3197 break;
3198 case RO_ARROW_STAR:
3199 error ("left hand operand of %<->*%> must be a pointer to class, "
3200 "but is a pointer to member of type %qT", type);
3201 break;
3202 default:
3203 gcc_unreachable ();
3205 else if (pointer != error_mark_node)
3206 invalid_indirection_error (input_location, type, errorstring);
3208 return error_mark_node;
3211 /* Entry point used by c-common, which expects folding. */
3213 tree
3214 build_indirect_ref (location_t /*loc*/,
3215 tree ptr, ref_operator errorstring)
3217 return cp_build_indirect_ref_1 (ptr, errorstring, tf_warning_or_error, true);
3220 /* Entry point used by internal indirection needs that don't correspond to any
3221 syntactic construct. */
3223 tree
3224 cp_build_fold_indirect_ref (tree pointer)
3226 return cp_build_indirect_ref_1 (pointer, RO_NULL, tf_warning_or_error, true);
3229 /* Entry point used by indirection needs that correspond to some syntactic
3230 construct. */
3232 tree
3233 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3234 tsubst_flags_t complain)
3236 return cp_build_indirect_ref_1 (ptr, errorstring, complain, false);
3239 /* This handles expressions of the form "a[i]", which denotes
3240 an array reference.
3242 This is logically equivalent in C to *(a+i), but we may do it differently.
3243 If A is a variable or a member, we generate a primitive ARRAY_REF.
3244 This avoids forcing the array out of registers, and can work on
3245 arrays that are not lvalues (for example, members of structures returned
3246 by functions).
3248 If INDEX is of some user-defined type, it must be converted to
3249 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3250 will inherit the type of the array, which will be some pointer type.
3252 LOC is the location to use in building the array reference. */
3254 tree
3255 cp_build_array_ref (location_t loc, tree array, tree idx,
3256 tsubst_flags_t complain)
3258 tree ret;
3260 if (idx == 0)
3262 if (complain & tf_error)
3263 error_at (loc, "subscript missing in array reference");
3264 return error_mark_node;
3267 if (TREE_TYPE (array) == error_mark_node
3268 || TREE_TYPE (idx) == error_mark_node)
3269 return error_mark_node;
3271 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3272 inside it. */
3273 switch (TREE_CODE (array))
3275 case COMPOUND_EXPR:
3277 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3278 complain);
3279 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3280 TREE_OPERAND (array, 0), value);
3281 SET_EXPR_LOCATION (ret, loc);
3282 return ret;
3285 case COND_EXPR:
3286 ret = build_conditional_expr
3287 (loc, TREE_OPERAND (array, 0),
3288 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3289 complain),
3290 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3291 complain),
3292 complain);
3293 protected_set_expr_location (ret, loc);
3294 return ret;
3296 default:
3297 break;
3300 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3302 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3304 tree rval, type;
3306 warn_array_subscript_with_type_char (loc, idx);
3308 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3310 if (complain & tf_error)
3311 error_at (loc, "array subscript is not an integer");
3312 return error_mark_node;
3315 /* Apply integral promotions *after* noticing character types.
3316 (It is unclear why we do these promotions -- the standard
3317 does not say that we should. In fact, the natural thing would
3318 seem to be to convert IDX to ptrdiff_t; we're performing
3319 pointer arithmetic.) */
3320 idx = cp_perform_integral_promotions (idx, complain);
3322 /* An array that is indexed by a non-constant
3323 cannot be stored in a register; we must be able to do
3324 address arithmetic on its address.
3325 Likewise an array of elements of variable size. */
3326 if (TREE_CODE (idx) != INTEGER_CST
3327 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3328 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3329 != INTEGER_CST)))
3331 if (!cxx_mark_addressable (array, true))
3332 return error_mark_node;
3335 /* An array that is indexed by a constant value which is not within
3336 the array bounds cannot be stored in a register either; because we
3337 would get a crash in store_bit_field/extract_bit_field when trying
3338 to access a non-existent part of the register. */
3339 if (TREE_CODE (idx) == INTEGER_CST
3340 && TYPE_DOMAIN (TREE_TYPE (array))
3341 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3343 if (!cxx_mark_addressable (array))
3344 return error_mark_node;
3347 /* Note in C++ it is valid to subscript a `register' array, since
3348 it is valid to take the address of something with that
3349 storage specification. */
3350 if (extra_warnings)
3352 tree foo = array;
3353 while (TREE_CODE (foo) == COMPONENT_REF)
3354 foo = TREE_OPERAND (foo, 0);
3355 if (VAR_P (foo) && DECL_REGISTER (foo)
3356 && (complain & tf_warning))
3357 warning_at (loc, OPT_Wextra,
3358 "subscripting array declared %<register%>");
3361 type = TREE_TYPE (TREE_TYPE (array));
3362 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3363 /* Array ref is const/volatile if the array elements are
3364 or if the array is.. */
3365 TREE_READONLY (rval)
3366 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3367 TREE_SIDE_EFFECTS (rval)
3368 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3369 TREE_THIS_VOLATILE (rval)
3370 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3371 ret = require_complete_type_sfinae (rval, complain);
3372 protected_set_expr_location (ret, loc);
3373 if (non_lvalue)
3374 ret = non_lvalue_loc (loc, ret);
3375 return ret;
3379 tree ar = cp_default_conversion (array, complain);
3380 tree ind = cp_default_conversion (idx, complain);
3382 /* Put the integer in IND to simplify error checking. */
3383 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3384 std::swap (ar, ind);
3386 if (ar == error_mark_node || ind == error_mark_node)
3387 return error_mark_node;
3389 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3391 if (complain & tf_error)
3392 error_at (loc, "subscripted value is neither array nor pointer");
3393 return error_mark_node;
3395 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3397 if (complain & tf_error)
3398 error_at (loc, "array subscript is not an integer");
3399 return error_mark_node;
3402 warn_array_subscript_with_type_char (loc, idx);
3404 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3405 PLUS_EXPR, ar, ind,
3406 complain),
3407 RO_ARRAY_INDEXING,
3408 complain);
3409 protected_set_expr_location (ret, loc);
3410 if (non_lvalue)
3411 ret = non_lvalue_loc (loc, ret);
3412 return ret;
3416 /* Entry point for Obj-C++. */
3418 tree
3419 build_array_ref (location_t loc, tree array, tree idx)
3421 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3424 /* Resolve a pointer to member function. INSTANCE is the object
3425 instance to use, if the member points to a virtual member.
3427 This used to avoid checking for virtual functions if basetype
3428 has no virtual functions, according to an earlier ANSI draft.
3429 With the final ISO C++ rules, such an optimization is
3430 incorrect: A pointer to a derived member can be static_cast
3431 to pointer-to-base-member, as long as the dynamic object
3432 later has the right member. So now we only do this optimization
3433 when we know the dynamic type of the object. */
3435 tree
3436 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3437 tsubst_flags_t complain)
3439 if (TREE_CODE (function) == OFFSET_REF)
3440 function = TREE_OPERAND (function, 1);
3442 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3444 tree idx, delta, e1, e2, e3, vtbl;
3445 bool nonvirtual;
3446 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3447 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3449 tree instance_ptr = *instance_ptrptr;
3450 tree instance_save_expr = 0;
3451 if (instance_ptr == error_mark_node)
3453 if (TREE_CODE (function) == PTRMEM_CST)
3455 /* Extracting the function address from a pmf is only
3456 allowed with -Wno-pmf-conversions. It only works for
3457 pmf constants. */
3458 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3459 e1 = convert (fntype, e1);
3460 return e1;
3462 else
3464 if (complain & tf_error)
3465 error ("object missing in use of %qE", function);
3466 return error_mark_node;
3470 /* True if we know that the dynamic type of the object doesn't have
3471 virtual functions, so we can assume the PFN field is a pointer. */
3472 nonvirtual = (COMPLETE_TYPE_P (basetype)
3473 && !TYPE_POLYMORPHIC_P (basetype)
3474 && resolves_to_fixed_type_p (instance_ptr, 0));
3476 /* If we don't really have an object (i.e. in an ill-formed
3477 conversion from PMF to pointer), we can't resolve virtual
3478 functions anyway. */
3479 if (!nonvirtual && is_dummy_object (instance_ptr))
3480 nonvirtual = true;
3482 if (TREE_SIDE_EFFECTS (instance_ptr))
3483 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3485 if (TREE_SIDE_EFFECTS (function))
3486 function = save_expr (function);
3488 /* Start by extracting all the information from the PMF itself. */
3489 e3 = pfn_from_ptrmemfunc (function);
3490 delta = delta_from_ptrmemfunc (function);
3491 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3492 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3494 int flag_sanitize_save;
3495 case ptrmemfunc_vbit_in_pfn:
3496 e1 = cp_build_binary_op (input_location,
3497 BIT_AND_EXPR, idx, integer_one_node,
3498 complain);
3499 idx = cp_build_binary_op (input_location,
3500 MINUS_EXPR, idx, integer_one_node,
3501 complain);
3502 if (idx == error_mark_node)
3503 return error_mark_node;
3504 break;
3506 case ptrmemfunc_vbit_in_delta:
3507 e1 = cp_build_binary_op (input_location,
3508 BIT_AND_EXPR, delta, integer_one_node,
3509 complain);
3510 /* Don't instrument the RSHIFT_EXPR we're about to create because
3511 we're going to use DELTA number of times, and that wouldn't play
3512 well with SAVE_EXPRs therein. */
3513 flag_sanitize_save = flag_sanitize;
3514 flag_sanitize = 0;
3515 delta = cp_build_binary_op (input_location,
3516 RSHIFT_EXPR, delta, integer_one_node,
3517 complain);
3518 flag_sanitize = flag_sanitize_save;
3519 if (delta == error_mark_node)
3520 return error_mark_node;
3521 break;
3523 default:
3524 gcc_unreachable ();
3527 if (e1 == error_mark_node)
3528 return error_mark_node;
3530 /* Convert down to the right base before using the instance. A
3531 special case is that in a pointer to member of class C, C may
3532 be incomplete. In that case, the function will of course be
3533 a member of C, and no conversion is required. In fact,
3534 lookup_base will fail in that case, because incomplete
3535 classes do not have BINFOs. */
3536 if (!same_type_ignoring_top_level_qualifiers_p
3537 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3539 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3540 basetype, ba_check, NULL, complain);
3541 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3542 1, complain);
3543 if (instance_ptr == error_mark_node)
3544 return error_mark_node;
3546 /* ...and then the delta in the PMF. */
3547 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3549 /* Hand back the adjusted 'this' argument to our caller. */
3550 *instance_ptrptr = instance_ptr;
3552 if (nonvirtual)
3553 /* Now just return the pointer. */
3554 return e3;
3556 /* Next extract the vtable pointer from the object. */
3557 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3558 instance_ptr);
3559 vtbl = cp_build_fold_indirect_ref (vtbl);
3560 if (vtbl == error_mark_node)
3561 return error_mark_node;
3563 /* Finally, extract the function pointer from the vtable. */
3564 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3565 e2 = cp_build_fold_indirect_ref (e2);
3566 if (e2 == error_mark_node)
3567 return error_mark_node;
3568 TREE_CONSTANT (e2) = 1;
3570 /* When using function descriptors, the address of the
3571 vtable entry is treated as a function pointer. */
3572 if (TARGET_VTABLE_USES_DESCRIPTORS)
3573 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3574 cp_build_addr_expr (e2, complain));
3576 e2 = fold_convert (TREE_TYPE (e3), e2);
3577 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3578 if (e1 == error_mark_node)
3579 return error_mark_node;
3581 /* Make sure this doesn't get evaluated first inside one of the
3582 branches of the COND_EXPR. */
3583 if (instance_save_expr)
3584 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3585 instance_save_expr, e1);
3587 function = e1;
3589 return function;
3592 /* Used by the C-common bits. */
3593 tree
3594 build_function_call (location_t /*loc*/,
3595 tree function, tree params)
3597 return cp_build_function_call (function, params, tf_warning_or_error);
3600 /* Used by the C-common bits. */
3601 tree
3602 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3603 tree function, vec<tree, va_gc> *params,
3604 vec<tree, va_gc> * /*origtypes*/)
3606 vec<tree, va_gc> *orig_params = params;
3607 tree ret = cp_build_function_call_vec (function, &params,
3608 tf_warning_or_error);
3610 /* cp_build_function_call_vec can reallocate PARAMS by adding
3611 default arguments. That should never happen here. Verify
3612 that. */
3613 gcc_assert (params == orig_params);
3615 return ret;
3618 /* Build a function call using a tree list of arguments. */
3620 static tree
3621 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3623 vec<tree, va_gc> *vec;
3624 tree ret;
3626 vec = make_tree_vector ();
3627 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3628 vec_safe_push (vec, TREE_VALUE (params));
3629 ret = cp_build_function_call_vec (function, &vec, complain);
3630 release_tree_vector (vec);
3631 return ret;
3634 /* Build a function call using varargs. */
3636 tree
3637 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3639 vec<tree, va_gc> *vec;
3640 va_list args;
3641 tree ret, t;
3643 vec = make_tree_vector ();
3644 va_start (args, complain);
3645 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3646 vec_safe_push (vec, t);
3647 va_end (args);
3648 ret = cp_build_function_call_vec (function, &vec, complain);
3649 release_tree_vector (vec);
3650 return ret;
3653 /* Build a function call using a vector of arguments. PARAMS may be
3654 NULL if there are no parameters. This changes the contents of
3655 PARAMS. */
3657 tree
3658 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3659 tsubst_flags_t complain)
3661 tree fntype, fndecl;
3662 int is_method;
3663 tree original = function;
3664 int nargs;
3665 tree *argarray;
3666 tree parm_types;
3667 vec<tree, va_gc> *allocated = NULL;
3668 tree ret;
3670 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3671 expressions, like those used for ObjC messenger dispatches. */
3672 if (params != NULL && !vec_safe_is_empty (*params))
3673 function = objc_rewrite_function_call (function, (**params)[0]);
3675 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3676 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3677 if (TREE_CODE (function) == NOP_EXPR
3678 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3679 function = TREE_OPERAND (function, 0);
3681 if (TREE_CODE (function) == FUNCTION_DECL)
3683 /* If the function is a non-template member function
3684 or a non-template friend, then we need to check the
3685 constraints.
3687 Note that if overload resolution failed with a single
3688 candidate this function will be used to explicitly diagnose
3689 the failure for the single call expression. The check is
3690 technically redundant since we also would have failed in
3691 add_function_candidate. */
3692 if (flag_concepts
3693 && (complain & tf_error)
3694 && !constraints_satisfied_p (function))
3696 error ("cannot call function %qD", function);
3697 location_t loc = DECL_SOURCE_LOCATION (function);
3698 diagnose_constraints (loc, function, NULL_TREE);
3699 return error_mark_node;
3702 if (!mark_used (function, complain) && !(complain & tf_error))
3703 return error_mark_node;
3704 fndecl = function;
3706 /* Convert anything with function type to a pointer-to-function. */
3707 if (DECL_MAIN_P (function))
3709 if (complain & tf_error)
3710 pedwarn (input_location, OPT_Wpedantic,
3711 "ISO C++ forbids calling %<::main%> from within program");
3712 else
3713 return error_mark_node;
3715 function = build_addr_func (function, complain);
3717 else
3719 fndecl = NULL_TREE;
3721 function = build_addr_func (function, complain);
3724 if (function == error_mark_node)
3725 return error_mark_node;
3727 fntype = TREE_TYPE (function);
3729 if (TYPE_PTRMEMFUNC_P (fntype))
3731 if (complain & tf_error)
3732 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3733 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3734 original, original);
3735 return error_mark_node;
3738 is_method = (TYPE_PTR_P (fntype)
3739 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3741 if (!(TYPE_PTRFN_P (fntype)
3742 || is_method
3743 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3745 if (complain & tf_error)
3747 if (!flag_diagnostics_show_caret)
3748 error_at (input_location,
3749 "%qE cannot be used as a function", original);
3750 else if (DECL_P (original))
3751 error_at (input_location,
3752 "%qD cannot be used as a function", original);
3753 else
3754 error_at (input_location,
3755 "expression cannot be used as a function");
3758 return error_mark_node;
3761 /* fntype now gets the type of function pointed to. */
3762 fntype = TREE_TYPE (fntype);
3763 parm_types = TYPE_ARG_TYPES (fntype);
3765 if (params == NULL)
3767 allocated = make_tree_vector ();
3768 params = &allocated;
3771 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3772 complain);
3773 if (nargs < 0)
3774 return error_mark_node;
3776 argarray = (*params)->address ();
3778 /* Check for errors in format strings and inappropriately
3779 null parameters. */
3780 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3781 nargs, argarray, NULL);
3783 ret = build_cxx_call (function, nargs, argarray, complain);
3785 if (warned_p)
3787 tree c = extract_call_expr (ret);
3788 if (TREE_CODE (c) == CALL_EXPR)
3789 TREE_NO_WARNING (c) = 1;
3792 if (allocated != NULL)
3793 release_tree_vector (allocated);
3795 return ret;
3798 /* Subroutine of convert_arguments.
3799 Print an error message about a wrong number of arguments. */
3801 static void
3802 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3804 if (fndecl)
3806 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3808 if (DECL_NAME (fndecl) == NULL_TREE
3809 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3810 error_at (loc,
3811 too_many_p
3812 ? G_("too many arguments to constructor %q#D")
3813 : G_("too few arguments to constructor %q#D"),
3814 fndecl);
3815 else
3816 error_at (loc,
3817 too_many_p
3818 ? G_("too many arguments to member function %q#D")
3819 : G_("too few arguments to member function %q#D"),
3820 fndecl);
3822 else
3823 error_at (loc,
3824 too_many_p
3825 ? G_("too many arguments to function %q#D")
3826 : G_("too few arguments to function %q#D"),
3827 fndecl);
3828 if (!DECL_IS_BUILTIN (fndecl))
3829 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3831 else
3833 if (c_dialect_objc () && objc_message_selector ())
3834 error_at (loc,
3835 too_many_p
3836 ? G_("too many arguments to method %q#D")
3837 : G_("too few arguments to method %q#D"),
3838 objc_message_selector ());
3839 else
3840 error_at (loc, too_many_p ? G_("too many arguments to function")
3841 : G_("too few arguments to function"));
3845 /* Convert the actual parameter expressions in the list VALUES to the
3846 types in the list TYPELIST. The converted expressions are stored
3847 back in the VALUES vector.
3848 If parmdecls is exhausted, or when an element has NULL as its type,
3849 perform the default conversions.
3851 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3853 This is also where warnings about wrong number of args are generated.
3855 Returns the actual number of arguments processed (which might be less
3856 than the length of the vector), or -1 on error.
3858 In C++, unspecified trailing parameters can be filled in with their
3859 default arguments, if such were specified. Do so here. */
3861 static int
3862 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3863 int flags, tsubst_flags_t complain)
3865 tree typetail;
3866 unsigned int i;
3868 /* Argument passing is always copy-initialization. */
3869 flags |= LOOKUP_ONLYCONVERTING;
3871 for (i = 0, typetail = typelist;
3872 i < vec_safe_length (*values);
3873 i++)
3875 tree type = typetail ? TREE_VALUE (typetail) : 0;
3876 tree val = (**values)[i];
3878 if (val == error_mark_node || type == error_mark_node)
3879 return -1;
3881 if (type == void_type_node)
3883 if (complain & tf_error)
3885 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3886 return i;
3888 else
3889 return -1;
3892 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3893 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3894 if (TREE_CODE (val) == NOP_EXPR
3895 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3896 && (type == 0 || !TYPE_REF_P (type)))
3897 val = TREE_OPERAND (val, 0);
3899 if (type == 0 || !TYPE_REF_P (type))
3901 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3902 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3903 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3904 val = decay_conversion (val, complain);
3907 if (val == error_mark_node)
3908 return -1;
3910 if (type != 0)
3912 /* Formal parm type is specified by a function prototype. */
3913 tree parmval;
3915 if (!COMPLETE_TYPE_P (complete_type (type)))
3917 if (complain & tf_error)
3919 if (fndecl)
3920 error ("parameter %P of %qD has incomplete type %qT",
3921 i, fndecl, type);
3922 else
3923 error ("parameter %P has incomplete type %qT", i, type);
3925 parmval = error_mark_node;
3927 else
3929 parmval = convert_for_initialization
3930 (NULL_TREE, type, val, flags,
3931 ICR_ARGPASS, fndecl, i, complain);
3932 parmval = convert_for_arg_passing (type, parmval, complain);
3935 if (parmval == error_mark_node)
3936 return -1;
3938 (**values)[i] = parmval;
3940 else
3942 if (fndecl && magic_varargs_p (fndecl))
3943 /* Don't do ellipsis conversion for __built_in_constant_p
3944 as this will result in spurious errors for non-trivial
3945 types. */
3946 val = require_complete_type_sfinae (val, complain);
3947 else
3948 val = convert_arg_to_ellipsis (val, complain);
3950 (**values)[i] = val;
3953 if (typetail)
3954 typetail = TREE_CHAIN (typetail);
3957 if (typetail != 0 && typetail != void_list_node)
3959 /* See if there are default arguments that can be used. Because
3960 we hold default arguments in the FUNCTION_TYPE (which is so
3961 wrong), we can see default parameters here from deduced
3962 contexts (and via typeof) for indirect function calls.
3963 Fortunately we know whether we have a function decl to
3964 provide default arguments in a language conformant
3965 manner. */
3966 if (fndecl && TREE_PURPOSE (typetail)
3967 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3969 for (; typetail != void_list_node; ++i)
3971 /* After DR777, with explicit template args we can end up with a
3972 default argument followed by no default argument. */
3973 if (!TREE_PURPOSE (typetail))
3974 break;
3975 tree parmval
3976 = convert_default_arg (TREE_VALUE (typetail),
3977 TREE_PURPOSE (typetail),
3978 fndecl, i, complain);
3980 if (parmval == error_mark_node)
3981 return -1;
3983 vec_safe_push (*values, parmval);
3984 typetail = TREE_CHAIN (typetail);
3985 /* ends with `...'. */
3986 if (typetail == NULL_TREE)
3987 break;
3991 if (typetail && typetail != void_list_node)
3993 if (complain & tf_error)
3994 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3995 return -1;
3999 return (int) i;
4002 /* Build a binary-operation expression, after performing default
4003 conversions on the operands. CODE is the kind of expression to
4004 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4005 are the tree codes which correspond to ARG1 and ARG2 when issuing
4006 warnings about possibly misplaced parentheses. They may differ
4007 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4008 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4009 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4010 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4011 ARG2_CODE as ERROR_MARK. */
4013 tree
4014 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
4015 enum tree_code arg1_code, tree arg2,
4016 enum tree_code arg2_code, tree *overload_p,
4017 tsubst_flags_t complain)
4019 tree orig_arg1;
4020 tree orig_arg2;
4021 tree expr;
4022 tree overload = NULL_TREE;
4024 orig_arg1 = arg1;
4025 orig_arg2 = arg2;
4027 if (processing_template_decl)
4029 if (type_dependent_expression_p (arg1)
4030 || type_dependent_expression_p (arg2))
4031 return build_min_nt_loc (loc, code, arg1, arg2);
4032 arg1 = build_non_dependent_expr (arg1);
4033 arg2 = build_non_dependent_expr (arg2);
4036 if (code == DOTSTAR_EXPR)
4037 expr = build_m_component_ref (arg1, arg2, complain);
4038 else
4039 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
4040 &overload, complain);
4042 if (overload_p != NULL)
4043 *overload_p = overload;
4045 /* Check for cases such as x+y<<z which users are likely to
4046 misinterpret. But don't warn about obj << x + y, since that is a
4047 common idiom for I/O. */
4048 if (warn_parentheses
4049 && (complain & tf_warning)
4050 && !processing_template_decl
4051 && !error_operand_p (arg1)
4052 && !error_operand_p (arg2)
4053 && (code != LSHIFT_EXPR
4054 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
4055 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
4056 arg2_code, orig_arg2);
4058 if (processing_template_decl && expr != error_mark_node)
4060 if (overload != NULL_TREE)
4061 return (build_min_non_dep_op_overload
4062 (code, expr, overload, orig_arg1, orig_arg2));
4064 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
4067 return expr;
4070 /* Build and return an ARRAY_REF expression. */
4072 tree
4073 build_x_array_ref (location_t loc, tree arg1, tree arg2,
4074 tsubst_flags_t complain)
4076 tree orig_arg1 = arg1;
4077 tree orig_arg2 = arg2;
4078 tree expr;
4079 tree overload = NULL_TREE;
4081 if (processing_template_decl)
4083 if (type_dependent_expression_p (arg1)
4084 || type_dependent_expression_p (arg2))
4085 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4086 NULL_TREE, NULL_TREE);
4087 arg1 = build_non_dependent_expr (arg1);
4088 arg2 = build_non_dependent_expr (arg2);
4091 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4092 NULL_TREE, &overload, complain);
4094 if (processing_template_decl && expr != error_mark_node)
4096 if (overload != NULL_TREE)
4097 return (build_min_non_dep_op_overload
4098 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4100 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4101 NULL_TREE, NULL_TREE);
4103 return expr;
4106 /* Return whether OP is an expression of enum type cast to integer
4107 type. In C++ even unsigned enum types are cast to signed integer
4108 types. We do not want to issue warnings about comparisons between
4109 signed and unsigned types when one of the types is an enum type.
4110 Those warnings are always false positives in practice. */
4112 static bool
4113 enum_cast_to_int (tree op)
4115 if (CONVERT_EXPR_P (op)
4116 && TREE_TYPE (op) == integer_type_node
4117 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4118 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4119 return true;
4121 /* The cast may have been pushed into a COND_EXPR. */
4122 if (TREE_CODE (op) == COND_EXPR)
4123 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4124 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4126 return false;
4129 /* For the c-common bits. */
4130 tree
4131 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4132 bool /*convert_p*/)
4134 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4137 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4138 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4140 static tree
4141 build_vec_cmp (tree_code code, tree type,
4142 tree arg0, tree arg1)
4144 tree zero_vec = build_zero_cst (type);
4145 tree minus_one_vec = build_minus_one_cst (type);
4146 tree cmp_type = build_same_sized_truth_vector_type(type);
4147 tree cmp = build2 (code, cmp_type, arg0, arg1);
4148 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4151 /* Possibly warn about an address never being NULL. */
4153 static void
4154 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4156 if (!warn_address
4157 || (complain & tf_warning) == 0
4158 || c_inhibit_evaluation_warnings != 0
4159 || TREE_NO_WARNING (op))
4160 return;
4162 tree cop = fold_non_dependent_expr (op);
4164 if (TREE_CODE (cop) == ADDR_EXPR
4165 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4166 && !TREE_NO_WARNING (cop))
4167 warning_at (location, OPT_Waddress, "the address of %qD will never "
4168 "be NULL", TREE_OPERAND (cop, 0));
4170 if (CONVERT_EXPR_P (op)
4171 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
4173 tree inner_op = op;
4174 STRIP_NOPS (inner_op);
4176 if (DECL_P (inner_op))
4177 warning_at (location, OPT_Waddress,
4178 "the compiler can assume that the address of "
4179 "%qD will never be NULL", inner_op);
4183 /* Build a binary-operation expression without default conversions.
4184 CODE is the kind of expression to build.
4185 LOCATION is the location_t of the operator in the source code.
4186 This function differs from `build' in several ways:
4187 the data type of the result is computed and recorded in it,
4188 warnings are generated if arg data types are invalid,
4189 special handling for addition and subtraction of pointers is known,
4190 and some optimization is done (operations on narrow ints
4191 are done in the narrower type when that gives the same result).
4192 Constant folding is also done before the result is returned.
4194 Note that the operands will never have enumeral types
4195 because either they have just had the default conversions performed
4196 or they have both just been converted to some other type in which
4197 the arithmetic is to be done.
4199 C++: must do special pointer arithmetic when implementing
4200 multiple inheritance, and deal with pointer to member functions. */
4202 tree
4203 cp_build_binary_op (location_t location,
4204 enum tree_code code, tree orig_op0, tree orig_op1,
4205 tsubst_flags_t complain)
4207 tree op0, op1;
4208 enum tree_code code0, code1;
4209 tree type0, type1;
4210 const char *invalid_op_diag;
4212 /* Expression code to give to the expression when it is built.
4213 Normally this is CODE, which is what the caller asked for,
4214 but in some special cases we change it. */
4215 enum tree_code resultcode = code;
4217 /* Data type in which the computation is to be performed.
4218 In the simplest cases this is the common type of the arguments. */
4219 tree result_type = NULL_TREE;
4221 /* Nonzero means operands have already been type-converted
4222 in whatever way is necessary.
4223 Zero means they need to be converted to RESULT_TYPE. */
4224 int converted = 0;
4226 /* Nonzero means create the expression with this type, rather than
4227 RESULT_TYPE. */
4228 tree build_type = 0;
4230 /* Nonzero means after finally constructing the expression
4231 convert it to this type. */
4232 tree final_type = 0;
4234 tree result, result_ovl;
4236 /* Nonzero if this is an operation like MIN or MAX which can
4237 safely be computed in short if both args are promoted shorts.
4238 Also implies COMMON.
4239 -1 indicates a bitwise operation; this makes a difference
4240 in the exact conditions for when it is safe to do the operation
4241 in a narrower mode. */
4242 int shorten = 0;
4244 /* Nonzero if this is a comparison operation;
4245 if both args are promoted shorts, compare the original shorts.
4246 Also implies COMMON. */
4247 int short_compare = 0;
4249 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4250 int common = 0;
4252 /* True if both operands have arithmetic type. */
4253 bool arithmetic_types_p;
4255 /* Apply default conversions. */
4256 op0 = orig_op0;
4257 op1 = orig_op1;
4259 /* Remember whether we're doing / or %. */
4260 bool doing_div_or_mod = false;
4262 /* Remember whether we're doing << or >>. */
4263 bool doing_shift = false;
4265 /* Tree holding instrumentation expression. */
4266 tree instrument_expr = NULL_TREE;
4268 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4269 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4270 || code == TRUTH_XOR_EXPR)
4272 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4273 op0 = decay_conversion (op0, complain);
4274 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4275 op1 = decay_conversion (op1, complain);
4277 else
4279 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4280 op0 = cp_default_conversion (op0, complain);
4281 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4282 op1 = cp_default_conversion (op1, complain);
4285 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4286 STRIP_TYPE_NOPS (op0);
4287 STRIP_TYPE_NOPS (op1);
4289 /* DTRT if one side is an overloaded function, but complain about it. */
4290 if (type_unknown_p (op0))
4292 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4293 if (t != error_mark_node)
4295 if (complain & tf_error)
4296 permerror (input_location, "assuming cast to type %qT from overloaded function",
4297 TREE_TYPE (t));
4298 op0 = t;
4301 if (type_unknown_p (op1))
4303 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4304 if (t != error_mark_node)
4306 if (complain & tf_error)
4307 permerror (input_location, "assuming cast to type %qT from overloaded function",
4308 TREE_TYPE (t));
4309 op1 = t;
4313 type0 = TREE_TYPE (op0);
4314 type1 = TREE_TYPE (op1);
4316 /* The expression codes of the data types of the arguments tell us
4317 whether the arguments are integers, floating, pointers, etc. */
4318 code0 = TREE_CODE (type0);
4319 code1 = TREE_CODE (type1);
4321 /* If an error was already reported for one of the arguments,
4322 avoid reporting another error. */
4323 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4324 return error_mark_node;
4326 if ((invalid_op_diag
4327 = targetm.invalid_binary_op (code, type0, type1)))
4329 if (complain & tf_error)
4330 error (invalid_op_diag);
4331 return error_mark_node;
4334 /* Issue warnings about peculiar, but valid, uses of NULL. */
4335 if ((null_node_p (orig_op0) || null_node_p (orig_op1))
4336 /* It's reasonable to use pointer values as operands of &&
4337 and ||, so NULL is no exception. */
4338 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4339 && ( /* Both are NULL (or 0) and the operation was not a
4340 comparison or a pointer subtraction. */
4341 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4342 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4343 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4344 || (!null_ptr_cst_p (orig_op0)
4345 && !TYPE_PTR_OR_PTRMEM_P (type0))
4346 || (!null_ptr_cst_p (orig_op1)
4347 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4348 && (complain & tf_warning))
4350 source_location loc =
4351 expansion_point_location_if_in_system_header (input_location);
4353 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4356 /* In case when one of the operands of the binary operation is
4357 a vector and another is a scalar -- convert scalar to vector. */
4358 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4360 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4361 complain & tf_error);
4363 switch (convert_flag)
4365 case stv_error:
4366 return error_mark_node;
4367 case stv_firstarg:
4369 op0 = convert (TREE_TYPE (type1), op0);
4370 op0 = save_expr (op0);
4371 op0 = build_vector_from_val (type1, op0);
4372 type0 = TREE_TYPE (op0);
4373 code0 = TREE_CODE (type0);
4374 converted = 1;
4375 break;
4377 case stv_secondarg:
4379 op1 = convert (TREE_TYPE (type0), op1);
4380 op1 = save_expr (op1);
4381 op1 = build_vector_from_val (type0, op1);
4382 type1 = TREE_TYPE (op1);
4383 code1 = TREE_CODE (type1);
4384 converted = 1;
4385 break;
4387 default:
4388 break;
4392 switch (code)
4394 case MINUS_EXPR:
4395 /* Subtraction of two similar pointers.
4396 We must subtract them as integers, then divide by object size. */
4397 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4398 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4399 TREE_TYPE (type1)))
4401 result = pointer_diff (location, op0, op1,
4402 common_pointer_type (type0, type1), complain,
4403 &instrument_expr);
4404 if (instrument_expr != NULL)
4405 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
4406 instrument_expr, result);
4408 return result;
4410 /* In all other cases except pointer - int, the usual arithmetic
4411 rules apply. */
4412 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4414 common = 1;
4415 break;
4417 /* The pointer - int case is just like pointer + int; fall
4418 through. */
4419 gcc_fallthrough ();
4420 case PLUS_EXPR:
4421 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4422 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4424 tree ptr_operand;
4425 tree int_operand;
4426 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4427 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4428 if (processing_template_decl)
4430 result_type = TREE_TYPE (ptr_operand);
4431 break;
4433 return cp_pointer_int_sum (location, code,
4434 ptr_operand,
4435 int_operand,
4436 complain);
4438 common = 1;
4439 break;
4441 case MULT_EXPR:
4442 common = 1;
4443 break;
4445 case TRUNC_DIV_EXPR:
4446 case CEIL_DIV_EXPR:
4447 case FLOOR_DIV_EXPR:
4448 case ROUND_DIV_EXPR:
4449 case EXACT_DIV_EXPR:
4450 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4452 tree type0 = TREE_OPERAND (op0, 0);
4453 tree type1 = TREE_OPERAND (op1, 0);
4454 tree first_arg = type0;
4455 if (!TYPE_P (type0))
4456 type0 = TREE_TYPE (type0);
4457 if (!TYPE_P (type1))
4458 type1 = TREE_TYPE (type1);
4459 if (INDIRECT_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4460 && !(TREE_CODE (first_arg) == PARM_DECL
4461 && DECL_ARRAY_PARAMETER_P (first_arg)
4462 && warn_sizeof_array_argument)
4463 && (complain & tf_warning))
4464 if (warning_at (location, OPT_Wsizeof_pointer_div,
4465 "division %<sizeof (%T) / sizeof (%T)%> does "
4466 "not compute the number of array elements",
4467 type0, type1))
4468 if (DECL_P (first_arg))
4469 inform (DECL_SOURCE_LOCATION (first_arg),
4470 "first %<sizeof%> operand was declared here");
4473 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4474 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4475 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4476 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4478 enum tree_code tcode0 = code0, tcode1 = code1;
4479 tree cop1 = fold_non_dependent_expr (op1);
4480 doing_div_or_mod = true;
4481 warn_for_div_by_zero (location, cop1);
4483 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4484 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4485 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4486 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4488 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4489 resultcode = RDIV_EXPR;
4490 else
4491 /* When dividing two signed integers, we have to promote to int.
4492 unless we divide by a constant != -1. Note that default
4493 conversion will have been performed on the operands at this
4494 point, so we have to dig out the original type to find out if
4495 it was unsigned. */
4496 shorten = ((TREE_CODE (op0) == NOP_EXPR
4497 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4498 || (TREE_CODE (op1) == INTEGER_CST
4499 && ! integer_all_onesp (op1)));
4501 common = 1;
4503 break;
4505 case BIT_AND_EXPR:
4506 case BIT_IOR_EXPR:
4507 case BIT_XOR_EXPR:
4508 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4509 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4510 && !VECTOR_FLOAT_TYPE_P (type0)
4511 && !VECTOR_FLOAT_TYPE_P (type1)))
4512 shorten = -1;
4513 break;
4515 case TRUNC_MOD_EXPR:
4516 case FLOOR_MOD_EXPR:
4518 tree cop1 = fold_non_dependent_expr (op1);
4519 doing_div_or_mod = true;
4520 warn_for_div_by_zero (location, cop1);
4523 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4524 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4525 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4526 common = 1;
4527 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4529 /* Although it would be tempting to shorten always here, that loses
4530 on some targets, since the modulo instruction is undefined if the
4531 quotient can't be represented in the computation mode. We shorten
4532 only if unsigned or if dividing by something we know != -1. */
4533 shorten = ((TREE_CODE (op0) == NOP_EXPR
4534 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4535 || (TREE_CODE (op1) == INTEGER_CST
4536 && ! integer_all_onesp (op1)));
4537 common = 1;
4539 break;
4541 case TRUTH_ANDIF_EXPR:
4542 case TRUTH_ORIF_EXPR:
4543 case TRUTH_AND_EXPR:
4544 case TRUTH_OR_EXPR:
4545 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4547 if (!COMPARISON_CLASS_P (op1))
4548 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4549 build_zero_cst (type1), complain);
4550 if (code == TRUTH_ANDIF_EXPR)
4552 tree z = build_zero_cst (TREE_TYPE (op1));
4553 return build_conditional_expr (location, op0, op1, z, complain);
4555 else if (code == TRUTH_ORIF_EXPR)
4557 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4558 return build_conditional_expr (location, op0, m1, op1, complain);
4560 else
4561 gcc_unreachable ();
4563 if (VECTOR_TYPE_P (type0))
4565 if (!COMPARISON_CLASS_P (op0))
4566 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4567 build_zero_cst (type0), complain);
4568 if (!VECTOR_TYPE_P (type1))
4570 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4571 tree z = build_zero_cst (TREE_TYPE (op0));
4572 op1 = build_conditional_expr (location, op1, m1, z, complain);
4574 else if (!COMPARISON_CLASS_P (op1))
4575 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4576 build_zero_cst (type1), complain);
4578 if (code == TRUTH_ANDIF_EXPR)
4579 code = BIT_AND_EXPR;
4580 else if (code == TRUTH_ORIF_EXPR)
4581 code = BIT_IOR_EXPR;
4582 else
4583 gcc_unreachable ();
4585 return cp_build_binary_op (location, code, op0, op1, complain);
4588 result_type = boolean_type_node;
4589 break;
4591 /* Shift operations: result has same type as first operand;
4592 always convert second operand to int.
4593 Also set SHORT_SHIFT if shifting rightward. */
4595 case RSHIFT_EXPR:
4596 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4597 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4599 result_type = type0;
4600 converted = 1;
4602 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4603 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4604 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4605 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4606 TYPE_VECTOR_SUBPARTS (type1)))
4608 result_type = type0;
4609 converted = 1;
4611 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4613 tree const_op1 = fold_non_dependent_expr (op1);
4614 if (TREE_CODE (const_op1) != INTEGER_CST)
4615 const_op1 = op1;
4616 result_type = type0;
4617 doing_shift = true;
4618 if (TREE_CODE (const_op1) == INTEGER_CST)
4620 if (tree_int_cst_lt (const_op1, integer_zero_node))
4622 if ((complain & tf_warning)
4623 && c_inhibit_evaluation_warnings == 0)
4624 warning (OPT_Wshift_count_negative,
4625 "right shift count is negative");
4627 else
4629 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4630 && (complain & tf_warning)
4631 && c_inhibit_evaluation_warnings == 0)
4632 warning (OPT_Wshift_count_overflow,
4633 "right shift count >= width of type");
4636 /* Avoid converting op1 to result_type later. */
4637 converted = 1;
4639 break;
4641 case LSHIFT_EXPR:
4642 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4643 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4645 result_type = type0;
4646 converted = 1;
4648 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4649 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4650 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4651 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
4652 TYPE_VECTOR_SUBPARTS (type1)))
4654 result_type = type0;
4655 converted = 1;
4657 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4659 tree const_op0 = fold_non_dependent_expr (op0);
4660 if (TREE_CODE (const_op0) != INTEGER_CST)
4661 const_op0 = op0;
4662 tree const_op1 = fold_non_dependent_expr (op1);
4663 if (TREE_CODE (const_op1) != INTEGER_CST)
4664 const_op1 = op1;
4665 result_type = type0;
4666 doing_shift = true;
4667 if (TREE_CODE (const_op0) == INTEGER_CST
4668 && tree_int_cst_sgn (const_op0) < 0
4669 && (complain & tf_warning)
4670 && c_inhibit_evaluation_warnings == 0)
4671 warning (OPT_Wshift_negative_value,
4672 "left shift of negative value");
4673 if (TREE_CODE (const_op1) == INTEGER_CST)
4675 if (tree_int_cst_lt (const_op1, integer_zero_node))
4677 if ((complain & tf_warning)
4678 && c_inhibit_evaluation_warnings == 0)
4679 warning (OPT_Wshift_count_negative,
4680 "left shift count is negative");
4682 else if (compare_tree_int (const_op1,
4683 TYPE_PRECISION (type0)) >= 0)
4685 if ((complain & tf_warning)
4686 && c_inhibit_evaluation_warnings == 0)
4687 warning (OPT_Wshift_count_overflow,
4688 "left shift count >= width of type");
4690 else if (TREE_CODE (const_op0) == INTEGER_CST
4691 && (complain & tf_warning))
4692 maybe_warn_shift_overflow (location, const_op0, const_op1);
4694 /* Avoid converting op1 to result_type later. */
4695 converted = 1;
4697 break;
4699 case RROTATE_EXPR:
4700 case LROTATE_EXPR:
4701 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4703 result_type = type0;
4704 if (TREE_CODE (op1) == INTEGER_CST)
4706 if (tree_int_cst_lt (op1, integer_zero_node))
4708 if (complain & tf_warning)
4709 warning (0, (code == LROTATE_EXPR)
4710 ? G_("left rotate count is negative")
4711 : G_("right rotate count is negative"));
4713 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4715 if (complain & tf_warning)
4716 warning (0, (code == LROTATE_EXPR)
4717 ? G_("left rotate count >= width of type")
4718 : G_("right rotate count >= width of type"));
4721 /* Convert the shift-count to an integer, regardless of
4722 size of value being shifted. */
4723 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4724 op1 = cp_convert (integer_type_node, op1, complain);
4726 break;
4728 case EQ_EXPR:
4729 case NE_EXPR:
4730 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4731 goto vector_compare;
4732 if ((complain & tf_warning)
4733 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4734 warning (OPT_Wfloat_equal,
4735 "comparing floating point with == or != is unsafe");
4736 if ((complain & tf_warning)
4737 && ((TREE_CODE (orig_op0) == STRING_CST
4738 && !integer_zerop (cp_fully_fold (op1)))
4739 || (TREE_CODE (orig_op1) == STRING_CST
4740 && !integer_zerop (cp_fully_fold (op0)))))
4741 warning (OPT_Waddress, "comparison with string literal results "
4742 "in unspecified behavior");
4744 build_type = boolean_type_node;
4745 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4746 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4747 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4748 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4749 short_compare = 1;
4750 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4751 && null_ptr_cst_p (orig_op1))
4752 /* Handle, eg, (void*)0 (c++/43906), and more. */
4753 || (code0 == POINTER_TYPE
4754 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4756 if (TYPE_PTR_P (type1))
4757 result_type = composite_pointer_type (type0, type1, op0, op1,
4758 CPO_COMPARISON, complain);
4759 else
4760 result_type = type0;
4762 if (char_type_p (TREE_TYPE (orig_op1))
4763 && warning (OPT_Wpointer_compare,
4764 "comparison between pointer and zero character "
4765 "constant"))
4766 inform (input_location,
4767 "did you mean to dereference the pointer?");
4768 warn_for_null_address (location, op0, complain);
4770 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4771 && null_ptr_cst_p (orig_op0))
4772 /* Handle, eg, (void*)0 (c++/43906), and more. */
4773 || (code1 == POINTER_TYPE
4774 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4776 if (TYPE_PTR_P (type0))
4777 result_type = composite_pointer_type (type0, type1, op0, op1,
4778 CPO_COMPARISON, complain);
4779 else
4780 result_type = type1;
4782 if (char_type_p (TREE_TYPE (orig_op0))
4783 && warning (OPT_Wpointer_compare,
4784 "comparison between pointer and zero character "
4785 "constant"))
4786 inform (input_location,
4787 "did you mean to dereference the pointer?");
4788 warn_for_null_address (location, op1, complain);
4790 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4791 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4792 result_type = composite_pointer_type (type0, type1, op0, op1,
4793 CPO_COMPARISON, complain);
4794 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4795 /* One of the operands must be of nullptr_t type. */
4796 result_type = TREE_TYPE (nullptr_node);
4797 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4799 result_type = type0;
4800 if (complain & tf_error)
4801 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4802 else
4803 return error_mark_node;
4805 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4807 result_type = type1;
4808 if (complain & tf_error)
4809 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4810 else
4811 return error_mark_node;
4813 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4815 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4816 == ptrmemfunc_vbit_in_delta)
4818 tree pfn0, delta0, e1, e2;
4820 if (TREE_SIDE_EFFECTS (op0))
4821 op0 = save_expr (op0);
4823 pfn0 = pfn_from_ptrmemfunc (op0);
4824 delta0 = delta_from_ptrmemfunc (op0);
4825 e1 = cp_build_binary_op (location,
4826 EQ_EXPR,
4827 pfn0,
4828 build_zero_cst (TREE_TYPE (pfn0)),
4829 complain);
4830 e2 = cp_build_binary_op (location,
4831 BIT_AND_EXPR,
4832 delta0,
4833 integer_one_node,
4834 complain);
4836 if (complain & tf_warning)
4837 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4839 e2 = cp_build_binary_op (location,
4840 EQ_EXPR, e2, integer_zero_node,
4841 complain);
4842 op0 = cp_build_binary_op (location,
4843 TRUTH_ANDIF_EXPR, e1, e2,
4844 complain);
4845 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4847 else
4849 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4850 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4852 result_type = TREE_TYPE (op0);
4854 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4855 return cp_build_binary_op (location, code, op1, op0, complain);
4856 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4858 tree type;
4859 /* E will be the final comparison. */
4860 tree e;
4861 /* E1 and E2 are for scratch. */
4862 tree e1;
4863 tree e2;
4864 tree pfn0;
4865 tree pfn1;
4866 tree delta0;
4867 tree delta1;
4869 type = composite_pointer_type (type0, type1, op0, op1,
4870 CPO_COMPARISON, complain);
4872 if (!same_type_p (TREE_TYPE (op0), type))
4873 op0 = cp_convert_and_check (type, op0, complain);
4874 if (!same_type_p (TREE_TYPE (op1), type))
4875 op1 = cp_convert_and_check (type, op1, complain);
4877 if (op0 == error_mark_node || op1 == error_mark_node)
4878 return error_mark_node;
4880 if (TREE_SIDE_EFFECTS (op0))
4881 op0 = save_expr (op0);
4882 if (TREE_SIDE_EFFECTS (op1))
4883 op1 = save_expr (op1);
4885 pfn0 = pfn_from_ptrmemfunc (op0);
4886 pfn0 = cp_fully_fold (pfn0);
4887 /* Avoid -Waddress warnings (c++/64877). */
4888 if (TREE_CODE (pfn0) == ADDR_EXPR)
4889 TREE_NO_WARNING (pfn0) = 1;
4890 pfn1 = pfn_from_ptrmemfunc (op1);
4891 pfn1 = cp_fully_fold (pfn1);
4892 delta0 = delta_from_ptrmemfunc (op0);
4893 delta1 = delta_from_ptrmemfunc (op1);
4894 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4895 == ptrmemfunc_vbit_in_delta)
4897 /* We generate:
4899 (op0.pfn == op1.pfn
4900 && ((op0.delta == op1.delta)
4901 || (!op0.pfn && op0.delta & 1 == 0
4902 && op1.delta & 1 == 0))
4904 The reason for the `!op0.pfn' bit is that a NULL
4905 pointer-to-member is any member with a zero PFN and
4906 LSB of the DELTA field is 0. */
4908 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4909 delta0,
4910 integer_one_node,
4911 complain);
4912 e1 = cp_build_binary_op (location,
4913 EQ_EXPR, e1, integer_zero_node,
4914 complain);
4915 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4916 delta1,
4917 integer_one_node,
4918 complain);
4919 e2 = cp_build_binary_op (location,
4920 EQ_EXPR, e2, integer_zero_node,
4921 complain);
4922 e1 = cp_build_binary_op (location,
4923 TRUTH_ANDIF_EXPR, e2, e1,
4924 complain);
4925 e2 = cp_build_binary_op (location, EQ_EXPR,
4926 pfn0,
4927 build_zero_cst (TREE_TYPE (pfn0)),
4928 complain);
4929 e2 = cp_build_binary_op (location,
4930 TRUTH_ANDIF_EXPR, e2, e1, complain);
4931 e1 = cp_build_binary_op (location,
4932 EQ_EXPR, delta0, delta1, complain);
4933 e1 = cp_build_binary_op (location,
4934 TRUTH_ORIF_EXPR, e1, e2, complain);
4936 else
4938 /* We generate:
4940 (op0.pfn == op1.pfn
4941 && (!op0.pfn || op0.delta == op1.delta))
4943 The reason for the `!op0.pfn' bit is that a NULL
4944 pointer-to-member is any member with a zero PFN; the
4945 DELTA field is unspecified. */
4947 e1 = cp_build_binary_op (location,
4948 EQ_EXPR, delta0, delta1, complain);
4949 e2 = cp_build_binary_op (location,
4950 EQ_EXPR,
4951 pfn0,
4952 build_zero_cst (TREE_TYPE (pfn0)),
4953 complain);
4954 e1 = cp_build_binary_op (location,
4955 TRUTH_ORIF_EXPR, e1, e2, complain);
4957 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4958 e = cp_build_binary_op (location,
4959 TRUTH_ANDIF_EXPR, e2, e1, complain);
4960 if (code == EQ_EXPR)
4961 return e;
4962 return cp_build_binary_op (location,
4963 EQ_EXPR, e, integer_zero_node, complain);
4965 else
4967 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4968 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4969 type1));
4970 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4971 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4972 type0));
4975 break;
4977 case MAX_EXPR:
4978 case MIN_EXPR:
4979 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4980 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4981 shorten = 1;
4982 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4983 result_type = composite_pointer_type (type0, type1, op0, op1,
4984 CPO_COMPARISON, complain);
4985 break;
4987 case LE_EXPR:
4988 case GE_EXPR:
4989 case LT_EXPR:
4990 case GT_EXPR:
4991 if (TREE_CODE (orig_op0) == STRING_CST
4992 || TREE_CODE (orig_op1) == STRING_CST)
4994 if (complain & tf_warning)
4995 warning (OPT_Waddress, "comparison with string literal results "
4996 "in unspecified behavior");
4999 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5001 vector_compare:
5002 tree intt;
5003 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
5004 TREE_TYPE (type1))
5005 && !vector_types_compatible_elements_p (type0, type1))
5007 if (complain & tf_error)
5009 error_at (location, "comparing vectors with different "
5010 "element types");
5011 inform (location, "operand types are %qT and %qT",
5012 type0, type1);
5014 return error_mark_node;
5017 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
5018 TYPE_VECTOR_SUBPARTS (type1)))
5020 if (complain & tf_error)
5022 error_at (location, "comparing vectors with different "
5023 "number of elements");
5024 inform (location, "operand types are %qT and %qT",
5025 type0, type1);
5027 return error_mark_node;
5030 /* It's not precisely specified how the usual arithmetic
5031 conversions apply to the vector types. Here, we use
5032 the unsigned type if one of the operands is signed and
5033 the other one is unsigned. */
5034 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
5036 if (!TYPE_UNSIGNED (type0))
5037 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
5038 else
5039 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
5040 warning_at (location, OPT_Wsign_compare, "comparison between "
5041 "types %qT and %qT", type0, type1);
5044 /* Always construct signed integer vector type. */
5045 intt = c_common_type_for_size
5046 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
5047 if (!intt)
5049 if (complain & tf_error)
5050 error_at (location, "could not find an integer type "
5051 "of the same size as %qT", TREE_TYPE (type0));
5052 return error_mark_node;
5054 result_type = build_opaque_vector_type (intt,
5055 TYPE_VECTOR_SUBPARTS (type0));
5056 converted = 1;
5057 return build_vec_cmp (resultcode, result_type, op0, op1);
5059 build_type = boolean_type_node;
5060 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
5061 || code0 == ENUMERAL_TYPE)
5062 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5063 || code1 == ENUMERAL_TYPE))
5064 short_compare = 1;
5065 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
5066 result_type = composite_pointer_type (type0, type1, op0, op1,
5067 CPO_COMPARISON, complain);
5068 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
5070 result_type = type0;
5071 if (extra_warnings && (complain & tf_warning))
5072 warning (OPT_Wextra,
5073 "ordered comparison of pointer with integer zero");
5075 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
5077 result_type = type1;
5078 if (extra_warnings && (complain & tf_warning))
5079 warning (OPT_Wextra,
5080 "ordered comparison of pointer with integer zero");
5082 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
5083 /* One of the operands must be of nullptr_t type. */
5084 result_type = TREE_TYPE (nullptr_node);
5085 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
5087 result_type = type0;
5088 if (complain & tf_error)
5089 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5090 else
5091 return error_mark_node;
5093 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5095 result_type = type1;
5096 if (complain & tf_error)
5097 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5098 else
5099 return error_mark_node;
5102 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
5103 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
5105 op0 = save_expr (op0);
5106 op1 = save_expr (op1);
5108 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
5109 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
5112 break;
5114 case UNORDERED_EXPR:
5115 case ORDERED_EXPR:
5116 case UNLT_EXPR:
5117 case UNLE_EXPR:
5118 case UNGT_EXPR:
5119 case UNGE_EXPR:
5120 case UNEQ_EXPR:
5121 build_type = integer_type_node;
5122 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5124 if (complain & tf_error)
5125 error ("unordered comparison on non-floating point argument");
5126 return error_mark_node;
5128 common = 1;
5129 break;
5131 default:
5132 break;
5135 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5136 || code0 == ENUMERAL_TYPE)
5137 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5138 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5139 arithmetic_types_p = 1;
5140 else
5142 arithmetic_types_p = 0;
5143 /* Vector arithmetic is only allowed when both sides are vectors. */
5144 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5146 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5147 || !vector_types_compatible_elements_p (type0, type1))
5149 if (complain & tf_error)
5151 /* "location" already embeds the locations of the
5152 operands, so we don't need to add them separately
5153 to richloc. */
5154 rich_location richloc (line_table, location);
5155 binary_op_error (&richloc, code, type0, type1);
5157 return error_mark_node;
5159 arithmetic_types_p = 1;
5162 /* Determine the RESULT_TYPE, if it is not already known. */
5163 if (!result_type
5164 && arithmetic_types_p
5165 && (shorten || common || short_compare))
5167 result_type = cp_common_type (type0, type1);
5168 if (complain & tf_warning)
5169 do_warn_double_promotion (result_type, type0, type1,
5170 "implicit conversion from %qH to %qI "
5171 "to match other operand of binary "
5172 "expression",
5173 location);
5176 if (!result_type)
5178 if (complain & tf_error)
5179 error_at (location,
5180 "invalid operands of types %qT and %qT to binary %qO",
5181 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5182 return error_mark_node;
5185 /* If we're in a template, the only thing we need to know is the
5186 RESULT_TYPE. */
5187 if (processing_template_decl)
5189 /* Since the middle-end checks the type when doing a build2, we
5190 need to build the tree in pieces. This built tree will never
5191 get out of the front-end as we replace it when instantiating
5192 the template. */
5193 tree tmp = build2 (resultcode,
5194 build_type ? build_type : result_type,
5195 NULL_TREE, op1);
5196 TREE_OPERAND (tmp, 0) = op0;
5197 return tmp;
5200 /* Remember the original type; RESULT_TYPE might be changed later on
5201 by shorten_binary_op. */
5202 tree orig_type = result_type;
5204 if (arithmetic_types_p)
5206 bool first_complex = (code0 == COMPLEX_TYPE);
5207 bool second_complex = (code1 == COMPLEX_TYPE);
5208 int none_complex = (!first_complex && !second_complex);
5210 /* Adapted from patch for c/24581. */
5211 if (first_complex != second_complex
5212 && (code == PLUS_EXPR
5213 || code == MINUS_EXPR
5214 || code == MULT_EXPR
5215 || (code == TRUNC_DIV_EXPR && first_complex))
5216 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5217 && flag_signed_zeros)
5219 /* An operation on mixed real/complex operands must be
5220 handled specially, but the language-independent code can
5221 more easily optimize the plain complex arithmetic if
5222 -fno-signed-zeros. */
5223 tree real_type = TREE_TYPE (result_type);
5224 tree real, imag;
5225 if (first_complex)
5227 if (TREE_TYPE (op0) != result_type)
5228 op0 = cp_convert_and_check (result_type, op0, complain);
5229 if (TREE_TYPE (op1) != real_type)
5230 op1 = cp_convert_and_check (real_type, op1, complain);
5232 else
5234 if (TREE_TYPE (op0) != real_type)
5235 op0 = cp_convert_and_check (real_type, op0, complain);
5236 if (TREE_TYPE (op1) != result_type)
5237 op1 = cp_convert_and_check (result_type, op1, complain);
5239 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5240 return error_mark_node;
5241 if (first_complex)
5243 op0 = save_expr (op0);
5244 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5245 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5246 switch (code)
5248 case MULT_EXPR:
5249 case TRUNC_DIV_EXPR:
5250 op1 = save_expr (op1);
5251 imag = build2 (resultcode, real_type, imag, op1);
5252 /* Fall through. */
5253 case PLUS_EXPR:
5254 case MINUS_EXPR:
5255 real = build2 (resultcode, real_type, real, op1);
5256 break;
5257 default:
5258 gcc_unreachable();
5261 else
5263 op1 = save_expr (op1);
5264 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5265 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5266 switch (code)
5268 case MULT_EXPR:
5269 op0 = save_expr (op0);
5270 imag = build2 (resultcode, real_type, op0, imag);
5271 /* Fall through. */
5272 case PLUS_EXPR:
5273 real = build2 (resultcode, real_type, op0, real);
5274 break;
5275 case MINUS_EXPR:
5276 real = build2 (resultcode, real_type, op0, real);
5277 imag = build1 (NEGATE_EXPR, real_type, imag);
5278 break;
5279 default:
5280 gcc_unreachable();
5283 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5284 return result;
5287 /* For certain operations (which identify themselves by shorten != 0)
5288 if both args were extended from the same smaller type,
5289 do the arithmetic in that type and then extend.
5291 shorten !=0 and !=1 indicates a bitwise operation.
5292 For them, this optimization is safe only if
5293 both args are zero-extended or both are sign-extended.
5294 Otherwise, we might change the result.
5295 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5296 but calculated in (unsigned short) it would be (unsigned short)-1. */
5298 if (shorten && none_complex)
5300 final_type = result_type;
5301 result_type = shorten_binary_op (result_type, op0, op1,
5302 shorten == -1);
5305 /* Comparison operations are shortened too but differently.
5306 They identify themselves by setting short_compare = 1. */
5308 if (short_compare)
5310 /* We call shorten_compare only for diagnostic-reason. */
5311 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5312 xresult_type = result_type;
5313 enum tree_code xresultcode = resultcode;
5314 shorten_compare (location, &xop0, &xop1, &xresult_type,
5315 &xresultcode);
5318 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5319 && warn_sign_compare
5320 /* Do not warn until the template is instantiated; we cannot
5321 bound the ranges of the arguments until that point. */
5322 && !processing_template_decl
5323 && (complain & tf_warning)
5324 && c_inhibit_evaluation_warnings == 0
5325 /* Even unsigned enum types promote to signed int. We don't
5326 want to issue -Wsign-compare warnings for this case. */
5327 && !enum_cast_to_int (orig_op0)
5328 && !enum_cast_to_int (orig_op1))
5330 tree oop0 = maybe_constant_value (orig_op0);
5331 tree oop1 = maybe_constant_value (orig_op1);
5333 if (TREE_CODE (oop0) != INTEGER_CST)
5334 oop0 = cp_fully_fold (orig_op0);
5335 if (TREE_CODE (oop1) != INTEGER_CST)
5336 oop1 = cp_fully_fold (orig_op1);
5337 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5338 result_type, resultcode);
5342 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5343 Then the expression will be built.
5344 It will be given type FINAL_TYPE if that is nonzero;
5345 otherwise, it will be given type RESULT_TYPE. */
5346 if (! converted)
5348 if (TREE_TYPE (op0) != result_type)
5349 op0 = cp_convert_and_check (result_type, op0, complain);
5350 if (TREE_TYPE (op1) != result_type)
5351 op1 = cp_convert_and_check (result_type, op1, complain);
5353 if (op0 == error_mark_node || op1 == error_mark_node)
5354 return error_mark_node;
5357 if (build_type == NULL_TREE)
5358 build_type = result_type;
5360 if (sanitize_flags_p ((SANITIZE_SHIFT
5361 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5362 && current_function_decl != NULL_TREE
5363 && !processing_template_decl
5364 && (doing_div_or_mod || doing_shift))
5366 /* OP0 and/or OP1 might have side-effects. */
5367 op0 = cp_save_expr (op0);
5368 op1 = cp_save_expr (op1);
5369 op0 = fold_non_dependent_expr (op0);
5370 op1 = fold_non_dependent_expr (op1);
5371 if (doing_div_or_mod
5372 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5374 /* For diagnostics we want to use the promoted types without
5375 shorten_binary_op. So convert the arguments to the
5376 original result_type. */
5377 tree cop0 = op0;
5378 tree cop1 = op1;
5379 if (TREE_TYPE (cop0) != orig_type)
5380 cop0 = cp_convert (orig_type, op0, complain);
5381 if (TREE_TYPE (cop1) != orig_type)
5382 cop1 = cp_convert (orig_type, op1, complain);
5383 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5385 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5386 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5389 result = build2_loc (location, resultcode, build_type, op0, op1);
5390 if (final_type != 0)
5391 result = cp_convert (final_type, result, complain);
5393 if (instrument_expr != NULL)
5394 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5395 instrument_expr, result);
5397 if (!processing_template_decl)
5399 op0 = cp_fully_fold (op0);
5400 /* Only consider the second argument if the first isn't overflowed. */
5401 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5402 return result;
5403 op1 = cp_fully_fold (op1);
5404 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5405 return result;
5407 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5408 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5409 return result;
5411 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5412 if (TREE_OVERFLOW_P (result_ovl))
5413 overflow_warning (location, result_ovl);
5415 return result;
5418 /* Build a VEC_PERM_EXPR.
5419 This is a simple wrapper for c_build_vec_perm_expr. */
5420 tree
5421 build_x_vec_perm_expr (location_t loc,
5422 tree arg0, tree arg1, tree arg2,
5423 tsubst_flags_t complain)
5425 tree orig_arg0 = arg0;
5426 tree orig_arg1 = arg1;
5427 tree orig_arg2 = arg2;
5428 if (processing_template_decl)
5430 if (type_dependent_expression_p (arg0)
5431 || type_dependent_expression_p (arg1)
5432 || type_dependent_expression_p (arg2))
5433 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5434 arg0 = build_non_dependent_expr (arg0);
5435 if (arg1)
5436 arg1 = build_non_dependent_expr (arg1);
5437 arg2 = build_non_dependent_expr (arg2);
5439 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5440 if (processing_template_decl && exp != error_mark_node)
5441 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5442 orig_arg1, orig_arg2);
5443 return exp;
5446 /* Return a tree for the sum or difference (RESULTCODE says which)
5447 of pointer PTROP and integer INTOP. */
5449 static tree
5450 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5451 tree intop, tsubst_flags_t complain)
5453 tree res_type = TREE_TYPE (ptrop);
5455 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5456 in certain circumstance (when it's valid to do so). So we need
5457 to make sure it's complete. We don't need to check here, if we
5458 can actually complete it at all, as those checks will be done in
5459 pointer_int_sum() anyway. */
5460 complete_type (TREE_TYPE (res_type));
5462 return pointer_int_sum (loc, resultcode, ptrop,
5463 intop, complain & tf_warning_or_error);
5466 /* Return a tree for the difference of pointers OP0 and OP1.
5467 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5468 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5470 static tree
5471 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5472 tsubst_flags_t complain, tree *instrument_expr)
5474 tree result, inttype;
5475 tree restype = ptrdiff_type_node;
5476 tree target_type = TREE_TYPE (ptrtype);
5478 if (!complete_type_or_else (target_type, NULL_TREE))
5479 return error_mark_node;
5481 if (VOID_TYPE_P (target_type))
5483 if (complain & tf_error)
5484 permerror (loc, "ISO C++ forbids using pointer of "
5485 "type %<void *%> in subtraction");
5486 else
5487 return error_mark_node;
5489 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5491 if (complain & tf_error)
5492 permerror (loc, "ISO C++ forbids using pointer to "
5493 "a function in subtraction");
5494 else
5495 return error_mark_node;
5497 if (TREE_CODE (target_type) == METHOD_TYPE)
5499 if (complain & tf_error)
5500 permerror (loc, "ISO C++ forbids using pointer to "
5501 "a method in subtraction");
5502 else
5503 return error_mark_node;
5506 /* Determine integer type result of the subtraction. This will usually
5507 be the same as the result type (ptrdiff_t), but may need to be a wider
5508 type if pointers for the address space are wider than ptrdiff_t. */
5509 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5510 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5511 else
5512 inttype = restype;
5514 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5516 op0 = save_expr (op0);
5517 op1 = save_expr (op1);
5519 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5520 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5523 /* First do the subtraction, then build the divide operator
5524 and only convert at the very end.
5525 Do not do default conversions in case restype is a short type. */
5527 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5528 pointers. If some platform cannot provide that, or has a larger
5529 ptrdiff_type to support differences larger than half the address
5530 space, cast the pointers to some larger integer type and do the
5531 computations in that type. */
5532 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5533 op0 = cp_build_binary_op (loc,
5534 MINUS_EXPR,
5535 cp_convert (inttype, op0, complain),
5536 cp_convert (inttype, op1, complain),
5537 complain);
5538 else
5539 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5541 /* This generates an error if op1 is a pointer to an incomplete type. */
5542 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5544 if (complain & tf_error)
5545 error_at (loc, "invalid use of a pointer to an incomplete type in "
5546 "pointer arithmetic");
5547 else
5548 return error_mark_node;
5551 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5553 if (complain & tf_error)
5554 error_at (loc, "arithmetic on pointer to an empty aggregate");
5555 else
5556 return error_mark_node;
5559 op1 = (TYPE_PTROB_P (ptrtype)
5560 ? size_in_bytes_loc (loc, target_type)
5561 : integer_one_node);
5563 /* Do the division. */
5565 result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
5566 cp_convert (inttype, op1, complain));
5567 return cp_convert (restype, result, complain);
5570 /* Construct and perhaps optimize a tree representation
5571 for a unary operation. CODE, a tree_code, specifies the operation
5572 and XARG is the operand. */
5574 tree
5575 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5576 tsubst_flags_t complain)
5578 tree orig_expr = xarg;
5579 tree exp;
5580 int ptrmem = 0;
5581 tree overload = NULL_TREE;
5583 if (processing_template_decl)
5585 if (type_dependent_expression_p (xarg))
5586 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5588 xarg = build_non_dependent_expr (xarg);
5591 exp = NULL_TREE;
5593 /* [expr.unary.op] says:
5595 The address of an object of incomplete type can be taken.
5597 (And is just the ordinary address operator, not an overloaded
5598 "operator &".) However, if the type is a template
5599 specialization, we must complete the type at this point so that
5600 an overloaded "operator &" will be available if required. */
5601 if (code == ADDR_EXPR
5602 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5603 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5604 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5605 || (TREE_CODE (xarg) == OFFSET_REF)))
5606 /* Don't look for a function. */;
5607 else
5608 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5609 NULL_TREE, &overload, complain);
5611 if (!exp && code == ADDR_EXPR)
5613 if (is_overloaded_fn (xarg))
5615 tree fn = get_first_fn (xarg);
5616 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5618 if (complain & tf_error)
5619 error (DECL_CONSTRUCTOR_P (fn)
5620 ? G_("taking address of constructor %qD")
5621 : G_("taking address of destructor %qD"),
5622 fn);
5623 return error_mark_node;
5627 /* A pointer to member-function can be formed only by saying
5628 &X::mf. */
5629 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5630 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5632 if (TREE_CODE (xarg) != OFFSET_REF
5633 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5635 if (complain & tf_error)
5637 error ("invalid use of %qE to form a "
5638 "pointer-to-member-function", xarg.get_value ());
5639 if (TREE_CODE (xarg) != OFFSET_REF)
5640 inform (input_location, " a qualified-id is required");
5642 return error_mark_node;
5644 else
5646 if (complain & tf_error)
5647 error ("parentheses around %qE cannot be used to form a"
5648 " pointer-to-member-function",
5649 xarg.get_value ());
5650 else
5651 return error_mark_node;
5652 PTRMEM_OK_P (xarg) = 1;
5656 if (TREE_CODE (xarg) == OFFSET_REF)
5658 ptrmem = PTRMEM_OK_P (xarg);
5660 if (!ptrmem && !flag_ms_extensions
5661 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5663 /* A single non-static member, make sure we don't allow a
5664 pointer-to-member. */
5665 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5666 TREE_OPERAND (xarg, 0),
5667 ovl_make (TREE_OPERAND (xarg, 1)));
5668 PTRMEM_OK_P (xarg) = ptrmem;
5672 exp = cp_build_addr_expr_strict (xarg, complain);
5675 if (processing_template_decl && exp != error_mark_node)
5677 if (overload != NULL_TREE)
5678 return (build_min_non_dep_op_overload
5679 (code, exp, overload, orig_expr, integer_zero_node));
5681 exp = build_min_non_dep (code, exp, orig_expr,
5682 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5684 if (TREE_CODE (exp) == ADDR_EXPR)
5685 PTRMEM_OK_P (exp) = ptrmem;
5686 return exp;
5689 /* Construct and perhaps optimize a tree representation
5690 for __builtin_addressof operation. ARG specifies the operand. */
5692 tree
5693 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5695 tree orig_expr = arg;
5697 if (processing_template_decl)
5699 if (type_dependent_expression_p (arg))
5700 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5702 arg = build_non_dependent_expr (arg);
5705 tree exp = cp_build_addr_expr_strict (arg, complain);
5707 if (processing_template_decl && exp != error_mark_node)
5708 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5709 return exp;
5712 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5713 constants, where a null value is represented by an INTEGER_CST of
5714 -1. */
5716 tree
5717 cp_truthvalue_conversion (tree expr)
5719 tree type = TREE_TYPE (expr);
5720 if (TYPE_PTR_OR_PTRMEM_P (type)
5721 /* Avoid ICE on invalid use of non-static member function. */
5722 || TREE_CODE (expr) == FUNCTION_DECL)
5723 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, true);
5724 else
5725 return c_common_truthvalue_conversion (input_location, expr);
5728 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5730 tree
5731 condition_conversion (tree expr)
5733 tree t;
5734 /* Anything that might happen in a template should go through
5735 maybe_convert_cond. */
5736 gcc_assert (!processing_template_decl);
5737 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5738 tf_warning_or_error, LOOKUP_NORMAL);
5739 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5740 return t;
5743 /* Returns the address of T. This function will fold away
5744 ADDR_EXPR of INDIRECT_REF. */
5746 tree
5747 build_address (tree t)
5749 if (error_operand_p (t) || !cxx_mark_addressable (t))
5750 return error_mark_node;
5751 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
5752 || processing_template_decl);
5753 t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
5754 if (TREE_CODE (t) != ADDR_EXPR)
5755 t = rvalue (t);
5756 return t;
5759 /* Return a NOP_EXPR converting EXPR to TYPE. */
5761 tree
5762 build_nop (tree type, tree expr)
5764 if (type == error_mark_node || error_operand_p (expr))
5765 return expr;
5766 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5769 /* Take the address of ARG, whatever that means under C++ semantics.
5770 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5771 and class rvalues as well.
5773 Nothing should call this function directly; instead, callers should use
5774 cp_build_addr_expr or cp_build_addr_expr_strict. */
5776 static tree
5777 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5779 tree argtype;
5780 tree val;
5782 if (!arg || error_operand_p (arg))
5783 return error_mark_node;
5785 arg = mark_lvalue_use (arg);
5786 if (error_operand_p (arg))
5787 return error_mark_node;
5789 argtype = lvalue_type (arg);
5791 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5793 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5794 && !really_overloaded_fn (arg))
5796 /* They're trying to take the address of a unique non-static
5797 member function. This is ill-formed (except in MS-land),
5798 but let's try to DTRT.
5799 Note: We only handle unique functions here because we don't
5800 want to complain if there's a static overload; non-unique
5801 cases will be handled by instantiate_type. But we need to
5802 handle this case here to allow casts on the resulting PMF.
5803 We could defer this in non-MS mode, but it's easier to give
5804 a useful error here. */
5806 /* Inside constant member functions, the `this' pointer
5807 contains an extra const qualifier. TYPE_MAIN_VARIANT
5808 is used here to remove this const from the diagnostics
5809 and the created OFFSET_REF. */
5810 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5811 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5812 if (!mark_used (fn, complain) && !(complain & tf_error))
5813 return error_mark_node;
5815 if (! flag_ms_extensions)
5817 tree name = DECL_NAME (fn);
5818 if (!(complain & tf_error))
5819 return error_mark_node;
5820 else if (current_class_type
5821 && TREE_OPERAND (arg, 0) == current_class_ref)
5822 /* An expression like &memfn. */
5823 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5824 " or parenthesized non-static member function to form"
5825 " a pointer to member function. Say %<&%T::%D%>",
5826 base, name);
5827 else
5828 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5829 " function to form a pointer to member function."
5830 " Say %<&%T::%D%>",
5831 base, name);
5833 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5836 /* Uninstantiated types are all functions. Taking the
5837 address of a function is a no-op, so just return the
5838 argument. */
5839 if (type_unknown_p (arg))
5840 return build1 (ADDR_EXPR, unknown_type_node, arg);
5842 if (TREE_CODE (arg) == OFFSET_REF)
5843 /* We want a pointer to member; bypass all the code for actually taking
5844 the address of something. */
5845 goto offset_ref;
5847 /* Anything not already handled and not a true memory reference
5848 is an error. */
5849 if (TREE_CODE (argtype) != FUNCTION_TYPE
5850 && TREE_CODE (argtype) != METHOD_TYPE)
5852 cp_lvalue_kind kind = lvalue_kind (arg);
5853 if (kind == clk_none)
5855 if (complain & tf_error)
5856 lvalue_error (input_location, lv_addressof);
5857 return error_mark_node;
5859 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5861 if (!(complain & tf_error))
5862 return error_mark_node;
5863 /* Make this a permerror because we used to accept it. */
5864 permerror (input_location, "taking address of rvalue");
5868 if (TYPE_REF_P (argtype))
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 (TYPE_REF_P (TREE_TYPE (arg)))
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 (TYPE_REF_P (TREE_TYPE (t)))
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 ((TYPE_REF_P (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 obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
6837 tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
6838 tree delta = (get_delta_difference
6839 (obase, nbase,
6840 allow_inverse_p, c_cast_p, complain));
6842 if (delta == error_mark_node)
6843 return error_mark_node;
6845 if (!same_type_p (obase, nbase))
6847 if (TREE_CODE (expr) == PTRMEM_CST)
6848 expr = cplus_expand_constant (expr);
6850 tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
6851 build_int_cst (TREE_TYPE (expr), -1),
6852 complain);
6853 tree op1 = build_nop (ptrdiff_type_node, expr);
6854 tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
6855 complain);
6857 expr = fold_build3_loc (input_location,
6858 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6861 return build_nop (type, expr);
6863 else
6864 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6865 allow_inverse_p, c_cast_p, complain);
6868 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6869 this static_cast is being attempted as one of the possible casts
6870 allowed by a C-style cast. (In that case, accessibility of base
6871 classes is not considered, and it is OK to cast away
6872 constness.) Return the result of the cast. *VALID_P is set to
6873 indicate whether or not the cast was valid. */
6875 static tree
6876 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6877 bool *valid_p, tsubst_flags_t complain)
6879 tree intype;
6880 tree result;
6881 cp_lvalue_kind clk;
6883 /* Assume the cast is valid. */
6884 *valid_p = true;
6886 intype = unlowered_expr_type (expr);
6888 /* Save casted types in the function's used types hash table. */
6889 used_types_insert (type);
6891 /* A prvalue of non-class type is cv-unqualified. */
6892 if (!CLASS_TYPE_P (type))
6893 type = cv_unqualified (type);
6895 /* [expr.static.cast]
6897 An lvalue of type "cv1 B", where B is a class type, can be cast
6898 to type "reference to cv2 D", where D is a class derived (clause
6899 _class.derived_) from B, if a valid standard conversion from
6900 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6901 same cv-qualification as, or greater cv-qualification than, cv1,
6902 and B is not a virtual base class of D. */
6903 /* We check this case before checking the validity of "TYPE t =
6904 EXPR;" below because for this case:
6906 struct B {};
6907 struct D : public B { D(const B&); };
6908 extern B& b;
6909 void f() { static_cast<const D&>(b); }
6911 we want to avoid constructing a new D. The standard is not
6912 completely clear about this issue, but our interpretation is
6913 consistent with other compilers. */
6914 if (TYPE_REF_P (type)
6915 && CLASS_TYPE_P (TREE_TYPE (type))
6916 && CLASS_TYPE_P (intype)
6917 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6918 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6919 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6920 build_pointer_type (TYPE_MAIN_VARIANT
6921 (TREE_TYPE (type))),
6922 complain)
6923 && (c_cast_p
6924 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6926 tree base;
6928 if (processing_template_decl)
6929 return expr;
6931 /* There is a standard conversion from "D*" to "B*" even if "B"
6932 is ambiguous or inaccessible. If this is really a
6933 static_cast, then we check both for inaccessibility and
6934 ambiguity. However, if this is a static_cast being performed
6935 because the user wrote a C-style cast, then accessibility is
6936 not considered. */
6937 base = lookup_base (TREE_TYPE (type), intype,
6938 c_cast_p ? ba_unique : ba_check,
6939 NULL, complain);
6940 expr = build_address (expr);
6942 if (sanitize_flags_p (SANITIZE_VPTR))
6944 tree ubsan_check
6945 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6946 intype, expr);
6947 if (ubsan_check)
6948 expr = ubsan_check;
6951 /* Convert from "B*" to "D*". This function will check that "B"
6952 is not a virtual base of "D". Even if we don't have a guarantee
6953 that expr is NULL, if the static_cast is to a reference type,
6954 it is UB if it would be NULL, so omit the non-NULL check. */
6955 expr = build_base_path (MINUS_EXPR, expr, base,
6956 /*nonnull=*/flag_delete_null_pointer_checks,
6957 complain);
6959 /* Convert the pointer to a reference -- but then remember that
6960 there are no expressions with reference type in C++.
6962 We call rvalue so that there's an actual tree code
6963 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6964 is a variable with the same type, the conversion would get folded
6965 away, leaving just the variable and causing lvalue_kind to give
6966 the wrong answer. */
6967 expr = cp_fold_convert (type, expr);
6969 /* When -fsanitize=null, make sure to diagnose reference binding to
6970 NULL even when the reference is converted to pointer later on. */
6971 if (sanitize_flags_p (SANITIZE_NULL)
6972 && TREE_CODE (expr) == COND_EXPR
6973 && TREE_OPERAND (expr, 2)
6974 && TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
6975 && TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
6976 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
6978 return convert_from_reference (rvalue (expr));
6981 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6982 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6983 if (TYPE_REF_P (type)
6984 && TYPE_REF_IS_RVALUE (type)
6985 && (clk = real_lvalue_p (expr))
6986 && reference_related_p (TREE_TYPE (type), intype)
6987 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6989 if (processing_template_decl)
6990 return expr;
6991 if (clk == clk_ordinary)
6993 /* Handle the (non-bit-field) lvalue case here by casting to
6994 lvalue reference and then changing it to an rvalue reference.
6995 Casting an xvalue to rvalue reference will be handled by the
6996 main code path. */
6997 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6998 result = (perform_direct_initialization_if_possible
6999 (lref, expr, c_cast_p, complain));
7000 result = build1 (NON_LVALUE_EXPR, type, result);
7001 return convert_from_reference (result);
7003 else
7004 /* For a bit-field or packed field, bind to a temporary. */
7005 expr = rvalue (expr);
7008 /* Resolve overloaded address here rather than once in
7009 implicit_conversion and again in the inverse code below. */
7010 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
7012 expr = instantiate_type (type, expr, complain);
7013 intype = TREE_TYPE (expr);
7016 /* [expr.static.cast]
7018 Any expression can be explicitly converted to type cv void. */
7019 if (VOID_TYPE_P (type))
7020 return convert_to_void (expr, ICV_CAST, complain);
7022 /* [class.abstract]
7023 An abstract class shall not be used ... as the type of an explicit
7024 conversion. */
7025 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
7026 return error_mark_node;
7028 /* [expr.static.cast]
7030 An expression e can be explicitly converted to a type T using a
7031 static_cast of the form static_cast<T>(e) if the declaration T
7032 t(e);" is well-formed, for some invented temporary variable
7033 t. */
7034 result = perform_direct_initialization_if_possible (type, expr,
7035 c_cast_p, complain);
7036 if (result)
7038 if (processing_template_decl)
7039 return expr;
7041 result = convert_from_reference (result);
7043 /* [expr.static.cast]
7045 If T is a reference type, the result is an lvalue; otherwise,
7046 the result is an rvalue. */
7047 if (!TYPE_REF_P (type))
7049 result = rvalue (result);
7051 if (result == expr && SCALAR_TYPE_P (type))
7052 /* Leave some record of the cast. */
7053 result = build_nop (type, expr);
7055 return result;
7058 /* [expr.static.cast]
7060 The inverse of any standard conversion sequence (clause _conv_),
7061 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7062 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7063 (_conv.bool_) conversions, can be performed explicitly using
7064 static_cast subject to the restriction that the explicit
7065 conversion does not cast away constness (_expr.const.cast_), and
7066 the following additional rules for specific cases: */
7067 /* For reference, the conversions not excluded are: integral
7068 promotions, floating point promotion, integral conversions,
7069 floating point conversions, floating-integral conversions,
7070 pointer conversions, and pointer to member conversions. */
7071 /* DR 128
7073 A value of integral _or enumeration_ type can be explicitly
7074 converted to an enumeration type. */
7075 /* The effect of all that is that any conversion between any two
7076 types which are integral, floating, or enumeration types can be
7077 performed. */
7078 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7079 || SCALAR_FLOAT_TYPE_P (type))
7080 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
7081 || SCALAR_FLOAT_TYPE_P (intype)))
7083 if (processing_template_decl)
7084 return expr;
7085 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
7088 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
7089 && CLASS_TYPE_P (TREE_TYPE (type))
7090 && CLASS_TYPE_P (TREE_TYPE (intype))
7091 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7092 (TREE_TYPE (intype))),
7093 build_pointer_type (TYPE_MAIN_VARIANT
7094 (TREE_TYPE (type))),
7095 complain))
7097 tree base;
7099 if (processing_template_decl)
7100 return expr;
7102 if (!c_cast_p
7103 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7104 complain))
7105 return error_mark_node;
7106 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
7107 c_cast_p ? ba_unique : ba_check,
7108 NULL, complain);
7109 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
7110 complain);
7112 if (sanitize_flags_p (SANITIZE_VPTR))
7114 tree ubsan_check
7115 = cp_ubsan_maybe_instrument_downcast (input_location, type,
7116 intype, expr);
7117 if (ubsan_check)
7118 expr = ubsan_check;
7121 return cp_fold_convert (type, expr);
7124 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7125 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7127 tree c1;
7128 tree c2;
7129 tree t1;
7130 tree t2;
7132 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
7133 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
7135 if (TYPE_PTRDATAMEM_P (type))
7137 t1 = (build_ptrmem_type
7138 (c1,
7139 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
7140 t2 = (build_ptrmem_type
7141 (c2,
7142 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7144 else
7146 t1 = intype;
7147 t2 = type;
7149 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7151 if (!c_cast_p
7152 && check_for_casting_away_constness (intype, type,
7153 STATIC_CAST_EXPR,
7154 complain))
7155 return error_mark_node;
7156 if (processing_template_decl)
7157 return expr;
7158 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7159 c_cast_p, complain);
7163 /* [expr.static.cast]
7165 An rvalue of type "pointer to cv void" can be explicitly
7166 converted to a pointer to object type. A value of type pointer
7167 to object converted to "pointer to cv void" and back to the
7168 original pointer type will have its original value. */
7169 if (TYPE_PTR_P (intype)
7170 && VOID_TYPE_P (TREE_TYPE (intype))
7171 && TYPE_PTROB_P (type))
7173 if (!c_cast_p
7174 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7175 complain))
7176 return error_mark_node;
7177 if (processing_template_decl)
7178 return expr;
7179 return build_nop (type, expr);
7182 *valid_p = false;
7183 return error_mark_node;
7186 /* Return an expression representing static_cast<TYPE>(EXPR). */
7188 tree
7189 build_static_cast (tree type, tree oexpr, tsubst_flags_t complain)
7191 tree expr = oexpr;
7192 tree result;
7193 bool valid_p;
7195 if (type == error_mark_node || expr == error_mark_node)
7196 return error_mark_node;
7198 bool dependent = (dependent_type_p (type)
7199 || type_dependent_expression_p (expr));
7200 if (dependent)
7202 tmpl:
7203 expr = build_min (STATIC_CAST_EXPR, type, oexpr);
7204 /* We don't know if it will or will not have side effects. */
7205 TREE_SIDE_EFFECTS (expr) = 1;
7206 return convert_from_reference (expr);
7208 else if (processing_template_decl)
7209 expr = build_non_dependent_expr (expr);
7211 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7212 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7213 if (!TYPE_REF_P (type)
7214 && TREE_CODE (expr) == NOP_EXPR
7215 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7216 expr = TREE_OPERAND (expr, 0);
7218 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7219 complain);
7220 if (valid_p)
7222 if (result != error_mark_node)
7224 maybe_warn_about_useless_cast (type, expr, complain);
7225 maybe_warn_about_cast_ignoring_quals (type, complain);
7227 if (processing_template_decl)
7228 goto tmpl;
7229 return result;
7232 if (complain & tf_error)
7233 error ("invalid static_cast from type %qT to type %qT",
7234 TREE_TYPE (expr), type);
7235 return error_mark_node;
7238 /* EXPR is an expression with member function or pointer-to-member
7239 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7240 not permitted by ISO C++, but we accept it in some modes. If we
7241 are not in one of those modes, issue a diagnostic. Return the
7242 converted expression. */
7244 tree
7245 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7247 tree intype;
7248 tree decl;
7250 intype = TREE_TYPE (expr);
7251 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7252 || TREE_CODE (intype) == METHOD_TYPE);
7254 if (!(complain & tf_warning_or_error))
7255 return error_mark_node;
7257 if (pedantic || warn_pmf2ptr)
7258 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7259 "converting from %qH to %qI", intype, type);
7261 if (TREE_CODE (intype) == METHOD_TYPE)
7262 expr = build_addr_func (expr, complain);
7263 else if (TREE_CODE (expr) == PTRMEM_CST)
7264 expr = build_address (PTRMEM_CST_MEMBER (expr));
7265 else
7267 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7268 decl = build_address (decl);
7269 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7272 if (expr == error_mark_node)
7273 return error_mark_node;
7275 return build_nop (type, expr);
7278 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7279 constexpr evaluation knows to reject it. */
7281 static tree
7282 build_nop_reinterpret (tree type, tree expr)
7284 tree ret = build_nop (type, expr);
7285 if (ret != expr)
7286 REINTERPRET_CAST_P (ret) = true;
7287 return ret;
7290 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7291 If C_CAST_P is true, this reinterpret cast is being done as part of
7292 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7293 indicate whether or not reinterpret_cast was valid. */
7295 static tree
7296 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7297 bool *valid_p, tsubst_flags_t complain)
7299 tree intype;
7301 /* Assume the cast is invalid. */
7302 if (valid_p)
7303 *valid_p = true;
7305 if (type == error_mark_node || error_operand_p (expr))
7306 return error_mark_node;
7308 intype = TREE_TYPE (expr);
7310 /* Save casted types in the function's used types hash table. */
7311 used_types_insert (type);
7313 /* A prvalue of non-class type is cv-unqualified. */
7314 if (!CLASS_TYPE_P (type))
7315 type = cv_unqualified (type);
7317 /* [expr.reinterpret.cast]
7318 A glvalue expression of type T1 can be cast to the type
7319 "reference to T2" if an expression of type "pointer to T1" can be
7320 explicitly converted to the type "pointer to T2" using a
7321 reinterpret_cast. */
7322 if (TYPE_REF_P (type))
7324 if (TYPE_REF_IS_RVALUE (type))
7326 if (!obvalue_p (expr))
7327 /* Perform the temporary materialization conversion. */
7328 expr = get_target_expr_sfinae (expr, complain);
7330 else if (!lvalue_p (expr))
7332 if (complain & tf_error)
7333 error ("invalid cast of an rvalue expression of type "
7334 "%qT to type %qT",
7335 intype, type);
7336 return error_mark_node;
7339 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7340 "B" are related class types; the reinterpret_cast does not
7341 adjust the pointer. */
7342 if (TYPE_PTR_P (intype)
7343 && (complain & tf_warning)
7344 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7345 COMPARE_BASE | COMPARE_DERIVED)))
7346 warning (0, "casting %qT to %qT does not dereference pointer",
7347 intype, type);
7349 expr = cp_build_addr_expr (expr, complain);
7351 if (warn_strict_aliasing > 2)
7352 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7354 if (expr != error_mark_node)
7355 expr = build_reinterpret_cast_1
7356 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7357 valid_p, complain);
7358 if (expr != error_mark_node)
7359 /* cp_build_indirect_ref isn't right for rvalue refs. */
7360 expr = convert_from_reference (fold_convert (type, expr));
7361 return expr;
7364 /* As a G++ extension, we consider conversions from member
7365 functions, and pointers to member functions to
7366 pointer-to-function and pointer-to-void types. If
7367 -Wno-pmf-conversions has not been specified,
7368 convert_member_func_to_ptr will issue an error message. */
7369 if ((TYPE_PTRMEMFUNC_P (intype)
7370 || TREE_CODE (intype) == METHOD_TYPE)
7371 && TYPE_PTR_P (type)
7372 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7373 || VOID_TYPE_P (TREE_TYPE (type))))
7374 return convert_member_func_to_ptr (type, expr, complain);
7376 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7377 array-to-pointer, and function-to-pointer conversions are
7378 performed. */
7379 expr = decay_conversion (expr, complain);
7381 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7382 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7383 if (TREE_CODE (expr) == NOP_EXPR
7384 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7385 expr = TREE_OPERAND (expr, 0);
7387 if (error_operand_p (expr))
7388 return error_mark_node;
7390 intype = TREE_TYPE (expr);
7392 /* [expr.reinterpret.cast]
7393 A pointer can be converted to any integral type large enough to
7394 hold it. ... A value of type std::nullptr_t can be converted to
7395 an integral type; the conversion has the same meaning and
7396 validity as a conversion of (void*)0 to the integral type. */
7397 if (CP_INTEGRAL_TYPE_P (type)
7398 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7400 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7402 if (complain & tf_error)
7403 permerror (input_location, "cast from %qH to %qI loses precision",
7404 intype, type);
7405 else
7406 return error_mark_node;
7408 if (NULLPTR_TYPE_P (intype))
7409 return build_int_cst (type, 0);
7411 /* [expr.reinterpret.cast]
7412 A value of integral or enumeration type can be explicitly
7413 converted to a pointer. */
7414 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7415 /* OK */
7417 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7418 || TYPE_PTR_OR_PTRMEM_P (type))
7419 && same_type_p (type, intype))
7420 /* DR 799 */
7421 return rvalue (expr);
7422 else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7424 if ((complain & tf_warning)
7425 && !cxx_safe_function_type_cast_p (TREE_TYPE (type),
7426 TREE_TYPE (intype)))
7427 warning (OPT_Wcast_function_type,
7428 "cast between incompatible function types"
7429 " from %qH to %qI", intype, type);
7430 return build_nop_reinterpret (type, expr);
7432 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
7434 if ((complain & tf_warning)
7435 && !cxx_safe_function_type_cast_p
7436 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
7437 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
7438 warning (OPT_Wcast_function_type,
7439 "cast between incompatible pointer to member types"
7440 " from %qH to %qI", intype, type);
7441 return build_nop_reinterpret (type, expr);
7443 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7444 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7446 if (!c_cast_p
7447 && check_for_casting_away_constness (intype, type,
7448 REINTERPRET_CAST_EXPR,
7449 complain))
7450 return error_mark_node;
7451 /* Warn about possible alignment problems. */
7452 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7453 && (complain & tf_warning)
7454 && !VOID_TYPE_P (type)
7455 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7456 && COMPLETE_TYPE_P (TREE_TYPE (type))
7457 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7458 && min_align_of_type (TREE_TYPE (type))
7459 > min_align_of_type (TREE_TYPE (intype)))
7460 warning (OPT_Wcast_align, "cast from %qH to %qI "
7461 "increases required alignment of target type", intype, type);
7463 if (warn_strict_aliasing <= 2)
7464 /* strict_aliasing_warning STRIP_NOPs its expr. */
7465 strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
7467 return build_nop_reinterpret (type, expr);
7469 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7470 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7472 if (complain & tf_warning)
7473 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7474 object pointer type or vice versa is conditionally-supported." */
7475 warning (OPT_Wconditionally_supported,
7476 "casting between pointer-to-function and pointer-to-object "
7477 "is conditionally-supported");
7478 return build_nop_reinterpret (type, expr);
7480 else if (VECTOR_TYPE_P (type))
7481 return convert_to_vector (type, expr);
7482 else if (VECTOR_TYPE_P (intype)
7483 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7484 return convert_to_integer_nofold (type, expr);
7485 else
7487 if (valid_p)
7488 *valid_p = false;
7489 if (complain & tf_error)
7490 error ("invalid cast from type %qT to type %qT", intype, type);
7491 return error_mark_node;
7494 expr = cp_convert (type, expr, complain);
7495 if (TREE_CODE (expr) == NOP_EXPR)
7496 /* Mark any nop_expr that created as a reintepret_cast. */
7497 REINTERPRET_CAST_P (expr) = true;
7498 return expr;
7501 tree
7502 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7504 tree r;
7506 if (type == error_mark_node || expr == error_mark_node)
7507 return error_mark_node;
7509 if (processing_template_decl)
7511 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7513 if (!TREE_SIDE_EFFECTS (t)
7514 && type_dependent_expression_p (expr))
7515 /* There might turn out to be side effects inside expr. */
7516 TREE_SIDE_EFFECTS (t) = 1;
7517 return convert_from_reference (t);
7520 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7521 /*valid_p=*/NULL, complain);
7522 if (r != error_mark_node)
7524 maybe_warn_about_useless_cast (type, expr, complain);
7525 maybe_warn_about_cast_ignoring_quals (type, complain);
7527 return r;
7530 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7531 return an appropriate expression. Otherwise, return
7532 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7533 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7534 performing a C-style cast, its value upon return will indicate
7535 whether or not the conversion succeeded. */
7537 static tree
7538 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7539 bool *valid_p)
7541 tree src_type;
7542 tree reference_type;
7544 /* Callers are responsible for handling error_mark_node as a
7545 destination type. */
7546 gcc_assert (dst_type != error_mark_node);
7547 /* In a template, callers should be building syntactic
7548 representations of casts, not using this machinery. */
7549 gcc_assert (!processing_template_decl);
7551 /* Assume the conversion is invalid. */
7552 if (valid_p)
7553 *valid_p = false;
7555 if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7557 if (complain & tf_error)
7558 error ("invalid use of const_cast with type %qT, "
7559 "which is not a pointer, "
7560 "reference, nor a pointer-to-data-member type", dst_type);
7561 return error_mark_node;
7564 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7566 if (complain & tf_error)
7567 error ("invalid use of const_cast with type %qT, which is a pointer "
7568 "or reference to a function type", dst_type);
7569 return error_mark_node;
7572 /* A prvalue of non-class type is cv-unqualified. */
7573 dst_type = cv_unqualified (dst_type);
7575 /* Save casted types in the function's used types hash table. */
7576 used_types_insert (dst_type);
7578 src_type = TREE_TYPE (expr);
7579 /* Expressions do not really have reference types. */
7580 if (TYPE_REF_P (src_type))
7581 src_type = TREE_TYPE (src_type);
7583 /* [expr.const.cast]
7585 For two object types T1 and T2, if a pointer to T1 can be explicitly
7586 converted to the type "pointer to T2" using a const_cast, then the
7587 following conversions can also be made:
7589 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7590 type T2 using the cast const_cast<T2&>;
7592 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7593 type T2 using the cast const_cast<T2&&>; and
7595 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7596 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7598 if (TYPE_REF_P (dst_type))
7600 reference_type = dst_type;
7601 if (!TYPE_REF_IS_RVALUE (dst_type)
7602 ? lvalue_p (expr)
7603 : obvalue_p (expr))
7604 /* OK. */;
7605 else
7607 if (complain & tf_error)
7608 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7609 src_type, dst_type);
7610 return error_mark_node;
7612 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7613 src_type = build_pointer_type (src_type);
7615 else
7617 reference_type = NULL_TREE;
7618 /* If the destination type is not a reference type, the
7619 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7620 conversions are performed. */
7621 src_type = type_decays_to (src_type);
7622 if (src_type == error_mark_node)
7623 return error_mark_node;
7626 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7628 if (comp_ptr_ttypes_const (dst_type, src_type))
7630 if (valid_p)
7632 *valid_p = true;
7633 /* This cast is actually a C-style cast. Issue a warning if
7634 the user is making a potentially unsafe cast. */
7635 check_for_casting_away_constness (src_type, dst_type,
7636 CAST_EXPR, complain);
7637 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7638 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7639 && (complain & tf_warning)
7640 && min_align_of_type (TREE_TYPE (dst_type))
7641 > min_align_of_type (TREE_TYPE (src_type)))
7642 warning (OPT_Wcast_align, "cast from %qH to %qI "
7643 "increases required alignment of target type",
7644 src_type, dst_type);
7646 if (reference_type)
7648 expr = cp_build_addr_expr (expr, complain);
7649 if (expr == error_mark_node)
7650 return error_mark_node;
7651 expr = build_nop (reference_type, expr);
7652 return convert_from_reference (expr);
7654 else
7656 expr = decay_conversion (expr, complain);
7657 if (expr == error_mark_node)
7658 return error_mark_node;
7660 /* build_c_cast puts on a NOP_EXPR to make the result not an
7661 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7662 non-lvalue context. */
7663 if (TREE_CODE (expr) == NOP_EXPR
7664 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7665 expr = TREE_OPERAND (expr, 0);
7666 return build_nop (dst_type, expr);
7669 else if (valid_p
7670 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7671 TREE_TYPE (src_type)))
7672 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7673 complain);
7676 if (complain & tf_error)
7677 error ("invalid const_cast from type %qT to type %qT",
7678 src_type, dst_type);
7679 return error_mark_node;
7682 tree
7683 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7685 tree r;
7687 if (type == error_mark_node || error_operand_p (expr))
7688 return error_mark_node;
7690 if (processing_template_decl)
7692 tree t = build_min (CONST_CAST_EXPR, type, expr);
7694 if (!TREE_SIDE_EFFECTS (t)
7695 && type_dependent_expression_p (expr))
7696 /* There might turn out to be side effects inside expr. */
7697 TREE_SIDE_EFFECTS (t) = 1;
7698 return convert_from_reference (t);
7701 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7702 if (r != error_mark_node)
7704 maybe_warn_about_useless_cast (type, expr, complain);
7705 maybe_warn_about_cast_ignoring_quals (type, complain);
7707 return r;
7710 /* Like cp_build_c_cast, but for the c-common bits. */
7712 tree
7713 build_c_cast (location_t /*loc*/, tree type, tree expr)
7715 return cp_build_c_cast (type, expr, tf_warning_or_error);
7718 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7719 preserve location information even for tree nodes that don't
7720 support it. */
7722 cp_expr
7723 build_c_cast (location_t loc, tree type, cp_expr expr)
7725 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7726 result.set_location (loc);
7727 return result;
7730 /* Build an expression representing an explicit C-style cast to type
7731 TYPE of expression EXPR. */
7733 tree
7734 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7736 tree value = expr;
7737 tree result;
7738 bool valid_p;
7740 if (type == error_mark_node || error_operand_p (expr))
7741 return error_mark_node;
7743 if (processing_template_decl)
7745 tree t = build_min (CAST_EXPR, type,
7746 tree_cons (NULL_TREE, value, NULL_TREE));
7747 /* We don't know if it will or will not have side effects. */
7748 TREE_SIDE_EFFECTS (t) = 1;
7749 return convert_from_reference (t);
7752 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7753 'Class') should always be retained, because this information aids
7754 in method lookup. */
7755 if (objc_is_object_ptr (type)
7756 && objc_is_object_ptr (TREE_TYPE (expr)))
7757 return build_nop (type, expr);
7759 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7760 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7761 if (!TYPE_REF_P (type)
7762 && TREE_CODE (value) == NOP_EXPR
7763 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7764 value = TREE_OPERAND (value, 0);
7766 if (TREE_CODE (type) == ARRAY_TYPE)
7768 /* Allow casting from T1* to T2[] because Cfront allows it.
7769 NIHCL uses it. It is not valid ISO C++ however. */
7770 if (TYPE_PTR_P (TREE_TYPE (expr)))
7772 if (complain & tf_error)
7773 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7774 else
7775 return error_mark_node;
7776 type = build_pointer_type (TREE_TYPE (type));
7778 else
7780 if (complain & tf_error)
7781 error ("ISO C++ forbids casting to an array type %qT", type);
7782 return error_mark_node;
7786 if (TREE_CODE (type) == FUNCTION_TYPE
7787 || TREE_CODE (type) == METHOD_TYPE)
7789 if (complain & tf_error)
7790 error ("invalid cast to function type %qT", type);
7791 return error_mark_node;
7794 if (TYPE_PTR_P (type)
7795 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7796 /* Casting to an integer of smaller size is an error detected elsewhere. */
7797 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7798 /* Don't warn about converting any constant. */
7799 && !TREE_CONSTANT (value))
7800 warning_at (input_location, OPT_Wint_to_pointer_cast,
7801 "cast to pointer from integer of different size");
7803 /* A C-style cast can be a const_cast. */
7804 result = build_const_cast_1 (type, value, complain & tf_warning,
7805 &valid_p);
7806 if (valid_p)
7808 if (result != error_mark_node)
7810 maybe_warn_about_useless_cast (type, value, complain);
7811 maybe_warn_about_cast_ignoring_quals (type, complain);
7813 return result;
7816 /* Or a static cast. */
7817 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7818 &valid_p, complain);
7819 /* Or a reinterpret_cast. */
7820 if (!valid_p)
7821 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7822 &valid_p, complain);
7823 /* The static_cast or reinterpret_cast may be followed by a
7824 const_cast. */
7825 if (valid_p
7826 /* A valid cast may result in errors if, for example, a
7827 conversion to an ambiguous base class is required. */
7828 && !error_operand_p (result))
7830 tree result_type;
7832 maybe_warn_about_useless_cast (type, value, complain);
7833 maybe_warn_about_cast_ignoring_quals (type, complain);
7835 /* Non-class rvalues always have cv-unqualified type. */
7836 if (!CLASS_TYPE_P (type))
7837 type = TYPE_MAIN_VARIANT (type);
7838 result_type = TREE_TYPE (result);
7839 if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
7840 result_type = TYPE_MAIN_VARIANT (result_type);
7841 /* If the type of RESULT does not match TYPE, perform a
7842 const_cast to make it match. If the static_cast or
7843 reinterpret_cast succeeded, we will differ by at most
7844 cv-qualification, so the follow-on const_cast is guaranteed
7845 to succeed. */
7846 if (!same_type_p (non_reference (type), non_reference (result_type)))
7848 result = build_const_cast_1 (type, result, false, &valid_p);
7849 gcc_assert (valid_p);
7851 return result;
7854 return error_mark_node;
7857 /* For use from the C common bits. */
7858 tree
7859 build_modify_expr (location_t location,
7860 tree lhs, tree /*lhs_origtype*/,
7861 enum tree_code modifycode,
7862 location_t /*rhs_location*/, tree rhs,
7863 tree /*rhs_origtype*/)
7865 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7866 tf_warning_or_error);
7869 /* Build an assignment expression of lvalue LHS from value RHS.
7870 MODIFYCODE is the code for a binary operator that we use
7871 to combine the old value of LHS with RHS to get the new value.
7872 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7874 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7876 tree
7877 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7878 tree rhs, tsubst_flags_t complain)
7880 lhs = mark_lvalue_use_nonread (lhs);
7882 tree result = NULL_TREE;
7883 tree newrhs = rhs;
7884 tree lhstype = TREE_TYPE (lhs);
7885 tree olhs = lhs;
7886 tree olhstype = lhstype;
7887 bool plain_assign = (modifycode == NOP_EXPR);
7888 bool compound_side_effects_p = false;
7889 tree preeval = NULL_TREE;
7891 /* Avoid duplicate error messages from operands that had errors. */
7892 if (error_operand_p (lhs) || error_operand_p (rhs))
7893 return error_mark_node;
7895 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7897 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7898 compound_side_effects_p = true;
7899 lhs = TREE_OPERAND (lhs, 1);
7902 /* Handle control structure constructs used as "lvalues". Note that we
7903 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7904 switch (TREE_CODE (lhs))
7906 /* Handle --foo = 5; as these are valid constructs in C++. */
7907 case PREDECREMENT_EXPR:
7908 case PREINCREMENT_EXPR:
7909 if (compound_side_effects_p)
7910 newrhs = rhs = stabilize_expr (rhs, &preeval);
7911 lhs = genericize_compound_lvalue (lhs);
7912 maybe_add_compound:
7913 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7914 and looked through the COMPOUND_EXPRs, readd them now around
7915 the resulting lhs. */
7916 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7918 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7919 tree *ptr = &TREE_OPERAND (lhs, 1);
7920 for (olhs = TREE_OPERAND (olhs, 1);
7921 TREE_CODE (olhs) == COMPOUND_EXPR;
7922 olhs = TREE_OPERAND (olhs, 1))
7924 *ptr = build2 (COMPOUND_EXPR, lhstype,
7925 TREE_OPERAND (olhs, 0), *ptr);
7926 ptr = &TREE_OPERAND (*ptr, 1);
7929 break;
7931 case MODIFY_EXPR:
7932 if (compound_side_effects_p)
7933 newrhs = rhs = stabilize_expr (rhs, &preeval);
7934 lhs = genericize_compound_lvalue (lhs);
7935 goto maybe_add_compound;
7937 case MIN_EXPR:
7938 case MAX_EXPR:
7939 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7940 when neither operand has side-effects. */
7941 if (!lvalue_or_else (lhs, lv_assign, complain))
7942 return error_mark_node;
7944 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7945 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7947 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7948 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7949 boolean_type_node,
7950 TREE_OPERAND (lhs, 0),
7951 TREE_OPERAND (lhs, 1)),
7952 TREE_OPERAND (lhs, 0),
7953 TREE_OPERAND (lhs, 1));
7954 gcc_fallthrough ();
7956 /* Handle (a ? b : c) used as an "lvalue". */
7957 case COND_EXPR:
7959 /* Produce (a ? (b = rhs) : (c = rhs))
7960 except that the RHS goes through a save-expr
7961 so the code to compute it is only emitted once. */
7962 tree cond;
7964 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7966 if (complain & tf_error)
7967 error ("void value not ignored as it ought to be");
7968 return error_mark_node;
7971 rhs = stabilize_expr (rhs, &preeval);
7973 /* Check this here to avoid odd errors when trying to convert
7974 a throw to the type of the COND_EXPR. */
7975 if (!lvalue_or_else (lhs, lv_assign, complain))
7976 return error_mark_node;
7978 cond = build_conditional_expr
7979 (input_location, TREE_OPERAND (lhs, 0),
7980 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7981 modifycode, rhs, complain),
7982 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7983 modifycode, rhs, complain),
7984 complain);
7986 if (cond == error_mark_node)
7987 return cond;
7988 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7989 and looked through the COMPOUND_EXPRs, readd them now around
7990 the resulting cond before adding the preevaluated rhs. */
7991 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7993 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7994 TREE_OPERAND (olhs, 0), cond);
7995 tree *ptr = &TREE_OPERAND (cond, 1);
7996 for (olhs = TREE_OPERAND (olhs, 1);
7997 TREE_CODE (olhs) == COMPOUND_EXPR;
7998 olhs = TREE_OPERAND (olhs, 1))
8000 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
8001 TREE_OPERAND (olhs, 0), *ptr);
8002 ptr = &TREE_OPERAND (*ptr, 1);
8005 /* Make sure the code to compute the rhs comes out
8006 before the split. */
8007 result = cond;
8008 goto ret;
8011 default:
8012 lhs = olhs;
8013 break;
8016 if (modifycode == INIT_EXPR)
8018 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8019 /* Do the default thing. */;
8020 else if (TREE_CODE (rhs) == CONSTRUCTOR)
8022 /* Compound literal. */
8023 if (! same_type_p (TREE_TYPE (rhs), lhstype))
8024 /* Call convert to generate an error; see PR 11063. */
8025 rhs = convert (lhstype, rhs);
8026 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
8027 TREE_SIDE_EFFECTS (result) = 1;
8028 goto ret;
8030 else if (! MAYBE_CLASS_TYPE_P (lhstype))
8031 /* Do the default thing. */;
8032 else
8034 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
8035 result = build_special_member_call (lhs, complete_ctor_identifier,
8036 &rhs_vec, lhstype, LOOKUP_NORMAL,
8037 complain);
8038 release_tree_vector (rhs_vec);
8039 if (result == NULL_TREE)
8040 return error_mark_node;
8041 goto ret;
8044 else
8046 lhs = require_complete_type_sfinae (lhs, complain);
8047 if (lhs == error_mark_node)
8048 return error_mark_node;
8050 if (modifycode == NOP_EXPR)
8052 if (c_dialect_objc ())
8054 result = objc_maybe_build_modify_expr (lhs, rhs);
8055 if (result)
8056 goto ret;
8059 /* `operator=' is not an inheritable operator. */
8060 if (! MAYBE_CLASS_TYPE_P (lhstype))
8061 /* Do the default thing. */;
8062 else
8064 result = build_new_op (input_location, MODIFY_EXPR,
8065 LOOKUP_NORMAL, lhs, rhs,
8066 make_node (NOP_EXPR), /*overload=*/NULL,
8067 complain);
8068 if (result == NULL_TREE)
8069 return error_mark_node;
8070 goto ret;
8072 lhstype = olhstype;
8074 else
8076 tree init = NULL_TREE;
8078 /* A binary op has been requested. Combine the old LHS
8079 value with the RHS producing the value we should actually
8080 store into the LHS. */
8081 gcc_assert (!((TYPE_REF_P (lhstype)
8082 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
8083 || MAYBE_CLASS_TYPE_P (lhstype)));
8085 /* Preevaluate the RHS to make sure its evaluation is complete
8086 before the lvalue-to-rvalue conversion of the LHS:
8088 [expr.ass] With respect to an indeterminately-sequenced
8089 function call, the operation of a compound assignment is a
8090 single evaluation. [ Note: Therefore, a function call shall
8091 not intervene between the lvalue-to-rvalue conversion and the
8092 side effect associated with any single compound assignment
8093 operator. -- end note ] */
8094 lhs = cp_stabilize_reference (lhs);
8095 rhs = decay_conversion (rhs, complain);
8096 if (rhs == error_mark_node)
8097 return error_mark_node;
8098 rhs = stabilize_expr (rhs, &init);
8099 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
8100 if (newrhs == error_mark_node)
8102 if (complain & tf_error)
8103 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
8104 TREE_TYPE (lhs), TREE_TYPE (rhs));
8105 return error_mark_node;
8108 if (init)
8109 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
8111 /* Now it looks like a plain assignment. */
8112 modifycode = NOP_EXPR;
8113 if (c_dialect_objc ())
8115 result = objc_maybe_build_modify_expr (lhs, newrhs);
8116 if (result)
8117 goto ret;
8120 gcc_assert (!TYPE_REF_P (lhstype));
8121 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
8124 /* The left-hand side must be an lvalue. */
8125 if (!lvalue_or_else (lhs, lv_assign, complain))
8126 return error_mark_node;
8128 /* Warn about modifying something that is `const'. Don't warn if
8129 this is initialization. */
8130 if (modifycode != INIT_EXPR
8131 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
8132 /* Functions are not modifiable, even though they are
8133 lvalues. */
8134 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
8135 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
8136 /* If it's an aggregate and any field is const, then it is
8137 effectively const. */
8138 || (CLASS_TYPE_P (lhstype)
8139 && C_TYPE_FIELDS_READONLY (lhstype))))
8141 if (complain & tf_error)
8142 cxx_readonly_error (lhs, lv_assign);
8143 return error_mark_node;
8146 /* If storing into a structure or union member, it may have been given a
8147 lowered bitfield type. We need to convert to the declared type first,
8148 so retrieve it now. */
8150 olhstype = unlowered_expr_type (lhs);
8152 /* Convert new value to destination type. */
8154 if (TREE_CODE (lhstype) == ARRAY_TYPE)
8156 int from_array;
8158 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
8160 if (modifycode != INIT_EXPR)
8162 if (complain & tf_error)
8163 error ("assigning to an array from an initializer list");
8164 return error_mark_node;
8166 if (check_array_initializer (lhs, lhstype, newrhs))
8167 return error_mark_node;
8168 newrhs = digest_init (lhstype, newrhs, complain);
8169 if (newrhs == error_mark_node)
8170 return error_mark_node;
8173 /* C++11 8.5/17: "If the destination type is an array of characters,
8174 an array of char16_t, an array of char32_t, or an array of wchar_t,
8175 and the initializer is a string literal...". */
8176 else if (TREE_CODE (newrhs) == STRING_CST
8177 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
8178 && modifycode == INIT_EXPR)
8180 newrhs = digest_init (lhstype, newrhs, complain);
8181 if (newrhs == error_mark_node)
8182 return error_mark_node;
8185 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
8186 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
8188 if (complain & tf_error)
8189 error ("incompatible types in assignment of %qT to %qT",
8190 TREE_TYPE (rhs), lhstype);
8191 return error_mark_node;
8194 /* Allow array assignment in compiler-generated code. */
8195 else if (!current_function_decl
8196 || !DECL_DEFAULTED_FN (current_function_decl))
8198 /* This routine is used for both initialization and assignment.
8199 Make sure the diagnostic message differentiates the context. */
8200 if (complain & tf_error)
8202 if (modifycode == INIT_EXPR)
8203 error ("array used as initializer");
8204 else
8205 error ("invalid array assignment");
8207 return error_mark_node;
8210 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8211 ? 1 + (modifycode != INIT_EXPR): 0;
8212 result = build_vec_init (lhs, NULL_TREE, newrhs,
8213 /*explicit_value_init_p=*/false,
8214 from_array, complain);
8215 goto ret;
8218 if (modifycode == INIT_EXPR)
8219 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8220 LOOKUP_ONLYCONVERTING. */
8221 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8222 ICR_INIT, NULL_TREE, 0,
8223 complain);
8224 else
8225 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8226 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8228 if (!same_type_p (lhstype, olhstype))
8229 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8231 if (modifycode != INIT_EXPR)
8233 if (TREE_CODE (newrhs) == CALL_EXPR
8234 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8235 newrhs = build_cplus_new (lhstype, newrhs, complain);
8237 /* Can't initialize directly from a TARGET_EXPR, since that would
8238 cause the lhs to be constructed twice, and possibly result in
8239 accidental self-initialization. So we force the TARGET_EXPR to be
8240 expanded without a target. */
8241 if (TREE_CODE (newrhs) == TARGET_EXPR)
8242 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8243 TREE_OPERAND (newrhs, 0));
8246 if (newrhs == error_mark_node)
8247 return error_mark_node;
8249 if (c_dialect_objc () && flag_objc_gc)
8251 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8253 if (result)
8254 goto ret;
8257 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8258 lhstype, lhs, newrhs);
8260 TREE_SIDE_EFFECTS (result) = 1;
8261 if (!plain_assign)
8262 TREE_NO_WARNING (result) = 1;
8264 ret:
8265 if (preeval)
8266 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8267 return result;
8270 cp_expr
8271 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8272 tree rhs, tsubst_flags_t complain)
8274 tree orig_lhs = lhs;
8275 tree orig_rhs = rhs;
8276 tree overload = NULL_TREE;
8277 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8279 if (processing_template_decl)
8281 if (modifycode == NOP_EXPR
8282 || type_dependent_expression_p (lhs)
8283 || type_dependent_expression_p (rhs))
8284 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8285 build_min_nt_loc (loc, modifycode, NULL_TREE,
8286 NULL_TREE), rhs);
8288 lhs = build_non_dependent_expr (lhs);
8289 rhs = build_non_dependent_expr (rhs);
8292 if (modifycode != NOP_EXPR)
8294 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8295 lhs, rhs, op, &overload, complain);
8296 if (rval)
8298 if (rval == error_mark_node)
8299 return rval;
8300 TREE_NO_WARNING (rval) = 1;
8301 if (processing_template_decl)
8303 if (overload != NULL_TREE)
8304 return (build_min_non_dep_op_overload
8305 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8307 return (build_min_non_dep
8308 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8310 return rval;
8313 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8316 /* Helper function for get_delta_difference which assumes FROM is a base
8317 class of TO. Returns a delta for the conversion of pointer-to-member
8318 of FROM to pointer-to-member of TO. If the conversion is invalid and
8319 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8320 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8321 If C_CAST_P is true, this conversion is taking place as part of a
8322 C-style cast. */
8324 static tree
8325 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8326 tsubst_flags_t complain)
8328 tree binfo;
8329 base_kind kind;
8331 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8332 &kind, complain);
8334 if (binfo == error_mark_node)
8336 if (!(complain & tf_error))
8337 return error_mark_node;
8339 error (" in pointer to member function conversion");
8340 return size_zero_node;
8342 else if (binfo)
8344 if (kind != bk_via_virtual)
8345 return BINFO_OFFSET (binfo);
8346 else
8347 /* FROM is a virtual base class of TO. Issue an error or warning
8348 depending on whether or not this is a reinterpret cast. */
8350 if (!(complain & tf_error))
8351 return error_mark_node;
8353 error ("pointer to member conversion via virtual base %qT",
8354 BINFO_TYPE (binfo_from_vbase (binfo)));
8356 return size_zero_node;
8359 else
8360 return NULL_TREE;
8363 /* Get difference in deltas for different pointer to member function
8364 types. If the conversion is invalid and tf_error is not set in
8365 COMPLAIN, returns error_mark_node, otherwise returns an integer
8366 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8367 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8368 conversions as well. If C_CAST_P is true this conversion is taking
8369 place as part of a C-style cast.
8371 Note that the naming of FROM and TO is kind of backwards; the return
8372 value is what we add to a TO in order to get a FROM. They are named
8373 this way because we call this function to find out how to convert from
8374 a pointer to member of FROM to a pointer to member of TO. */
8376 static tree
8377 get_delta_difference (tree from, tree to,
8378 bool allow_inverse_p,
8379 bool c_cast_p, tsubst_flags_t complain)
8381 tree result;
8383 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8384 /* Pointer to member of incomplete class is permitted*/
8385 result = size_zero_node;
8386 else
8387 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8389 if (result == error_mark_node)
8390 return error_mark_node;
8392 if (!result)
8394 if (!allow_inverse_p)
8396 if (!(complain & tf_error))
8397 return error_mark_node;
8399 error_not_base_type (from, to);
8400 error (" in pointer to member conversion");
8401 result = size_zero_node;
8403 else
8405 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8407 if (result == error_mark_node)
8408 return error_mark_node;
8410 if (result)
8411 result = size_diffop_loc (input_location,
8412 size_zero_node, result);
8413 else
8415 if (!(complain & tf_error))
8416 return error_mark_node;
8418 error_not_base_type (from, to);
8419 error (" in pointer to member conversion");
8420 result = size_zero_node;
8425 return convert_to_integer (ptrdiff_type_node, result);
8428 /* Return a constructor for the pointer-to-member-function TYPE using
8429 the other components as specified. */
8431 tree
8432 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8434 tree u = NULL_TREE;
8435 tree delta_field;
8436 tree pfn_field;
8437 vec<constructor_elt, va_gc> *v;
8439 /* Pull the FIELD_DECLs out of the type. */
8440 pfn_field = TYPE_FIELDS (type);
8441 delta_field = DECL_CHAIN (pfn_field);
8443 /* Make sure DELTA has the type we want. */
8444 delta = convert_and_check (input_location, delta_type_node, delta);
8446 /* Convert to the correct target type if necessary. */
8447 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8449 /* Finish creating the initializer. */
8450 vec_alloc (v, 2);
8451 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8452 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8453 u = build_constructor (type, v);
8454 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8455 TREE_STATIC (u) = (TREE_CONSTANT (u)
8456 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8457 != NULL_TREE)
8458 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8459 != NULL_TREE));
8460 return u;
8463 /* Build a constructor for a pointer to member function. It can be
8464 used to initialize global variables, local variable, or used
8465 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8466 want to be.
8468 If FORCE is nonzero, then force this conversion, even if
8469 we would rather not do it. Usually set when using an explicit
8470 cast. A C-style cast is being processed iff C_CAST_P is true.
8472 Return error_mark_node, if something goes wrong. */
8474 tree
8475 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8476 tsubst_flags_t complain)
8478 tree fn;
8479 tree pfn_type;
8480 tree to_type;
8482 if (error_operand_p (pfn))
8483 return error_mark_node;
8485 pfn_type = TREE_TYPE (pfn);
8486 to_type = build_ptrmemfunc_type (type);
8488 /* Handle multiple conversions of pointer to member functions. */
8489 if (TYPE_PTRMEMFUNC_P (pfn_type))
8491 tree delta = NULL_TREE;
8492 tree npfn = NULL_TREE;
8493 tree n;
8495 if (!force
8496 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8497 LOOKUP_NORMAL, complain))
8499 if (complain & tf_error)
8500 error ("invalid conversion to type %qT from type %qT",
8501 to_type, pfn_type);
8502 else
8503 return error_mark_node;
8506 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8507 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8508 force,
8509 c_cast_p, complain);
8510 if (n == error_mark_node)
8511 return error_mark_node;
8513 /* We don't have to do any conversion to convert a
8514 pointer-to-member to its own type. But, we don't want to
8515 just return a PTRMEM_CST if there's an explicit cast; that
8516 cast should make the expression an invalid template argument. */
8517 if (TREE_CODE (pfn) != PTRMEM_CST)
8519 if (same_type_p (to_type, pfn_type))
8520 return pfn;
8521 else if (integer_zerop (n) && TREE_CODE (pfn) != CONSTRUCTOR)
8522 return build_reinterpret_cast (to_type, pfn,
8523 complain);
8526 if (TREE_SIDE_EFFECTS (pfn))
8527 pfn = save_expr (pfn);
8529 /* Obtain the function pointer and the current DELTA. */
8530 if (TREE_CODE (pfn) == PTRMEM_CST)
8531 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8532 else
8534 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8535 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8538 /* Just adjust the DELTA field. */
8539 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8540 (TREE_TYPE (delta), ptrdiff_type_node));
8541 if (!integer_zerop (n))
8543 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8544 n = cp_build_binary_op (input_location,
8545 LSHIFT_EXPR, n, integer_one_node,
8546 complain);
8547 delta = cp_build_binary_op (input_location,
8548 PLUS_EXPR, delta, n, complain);
8550 return build_ptrmemfunc1 (to_type, delta, npfn);
8553 /* Handle null pointer to member function conversions. */
8554 if (null_ptr_cst_p (pfn))
8556 pfn = cp_build_c_cast (type, pfn, complain);
8557 return build_ptrmemfunc1 (to_type,
8558 integer_zero_node,
8559 pfn);
8562 if (type_unknown_p (pfn))
8563 return instantiate_type (type, pfn, complain);
8565 fn = TREE_OPERAND (pfn, 0);
8566 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8567 /* In a template, we will have preserved the
8568 OFFSET_REF. */
8569 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8570 return make_ptrmem_cst (to_type, fn);
8573 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8574 given by CST.
8576 ??? There is no consistency as to the types returned for the above
8577 values. Some code acts as if it were a sizetype and some as if it were
8578 integer_type_node. */
8580 void
8581 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8583 tree type = TREE_TYPE (cst);
8584 tree fn = PTRMEM_CST_MEMBER (cst);
8585 tree ptr_class, fn_class;
8587 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8589 /* The class that the function belongs to. */
8590 fn_class = DECL_CONTEXT (fn);
8592 /* The class that we're creating a pointer to member of. */
8593 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8595 /* First, calculate the adjustment to the function's class. */
8596 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8597 /*c_cast_p=*/0, tf_warning_or_error);
8599 if (!DECL_VIRTUAL_P (fn))
8600 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8601 build_addr_func (fn, tf_warning_or_error));
8602 else
8604 /* If we're dealing with a virtual function, we have to adjust 'this'
8605 again, to point to the base which provides the vtable entry for
8606 fn; the call will do the opposite adjustment. */
8607 tree orig_class = DECL_CONTEXT (fn);
8608 tree binfo = binfo_or_else (orig_class, fn_class);
8609 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8610 *delta, BINFO_OFFSET (binfo));
8612 /* We set PFN to the vtable offset at which the function can be
8613 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8614 case delta is shifted left, and then incremented). */
8615 *pfn = DECL_VINDEX (fn);
8616 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8617 TYPE_SIZE_UNIT (vtable_entry_type));
8619 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8621 case ptrmemfunc_vbit_in_pfn:
8622 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8623 integer_one_node);
8624 break;
8626 case ptrmemfunc_vbit_in_delta:
8627 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8628 *delta, integer_one_node);
8629 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8630 *delta, integer_one_node);
8631 break;
8633 default:
8634 gcc_unreachable ();
8637 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8641 /* Return an expression for PFN from the pointer-to-member function
8642 given by T. */
8644 static tree
8645 pfn_from_ptrmemfunc (tree t)
8647 if (TREE_CODE (t) == PTRMEM_CST)
8649 tree delta;
8650 tree pfn;
8652 expand_ptrmemfunc_cst (t, &delta, &pfn);
8653 if (pfn)
8654 return pfn;
8657 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8660 /* Return an expression for DELTA from the pointer-to-member function
8661 given by T. */
8663 static tree
8664 delta_from_ptrmemfunc (tree t)
8666 if (TREE_CODE (t) == PTRMEM_CST)
8668 tree delta;
8669 tree pfn;
8671 expand_ptrmemfunc_cst (t, &delta, &pfn);
8672 if (delta)
8673 return delta;
8676 return build_ptrmemfunc_access_expr (t, delta_identifier);
8679 /* Convert value RHS to type TYPE as preparation for an assignment to
8680 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8681 implicit conversion is. If FNDECL is non-NULL, we are doing the
8682 conversion in order to pass the PARMNUMth argument of FNDECL.
8683 If FNDECL is NULL, we are doing the conversion in function pointer
8684 argument passing, conversion in initialization, etc. */
8686 static tree
8687 convert_for_assignment (tree type, tree rhs,
8688 impl_conv_rhs errtype, tree fndecl, int parmnum,
8689 tsubst_flags_t complain, int flags)
8691 tree rhstype;
8692 enum tree_code coder;
8694 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8695 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8696 rhs = TREE_OPERAND (rhs, 0);
8698 /* Handle [dcl.init.list] direct-list-initialization from
8699 single element of enumeration with a fixed underlying type. */
8700 if (is_direct_enum_init (type, rhs))
8702 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8703 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8705 warning_sentinel w (warn_useless_cast);
8706 warning_sentinel w2 (warn_ignored_qualifiers);
8707 rhs = cp_build_c_cast (type, elt, complain);
8709 else
8710 rhs = error_mark_node;
8713 rhstype = TREE_TYPE (rhs);
8714 coder = TREE_CODE (rhstype);
8716 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8717 && vector_types_convertible_p (type, rhstype, true))
8719 rhs = mark_rvalue_use (rhs);
8720 return convert (type, rhs);
8723 if (rhs == error_mark_node || rhstype == error_mark_node)
8724 return error_mark_node;
8725 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8726 return error_mark_node;
8728 /* The RHS of an assignment cannot have void type. */
8729 if (coder == VOID_TYPE)
8731 if (complain & tf_error)
8732 error ("void value not ignored as it ought to be");
8733 return error_mark_node;
8736 if (c_dialect_objc ())
8738 int parmno;
8739 tree selector;
8740 tree rname = fndecl;
8742 switch (errtype)
8744 case ICR_ASSIGN:
8745 parmno = -1;
8746 break;
8747 case ICR_INIT:
8748 parmno = -2;
8749 break;
8750 default:
8751 selector = objc_message_selector ();
8752 parmno = parmnum;
8753 if (selector && parmno > 1)
8755 rname = selector;
8756 parmno -= 1;
8760 if (objc_compare_types (type, rhstype, parmno, rname))
8762 rhs = mark_rvalue_use (rhs);
8763 return convert (type, rhs);
8767 /* [expr.ass]
8769 The expression is implicitly converted (clause _conv_) to the
8770 cv-unqualified type of the left operand.
8772 We allow bad conversions here because by the time we get to this point
8773 we are committed to doing the conversion. If we end up doing a bad
8774 conversion, convert_like will complain. */
8775 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8777 /* When -Wno-pmf-conversions is use, we just silently allow
8778 conversions from pointers-to-members to plain pointers. If
8779 the conversion doesn't work, cp_convert will complain. */
8780 if (!warn_pmf2ptr
8781 && TYPE_PTR_P (type)
8782 && TYPE_PTRMEMFUNC_P (rhstype))
8783 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8784 else
8786 if (complain & tf_error)
8788 /* If the right-hand side has unknown type, then it is an
8789 overloaded function. Call instantiate_type to get error
8790 messages. */
8791 if (rhstype == unknown_type_node)
8793 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8794 /* -fpermissive might allow this; recurse. */
8795 if (!seen_error ())
8796 return convert_for_assignment (type, r, errtype, fndecl,
8797 parmnum, complain, flags);
8799 else if (fndecl)
8801 error_at (EXPR_LOC_OR_LOC (rhs, input_location),
8802 "cannot convert %qH to %qI",
8803 rhstype, type);
8804 inform (get_fndecl_argument_location (fndecl, parmnum),
8805 " initializing argument %P of %qD", parmnum, fndecl);
8807 else
8808 switch (errtype)
8810 case ICR_DEFAULT_ARGUMENT:
8811 error ("cannot convert %qH to %qI in default argument",
8812 rhstype, type);
8813 break;
8814 case ICR_ARGPASS:
8815 error ("cannot convert %qH to %qI in argument passing",
8816 rhstype, type);
8817 break;
8818 case ICR_CONVERTING:
8819 error ("cannot convert %qH to %qI",
8820 rhstype, type);
8821 break;
8822 case ICR_INIT:
8823 error ("cannot convert %qH to %qI in initialization",
8824 rhstype, type);
8825 break;
8826 case ICR_RETURN:
8827 error ("cannot convert %qH to %qI in return",
8828 rhstype, type);
8829 break;
8830 case ICR_ASSIGN:
8831 error ("cannot convert %qH to %qI in assignment",
8832 rhstype, type);
8833 break;
8834 default:
8835 gcc_unreachable();
8837 if (TYPE_PTR_P (rhstype)
8838 && TYPE_PTR_P (type)
8839 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8840 && CLASS_TYPE_P (TREE_TYPE (type))
8841 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8842 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8843 (TREE_TYPE (rhstype))),
8844 "class type %qT is incomplete", TREE_TYPE (rhstype));
8846 return error_mark_node;
8849 if (warn_suggest_attribute_format)
8851 const enum tree_code codel = TREE_CODE (type);
8852 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8853 && coder == codel
8854 && check_missing_format_attribute (type, rhstype)
8855 && (complain & tf_warning))
8856 switch (errtype)
8858 case ICR_ARGPASS:
8859 case ICR_DEFAULT_ARGUMENT:
8860 if (fndecl)
8861 warning (OPT_Wsuggest_attribute_format,
8862 "parameter %qP of %qD might be a candidate "
8863 "for a format attribute", parmnum, fndecl);
8864 else
8865 warning (OPT_Wsuggest_attribute_format,
8866 "parameter might be a candidate "
8867 "for a format attribute");
8868 break;
8869 case ICR_CONVERTING:
8870 warning (OPT_Wsuggest_attribute_format,
8871 "target of conversion might be a candidate "
8872 "for a format attribute");
8873 break;
8874 case ICR_INIT:
8875 warning (OPT_Wsuggest_attribute_format,
8876 "target of initialization might be a candidate "
8877 "for a format attribute");
8878 break;
8879 case ICR_RETURN:
8880 warning (OPT_Wsuggest_attribute_format,
8881 "return type might be a candidate "
8882 "for a format attribute");
8883 break;
8884 case ICR_ASSIGN:
8885 warning (OPT_Wsuggest_attribute_format,
8886 "left-hand side of assignment might be a candidate "
8887 "for a format attribute");
8888 break;
8889 default:
8890 gcc_unreachable();
8894 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8895 does not. */
8896 if (warn_parentheses
8897 && TREE_CODE (type) == BOOLEAN_TYPE
8898 && TREE_CODE (rhs) == MODIFY_EXPR
8899 && !TREE_NO_WARNING (rhs)
8900 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8901 && (complain & tf_warning))
8903 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8905 warning_at (loc, OPT_Wparentheses,
8906 "suggest parentheses around assignment used as truth value");
8907 TREE_NO_WARNING (rhs) = 1;
8910 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8911 complain, flags);
8914 /* Convert RHS to be of type TYPE.
8915 If EXP is nonzero, it is the target of the initialization.
8916 ERRTYPE indicates what kind of error the implicit conversion is.
8918 Two major differences between the behavior of
8919 `convert_for_assignment' and `convert_for_initialization'
8920 are that references are bashed in the former, while
8921 copied in the latter, and aggregates are assigned in
8922 the former (operator=) while initialized in the
8923 latter (X(X&)).
8925 If using constructor make sure no conversion operator exists, if one does
8926 exist, an ambiguity exists. */
8928 tree
8929 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8930 impl_conv_rhs errtype, tree fndecl, int parmnum,
8931 tsubst_flags_t complain)
8933 enum tree_code codel = TREE_CODE (type);
8934 tree rhstype;
8935 enum tree_code coder;
8937 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8938 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8939 if (TREE_CODE (rhs) == NOP_EXPR
8940 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8941 && codel != REFERENCE_TYPE)
8942 rhs = TREE_OPERAND (rhs, 0);
8944 if (type == error_mark_node
8945 || rhs == error_mark_node
8946 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8947 return error_mark_node;
8949 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8951 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8952 && TREE_CODE (type) != ARRAY_TYPE
8953 && (!TYPE_REF_P (type)
8954 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8955 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8956 && !TYPE_REFFN_P (type))
8957 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8958 rhs = decay_conversion (rhs, complain);
8960 rhstype = TREE_TYPE (rhs);
8961 coder = TREE_CODE (rhstype);
8963 if (coder == ERROR_MARK)
8964 return error_mark_node;
8966 /* We accept references to incomplete types, so we can
8967 return here before checking if RHS is of complete type. */
8969 if (codel == REFERENCE_TYPE)
8971 /* This should eventually happen in convert_arguments. */
8972 int savew = 0, savee = 0;
8974 if (fndecl)
8975 savew = warningcount + werrorcount, savee = errorcount;
8976 rhs = initialize_reference (type, rhs, flags, complain);
8978 if (fndecl
8979 && (warningcount + werrorcount > savew || errorcount > savee))
8980 inform (DECL_SOURCE_LOCATION (fndecl),
8981 "in passing argument %P of %qD", parmnum, fndecl);
8983 return rhs;
8986 if (exp != 0)
8987 exp = require_complete_type_sfinae (exp, complain);
8988 if (exp == error_mark_node)
8989 return error_mark_node;
8991 rhstype = non_reference (rhstype);
8993 type = complete_type (type);
8995 if (DIRECT_INIT_EXPR_P (type, rhs))
8996 /* Don't try to do copy-initialization if we already have
8997 direct-initialization. */
8998 return rhs;
9000 if (MAYBE_CLASS_TYPE_P (type))
9001 return perform_implicit_conversion_flags (type, rhs, complain, flags);
9003 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
9004 complain, flags);
9007 /* If RETVAL is the address of, or a reference to, a local variable or
9008 temporary give an appropriate warning and return true. */
9010 static bool
9011 maybe_warn_about_returning_address_of_local (tree retval)
9013 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
9014 tree whats_returned = fold_for_warn (retval);
9016 for (;;)
9018 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
9019 whats_returned = TREE_OPERAND (whats_returned, 1);
9020 else if (CONVERT_EXPR_P (whats_returned)
9021 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
9022 whats_returned = TREE_OPERAND (whats_returned, 0);
9023 else
9024 break;
9027 if (TREE_CODE (whats_returned) != ADDR_EXPR)
9028 return false;
9029 whats_returned = TREE_OPERAND (whats_returned, 0);
9031 while (TREE_CODE (whats_returned) == COMPONENT_REF
9032 || TREE_CODE (whats_returned) == ARRAY_REF)
9033 whats_returned = TREE_OPERAND (whats_returned, 0);
9035 if (TYPE_REF_P (valtype))
9037 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
9038 || TREE_CODE (whats_returned) == TARGET_EXPR)
9040 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
9041 return true;
9043 if (VAR_P (whats_returned)
9044 && DECL_NAME (whats_returned)
9045 && TEMP_NAME_P (DECL_NAME (whats_returned)))
9047 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
9048 return true;
9052 if (DECL_P (whats_returned)
9053 && DECL_NAME (whats_returned)
9054 && DECL_FUNCTION_SCOPE_P (whats_returned)
9055 && !is_capture_proxy (whats_returned)
9056 && !(TREE_STATIC (whats_returned)
9057 || TREE_PUBLIC (whats_returned)))
9059 if (TYPE_REF_P (valtype))
9060 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9061 OPT_Wreturn_local_addr,
9062 "reference to local variable %qD returned",
9063 whats_returned);
9064 else if (TREE_CODE (whats_returned) == LABEL_DECL)
9065 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9066 OPT_Wreturn_local_addr, "address of label %qD returned",
9067 whats_returned);
9068 else
9069 warning_at (DECL_SOURCE_LOCATION (whats_returned),
9070 OPT_Wreturn_local_addr, "address of local variable %qD "
9071 "returned", whats_returned);
9072 return true;
9075 return false;
9078 /* Check that returning RETVAL from the current function is valid.
9079 Return an expression explicitly showing all conversions required to
9080 change RETVAL into the function return type, and to assign it to
9081 the DECL_RESULT for the function. Set *NO_WARNING to true if
9082 code reaches end of non-void function warning shouldn't be issued
9083 on this RETURN_EXPR. */
9085 tree
9086 check_return_expr (tree retval, bool *no_warning)
9088 tree result;
9089 /* The type actually returned by the function. */
9090 tree valtype;
9091 /* The type the function is declared to return, or void if
9092 the declared type is incomplete. */
9093 tree functype;
9094 int fn_returns_value_p;
9095 bool named_return_value_okay_p;
9097 *no_warning = false;
9099 /* A `volatile' function is one that isn't supposed to return, ever.
9100 (This is a G++ extension, used to get better code for functions
9101 that call the `volatile' function.) */
9102 if (TREE_THIS_VOLATILE (current_function_decl))
9103 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9105 /* Check for various simple errors. */
9106 if (DECL_DESTRUCTOR_P (current_function_decl))
9108 if (retval)
9109 error ("returning a value from a destructor");
9110 return NULL_TREE;
9112 else if (DECL_CONSTRUCTOR_P (current_function_decl))
9114 if (in_function_try_handler)
9115 /* If a return statement appears in a handler of the
9116 function-try-block of a constructor, the program is ill-formed. */
9117 error ("cannot return from a handler of a function-try-block of a constructor");
9118 else if (retval)
9119 /* You can't return a value from a constructor. */
9120 error ("returning a value from a constructor");
9121 return NULL_TREE;
9124 const tree saved_retval = retval;
9126 if (processing_template_decl)
9128 current_function_returns_value = 1;
9130 if (check_for_bare_parameter_packs (retval))
9131 return error_mark_node;
9133 /* If one of the types might be void, we can't tell whether we're
9134 returning a value. */
9135 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
9136 && !current_function_auto_return_pattern)
9137 || (retval != NULL_TREE
9138 && (TREE_TYPE (retval) == NULL_TREE
9139 || WILDCARD_TYPE_P (TREE_TYPE (retval)))))
9140 goto dependent;
9143 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
9145 /* Deduce auto return type from a return statement. */
9146 if (current_function_auto_return_pattern)
9148 tree auto_node;
9149 tree type;
9151 if (!retval && !is_auto (current_function_auto_return_pattern))
9153 /* Give a helpful error message. */
9154 error ("return-statement with no value, in function returning %qT",
9155 current_function_auto_return_pattern);
9156 inform (input_location, "only plain %<auto%> return type can be "
9157 "deduced to %<void%>");
9158 type = error_mark_node;
9160 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
9162 error ("returning initializer list");
9163 type = error_mark_node;
9165 else
9167 if (!retval)
9168 retval = void_node;
9169 auto_node = type_uses_auto (current_function_auto_return_pattern);
9170 type = do_auto_deduction (current_function_auto_return_pattern,
9171 retval, auto_node);
9174 if (type == error_mark_node)
9175 /* Leave it. */;
9176 else if (functype == current_function_auto_return_pattern)
9177 apply_deduced_return_type (current_function_decl, type);
9178 else if (!same_type_p (type, functype))
9180 if (LAMBDA_FUNCTION_P (current_function_decl))
9181 error ("inconsistent types %qT and %qT deduced for "
9182 "lambda return type", functype, type);
9183 else
9184 error ("inconsistent deduction for auto return type: "
9185 "%qT and then %qT", functype, type);
9187 functype = type;
9190 result = DECL_RESULT (current_function_decl);
9191 valtype = TREE_TYPE (result);
9192 gcc_assert (valtype != NULL_TREE);
9193 fn_returns_value_p = !VOID_TYPE_P (valtype);
9195 /* Check for a return statement with no return value in a function
9196 that's supposed to return a value. */
9197 if (!retval && fn_returns_value_p)
9199 if (functype != error_mark_node)
9200 permerror (input_location, "return-statement with no value, in "
9201 "function returning %qT", valtype);
9202 /* Remember that this function did return. */
9203 current_function_returns_value = 1;
9204 /* And signal caller that TREE_NO_WARNING should be set on the
9205 RETURN_EXPR to avoid control reaches end of non-void function
9206 warnings in tree-cfg.c. */
9207 *no_warning = true;
9209 /* Check for a return statement with a value in a function that
9210 isn't supposed to return a value. */
9211 else if (retval && !fn_returns_value_p)
9213 if (VOID_TYPE_P (TREE_TYPE (retval)))
9214 /* You can return a `void' value from a function of `void'
9215 type. In that case, we have to evaluate the expression for
9216 its side-effects. */
9217 finish_expr_stmt (retval);
9218 else
9219 permerror (input_location,
9220 "return-statement with a value, in function "
9221 "returning %qT", valtype);
9222 current_function_returns_null = 1;
9224 /* There's really no value to return, after all. */
9225 return NULL_TREE;
9227 else if (!retval)
9228 /* Remember that this function can sometimes return without a
9229 value. */
9230 current_function_returns_null = 1;
9231 else
9232 /* Remember that this function did return a value. */
9233 current_function_returns_value = 1;
9235 /* Check for erroneous operands -- but after giving ourselves a
9236 chance to provide an error about returning a value from a void
9237 function. */
9238 if (error_operand_p (retval))
9240 current_function_return_value = error_mark_node;
9241 return error_mark_node;
9244 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9245 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
9246 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9247 && ! flag_check_new
9248 && retval && null_ptr_cst_p (retval))
9249 warning (0, "%<operator new%> must not return NULL unless it is "
9250 "declared %<throw()%> (or -fcheck-new is in effect)");
9252 /* Effective C++ rule 15. See also start_function. */
9253 if (warn_ecpp
9254 && DECL_NAME (current_function_decl) == assign_op_identifier
9255 && !type_dependent_expression_p (retval))
9257 bool warn = true;
9259 /* The function return type must be a reference to the current
9260 class. */
9261 if (TYPE_REF_P (valtype)
9262 && same_type_ignoring_top_level_qualifiers_p
9263 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9265 /* Returning '*this' is obviously OK. */
9266 if (retval == current_class_ref)
9267 warn = false;
9268 /* If we are calling a function whose return type is the same of
9269 the current class reference, it is ok. */
9270 else if (INDIRECT_REF_P (retval)
9271 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9272 warn = false;
9275 if (warn)
9276 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9279 if (dependent_type_p (functype)
9280 || type_dependent_expression_p (retval))
9282 dependent:
9283 /* We should not have changed the return value. */
9284 gcc_assert (retval == saved_retval);
9285 return retval;
9288 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9290 [...] For a function with a class return type, if the expression
9291 in the return statement is the name of a local object, and the cv-
9292 unqualified type of the local object is the same as the function
9293 return type, an implementation is permitted to omit creating the tem-
9294 porary object to hold the function return value [...]
9296 So, if this is a value-returning function that always returns the same
9297 local variable, remember it.
9299 It might be nice to be more flexible, and choose the first suitable
9300 variable even if the function sometimes returns something else, but
9301 then we run the risk of clobbering the variable we chose if the other
9302 returned expression uses the chosen variable somehow. And people expect
9303 this restriction, anyway. (jason 2000-11-19)
9305 See finish_function and finalize_nrv for the rest of this optimization. */
9307 named_return_value_okay_p =
9308 (retval != NULL_TREE
9309 && !processing_template_decl
9310 /* Must be a local, automatic variable. */
9311 && VAR_P (retval)
9312 && DECL_CONTEXT (retval) == current_function_decl
9313 && ! TREE_STATIC (retval)
9314 /* And not a lambda or anonymous union proxy. */
9315 && !DECL_HAS_VALUE_EXPR_P (retval)
9316 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9317 /* The cv-unqualified type of the returned value must be the
9318 same as the cv-unqualified return type of the
9319 function. */
9320 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9321 (TYPE_MAIN_VARIANT (functype)))
9322 /* And the returned value must be non-volatile. */
9323 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9325 if (fn_returns_value_p && flag_elide_constructors)
9327 if (named_return_value_okay_p
9328 && (current_function_return_value == NULL_TREE
9329 || current_function_return_value == retval))
9330 current_function_return_value = retval;
9331 else
9332 current_function_return_value = error_mark_node;
9335 /* We don't need to do any conversions when there's nothing being
9336 returned. */
9337 if (!retval)
9338 return NULL_TREE;
9340 /* Do any required conversions. */
9341 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9342 /* No conversions are required. */
9344 else
9346 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9348 /* The functype's return type will have been set to void, if it
9349 was an incomplete type. Just treat this as 'return;' */
9350 if (VOID_TYPE_P (functype))
9351 return error_mark_node;
9353 /* If we had an id-expression obfuscated by force_paren_expr, we need
9354 to undo it so we can try to treat it as an rvalue below. */
9355 retval = maybe_undo_parenthesized_ref (retval);
9357 if (processing_template_decl)
9358 retval = build_non_dependent_expr (retval);
9360 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9361 treated as an rvalue for the purposes of overload resolution to
9362 favor move constructors over copy constructors.
9364 Note that these conditions are similar to, but not as strict as,
9365 the conditions for the named return value optimization. */
9366 bool converted = false;
9367 if ((cxx_dialect != cxx98)
9368 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9369 || TREE_CODE (retval) == PARM_DECL)
9370 && DECL_CONTEXT (retval) == current_function_decl
9371 && !TREE_STATIC (retval)
9372 /* This is only interesting for class type. */
9373 && CLASS_TYPE_P (functype))
9375 tree moved = move (retval);
9376 moved = convert_for_initialization
9377 (NULL_TREE, functype, moved, flags|LOOKUP_PREFER_RVALUE,
9378 ICR_RETURN, NULL_TREE, 0, tf_none);
9379 if (moved != error_mark_node)
9381 retval = moved;
9382 converted = true;
9386 /* First convert the value to the function's return type, then
9387 to the type of return value's location to handle the
9388 case that functype is smaller than the valtype. */
9389 if (!converted)
9390 retval = convert_for_initialization
9391 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9392 tf_warning_or_error);
9393 retval = convert (valtype, retval);
9395 /* If the conversion failed, treat this just like `return;'. */
9396 if (retval == error_mark_node)
9397 return retval;
9398 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9399 else if (! cfun->returns_struct
9400 && TREE_CODE (retval) == TARGET_EXPR
9401 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9402 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9403 TREE_OPERAND (retval, 0));
9404 else if (!processing_template_decl
9405 && maybe_warn_about_returning_address_of_local (retval))
9406 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9407 build_zero_cst (TREE_TYPE (retval)));
9410 if (processing_template_decl)
9411 return saved_retval;
9413 /* Actually copy the value returned into the appropriate location. */
9414 if (retval && retval != result)
9415 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9417 return retval;
9421 /* Returns nonzero if the pointer-type FROM can be converted to the
9422 pointer-type TO via a qualification conversion. If CONSTP is -1,
9423 then we return nonzero if the pointers are similar, and the
9424 cv-qualification signature of FROM is a proper subset of that of TO.
9426 If CONSTP is positive, then all outer pointers have been
9427 const-qualified. */
9429 static int
9430 comp_ptr_ttypes_real (tree to, tree from, int constp)
9432 bool to_more_cv_qualified = false;
9433 bool is_opaque_pointer = false;
9435 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9437 if (TREE_CODE (to) != TREE_CODE (from))
9438 return 0;
9440 if (TREE_CODE (from) == OFFSET_TYPE
9441 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9442 TYPE_OFFSET_BASETYPE (to)))
9443 return 0;
9445 /* Const and volatile mean something different for function types,
9446 so the usual checks are not appropriate. */
9447 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9449 if (!at_least_as_qualified_p (to, from))
9450 return 0;
9452 if (!at_least_as_qualified_p (from, to))
9454 if (constp == 0)
9455 return 0;
9456 to_more_cv_qualified = true;
9459 if (constp > 0)
9460 constp &= TYPE_READONLY (to);
9463 if (VECTOR_TYPE_P (to))
9464 is_opaque_pointer = vector_targets_convertible_p (to, from);
9466 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9467 return ((constp >= 0 || to_more_cv_qualified)
9468 && (is_opaque_pointer
9469 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9473 /* When comparing, say, char ** to char const **, this function takes
9474 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9475 types to this function. */
9478 comp_ptr_ttypes (tree to, tree from)
9480 return comp_ptr_ttypes_real (to, from, 1);
9483 /* Returns true iff FNTYPE is a non-class type that involves
9484 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9485 if a parameter type is ill-formed. */
9487 bool
9488 error_type_p (const_tree type)
9490 tree t;
9492 switch (TREE_CODE (type))
9494 case ERROR_MARK:
9495 return true;
9497 case POINTER_TYPE:
9498 case REFERENCE_TYPE:
9499 case OFFSET_TYPE:
9500 return error_type_p (TREE_TYPE (type));
9502 case FUNCTION_TYPE:
9503 case METHOD_TYPE:
9504 if (error_type_p (TREE_TYPE (type)))
9505 return true;
9506 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9507 if (error_type_p (TREE_VALUE (t)))
9508 return true;
9509 return false;
9511 case RECORD_TYPE:
9512 if (TYPE_PTRMEMFUNC_P (type))
9513 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9514 return false;
9516 default:
9517 return false;
9521 /* Returns true if to and from are (possibly multi-level) pointers to the same
9522 type or inheritance-related types, regardless of cv-quals. */
9524 bool
9525 ptr_reasonably_similar (const_tree to, const_tree from)
9527 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9529 /* Any target type is similar enough to void. */
9530 if (VOID_TYPE_P (to))
9531 return !error_type_p (from);
9532 if (VOID_TYPE_P (from))
9533 return !error_type_p (to);
9535 if (TREE_CODE (to) != TREE_CODE (from))
9536 return false;
9538 if (TREE_CODE (from) == OFFSET_TYPE
9539 && comptypes (TYPE_OFFSET_BASETYPE (to),
9540 TYPE_OFFSET_BASETYPE (from),
9541 COMPARE_BASE | COMPARE_DERIVED))
9542 continue;
9544 if (VECTOR_TYPE_P (to)
9545 && vector_types_convertible_p (to, from, false))
9546 return true;
9548 if (TREE_CODE (to) == INTEGER_TYPE
9549 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9550 return true;
9552 if (TREE_CODE (to) == FUNCTION_TYPE)
9553 return !error_type_p (to) && !error_type_p (from);
9555 if (!TYPE_PTR_P (to))
9557 /* When either type is incomplete avoid DERIVED_FROM_P,
9558 which may call complete_type (c++/57942). */
9559 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9560 return comptypes
9561 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9562 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9567 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9568 pointer-to-member types) are the same, ignoring cv-qualification at
9569 all levels. */
9571 bool
9572 comp_ptr_ttypes_const (tree to, tree from)
9574 bool is_opaque_pointer = false;
9576 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9578 if (TREE_CODE (to) != TREE_CODE (from))
9579 return false;
9581 if (TREE_CODE (from) == OFFSET_TYPE
9582 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9583 TYPE_OFFSET_BASETYPE (to)))
9584 continue;
9586 if (VECTOR_TYPE_P (to))
9587 is_opaque_pointer = vector_targets_convertible_p (to, from);
9589 if (!TYPE_PTR_P (to))
9590 return (is_opaque_pointer
9591 || same_type_ignoring_top_level_qualifiers_p (to, from));
9595 /* Returns the type qualifiers for this type, including the qualifiers on the
9596 elements for an array type. */
9599 cp_type_quals (const_tree type)
9601 int quals;
9602 /* This CONST_CAST is okay because strip_array_types returns its
9603 argument unmodified and we assign it to a const_tree. */
9604 type = strip_array_types (CONST_CAST_TREE (type));
9605 if (type == error_mark_node
9606 /* Quals on a FUNCTION_TYPE are memfn quals. */
9607 || TREE_CODE (type) == FUNCTION_TYPE)
9608 return TYPE_UNQUALIFIED;
9609 quals = TYPE_QUALS (type);
9610 /* METHOD and REFERENCE_TYPEs should never have quals. */
9611 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9612 && !TYPE_REF_P (type))
9613 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9614 == TYPE_UNQUALIFIED));
9615 return quals;
9618 /* Returns the function-ref-qualifier for TYPE */
9620 cp_ref_qualifier
9621 type_memfn_rqual (const_tree type)
9623 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9624 || TREE_CODE (type) == METHOD_TYPE);
9626 if (!FUNCTION_REF_QUALIFIED (type))
9627 return REF_QUAL_NONE;
9628 else if (FUNCTION_RVALUE_QUALIFIED (type))
9629 return REF_QUAL_RVALUE;
9630 else
9631 return REF_QUAL_LVALUE;
9634 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9635 METHOD_TYPE. */
9638 type_memfn_quals (const_tree type)
9640 if (TREE_CODE (type) == FUNCTION_TYPE)
9641 return TYPE_QUALS (type);
9642 else if (TREE_CODE (type) == METHOD_TYPE)
9643 return cp_type_quals (class_of_this_parm (type));
9644 else
9645 gcc_unreachable ();
9648 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9649 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9651 tree
9652 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9654 /* Could handle METHOD_TYPE here if necessary. */
9655 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9656 if (TYPE_QUALS (type) == memfn_quals
9657 && type_memfn_rqual (type) == rqual)
9658 return type;
9660 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9661 complex. */
9662 tree result = build_qualified_type (type, memfn_quals);
9663 return build_ref_qualified_type (result, rqual);
9666 /* Returns nonzero if TYPE is const or volatile. */
9668 bool
9669 cv_qualified_p (const_tree type)
9671 int quals = cp_type_quals (type);
9672 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9675 /* Returns nonzero if the TYPE contains a mutable member. */
9677 bool
9678 cp_has_mutable_p (const_tree type)
9680 /* This CONST_CAST is okay because strip_array_types returns its
9681 argument unmodified and we assign it to a const_tree. */
9682 type = strip_array_types (CONST_CAST_TREE(type));
9684 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9687 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9688 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9689 approximation. In particular, consider:
9691 int f();
9692 struct S { int i; };
9693 const S s = { f(); }
9695 Here, we will make "s" as TREE_READONLY (because it is declared
9696 "const") -- only to reverse ourselves upon seeing that the
9697 initializer is non-constant. */
9699 void
9700 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9702 tree type = TREE_TYPE (decl);
9704 if (type == error_mark_node)
9705 return;
9707 if (TREE_CODE (decl) == TYPE_DECL)
9708 return;
9710 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9711 && type_quals != TYPE_UNQUALIFIED));
9713 /* Avoid setting TREE_READONLY incorrectly. */
9714 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9715 constructor can produce constant init, so rely on cp_finish_decl to
9716 clear TREE_READONLY if the variable has non-constant init. */
9718 /* If the type has (or might have) a mutable component, that component
9719 might be modified. */
9720 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9721 type_quals &= ~TYPE_QUAL_CONST;
9723 c_apply_type_quals_to_decl (type_quals, decl);
9726 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9727 exemplar types such that casting T1 to T2 is casting away constness
9728 if and only if there is no implicit conversion from T1 to T2. */
9730 static void
9731 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9733 int quals1;
9734 int quals2;
9736 /* [expr.const.cast]
9738 For multi-level pointer to members and multi-level mixed pointers
9739 and pointers to members (conv.qual), the "member" aspect of a
9740 pointer to member level is ignored when determining if a const
9741 cv-qualifier has been cast away. */
9742 /* [expr.const.cast]
9744 For two pointer types:
9746 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9747 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9748 K is min(N,M)
9750 casting from X1 to X2 casts away constness if, for a non-pointer
9751 type T there does not exist an implicit conversion (clause
9752 _conv_) from:
9754 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9758 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9759 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9760 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9762 *t1 = cp_build_qualified_type (void_type_node,
9763 cp_type_quals (*t1));
9764 *t2 = cp_build_qualified_type (void_type_node,
9765 cp_type_quals (*t2));
9766 return;
9769 quals1 = cp_type_quals (*t1);
9770 quals2 = cp_type_quals (*t2);
9772 if (TYPE_PTRDATAMEM_P (*t1))
9773 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9774 else
9775 *t1 = TREE_TYPE (*t1);
9776 if (TYPE_PTRDATAMEM_P (*t2))
9777 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9778 else
9779 *t2 = TREE_TYPE (*t2);
9781 casts_away_constness_r (t1, t2, complain);
9782 *t1 = build_pointer_type (*t1);
9783 *t2 = build_pointer_type (*t2);
9784 *t1 = cp_build_qualified_type (*t1, quals1);
9785 *t2 = cp_build_qualified_type (*t2, quals2);
9788 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9789 constness.
9791 ??? This function returns non-zero if casting away qualifiers not
9792 just const. We would like to return to the caller exactly which
9793 qualifiers are casted away to give more accurate diagnostics.
9796 static bool
9797 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9799 if (TYPE_REF_P (t2))
9801 /* [expr.const.cast]
9803 Casting from an lvalue of type T1 to an lvalue of type T2
9804 using a reference cast casts away constness if a cast from an
9805 rvalue of type "pointer to T1" to the type "pointer to T2"
9806 casts away constness. */
9807 t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
9808 return casts_away_constness (build_pointer_type (t1),
9809 build_pointer_type (TREE_TYPE (t2)),
9810 complain);
9813 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9814 /* [expr.const.cast]
9816 Casting from an rvalue of type "pointer to data member of X
9817 of type T1" to the type "pointer to data member of Y of type
9818 T2" casts away constness if a cast from an rvalue of type
9819 "pointer to T1" to the type "pointer to T2" casts away
9820 constness. */
9821 return casts_away_constness
9822 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9823 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9824 complain);
9826 /* Casting away constness is only something that makes sense for
9827 pointer or reference types. */
9828 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9829 return false;
9831 /* Top-level qualifiers don't matter. */
9832 t1 = TYPE_MAIN_VARIANT (t1);
9833 t2 = TYPE_MAIN_VARIANT (t2);
9834 casts_away_constness_r (&t1, &t2, complain);
9835 if (!can_convert (t2, t1, complain))
9836 return true;
9838 return false;
9841 /* If T is a REFERENCE_TYPE return the type to which T refers.
9842 Otherwise, return T itself. */
9844 tree
9845 non_reference (tree t)
9847 if (t && TYPE_REF_P (t))
9848 t = TREE_TYPE (t);
9849 return t;
9853 /* Return nonzero if REF is an lvalue valid for this language;
9854 otherwise, print an error message and return zero. USE says
9855 how the lvalue is being used and so selects the error message. */
9858 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9860 cp_lvalue_kind kind = lvalue_kind (ref);
9862 if (kind == clk_none)
9864 if (complain & tf_error)
9865 lvalue_error (input_location, use);
9866 return 0;
9868 else if (kind & (clk_rvalueref|clk_class))
9870 if (!(complain & tf_error))
9871 return 0;
9872 /* Make this a permerror because we used to accept it. */
9873 permerror (input_location, "using rvalue as lvalue");
9875 return 1;
9878 /* Return true if a user-defined literal operator is a raw operator. */
9880 bool
9881 check_raw_literal_operator (const_tree decl)
9883 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9884 tree argtype;
9885 int arity;
9886 bool maybe_raw_p = false;
9888 /* Count the number and type of arguments and check for ellipsis. */
9889 for (argtype = argtypes, arity = 0;
9890 argtype && argtype != void_list_node;
9891 ++arity, argtype = TREE_CHAIN (argtype))
9893 tree t = TREE_VALUE (argtype);
9895 if (same_type_p (t, const_string_type_node))
9896 maybe_raw_p = true;
9898 if (!argtype)
9899 return false; /* Found ellipsis. */
9901 if (!maybe_raw_p || arity != 1)
9902 return false;
9904 return true;
9908 /* Return true if a user-defined literal operator has one of the allowed
9909 argument types. */
9911 bool
9912 check_literal_operator_args (const_tree decl,
9913 bool *long_long_unsigned_p, bool *long_double_p)
9915 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9917 *long_long_unsigned_p = false;
9918 *long_double_p = false;
9919 if (processing_template_decl || processing_specialization)
9920 return argtypes == void_list_node;
9921 else
9923 tree argtype;
9924 int arity;
9925 int max_arity = 2;
9927 /* Count the number and type of arguments and check for ellipsis. */
9928 for (argtype = argtypes, arity = 0;
9929 argtype && argtype != void_list_node;
9930 argtype = TREE_CHAIN (argtype))
9932 tree t = TREE_VALUE (argtype);
9933 ++arity;
9935 if (TYPE_PTR_P (t))
9937 bool maybe_raw_p = false;
9938 t = TREE_TYPE (t);
9939 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9940 return false;
9941 t = TYPE_MAIN_VARIANT (t);
9942 if ((maybe_raw_p = same_type_p (t, char_type_node))
9943 || same_type_p (t, wchar_type_node)
9944 || same_type_p (t, char16_type_node)
9945 || same_type_p (t, char32_type_node))
9947 argtype = TREE_CHAIN (argtype);
9948 if (!argtype)
9949 return false;
9950 t = TREE_VALUE (argtype);
9951 if (maybe_raw_p && argtype == void_list_node)
9952 return true;
9953 else if (same_type_p (t, size_type_node))
9955 ++arity;
9956 continue;
9958 else
9959 return false;
9962 else if (same_type_p (t, long_long_unsigned_type_node))
9964 max_arity = 1;
9965 *long_long_unsigned_p = true;
9967 else if (same_type_p (t, long_double_type_node))
9969 max_arity = 1;
9970 *long_double_p = true;
9972 else if (same_type_p (t, char_type_node))
9973 max_arity = 1;
9974 else if (same_type_p (t, wchar_type_node))
9975 max_arity = 1;
9976 else if (same_type_p (t, char16_type_node))
9977 max_arity = 1;
9978 else if (same_type_p (t, char32_type_node))
9979 max_arity = 1;
9980 else
9981 return false;
9983 if (!argtype)
9984 return false; /* Found ellipsis. */
9986 if (arity != max_arity)
9987 return false;
9989 return true;
9993 /* Always returns false since unlike C90, C++ has no concept of implicit
9994 function declarations. */
9996 bool
9997 c_decl_implicit (const_tree)
9999 return false;