/cp
[official-gcc.git] / gcc / cp / typeck.c
bloba58de1d6206e8ca1afd32db584bb9480d9433a0f
1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2017 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 "asan.h"
42 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
43 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
44 static tree pfn_from_ptrmemfunc (tree);
45 static tree delta_from_ptrmemfunc (tree);
46 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
47 tsubst_flags_t, int);
48 static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
49 tsubst_flags_t);
50 static tree rationalize_conditional_expr (enum tree_code, tree,
51 tsubst_flags_t);
52 static int comp_ptr_ttypes_real (tree, tree, int);
53 static bool comp_except_types (tree, tree, bool);
54 static bool comp_array_types (const_tree, const_tree, bool);
55 static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t);
56 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
57 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
58 static bool casts_away_constness (tree, tree, tsubst_flags_t);
59 static bool maybe_warn_about_returning_address_of_local (tree);
60 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
61 static void error_args_num (location_t, tree, bool);
62 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
63 tsubst_flags_t);
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
70 tree
71 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
73 tree type;
75 if (processing_template_decl || value == error_mark_node)
76 return value;
78 if (TREE_CODE (value) == OVERLOAD)
79 type = unknown_type_node;
80 else
81 type = TREE_TYPE (value);
83 if (type == error_mark_node)
84 return error_mark_node;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type))
88 return value;
90 if (complete_type_or_maybe_complain (type, value, complain))
91 return value;
92 else
93 return error_mark_node;
96 tree
97 require_complete_type (tree value)
99 return require_complete_type_sfinae (value, tf_warning_or_error);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
107 tree
108 complete_type (tree type)
110 if (type == NULL_TREE)
111 /* Rather than crash, we return something sure to cause an error
112 at some point. */
113 return error_mark_node;
115 if (type == error_mark_node || COMPLETE_TYPE_P (type))
117 else if (TREE_CODE (type) == ARRAY_TYPE)
119 tree t = complete_type (TREE_TYPE (type));
120 unsigned int needs_constructing, has_nontrivial_dtor;
121 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
122 layout_type (type);
123 needs_constructing
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
125 has_nontrivial_dtor
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
127 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
129 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
133 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type));
136 return type;
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
143 tree
144 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
146 type = complete_type (type);
147 if (type == error_mark_node)
148 /* We already issued an error. */
149 return NULL_TREE;
150 else if (!COMPLETE_TYPE_P (type))
152 if (complain & tf_error)
153 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
154 return NULL_TREE;
156 else
157 return type;
160 tree
161 complete_type_or_else (tree type, tree value)
163 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
167 /* Return the common type of two parameter lists.
168 We assume that comptypes has already been done and returned 1;
169 if that isn't so, this may crash.
171 As an optimization, free the space we allocate if the parameter
172 lists are already common. */
174 static tree
175 commonparms (tree p1, tree p2)
177 tree oldargs = p1, newargs, n;
178 int i, len;
179 int any_change = 0;
181 len = list_length (p1);
182 newargs = tree_last (p1);
184 if (newargs == void_list_node)
185 i = 1;
186 else
188 i = 0;
189 newargs = 0;
192 for (; i < len; i++)
193 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
195 n = newargs;
197 for (i = 0; p1;
198 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
200 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
202 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
203 any_change = 1;
205 else if (! TREE_PURPOSE (p1))
207 if (TREE_PURPOSE (p2))
209 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
210 any_change = 1;
213 else
215 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
216 any_change = 1;
217 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
219 if (TREE_VALUE (p1) != TREE_VALUE (p2))
221 any_change = 1;
222 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
224 else
225 TREE_VALUE (n) = TREE_VALUE (p1);
227 if (! any_change)
228 return oldargs;
230 return newargs;
233 /* Given a type, perhaps copied for a typedef,
234 find the "original" version of it. */
235 static tree
236 original_type (tree t)
238 int quals = cp_type_quals (t);
239 while (t != error_mark_node
240 && TYPE_NAME (t) != NULL_TREE)
242 tree x = TYPE_NAME (t);
243 if (TREE_CODE (x) != TYPE_DECL)
244 break;
245 x = DECL_ORIGINAL_TYPE (x);
246 if (x == NULL_TREE)
247 break;
248 t = x;
250 return cp_build_qualified_type (t, quals);
253 /* Return the common type for two arithmetic types T1 and T2 under the
254 usual arithmetic conversions. The default conversions have already
255 been applied, and enumerated types converted to their compatible
256 integer types. */
258 static tree
259 cp_common_type (tree t1, tree t2)
261 enum tree_code code1 = TREE_CODE (t1);
262 enum tree_code code2 = TREE_CODE (t2);
263 tree attributes;
264 int i;
267 /* In what follows, we slightly generalize the rules given in [expr] so
268 as to deal with `long long' and `complex'. First, merge the
269 attributes. */
270 attributes = (*targetm.merge_type_attributes) (t1, t2);
272 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
274 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
275 return build_type_attribute_variant (t1, attributes);
276 else
277 return NULL_TREE;
280 /* FIXME: Attributes. */
281 gcc_assert (ARITHMETIC_TYPE_P (t1)
282 || VECTOR_TYPE_P (t1)
283 || UNSCOPED_ENUM_P (t1));
284 gcc_assert (ARITHMETIC_TYPE_P (t2)
285 || VECTOR_TYPE_P (t2)
286 || UNSCOPED_ENUM_P (t2));
288 /* If one type is complex, form the common type of the non-complex
289 components, then make that complex. Use T1 or T2 if it is the
290 required type. */
291 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
293 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
294 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
295 tree subtype
296 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
298 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
299 return build_type_attribute_variant (t1, attributes);
300 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
301 return build_type_attribute_variant (t2, attributes);
302 else
303 return build_type_attribute_variant (build_complex_type (subtype),
304 attributes);
307 if (code1 == VECTOR_TYPE)
309 /* When we get here we should have two vectors of the same size.
310 Just prefer the unsigned one if present. */
311 if (TYPE_UNSIGNED (t1))
312 return build_type_attribute_variant (t1, attributes);
313 else
314 return build_type_attribute_variant (t2, attributes);
317 /* If only one is real, use it as the result. */
318 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
319 return build_type_attribute_variant (t1, attributes);
320 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
321 return build_type_attribute_variant (t2, attributes);
323 /* Both real or both integers; use the one with greater precision. */
324 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
325 return build_type_attribute_variant (t1, attributes);
326 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
327 return build_type_attribute_variant (t2, attributes);
329 /* The types are the same; no need to do anything fancy. */
330 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
331 return build_type_attribute_variant (t1, attributes);
333 if (code1 != REAL_TYPE)
335 /* If one is unsigned long long, then convert the other to unsigned
336 long long. */
337 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
338 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
339 return build_type_attribute_variant (long_long_unsigned_type_node,
340 attributes);
341 /* If one is a long long, and the other is an unsigned long, and
342 long long can represent all the values of an unsigned long, then
343 convert to a long long. Otherwise, convert to an unsigned long
344 long. Otherwise, if either operand is long long, convert the
345 other to long long.
347 Since we're here, we know the TYPE_PRECISION is the same;
348 therefore converting to long long cannot represent all the values
349 of an unsigned long, so we choose unsigned long long in that
350 case. */
351 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
352 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
354 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
355 ? long_long_unsigned_type_node
356 : long_long_integer_type_node);
357 return build_type_attribute_variant (t, attributes);
360 /* Go through the same procedure, but for longs. */
361 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
362 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
363 return build_type_attribute_variant (long_unsigned_type_node,
364 attributes);
365 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
366 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
368 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
369 ? long_unsigned_type_node : long_integer_type_node);
370 return build_type_attribute_variant (t, attributes);
373 /* For __intN types, either the type is __int128 (and is lower
374 priority than the types checked above, but higher than other
375 128-bit types) or it's known to not be the same size as other
376 types (enforced in toplev.c). Prefer the unsigned type. */
377 for (i = 0; i < NUM_INT_N_ENTS; i ++)
379 if (int_n_enabled_p [i]
380 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
381 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
382 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
383 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
385 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
386 ? int_n_trees[i].unsigned_type
387 : int_n_trees[i].signed_type);
388 return build_type_attribute_variant (t, attributes);
392 /* Otherwise prefer the unsigned one. */
393 if (TYPE_UNSIGNED (t1))
394 return build_type_attribute_variant (t1, attributes);
395 else
396 return build_type_attribute_variant (t2, attributes);
398 else
400 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
401 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
402 return build_type_attribute_variant (long_double_type_node,
403 attributes);
404 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
405 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
406 return build_type_attribute_variant (double_type_node,
407 attributes);
408 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
409 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
410 return build_type_attribute_variant (float_type_node,
411 attributes);
413 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
414 the standard C++ floating-point types. Logic earlier in this
415 function has already eliminated the possibility that
416 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
417 compelling reason to choose one or the other. */
418 return build_type_attribute_variant (t1, attributes);
422 /* T1 and T2 are arithmetic or enumeration types. Return the type
423 that will result from the "usual arithmetic conversions" on T1 and
424 T2 as described in [expr]. */
426 tree
427 type_after_usual_arithmetic_conversions (tree t1, tree t2)
429 gcc_assert (ARITHMETIC_TYPE_P (t1)
430 || VECTOR_TYPE_P (t1)
431 || UNSCOPED_ENUM_P (t1));
432 gcc_assert (ARITHMETIC_TYPE_P (t2)
433 || VECTOR_TYPE_P (t2)
434 || UNSCOPED_ENUM_P (t2));
436 /* Perform the integral promotions. We do not promote real types here. */
437 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
438 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
440 t1 = type_promotes_to (t1);
441 t2 = type_promotes_to (t2);
444 return cp_common_type (t1, t2);
447 static void
448 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
449 composite_pointer_operation operation)
451 switch (operation)
453 case CPO_COMPARISON:
454 emit_diagnostic (kind, input_location, 0,
455 "comparison between "
456 "distinct pointer types %qT and %qT lacks a cast",
457 t1, t2);
458 break;
459 case CPO_CONVERSION:
460 emit_diagnostic (kind, input_location, 0,
461 "conversion between "
462 "distinct pointer types %qT and %qT lacks a cast",
463 t1, t2);
464 break;
465 case CPO_CONDITIONAL_EXPR:
466 emit_diagnostic (kind, input_location, 0,
467 "conditional expression between "
468 "distinct pointer types %qT and %qT lacks a cast",
469 t1, t2);
470 break;
471 default:
472 gcc_unreachable ();
476 /* Subroutine of composite_pointer_type to implement the recursive
477 case. See that function for documentation of the parameters. */
479 static tree
480 composite_pointer_type_r (tree t1, tree t2,
481 composite_pointer_operation operation,
482 tsubst_flags_t complain)
484 tree pointee1;
485 tree pointee2;
486 tree result_type;
487 tree attributes;
489 /* Determine the types pointed to by T1 and T2. */
490 if (TYPE_PTR_P (t1))
492 pointee1 = TREE_TYPE (t1);
493 pointee2 = TREE_TYPE (t2);
495 else
497 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
498 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
501 /* [expr.rel]
503 Otherwise, the composite pointer type is a pointer type
504 similar (_conv.qual_) to the type of one of the operands,
505 with a cv-qualification signature (_conv.qual_) that is the
506 union of the cv-qualification signatures of the operand
507 types. */
508 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
509 result_type = pointee1;
510 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
511 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
513 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
514 complain);
515 if (result_type == error_mark_node)
516 return error_mark_node;
518 else
520 if (complain & tf_error)
521 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
522 else
523 return error_mark_node;
524 result_type = void_type_node;
526 result_type = cp_build_qualified_type (result_type,
527 (cp_type_quals (pointee1)
528 | cp_type_quals (pointee2)));
529 /* If the original types were pointers to members, so is the
530 result. */
531 if (TYPE_PTRMEM_P (t1))
533 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
534 TYPE_PTRMEM_CLASS_TYPE (t2)))
536 if (complain & tf_error)
537 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
538 else
539 return error_mark_node;
541 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
542 result_type);
544 else
545 result_type = build_pointer_type (result_type);
547 /* Merge the attributes. */
548 attributes = (*targetm.merge_type_attributes) (t1, t2);
549 return build_type_attribute_variant (result_type, attributes);
552 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
553 ARG1 and ARG2 are the values with those types. The OPERATION is to
554 describe the operation between the pointer types,
555 in case an error occurs.
557 This routine also implements the computation of a common type for
558 pointers-to-members as per [expr.eq]. */
560 tree
561 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
562 composite_pointer_operation operation,
563 tsubst_flags_t complain)
565 tree class1;
566 tree class2;
568 /* [expr.rel]
570 If one operand is a null pointer constant, the composite pointer
571 type is the type of the other operand. */
572 if (null_ptr_cst_p (arg1))
573 return t2;
574 if (null_ptr_cst_p (arg2))
575 return t1;
577 /* We have:
579 [expr.rel]
581 If one of the operands has type "pointer to cv1 void*", then
582 the other has type "pointer to cv2T", and the composite pointer
583 type is "pointer to cv12 void", where cv12 is the union of cv1
584 and cv2.
586 If either type is a pointer to void, make sure it is T1. */
587 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
588 std::swap (t1, t2);
590 /* Now, if T1 is a pointer to void, merge the qualifiers. */
591 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
593 tree attributes;
594 tree result_type;
596 if (TYPE_PTRFN_P (t2))
598 if (complain & tf_error)
600 switch (operation)
602 case CPO_COMPARISON:
603 pedwarn (input_location, OPT_Wpedantic,
604 "ISO C++ forbids comparison between pointer "
605 "of type %<void *%> and pointer-to-function");
606 break;
607 case CPO_CONVERSION:
608 pedwarn (input_location, OPT_Wpedantic,
609 "ISO C++ forbids conversion between pointer "
610 "of type %<void *%> and pointer-to-function");
611 break;
612 case CPO_CONDITIONAL_EXPR:
613 pedwarn (input_location, OPT_Wpedantic,
614 "ISO C++ forbids conditional expression between "
615 "pointer of type %<void *%> and "
616 "pointer-to-function");
617 break;
618 default:
619 gcc_unreachable ();
622 else
623 return error_mark_node;
625 result_type
626 = cp_build_qualified_type (void_type_node,
627 (cp_type_quals (TREE_TYPE (t1))
628 | cp_type_quals (TREE_TYPE (t2))));
629 result_type = build_pointer_type (result_type);
630 /* Merge the attributes. */
631 attributes = (*targetm.merge_type_attributes) (t1, t2);
632 return build_type_attribute_variant (result_type, attributes);
635 if (c_dialect_objc () && TYPE_PTR_P (t1)
636 && TYPE_PTR_P (t2))
638 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
639 return objc_common_type (t1, t2);
642 /* if T1 or T2 is "pointer to noexcept function" and the other type is
643 "pointer to function", where the function types are otherwise the same,
644 "pointer to function" */
645 if (fnptr_conv_p (t1, t2))
646 return t1;
647 if (fnptr_conv_p (t2, t1))
648 return t2;
650 /* [expr.eq] permits the application of a pointer conversion to
651 bring the pointers to a common type. */
652 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
653 && CLASS_TYPE_P (TREE_TYPE (t1))
654 && CLASS_TYPE_P (TREE_TYPE (t2))
655 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
656 TREE_TYPE (t2)))
658 class1 = TREE_TYPE (t1);
659 class2 = TREE_TYPE (t2);
661 if (DERIVED_FROM_P (class1, class2))
662 t2 = (build_pointer_type
663 (cp_build_qualified_type (class1, cp_type_quals (class2))));
664 else if (DERIVED_FROM_P (class2, class1))
665 t1 = (build_pointer_type
666 (cp_build_qualified_type (class2, cp_type_quals (class1))));
667 else
669 if (complain & tf_error)
670 composite_pointer_error (DK_ERROR, t1, t2, operation);
671 return error_mark_node;
674 /* [expr.eq] permits the application of a pointer-to-member
675 conversion to change the class type of one of the types. */
676 else if (TYPE_PTRMEM_P (t1)
677 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
678 TYPE_PTRMEM_CLASS_TYPE (t2)))
680 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
681 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
683 if (DERIVED_FROM_P (class1, class2))
684 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
685 else if (DERIVED_FROM_P (class2, class1))
686 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
687 else
689 if (complain & tf_error)
690 switch (operation)
692 case CPO_COMPARISON:
693 error ("comparison between distinct "
694 "pointer-to-member types %qT and %qT lacks a cast",
695 t1, t2);
696 break;
697 case CPO_CONVERSION:
698 error ("conversion between distinct "
699 "pointer-to-member types %qT and %qT lacks a cast",
700 t1, t2);
701 break;
702 case CPO_CONDITIONAL_EXPR:
703 error ("conditional expression between distinct "
704 "pointer-to-member types %qT and %qT lacks a cast",
705 t1, t2);
706 break;
707 default:
708 gcc_unreachable ();
710 return error_mark_node;
714 return composite_pointer_type_r (t1, t2, operation, complain);
717 /* Return the merged type of two types.
718 We assume that comptypes has already been done and returned 1;
719 if that isn't so, this may crash.
721 This just combines attributes and default arguments; any other
722 differences would cause the two types to compare unalike. */
724 tree
725 merge_types (tree t1, tree t2)
727 enum tree_code code1;
728 enum tree_code code2;
729 tree attributes;
731 /* Save time if the two types are the same. */
732 if (t1 == t2)
733 return t1;
734 if (original_type (t1) == original_type (t2))
735 return t1;
737 /* If one type is nonsense, use the other. */
738 if (t1 == error_mark_node)
739 return t2;
740 if (t2 == error_mark_node)
741 return t1;
743 /* Handle merging an auto redeclaration with a previous deduced
744 return type. */
745 if (is_auto (t1))
746 return t2;
748 /* Merge the attributes. */
749 attributes = (*targetm.merge_type_attributes) (t1, t2);
751 if (TYPE_PTRMEMFUNC_P (t1))
752 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
753 if (TYPE_PTRMEMFUNC_P (t2))
754 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
756 code1 = TREE_CODE (t1);
757 code2 = TREE_CODE (t2);
758 if (code1 != code2)
760 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
761 if (code1 == TYPENAME_TYPE)
763 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
764 code1 = TREE_CODE (t1);
766 else
768 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
769 code2 = TREE_CODE (t2);
773 switch (code1)
775 case POINTER_TYPE:
776 case REFERENCE_TYPE:
777 /* For two pointers, do this recursively on the target type. */
779 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
780 int quals = cp_type_quals (t1);
782 if (code1 == POINTER_TYPE)
784 t1 = build_pointer_type (target);
785 if (TREE_CODE (target) == METHOD_TYPE)
786 t1 = build_ptrmemfunc_type (t1);
788 else
789 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
790 t1 = build_type_attribute_variant (t1, attributes);
791 t1 = cp_build_qualified_type (t1, quals);
793 return t1;
796 case OFFSET_TYPE:
798 int quals;
799 tree pointee;
800 quals = cp_type_quals (t1);
801 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
802 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
803 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
804 pointee);
805 t1 = cp_build_qualified_type (t1, quals);
806 break;
809 case ARRAY_TYPE:
811 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
812 /* Save space: see if the result is identical to one of the args. */
813 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
814 return build_type_attribute_variant (t1, attributes);
815 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
816 return build_type_attribute_variant (t2, attributes);
817 /* Merge the element types, and have a size if either arg has one. */
818 t1 = build_cplus_array_type
819 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
820 break;
823 case FUNCTION_TYPE:
824 /* Function types: prefer the one that specified arg types.
825 If both do, merge the arg types. Also merge the return types. */
827 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
828 tree p1 = TYPE_ARG_TYPES (t1);
829 tree p2 = TYPE_ARG_TYPES (t2);
830 tree parms;
831 tree rval, raises;
832 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
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 rval = build_function_type (valtype, parms);
849 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
850 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
851 rval = apply_memfn_quals (rval,
852 type_memfn_quals (t1),
853 type_memfn_rqual (t1));
854 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
855 TYPE_RAISES_EXCEPTIONS (t2));
856 t1 = build_exception_variant (rval, raises);
857 if (late_return_type_p)
858 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
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);
872 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
874 /* If this was a member function type, get back to the
875 original type of type member function (i.e., without
876 the class instance variable up front. */
877 t1 = build_function_type (TREE_TYPE (t1),
878 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
879 t2 = build_function_type (TREE_TYPE (t2),
880 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
881 t3 = merge_types (t1, t2);
882 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
883 TYPE_ARG_TYPES (t3));
884 t1 = build_exception_variant (t3, raises);
885 t1 = build_ref_qualified_type (t1, rqual);
886 if (late_return_type_1_p)
887 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
888 if (late_return_type_2_p)
889 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
890 break;
893 case TYPENAME_TYPE:
894 /* There is no need to merge attributes into a TYPENAME_TYPE.
895 When the type is instantiated it will have whatever
896 attributes result from the instantiation. */
897 return t1;
899 default:;
902 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
903 return t1;
904 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
905 return t2;
906 else
907 return cp_build_type_attribute_variant (t1, attributes);
910 /* Return the ARRAY_TYPE type without its domain. */
912 tree
913 strip_array_domain (tree type)
915 tree t2;
916 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
917 if (TYPE_DOMAIN (type) == NULL_TREE)
918 return type;
919 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
920 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
923 /* Wrapper around cp_common_type that is used by c-common.c and other
924 front end optimizations that remove promotions.
926 Return the common type for two arithmetic types T1 and T2 under the
927 usual arithmetic conversions. The default conversions have already
928 been applied, and enumerated types converted to their compatible
929 integer types. */
931 tree
932 common_type (tree t1, tree t2)
934 /* If one type is nonsense, use the other */
935 if (t1 == error_mark_node)
936 return t2;
937 if (t2 == error_mark_node)
938 return t1;
940 return cp_common_type (t1, t2);
943 /* Return the common type of two pointer types T1 and T2. This is the
944 type for the result of most arithmetic operations if the operands
945 have the given two types.
947 We assume that comp_target_types has already been done and returned
948 nonzero; if that isn't so, this may crash. */
950 tree
951 common_pointer_type (tree t1, tree t2)
953 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
954 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
955 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
957 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
958 CPO_CONVERSION, tf_warning_or_error);
961 /* Compare two exception specifier types for exactness or subsetness, if
962 allowed. Returns false for mismatch, true for match (same, or
963 derived and !exact).
965 [except.spec] "If a class X ... objects of class X or any class publicly
966 and unambiguously derived from X. Similarly, if a pointer type Y * ...
967 exceptions of type Y * or that are pointers to any type publicly and
968 unambiguously derived from Y. Otherwise a function only allows exceptions
969 that have the same type ..."
970 This does not mention cv qualifiers and is different to what throw
971 [except.throw] and catch [except.catch] will do. They will ignore the
972 top level cv qualifiers, and allow qualifiers in the pointer to class
973 example.
975 We implement the letter of the standard. */
977 static bool
978 comp_except_types (tree a, tree b, bool exact)
980 if (same_type_p (a, b))
981 return true;
982 else if (!exact)
984 if (cp_type_quals (a) || cp_type_quals (b))
985 return false;
987 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
989 a = TREE_TYPE (a);
990 b = TREE_TYPE (b);
991 if (cp_type_quals (a) || cp_type_quals (b))
992 return false;
995 if (TREE_CODE (a) != RECORD_TYPE
996 || TREE_CODE (b) != RECORD_TYPE)
997 return false;
999 if (publicly_uniquely_derived_p (a, b))
1000 return true;
1002 return false;
1005 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1006 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1007 If EXACT is ce_type, the C++17 type compatibility rules apply.
1008 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1009 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1010 are unordered, but we've already filtered out duplicates. Most lists will
1011 be in order, we should try to make use of that. */
1013 bool
1014 comp_except_specs (const_tree t1, const_tree t2, int exact)
1016 const_tree probe;
1017 const_tree base;
1018 int length = 0;
1020 if (t1 == t2)
1021 return true;
1023 /* First handle noexcept. */
1024 if (exact < ce_exact)
1026 if (exact == ce_type
1027 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1028 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1029 return true;
1031 /* noexcept(false) is compatible with no exception-specification,
1032 and less strict than any spec. */
1033 if (t1 == noexcept_false_spec)
1034 return t2 == NULL_TREE || exact == ce_derived;
1035 /* Even a derived noexcept(false) is compatible with no
1036 exception-specification. */
1037 if (t2 == noexcept_false_spec)
1038 return t1 == NULL_TREE;
1040 /* Otherwise, if we aren't looking for an exact match, noexcept is
1041 equivalent to throw(). */
1042 if (t1 == noexcept_true_spec)
1043 t1 = empty_except_spec;
1044 if (t2 == noexcept_true_spec)
1045 t2 = empty_except_spec;
1048 /* If any noexcept is left, it is only comparable to itself;
1049 either we're looking for an exact match or we're redeclaring a
1050 template with dependent noexcept. */
1051 if ((t1 && TREE_PURPOSE (t1))
1052 || (t2 && TREE_PURPOSE (t2)))
1053 return (t1 && t2
1054 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1056 if (t1 == NULL_TREE) /* T1 is ... */
1057 return t2 == NULL_TREE || exact == ce_derived;
1058 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1059 return t2 != NULL_TREE && !TREE_VALUE (t2);
1060 if (t2 == NULL_TREE) /* T2 is ... */
1061 return false;
1062 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1063 return exact == ce_derived;
1065 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1066 Count how many we find, to determine exactness. For exact matching and
1067 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1068 O(nm). */
1069 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1071 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1073 tree a = TREE_VALUE (probe);
1074 tree b = TREE_VALUE (t2);
1076 if (comp_except_types (a, b, exact))
1078 if (probe == base && exact > ce_derived)
1079 base = TREE_CHAIN (probe);
1080 length++;
1081 break;
1084 if (probe == NULL_TREE)
1085 return false;
1087 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1090 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1091 [] can match [size]. */
1093 static bool
1094 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1096 tree d1;
1097 tree d2;
1098 tree max1, max2;
1100 if (t1 == t2)
1101 return true;
1103 /* The type of the array elements must be the same. */
1104 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1105 return false;
1107 d1 = TYPE_DOMAIN (t1);
1108 d2 = TYPE_DOMAIN (t2);
1110 if (d1 == d2)
1111 return true;
1113 /* If one of the arrays is dimensionless, and the other has a
1114 dimension, they are of different types. However, it is valid to
1115 write:
1117 extern int a[];
1118 int a[3];
1120 by [basic.link]:
1122 declarations for an array object can specify
1123 array types that differ by the presence or absence of a major
1124 array bound (_dcl.array_). */
1125 if (!d1 || !d2)
1126 return allow_redeclaration;
1128 /* Check that the dimensions are the same. */
1130 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1131 return false;
1132 max1 = TYPE_MAX_VALUE (d1);
1133 max2 = TYPE_MAX_VALUE (d2);
1135 if (!cp_tree_equal (max1, max2))
1136 return false;
1138 return true;
1141 /* Compare the relative position of T1 and T2 into their respective
1142 template parameter list.
1143 T1 and T2 must be template parameter types.
1144 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1146 static bool
1147 comp_template_parms_position (tree t1, tree t2)
1149 tree index1, index2;
1150 gcc_assert (t1 && t2
1151 && TREE_CODE (t1) == TREE_CODE (t2)
1152 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1153 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1154 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1156 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1157 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1159 /* Then compare their relative position. */
1160 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1161 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1162 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1163 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1164 return false;
1166 /* In C++14 we can end up comparing 'auto' to a normal template
1167 parameter. Don't confuse them. */
1168 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1169 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1171 return true;
1174 /* Subroutine in comptypes. */
1176 static bool
1177 structural_comptypes (tree t1, tree t2, int strict)
1179 if (t1 == t2)
1180 return true;
1182 /* Suppress errors caused by previously reported errors. */
1183 if (t1 == error_mark_node || t2 == error_mark_node)
1184 return false;
1186 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1188 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1189 current instantiation. */
1190 if (TREE_CODE (t1) == TYPENAME_TYPE)
1191 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1193 if (TREE_CODE (t2) == TYPENAME_TYPE)
1194 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1196 if (TYPE_PTRMEMFUNC_P (t1))
1197 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1198 if (TYPE_PTRMEMFUNC_P (t2))
1199 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1201 /* Different classes of types can't be compatible. */
1202 if (TREE_CODE (t1) != TREE_CODE (t2))
1203 return false;
1205 /* Qualifiers must match. For array types, we will check when we
1206 recur on the array element types. */
1207 if (TREE_CODE (t1) != ARRAY_TYPE
1208 && cp_type_quals (t1) != cp_type_quals (t2))
1209 return false;
1210 if (TREE_CODE (t1) == FUNCTION_TYPE
1211 && type_memfn_quals (t1) != type_memfn_quals (t2))
1212 return false;
1213 /* Need to check this before TYPE_MAIN_VARIANT.
1214 FIXME function qualifiers should really change the main variant. */
1215 if (TREE_CODE (t1) == FUNCTION_TYPE
1216 || TREE_CODE (t1) == METHOD_TYPE)
1218 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1219 return false;
1220 if (flag_noexcept_type
1221 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1222 TYPE_RAISES_EXCEPTIONS (t2),
1223 ce_type))
1224 return false;
1227 /* Allow for two different type nodes which have essentially the same
1228 definition. Note that we already checked for equality of the type
1229 qualifiers (just above). */
1231 if (TREE_CODE (t1) != ARRAY_TYPE
1232 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1233 return true;
1236 /* Compare the types. Break out if they could be the same. */
1237 switch (TREE_CODE (t1))
1239 case VOID_TYPE:
1240 case BOOLEAN_TYPE:
1241 /* All void and bool types are the same. */
1242 break;
1244 case INTEGER_TYPE:
1245 case FIXED_POINT_TYPE:
1246 case REAL_TYPE:
1247 /* With these nodes, we can't determine type equivalence by
1248 looking at what is stored in the nodes themselves, because
1249 two nodes might have different TYPE_MAIN_VARIANTs but still
1250 represent the same type. For example, wchar_t and int could
1251 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1252 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1253 and are distinct types. On the other hand, int and the
1254 following typedef
1256 typedef int INT __attribute((may_alias));
1258 have identical properties, different TYPE_MAIN_VARIANTs, but
1259 represent the same type. The canonical type system keeps
1260 track of equivalence in this case, so we fall back on it. */
1261 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1263 case TEMPLATE_TEMPLATE_PARM:
1264 case BOUND_TEMPLATE_TEMPLATE_PARM:
1265 if (!comp_template_parms_position (t1, t2))
1266 return false;
1267 if (!comp_template_parms
1268 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1269 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1270 return false;
1271 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1272 break;
1273 /* Don't check inheritance. */
1274 strict = COMPARE_STRICT;
1275 /* Fall through. */
1277 case RECORD_TYPE:
1278 case UNION_TYPE:
1279 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1280 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1281 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1282 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1283 break;
1285 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1286 break;
1287 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1288 break;
1290 return false;
1292 case OFFSET_TYPE:
1293 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1294 strict & ~COMPARE_REDECLARATION))
1295 return false;
1296 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1297 return false;
1298 break;
1300 case REFERENCE_TYPE:
1301 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1302 return false;
1303 /* fall through to checks for pointer types */
1304 gcc_fallthrough ();
1306 case POINTER_TYPE:
1307 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1308 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1309 return false;
1310 break;
1312 case METHOD_TYPE:
1313 case FUNCTION_TYPE:
1314 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1315 return false;
1316 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1317 return false;
1318 break;
1320 case ARRAY_TYPE:
1321 /* Target types must match incl. qualifiers. */
1322 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1323 return false;
1324 break;
1326 case TEMPLATE_TYPE_PARM:
1327 /* If T1 and T2 don't have the same relative position in their
1328 template parameters set, they can't be equal. */
1329 if (!comp_template_parms_position (t1, t2))
1330 return false;
1331 /* Constrained 'auto's are distinct from parms that don't have the same
1332 constraints. */
1333 if (!equivalent_placeholder_constraints (t1, t2))
1334 return false;
1335 break;
1337 case TYPENAME_TYPE:
1338 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1339 TYPENAME_TYPE_FULLNAME (t2)))
1340 return false;
1341 /* Qualifiers don't matter on scopes. */
1342 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1343 TYPE_CONTEXT (t2)))
1344 return false;
1345 break;
1347 case UNBOUND_CLASS_TEMPLATE:
1348 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1349 return false;
1350 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1351 return false;
1352 break;
1354 case COMPLEX_TYPE:
1355 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1356 return false;
1357 break;
1359 case VECTOR_TYPE:
1360 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1361 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1362 return false;
1363 break;
1365 case TYPE_PACK_EXPANSION:
1366 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1367 PACK_EXPANSION_PATTERN (t2))
1368 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1369 PACK_EXPANSION_EXTRA_ARGS (t2)));
1371 case DECLTYPE_TYPE:
1372 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1373 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1374 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1375 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1376 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1377 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1378 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1379 DECLTYPE_TYPE_EXPR (t2)))
1380 return false;
1381 break;
1383 case UNDERLYING_TYPE:
1384 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1385 UNDERLYING_TYPE_TYPE (t2));
1387 default:
1388 return false;
1391 /* If we get here, we know that from a target independent POV the
1392 types are the same. Make sure the target attributes are also
1393 the same. */
1394 return comp_type_attributes (t1, t2);
1397 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1398 is a bitwise-or of the COMPARE_* flags. */
1400 bool
1401 comptypes (tree t1, tree t2, int strict)
1403 if (strict == COMPARE_STRICT)
1405 if (t1 == t2)
1406 return true;
1408 if (t1 == error_mark_node || t2 == error_mark_node)
1409 return false;
1411 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1412 /* At least one of the types requires structural equality, so
1413 perform a deep check. */
1414 return structural_comptypes (t1, t2, strict);
1416 if (flag_checking && USE_CANONICAL_TYPES)
1418 bool result = structural_comptypes (t1, t2, strict);
1420 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1421 /* The two types are structurally equivalent, but their
1422 canonical types were different. This is a failure of the
1423 canonical type propagation code.*/
1424 internal_error
1425 ("canonical types differ for identical types %qT and %qT",
1426 t1, t2);
1427 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1428 /* Two types are structurally different, but the canonical
1429 types are the same. This means we were over-eager in
1430 assigning canonical types. */
1431 internal_error
1432 ("same canonical type node for different types %qT and %qT",
1433 t1, t2);
1435 return result;
1437 if (!flag_checking && USE_CANONICAL_TYPES)
1438 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1439 else
1440 return structural_comptypes (t1, t2, strict);
1442 else if (strict == COMPARE_STRUCTURAL)
1443 return structural_comptypes (t1, t2, COMPARE_STRICT);
1444 else
1445 return structural_comptypes (t1, t2, strict);
1448 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1449 top-level qualifiers. */
1451 bool
1452 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1454 if (type1 == error_mark_node || type2 == error_mark_node)
1455 return false;
1457 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1458 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1459 return same_type_p (type1, type2);
1462 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1464 bool
1465 at_least_as_qualified_p (const_tree type1, const_tree type2)
1467 int q1 = cp_type_quals (type1);
1468 int q2 = cp_type_quals (type2);
1470 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1471 return (q1 & q2) == q2;
1474 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1475 more cv-qualified that TYPE1, and 0 otherwise. */
1478 comp_cv_qualification (int q1, int q2)
1480 if (q1 == q2)
1481 return 0;
1483 if ((q1 & q2) == q2)
1484 return 1;
1485 else if ((q1 & q2) == q1)
1486 return -1;
1488 return 0;
1492 comp_cv_qualification (const_tree type1, const_tree type2)
1494 int q1 = cp_type_quals (type1);
1495 int q2 = cp_type_quals (type2);
1496 return comp_cv_qualification (q1, q2);
1499 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1500 subset of the cv-qualification signature of TYPE2, and the types
1501 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1504 comp_cv_qual_signature (tree type1, tree type2)
1506 if (comp_ptr_ttypes_real (type2, type1, -1))
1507 return 1;
1508 else if (comp_ptr_ttypes_real (type1, type2, -1))
1509 return -1;
1510 else
1511 return 0;
1514 /* Subroutines of `comptypes'. */
1516 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1517 equivalent in the sense that functions with those parameter types
1518 can have equivalent types. The two lists must be equivalent,
1519 element by element. */
1521 bool
1522 compparms (const_tree parms1, const_tree parms2)
1524 const_tree t1, t2;
1526 /* An unspecified parmlist matches any specified parmlist
1527 whose argument types don't need default promotions. */
1529 for (t1 = parms1, t2 = parms2;
1530 t1 || t2;
1531 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1533 /* If one parmlist is shorter than the other,
1534 they fail to match. */
1535 if (!t1 || !t2)
1536 return false;
1537 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1538 return false;
1540 return true;
1544 /* Process a sizeof or alignof expression where the operand is a
1545 type. */
1547 tree
1548 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1550 tree value;
1551 bool dependent_p;
1553 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1554 if (type == error_mark_node)
1555 return error_mark_node;
1557 type = non_reference (type);
1558 if (TREE_CODE (type) == METHOD_TYPE)
1560 if (complain)
1561 pedwarn (input_location, OPT_Wpointer_arith,
1562 "invalid application of %qs to a member function",
1563 operator_name_info[(int) op].name);
1564 else
1565 return error_mark_node;
1566 value = size_one_node;
1569 dependent_p = dependent_type_p (type);
1570 if (!dependent_p)
1571 complete_type (type);
1572 if (dependent_p
1573 /* VLA types will have a non-constant size. In the body of an
1574 uninstantiated template, we don't need to try to compute the
1575 value, because the sizeof expression is not an integral
1576 constant expression in that case. And, if we do try to
1577 compute the value, we'll likely end up with SAVE_EXPRs, which
1578 the template substitution machinery does not expect to see. */
1579 || (processing_template_decl
1580 && COMPLETE_TYPE_P (type)
1581 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1583 value = build_min (op, size_type_node, type);
1584 TREE_READONLY (value) = 1;
1585 return value;
1588 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1589 op == SIZEOF_EXPR, false,
1590 complain);
1593 /* Return the size of the type, without producing any warnings for
1594 types whose size cannot be taken. This routine should be used only
1595 in some other routine that has already produced a diagnostic about
1596 using the size of such a type. */
1597 tree
1598 cxx_sizeof_nowarn (tree type)
1600 if (TREE_CODE (type) == FUNCTION_TYPE
1601 || VOID_TYPE_P (type)
1602 || TREE_CODE (type) == ERROR_MARK)
1603 return size_one_node;
1604 else if (!COMPLETE_TYPE_P (type))
1605 return size_zero_node;
1606 else
1607 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1610 /* Process a sizeof expression where the operand is an expression. */
1612 static tree
1613 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1615 if (e == error_mark_node)
1616 return error_mark_node;
1618 if (processing_template_decl)
1620 e = build_min (SIZEOF_EXPR, size_type_node, e);
1621 TREE_SIDE_EFFECTS (e) = 0;
1622 TREE_READONLY (e) = 1;
1624 return e;
1627 /* To get the size of a static data member declared as an array of
1628 unknown bound, we need to instantiate it. */
1629 if (VAR_P (e)
1630 && VAR_HAD_UNKNOWN_BOUND (e)
1631 && DECL_TEMPLATE_INSTANTIATION (e))
1632 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1634 if (TREE_CODE (e) == PARM_DECL
1635 && DECL_ARRAY_PARAMETER_P (e)
1636 && (complain & tf_warning))
1638 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1639 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1640 inform (DECL_SOURCE_LOCATION (e), "declared here");
1643 e = mark_type_use (e);
1645 if (bitfield_p (e))
1647 if (complain & tf_error)
1648 error ("invalid application of %<sizeof%> to a bit-field");
1649 else
1650 return error_mark_node;
1651 e = char_type_node;
1653 else if (is_overloaded_fn (e))
1655 if (complain & tf_error)
1656 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1657 "function type");
1658 else
1659 return error_mark_node;
1660 e = char_type_node;
1662 else if (type_unknown_p (e))
1664 if (complain & tf_error)
1665 cxx_incomplete_type_error (e, TREE_TYPE (e));
1666 else
1667 return error_mark_node;
1668 e = char_type_node;
1670 else
1671 e = TREE_TYPE (e);
1673 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1676 /* Implement the __alignof keyword: Return the minimum required
1677 alignment of E, measured in bytes. For VAR_DECL's and
1678 FIELD_DECL's return DECL_ALIGN (which can be set from an
1679 "aligned" __attribute__ specification). */
1681 static tree
1682 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1684 tree t;
1686 if (e == error_mark_node)
1687 return error_mark_node;
1689 if (processing_template_decl)
1691 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1692 TREE_SIDE_EFFECTS (e) = 0;
1693 TREE_READONLY (e) = 1;
1695 return e;
1698 e = mark_type_use (e);
1700 if (VAR_P (e))
1701 t = size_int (DECL_ALIGN_UNIT (e));
1702 else if (bitfield_p (e))
1704 if (complain & tf_error)
1705 error ("invalid application of %<__alignof%> to a bit-field");
1706 else
1707 return error_mark_node;
1708 t = size_one_node;
1710 else if (TREE_CODE (e) == COMPONENT_REF
1711 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1712 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1713 else if (is_overloaded_fn (e))
1715 if (complain & tf_error)
1716 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1717 "function type");
1718 else
1719 return error_mark_node;
1720 if (TREE_CODE (e) == FUNCTION_DECL)
1721 t = size_int (DECL_ALIGN_UNIT (e));
1722 else
1723 t = size_one_node;
1725 else if (type_unknown_p (e))
1727 if (complain & tf_error)
1728 cxx_incomplete_type_error (e, TREE_TYPE (e));
1729 else
1730 return error_mark_node;
1731 t = size_one_node;
1733 else
1734 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1735 complain & tf_error);
1737 return fold_convert (size_type_node, t);
1740 /* Process a sizeof or alignof expression E with code OP where the operand
1741 is an expression. */
1743 tree
1744 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1746 if (op == SIZEOF_EXPR)
1747 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1748 else
1749 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1752 /* Build a representation of an expression 'alignas(E).' Return the
1753 folded integer value of E if it is an integral constant expression
1754 that resolves to a valid alignment. If E depends on a template
1755 parameter, return a syntactic representation tree of kind
1756 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1757 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1759 tree
1760 cxx_alignas_expr (tree e)
1762 if (e == NULL_TREE || e == error_mark_node
1763 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1764 return e;
1766 if (TYPE_P (e))
1767 /* [dcl.align]/3:
1769 When the alignment-specifier is of the form
1770 alignas(type-id ), it shall have the same effect as
1771 alignas(alignof(type-id )). */
1773 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1775 /* If we reach this point, it means the alignas expression if of
1776 the form "alignas(assignment-expression)", so we should follow
1777 what is stated by [dcl.align]/2. */
1779 if (value_dependent_expression_p (e))
1780 /* Leave value-dependent expression alone for now. */
1781 return e;
1783 e = instantiate_non_dependent_expr (e);
1784 e = mark_rvalue_use (e);
1786 /* [dcl.align]/2 says:
1788 the assignment-expression shall be an integral constant
1789 expression. */
1791 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1793 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1794 return error_mark_node;
1797 return cxx_constant_value (e);
1801 /* EXPR is being used in a context that is not a function call.
1802 Enforce:
1804 [expr.ref]
1806 The expression can be used only as the left-hand operand of a
1807 member function call.
1809 [expr.mptr.operator]
1811 If the result of .* or ->* is a function, then that result can be
1812 used only as the operand for the function call operator ().
1814 by issuing an error message if appropriate. Returns true iff EXPR
1815 violates these rules. */
1817 bool
1818 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1820 if (expr == NULL_TREE)
1821 return false;
1822 /* Don't enforce this in MS mode. */
1823 if (flag_ms_extensions)
1824 return false;
1825 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1826 expr = get_first_fn (expr);
1827 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1829 if (complain & tf_error)
1831 if (DECL_P (expr))
1833 error_at (loc, "invalid use of non-static member function %qD",
1834 expr);
1835 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1837 else
1838 error_at (loc, "invalid use of non-static member function of "
1839 "type %qT", TREE_TYPE (expr));
1841 return true;
1843 return false;
1846 /* If EXP is a reference to a bitfield, and the type of EXP does not
1847 match the declared type of the bitfield, return the declared type
1848 of the bitfield. Otherwise, return NULL_TREE. */
1850 tree
1851 is_bitfield_expr_with_lowered_type (const_tree exp)
1853 switch (TREE_CODE (exp))
1855 case COND_EXPR:
1856 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1857 ? TREE_OPERAND (exp, 1)
1858 : TREE_OPERAND (exp, 0)))
1859 return NULL_TREE;
1860 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1862 case COMPOUND_EXPR:
1863 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1865 case MODIFY_EXPR:
1866 case SAVE_EXPR:
1867 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1869 case COMPONENT_REF:
1871 tree field;
1873 field = TREE_OPERAND (exp, 1);
1874 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1875 return NULL_TREE;
1876 if (same_type_ignoring_top_level_qualifiers_p
1877 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1878 return NULL_TREE;
1879 return DECL_BIT_FIELD_TYPE (field);
1882 case VAR_DECL:
1883 if (DECL_HAS_VALUE_EXPR_P (exp))
1884 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1885 (CONST_CAST_TREE (exp)));
1886 return NULL_TREE;
1888 CASE_CONVERT:
1889 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1890 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1891 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1892 /* Fallthrough. */
1894 default:
1895 return NULL_TREE;
1899 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1900 bitfield with a lowered type, the type of EXP is returned, rather
1901 than NULL_TREE. */
1903 tree
1904 unlowered_expr_type (const_tree exp)
1906 tree type;
1907 tree etype = TREE_TYPE (exp);
1909 type = is_bitfield_expr_with_lowered_type (exp);
1910 if (type)
1911 type = cp_build_qualified_type (type, cp_type_quals (etype));
1912 else
1913 type = etype;
1915 return type;
1918 /* Perform the conversions in [expr] that apply when an lvalue appears
1919 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1920 function-to-pointer conversions. In addition, bitfield references are
1921 converted to their declared types. Note that this function does not perform
1922 the lvalue-to-rvalue conversion for class types. If you need that conversion
1923 for class types, then you probably need to use force_rvalue.
1925 Although the returned value is being used as an rvalue, this
1926 function does not wrap the returned expression in a
1927 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1928 that the return value is no longer an lvalue. */
1930 tree
1931 decay_conversion (tree exp,
1932 tsubst_flags_t complain,
1933 bool reject_builtin /* = true */)
1935 tree type;
1936 enum tree_code code;
1937 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1939 type = TREE_TYPE (exp);
1940 if (type == error_mark_node)
1941 return error_mark_node;
1943 exp = resolve_nondeduced_context (exp, complain);
1944 if (type_unknown_p (exp))
1946 if (complain & tf_error)
1947 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1948 return error_mark_node;
1951 code = TREE_CODE (type);
1953 if (error_operand_p (exp))
1954 return error_mark_node;
1956 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1957 return nullptr_node;
1959 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1960 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1961 if (code == VOID_TYPE)
1963 if (complain & tf_error)
1964 error_at (loc, "void value not ignored as it ought to be");
1965 return error_mark_node;
1967 if (invalid_nonstatic_memfn_p (loc, exp, complain))
1968 return error_mark_node;
1969 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1971 exp = mark_lvalue_use (exp);
1972 if (reject_builtin && reject_gcc_builtin (exp, loc))
1973 return error_mark_node;
1974 return cp_build_addr_expr (exp, complain);
1976 if (code == ARRAY_TYPE)
1978 tree adr;
1979 tree ptrtype;
1981 exp = mark_lvalue_use (exp);
1983 if (INDIRECT_REF_P (exp))
1984 return build_nop (build_pointer_type (TREE_TYPE (type)),
1985 TREE_OPERAND (exp, 0));
1987 if (TREE_CODE (exp) == COMPOUND_EXPR)
1989 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1990 if (op1 == error_mark_node)
1991 return error_mark_node;
1992 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1993 TREE_OPERAND (exp, 0), op1);
1996 if (!obvalue_p (exp)
1997 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1999 if (complain & tf_error)
2000 error_at (loc, "invalid use of non-lvalue array");
2001 return error_mark_node;
2004 /* Don't let an array compound literal decay to a pointer. It can
2005 still be used to initialize an array or bind to a reference. */
2006 if (TREE_CODE (exp) == TARGET_EXPR)
2008 if (complain & tf_error)
2009 error_at (loc, "taking address of temporary array");
2010 return error_mark_node;
2013 ptrtype = build_pointer_type (TREE_TYPE (type));
2015 if (VAR_P (exp))
2017 if (!cxx_mark_addressable (exp))
2018 return error_mark_node;
2019 adr = build_nop (ptrtype, build_address (exp));
2020 return adr;
2022 /* This way is better for a COMPONENT_REF since it can
2023 simplify the offset for a component. */
2024 adr = cp_build_addr_expr (exp, complain);
2025 return cp_convert (ptrtype, adr, complain);
2028 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2029 exp = mark_rvalue_use (exp, loc, reject_builtin);
2031 /* If a bitfield is used in a context where integral promotion
2032 applies, then the caller is expected to have used
2033 default_conversion. That function promotes bitfields correctly
2034 before calling this function. At this point, if we have a
2035 bitfield referenced, we may assume that is not subject to
2036 promotion, and that, therefore, the type of the resulting rvalue
2037 is the declared type of the bitfield. */
2038 exp = convert_bitfield_to_declared_type (exp);
2040 /* We do not call rvalue() here because we do not want to wrap EXP
2041 in a NON_LVALUE_EXPR. */
2043 /* [basic.lval]
2045 Non-class rvalues always have cv-unqualified types. */
2046 type = TREE_TYPE (exp);
2047 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2048 exp = build_nop (cv_unqualified (type), exp);
2050 if (!complete_type_or_maybe_complain (type, exp, complain))
2051 return error_mark_node;
2053 return exp;
2056 /* Perform preparatory conversions, as part of the "usual arithmetic
2057 conversions". In particular, as per [expr]:
2059 Whenever an lvalue expression appears as an operand of an
2060 operator that expects the rvalue for that operand, the
2061 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2062 standard conversions are applied to convert the expression to an
2063 rvalue.
2065 In addition, we perform integral promotions here, as those are
2066 applied to both operands to a binary operator before determining
2067 what additional conversions should apply. */
2069 static tree
2070 cp_default_conversion (tree exp, tsubst_flags_t complain)
2072 /* Check for target-specific promotions. */
2073 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2074 if (promoted_type)
2075 exp = cp_convert (promoted_type, exp, complain);
2076 /* Perform the integral promotions first so that bitfield
2077 expressions (which may promote to "int", even if the bitfield is
2078 declared "unsigned") are promoted correctly. */
2079 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2080 exp = cp_perform_integral_promotions (exp, complain);
2081 /* Perform the other conversions. */
2082 exp = decay_conversion (exp, complain);
2084 return exp;
2087 /* C version. */
2089 tree
2090 default_conversion (tree exp)
2092 return cp_default_conversion (exp, tf_warning_or_error);
2095 /* EXPR is an expression with an integral or enumeration type.
2096 Perform the integral promotions in [conv.prom], and return the
2097 converted value. */
2099 tree
2100 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2102 tree type;
2103 tree promoted_type;
2105 expr = mark_rvalue_use (expr);
2107 /* [conv.prom]
2109 If the bitfield has an enumerated type, it is treated as any
2110 other value of that type for promotion purposes. */
2111 type = is_bitfield_expr_with_lowered_type (expr);
2112 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2113 type = TREE_TYPE (expr);
2114 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2115 /* Scoped enums don't promote. */
2116 if (SCOPED_ENUM_P (type))
2117 return expr;
2118 promoted_type = type_promotes_to (type);
2119 if (type != promoted_type)
2120 expr = cp_convert (promoted_type, expr, complain);
2121 return expr;
2124 /* C version. */
2126 tree
2127 perform_integral_promotions (tree expr)
2129 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2132 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2133 decay_conversion to one. */
2136 string_conv_p (const_tree totype, const_tree exp, int warn)
2138 tree t;
2140 if (!TYPE_PTR_P (totype))
2141 return 0;
2143 t = TREE_TYPE (totype);
2144 if (!same_type_p (t, char_type_node)
2145 && !same_type_p (t, char16_type_node)
2146 && !same_type_p (t, char32_type_node)
2147 && !same_type_p (t, wchar_type_node))
2148 return 0;
2150 if (TREE_CODE (exp) == STRING_CST)
2152 /* Make sure that we don't try to convert between char and wide chars. */
2153 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2154 return 0;
2156 else
2158 /* Is this a string constant which has decayed to 'const char *'? */
2159 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2160 if (!same_type_p (TREE_TYPE (exp), t))
2161 return 0;
2162 STRIP_NOPS (exp);
2163 if (TREE_CODE (exp) != ADDR_EXPR
2164 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2165 return 0;
2167 if (warn)
2169 if (cxx_dialect >= cxx11)
2170 pedwarn (input_location, OPT_Wwrite_strings,
2171 "ISO C++ forbids converting a string constant to %qT",
2172 totype);
2173 else
2174 warning (OPT_Wwrite_strings,
2175 "deprecated conversion from string constant to %qT",
2176 totype);
2179 return 1;
2182 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2183 can, for example, use as an lvalue. This code used to be in
2184 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2185 expressions, where we're dealing with aggregates. But now it's again only
2186 called from unary_complex_lvalue. The case (in particular) that led to
2187 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2188 get it there. */
2190 static tree
2191 rationalize_conditional_expr (enum tree_code code, tree t,
2192 tsubst_flags_t complain)
2194 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2196 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2197 the first operand is always the one to be used if both operands
2198 are equal, so we know what conditional expression this used to be. */
2199 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2201 tree op0 = TREE_OPERAND (t, 0);
2202 tree op1 = TREE_OPERAND (t, 1);
2204 /* The following code is incorrect if either operand side-effects. */
2205 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2206 && !TREE_SIDE_EFFECTS (op1));
2207 return
2208 build_conditional_expr (loc,
2209 build_x_binary_op (loc,
2210 (TREE_CODE (t) == MIN_EXPR
2211 ? LE_EXPR : GE_EXPR),
2212 op0, TREE_CODE (op0),
2213 op1, TREE_CODE (op1),
2214 /*overload=*/NULL,
2215 complain),
2216 cp_build_unary_op (code, op0, false, complain),
2217 cp_build_unary_op (code, op1, false, complain),
2218 complain);
2221 return
2222 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2223 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
2224 complain),
2225 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
2226 complain),
2227 complain);
2230 /* Given the TYPE of an anonymous union field inside T, return the
2231 FIELD_DECL for the field. If not found return NULL_TREE. Because
2232 anonymous unions can nest, we must also search all anonymous unions
2233 that are directly reachable. */
2235 tree
2236 lookup_anon_field (tree t, tree type)
2238 tree field;
2240 t = TYPE_MAIN_VARIANT (t);
2242 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2244 if (TREE_STATIC (field))
2245 continue;
2246 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2247 continue;
2249 /* If we find it directly, return the field. */
2250 if (DECL_NAME (field) == NULL_TREE
2251 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2253 return field;
2256 /* Otherwise, it could be nested, search harder. */
2257 if (DECL_NAME (field) == NULL_TREE
2258 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2260 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2261 if (subfield)
2262 return subfield;
2265 return NULL_TREE;
2268 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2269 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2270 non-NULL, it indicates the path to the base used to name MEMBER.
2271 If PRESERVE_REFERENCE is true, the expression returned will have
2272 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2273 returned will have the type referred to by the reference.
2275 This function does not perform access control; that is either done
2276 earlier by the parser when the name of MEMBER is resolved to MEMBER
2277 itself, or later when overload resolution selects one of the
2278 functions indicated by MEMBER. */
2280 tree
2281 build_class_member_access_expr (cp_expr object, tree member,
2282 tree access_path, bool preserve_reference,
2283 tsubst_flags_t complain)
2285 tree object_type;
2286 tree member_scope;
2287 tree result = NULL_TREE;
2288 tree using_decl = NULL_TREE;
2290 if (error_operand_p (object) || error_operand_p (member))
2291 return error_mark_node;
2293 gcc_assert (DECL_P (member) || BASELINK_P (member));
2295 /* [expr.ref]
2297 The type of the first expression shall be "class object" (of a
2298 complete type). */
2299 object_type = TREE_TYPE (object);
2300 if (!currently_open_class (object_type)
2301 && !complete_type_or_maybe_complain (object_type, object, complain))
2302 return error_mark_node;
2303 if (!CLASS_TYPE_P (object_type))
2305 if (complain & tf_error)
2307 if (POINTER_TYPE_P (object_type)
2308 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2309 error ("request for member %qD in %qE, which is of pointer "
2310 "type %qT (maybe you meant to use %<->%> ?)",
2311 member, object.get_value (), object_type);
2312 else
2313 error ("request for member %qD in %qE, which is of non-class "
2314 "type %qT", member, object.get_value (), object_type);
2316 return error_mark_node;
2319 /* The standard does not seem to actually say that MEMBER must be a
2320 member of OBJECT_TYPE. However, that is clearly what is
2321 intended. */
2322 if (DECL_P (member))
2324 member_scope = DECL_CLASS_CONTEXT (member);
2325 if (!mark_used (member, complain) && !(complain & tf_error))
2326 return error_mark_node;
2327 if (TREE_DEPRECATED (member))
2328 warn_deprecated_use (member, NULL_TREE);
2330 else
2331 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2332 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2333 presently be the anonymous union. Go outwards until we find a
2334 type related to OBJECT_TYPE. */
2335 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2336 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2337 object_type))
2338 member_scope = TYPE_CONTEXT (member_scope);
2339 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2341 if (complain & tf_error)
2343 if (TREE_CODE (member) == FIELD_DECL)
2344 error ("invalid use of nonstatic data member %qE", member);
2345 else
2346 error ("%qD is not a member of %qT", member, object_type);
2348 return error_mark_node;
2351 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2352 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2353 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2355 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2356 if (temp)
2357 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2360 /* In [expr.ref], there is an explicit list of the valid choices for
2361 MEMBER. We check for each of those cases here. */
2362 if (VAR_P (member))
2364 /* A static data member. */
2365 result = member;
2366 mark_exp_read (object);
2367 /* If OBJECT has side-effects, they are supposed to occur. */
2368 if (TREE_SIDE_EFFECTS (object))
2369 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2371 else if (TREE_CODE (member) == FIELD_DECL)
2373 /* A non-static data member. */
2374 bool null_object_p;
2375 int type_quals;
2376 tree member_type;
2378 if (INDIRECT_REF_P (object))
2379 null_object_p =
2380 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2381 else
2382 null_object_p = false;
2384 /* Convert OBJECT to the type of MEMBER. */
2385 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2386 TYPE_MAIN_VARIANT (member_scope)))
2388 tree binfo;
2389 base_kind kind;
2391 binfo = lookup_base (access_path ? access_path : object_type,
2392 member_scope, ba_unique, &kind, complain);
2393 if (binfo == error_mark_node)
2394 return error_mark_node;
2396 /* It is invalid to try to get to a virtual base of a
2397 NULL object. The most common cause is invalid use of
2398 offsetof macro. */
2399 if (null_object_p && kind == bk_via_virtual)
2401 if (complain & tf_error)
2403 error ("invalid access to non-static data member %qD in "
2404 "virtual base of NULL object", member);
2406 return error_mark_node;
2409 /* Convert to the base. */
2410 object = build_base_path (PLUS_EXPR, object, binfo,
2411 /*nonnull=*/1, complain);
2412 /* If we found the base successfully then we should be able
2413 to convert to it successfully. */
2414 gcc_assert (object != error_mark_node);
2417 /* If MEMBER is from an anonymous aggregate, we have converted
2418 OBJECT so that it refers to the class containing the
2419 anonymous union. Generate a reference to the anonymous union
2420 itself, and recur to find MEMBER. */
2421 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2422 /* When this code is called from build_field_call, the
2423 object already has the type of the anonymous union.
2424 That is because the COMPONENT_REF was already
2425 constructed, and was then disassembled before calling
2426 build_field_call. After the function-call code is
2427 cleaned up, this waste can be eliminated. */
2428 && (!same_type_ignoring_top_level_qualifiers_p
2429 (TREE_TYPE (object), DECL_CONTEXT (member))))
2431 tree anonymous_union;
2433 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2434 DECL_CONTEXT (member));
2435 object = build_class_member_access_expr (object,
2436 anonymous_union,
2437 /*access_path=*/NULL_TREE,
2438 preserve_reference,
2439 complain);
2442 /* Compute the type of the field, as described in [expr.ref]. */
2443 type_quals = TYPE_UNQUALIFIED;
2444 member_type = TREE_TYPE (member);
2445 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2447 type_quals = (cp_type_quals (member_type)
2448 | cp_type_quals (object_type));
2450 /* A field is const (volatile) if the enclosing object, or the
2451 field itself, is const (volatile). But, a mutable field is
2452 not const, even within a const object. */
2453 if (DECL_MUTABLE_P (member))
2454 type_quals &= ~TYPE_QUAL_CONST;
2455 member_type = cp_build_qualified_type (member_type, type_quals);
2458 result = build3_loc (input_location, COMPONENT_REF, member_type,
2459 object, member, NULL_TREE);
2461 /* Mark the expression const or volatile, as appropriate. Even
2462 though we've dealt with the type above, we still have to mark the
2463 expression itself. */
2464 if (type_quals & TYPE_QUAL_CONST)
2465 TREE_READONLY (result) = 1;
2466 if (type_quals & TYPE_QUAL_VOLATILE)
2467 TREE_THIS_VOLATILE (result) = 1;
2469 else if (BASELINK_P (member))
2471 /* The member is a (possibly overloaded) member function. */
2472 tree functions;
2473 tree type;
2475 /* If the MEMBER is exactly one static member function, then we
2476 know the type of the expression. Otherwise, we must wait
2477 until overload resolution has been performed. */
2478 functions = BASELINK_FUNCTIONS (member);
2479 if (TREE_CODE (functions) == FUNCTION_DECL
2480 && DECL_STATIC_FUNCTION_P (functions))
2481 type = TREE_TYPE (functions);
2482 else
2483 type = unknown_type_node;
2484 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2485 base. That will happen when the function is called. */
2486 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2488 else if (TREE_CODE (member) == CONST_DECL)
2490 /* The member is an enumerator. */
2491 result = member;
2492 /* If OBJECT has side-effects, they are supposed to occur. */
2493 if (TREE_SIDE_EFFECTS (object))
2494 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2495 object, result);
2497 else if ((using_decl = strip_using_decl (member)) != member)
2498 result = build_class_member_access_expr (object,
2499 using_decl,
2500 access_path, preserve_reference,
2501 complain);
2502 else
2504 if (complain & tf_error)
2505 error ("invalid use of %qD", member);
2506 return error_mark_node;
2509 if (!preserve_reference)
2510 /* [expr.ref]
2512 If E2 is declared to have type "reference to T", then ... the
2513 type of E1.E2 is T. */
2514 result = convert_from_reference (result);
2516 return result;
2519 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2520 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2522 static tree
2523 lookup_destructor (tree object, tree scope, tree dtor_name,
2524 tsubst_flags_t complain)
2526 tree object_type = TREE_TYPE (object);
2527 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2528 tree expr;
2530 /* We've already complained about this destructor. */
2531 if (dtor_type == error_mark_node)
2532 return error_mark_node;
2534 if (scope && !check_dtor_name (scope, dtor_type))
2536 if (complain & tf_error)
2537 error ("qualified type %qT does not match destructor name ~%qT",
2538 scope, dtor_type);
2539 return error_mark_node;
2541 if (is_auto (dtor_type))
2542 dtor_type = object_type;
2543 else if (identifier_p (dtor_type))
2545 /* In a template, names we can't find a match for are still accepted
2546 destructor names, and we check them here. */
2547 if (check_dtor_name (object_type, dtor_type))
2548 dtor_type = object_type;
2549 else
2551 if (complain & tf_error)
2552 error ("object type %qT does not match destructor name ~%qT",
2553 object_type, dtor_type);
2554 return error_mark_node;
2558 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2560 if (complain & tf_error)
2561 error ("the type being destroyed is %qT, but the destructor "
2562 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2563 return error_mark_node;
2565 expr = lookup_member (dtor_type, complete_dtor_identifier,
2566 /*protect=*/1, /*want_type=*/false,
2567 tf_warning_or_error);
2568 if (!expr)
2570 if (complain & tf_error)
2571 cxx_incomplete_type_error (dtor_name, dtor_type);
2572 return error_mark_node;
2574 expr = (adjust_result_of_qualified_name_lookup
2575 (expr, dtor_type, object_type));
2576 if (scope == NULL_TREE)
2577 /* We need to call adjust_result_of_qualified_name_lookup in case the
2578 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2579 that we still get virtual function binding. */
2580 BASELINK_QUALIFIED_P (expr) = false;
2581 return expr;
2584 /* An expression of the form "A::template B" has been resolved to
2585 DECL. Issue a diagnostic if B is not a template or template
2586 specialization. */
2588 void
2589 check_template_keyword (tree decl)
2591 /* The standard says:
2593 [temp.names]
2595 If a name prefixed by the keyword template is not a member
2596 template, the program is ill-formed.
2598 DR 228 removed the restriction that the template be a member
2599 template.
2601 DR 96, if accepted would add the further restriction that explicit
2602 template arguments must be provided if the template keyword is
2603 used, but, as of 2005-10-16, that DR is still in "drafting". If
2604 this DR is accepted, then the semantic checks here can be
2605 simplified, as the entity named must in fact be a template
2606 specialization, rather than, as at present, a set of overloaded
2607 functions containing at least one template function. */
2608 if (TREE_CODE (decl) != TEMPLATE_DECL
2609 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2611 if (VAR_P (decl))
2613 if (DECL_USE_TEMPLATE (decl)
2614 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2616 else
2617 permerror (input_location, "%qD is not a template", decl);
2619 else if (!is_overloaded_fn (decl))
2620 permerror (input_location, "%qD is not a template", decl);
2621 else
2623 bool found = false;
2625 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
2626 !found && iter; ++iter)
2628 tree fn = *iter;
2629 if (TREE_CODE (fn) == TEMPLATE_DECL
2630 || TREE_CODE (fn) == TEMPLATE_ID_EXPR
2631 || (TREE_CODE (fn) == FUNCTION_DECL
2632 && DECL_USE_TEMPLATE (fn)
2633 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
2634 found = true;
2636 if (!found)
2637 permerror (input_location, "%qD is not a template", decl);
2642 /* Record that an access failure occurred on BASETYPE_PATH attempting
2643 to access FIELD_DECL. */
2645 void
2646 access_failure_info::record_access_failure (tree basetype_path,
2647 tree field_decl)
2649 m_was_inaccessible = true;
2650 m_basetype_path = basetype_path;
2651 m_field_decl = field_decl;
2654 /* If an access failure was recorded, then attempt to locate an
2655 accessor function for the pertinent field, and if one is
2656 available, add a note and fix-it hint suggesting using it. */
2658 void
2659 access_failure_info::maybe_suggest_accessor (bool const_p) const
2661 if (!m_was_inaccessible)
2662 return;
2664 tree accessor
2665 = locate_field_accessor (m_basetype_path, m_field_decl, const_p);
2666 if (!accessor)
2667 return;
2669 /* The accessor must itself be accessible for it to be a reasonable
2670 suggestion. */
2671 if (!accessible_p (m_basetype_path, accessor, true))
2672 return;
2674 rich_location richloc (line_table, input_location);
2675 pretty_printer pp;
2676 pp_printf (&pp, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor)));
2677 richloc.add_fixit_replace (pp_formatted_text (&pp));
2678 inform_at_rich_loc (&richloc, "field %q#D can be accessed via %q#D",
2679 m_field_decl, accessor);
2682 /* This function is called by the parser to process a class member
2683 access expression of the form OBJECT.NAME. NAME is a node used by
2684 the parser to represent a name; it is not yet a DECL. It may,
2685 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2686 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2687 there is no reason to do the lookup twice, so the parser keeps the
2688 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2689 be a template via the use of the "A::template B" syntax. */
2691 tree
2692 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2693 tsubst_flags_t complain)
2695 tree expr;
2696 tree object_type;
2697 tree member;
2698 tree access_path = NULL_TREE;
2699 tree orig_object = object;
2700 tree orig_name = name;
2702 if (object == error_mark_node || name == error_mark_node)
2703 return error_mark_node;
2705 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2706 if (!objc_is_public (object, name))
2707 return error_mark_node;
2709 object_type = TREE_TYPE (object);
2711 if (processing_template_decl)
2713 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2714 type_dependent_object_expression_p (object)
2715 /* If NAME is "f<args>", where either 'f' or 'args' is
2716 dependent, then the expression is dependent. */
2717 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2718 && dependent_template_id_p (TREE_OPERAND (name, 0),
2719 TREE_OPERAND (name, 1)))
2720 /* If NAME is "T::X" where "T" is dependent, then the
2721 expression is dependent. */
2722 || (TREE_CODE (name) == SCOPE_REF
2723 && TYPE_P (TREE_OPERAND (name, 0))
2724 && dependent_scope_p (TREE_OPERAND (name, 0))))
2726 dependent:
2727 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2728 orig_object, orig_name, NULL_TREE);
2730 object = build_non_dependent_expr (object);
2732 else if (c_dialect_objc ()
2733 && identifier_p (name)
2734 && (expr = objc_maybe_build_component_ref (object, name)))
2735 return expr;
2737 /* [expr.ref]
2739 The type of the first expression shall be "class object" (of a
2740 complete type). */
2741 if (!currently_open_class (object_type)
2742 && !complete_type_or_maybe_complain (object_type, object, complain))
2743 return error_mark_node;
2744 if (!CLASS_TYPE_P (object_type))
2746 if (complain & tf_error)
2748 if (POINTER_TYPE_P (object_type)
2749 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2750 error ("request for member %qD in %qE, which is of pointer "
2751 "type %qT (maybe you meant to use %<->%> ?)",
2752 name, object.get_value (), object_type);
2753 else
2754 error ("request for member %qD in %qE, which is of non-class "
2755 "type %qT", name, object.get_value (), object_type);
2757 return error_mark_node;
2760 if (BASELINK_P (name))
2761 /* A member function that has already been looked up. */
2762 member = name;
2763 else
2765 bool is_template_id = false;
2766 tree template_args = NULL_TREE;
2767 tree scope = NULL_TREE;
2769 access_path = object_type;
2771 if (TREE_CODE (name) == SCOPE_REF)
2773 /* A qualified name. The qualifying class or namespace `S'
2774 has already been looked up; it is either a TYPE or a
2775 NAMESPACE_DECL. */
2776 scope = TREE_OPERAND (name, 0);
2777 name = TREE_OPERAND (name, 1);
2779 /* If SCOPE is a namespace, then the qualified name does not
2780 name a member of OBJECT_TYPE. */
2781 if (TREE_CODE (scope) == NAMESPACE_DECL)
2783 if (complain & tf_error)
2784 error ("%<%D::%D%> is not a member of %qT",
2785 scope, name, object_type);
2786 return error_mark_node;
2790 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2792 is_template_id = true;
2793 template_args = TREE_OPERAND (name, 1);
2794 name = TREE_OPERAND (name, 0);
2796 if (!identifier_p (name))
2797 name = OVL_NAME (name);
2800 if (scope)
2802 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2804 gcc_assert (!is_template_id);
2805 /* Looking up a member enumerator (c++/56793). */
2806 if (!TYPE_CLASS_SCOPE_P (scope)
2807 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2809 if (complain & tf_error)
2810 error ("%<%D::%D%> is not a member of %qT",
2811 scope, name, object_type);
2812 return error_mark_node;
2814 tree val = lookup_enumerator (scope, name);
2815 if (!val)
2817 if (complain & tf_error)
2818 error ("%qD is not a member of %qD",
2819 name, scope);
2820 return error_mark_node;
2823 if (TREE_SIDE_EFFECTS (object))
2824 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2825 return val;
2828 gcc_assert (CLASS_TYPE_P (scope));
2829 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2831 if (constructor_name_p (name, scope))
2833 if (complain & tf_error)
2834 error ("cannot call constructor %<%T::%D%> directly",
2835 scope, name);
2836 return error_mark_node;
2839 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2840 access_path = lookup_base (object_type, scope, ba_check,
2841 NULL, complain);
2842 if (access_path == error_mark_node)
2843 return error_mark_node;
2844 if (!access_path)
2846 if (any_dependent_bases_p (object_type))
2847 goto dependent;
2848 if (complain & tf_error)
2849 error ("%qT is not a base of %qT", scope, object_type);
2850 return error_mark_node;
2854 if (TREE_CODE (name) == BIT_NOT_EXPR)
2856 if (dependent_type_p (object_type))
2857 /* The destructor isn't declared yet. */
2858 goto dependent;
2859 member = lookup_destructor (object, scope, name, complain);
2861 else
2863 /* Look up the member. */
2864 access_failure_info afi;
2865 member = lookup_member (access_path, name, /*protect=*/1,
2866 /*want_type=*/false, complain,
2867 &afi);
2868 afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
2869 if (member == NULL_TREE)
2871 if (dependent_type_p (object_type))
2872 /* Try again at instantiation time. */
2873 goto dependent;
2874 if (complain & tf_error)
2876 tree guessed_id = lookup_member_fuzzy (access_path, name,
2877 /*want_type=*/false);
2878 if (guessed_id)
2880 location_t bogus_component_loc = input_location;
2881 gcc_rich_location rich_loc (bogus_component_loc);
2882 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2883 guessed_id);
2884 error_at_rich_loc
2885 (&rich_loc,
2886 "%q#T has no member named %qE; did you mean %qE?",
2887 TREE_CODE (access_path) == TREE_BINFO
2888 ? TREE_TYPE (access_path) : object_type, name,
2889 guessed_id);
2891 else
2892 error ("%q#T has no member named %qE",
2893 TREE_CODE (access_path) == TREE_BINFO
2894 ? TREE_TYPE (access_path) : object_type, name);
2896 return error_mark_node;
2898 if (member == error_mark_node)
2899 return error_mark_node;
2900 if (DECL_P (member)
2901 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2902 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2903 wrong, so don't use it. */
2904 goto dependent;
2905 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2906 goto dependent;
2909 if (is_template_id)
2911 tree templ = member;
2913 if (BASELINK_P (templ))
2914 member = lookup_template_function (templ, template_args);
2915 else if (variable_template_p (templ))
2916 member = (lookup_and_finish_template_variable
2917 (templ, template_args, complain));
2918 else
2920 if (complain & tf_error)
2921 error ("%qD is not a member template function", name);
2922 return error_mark_node;
2927 if (TREE_DEPRECATED (member))
2928 warn_deprecated_use (member, NULL_TREE);
2930 if (template_p)
2931 check_template_keyword (member);
2933 expr = build_class_member_access_expr (object, member, access_path,
2934 /*preserve_reference=*/false,
2935 complain);
2936 if (processing_template_decl && expr != error_mark_node)
2938 if (BASELINK_P (member))
2940 if (TREE_CODE (orig_name) == SCOPE_REF)
2941 BASELINK_QUALIFIED_P (member) = 1;
2942 orig_name = member;
2944 return build_min_non_dep (COMPONENT_REF, expr,
2945 orig_object, orig_name,
2946 NULL_TREE);
2949 return expr;
2952 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2953 type. */
2955 tree
2956 build_simple_component_ref (tree object, tree member)
2958 tree type = cp_build_qualified_type (TREE_TYPE (member),
2959 cp_type_quals (TREE_TYPE (object)));
2960 return build3_loc (input_location,
2961 COMPONENT_REF, type,
2962 object, member, NULL_TREE);
2965 /* Return an expression for the MEMBER_NAME field in the internal
2966 representation of PTRMEM, a pointer-to-member function. (Each
2967 pointer-to-member function type gets its own RECORD_TYPE so it is
2968 more convenient to access the fields by name than by FIELD_DECL.)
2969 This routine converts the NAME to a FIELD_DECL and then creates the
2970 node for the complete expression. */
2972 tree
2973 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2975 tree ptrmem_type;
2976 tree member;
2978 /* This code is a stripped down version of
2979 build_class_member_access_expr. It does not work to use that
2980 routine directly because it expects the object to be of class
2981 type. */
2982 ptrmem_type = TREE_TYPE (ptrmem);
2983 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2984 for (member = TYPE_FIELDS (ptrmem_type); member;
2985 member = DECL_CHAIN (member))
2986 if (DECL_NAME (member) == member_name)
2987 break;
2988 tree res = build_simple_component_ref (ptrmem, member);
2990 TREE_NO_WARNING (res) = 1;
2991 return res;
2994 /* Given an expression PTR for a pointer, return an expression
2995 for the value pointed to.
2996 ERRORSTRING is the name of the operator to appear in error messages.
2998 This function may need to overload OPERATOR_FNNAME.
2999 Must also handle REFERENCE_TYPEs for C++. */
3001 tree
3002 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
3003 tsubst_flags_t complain)
3005 tree orig_expr = expr;
3006 tree rval;
3007 tree overload = NULL_TREE;
3009 if (processing_template_decl)
3011 /* Retain the type if we know the operand is a pointer. */
3012 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
3013 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
3014 if (type_dependent_expression_p (expr))
3015 return build_min_nt_loc (loc, INDIRECT_REF, expr);
3016 expr = build_non_dependent_expr (expr);
3019 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
3020 NULL_TREE, NULL_TREE, &overload, complain);
3021 if (!rval)
3022 rval = cp_build_indirect_ref (expr, errorstring, complain);
3024 if (processing_template_decl && rval != error_mark_node)
3026 if (overload != NULL_TREE)
3027 return (build_min_non_dep_op_overload
3028 (INDIRECT_REF, rval, overload, orig_expr));
3030 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3032 else
3033 return rval;
3036 /* Helper function called from c-common. */
3037 tree
3038 build_indirect_ref (location_t /*loc*/,
3039 tree ptr, ref_operator errorstring)
3041 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
3044 tree
3045 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
3046 tsubst_flags_t complain)
3048 tree pointer, type;
3050 if (ptr == current_class_ptr
3051 || (TREE_CODE (ptr) == NOP_EXPR
3052 && TREE_OPERAND (ptr, 0) == current_class_ptr
3053 && (same_type_ignoring_top_level_qualifiers_p
3054 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
3055 return current_class_ref;
3057 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
3058 ? ptr : decay_conversion (ptr, complain));
3059 if (pointer == error_mark_node)
3060 return error_mark_node;
3062 type = TREE_TYPE (pointer);
3064 if (POINTER_TYPE_P (type))
3066 /* [expr.unary.op]
3068 If the type of the expression is "pointer to T," the type
3069 of the result is "T." */
3070 tree t = TREE_TYPE (type);
3072 if ((CONVERT_EXPR_P (ptr)
3073 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3074 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
3076 /* If a warning is issued, mark it to avoid duplicates from
3077 the backend. This only needs to be done at
3078 warn_strict_aliasing > 2. */
3079 if (warn_strict_aliasing > 2)
3080 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
3081 type, TREE_OPERAND (ptr, 0)))
3082 TREE_NO_WARNING (ptr) = 1;
3085 if (VOID_TYPE_P (t))
3087 /* A pointer to incomplete type (other than cv void) can be
3088 dereferenced [expr.unary.op]/1 */
3089 if (complain & tf_error)
3090 error ("%qT is not a pointer-to-object type", type);
3091 return error_mark_node;
3093 else if (TREE_CODE (pointer) == ADDR_EXPR
3094 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3095 /* The POINTER was something like `&x'. We simplify `*&x' to
3096 `x'. */
3097 return TREE_OPERAND (pointer, 0);
3098 else
3100 tree ref = build1 (INDIRECT_REF, t, pointer);
3102 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3103 so that we get the proper error message if the result is used
3104 to assign to. Also, &* is supposed to be a no-op. */
3105 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3106 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3107 TREE_SIDE_EFFECTS (ref)
3108 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3109 return ref;
3112 else if (!(complain & tf_error))
3113 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3115 /* `pointer' won't be an error_mark_node if we were given a
3116 pointer to member, so it's cool to check for this here. */
3117 else if (TYPE_PTRMEM_P (type))
3118 switch (errorstring)
3120 case RO_ARRAY_INDEXING:
3121 error ("invalid use of array indexing on pointer to member");
3122 break;
3123 case RO_UNARY_STAR:
3124 error ("invalid use of unary %<*%> on pointer to member");
3125 break;
3126 case RO_IMPLICIT_CONVERSION:
3127 error ("invalid use of implicit conversion on pointer to member");
3128 break;
3129 case RO_ARROW_STAR:
3130 error ("left hand operand of %<->*%> must be a pointer to class, "
3131 "but is a pointer to member of type %qT", type);
3132 break;
3133 default:
3134 gcc_unreachable ();
3136 else if (pointer != error_mark_node)
3137 invalid_indirection_error (input_location, type, errorstring);
3139 return error_mark_node;
3142 /* This handles expressions of the form "a[i]", which denotes
3143 an array reference.
3145 This is logically equivalent in C to *(a+i), but we may do it differently.
3146 If A is a variable or a member, we generate a primitive ARRAY_REF.
3147 This avoids forcing the array out of registers, and can work on
3148 arrays that are not lvalues (for example, members of structures returned
3149 by functions).
3151 If INDEX is of some user-defined type, it must be converted to
3152 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3153 will inherit the type of the array, which will be some pointer type.
3155 LOC is the location to use in building the array reference. */
3157 tree
3158 cp_build_array_ref (location_t loc, tree array, tree idx,
3159 tsubst_flags_t complain)
3161 tree ret;
3163 if (idx == 0)
3165 if (complain & tf_error)
3166 error_at (loc, "subscript missing in array reference");
3167 return error_mark_node;
3170 /* If an array's index is an array notation, then its rank cannot be
3171 greater than one. */
3172 if (flag_cilkplus && contains_array_notation_expr (idx))
3174 size_t rank = 0;
3176 /* If find_rank returns false, then it should have reported an error,
3177 thus it is unnecessary for repetition. */
3178 if (!find_rank (loc, idx, idx, true, &rank))
3179 return error_mark_node;
3180 if (rank > 1)
3182 error_at (loc, "rank of the array%'s index is greater than 1");
3183 return error_mark_node;
3186 if (TREE_TYPE (array) == error_mark_node
3187 || TREE_TYPE (idx) == error_mark_node)
3188 return error_mark_node;
3190 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3191 inside it. */
3192 switch (TREE_CODE (array))
3194 case COMPOUND_EXPR:
3196 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3197 complain);
3198 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3199 TREE_OPERAND (array, 0), value);
3200 SET_EXPR_LOCATION (ret, loc);
3201 return ret;
3204 case COND_EXPR:
3205 ret = build_conditional_expr
3206 (loc, TREE_OPERAND (array, 0),
3207 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3208 complain),
3209 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3210 complain),
3211 complain);
3212 protected_set_expr_location (ret, loc);
3213 return ret;
3215 default:
3216 break;
3219 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
3221 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3223 tree rval, type;
3225 warn_array_subscript_with_type_char (loc, idx);
3227 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3229 if (complain & tf_error)
3230 error_at (loc, "array subscript is not an integer");
3231 return error_mark_node;
3234 /* Apply integral promotions *after* noticing character types.
3235 (It is unclear why we do these promotions -- the standard
3236 does not say that we should. In fact, the natural thing would
3237 seem to be to convert IDX to ptrdiff_t; we're performing
3238 pointer arithmetic.) */
3239 idx = cp_perform_integral_promotions (idx, complain);
3241 /* An array that is indexed by a non-constant
3242 cannot be stored in a register; we must be able to do
3243 address arithmetic on its address.
3244 Likewise an array of elements of variable size. */
3245 if (TREE_CODE (idx) != INTEGER_CST
3246 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3247 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3248 != INTEGER_CST)))
3250 if (!cxx_mark_addressable (array, true))
3251 return error_mark_node;
3254 /* An array that is indexed by a constant value which is not within
3255 the array bounds cannot be stored in a register either; because we
3256 would get a crash in store_bit_field/extract_bit_field when trying
3257 to access a non-existent part of the register. */
3258 if (TREE_CODE (idx) == INTEGER_CST
3259 && TYPE_DOMAIN (TREE_TYPE (array))
3260 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3262 if (!cxx_mark_addressable (array))
3263 return error_mark_node;
3266 /* Note in C++ it is valid to subscript a `register' array, since
3267 it is valid to take the address of something with that
3268 storage specification. */
3269 if (extra_warnings)
3271 tree foo = array;
3272 while (TREE_CODE (foo) == COMPONENT_REF)
3273 foo = TREE_OPERAND (foo, 0);
3274 if (VAR_P (foo) && DECL_REGISTER (foo)
3275 && (complain & tf_warning))
3276 warning_at (loc, OPT_Wextra,
3277 "subscripting array declared %<register%>");
3280 type = TREE_TYPE (TREE_TYPE (array));
3281 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3282 /* Array ref is const/volatile if the array elements are
3283 or if the array is.. */
3284 TREE_READONLY (rval)
3285 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3286 TREE_SIDE_EFFECTS (rval)
3287 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3288 TREE_THIS_VOLATILE (rval)
3289 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3290 ret = require_complete_type_sfinae (rval, complain);
3291 protected_set_expr_location (ret, loc);
3292 if (non_lvalue)
3293 ret = non_lvalue_loc (loc, ret);
3294 return ret;
3298 tree ar = cp_default_conversion (array, complain);
3299 tree ind = cp_default_conversion (idx, complain);
3301 /* Put the integer in IND to simplify error checking. */
3302 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3303 std::swap (ar, ind);
3305 if (ar == error_mark_node || ind == error_mark_node)
3306 return error_mark_node;
3308 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3310 if (complain & tf_error)
3311 error_at (loc, "subscripted value is neither array nor pointer");
3312 return error_mark_node;
3314 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3316 if (complain & tf_error)
3317 error_at (loc, "array subscript is not an integer");
3318 return error_mark_node;
3321 warn_array_subscript_with_type_char (loc, idx);
3323 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3324 PLUS_EXPR, ar, ind,
3325 complain),
3326 RO_ARRAY_INDEXING,
3327 complain);
3328 protected_set_expr_location (ret, loc);
3329 if (non_lvalue)
3330 ret = non_lvalue_loc (loc, ret);
3331 return ret;
3335 /* Entry point for Obj-C++. */
3337 tree
3338 build_array_ref (location_t loc, tree array, tree idx)
3340 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3343 /* Resolve a pointer to member function. INSTANCE is the object
3344 instance to use, if the member points to a virtual member.
3346 This used to avoid checking for virtual functions if basetype
3347 has no virtual functions, according to an earlier ANSI draft.
3348 With the final ISO C++ rules, such an optimization is
3349 incorrect: A pointer to a derived member can be static_cast
3350 to pointer-to-base-member, as long as the dynamic object
3351 later has the right member. So now we only do this optimization
3352 when we know the dynamic type of the object. */
3354 tree
3355 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3356 tsubst_flags_t complain)
3358 if (TREE_CODE (function) == OFFSET_REF)
3359 function = TREE_OPERAND (function, 1);
3361 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3363 tree idx, delta, e1, e2, e3, vtbl;
3364 bool nonvirtual;
3365 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3366 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3368 tree instance_ptr = *instance_ptrptr;
3369 tree instance_save_expr = 0;
3370 if (instance_ptr == error_mark_node)
3372 if (TREE_CODE (function) == PTRMEM_CST)
3374 /* Extracting the function address from a pmf is only
3375 allowed with -Wno-pmf-conversions. It only works for
3376 pmf constants. */
3377 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3378 e1 = convert (fntype, e1);
3379 return e1;
3381 else
3383 if (complain & tf_error)
3384 error ("object missing in use of %qE", function);
3385 return error_mark_node;
3389 /* True if we know that the dynamic type of the object doesn't have
3390 virtual functions, so we can assume the PFN field is a pointer. */
3391 nonvirtual = (COMPLETE_TYPE_P (basetype)
3392 && !TYPE_POLYMORPHIC_P (basetype)
3393 && resolves_to_fixed_type_p (instance_ptr, 0));
3395 /* If we don't really have an object (i.e. in an ill-formed
3396 conversion from PMF to pointer), we can't resolve virtual
3397 functions anyway. */
3398 if (!nonvirtual && is_dummy_object (instance_ptr))
3399 nonvirtual = true;
3401 if (TREE_SIDE_EFFECTS (instance_ptr))
3402 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3404 if (TREE_SIDE_EFFECTS (function))
3405 function = save_expr (function);
3407 /* Start by extracting all the information from the PMF itself. */
3408 e3 = pfn_from_ptrmemfunc (function);
3409 delta = delta_from_ptrmemfunc (function);
3410 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3411 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3413 int flag_sanitize_save;
3414 case ptrmemfunc_vbit_in_pfn:
3415 e1 = cp_build_binary_op (input_location,
3416 BIT_AND_EXPR, idx, integer_one_node,
3417 complain);
3418 idx = cp_build_binary_op (input_location,
3419 MINUS_EXPR, idx, integer_one_node,
3420 complain);
3421 if (idx == error_mark_node)
3422 return error_mark_node;
3423 break;
3425 case ptrmemfunc_vbit_in_delta:
3426 e1 = cp_build_binary_op (input_location,
3427 BIT_AND_EXPR, delta, integer_one_node,
3428 complain);
3429 /* Don't instrument the RSHIFT_EXPR we're about to create because
3430 we're going to use DELTA number of times, and that wouldn't play
3431 well with SAVE_EXPRs therein. */
3432 flag_sanitize_save = flag_sanitize;
3433 flag_sanitize = 0;
3434 delta = cp_build_binary_op (input_location,
3435 RSHIFT_EXPR, delta, integer_one_node,
3436 complain);
3437 flag_sanitize = flag_sanitize_save;
3438 if (delta == error_mark_node)
3439 return error_mark_node;
3440 break;
3442 default:
3443 gcc_unreachable ();
3446 if (e1 == error_mark_node)
3447 return error_mark_node;
3449 /* Convert down to the right base before using the instance. A
3450 special case is that in a pointer to member of class C, C may
3451 be incomplete. In that case, the function will of course be
3452 a member of C, and no conversion is required. In fact,
3453 lookup_base will fail in that case, because incomplete
3454 classes do not have BINFOs. */
3455 if (!same_type_ignoring_top_level_qualifiers_p
3456 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3458 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3459 basetype, ba_check, NULL, complain);
3460 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3461 1, complain);
3462 if (instance_ptr == error_mark_node)
3463 return error_mark_node;
3465 /* ...and then the delta in the PMF. */
3466 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3468 /* Hand back the adjusted 'this' argument to our caller. */
3469 *instance_ptrptr = instance_ptr;
3471 if (nonvirtual)
3472 /* Now just return the pointer. */
3473 return e3;
3475 /* Next extract the vtable pointer from the object. */
3476 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3477 instance_ptr);
3478 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3479 if (vtbl == error_mark_node)
3480 return error_mark_node;
3482 /* Finally, extract the function pointer from the vtable. */
3483 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3484 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3485 if (e2 == error_mark_node)
3486 return error_mark_node;
3487 TREE_CONSTANT (e2) = 1;
3489 /* When using function descriptors, the address of the
3490 vtable entry is treated as a function pointer. */
3491 if (TARGET_VTABLE_USES_DESCRIPTORS)
3492 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3493 cp_build_addr_expr (e2, complain));
3495 e2 = fold_convert (TREE_TYPE (e3), e2);
3496 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3497 if (e1 == error_mark_node)
3498 return error_mark_node;
3500 /* Make sure this doesn't get evaluated first inside one of the
3501 branches of the COND_EXPR. */
3502 if (instance_save_expr)
3503 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3504 instance_save_expr, e1);
3506 function = e1;
3508 return function;
3511 /* Used by the C-common bits. */
3512 tree
3513 build_function_call (location_t /*loc*/,
3514 tree function, tree params)
3516 return cp_build_function_call (function, params, tf_warning_or_error);
3519 /* Used by the C-common bits. */
3520 tree
3521 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3522 tree function, vec<tree, va_gc> *params,
3523 vec<tree, va_gc> * /*origtypes*/)
3525 vec<tree, va_gc> *orig_params = params;
3526 tree ret = cp_build_function_call_vec (function, &params,
3527 tf_warning_or_error);
3529 /* cp_build_function_call_vec can reallocate PARAMS by adding
3530 default arguments. That should never happen here. Verify
3531 that. */
3532 gcc_assert (params == orig_params);
3534 return ret;
3537 /* Build a function call using a tree list of arguments. */
3539 static tree
3540 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3542 vec<tree, va_gc> *vec;
3543 tree ret;
3545 vec = make_tree_vector ();
3546 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3547 vec_safe_push (vec, TREE_VALUE (params));
3548 ret = cp_build_function_call_vec (function, &vec, complain);
3549 release_tree_vector (vec);
3550 return ret;
3553 /* Build a function call using varargs. */
3555 tree
3556 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3558 vec<tree, va_gc> *vec;
3559 va_list args;
3560 tree ret, t;
3562 vec = make_tree_vector ();
3563 va_start (args, complain);
3564 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3565 vec_safe_push (vec, t);
3566 va_end (args);
3567 ret = cp_build_function_call_vec (function, &vec, complain);
3568 release_tree_vector (vec);
3569 return ret;
3572 /* Build a function call using a vector of arguments. PARAMS may be
3573 NULL if there are no parameters. This changes the contents of
3574 PARAMS. */
3576 tree
3577 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3578 tsubst_flags_t complain)
3580 tree fntype, fndecl;
3581 int is_method;
3582 tree original = function;
3583 int nargs;
3584 tree *argarray;
3585 tree parm_types;
3586 vec<tree, va_gc> *allocated = NULL;
3587 tree ret;
3589 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3590 expressions, like those used for ObjC messenger dispatches. */
3591 if (params != NULL && !vec_safe_is_empty (*params))
3592 function = objc_rewrite_function_call (function, (**params)[0]);
3594 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3595 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3596 if (TREE_CODE (function) == NOP_EXPR
3597 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3598 function = TREE_OPERAND (function, 0);
3600 if (TREE_CODE (function) == FUNCTION_DECL)
3602 /* If the function is a non-template member function
3603 or a non-template friend, then we need to check the
3604 constraints.
3606 Note that if overload resolution failed with a single
3607 candidate this function will be used to explicitly diagnose
3608 the failure for the single call expression. The check is
3609 technically redundant since we also would have failed in
3610 add_function_candidate. */
3611 if (flag_concepts
3612 && (complain & tf_error)
3613 && !constraints_satisfied_p (function))
3615 error ("cannot call function %qD", function);
3616 location_t loc = DECL_SOURCE_LOCATION (function);
3617 diagnose_constraints (loc, function, NULL_TREE);
3618 return error_mark_node;
3621 if (!mark_used (function, complain) && !(complain & tf_error))
3622 return error_mark_node;
3623 fndecl = function;
3625 /* Convert anything with function type to a pointer-to-function. */
3626 if (DECL_MAIN_P (function))
3628 if (complain & tf_error)
3629 pedwarn (input_location, OPT_Wpedantic,
3630 "ISO C++ forbids calling %<::main%> from within program");
3631 else
3632 return error_mark_node;
3634 function = build_addr_func (function, complain);
3636 else
3638 fndecl = NULL_TREE;
3640 function = build_addr_func (function, complain);
3643 if (function == error_mark_node)
3644 return error_mark_node;
3646 fntype = TREE_TYPE (function);
3648 if (TYPE_PTRMEMFUNC_P (fntype))
3650 if (complain & tf_error)
3651 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3652 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3653 original, original);
3654 return error_mark_node;
3657 is_method = (TYPE_PTR_P (fntype)
3658 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3660 if (!(TYPE_PTRFN_P (fntype)
3661 || is_method
3662 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3664 if (complain & tf_error)
3666 if (!flag_diagnostics_show_caret)
3667 error_at (input_location,
3668 "%qE cannot be used as a function", original);
3669 else if (DECL_P (original))
3670 error_at (input_location,
3671 "%qD cannot be used as a function", original);
3672 else
3673 error_at (input_location,
3674 "expression cannot be used as a function");
3677 return error_mark_node;
3680 /* fntype now gets the type of function pointed to. */
3681 fntype = TREE_TYPE (fntype);
3682 parm_types = TYPE_ARG_TYPES (fntype);
3684 if (params == NULL)
3686 allocated = make_tree_vector ();
3687 params = &allocated;
3690 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3691 complain);
3692 if (nargs < 0)
3693 return error_mark_node;
3695 argarray = (*params)->address ();
3697 /* Check for errors in format strings and inappropriately
3698 null parameters. */
3699 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
3700 nargs, argarray);
3702 ret = build_cxx_call (function, nargs, argarray, complain);
3704 if (warned_p)
3706 tree c = extract_call_expr (ret);
3707 if (TREE_CODE (c) == CALL_EXPR)
3708 TREE_NO_WARNING (c) = 1;
3711 if (allocated != NULL)
3712 release_tree_vector (allocated);
3714 return ret;
3717 /* Subroutine of convert_arguments.
3718 Print an error message about a wrong number of arguments. */
3720 static void
3721 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3723 if (fndecl)
3725 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3727 if (DECL_NAME (fndecl) == NULL_TREE
3728 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3729 error_at (loc,
3730 too_many_p
3731 ? G_("too many arguments to constructor %q#D")
3732 : G_("too few arguments to constructor %q#D"),
3733 fndecl);
3734 else
3735 error_at (loc,
3736 too_many_p
3737 ? G_("too many arguments to member function %q#D")
3738 : G_("too few arguments to member function %q#D"),
3739 fndecl);
3741 else
3742 error_at (loc,
3743 too_many_p
3744 ? G_("too many arguments to function %q#D")
3745 : G_("too few arguments to function %q#D"),
3746 fndecl);
3747 if (!DECL_IS_BUILTIN (fndecl))
3748 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3750 else
3752 if (c_dialect_objc () && objc_message_selector ())
3753 error_at (loc,
3754 too_many_p
3755 ? G_("too many arguments to method %q#D")
3756 : G_("too few arguments to method %q#D"),
3757 objc_message_selector ());
3758 else
3759 error_at (loc, too_many_p ? G_("too many arguments to function")
3760 : G_("too few arguments to function"));
3764 /* Convert the actual parameter expressions in the list VALUES to the
3765 types in the list TYPELIST. The converted expressions are stored
3766 back in the VALUES vector.
3767 If parmdecls is exhausted, or when an element has NULL as its type,
3768 perform the default conversions.
3770 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3772 This is also where warnings about wrong number of args are generated.
3774 Returns the actual number of arguments processed (which might be less
3775 than the length of the vector), or -1 on error.
3777 In C++, unspecified trailing parameters can be filled in with their
3778 default arguments, if such were specified. Do so here. */
3780 static int
3781 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3782 int flags, tsubst_flags_t complain)
3784 tree typetail;
3785 unsigned int i;
3787 /* Argument passing is always copy-initialization. */
3788 flags |= LOOKUP_ONLYCONVERTING;
3790 for (i = 0, typetail = typelist;
3791 i < vec_safe_length (*values);
3792 i++)
3794 tree type = typetail ? TREE_VALUE (typetail) : 0;
3795 tree val = (**values)[i];
3797 if (val == error_mark_node || type == error_mark_node)
3798 return -1;
3800 if (type == void_type_node)
3802 if (complain & tf_error)
3804 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3805 return i;
3807 else
3808 return -1;
3811 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3812 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3813 if (TREE_CODE (val) == NOP_EXPR
3814 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3815 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3816 val = TREE_OPERAND (val, 0);
3818 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3820 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3821 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3822 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3823 val = decay_conversion (val, complain);
3826 if (val == error_mark_node)
3827 return -1;
3829 if (type != 0)
3831 /* Formal parm type is specified by a function prototype. */
3832 tree parmval;
3834 if (!COMPLETE_TYPE_P (complete_type (type)))
3836 if (complain & tf_error)
3838 if (fndecl)
3839 error ("parameter %P of %qD has incomplete type %qT",
3840 i, fndecl, type);
3841 else
3842 error ("parameter %P has incomplete type %qT", i, type);
3844 parmval = error_mark_node;
3846 else
3848 parmval = convert_for_initialization
3849 (NULL_TREE, type, val, flags,
3850 ICR_ARGPASS, fndecl, i, complain);
3851 parmval = convert_for_arg_passing (type, parmval, complain);
3854 if (parmval == error_mark_node)
3855 return -1;
3857 (**values)[i] = parmval;
3859 else
3861 if (fndecl && magic_varargs_p (fndecl))
3862 /* Don't do ellipsis conversion for __built_in_constant_p
3863 as this will result in spurious errors for non-trivial
3864 types. */
3865 val = require_complete_type_sfinae (val, complain);
3866 else
3867 val = convert_arg_to_ellipsis (val, complain);
3869 (**values)[i] = val;
3872 if (typetail)
3873 typetail = TREE_CHAIN (typetail);
3876 if (typetail != 0 && typetail != void_list_node)
3878 /* See if there are default arguments that can be used. Because
3879 we hold default arguments in the FUNCTION_TYPE (which is so
3880 wrong), we can see default parameters here from deduced
3881 contexts (and via typeof) for indirect function calls.
3882 Fortunately we know whether we have a function decl to
3883 provide default arguments in a language conformant
3884 manner. */
3885 if (fndecl && TREE_PURPOSE (typetail)
3886 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3888 for (; typetail != void_list_node; ++i)
3890 /* After DR777, with explicit template args we can end up with a
3891 default argument followed by no default argument. */
3892 if (!TREE_PURPOSE (typetail))
3893 break;
3894 tree parmval
3895 = convert_default_arg (TREE_VALUE (typetail),
3896 TREE_PURPOSE (typetail),
3897 fndecl, i, complain);
3899 if (parmval == error_mark_node)
3900 return -1;
3902 vec_safe_push (*values, parmval);
3903 typetail = TREE_CHAIN (typetail);
3904 /* ends with `...'. */
3905 if (typetail == NULL_TREE)
3906 break;
3910 if (typetail && typetail != void_list_node)
3912 if (complain & tf_error)
3913 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3914 return -1;
3918 return (int) i;
3921 /* Build a binary-operation expression, after performing default
3922 conversions on the operands. CODE is the kind of expression to
3923 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3924 are the tree codes which correspond to ARG1 and ARG2 when issuing
3925 warnings about possibly misplaced parentheses. They may differ
3926 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3927 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3928 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3929 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3930 ARG2_CODE as ERROR_MARK. */
3932 tree
3933 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3934 enum tree_code arg1_code, tree arg2,
3935 enum tree_code arg2_code, tree *overload_p,
3936 tsubst_flags_t complain)
3938 tree orig_arg1;
3939 tree orig_arg2;
3940 tree expr;
3941 tree overload = NULL_TREE;
3943 orig_arg1 = arg1;
3944 orig_arg2 = arg2;
3946 if (processing_template_decl)
3948 if (type_dependent_expression_p (arg1)
3949 || type_dependent_expression_p (arg2))
3950 return build_min_nt_loc (loc, code, arg1, arg2);
3951 arg1 = build_non_dependent_expr (arg1);
3952 arg2 = build_non_dependent_expr (arg2);
3955 if (code == DOTSTAR_EXPR)
3956 expr = build_m_component_ref (arg1, arg2, complain);
3957 else
3958 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3959 &overload, complain);
3961 if (overload_p != NULL)
3962 *overload_p = overload;
3964 /* Check for cases such as x+y<<z which users are likely to
3965 misinterpret. But don't warn about obj << x + y, since that is a
3966 common idiom for I/O. */
3967 if (warn_parentheses
3968 && (complain & tf_warning)
3969 && !processing_template_decl
3970 && !error_operand_p (arg1)
3971 && !error_operand_p (arg2)
3972 && (code != LSHIFT_EXPR
3973 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3974 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3975 arg2_code, orig_arg2);
3977 if (processing_template_decl && expr != error_mark_node)
3979 if (overload != NULL_TREE)
3980 return (build_min_non_dep_op_overload
3981 (code, expr, overload, orig_arg1, orig_arg2));
3983 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3986 return expr;
3989 /* Build and return an ARRAY_REF expression. */
3991 tree
3992 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3993 tsubst_flags_t complain)
3995 tree orig_arg1 = arg1;
3996 tree orig_arg2 = arg2;
3997 tree expr;
3998 tree overload = NULL_TREE;
4000 if (processing_template_decl)
4002 if (type_dependent_expression_p (arg1)
4003 || type_dependent_expression_p (arg2))
4004 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
4005 NULL_TREE, NULL_TREE);
4006 arg1 = build_non_dependent_expr (arg1);
4007 arg2 = build_non_dependent_expr (arg2);
4010 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
4011 NULL_TREE, &overload, complain);
4013 if (processing_template_decl && expr != error_mark_node)
4015 if (overload != NULL_TREE)
4016 return (build_min_non_dep_op_overload
4017 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
4019 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
4020 NULL_TREE, NULL_TREE);
4022 return expr;
4025 /* Return whether OP is an expression of enum type cast to integer
4026 type. In C++ even unsigned enum types are cast to signed integer
4027 types. We do not want to issue warnings about comparisons between
4028 signed and unsigned types when one of the types is an enum type.
4029 Those warnings are always false positives in practice. */
4031 static bool
4032 enum_cast_to_int (tree op)
4034 if (CONVERT_EXPR_P (op)
4035 && TREE_TYPE (op) == integer_type_node
4036 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4037 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4038 return true;
4040 /* The cast may have been pushed into a COND_EXPR. */
4041 if (TREE_CODE (op) == COND_EXPR)
4042 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4043 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4045 return false;
4048 /* For the c-common bits. */
4049 tree
4050 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
4051 int /*convert_p*/)
4053 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
4056 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4057 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4059 static tree
4060 build_vec_cmp (tree_code code, tree type,
4061 tree arg0, tree arg1)
4063 tree zero_vec = build_zero_cst (type);
4064 tree minus_one_vec = build_minus_one_cst (type);
4065 tree cmp_type = build_same_sized_truth_vector_type(type);
4066 tree cmp = build2 (code, cmp_type, arg0, arg1);
4067 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4070 /* Possibly warn about an address never being NULL. */
4072 static void
4073 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4075 if (!warn_address
4076 || (complain & tf_warning) == 0
4077 || c_inhibit_evaluation_warnings != 0
4078 || TREE_NO_WARNING (op))
4079 return;
4081 tree cop = fold_non_dependent_expr (op);
4083 if (TREE_CODE (cop) == ADDR_EXPR
4084 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4085 && !TREE_NO_WARNING (cop))
4086 warning_at (location, OPT_Waddress, "the address of %qD will never "
4087 "be NULL", TREE_OPERAND (cop, 0));
4089 if (CONVERT_EXPR_P (op)
4090 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4092 tree inner_op = op;
4093 STRIP_NOPS (inner_op);
4095 if (DECL_P (inner_op))
4096 warning_at (location, OPT_Waddress,
4097 "the compiler can assume that the address of "
4098 "%qD will never be NULL", inner_op);
4102 /* Build a binary-operation expression without default conversions.
4103 CODE is the kind of expression to build.
4104 LOCATION is the location_t of the operator in the source code.
4105 This function differs from `build' in several ways:
4106 the data type of the result is computed and recorded in it,
4107 warnings are generated if arg data types are invalid,
4108 special handling for addition and subtraction of pointers is known,
4109 and some optimization is done (operations on narrow ints
4110 are done in the narrower type when that gives the same result).
4111 Constant folding is also done before the result is returned.
4113 Note that the operands will never have enumeral types
4114 because either they have just had the default conversions performed
4115 or they have both just been converted to some other type in which
4116 the arithmetic is to be done.
4118 C++: must do special pointer arithmetic when implementing
4119 multiple inheritance, and deal with pointer to member functions. */
4121 tree
4122 cp_build_binary_op (location_t location,
4123 enum tree_code code, tree orig_op0, tree orig_op1,
4124 tsubst_flags_t complain)
4126 tree op0, op1;
4127 enum tree_code code0, code1;
4128 tree type0, type1;
4129 const char *invalid_op_diag;
4131 /* Expression code to give to the expression when it is built.
4132 Normally this is CODE, which is what the caller asked for,
4133 but in some special cases we change it. */
4134 enum tree_code resultcode = code;
4136 /* Data type in which the computation is to be performed.
4137 In the simplest cases this is the common type of the arguments. */
4138 tree result_type = NULL_TREE;
4140 /* Nonzero means operands have already been type-converted
4141 in whatever way is necessary.
4142 Zero means they need to be converted to RESULT_TYPE. */
4143 int converted = 0;
4145 /* Nonzero means create the expression with this type, rather than
4146 RESULT_TYPE. */
4147 tree build_type = 0;
4149 /* Nonzero means after finally constructing the expression
4150 convert it to this type. */
4151 tree final_type = 0;
4153 tree result, result_ovl;
4155 /* Nonzero if this is an operation like MIN or MAX which can
4156 safely be computed in short if both args are promoted shorts.
4157 Also implies COMMON.
4158 -1 indicates a bitwise operation; this makes a difference
4159 in the exact conditions for when it is safe to do the operation
4160 in a narrower mode. */
4161 int shorten = 0;
4163 /* Nonzero if this is a comparison operation;
4164 if both args are promoted shorts, compare the original shorts.
4165 Also implies COMMON. */
4166 int short_compare = 0;
4168 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4169 int common = 0;
4171 /* True if both operands have arithmetic type. */
4172 bool arithmetic_types_p;
4174 /* Apply default conversions. */
4175 op0 = orig_op0;
4176 op1 = orig_op1;
4178 /* Remember whether we're doing / or %. */
4179 bool doing_div_or_mod = false;
4181 /* Remember whether we're doing << or >>. */
4182 bool doing_shift = false;
4184 /* Tree holding instrumentation expression. */
4185 tree instrument_expr = NULL_TREE;
4187 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4188 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4189 || code == TRUTH_XOR_EXPR)
4191 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4192 op0 = decay_conversion (op0, complain);
4193 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4194 op1 = decay_conversion (op1, complain);
4196 else
4198 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4199 op0 = cp_default_conversion (op0, complain);
4200 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4201 op1 = cp_default_conversion (op1, complain);
4204 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4205 STRIP_TYPE_NOPS (op0);
4206 STRIP_TYPE_NOPS (op1);
4208 /* DTRT if one side is an overloaded function, but complain about it. */
4209 if (type_unknown_p (op0))
4211 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4212 if (t != error_mark_node)
4214 if (complain & tf_error)
4215 permerror (input_location, "assuming cast to type %qT from overloaded function",
4216 TREE_TYPE (t));
4217 op0 = t;
4220 if (type_unknown_p (op1))
4222 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4223 if (t != error_mark_node)
4225 if (complain & tf_error)
4226 permerror (input_location, "assuming cast to type %qT from overloaded function",
4227 TREE_TYPE (t));
4228 op1 = t;
4232 type0 = TREE_TYPE (op0);
4233 type1 = TREE_TYPE (op1);
4235 /* The expression codes of the data types of the arguments tell us
4236 whether the arguments are integers, floating, pointers, etc. */
4237 code0 = TREE_CODE (type0);
4238 code1 = TREE_CODE (type1);
4240 /* If an error was already reported for one of the arguments,
4241 avoid reporting another error. */
4242 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4243 return error_mark_node;
4245 if ((invalid_op_diag
4246 = targetm.invalid_binary_op (code, type0, type1)))
4248 if (complain & tf_error)
4249 error (invalid_op_diag);
4250 return error_mark_node;
4253 /* Issue warnings about peculiar, but valid, uses of NULL. */
4254 if ((orig_op0 == null_node || orig_op1 == null_node)
4255 /* It's reasonable to use pointer values as operands of &&
4256 and ||, so NULL is no exception. */
4257 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4258 && ( /* Both are NULL (or 0) and the operation was not a
4259 comparison or a pointer subtraction. */
4260 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4261 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4262 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4263 || (!null_ptr_cst_p (orig_op0)
4264 && !TYPE_PTR_OR_PTRMEM_P (type0))
4265 || (!null_ptr_cst_p (orig_op1)
4266 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4267 && (complain & tf_warning))
4269 source_location loc =
4270 expansion_point_location_if_in_system_header (input_location);
4272 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4275 /* In case when one of the operands of the binary operation is
4276 a vector and another is a scalar -- convert scalar to vector. */
4277 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4279 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4280 complain & tf_error);
4282 switch (convert_flag)
4284 case stv_error:
4285 return error_mark_node;
4286 case stv_firstarg:
4288 op0 = convert (TREE_TYPE (type1), op0);
4289 op0 = save_expr (op0);
4290 op0 = build_vector_from_val (type1, op0);
4291 type0 = TREE_TYPE (op0);
4292 code0 = TREE_CODE (type0);
4293 converted = 1;
4294 break;
4296 case stv_secondarg:
4298 op1 = convert (TREE_TYPE (type0), op1);
4299 op1 = save_expr (op1);
4300 op1 = build_vector_from_val (type0, op1);
4301 type1 = TREE_TYPE (op1);
4302 code1 = TREE_CODE (type1);
4303 converted = 1;
4304 break;
4306 default:
4307 break;
4311 switch (code)
4313 case MINUS_EXPR:
4314 /* Subtraction of two similar pointers.
4315 We must subtract them as integers, then divide by object size. */
4316 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4317 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4318 TREE_TYPE (type1)))
4319 return pointer_diff (location, op0, op1,
4320 common_pointer_type (type0, type1), complain);
4321 /* In all other cases except pointer - int, the usual arithmetic
4322 rules apply. */
4323 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4325 common = 1;
4326 break;
4328 /* The pointer - int case is just like pointer + int; fall
4329 through. */
4330 gcc_fallthrough ();
4331 case PLUS_EXPR:
4332 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4333 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4335 tree ptr_operand;
4336 tree int_operand;
4337 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4338 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4339 if (processing_template_decl)
4341 result_type = TREE_TYPE (ptr_operand);
4342 break;
4344 return cp_pointer_int_sum (location, code,
4345 ptr_operand,
4346 int_operand,
4347 complain);
4349 common = 1;
4350 break;
4352 case MULT_EXPR:
4353 common = 1;
4354 break;
4356 case TRUNC_DIV_EXPR:
4357 case CEIL_DIV_EXPR:
4358 case FLOOR_DIV_EXPR:
4359 case ROUND_DIV_EXPR:
4360 case EXACT_DIV_EXPR:
4361 if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
4363 tree type0 = TREE_OPERAND (op0, 0);
4364 tree type1 = TREE_OPERAND (op1, 0);
4365 tree first_arg = type0;
4366 if (!TYPE_P (type0))
4367 type0 = TREE_TYPE (type0);
4368 if (!TYPE_P (type1))
4369 type1 = TREE_TYPE (type1);
4370 if (POINTER_TYPE_P (type0) && same_type_p (TREE_TYPE (type0), type1)
4371 && !(TREE_CODE (first_arg) == PARM_DECL
4372 && DECL_ARRAY_PARAMETER_P (first_arg)
4373 && warn_sizeof_array_argument)
4374 && (complain & tf_warning))
4375 if (warning_at (location, OPT_Wsizeof_pointer_div,
4376 "division %<sizeof (%T) / sizeof (%T)%> does "
4377 "not compute the number of array elements",
4378 type0, type1))
4379 if (DECL_P (first_arg))
4380 inform (DECL_SOURCE_LOCATION (first_arg),
4381 "first %<sizeof%> operand was declared here");
4384 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4385 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4386 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4387 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4389 enum tree_code tcode0 = code0, tcode1 = code1;
4390 tree cop1 = fold_non_dependent_expr (op1);
4391 doing_div_or_mod = true;
4392 warn_for_div_by_zero (location, cop1);
4394 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4395 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4396 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4397 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4399 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4400 resultcode = RDIV_EXPR;
4401 else
4402 /* When dividing two signed integers, we have to promote to int.
4403 unless we divide by a constant != -1. Note that default
4404 conversion will have been performed on the operands at this
4405 point, so we have to dig out the original type to find out if
4406 it was unsigned. */
4407 shorten = ((TREE_CODE (op0) == NOP_EXPR
4408 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4409 || (TREE_CODE (op1) == INTEGER_CST
4410 && ! integer_all_onesp (op1)));
4412 common = 1;
4414 break;
4416 case BIT_AND_EXPR:
4417 case BIT_IOR_EXPR:
4418 case BIT_XOR_EXPR:
4419 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4420 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4421 && !VECTOR_FLOAT_TYPE_P (type0)
4422 && !VECTOR_FLOAT_TYPE_P (type1)))
4423 shorten = -1;
4424 break;
4426 case TRUNC_MOD_EXPR:
4427 case FLOOR_MOD_EXPR:
4429 tree cop1 = fold_non_dependent_expr (op1);
4430 doing_div_or_mod = true;
4431 warn_for_div_by_zero (location, cop1);
4434 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4435 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4436 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4437 common = 1;
4438 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4440 /* Although it would be tempting to shorten always here, that loses
4441 on some targets, since the modulo instruction is undefined if the
4442 quotient can't be represented in the computation mode. We shorten
4443 only if unsigned or if dividing by something we know != -1. */
4444 shorten = ((TREE_CODE (op0) == NOP_EXPR
4445 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4446 || (TREE_CODE (op1) == INTEGER_CST
4447 && ! integer_all_onesp (op1)));
4448 common = 1;
4450 break;
4452 case TRUTH_ANDIF_EXPR:
4453 case TRUTH_ORIF_EXPR:
4454 case TRUTH_AND_EXPR:
4455 case TRUTH_OR_EXPR:
4456 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4458 if (!COMPARISON_CLASS_P (op1))
4459 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4460 build_zero_cst (type1), complain);
4461 if (code == TRUTH_ANDIF_EXPR)
4463 tree z = build_zero_cst (TREE_TYPE (op1));
4464 return build_conditional_expr (location, op0, op1, z, complain);
4466 else if (code == TRUTH_ORIF_EXPR)
4468 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4469 return build_conditional_expr (location, op0, m1, op1, complain);
4471 else
4472 gcc_unreachable ();
4474 if (VECTOR_TYPE_P (type0))
4476 if (!COMPARISON_CLASS_P (op0))
4477 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4478 build_zero_cst (type0), complain);
4479 if (!VECTOR_TYPE_P (type1))
4481 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4482 tree z = build_zero_cst (TREE_TYPE (op0));
4483 op1 = build_conditional_expr (location, op1, m1, z, complain);
4485 else if (!COMPARISON_CLASS_P (op1))
4486 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4487 build_zero_cst (type1), complain);
4489 if (code == TRUTH_ANDIF_EXPR)
4490 code = BIT_AND_EXPR;
4491 else if (code == TRUTH_ORIF_EXPR)
4492 code = BIT_IOR_EXPR;
4493 else
4494 gcc_unreachable ();
4496 return cp_build_binary_op (location, code, op0, op1, complain);
4499 result_type = boolean_type_node;
4500 break;
4502 /* Shift operations: result has same type as first operand;
4503 always convert second operand to int.
4504 Also set SHORT_SHIFT if shifting rightward. */
4506 case RSHIFT_EXPR:
4507 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4508 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4510 result_type = type0;
4511 converted = 1;
4513 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4514 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4515 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4516 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4518 result_type = type0;
4519 converted = 1;
4521 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4523 tree const_op1 = fold_non_dependent_expr (op1);
4524 if (TREE_CODE (const_op1) != INTEGER_CST)
4525 const_op1 = op1;
4526 result_type = type0;
4527 doing_shift = true;
4528 if (TREE_CODE (const_op1) == INTEGER_CST)
4530 if (tree_int_cst_lt (const_op1, integer_zero_node))
4532 if ((complain & tf_warning)
4533 && c_inhibit_evaluation_warnings == 0)
4534 warning (OPT_Wshift_count_negative,
4535 "right shift count is negative");
4537 else
4539 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4540 && (complain & tf_warning)
4541 && c_inhibit_evaluation_warnings == 0)
4542 warning (OPT_Wshift_count_overflow,
4543 "right shift count >= width of type");
4546 /* Avoid converting op1 to result_type later. */
4547 converted = 1;
4549 break;
4551 case LSHIFT_EXPR:
4552 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4553 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4555 result_type = type0;
4556 converted = 1;
4558 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4559 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4560 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4561 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4563 result_type = type0;
4564 converted = 1;
4566 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4568 tree const_op0 = fold_non_dependent_expr (op0);
4569 if (TREE_CODE (const_op0) != INTEGER_CST)
4570 const_op0 = op0;
4571 tree const_op1 = fold_non_dependent_expr (op1);
4572 if (TREE_CODE (const_op1) != INTEGER_CST)
4573 const_op1 = op1;
4574 result_type = type0;
4575 doing_shift = true;
4576 if (TREE_CODE (const_op0) == INTEGER_CST
4577 && tree_int_cst_sgn (const_op0) < 0
4578 && (complain & tf_warning)
4579 && c_inhibit_evaluation_warnings == 0)
4580 warning (OPT_Wshift_negative_value,
4581 "left shift of negative value");
4582 if (TREE_CODE (const_op1) == INTEGER_CST)
4584 if (tree_int_cst_lt (const_op1, integer_zero_node))
4586 if ((complain & tf_warning)
4587 && c_inhibit_evaluation_warnings == 0)
4588 warning (OPT_Wshift_count_negative,
4589 "left shift count is negative");
4591 else if (compare_tree_int (const_op1,
4592 TYPE_PRECISION (type0)) >= 0)
4594 if ((complain & tf_warning)
4595 && c_inhibit_evaluation_warnings == 0)
4596 warning (OPT_Wshift_count_overflow,
4597 "left shift count >= width of type");
4599 else if (TREE_CODE (const_op0) == INTEGER_CST
4600 && (complain & tf_warning))
4601 maybe_warn_shift_overflow (location, const_op0, const_op1);
4603 /* Avoid converting op1 to result_type later. */
4604 converted = 1;
4606 break;
4608 case RROTATE_EXPR:
4609 case LROTATE_EXPR:
4610 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4612 result_type = type0;
4613 if (TREE_CODE (op1) == INTEGER_CST)
4615 if (tree_int_cst_lt (op1, integer_zero_node))
4617 if (complain & tf_warning)
4618 warning (0, (code == LROTATE_EXPR)
4619 ? G_("left rotate count is negative")
4620 : G_("right rotate count is negative"));
4622 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4624 if (complain & tf_warning)
4625 warning (0, (code == LROTATE_EXPR)
4626 ? G_("left rotate count >= width of type")
4627 : G_("right rotate count >= width of type"));
4630 /* Convert the shift-count to an integer, regardless of
4631 size of value being shifted. */
4632 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4633 op1 = cp_convert (integer_type_node, op1, complain);
4635 break;
4637 case EQ_EXPR:
4638 case NE_EXPR:
4639 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4640 goto vector_compare;
4641 if ((complain & tf_warning)
4642 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4643 warning (OPT_Wfloat_equal,
4644 "comparing floating point with == or != is unsafe");
4645 if ((complain & tf_warning)
4646 && ((TREE_CODE (orig_op0) == STRING_CST
4647 && !integer_zerop (cp_fully_fold (op1)))
4648 || (TREE_CODE (orig_op1) == STRING_CST
4649 && !integer_zerop (cp_fully_fold (op0)))))
4650 warning (OPT_Waddress, "comparison with string literal results "
4651 "in unspecified behavior");
4653 build_type = boolean_type_node;
4654 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4655 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4656 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4657 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4658 short_compare = 1;
4659 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4660 && null_ptr_cst_p (orig_op1))
4661 /* Handle, eg, (void*)0 (c++/43906), and more. */
4662 || (code0 == POINTER_TYPE
4663 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4665 if (TYPE_PTR_P (type1))
4666 result_type = composite_pointer_type (type0, type1, op0, op1,
4667 CPO_COMPARISON, complain);
4668 else
4669 result_type = type0;
4671 if (char_type_p (TREE_TYPE (orig_op1))
4672 && warning (OPT_Wpointer_compare,
4673 "comparison between pointer and zero character "
4674 "constant"))
4675 inform (input_location,
4676 "did you mean to dereference the pointer?");
4677 warn_for_null_address (location, op0, complain);
4679 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4680 && null_ptr_cst_p (orig_op0))
4681 /* Handle, eg, (void*)0 (c++/43906), and more. */
4682 || (code1 == POINTER_TYPE
4683 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4685 if (TYPE_PTR_P (type0))
4686 result_type = composite_pointer_type (type0, type1, op0, op1,
4687 CPO_COMPARISON, complain);
4688 else
4689 result_type = type1;
4691 if (char_type_p (TREE_TYPE (orig_op0))
4692 && warning (OPT_Wpointer_compare,
4693 "comparison between pointer and zero character "
4694 "constant"))
4695 inform (input_location,
4696 "did you mean to dereference the pointer?");
4697 warn_for_null_address (location, op1, complain);
4699 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4700 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4701 result_type = composite_pointer_type (type0, type1, op0, op1,
4702 CPO_COMPARISON, complain);
4703 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4704 /* One of the operands must be of nullptr_t type. */
4705 result_type = TREE_TYPE (nullptr_node);
4706 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4708 result_type = type0;
4709 if (complain & tf_error)
4710 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4711 else
4712 return error_mark_node;
4714 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4716 result_type = type1;
4717 if (complain & tf_error)
4718 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4719 else
4720 return error_mark_node;
4722 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
4724 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4725 == ptrmemfunc_vbit_in_delta)
4727 tree pfn0, delta0, e1, e2;
4729 if (TREE_SIDE_EFFECTS (op0))
4730 op0 = save_expr (op0);
4732 pfn0 = pfn_from_ptrmemfunc (op0);
4733 delta0 = delta_from_ptrmemfunc (op0);
4734 e1 = cp_build_binary_op (location,
4735 EQ_EXPR,
4736 pfn0,
4737 build_zero_cst (TREE_TYPE (pfn0)),
4738 complain);
4739 e2 = cp_build_binary_op (location,
4740 BIT_AND_EXPR,
4741 delta0,
4742 integer_one_node,
4743 complain);
4745 if (complain & tf_warning)
4746 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4748 e2 = cp_build_binary_op (location,
4749 EQ_EXPR, e2, integer_zero_node,
4750 complain);
4751 op0 = cp_build_binary_op (location,
4752 TRUTH_ANDIF_EXPR, e1, e2,
4753 complain);
4754 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4756 else
4758 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4759 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4761 result_type = TREE_TYPE (op0);
4763 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
4764 return cp_build_binary_op (location, code, op1, op0, complain);
4765 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4767 tree type;
4768 /* E will be the final comparison. */
4769 tree e;
4770 /* E1 and E2 are for scratch. */
4771 tree e1;
4772 tree e2;
4773 tree pfn0;
4774 tree pfn1;
4775 tree delta0;
4776 tree delta1;
4778 type = composite_pointer_type (type0, type1, op0, op1,
4779 CPO_COMPARISON, complain);
4781 if (!same_type_p (TREE_TYPE (op0), type))
4782 op0 = cp_convert_and_check (type, op0, complain);
4783 if (!same_type_p (TREE_TYPE (op1), type))
4784 op1 = cp_convert_and_check (type, op1, complain);
4786 if (op0 == error_mark_node || op1 == error_mark_node)
4787 return error_mark_node;
4789 if (TREE_SIDE_EFFECTS (op0))
4790 op0 = save_expr (op0);
4791 if (TREE_SIDE_EFFECTS (op1))
4792 op1 = save_expr (op1);
4794 pfn0 = pfn_from_ptrmemfunc (op0);
4795 pfn0 = cp_fully_fold (pfn0);
4796 /* Avoid -Waddress warnings (c++/64877). */
4797 if (TREE_CODE (pfn0) == ADDR_EXPR)
4798 TREE_NO_WARNING (pfn0) = 1;
4799 pfn1 = pfn_from_ptrmemfunc (op1);
4800 pfn1 = cp_fully_fold (pfn1);
4801 delta0 = delta_from_ptrmemfunc (op0);
4802 delta1 = delta_from_ptrmemfunc (op1);
4803 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4804 == ptrmemfunc_vbit_in_delta)
4806 /* We generate:
4808 (op0.pfn == op1.pfn
4809 && ((op0.delta == op1.delta)
4810 || (!op0.pfn && op0.delta & 1 == 0
4811 && op1.delta & 1 == 0))
4813 The reason for the `!op0.pfn' bit is that a NULL
4814 pointer-to-member is any member with a zero PFN and
4815 LSB of the DELTA field is 0. */
4817 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4818 delta0,
4819 integer_one_node,
4820 complain);
4821 e1 = cp_build_binary_op (location,
4822 EQ_EXPR, e1, integer_zero_node,
4823 complain);
4824 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4825 delta1,
4826 integer_one_node,
4827 complain);
4828 e2 = cp_build_binary_op (location,
4829 EQ_EXPR, e2, integer_zero_node,
4830 complain);
4831 e1 = cp_build_binary_op (location,
4832 TRUTH_ANDIF_EXPR, e2, e1,
4833 complain);
4834 e2 = cp_build_binary_op (location, EQ_EXPR,
4835 pfn0,
4836 build_zero_cst (TREE_TYPE (pfn0)),
4837 complain);
4838 e2 = cp_build_binary_op (location,
4839 TRUTH_ANDIF_EXPR, e2, e1, complain);
4840 e1 = cp_build_binary_op (location,
4841 EQ_EXPR, delta0, delta1, complain);
4842 e1 = cp_build_binary_op (location,
4843 TRUTH_ORIF_EXPR, e1, e2, complain);
4845 else
4847 /* We generate:
4849 (op0.pfn == op1.pfn
4850 && (!op0.pfn || op0.delta == op1.delta))
4852 The reason for the `!op0.pfn' bit is that a NULL
4853 pointer-to-member is any member with a zero PFN; the
4854 DELTA field is unspecified. */
4856 e1 = cp_build_binary_op (location,
4857 EQ_EXPR, delta0, delta1, complain);
4858 e2 = cp_build_binary_op (location,
4859 EQ_EXPR,
4860 pfn0,
4861 build_zero_cst (TREE_TYPE (pfn0)),
4862 complain);
4863 e1 = cp_build_binary_op (location,
4864 TRUTH_ORIF_EXPR, e1, e2, complain);
4866 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4867 e = cp_build_binary_op (location,
4868 TRUTH_ANDIF_EXPR, e2, e1, complain);
4869 if (code == EQ_EXPR)
4870 return e;
4871 return cp_build_binary_op (location,
4872 EQ_EXPR, e, integer_zero_node, complain);
4874 else
4876 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4877 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4878 type1));
4879 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4880 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4881 type0));
4884 break;
4886 case MAX_EXPR:
4887 case MIN_EXPR:
4888 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4889 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4890 shorten = 1;
4891 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4892 result_type = composite_pointer_type (type0, type1, op0, op1,
4893 CPO_COMPARISON, complain);
4894 break;
4896 case LE_EXPR:
4897 case GE_EXPR:
4898 case LT_EXPR:
4899 case GT_EXPR:
4900 if (TREE_CODE (orig_op0) == STRING_CST
4901 || TREE_CODE (orig_op1) == STRING_CST)
4903 if (complain & tf_warning)
4904 warning (OPT_Waddress, "comparison with string literal results "
4905 "in unspecified behavior");
4908 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4910 vector_compare:
4911 tree intt;
4912 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4913 TREE_TYPE (type1))
4914 && !vector_types_compatible_elements_p (type0, type1))
4916 if (complain & tf_error)
4918 error_at (location, "comparing vectors with different "
4919 "element types");
4920 inform (location, "operand types are %qT and %qT",
4921 type0, type1);
4923 return error_mark_node;
4926 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4928 if (complain & tf_error)
4930 error_at (location, "comparing vectors with different "
4931 "number of elements");
4932 inform (location, "operand types are %qT and %qT",
4933 type0, type1);
4935 return error_mark_node;
4938 /* It's not precisely specified how the usual arithmetic
4939 conversions apply to the vector types. Here, we use
4940 the unsigned type if one of the operands is signed and
4941 the other one is unsigned. */
4942 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
4944 if (!TYPE_UNSIGNED (type0))
4945 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
4946 else
4947 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
4948 warning_at (location, OPT_Wsign_compare, "comparison between "
4949 "types %qT and %qT", type0, type1);
4952 /* Always construct signed integer vector type. */
4953 intt = c_common_type_for_size (GET_MODE_BITSIZE
4954 (TYPE_MODE (TREE_TYPE (type0))), 0);
4955 if (!intt)
4957 if (complain & tf_error)
4958 error_at (location, "could not find an integer type "
4959 "of the same size as %qT", TREE_TYPE (type0));
4960 return error_mark_node;
4962 result_type = build_opaque_vector_type (intt,
4963 TYPE_VECTOR_SUBPARTS (type0));
4964 converted = 1;
4965 return build_vec_cmp (resultcode, result_type, op0, op1);
4967 build_type = boolean_type_node;
4968 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4969 || code0 == ENUMERAL_TYPE)
4970 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4971 || code1 == ENUMERAL_TYPE))
4972 short_compare = 1;
4973 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4974 result_type = composite_pointer_type (type0, type1, op0, op1,
4975 CPO_COMPARISON, complain);
4976 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
4978 result_type = type0;
4979 if (extra_warnings && (complain & tf_warning))
4980 warning (OPT_Wextra,
4981 "ordered comparison of pointer with integer zero");
4983 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
4985 result_type = type1;
4986 if (extra_warnings && (complain & tf_warning))
4987 warning (OPT_Wextra,
4988 "ordered comparison of pointer with integer zero");
4990 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
4991 /* One of the operands must be of nullptr_t type. */
4992 result_type = TREE_TYPE (nullptr_node);
4993 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4995 result_type = type0;
4996 if (complain & tf_error)
4997 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4998 else
4999 return error_mark_node;
5001 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
5003 result_type = type1;
5004 if (complain & tf_error)
5005 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5006 else
5007 return error_mark_node;
5009 break;
5011 case UNORDERED_EXPR:
5012 case ORDERED_EXPR:
5013 case UNLT_EXPR:
5014 case UNLE_EXPR:
5015 case UNGT_EXPR:
5016 case UNGE_EXPR:
5017 case UNEQ_EXPR:
5018 build_type = integer_type_node;
5019 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
5021 if (complain & tf_error)
5022 error ("unordered comparison on non-floating point argument");
5023 return error_mark_node;
5025 common = 1;
5026 break;
5028 default:
5029 break;
5032 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
5033 || code0 == ENUMERAL_TYPE)
5034 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5035 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
5036 arithmetic_types_p = 1;
5037 else
5039 arithmetic_types_p = 0;
5040 /* Vector arithmetic is only allowed when both sides are vectors. */
5041 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
5043 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
5044 || !vector_types_compatible_elements_p (type0, type1))
5046 if (complain & tf_error)
5048 /* "location" already embeds the locations of the
5049 operands, so we don't need to add them separately
5050 to richloc. */
5051 rich_location richloc (line_table, location);
5052 binary_op_error (&richloc, code, type0, type1);
5054 return error_mark_node;
5056 arithmetic_types_p = 1;
5059 /* Determine the RESULT_TYPE, if it is not already known. */
5060 if (!result_type
5061 && arithmetic_types_p
5062 && (shorten || common || short_compare))
5064 result_type = cp_common_type (type0, type1);
5065 if (complain & tf_warning)
5066 do_warn_double_promotion (result_type, type0, type1,
5067 "implicit conversion from %qH to %qI "
5068 "to match other operand of binary "
5069 "expression",
5070 location);
5073 if (!result_type)
5075 if (complain & tf_error)
5076 error_at (location,
5077 "invalid operands of types %qT and %qT to binary %qO",
5078 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
5079 return error_mark_node;
5082 /* If we're in a template, the only thing we need to know is the
5083 RESULT_TYPE. */
5084 if (processing_template_decl)
5086 /* Since the middle-end checks the type when doing a build2, we
5087 need to build the tree in pieces. This built tree will never
5088 get out of the front-end as we replace it when instantiating
5089 the template. */
5090 tree tmp = build2 (resultcode,
5091 build_type ? build_type : result_type,
5092 NULL_TREE, op1);
5093 TREE_OPERAND (tmp, 0) = op0;
5094 return tmp;
5097 /* Remember the original type; RESULT_TYPE might be changed later on
5098 by shorten_binary_op. */
5099 tree orig_type = result_type;
5101 if (arithmetic_types_p)
5103 bool first_complex = (code0 == COMPLEX_TYPE);
5104 bool second_complex = (code1 == COMPLEX_TYPE);
5105 int none_complex = (!first_complex && !second_complex);
5107 /* Adapted from patch for c/24581. */
5108 if (first_complex != second_complex
5109 && (code == PLUS_EXPR
5110 || code == MINUS_EXPR
5111 || code == MULT_EXPR
5112 || (code == TRUNC_DIV_EXPR && first_complex))
5113 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5114 && flag_signed_zeros)
5116 /* An operation on mixed real/complex operands must be
5117 handled specially, but the language-independent code can
5118 more easily optimize the plain complex arithmetic if
5119 -fno-signed-zeros. */
5120 tree real_type = TREE_TYPE (result_type);
5121 tree real, imag;
5122 if (first_complex)
5124 if (TREE_TYPE (op0) != result_type)
5125 op0 = cp_convert_and_check (result_type, op0, complain);
5126 if (TREE_TYPE (op1) != real_type)
5127 op1 = cp_convert_and_check (real_type, op1, complain);
5129 else
5131 if (TREE_TYPE (op0) != real_type)
5132 op0 = cp_convert_and_check (real_type, op0, complain);
5133 if (TREE_TYPE (op1) != result_type)
5134 op1 = cp_convert_and_check (result_type, op1, complain);
5136 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5137 return error_mark_node;
5138 if (first_complex)
5140 op0 = save_expr (op0);
5141 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5142 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
5143 switch (code)
5145 case MULT_EXPR:
5146 case TRUNC_DIV_EXPR:
5147 op1 = save_expr (op1);
5148 imag = build2 (resultcode, real_type, imag, op1);
5149 /* Fall through. */
5150 case PLUS_EXPR:
5151 case MINUS_EXPR:
5152 real = build2 (resultcode, real_type, real, op1);
5153 break;
5154 default:
5155 gcc_unreachable();
5158 else
5160 op1 = save_expr (op1);
5161 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5162 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
5163 switch (code)
5165 case MULT_EXPR:
5166 op0 = save_expr (op0);
5167 imag = build2 (resultcode, real_type, op0, imag);
5168 /* Fall through. */
5169 case PLUS_EXPR:
5170 real = build2 (resultcode, real_type, op0, real);
5171 break;
5172 case MINUS_EXPR:
5173 real = build2 (resultcode, real_type, op0, real);
5174 imag = build1 (NEGATE_EXPR, real_type, imag);
5175 break;
5176 default:
5177 gcc_unreachable();
5180 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5181 return result;
5184 /* For certain operations (which identify themselves by shorten != 0)
5185 if both args were extended from the same smaller type,
5186 do the arithmetic in that type and then extend.
5188 shorten !=0 and !=1 indicates a bitwise operation.
5189 For them, this optimization is safe only if
5190 both args are zero-extended or both are sign-extended.
5191 Otherwise, we might change the result.
5192 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5193 but calculated in (unsigned short) it would be (unsigned short)-1. */
5195 if (shorten && none_complex)
5197 final_type = result_type;
5198 result_type = shorten_binary_op (result_type, op0, op1,
5199 shorten == -1);
5202 /* Comparison operations are shortened too but differently.
5203 They identify themselves by setting short_compare = 1. */
5205 if (short_compare)
5207 /* We call shorten_compare only for diagnostic-reason. */
5208 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5209 xresult_type = result_type;
5210 enum tree_code xresultcode = resultcode;
5211 shorten_compare (location, &xop0, &xop1, &xresult_type,
5212 &xresultcode);
5215 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5216 && warn_sign_compare
5217 /* Do not warn until the template is instantiated; we cannot
5218 bound the ranges of the arguments until that point. */
5219 && !processing_template_decl
5220 && (complain & tf_warning)
5221 && c_inhibit_evaluation_warnings == 0
5222 /* Even unsigned enum types promote to signed int. We don't
5223 want to issue -Wsign-compare warnings for this case. */
5224 && !enum_cast_to_int (orig_op0)
5225 && !enum_cast_to_int (orig_op1))
5227 tree oop0 = maybe_constant_value (orig_op0);
5228 tree oop1 = maybe_constant_value (orig_op1);
5230 if (TREE_CODE (oop0) != INTEGER_CST)
5231 oop0 = cp_fully_fold (orig_op0);
5232 if (TREE_CODE (oop1) != INTEGER_CST)
5233 oop1 = cp_fully_fold (orig_op1);
5234 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5235 result_type, resultcode);
5239 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5240 Then the expression will be built.
5241 It will be given type FINAL_TYPE if that is nonzero;
5242 otherwise, it will be given type RESULT_TYPE. */
5243 if (! converted)
5245 if (TREE_TYPE (op0) != result_type)
5246 op0 = cp_convert_and_check (result_type, op0, complain);
5247 if (TREE_TYPE (op1) != result_type)
5248 op1 = cp_convert_and_check (result_type, op1, complain);
5250 if (op0 == error_mark_node || op1 == error_mark_node)
5251 return error_mark_node;
5254 if (build_type == NULL_TREE)
5255 build_type = result_type;
5257 if (sanitize_flags_p ((SANITIZE_SHIFT
5258 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5259 && current_function_decl != NULL_TREE
5260 && !processing_template_decl
5261 && (doing_div_or_mod || doing_shift))
5263 /* OP0 and/or OP1 might have side-effects. */
5264 op0 = cp_save_expr (op0);
5265 op1 = cp_save_expr (op1);
5266 op0 = fold_non_dependent_expr (op0);
5267 op1 = fold_non_dependent_expr (op1);
5268 if (doing_div_or_mod
5269 && sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
5271 /* For diagnostics we want to use the promoted types without
5272 shorten_binary_op. So convert the arguments to the
5273 original result_type. */
5274 tree cop0 = op0;
5275 tree cop1 = op1;
5276 if (TREE_TYPE (cop0) != orig_type)
5277 cop0 = cp_convert (orig_type, op0, complain);
5278 if (TREE_TYPE (cop1) != orig_type)
5279 cop1 = cp_convert (orig_type, op1, complain);
5280 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5282 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
5283 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5286 result = build2_loc (location, resultcode, build_type, op0, op1);
5287 if (final_type != 0)
5288 result = cp_convert (final_type, result, complain);
5290 if (instrument_expr != NULL)
5291 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5292 instrument_expr, result);
5294 if (!processing_template_decl)
5296 op0 = cp_fully_fold (op0);
5297 /* Only consider the second argument if the first isn't overflowed. */
5298 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5299 return result;
5300 op1 = cp_fully_fold (op1);
5301 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5302 return result;
5304 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5305 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5306 return result;
5308 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5309 if (TREE_OVERFLOW_P (result_ovl))
5310 overflow_warning (location, result_ovl);
5312 return result;
5315 /* Build a VEC_PERM_EXPR.
5316 This is a simple wrapper for c_build_vec_perm_expr. */
5317 tree
5318 build_x_vec_perm_expr (location_t loc,
5319 tree arg0, tree arg1, tree arg2,
5320 tsubst_flags_t complain)
5322 tree orig_arg0 = arg0;
5323 tree orig_arg1 = arg1;
5324 tree orig_arg2 = arg2;
5325 if (processing_template_decl)
5327 if (type_dependent_expression_p (arg0)
5328 || type_dependent_expression_p (arg1)
5329 || type_dependent_expression_p (arg2))
5330 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5331 arg0 = build_non_dependent_expr (arg0);
5332 if (arg1)
5333 arg1 = build_non_dependent_expr (arg1);
5334 arg2 = build_non_dependent_expr (arg2);
5336 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5337 if (processing_template_decl && exp != error_mark_node)
5338 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5339 orig_arg1, orig_arg2);
5340 return exp;
5343 /* Return a tree for the sum or difference (RESULTCODE says which)
5344 of pointer PTROP and integer INTOP. */
5346 static tree
5347 cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5348 tree intop, tsubst_flags_t complain)
5350 tree res_type = TREE_TYPE (ptrop);
5352 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5353 in certain circumstance (when it's valid to do so). So we need
5354 to make sure it's complete. We don't need to check here, if we
5355 can actually complete it at all, as those checks will be done in
5356 pointer_int_sum() anyway. */
5357 complete_type (TREE_TYPE (res_type));
5359 return pointer_int_sum (loc, resultcode, ptrop,
5360 intop, complain & tf_warning_or_error);
5363 /* Return a tree for the difference of pointers OP0 and OP1.
5364 The resulting tree has type int. */
5366 static tree
5367 pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5368 tsubst_flags_t complain)
5370 tree result;
5371 tree restype = ptrdiff_type_node;
5372 tree target_type = TREE_TYPE (ptrtype);
5374 if (!complete_type_or_else (target_type, NULL_TREE))
5375 return error_mark_node;
5377 if (VOID_TYPE_P (target_type))
5379 if (complain & tf_error)
5380 permerror (loc, "ISO C++ forbids using pointer of "
5381 "type %<void *%> in subtraction");
5382 else
5383 return error_mark_node;
5385 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5387 if (complain & tf_error)
5388 permerror (loc, "ISO C++ forbids using pointer to "
5389 "a function in subtraction");
5390 else
5391 return error_mark_node;
5393 if (TREE_CODE (target_type) == METHOD_TYPE)
5395 if (complain & tf_error)
5396 permerror (loc, "ISO C++ forbids using pointer to "
5397 "a method in subtraction");
5398 else
5399 return error_mark_node;
5402 /* First do the subtraction as integers;
5403 then drop through to build the divide operator. */
5405 op0 = cp_build_binary_op (loc,
5406 MINUS_EXPR,
5407 cp_convert (restype, op0, complain),
5408 cp_convert (restype, op1, complain),
5409 complain);
5411 /* This generates an error if op1 is a pointer to an incomplete type. */
5412 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5414 if (complain & tf_error)
5415 error_at (loc, "invalid use of a pointer to an incomplete type in "
5416 "pointer arithmetic");
5417 else
5418 return error_mark_node;
5421 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5423 if (complain & tf_error)
5424 error_at (loc, "arithmetic on pointer to an empty aggregate");
5425 else
5426 return error_mark_node;
5429 op1 = (TYPE_PTROB_P (ptrtype)
5430 ? size_in_bytes_loc (loc, target_type)
5431 : integer_one_node);
5433 /* Do the division. */
5435 result = build2_loc (loc, EXACT_DIV_EXPR, restype, op0,
5436 cp_convert (restype, op1, complain));
5437 return result;
5440 /* Construct and perhaps optimize a tree representation
5441 for a unary operation. CODE, a tree_code, specifies the operation
5442 and XARG is the operand. */
5444 tree
5445 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5446 tsubst_flags_t complain)
5448 tree orig_expr = xarg;
5449 tree exp;
5450 int ptrmem = 0;
5451 tree overload = NULL_TREE;
5453 if (processing_template_decl)
5455 if (type_dependent_expression_p (xarg))
5456 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5458 xarg = build_non_dependent_expr (xarg);
5461 exp = NULL_TREE;
5463 /* [expr.unary.op] says:
5465 The address of an object of incomplete type can be taken.
5467 (And is just the ordinary address operator, not an overloaded
5468 "operator &".) However, if the type is a template
5469 specialization, we must complete the type at this point so that
5470 an overloaded "operator &" will be available if required. */
5471 if (code == ADDR_EXPR
5472 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5473 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5474 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5475 || (TREE_CODE (xarg) == OFFSET_REF)))
5476 /* Don't look for a function. */;
5477 else
5478 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5479 NULL_TREE, &overload, complain);
5481 if (!exp && code == ADDR_EXPR)
5483 if (is_overloaded_fn (xarg))
5485 tree fn = get_first_fn (xarg);
5486 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5488 if (complain & tf_error)
5489 error (DECL_CONSTRUCTOR_P (fn)
5490 ? G_("taking address of constructor %qD")
5491 : G_("taking address of destructor %qD"),
5492 fn);
5493 return error_mark_node;
5497 /* A pointer to member-function can be formed only by saying
5498 &X::mf. */
5499 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5500 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5502 if (TREE_CODE (xarg) != OFFSET_REF
5503 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5505 if (complain & tf_error)
5507 error ("invalid use of %qE to form a "
5508 "pointer-to-member-function", xarg.get_value ());
5509 if (TREE_CODE (xarg) != OFFSET_REF)
5510 inform (input_location, " a qualified-id is required");
5512 return error_mark_node;
5514 else
5516 if (complain & tf_error)
5517 error ("parentheses around %qE cannot be used to form a"
5518 " pointer-to-member-function",
5519 xarg.get_value ());
5520 else
5521 return error_mark_node;
5522 PTRMEM_OK_P (xarg) = 1;
5526 if (TREE_CODE (xarg) == OFFSET_REF)
5528 ptrmem = PTRMEM_OK_P (xarg);
5530 if (!ptrmem && !flag_ms_extensions
5531 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5533 /* A single non-static member, make sure we don't allow a
5534 pointer-to-member. */
5535 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5536 TREE_OPERAND (xarg, 0),
5537 ovl_make (TREE_OPERAND (xarg, 1)));
5538 PTRMEM_OK_P (xarg) = ptrmem;
5542 exp = cp_build_addr_expr_strict (xarg, complain);
5545 if (processing_template_decl && exp != error_mark_node)
5547 if (overload != NULL_TREE)
5548 return (build_min_non_dep_op_overload
5549 (code, exp, overload, orig_expr, integer_zero_node));
5551 exp = build_min_non_dep (code, exp, orig_expr,
5552 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5554 if (TREE_CODE (exp) == ADDR_EXPR)
5555 PTRMEM_OK_P (exp) = ptrmem;
5556 return exp;
5559 /* Construct and perhaps optimize a tree representation
5560 for __builtin_addressof operation. ARG specifies the operand. */
5562 tree
5563 cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5565 tree orig_expr = arg;
5567 if (processing_template_decl)
5569 if (type_dependent_expression_p (arg))
5570 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5572 arg = build_non_dependent_expr (arg);
5575 tree exp = cp_build_addr_expr_strict (arg, complain);
5577 if (processing_template_decl && exp != error_mark_node)
5578 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5579 return exp;
5582 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5583 constants, where a null value is represented by an INTEGER_CST of
5584 -1. */
5586 tree
5587 cp_truthvalue_conversion (tree expr)
5589 tree type = TREE_TYPE (expr);
5590 if (TYPE_PTR_OR_PTRMEM_P (type)
5591 /* Avoid ICE on invalid use of non-static member function. */
5592 || TREE_CODE (expr) == FUNCTION_DECL)
5593 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, 1);
5594 else
5595 return c_common_truthvalue_conversion (input_location, expr);
5598 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5600 tree
5601 condition_conversion (tree expr)
5603 tree t;
5604 if (processing_template_decl)
5605 return expr;
5606 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5607 tf_warning_or_error, LOOKUP_NORMAL);
5608 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5609 return t;
5612 /* Returns the address of T. This function will fold away
5613 ADDR_EXPR of INDIRECT_REF. */
5615 tree
5616 build_address (tree t)
5618 if (error_operand_p (t) || !cxx_mark_addressable (t))
5619 return error_mark_node;
5620 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5621 t = build_fold_addr_expr (t);
5622 if (TREE_CODE (t) != ADDR_EXPR)
5623 t = rvalue (t);
5624 return t;
5627 /* Return a NOP_EXPR converting EXPR to TYPE. */
5629 tree
5630 build_nop (tree type, tree expr)
5632 if (type == error_mark_node || error_operand_p (expr))
5633 return expr;
5634 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5637 /* Take the address of ARG, whatever that means under C++ semantics.
5638 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5639 and class rvalues as well.
5641 Nothing should call this function directly; instead, callers should use
5642 cp_build_addr_expr or cp_build_addr_expr_strict. */
5644 static tree
5645 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5647 tree argtype;
5648 tree val;
5650 if (!arg || error_operand_p (arg))
5651 return error_mark_node;
5653 arg = mark_lvalue_use (arg);
5654 argtype = lvalue_type (arg);
5656 gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
5658 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5659 && !really_overloaded_fn (arg))
5661 /* They're trying to take the address of a unique non-static
5662 member function. This is ill-formed (except in MS-land),
5663 but let's try to DTRT.
5664 Note: We only handle unique functions here because we don't
5665 want to complain if there's a static overload; non-unique
5666 cases will be handled by instantiate_type. But we need to
5667 handle this case here to allow casts on the resulting PMF.
5668 We could defer this in non-MS mode, but it's easier to give
5669 a useful error here. */
5671 /* Inside constant member functions, the `this' pointer
5672 contains an extra const qualifier. TYPE_MAIN_VARIANT
5673 is used here to remove this const from the diagnostics
5674 and the created OFFSET_REF. */
5675 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5676 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5677 if (!mark_used (fn, complain) && !(complain & tf_error))
5678 return error_mark_node;
5680 if (! flag_ms_extensions)
5682 tree name = DECL_NAME (fn);
5683 if (!(complain & tf_error))
5684 return error_mark_node;
5685 else if (current_class_type
5686 && TREE_OPERAND (arg, 0) == current_class_ref)
5687 /* An expression like &memfn. */
5688 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5689 " or parenthesized non-static member function to form"
5690 " a pointer to member function. Say %<&%T::%D%>",
5691 base, name);
5692 else
5693 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5694 " function to form a pointer to member function."
5695 " Say %<&%T::%D%>",
5696 base, name);
5698 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5701 /* Uninstantiated types are all functions. Taking the
5702 address of a function is a no-op, so just return the
5703 argument. */
5704 if (type_unknown_p (arg))
5705 return build1 (ADDR_EXPR, unknown_type_node, arg);
5707 if (TREE_CODE (arg) == OFFSET_REF)
5708 /* We want a pointer to member; bypass all the code for actually taking
5709 the address of something. */
5710 goto offset_ref;
5712 /* Anything not already handled and not a true memory reference
5713 is an error. */
5714 if (TREE_CODE (argtype) != FUNCTION_TYPE
5715 && TREE_CODE (argtype) != METHOD_TYPE)
5717 cp_lvalue_kind kind = lvalue_kind (arg);
5718 if (kind == clk_none)
5720 if (complain & tf_error)
5721 lvalue_error (input_location, lv_addressof);
5722 return error_mark_node;
5724 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5726 if (!(complain & tf_error))
5727 return error_mark_node;
5728 if (kind & clk_class)
5729 /* Make this a permerror because we used to accept it. */
5730 permerror (input_location, "taking address of temporary");
5731 else
5732 error ("taking address of xvalue (rvalue reference)");
5736 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5738 tree type = build_pointer_type (TREE_TYPE (argtype));
5739 arg = build1 (CONVERT_EXPR, type, arg);
5740 return arg;
5742 else if (pedantic && DECL_MAIN_P (arg))
5744 /* ARM $3.4 */
5745 /* Apparently a lot of autoconf scripts for C++ packages do this,
5746 so only complain if -Wpedantic. */
5747 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5748 pedwarn (input_location, OPT_Wpedantic,
5749 "ISO C++ forbids taking address of function %<::main%>");
5750 else if (flag_pedantic_errors)
5751 return error_mark_node;
5754 /* Let &* cancel out to simplify resulting code. */
5755 if (INDIRECT_REF_P (arg))
5757 arg = TREE_OPERAND (arg, 0);
5758 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5760 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5761 arg = build1 (CONVERT_EXPR, type, arg);
5763 else
5764 /* Don't let this be an lvalue. */
5765 arg = rvalue (arg);
5766 return arg;
5769 /* ??? Cope with user tricks that amount to offsetof. */
5770 if (TREE_CODE (argtype) != FUNCTION_TYPE
5771 && TREE_CODE (argtype) != METHOD_TYPE
5772 && argtype != unknown_type_node
5773 && (val = get_base_address (arg))
5774 && COMPLETE_TYPE_P (TREE_TYPE (val))
5775 && INDIRECT_REF_P (val)
5776 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5778 tree type = build_pointer_type (argtype);
5779 return fold_convert (type, fold_offsetof_1 (arg));
5782 /* Handle complex lvalues (when permitted)
5783 by reduction to simpler cases. */
5784 val = unary_complex_lvalue (ADDR_EXPR, arg);
5785 if (val != 0)
5786 return val;
5788 switch (TREE_CODE (arg))
5790 CASE_CONVERT:
5791 case FLOAT_EXPR:
5792 case FIX_TRUNC_EXPR:
5793 /* We should have handled this above in the lvalue_kind check. */
5794 gcc_unreachable ();
5795 break;
5797 case BASELINK:
5798 arg = BASELINK_FUNCTIONS (arg);
5799 /* Fall through. */
5801 case OVERLOAD:
5802 arg = OVL_FIRST (arg);
5803 break;
5805 case OFFSET_REF:
5806 offset_ref:
5807 /* Turn a reference to a non-static data member into a
5808 pointer-to-member. */
5810 tree type;
5811 tree t;
5813 gcc_assert (PTRMEM_OK_P (arg));
5815 t = TREE_OPERAND (arg, 1);
5816 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5818 if (complain & tf_error)
5819 error ("cannot create pointer to reference member %qD", t);
5820 return error_mark_node;
5823 type = build_ptrmem_type (context_for_name_lookup (t),
5824 TREE_TYPE (t));
5825 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5826 return t;
5829 default:
5830 break;
5833 if (argtype != error_mark_node)
5834 argtype = build_pointer_type (argtype);
5836 if (bitfield_p (arg))
5838 if (complain & tf_error)
5839 error ("attempt to take address of bit-field");
5840 return error_mark_node;
5843 /* In a template, we are processing a non-dependent expression
5844 so we can just form an ADDR_EXPR with the correct type. */
5845 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5847 val = build_address (arg);
5848 if (TREE_CODE (arg) == OFFSET_REF)
5849 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5851 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5853 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5855 /* We can only get here with a single static member
5856 function. */
5857 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5858 && DECL_STATIC_FUNCTION_P (fn));
5859 if (!mark_used (fn, complain) && !(complain & tf_error))
5860 return error_mark_node;
5861 val = build_address (fn);
5862 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5863 /* Do not lose object's side effects. */
5864 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5865 TREE_OPERAND (arg, 0), val);
5867 else
5869 tree object = TREE_OPERAND (arg, 0);
5870 tree field = TREE_OPERAND (arg, 1);
5871 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5872 (TREE_TYPE (object), decl_type_context (field)));
5873 val = build_address (arg);
5876 if (TYPE_PTR_P (argtype)
5877 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5879 build_ptrmemfunc_type (argtype);
5880 val = build_ptrmemfunc (argtype, val, 0,
5881 /*c_cast_p=*/false,
5882 complain);
5885 return val;
5888 /* Take the address of ARG if it has one, even if it's an rvalue. */
5890 tree
5891 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5893 return cp_build_addr_expr_1 (arg, 0, complain);
5896 /* Take the address of ARG, but only if it's an lvalue. */
5898 static tree
5899 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5901 return cp_build_addr_expr_1 (arg, 1, complain);
5904 /* C++: Must handle pointers to members.
5906 Perhaps type instantiation should be extended to handle conversion
5907 from aggregates to types we don't yet know we want? (Or are those
5908 cases typically errors which should be reported?)
5910 NOCONVERT suppresses the default promotions (such as from short to int). */
5912 tree
5913 cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
5914 tsubst_flags_t complain)
5916 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5917 tree arg = xarg;
5918 location_t location = EXPR_LOC_OR_LOC (arg, input_location);
5919 tree argtype = 0;
5920 const char *errstring = NULL;
5921 tree val;
5922 const char *invalid_op_diag;
5924 if (!arg || error_operand_p (arg))
5925 return error_mark_node;
5927 if ((invalid_op_diag
5928 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5929 ? CONVERT_EXPR
5930 : code),
5931 TREE_TYPE (xarg))))
5933 if (complain & tf_error)
5934 error (invalid_op_diag);
5935 return error_mark_node;
5938 switch (code)
5940 case UNARY_PLUS_EXPR:
5941 case NEGATE_EXPR:
5943 int flags = WANT_ARITH | WANT_ENUM;
5944 /* Unary plus (but not unary minus) is allowed on pointers. */
5945 if (code == UNARY_PLUS_EXPR)
5946 flags |= WANT_POINTER;
5947 arg = build_expr_type_conversion (flags, arg, true);
5948 if (!arg)
5949 errstring = (code == NEGATE_EXPR
5950 ? _("wrong type argument to unary minus")
5951 : _("wrong type argument to unary plus"));
5952 else
5954 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5955 arg = cp_perform_integral_promotions (arg, complain);
5957 /* Make sure the result is not an lvalue: a unary plus or minus
5958 expression is always a rvalue. */
5959 arg = rvalue (arg);
5962 break;
5964 case BIT_NOT_EXPR:
5965 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5967 code = CONJ_EXPR;
5968 if (!noconvert)
5970 arg = cp_default_conversion (arg, complain);
5971 if (arg == error_mark_node)
5972 return error_mark_node;
5975 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5976 | WANT_VECTOR_OR_COMPLEX,
5977 arg, true)))
5978 errstring = _("wrong type argument to bit-complement");
5979 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5981 /* Warn if the expression has boolean value. */
5982 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
5983 && warning_at (location, OPT_Wbool_operation,
5984 "%<~%> on an expression of type bool"))
5985 inform (location, "did you mean to use logical not (%<!%>)?");
5986 arg = cp_perform_integral_promotions (arg, complain);
5988 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
5989 arg = mark_rvalue_use (arg);
5990 break;
5992 case ABS_EXPR:
5993 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5994 errstring = _("wrong type argument to abs");
5995 else if (!noconvert)
5997 arg = cp_default_conversion (arg, complain);
5998 if (arg == error_mark_node)
5999 return error_mark_node;
6001 break;
6003 case CONJ_EXPR:
6004 /* Conjugating a real value is a no-op, but allow it anyway. */
6005 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
6006 errstring = _("wrong type argument to conjugation");
6007 else if (!noconvert)
6009 arg = cp_default_conversion (arg, complain);
6010 if (arg == error_mark_node)
6011 return error_mark_node;
6013 break;
6015 case TRUTH_NOT_EXPR:
6016 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
6017 return cp_build_binary_op (input_location, EQ_EXPR, arg,
6018 build_zero_cst (TREE_TYPE (arg)), complain);
6019 arg = perform_implicit_conversion (boolean_type_node, arg,
6020 complain);
6021 val = invert_truthvalue_loc (input_location, arg);
6022 if (arg != error_mark_node)
6023 return val;
6024 errstring = _("in argument to unary !");
6025 break;
6027 case NOP_EXPR:
6028 break;
6030 case REALPART_EXPR:
6031 case IMAGPART_EXPR:
6032 arg = build_real_imag_expr (input_location, code, arg);
6033 return arg;
6035 case PREINCREMENT_EXPR:
6036 case POSTINCREMENT_EXPR:
6037 case PREDECREMENT_EXPR:
6038 case POSTDECREMENT_EXPR:
6039 /* Handle complex lvalues (when permitted)
6040 by reduction to simpler cases. */
6042 val = unary_complex_lvalue (code, arg);
6043 if (val != 0)
6044 return val;
6046 arg = mark_lvalue_use (arg);
6048 /* Increment or decrement the real part of the value,
6049 and don't change the imaginary part. */
6050 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
6052 tree real, imag;
6054 arg = cp_stabilize_reference (arg);
6055 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6056 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6057 real = cp_build_unary_op (code, real, true, complain);
6058 if (real == error_mark_node || imag == error_mark_node)
6059 return error_mark_node;
6060 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6061 real, imag);
6064 /* Report invalid types. */
6066 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
6067 arg, true)))
6069 if (code == PREINCREMENT_EXPR)
6070 errstring = _("no pre-increment operator for type");
6071 else if (code == POSTINCREMENT_EXPR)
6072 errstring = _("no post-increment operator for type");
6073 else if (code == PREDECREMENT_EXPR)
6074 errstring = _("no pre-decrement operator for type");
6075 else
6076 errstring = _("no post-decrement operator for type");
6077 break;
6079 else if (arg == error_mark_node)
6080 return error_mark_node;
6082 /* Report something read-only. */
6084 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
6085 || TREE_READONLY (arg))
6087 if (complain & tf_error)
6088 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6089 || code == POSTINCREMENT_EXPR)
6090 ? lv_increment : lv_decrement));
6091 else
6092 return error_mark_node;
6096 tree inc;
6097 tree declared_type = unlowered_expr_type (arg);
6099 argtype = TREE_TYPE (arg);
6101 /* ARM $5.2.5 last annotation says this should be forbidden. */
6102 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
6104 if (complain & tf_error)
6105 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6106 ? G_("ISO C++ forbids incrementing an enum")
6107 : G_("ISO C++ forbids decrementing an enum"));
6108 else
6109 return error_mark_node;
6112 /* Compute the increment. */
6114 if (TYPE_PTR_P (argtype))
6116 tree type = complete_type (TREE_TYPE (argtype));
6118 if (!COMPLETE_OR_VOID_TYPE_P (type))
6120 if (complain & tf_error)
6121 error (((code == PREINCREMENT_EXPR
6122 || code == POSTINCREMENT_EXPR))
6123 ? G_("cannot increment a pointer to incomplete type %qT")
6124 : G_("cannot decrement a pointer to incomplete type %qT"),
6125 TREE_TYPE (argtype));
6126 else
6127 return error_mark_node;
6129 else if (!TYPE_PTROB_P (argtype))
6131 if (complain & tf_error)
6132 pedwarn (input_location, OPT_Wpointer_arith,
6133 (code == PREINCREMENT_EXPR
6134 || code == POSTINCREMENT_EXPR)
6135 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6136 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6137 argtype);
6138 else
6139 return error_mark_node;
6142 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6144 else
6145 inc = VECTOR_TYPE_P (argtype)
6146 ? build_one_cst (argtype)
6147 : integer_one_node;
6149 inc = cp_convert (argtype, inc, complain);
6151 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6152 need to ask Objective-C to build the increment or decrement
6153 expression for it. */
6154 if (objc_is_property_ref (arg))
6155 return objc_build_incr_expr_for_property_ref (input_location, code,
6156 arg, inc);
6158 /* Complain about anything else that is not a true lvalue. */
6159 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6160 || code == POSTINCREMENT_EXPR)
6161 ? lv_increment : lv_decrement),
6162 complain))
6163 return error_mark_node;
6165 /* Forbid using -- or ++ in C++17 on `bool'. */
6166 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6168 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6170 if (complain & tf_error)
6171 error ("use of an operand of type %qT in %<operator--%> "
6172 "is forbidden", boolean_type_node);
6173 return error_mark_node;
6175 else
6177 if (cxx_dialect >= cxx1z)
6179 if (complain & tf_error)
6180 error ("use of an operand of type %qT in "
6181 "%<operator++%> is forbidden in C++1z",
6182 boolean_type_node);
6183 return error_mark_node;
6185 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6186 else if (!in_system_header_at (input_location))
6187 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6188 "in %<operator++%> is deprecated",
6189 boolean_type_node);
6191 val = boolean_increment (code, arg);
6193 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6194 /* An rvalue has no cv-qualifiers. */
6195 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6196 else
6197 val = build2 (code, TREE_TYPE (arg), arg, inc);
6199 TREE_SIDE_EFFECTS (val) = 1;
6200 return val;
6203 case ADDR_EXPR:
6204 /* Note that this operation never does default_conversion
6205 regardless of NOCONVERT. */
6206 return cp_build_addr_expr (arg, complain);
6208 default:
6209 break;
6212 if (!errstring)
6214 if (argtype == 0)
6215 argtype = TREE_TYPE (arg);
6216 return build1 (code, argtype, arg);
6219 if (complain & tf_error)
6220 error ("%s", errstring);
6221 return error_mark_node;
6224 /* Hook for the c-common bits that build a unary op. */
6225 tree
6226 build_unary_op (location_t /*location*/,
6227 enum tree_code code, tree xarg, bool noconvert)
6229 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6232 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6233 for certain kinds of expressions which are not really lvalues
6234 but which we can accept as lvalues.
6236 If ARG is not a kind of expression we can handle, return
6237 NULL_TREE. */
6239 tree
6240 unary_complex_lvalue (enum tree_code code, tree arg)
6242 /* Inside a template, making these kinds of adjustments is
6243 pointless; we are only concerned with the type of the
6244 expression. */
6245 if (processing_template_decl)
6246 return NULL_TREE;
6248 /* Handle (a, b) used as an "lvalue". */
6249 if (TREE_CODE (arg) == COMPOUND_EXPR)
6251 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
6252 tf_warning_or_error);
6253 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6254 TREE_OPERAND (arg, 0), real_result);
6257 /* Handle (a ? b : c) used as an "lvalue". */
6258 if (TREE_CODE (arg) == COND_EXPR
6259 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6260 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6262 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6263 if (TREE_CODE (arg) == MODIFY_EXPR
6264 || TREE_CODE (arg) == PREINCREMENT_EXPR
6265 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6267 tree lvalue = TREE_OPERAND (arg, 0);
6268 if (TREE_SIDE_EFFECTS (lvalue))
6270 lvalue = cp_stabilize_reference (lvalue);
6271 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6272 lvalue, TREE_OPERAND (arg, 1));
6274 return unary_complex_lvalue
6275 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6278 if (code != ADDR_EXPR)
6279 return NULL_TREE;
6281 /* Handle (a = b) used as an "lvalue" for `&'. */
6282 if (TREE_CODE (arg) == MODIFY_EXPR
6283 || TREE_CODE (arg) == INIT_EXPR)
6285 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
6286 tf_warning_or_error);
6287 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6288 arg, real_result);
6289 TREE_NO_WARNING (arg) = 1;
6290 return arg;
6293 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6294 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6295 || TREE_CODE (arg) == OFFSET_REF)
6296 return NULL_TREE;
6298 /* We permit compiler to make function calls returning
6299 objects of aggregate type look like lvalues. */
6301 tree targ = arg;
6303 if (TREE_CODE (targ) == SAVE_EXPR)
6304 targ = TREE_OPERAND (targ, 0);
6306 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6308 if (TREE_CODE (arg) == SAVE_EXPR)
6309 targ = arg;
6310 else
6311 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6312 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6315 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6316 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6317 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6320 /* Don't let anything else be handled specially. */
6321 return NULL_TREE;
6324 /* Mark EXP saying that we need to be able to take the
6325 address of it; it should not be allocated in a register.
6326 Value is true if successful. ARRAY_REF_P is true if this
6327 is for ARRAY_REF construction - in that case we don't want
6328 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6329 it is fine to use ARRAY_REFs for vector subscripts on vector
6330 register variables.
6332 C++: we do not allow `current_class_ptr' to be addressable. */
6334 bool
6335 cxx_mark_addressable (tree exp, bool array_ref_p)
6337 tree x = exp;
6339 while (1)
6340 switch (TREE_CODE (x))
6342 case VIEW_CONVERT_EXPR:
6343 if (array_ref_p
6344 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6345 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6346 return true;
6347 /* FALLTHRU */
6348 case ADDR_EXPR:
6349 case COMPONENT_REF:
6350 case ARRAY_REF:
6351 case REALPART_EXPR:
6352 case IMAGPART_EXPR:
6353 x = TREE_OPERAND (x, 0);
6354 break;
6356 case PARM_DECL:
6357 if (x == current_class_ptr)
6359 error ("cannot take the address of %<this%>, which is an rvalue expression");
6360 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6361 return true;
6363 /* Fall through. */
6365 case VAR_DECL:
6366 /* Caller should not be trying to mark initialized
6367 constant fields addressable. */
6368 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6369 || DECL_IN_AGGR_P (x) == 0
6370 || TREE_STATIC (x)
6371 || DECL_EXTERNAL (x));
6372 /* Fall through. */
6374 case RESULT_DECL:
6375 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6376 && !DECL_ARTIFICIAL (x))
6378 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6380 error
6381 ("address of explicit register variable %qD requested", x);
6382 return false;
6384 else if (extra_warnings)
6385 warning
6386 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6388 TREE_ADDRESSABLE (x) = 1;
6389 return true;
6391 case CONST_DECL:
6392 case FUNCTION_DECL:
6393 TREE_ADDRESSABLE (x) = 1;
6394 return true;
6396 case CONSTRUCTOR:
6397 TREE_ADDRESSABLE (x) = 1;
6398 return true;
6400 case TARGET_EXPR:
6401 TREE_ADDRESSABLE (x) = 1;
6402 cxx_mark_addressable (TREE_OPERAND (x, 0));
6403 return true;
6405 default:
6406 return true;
6410 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6412 tree
6413 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6414 tsubst_flags_t complain)
6416 tree orig_ifexp = ifexp;
6417 tree orig_op1 = op1;
6418 tree orig_op2 = op2;
6419 tree expr;
6421 if (processing_template_decl)
6423 /* The standard says that the expression is type-dependent if
6424 IFEXP is type-dependent, even though the eventual type of the
6425 expression doesn't dependent on IFEXP. */
6426 if (type_dependent_expression_p (ifexp)
6427 /* As a GNU extension, the middle operand may be omitted. */
6428 || (op1 && type_dependent_expression_p (op1))
6429 || type_dependent_expression_p (op2))
6430 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6431 ifexp = build_non_dependent_expr (ifexp);
6432 if (op1)
6433 op1 = build_non_dependent_expr (op1);
6434 op2 = build_non_dependent_expr (op2);
6437 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6438 if (processing_template_decl && expr != error_mark_node)
6440 tree min = build_min_non_dep (COND_EXPR, expr,
6441 orig_ifexp, orig_op1, orig_op2);
6442 /* Remember that the result is an lvalue or xvalue. */
6443 if (glvalue_p (expr) && !glvalue_p (min))
6444 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6445 !lvalue_p (expr));
6446 expr = convert_from_reference (min);
6448 return expr;
6451 /* Given a list of expressions, return a compound expression
6452 that performs them all and returns the value of the last of them. */
6454 tree
6455 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6456 tsubst_flags_t complain)
6458 tree expr = TREE_VALUE (list);
6460 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6461 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6463 if (complain & tf_error)
6464 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6465 "list-initializer for non-class type must not "
6466 "be parenthesized");
6467 else
6468 return error_mark_node;
6471 if (TREE_CHAIN (list))
6473 if (complain & tf_error)
6474 switch (exp)
6476 case ELK_INIT:
6477 permerror (input_location, "expression list treated as compound "
6478 "expression in initializer");
6479 break;
6480 case ELK_MEM_INIT:
6481 permerror (input_location, "expression list treated as compound "
6482 "expression in mem-initializer");
6483 break;
6484 case ELK_FUNC_CAST:
6485 permerror (input_location, "expression list treated as compound "
6486 "expression in functional cast");
6487 break;
6488 default:
6489 gcc_unreachable ();
6491 else
6492 return error_mark_node;
6494 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6495 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6496 expr, TREE_VALUE (list), complain);
6499 return expr;
6502 /* Like build_x_compound_expr_from_list, but using a VEC. */
6504 tree
6505 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6506 tsubst_flags_t complain)
6508 if (vec_safe_is_empty (vec))
6509 return NULL_TREE;
6510 else if (vec->length () == 1)
6511 return (*vec)[0];
6512 else
6514 tree expr;
6515 unsigned int ix;
6516 tree t;
6518 if (msg != NULL)
6520 if (complain & tf_error)
6521 permerror (input_location,
6522 "%s expression list treated as compound expression",
6523 msg);
6524 else
6525 return error_mark_node;
6528 expr = (*vec)[0];
6529 for (ix = 1; vec->iterate (ix, &t); ++ix)
6530 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6531 t, complain);
6533 return expr;
6537 /* Handle overloading of the ',' operator when needed. */
6539 tree
6540 build_x_compound_expr (location_t loc, tree op1, tree op2,
6541 tsubst_flags_t complain)
6543 tree result;
6544 tree orig_op1 = op1;
6545 tree orig_op2 = op2;
6546 tree overload = NULL_TREE;
6548 if (processing_template_decl)
6550 if (type_dependent_expression_p (op1)
6551 || type_dependent_expression_p (op2))
6552 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6553 op1 = build_non_dependent_expr (op1);
6554 op2 = build_non_dependent_expr (op2);
6557 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6558 NULL_TREE, &overload, complain);
6559 if (!result)
6560 result = cp_build_compound_expr (op1, op2, complain);
6562 if (processing_template_decl && result != error_mark_node)
6564 if (overload != NULL_TREE)
6565 return (build_min_non_dep_op_overload
6566 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6568 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6571 return result;
6574 /* Like cp_build_compound_expr, but for the c-common bits. */
6576 tree
6577 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6579 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6582 /* Build a compound expression. */
6584 tree
6585 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6587 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6589 if (lhs == error_mark_node || rhs == error_mark_node)
6590 return error_mark_node;
6592 if (flag_cilkplus
6593 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6594 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6596 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6597 : EXPR_LOCATION (rhs));
6598 error_at (loc,
6599 "spawned function call cannot be part of a comma expression");
6600 return error_mark_node;
6603 if (TREE_CODE (rhs) == TARGET_EXPR)
6605 /* If the rhs is a TARGET_EXPR, then build the compound
6606 expression inside the target_expr's initializer. This
6607 helps the compiler to eliminate unnecessary temporaries. */
6608 tree init = TREE_OPERAND (rhs, 1);
6610 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6611 TREE_OPERAND (rhs, 1) = init;
6613 return rhs;
6616 if (type_unknown_p (rhs))
6618 if (complain & tf_error)
6619 error ("no context to resolve type of %qE", rhs);
6620 return error_mark_node;
6623 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6626 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6627 casts away constness. CAST gives the type of cast. Returns true
6628 if the cast is ill-formed, false if it is well-formed.
6630 ??? This function warns for casting away any qualifier not just
6631 const. We would like to specify exactly what qualifiers are casted
6632 away.
6635 static bool
6636 check_for_casting_away_constness (tree src_type, tree dest_type,
6637 enum tree_code cast, tsubst_flags_t complain)
6639 /* C-style casts are allowed to cast away constness. With
6640 WARN_CAST_QUAL, we still want to issue a warning. */
6641 if (cast == CAST_EXPR && !warn_cast_qual)
6642 return false;
6644 if (!casts_away_constness (src_type, dest_type, complain))
6645 return false;
6647 switch (cast)
6649 case CAST_EXPR:
6650 if (complain & tf_warning)
6651 warning (OPT_Wcast_qual,
6652 "cast from type %qT to type %qT casts away qualifiers",
6653 src_type, dest_type);
6654 return false;
6656 case STATIC_CAST_EXPR:
6657 if (complain & tf_error)
6658 error ("static_cast from type %qT to type %qT casts away qualifiers",
6659 src_type, dest_type);
6660 return true;
6662 case REINTERPRET_CAST_EXPR:
6663 if (complain & tf_error)
6664 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6665 src_type, dest_type);
6666 return true;
6668 default:
6669 gcc_unreachable();
6673 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6674 void
6675 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6677 if (warn_useless_cast
6678 && complain & tf_warning)
6680 if ((TREE_CODE (type) == REFERENCE_TYPE
6681 && (TYPE_REF_IS_RVALUE (type)
6682 ? xvalue_p (expr) : lvalue_p (expr))
6683 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6684 || same_type_p (TREE_TYPE (expr), type))
6685 warning (OPT_Wuseless_cast, "useless cast to type %q#T", type);
6689 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6690 void
6691 maybe_warn_about_cast_ignoring_quals (tree type, tsubst_flags_t complain)
6693 if (warn_ignored_qualifiers
6694 && complain & tf_warning
6695 && !CLASS_TYPE_P (type)
6696 && (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
6698 warning (OPT_Wignored_qualifiers, "type qualifiers ignored on cast "
6699 "result type");
6703 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6704 (another pointer-to-member type in the same hierarchy) and return
6705 the converted expression. If ALLOW_INVERSE_P is permitted, a
6706 pointer-to-derived may be converted to pointer-to-base; otherwise,
6707 only the other direction is permitted. If C_CAST_P is true, this
6708 conversion is taking place as part of a C-style cast. */
6710 tree
6711 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6712 bool c_cast_p, tsubst_flags_t complain)
6714 if (same_type_p (type, TREE_TYPE (expr)))
6715 return expr;
6717 if (TYPE_PTRDATAMEM_P (type))
6719 tree delta;
6721 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6722 TYPE_PTRMEM_CLASS_TYPE (type),
6723 allow_inverse_p,
6724 c_cast_p, complain);
6725 if (delta == error_mark_node)
6726 return error_mark_node;
6728 if (!integer_zerop (delta))
6730 tree cond, op1, op2;
6732 if (TREE_CODE (expr) == PTRMEM_CST)
6733 expr = cplus_expand_constant (expr);
6734 cond = cp_build_binary_op (input_location,
6735 EQ_EXPR,
6736 expr,
6737 build_int_cst (TREE_TYPE (expr), -1),
6738 complain);
6739 op1 = build_nop (ptrdiff_type_node, expr);
6740 op2 = cp_build_binary_op (input_location,
6741 PLUS_EXPR, op1, delta,
6742 complain);
6744 expr = fold_build3_loc (input_location,
6745 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6749 return build_nop (type, expr);
6751 else
6752 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6753 allow_inverse_p, c_cast_p, complain);
6756 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6757 this static_cast is being attempted as one of the possible casts
6758 allowed by a C-style cast. (In that case, accessibility of base
6759 classes is not considered, and it is OK to cast away
6760 constness.) Return the result of the cast. *VALID_P is set to
6761 indicate whether or not the cast was valid. */
6763 static tree
6764 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6765 bool *valid_p, tsubst_flags_t complain)
6767 tree intype;
6768 tree result;
6769 cp_lvalue_kind clk;
6771 /* Assume the cast is valid. */
6772 *valid_p = true;
6774 intype = unlowered_expr_type (expr);
6776 /* Save casted types in the function's used types hash table. */
6777 used_types_insert (type);
6779 /* A prvalue of non-class type is cv-unqualified. */
6780 if (!CLASS_TYPE_P (type))
6781 type = cv_unqualified (type);
6783 /* [expr.static.cast]
6785 An lvalue of type "cv1 B", where B is a class type, can be cast
6786 to type "reference to cv2 D", where D is a class derived (clause
6787 _class.derived_) from B, if a valid standard conversion from
6788 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6789 same cv-qualification as, or greater cv-qualification than, cv1,
6790 and B is not a virtual base class of D. */
6791 /* We check this case before checking the validity of "TYPE t =
6792 EXPR;" below because for this case:
6794 struct B {};
6795 struct D : public B { D(const B&); };
6796 extern B& b;
6797 void f() { static_cast<const D&>(b); }
6799 we want to avoid constructing a new D. The standard is not
6800 completely clear about this issue, but our interpretation is
6801 consistent with other compilers. */
6802 if (TREE_CODE (type) == REFERENCE_TYPE
6803 && CLASS_TYPE_P (TREE_TYPE (type))
6804 && CLASS_TYPE_P (intype)
6805 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
6806 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6807 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6808 build_pointer_type (TYPE_MAIN_VARIANT
6809 (TREE_TYPE (type))),
6810 complain)
6811 && (c_cast_p
6812 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6814 tree base;
6816 /* There is a standard conversion from "D*" to "B*" even if "B"
6817 is ambiguous or inaccessible. If this is really a
6818 static_cast, then we check both for inaccessibility and
6819 ambiguity. However, if this is a static_cast being performed
6820 because the user wrote a C-style cast, then accessibility is
6821 not considered. */
6822 base = lookup_base (TREE_TYPE (type), intype,
6823 c_cast_p ? ba_unique : ba_check,
6824 NULL, complain);
6825 expr = build_address (expr);
6827 if (sanitize_flags_p (SANITIZE_VPTR))
6829 tree ubsan_check
6830 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6831 intype, expr);
6832 if (ubsan_check)
6833 expr = ubsan_check;
6836 /* Convert from "B*" to "D*". This function will check that "B"
6837 is not a virtual base of "D". */
6838 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6839 complain);
6841 /* Convert the pointer to a reference -- but then remember that
6842 there are no expressions with reference type in C++.
6844 We call rvalue so that there's an actual tree code
6845 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6846 is a variable with the same type, the conversion would get folded
6847 away, leaving just the variable and causing lvalue_kind to give
6848 the wrong answer. */
6849 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6852 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6853 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6854 if (TREE_CODE (type) == REFERENCE_TYPE
6855 && TYPE_REF_IS_RVALUE (type)
6856 && (clk = real_lvalue_p (expr))
6857 && reference_related_p (TREE_TYPE (type), intype)
6858 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6860 if (clk == clk_ordinary)
6862 /* Handle the (non-bit-field) lvalue case here by casting to
6863 lvalue reference and then changing it to an rvalue reference.
6864 Casting an xvalue to rvalue reference will be handled by the
6865 main code path. */
6866 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6867 result = (perform_direct_initialization_if_possible
6868 (lref, expr, c_cast_p, complain));
6869 result = build1 (NON_LVALUE_EXPR, type, result);
6870 return convert_from_reference (result);
6872 else
6873 /* For a bit-field or packed field, bind to a temporary. */
6874 expr = rvalue (expr);
6877 /* Resolve overloaded address here rather than once in
6878 implicit_conversion and again in the inverse code below. */
6879 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6881 expr = instantiate_type (type, expr, complain);
6882 intype = TREE_TYPE (expr);
6885 /* [expr.static.cast]
6887 Any expression can be explicitly converted to type cv void. */
6888 if (VOID_TYPE_P (type))
6889 return convert_to_void (expr, ICV_CAST, complain);
6891 /* [class.abstract]
6892 An abstract class shall not be used ... as the type of an explicit
6893 conversion. */
6894 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6895 return error_mark_node;
6897 /* [expr.static.cast]
6899 An expression e can be explicitly converted to a type T using a
6900 static_cast of the form static_cast<T>(e) if the declaration T
6901 t(e);" is well-formed, for some invented temporary variable
6902 t. */
6903 result = perform_direct_initialization_if_possible (type, expr,
6904 c_cast_p, complain);
6905 if (result)
6907 result = convert_from_reference (result);
6909 /* [expr.static.cast]
6911 If T is a reference type, the result is an lvalue; otherwise,
6912 the result is an rvalue. */
6913 if (TREE_CODE (type) != REFERENCE_TYPE)
6915 result = rvalue (result);
6917 if (result == expr && SCALAR_TYPE_P (type))
6918 /* Leave some record of the cast. */
6919 result = build_nop (type, expr);
6921 return result;
6924 /* [expr.static.cast]
6926 The inverse of any standard conversion sequence (clause _conv_),
6927 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6928 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6929 (_conv.bool_) conversions, can be performed explicitly using
6930 static_cast subject to the restriction that the explicit
6931 conversion does not cast away constness (_expr.const.cast_), and
6932 the following additional rules for specific cases: */
6933 /* For reference, the conversions not excluded are: integral
6934 promotions, floating point promotion, integral conversions,
6935 floating point conversions, floating-integral conversions,
6936 pointer conversions, and pointer to member conversions. */
6937 /* DR 128
6939 A value of integral _or enumeration_ type can be explicitly
6940 converted to an enumeration type. */
6941 /* The effect of all that is that any conversion between any two
6942 types which are integral, floating, or enumeration types can be
6943 performed. */
6944 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6945 || SCALAR_FLOAT_TYPE_P (type))
6946 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6947 || SCALAR_FLOAT_TYPE_P (intype)))
6948 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6950 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6951 && CLASS_TYPE_P (TREE_TYPE (type))
6952 && CLASS_TYPE_P (TREE_TYPE (intype))
6953 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6954 (TREE_TYPE (intype))),
6955 build_pointer_type (TYPE_MAIN_VARIANT
6956 (TREE_TYPE (type))),
6957 complain))
6959 tree base;
6961 if (!c_cast_p
6962 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6963 complain))
6964 return error_mark_node;
6965 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6966 c_cast_p ? ba_unique : ba_check,
6967 NULL, complain);
6968 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6969 complain);
6971 if (sanitize_flags_p (SANITIZE_VPTR))
6973 tree ubsan_check
6974 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6975 intype, expr);
6976 if (ubsan_check)
6977 expr = ubsan_check;
6980 return cp_fold_convert (type, expr);
6983 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6984 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6986 tree c1;
6987 tree c2;
6988 tree t1;
6989 tree t2;
6991 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6992 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6994 if (TYPE_PTRDATAMEM_P (type))
6996 t1 = (build_ptrmem_type
6997 (c1,
6998 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6999 t2 = (build_ptrmem_type
7000 (c2,
7001 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
7003 else
7005 t1 = intype;
7006 t2 = type;
7008 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
7010 if (!c_cast_p
7011 && check_for_casting_away_constness (intype, type,
7012 STATIC_CAST_EXPR,
7013 complain))
7014 return error_mark_node;
7015 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
7016 c_cast_p, complain);
7020 /* [expr.static.cast]
7022 An rvalue of type "pointer to cv void" can be explicitly
7023 converted to a pointer to object type. A value of type pointer
7024 to object converted to "pointer to cv void" and back to the
7025 original pointer type will have its original value. */
7026 if (TYPE_PTR_P (intype)
7027 && VOID_TYPE_P (TREE_TYPE (intype))
7028 && TYPE_PTROB_P (type))
7030 if (!c_cast_p
7031 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
7032 complain))
7033 return error_mark_node;
7034 return build_nop (type, expr);
7037 *valid_p = false;
7038 return error_mark_node;
7041 /* Return an expression representing static_cast<TYPE>(EXPR). */
7043 tree
7044 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
7046 tree result;
7047 bool valid_p;
7049 if (type == error_mark_node || expr == error_mark_node)
7050 return error_mark_node;
7052 if (processing_template_decl)
7054 expr = build_min (STATIC_CAST_EXPR, type, expr);
7055 /* We don't know if it will or will not have side effects. */
7056 TREE_SIDE_EFFECTS (expr) = 1;
7057 return convert_from_reference (expr);
7060 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7061 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7062 if (TREE_CODE (type) != REFERENCE_TYPE
7063 && TREE_CODE (expr) == NOP_EXPR
7064 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7065 expr = TREE_OPERAND (expr, 0);
7067 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
7068 complain);
7069 if (valid_p)
7071 if (result != error_mark_node)
7073 maybe_warn_about_useless_cast (type, expr, complain);
7074 maybe_warn_about_cast_ignoring_quals (type, complain);
7076 return result;
7079 if (complain & tf_error)
7080 error ("invalid static_cast from type %qT to type %qT",
7081 TREE_TYPE (expr), type);
7082 return error_mark_node;
7085 /* EXPR is an expression with member function or pointer-to-member
7086 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7087 not permitted by ISO C++, but we accept it in some modes. If we
7088 are not in one of those modes, issue a diagnostic. Return the
7089 converted expression. */
7091 tree
7092 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
7094 tree intype;
7095 tree decl;
7097 intype = TREE_TYPE (expr);
7098 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7099 || TREE_CODE (intype) == METHOD_TYPE);
7101 if (!(complain & tf_warning_or_error))
7102 return error_mark_node;
7104 if (pedantic || warn_pmf2ptr)
7105 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
7106 "converting from %qH to %qI", intype, type);
7108 if (TREE_CODE (intype) == METHOD_TYPE)
7109 expr = build_addr_func (expr, complain);
7110 else if (TREE_CODE (expr) == PTRMEM_CST)
7111 expr = build_address (PTRMEM_CST_MEMBER (expr));
7112 else
7114 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7115 decl = build_address (decl);
7116 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
7119 if (expr == error_mark_node)
7120 return error_mark_node;
7122 return build_nop (type, expr);
7125 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7126 If C_CAST_P is true, this reinterpret cast is being done as part of
7127 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7128 indicate whether or not reinterpret_cast was valid. */
7130 static tree
7131 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
7132 bool *valid_p, tsubst_flags_t complain)
7134 tree intype;
7136 /* Assume the cast is invalid. */
7137 if (valid_p)
7138 *valid_p = true;
7140 if (type == error_mark_node || error_operand_p (expr))
7141 return error_mark_node;
7143 intype = TREE_TYPE (expr);
7145 /* Save casted types in the function's used types hash table. */
7146 used_types_insert (type);
7148 /* A prvalue of non-class type is cv-unqualified. */
7149 if (!CLASS_TYPE_P (type))
7150 type = cv_unqualified (type);
7152 /* [expr.reinterpret.cast]
7153 An lvalue expression of type T1 can be cast to the type
7154 "reference to T2" if an expression of type "pointer to T1" can be
7155 explicitly converted to the type "pointer to T2" using a
7156 reinterpret_cast. */
7157 if (TREE_CODE (type) == REFERENCE_TYPE)
7159 if (! lvalue_p (expr))
7161 if (complain & tf_error)
7162 error ("invalid cast of an rvalue expression of type "
7163 "%qT to type %qT",
7164 intype, type);
7165 return error_mark_node;
7168 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7169 "B" are related class types; the reinterpret_cast does not
7170 adjust the pointer. */
7171 if (TYPE_PTR_P (intype)
7172 && (complain & tf_warning)
7173 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7174 COMPARE_BASE | COMPARE_DERIVED)))
7175 warning (0, "casting %qT to %qT does not dereference pointer",
7176 intype, type);
7178 expr = cp_build_addr_expr (expr, complain);
7180 if (warn_strict_aliasing > 2)
7181 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
7183 if (expr != error_mark_node)
7184 expr = build_reinterpret_cast_1
7185 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
7186 valid_p, complain);
7187 if (expr != error_mark_node)
7188 /* cp_build_indirect_ref isn't right for rvalue refs. */
7189 expr = convert_from_reference (fold_convert (type, expr));
7190 return expr;
7193 /* As a G++ extension, we consider conversions from member
7194 functions, and pointers to member functions to
7195 pointer-to-function and pointer-to-void types. If
7196 -Wno-pmf-conversions has not been specified,
7197 convert_member_func_to_ptr will issue an error message. */
7198 if ((TYPE_PTRMEMFUNC_P (intype)
7199 || TREE_CODE (intype) == METHOD_TYPE)
7200 && TYPE_PTR_P (type)
7201 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7202 || VOID_TYPE_P (TREE_TYPE (type))))
7203 return convert_member_func_to_ptr (type, expr, complain);
7205 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7206 array-to-pointer, and function-to-pointer conversions are
7207 performed. */
7208 expr = decay_conversion (expr, complain);
7210 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7211 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7212 if (TREE_CODE (expr) == NOP_EXPR
7213 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7214 expr = TREE_OPERAND (expr, 0);
7216 if (error_operand_p (expr))
7217 return error_mark_node;
7219 intype = TREE_TYPE (expr);
7221 /* [expr.reinterpret.cast]
7222 A pointer can be converted to any integral type large enough to
7223 hold it. ... A value of type std::nullptr_t can be converted to
7224 an integral type; the conversion has the same meaning and
7225 validity as a conversion of (void*)0 to the integral type. */
7226 if (CP_INTEGRAL_TYPE_P (type)
7227 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7229 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7231 if (complain & tf_error)
7232 permerror (input_location, "cast from %qH to %qI loses precision",
7233 intype, type);
7234 else
7235 return error_mark_node;
7237 if (NULLPTR_TYPE_P (intype))
7238 return build_int_cst (type, 0);
7240 /* [expr.reinterpret.cast]
7241 A value of integral or enumeration type can be explicitly
7242 converted to a pointer. */
7243 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7244 /* OK */
7246 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7247 || TYPE_PTR_OR_PTRMEM_P (type))
7248 && same_type_p (type, intype))
7249 /* DR 799 */
7250 return rvalue (expr);
7251 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7252 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7253 return build_nop (type, expr);
7254 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7255 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7257 tree sexpr = expr;
7259 if (!c_cast_p
7260 && check_for_casting_away_constness (intype, type,
7261 REINTERPRET_CAST_EXPR,
7262 complain))
7263 return error_mark_node;
7264 /* Warn about possible alignment problems. */
7265 if (STRICT_ALIGNMENT && warn_cast_align
7266 && (complain & tf_warning)
7267 && !VOID_TYPE_P (type)
7268 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7269 && COMPLETE_TYPE_P (TREE_TYPE (type))
7270 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7271 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
7272 warning (OPT_Wcast_align, "cast from %qH to %qI "
7273 "increases required alignment of target type", intype, type);
7275 /* We need to strip nops here, because the front end likes to
7276 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7277 STRIP_NOPS (sexpr);
7278 if (warn_strict_aliasing <= 2)
7279 strict_aliasing_warning (intype, type, sexpr);
7281 return build_nop (type, expr);
7283 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7284 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7286 if (complain & tf_warning)
7287 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7288 object pointer type or vice versa is conditionally-supported." */
7289 warning (OPT_Wconditionally_supported,
7290 "casting between pointer-to-function and pointer-to-object "
7291 "is conditionally-supported");
7292 return build_nop (type, expr);
7294 else if (VECTOR_TYPE_P (type))
7295 return convert_to_vector (type, expr);
7296 else if (VECTOR_TYPE_P (intype)
7297 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7298 return convert_to_integer_nofold (type, expr);
7299 else
7301 if (valid_p)
7302 *valid_p = false;
7303 if (complain & tf_error)
7304 error ("invalid cast from type %qT to type %qT", intype, type);
7305 return error_mark_node;
7308 return cp_convert (type, expr, complain);
7311 tree
7312 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7314 tree r;
7316 if (type == error_mark_node || expr == error_mark_node)
7317 return error_mark_node;
7319 if (processing_template_decl)
7321 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7323 if (!TREE_SIDE_EFFECTS (t)
7324 && type_dependent_expression_p (expr))
7325 /* There might turn out to be side effects inside expr. */
7326 TREE_SIDE_EFFECTS (t) = 1;
7327 return convert_from_reference (t);
7330 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7331 /*valid_p=*/NULL, complain);
7332 if (r != error_mark_node)
7334 maybe_warn_about_useless_cast (type, expr, complain);
7335 maybe_warn_about_cast_ignoring_quals (type, complain);
7337 return r;
7340 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7341 return an appropriate expression. Otherwise, return
7342 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7343 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7344 performing a C-style cast, its value upon return will indicate
7345 whether or not the conversion succeeded. */
7347 static tree
7348 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7349 bool *valid_p)
7351 tree src_type;
7352 tree reference_type;
7354 /* Callers are responsible for handling error_mark_node as a
7355 destination type. */
7356 gcc_assert (dst_type != error_mark_node);
7357 /* In a template, callers should be building syntactic
7358 representations of casts, not using this machinery. */
7359 gcc_assert (!processing_template_decl);
7361 /* Assume the conversion is invalid. */
7362 if (valid_p)
7363 *valid_p = false;
7365 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7367 if (complain & tf_error)
7368 error ("invalid use of const_cast with type %qT, "
7369 "which is not a pointer, "
7370 "reference, nor a pointer-to-data-member type", dst_type);
7371 return error_mark_node;
7374 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7376 if (complain & tf_error)
7377 error ("invalid use of const_cast with type %qT, which is a pointer "
7378 "or reference to a function type", dst_type);
7379 return error_mark_node;
7382 /* A prvalue of non-class type is cv-unqualified. */
7383 dst_type = cv_unqualified (dst_type);
7385 /* Save casted types in the function's used types hash table. */
7386 used_types_insert (dst_type);
7388 src_type = TREE_TYPE (expr);
7389 /* Expressions do not really have reference types. */
7390 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7391 src_type = TREE_TYPE (src_type);
7393 /* [expr.const.cast]
7395 For two object types T1 and T2, if a pointer to T1 can be explicitly
7396 converted to the type "pointer to T2" using a const_cast, then the
7397 following conversions can also be made:
7399 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7400 type T2 using the cast const_cast<T2&>;
7402 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7403 type T2 using the cast const_cast<T2&&>; and
7405 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7406 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7408 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7410 reference_type = dst_type;
7411 if (!TYPE_REF_IS_RVALUE (dst_type)
7412 ? lvalue_p (expr)
7413 : obvalue_p (expr))
7414 /* OK. */;
7415 else
7417 if (complain & tf_error)
7418 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7419 src_type, dst_type);
7420 return error_mark_node;
7422 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7423 src_type = build_pointer_type (src_type);
7425 else
7427 reference_type = NULL_TREE;
7428 /* If the destination type is not a reference type, the
7429 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7430 conversions are performed. */
7431 src_type = type_decays_to (src_type);
7432 if (src_type == error_mark_node)
7433 return error_mark_node;
7436 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7438 if (comp_ptr_ttypes_const (dst_type, src_type))
7440 if (valid_p)
7442 *valid_p = true;
7443 /* This cast is actually a C-style cast. Issue a warning if
7444 the user is making a potentially unsafe cast. */
7445 check_for_casting_away_constness (src_type, dst_type,
7446 CAST_EXPR, complain);
7448 if (reference_type)
7450 expr = cp_build_addr_expr (expr, complain);
7451 if (expr == error_mark_node)
7452 return error_mark_node;
7453 expr = build_nop (reference_type, expr);
7454 return convert_from_reference (expr);
7456 else
7458 expr = decay_conversion (expr, complain);
7459 if (expr == error_mark_node)
7460 return error_mark_node;
7462 /* build_c_cast puts on a NOP_EXPR to make the result not an
7463 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7464 non-lvalue context. */
7465 if (TREE_CODE (expr) == NOP_EXPR
7466 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7467 expr = TREE_OPERAND (expr, 0);
7468 return build_nop (dst_type, expr);
7471 else if (valid_p
7472 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7473 TREE_TYPE (src_type)))
7474 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7475 complain);
7478 if (complain & tf_error)
7479 error ("invalid const_cast from type %qT to type %qT",
7480 src_type, dst_type);
7481 return error_mark_node;
7484 tree
7485 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7487 tree r;
7489 if (type == error_mark_node || error_operand_p (expr))
7490 return error_mark_node;
7492 if (processing_template_decl)
7494 tree t = build_min (CONST_CAST_EXPR, type, expr);
7496 if (!TREE_SIDE_EFFECTS (t)
7497 && type_dependent_expression_p (expr))
7498 /* There might turn out to be side effects inside expr. */
7499 TREE_SIDE_EFFECTS (t) = 1;
7500 return convert_from_reference (t);
7503 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7504 if (r != error_mark_node)
7506 maybe_warn_about_useless_cast (type, expr, complain);
7507 maybe_warn_about_cast_ignoring_quals (type, complain);
7509 return r;
7512 /* Like cp_build_c_cast, but for the c-common bits. */
7514 tree
7515 build_c_cast (location_t /*loc*/, tree type, tree expr)
7517 return cp_build_c_cast (type, expr, tf_warning_or_error);
7520 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7521 preserve location information even for tree nodes that don't
7522 support it. */
7524 cp_expr
7525 build_c_cast (location_t loc, tree type, cp_expr expr)
7527 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7528 result.set_location (loc);
7529 return result;
7532 /* Build an expression representing an explicit C-style cast to type
7533 TYPE of expression EXPR. */
7535 tree
7536 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7538 tree value = expr;
7539 tree result;
7540 bool valid_p;
7542 if (type == error_mark_node || error_operand_p (expr))
7543 return error_mark_node;
7545 if (processing_template_decl)
7547 tree t = build_min (CAST_EXPR, type,
7548 tree_cons (NULL_TREE, value, NULL_TREE));
7549 /* We don't know if it will or will not have side effects. */
7550 TREE_SIDE_EFFECTS (t) = 1;
7551 return convert_from_reference (t);
7554 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7555 'Class') should always be retained, because this information aids
7556 in method lookup. */
7557 if (objc_is_object_ptr (type)
7558 && objc_is_object_ptr (TREE_TYPE (expr)))
7559 return build_nop (type, expr);
7561 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7562 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7563 if (TREE_CODE (type) != REFERENCE_TYPE
7564 && TREE_CODE (value) == NOP_EXPR
7565 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7566 value = TREE_OPERAND (value, 0);
7568 if (TREE_CODE (type) == ARRAY_TYPE)
7570 /* Allow casting from T1* to T2[] because Cfront allows it.
7571 NIHCL uses it. It is not valid ISO C++ however. */
7572 if (TYPE_PTR_P (TREE_TYPE (expr)))
7574 if (complain & tf_error)
7575 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7576 else
7577 return error_mark_node;
7578 type = build_pointer_type (TREE_TYPE (type));
7580 else
7582 if (complain & tf_error)
7583 error ("ISO C++ forbids casting to an array type %qT", type);
7584 return error_mark_node;
7588 if (TREE_CODE (type) == FUNCTION_TYPE
7589 || TREE_CODE (type) == METHOD_TYPE)
7591 if (complain & tf_error)
7592 error ("invalid cast to function type %qT", type);
7593 return error_mark_node;
7596 if (TYPE_PTR_P (type)
7597 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7598 /* Casting to an integer of smaller size is an error detected elsewhere. */
7599 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7600 /* Don't warn about converting any constant. */
7601 && !TREE_CONSTANT (value))
7602 warning_at (input_location, OPT_Wint_to_pointer_cast,
7603 "cast to pointer from integer of different size");
7605 /* A C-style cast can be a const_cast. */
7606 result = build_const_cast_1 (type, value, complain & tf_warning,
7607 &valid_p);
7608 if (valid_p)
7610 if (result != error_mark_node)
7612 maybe_warn_about_useless_cast (type, value, complain);
7613 maybe_warn_about_cast_ignoring_quals (type, complain);
7615 return result;
7618 /* Or a static cast. */
7619 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7620 &valid_p, complain);
7621 /* Or a reinterpret_cast. */
7622 if (!valid_p)
7623 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7624 &valid_p, complain);
7625 /* The static_cast or reinterpret_cast may be followed by a
7626 const_cast. */
7627 if (valid_p
7628 /* A valid cast may result in errors if, for example, a
7629 conversion to an ambiguous base class is required. */
7630 && !error_operand_p (result))
7632 tree result_type;
7634 maybe_warn_about_useless_cast (type, value, complain);
7635 maybe_warn_about_cast_ignoring_quals (type, complain);
7637 /* Non-class rvalues always have cv-unqualified type. */
7638 if (!CLASS_TYPE_P (type))
7639 type = TYPE_MAIN_VARIANT (type);
7640 result_type = TREE_TYPE (result);
7641 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7642 result_type = TYPE_MAIN_VARIANT (result_type);
7643 /* If the type of RESULT does not match TYPE, perform a
7644 const_cast to make it match. If the static_cast or
7645 reinterpret_cast succeeded, we will differ by at most
7646 cv-qualification, so the follow-on const_cast is guaranteed
7647 to succeed. */
7648 if (!same_type_p (non_reference (type), non_reference (result_type)))
7650 result = build_const_cast_1 (type, result, false, &valid_p);
7651 gcc_assert (valid_p);
7653 return result;
7656 return error_mark_node;
7659 /* For use from the C common bits. */
7660 tree
7661 build_modify_expr (location_t location,
7662 tree lhs, tree /*lhs_origtype*/,
7663 enum tree_code modifycode,
7664 location_t /*rhs_location*/, tree rhs,
7665 tree /*rhs_origtype*/)
7667 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7668 tf_warning_or_error);
7671 /* Build an assignment expression of lvalue LHS from value RHS.
7672 MODIFYCODE is the code for a binary operator that we use
7673 to combine the old value of LHS with RHS to get the new value.
7674 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7676 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7678 tree
7679 cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7680 tree rhs, tsubst_flags_t complain)
7682 tree result = NULL_TREE;
7683 tree newrhs = rhs;
7684 tree lhstype = TREE_TYPE (lhs);
7685 tree olhs = lhs;
7686 tree olhstype = lhstype;
7687 bool plain_assign = (modifycode == NOP_EXPR);
7688 bool compound_side_effects_p = false;
7689 tree preeval = NULL_TREE;
7691 /* Avoid duplicate error messages from operands that had errors. */
7692 if (error_operand_p (lhs) || error_operand_p (rhs))
7693 return error_mark_node;
7695 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7697 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7698 compound_side_effects_p = true;
7699 lhs = TREE_OPERAND (lhs, 1);
7702 /* Handle control structure constructs used as "lvalues". Note that we
7703 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7704 switch (TREE_CODE (lhs))
7706 /* Handle --foo = 5; as these are valid constructs in C++. */
7707 case PREDECREMENT_EXPR:
7708 case PREINCREMENT_EXPR:
7709 if (compound_side_effects_p)
7710 newrhs = rhs = stabilize_expr (rhs, &preeval);
7711 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7712 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7713 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7714 TREE_OPERAND (lhs, 1));
7715 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7716 maybe_add_compound:
7717 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7718 and looked through the COMPOUND_EXPRs, readd them now around
7719 the resulting lhs. */
7720 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7722 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7723 tree *ptr = &TREE_OPERAND (lhs, 1);
7724 for (olhs = TREE_OPERAND (olhs, 1);
7725 TREE_CODE (olhs) == COMPOUND_EXPR;
7726 olhs = TREE_OPERAND (olhs, 1))
7728 *ptr = build2 (COMPOUND_EXPR, lhstype,
7729 TREE_OPERAND (olhs, 0), *ptr);
7730 ptr = &TREE_OPERAND (*ptr, 1);
7733 break;
7735 case MODIFY_EXPR:
7736 if (compound_side_effects_p)
7737 newrhs = rhs = stabilize_expr (rhs, &preeval);
7738 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7739 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7740 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
7741 TREE_OPERAND (lhs, 1));
7742 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
7743 goto maybe_add_compound;
7745 case MIN_EXPR:
7746 case MAX_EXPR:
7747 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7748 when neither operand has side-effects. */
7749 if (!lvalue_or_else (lhs, lv_assign, complain))
7750 return error_mark_node;
7752 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7753 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7755 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7756 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7757 boolean_type_node,
7758 TREE_OPERAND (lhs, 0),
7759 TREE_OPERAND (lhs, 1)),
7760 TREE_OPERAND (lhs, 0),
7761 TREE_OPERAND (lhs, 1));
7762 gcc_fallthrough ();
7764 /* Handle (a ? b : c) used as an "lvalue". */
7765 case COND_EXPR:
7767 /* Produce (a ? (b = rhs) : (c = rhs))
7768 except that the RHS goes through a save-expr
7769 so the code to compute it is only emitted once. */
7770 tree cond;
7772 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7774 if (complain & tf_error)
7775 error ("void value not ignored as it ought to be");
7776 return error_mark_node;
7779 rhs = stabilize_expr (rhs, &preeval);
7781 /* Check this here to avoid odd errors when trying to convert
7782 a throw to the type of the COND_EXPR. */
7783 if (!lvalue_or_else (lhs, lv_assign, complain))
7784 return error_mark_node;
7786 cond = build_conditional_expr
7787 (input_location, TREE_OPERAND (lhs, 0),
7788 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
7789 modifycode, rhs, complain),
7790 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
7791 modifycode, rhs, complain),
7792 complain);
7794 if (cond == error_mark_node)
7795 return cond;
7796 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7797 and looked through the COMPOUND_EXPRs, readd them now around
7798 the resulting cond before adding the preevaluated rhs. */
7799 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7801 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7802 TREE_OPERAND (olhs, 0), cond);
7803 tree *ptr = &TREE_OPERAND (cond, 1);
7804 for (olhs = TREE_OPERAND (olhs, 1);
7805 TREE_CODE (olhs) == COMPOUND_EXPR;
7806 olhs = TREE_OPERAND (olhs, 1))
7808 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7809 TREE_OPERAND (olhs, 0), *ptr);
7810 ptr = &TREE_OPERAND (*ptr, 1);
7813 /* Make sure the code to compute the rhs comes out
7814 before the split. */
7815 result = cond;
7816 goto ret;
7819 default:
7820 lhs = olhs;
7821 break;
7824 if (modifycode == INIT_EXPR)
7826 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7827 /* Do the default thing. */;
7828 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7830 /* Compound literal. */
7831 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7832 /* Call convert to generate an error; see PR 11063. */
7833 rhs = convert (lhstype, rhs);
7834 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7835 TREE_SIDE_EFFECTS (result) = 1;
7836 goto ret;
7838 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7839 /* Do the default thing. */;
7840 else
7842 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7843 result = build_special_member_call (lhs, complete_ctor_identifier,
7844 &rhs_vec, lhstype, LOOKUP_NORMAL,
7845 complain);
7846 release_tree_vector (rhs_vec);
7847 if (result == NULL_TREE)
7848 return error_mark_node;
7849 goto ret;
7852 else
7854 lhs = require_complete_type_sfinae (lhs, complain);
7855 if (lhs == error_mark_node)
7856 return error_mark_node;
7858 if (modifycode == NOP_EXPR)
7860 if (c_dialect_objc ())
7862 result = objc_maybe_build_modify_expr (lhs, rhs);
7863 if (result)
7864 goto ret;
7867 /* `operator=' is not an inheritable operator. */
7868 if (! MAYBE_CLASS_TYPE_P (lhstype))
7869 /* Do the default thing. */;
7870 else
7872 result = build_new_op (input_location, MODIFY_EXPR,
7873 LOOKUP_NORMAL, lhs, rhs,
7874 make_node (NOP_EXPR), /*overload=*/NULL,
7875 complain);
7876 if (result == NULL_TREE)
7877 return error_mark_node;
7878 goto ret;
7880 lhstype = olhstype;
7882 else
7884 tree init = NULL_TREE;
7886 /* A binary op has been requested. Combine the old LHS
7887 value with the RHS producing the value we should actually
7888 store into the LHS. */
7889 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7890 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7891 || MAYBE_CLASS_TYPE_P (lhstype)));
7893 /* Preevaluate the RHS to make sure its evaluation is complete
7894 before the lvalue-to-rvalue conversion of the LHS:
7896 [expr.ass] With respect to an indeterminately-sequenced
7897 function call, the operation of a compound assignment is a
7898 single evaluation. [ Note: Therefore, a function call shall
7899 not intervene between the lvalue-to-rvalue conversion and the
7900 side effect associated with any single compound assignment
7901 operator. -- end note ] */
7902 lhs = cp_stabilize_reference (lhs);
7903 rhs = rvalue (rhs);
7904 rhs = stabilize_expr (rhs, &init);
7905 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
7906 if (newrhs == error_mark_node)
7908 if (complain & tf_error)
7909 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7910 TREE_TYPE (lhs), TREE_TYPE (rhs));
7911 return error_mark_node;
7914 if (init)
7915 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7917 /* Now it looks like a plain assignment. */
7918 modifycode = NOP_EXPR;
7919 if (c_dialect_objc ())
7921 result = objc_maybe_build_modify_expr (lhs, newrhs);
7922 if (result)
7923 goto ret;
7926 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7927 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7930 /* The left-hand side must be an lvalue. */
7931 if (!lvalue_or_else (lhs, lv_assign, complain))
7932 return error_mark_node;
7934 /* Warn about modifying something that is `const'. Don't warn if
7935 this is initialization. */
7936 if (modifycode != INIT_EXPR
7937 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7938 /* Functions are not modifiable, even though they are
7939 lvalues. */
7940 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7941 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7942 /* If it's an aggregate and any field is const, then it is
7943 effectively const. */
7944 || (CLASS_TYPE_P (lhstype)
7945 && C_TYPE_FIELDS_READONLY (lhstype))))
7947 if (complain & tf_error)
7948 cxx_readonly_error (lhs, lv_assign);
7949 else
7950 return error_mark_node;
7953 /* If storing into a structure or union member, it may have been given a
7954 lowered bitfield type. We need to convert to the declared type first,
7955 so retrieve it now. */
7957 olhstype = unlowered_expr_type (lhs);
7959 /* Convert new value to destination type. */
7961 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7963 int from_array;
7965 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7967 if (modifycode != INIT_EXPR)
7969 if (complain & tf_error)
7970 error ("assigning to an array from an initializer list");
7971 return error_mark_node;
7973 if (check_array_initializer (lhs, lhstype, newrhs))
7974 return error_mark_node;
7975 newrhs = digest_init (lhstype, newrhs, complain);
7976 if (newrhs == error_mark_node)
7977 return error_mark_node;
7980 /* C++11 8.5/17: "If the destination type is an array of characters,
7981 an array of char16_t, an array of char32_t, or an array of wchar_t,
7982 and the initializer is a string literal...". */
7983 else if (TREE_CODE (newrhs) == STRING_CST
7984 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7985 && modifycode == INIT_EXPR)
7987 newrhs = digest_init (lhstype, newrhs, complain);
7988 if (newrhs == error_mark_node)
7989 return error_mark_node;
7992 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7993 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7995 if (complain & tf_error)
7996 error ("incompatible types in assignment of %qT to %qT",
7997 TREE_TYPE (rhs), lhstype);
7998 return error_mark_node;
8001 /* Allow array assignment in compiler-generated code. */
8002 else if (!current_function_decl
8003 || !DECL_DEFAULTED_FN (current_function_decl))
8005 /* This routine is used for both initialization and assignment.
8006 Make sure the diagnostic message differentiates the context. */
8007 if (complain & tf_error)
8009 if (modifycode == INIT_EXPR)
8010 error ("array used as initializer");
8011 else
8012 error ("invalid array assignment");
8014 return error_mark_node;
8017 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
8018 ? 1 + (modifycode != INIT_EXPR): 0;
8019 result = build_vec_init (lhs, NULL_TREE, newrhs,
8020 /*explicit_value_init_p=*/false,
8021 from_array, complain);
8022 goto ret;
8025 if (modifycode == INIT_EXPR)
8026 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8027 LOOKUP_ONLYCONVERTING. */
8028 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
8029 ICR_INIT, NULL_TREE, 0,
8030 complain);
8031 else
8032 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
8033 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
8035 if (!same_type_p (lhstype, olhstype))
8036 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
8038 if (modifycode != INIT_EXPR)
8040 if (TREE_CODE (newrhs) == CALL_EXPR
8041 && TYPE_NEEDS_CONSTRUCTING (lhstype))
8042 newrhs = build_cplus_new (lhstype, newrhs, complain);
8044 /* Can't initialize directly from a TARGET_EXPR, since that would
8045 cause the lhs to be constructed twice, and possibly result in
8046 accidental self-initialization. So we force the TARGET_EXPR to be
8047 expanded without a target. */
8048 if (TREE_CODE (newrhs) == TARGET_EXPR)
8049 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
8050 TREE_OPERAND (newrhs, 0));
8053 if (newrhs == error_mark_node)
8054 return error_mark_node;
8056 if (c_dialect_objc () && flag_objc_gc)
8058 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
8060 if (result)
8061 goto ret;
8064 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
8065 lhstype, lhs, newrhs);
8067 TREE_SIDE_EFFECTS (result) = 1;
8068 if (!plain_assign)
8069 TREE_NO_WARNING (result) = 1;
8071 ret:
8072 if (preeval)
8073 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
8074 return result;
8077 cp_expr
8078 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
8079 tree rhs, tsubst_flags_t complain)
8081 tree orig_lhs = lhs;
8082 tree orig_rhs = rhs;
8083 tree overload = NULL_TREE;
8084 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
8086 if (processing_template_decl)
8088 if (modifycode == NOP_EXPR
8089 || type_dependent_expression_p (lhs)
8090 || type_dependent_expression_p (rhs))
8091 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8092 build_min_nt_loc (loc, modifycode, NULL_TREE,
8093 NULL_TREE), rhs);
8095 lhs = build_non_dependent_expr (lhs);
8096 rhs = build_non_dependent_expr (rhs);
8099 if (modifycode != NOP_EXPR)
8101 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8102 lhs, rhs, op, &overload, complain);
8103 if (rval)
8105 if (rval == error_mark_node)
8106 return rval;
8107 TREE_NO_WARNING (rval) = 1;
8108 if (processing_template_decl)
8110 if (overload != NULL_TREE)
8111 return (build_min_non_dep_op_overload
8112 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8114 return (build_min_non_dep
8115 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8117 return rval;
8120 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
8123 /* Helper function for get_delta_difference which assumes FROM is a base
8124 class of TO. Returns a delta for the conversion of pointer-to-member
8125 of FROM to pointer-to-member of TO. If the conversion is invalid and
8126 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8127 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8128 If C_CAST_P is true, this conversion is taking place as part of a
8129 C-style cast. */
8131 static tree
8132 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8133 tsubst_flags_t complain)
8135 tree binfo;
8136 base_kind kind;
8138 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8139 &kind, complain);
8141 if (binfo == error_mark_node)
8143 if (!(complain & tf_error))
8144 return error_mark_node;
8146 error (" in pointer to member function conversion");
8147 return size_zero_node;
8149 else if (binfo)
8151 if (kind != bk_via_virtual)
8152 return BINFO_OFFSET (binfo);
8153 else
8154 /* FROM is a virtual base class of TO. Issue an error or warning
8155 depending on whether or not this is a reinterpret cast. */
8157 if (!(complain & tf_error))
8158 return error_mark_node;
8160 error ("pointer to member conversion via virtual base %qT",
8161 BINFO_TYPE (binfo_from_vbase (binfo)));
8163 return size_zero_node;
8166 else
8167 return NULL_TREE;
8170 /* Get difference in deltas for different pointer to member function
8171 types. If the conversion is invalid and tf_error is not set in
8172 COMPLAIN, returns error_mark_node, otherwise returns an integer
8173 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8174 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8175 conversions as well. If C_CAST_P is true this conversion is taking
8176 place as part of a C-style cast.
8178 Note that the naming of FROM and TO is kind of backwards; the return
8179 value is what we add to a TO in order to get a FROM. They are named
8180 this way because we call this function to find out how to convert from
8181 a pointer to member of FROM to a pointer to member of TO. */
8183 static tree
8184 get_delta_difference (tree from, tree to,
8185 bool allow_inverse_p,
8186 bool c_cast_p, tsubst_flags_t complain)
8188 tree result;
8190 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8191 /* Pointer to member of incomplete class is permitted*/
8192 result = size_zero_node;
8193 else
8194 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8196 if (result == error_mark_node)
8197 return error_mark_node;
8199 if (!result)
8201 if (!allow_inverse_p)
8203 if (!(complain & tf_error))
8204 return error_mark_node;
8206 error_not_base_type (from, to);
8207 error (" in pointer to member conversion");
8208 result = size_zero_node;
8210 else
8212 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8214 if (result == error_mark_node)
8215 return error_mark_node;
8217 if (result)
8218 result = size_diffop_loc (input_location,
8219 size_zero_node, result);
8220 else
8222 if (!(complain & tf_error))
8223 return error_mark_node;
8225 error_not_base_type (from, to);
8226 error (" in pointer to member conversion");
8227 result = size_zero_node;
8232 return convert_to_integer (ptrdiff_type_node, result);
8235 /* Return a constructor for the pointer-to-member-function TYPE using
8236 the other components as specified. */
8238 tree
8239 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8241 tree u = NULL_TREE;
8242 tree delta_field;
8243 tree pfn_field;
8244 vec<constructor_elt, va_gc> *v;
8246 /* Pull the FIELD_DECLs out of the type. */
8247 pfn_field = TYPE_FIELDS (type);
8248 delta_field = DECL_CHAIN (pfn_field);
8250 /* Make sure DELTA has the type we want. */
8251 delta = convert_and_check (input_location, delta_type_node, delta);
8253 /* Convert to the correct target type if necessary. */
8254 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8256 /* Finish creating the initializer. */
8257 vec_alloc (v, 2);
8258 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8259 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8260 u = build_constructor (type, v);
8261 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8262 TREE_STATIC (u) = (TREE_CONSTANT (u)
8263 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8264 != NULL_TREE)
8265 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8266 != NULL_TREE));
8267 return u;
8270 /* Build a constructor for a pointer to member function. It can be
8271 used to initialize global variables, local variable, or used
8272 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8273 want to be.
8275 If FORCE is nonzero, then force this conversion, even if
8276 we would rather not do it. Usually set when using an explicit
8277 cast. A C-style cast is being processed iff C_CAST_P is true.
8279 Return error_mark_node, if something goes wrong. */
8281 tree
8282 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8283 tsubst_flags_t complain)
8285 tree fn;
8286 tree pfn_type;
8287 tree to_type;
8289 if (error_operand_p (pfn))
8290 return error_mark_node;
8292 pfn_type = TREE_TYPE (pfn);
8293 to_type = build_ptrmemfunc_type (type);
8295 /* Handle multiple conversions of pointer to member functions. */
8296 if (TYPE_PTRMEMFUNC_P (pfn_type))
8298 tree delta = NULL_TREE;
8299 tree npfn = NULL_TREE;
8300 tree n;
8302 if (!force
8303 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8304 LOOKUP_NORMAL, complain))
8306 if (complain & tf_error)
8307 error ("invalid conversion to type %qT from type %qT",
8308 to_type, pfn_type);
8309 else
8310 return error_mark_node;
8313 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8314 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8315 force,
8316 c_cast_p, complain);
8317 if (n == error_mark_node)
8318 return error_mark_node;
8320 /* We don't have to do any conversion to convert a
8321 pointer-to-member to its own type. But, we don't want to
8322 just return a PTRMEM_CST if there's an explicit cast; that
8323 cast should make the expression an invalid template argument. */
8324 if (TREE_CODE (pfn) != PTRMEM_CST)
8326 if (same_type_p (to_type, pfn_type))
8327 return pfn;
8328 else if (integer_zerop (n))
8329 return build_reinterpret_cast (to_type, pfn,
8330 complain);
8333 if (TREE_SIDE_EFFECTS (pfn))
8334 pfn = save_expr (pfn);
8336 /* Obtain the function pointer and the current DELTA. */
8337 if (TREE_CODE (pfn) == PTRMEM_CST)
8338 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8339 else
8341 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8342 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8345 /* Just adjust the DELTA field. */
8346 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8347 (TREE_TYPE (delta), ptrdiff_type_node));
8348 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8349 n = cp_build_binary_op (input_location,
8350 LSHIFT_EXPR, n, integer_one_node,
8351 complain);
8352 delta = cp_build_binary_op (input_location,
8353 PLUS_EXPR, delta, n, complain);
8354 return build_ptrmemfunc1 (to_type, delta, npfn);
8357 /* Handle null pointer to member function conversions. */
8358 if (null_ptr_cst_p (pfn))
8360 pfn = cp_build_c_cast (type, pfn, complain);
8361 return build_ptrmemfunc1 (to_type,
8362 integer_zero_node,
8363 pfn);
8366 if (type_unknown_p (pfn))
8367 return instantiate_type (type, pfn, complain);
8369 fn = TREE_OPERAND (pfn, 0);
8370 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8371 /* In a template, we will have preserved the
8372 OFFSET_REF. */
8373 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8374 return make_ptrmem_cst (to_type, fn);
8377 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8378 given by CST.
8380 ??? There is no consistency as to the types returned for the above
8381 values. Some code acts as if it were a sizetype and some as if it were
8382 integer_type_node. */
8384 void
8385 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8387 tree type = TREE_TYPE (cst);
8388 tree fn = PTRMEM_CST_MEMBER (cst);
8389 tree ptr_class, fn_class;
8391 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8393 /* The class that the function belongs to. */
8394 fn_class = DECL_CONTEXT (fn);
8396 /* The class that we're creating a pointer to member of. */
8397 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8399 /* First, calculate the adjustment to the function's class. */
8400 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8401 /*c_cast_p=*/0, tf_warning_or_error);
8403 if (!DECL_VIRTUAL_P (fn))
8404 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8405 build_addr_func (fn, tf_warning_or_error));
8406 else
8408 /* If we're dealing with a virtual function, we have to adjust 'this'
8409 again, to point to the base which provides the vtable entry for
8410 fn; the call will do the opposite adjustment. */
8411 tree orig_class = DECL_CONTEXT (fn);
8412 tree binfo = binfo_or_else (orig_class, fn_class);
8413 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8414 *delta, BINFO_OFFSET (binfo));
8416 /* We set PFN to the vtable offset at which the function can be
8417 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8418 case delta is shifted left, and then incremented). */
8419 *pfn = DECL_VINDEX (fn);
8420 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8421 TYPE_SIZE_UNIT (vtable_entry_type));
8423 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8425 case ptrmemfunc_vbit_in_pfn:
8426 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8427 integer_one_node);
8428 break;
8430 case ptrmemfunc_vbit_in_delta:
8431 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8432 *delta, integer_one_node);
8433 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8434 *delta, integer_one_node);
8435 break;
8437 default:
8438 gcc_unreachable ();
8441 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8445 /* Return an expression for PFN from the pointer-to-member function
8446 given by T. */
8448 static tree
8449 pfn_from_ptrmemfunc (tree t)
8451 if (TREE_CODE (t) == PTRMEM_CST)
8453 tree delta;
8454 tree pfn;
8456 expand_ptrmemfunc_cst (t, &delta, &pfn);
8457 if (pfn)
8458 return pfn;
8461 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8464 /* Return an expression for DELTA from the pointer-to-member function
8465 given by T. */
8467 static tree
8468 delta_from_ptrmemfunc (tree t)
8470 if (TREE_CODE (t) == PTRMEM_CST)
8472 tree delta;
8473 tree pfn;
8475 expand_ptrmemfunc_cst (t, &delta, &pfn);
8476 if (delta)
8477 return delta;
8480 return build_ptrmemfunc_access_expr (t, delta_identifier);
8483 /* Convert value RHS to type TYPE as preparation for an assignment to
8484 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8485 implicit conversion is. If FNDECL is non-NULL, we are doing the
8486 conversion in order to pass the PARMNUMth argument of FNDECL.
8487 If FNDECL is NULL, we are doing the conversion in function pointer
8488 argument passing, conversion in initialization, etc. */
8490 static tree
8491 convert_for_assignment (tree type, tree rhs,
8492 impl_conv_rhs errtype, tree fndecl, int parmnum,
8493 tsubst_flags_t complain, int flags)
8495 tree rhstype;
8496 enum tree_code coder;
8498 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8499 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8500 rhs = TREE_OPERAND (rhs, 0);
8502 /* Handle [dcl.init.list] direct-list-initialization from
8503 single element of enumeration with a fixed underlying type. */
8504 if (is_direct_enum_init (type, rhs))
8506 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8507 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8508 rhs = cp_build_c_cast (type, elt, complain);
8509 else
8510 rhs = error_mark_node;
8513 rhstype = TREE_TYPE (rhs);
8514 coder = TREE_CODE (rhstype);
8516 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8517 && vector_types_convertible_p (type, rhstype, true))
8519 rhs = mark_rvalue_use (rhs);
8520 return convert (type, rhs);
8523 if (rhs == error_mark_node || rhstype == error_mark_node)
8524 return error_mark_node;
8525 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8526 return error_mark_node;
8528 /* The RHS of an assignment cannot have void type. */
8529 if (coder == VOID_TYPE)
8531 if (complain & tf_error)
8532 error ("void value not ignored as it ought to be");
8533 return error_mark_node;
8536 if (c_dialect_objc ())
8538 int parmno;
8539 tree selector;
8540 tree rname = fndecl;
8542 switch (errtype)
8544 case ICR_ASSIGN:
8545 parmno = -1;
8546 break;
8547 case ICR_INIT:
8548 parmno = -2;
8549 break;
8550 default:
8551 selector = objc_message_selector ();
8552 parmno = parmnum;
8553 if (selector && parmno > 1)
8555 rname = selector;
8556 parmno -= 1;
8560 if (objc_compare_types (type, rhstype, parmno, rname))
8562 rhs = mark_rvalue_use (rhs);
8563 return convert (type, rhs);
8567 /* [expr.ass]
8569 The expression is implicitly converted (clause _conv_) to the
8570 cv-unqualified type of the left operand.
8572 We allow bad conversions here because by the time we get to this point
8573 we are committed to doing the conversion. If we end up doing a bad
8574 conversion, convert_like will complain. */
8575 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8577 /* When -Wno-pmf-conversions is use, we just silently allow
8578 conversions from pointers-to-members to plain pointers. If
8579 the conversion doesn't work, cp_convert will complain. */
8580 if (!warn_pmf2ptr
8581 && TYPE_PTR_P (type)
8582 && TYPE_PTRMEMFUNC_P (rhstype))
8583 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8584 else
8586 if (complain & tf_error)
8588 /* If the right-hand side has unknown type, then it is an
8589 overloaded function. Call instantiate_type to get error
8590 messages. */
8591 if (rhstype == unknown_type_node)
8593 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8594 /* -fpermissive might allow this; recurse. */
8595 if (!seen_error ())
8596 return convert_for_assignment (type, r, errtype, fndecl,
8597 parmnum, complain, flags);
8599 else if (fndecl)
8600 error ("cannot convert %qH to %qI for argument %qP to %qD",
8601 rhstype, type, parmnum, fndecl);
8602 else
8603 switch (errtype)
8605 case ICR_DEFAULT_ARGUMENT:
8606 error ("cannot convert %qH to %qI in default argument",
8607 rhstype, type);
8608 break;
8609 case ICR_ARGPASS:
8610 error ("cannot convert %qH to %qI in argument passing",
8611 rhstype, type);
8612 break;
8613 case ICR_CONVERTING:
8614 error ("cannot convert %qH to %qI",
8615 rhstype, type);
8616 break;
8617 case ICR_INIT:
8618 error ("cannot convert %qH to %qI in initialization",
8619 rhstype, type);
8620 break;
8621 case ICR_RETURN:
8622 error ("cannot convert %qH to %qI in return",
8623 rhstype, type);
8624 break;
8625 case ICR_ASSIGN:
8626 error ("cannot convert %qH to %qI in assignment",
8627 rhstype, type);
8628 break;
8629 default:
8630 gcc_unreachable();
8632 if (TYPE_PTR_P (rhstype)
8633 && TYPE_PTR_P (type)
8634 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8635 && CLASS_TYPE_P (TREE_TYPE (type))
8636 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8637 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8638 (TREE_TYPE (rhstype))),
8639 "class type %qT is incomplete", TREE_TYPE (rhstype));
8641 return error_mark_node;
8644 if (warn_suggest_attribute_format)
8646 const enum tree_code codel = TREE_CODE (type);
8647 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8648 && coder == codel
8649 && check_missing_format_attribute (type, rhstype)
8650 && (complain & tf_warning))
8651 switch (errtype)
8653 case ICR_ARGPASS:
8654 case ICR_DEFAULT_ARGUMENT:
8655 if (fndecl)
8656 warning (OPT_Wsuggest_attribute_format,
8657 "parameter %qP of %qD might be a candidate "
8658 "for a format attribute", parmnum, fndecl);
8659 else
8660 warning (OPT_Wsuggest_attribute_format,
8661 "parameter might be a candidate "
8662 "for a format attribute");
8663 break;
8664 case ICR_CONVERTING:
8665 warning (OPT_Wsuggest_attribute_format,
8666 "target of conversion might be a candidate "
8667 "for a format attribute");
8668 break;
8669 case ICR_INIT:
8670 warning (OPT_Wsuggest_attribute_format,
8671 "target of initialization might be a candidate "
8672 "for a format attribute");
8673 break;
8674 case ICR_RETURN:
8675 warning (OPT_Wsuggest_attribute_format,
8676 "return type might be a candidate "
8677 "for a format attribute");
8678 break;
8679 case ICR_ASSIGN:
8680 warning (OPT_Wsuggest_attribute_format,
8681 "left-hand side of assignment might be a candidate "
8682 "for a format attribute");
8683 break;
8684 default:
8685 gcc_unreachable();
8689 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8690 does not. */
8691 if (warn_parentheses
8692 && TREE_CODE (type) == BOOLEAN_TYPE
8693 && TREE_CODE (rhs) == MODIFY_EXPR
8694 && !TREE_NO_WARNING (rhs)
8695 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8696 && (complain & tf_warning))
8698 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8700 warning_at (loc, OPT_Wparentheses,
8701 "suggest parentheses around assignment used as truth value");
8702 TREE_NO_WARNING (rhs) = 1;
8705 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8706 complain, flags);
8709 /* Convert RHS to be of type TYPE.
8710 If EXP is nonzero, it is the target of the initialization.
8711 ERRTYPE indicates what kind of error the implicit conversion is.
8713 Two major differences between the behavior of
8714 `convert_for_assignment' and `convert_for_initialization'
8715 are that references are bashed in the former, while
8716 copied in the latter, and aggregates are assigned in
8717 the former (operator=) while initialized in the
8718 latter (X(X&)).
8720 If using constructor make sure no conversion operator exists, if one does
8721 exist, an ambiguity exists. */
8723 tree
8724 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8725 impl_conv_rhs errtype, tree fndecl, int parmnum,
8726 tsubst_flags_t complain)
8728 enum tree_code codel = TREE_CODE (type);
8729 tree rhstype;
8730 enum tree_code coder;
8732 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8733 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8734 if (TREE_CODE (rhs) == NOP_EXPR
8735 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8736 && codel != REFERENCE_TYPE)
8737 rhs = TREE_OPERAND (rhs, 0);
8739 if (type == error_mark_node
8740 || rhs == error_mark_node
8741 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8742 return error_mark_node;
8744 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8746 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8747 && TREE_CODE (type) != ARRAY_TYPE
8748 && (TREE_CODE (type) != REFERENCE_TYPE
8749 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8750 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8751 && !TYPE_REFFN_P (type))
8752 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8753 rhs = decay_conversion (rhs, complain);
8755 rhstype = TREE_TYPE (rhs);
8756 coder = TREE_CODE (rhstype);
8758 if (coder == ERROR_MARK)
8759 return error_mark_node;
8761 /* We accept references to incomplete types, so we can
8762 return here before checking if RHS is of complete type. */
8764 if (codel == REFERENCE_TYPE)
8766 /* This should eventually happen in convert_arguments. */
8767 int savew = 0, savee = 0;
8769 if (fndecl)
8770 savew = warningcount + werrorcount, savee = errorcount;
8771 rhs = initialize_reference (type, rhs, flags, complain);
8773 if (fndecl
8774 && (warningcount + werrorcount > savew || errorcount > savee))
8775 inform (DECL_SOURCE_LOCATION (fndecl),
8776 "in passing argument %P of %qD", parmnum, fndecl);
8778 return rhs;
8781 if (exp != 0)
8782 exp = require_complete_type_sfinae (exp, complain);
8783 if (exp == error_mark_node)
8784 return error_mark_node;
8786 rhstype = non_reference (rhstype);
8788 type = complete_type (type);
8790 if (DIRECT_INIT_EXPR_P (type, rhs))
8791 /* Don't try to do copy-initialization if we already have
8792 direct-initialization. */
8793 return rhs;
8795 if (MAYBE_CLASS_TYPE_P (type))
8796 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8798 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8799 complain, flags);
8802 /* If RETVAL is the address of, or a reference to, a local variable or
8803 temporary give an appropriate warning and return true. */
8805 static bool
8806 maybe_warn_about_returning_address_of_local (tree retval)
8808 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8809 tree whats_returned = fold_for_warn (retval);
8811 for (;;)
8813 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8814 whats_returned = TREE_OPERAND (whats_returned, 1);
8815 else if (CONVERT_EXPR_P (whats_returned)
8816 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8817 whats_returned = TREE_OPERAND (whats_returned, 0);
8818 else
8819 break;
8822 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8823 return false;
8824 whats_returned = TREE_OPERAND (whats_returned, 0);
8826 while (TREE_CODE (whats_returned) == COMPONENT_REF
8827 || TREE_CODE (whats_returned) == ARRAY_REF)
8828 whats_returned = TREE_OPERAND (whats_returned, 0);
8830 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8832 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8833 || TREE_CODE (whats_returned) == TARGET_EXPR)
8835 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8836 return true;
8838 if (VAR_P (whats_returned)
8839 && DECL_NAME (whats_returned)
8840 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8842 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8843 return true;
8847 if (DECL_P (whats_returned)
8848 && DECL_NAME (whats_returned)
8849 && DECL_FUNCTION_SCOPE_P (whats_returned)
8850 && !is_capture_proxy (whats_returned)
8851 && !(TREE_STATIC (whats_returned)
8852 || TREE_PUBLIC (whats_returned)))
8854 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8855 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8856 OPT_Wreturn_local_addr,
8857 "reference to local variable %qD returned",
8858 whats_returned);
8859 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8860 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8861 OPT_Wreturn_local_addr, "address of label %qD returned",
8862 whats_returned);
8863 else
8864 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8865 OPT_Wreturn_local_addr, "address of local variable %qD "
8866 "returned", whats_returned);
8867 return true;
8870 return false;
8873 /* Check that returning RETVAL from the current function is valid.
8874 Return an expression explicitly showing all conversions required to
8875 change RETVAL into the function return type, and to assign it to
8876 the DECL_RESULT for the function. Set *NO_WARNING to true if
8877 code reaches end of non-void function warning shouldn't be issued
8878 on this RETURN_EXPR. */
8880 tree
8881 check_return_expr (tree retval, bool *no_warning)
8883 tree result;
8884 /* The type actually returned by the function. */
8885 tree valtype;
8886 /* The type the function is declared to return, or void if
8887 the declared type is incomplete. */
8888 tree functype;
8889 int fn_returns_value_p;
8890 bool named_return_value_okay_p;
8892 *no_warning = false;
8894 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8896 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8897 "statement is not allowed");
8898 return NULL_TREE;
8901 /* A `volatile' function is one that isn't supposed to return, ever.
8902 (This is a G++ extension, used to get better code for functions
8903 that call the `volatile' function.) */
8904 if (TREE_THIS_VOLATILE (current_function_decl))
8905 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8907 /* Check for various simple errors. */
8908 if (DECL_DESTRUCTOR_P (current_function_decl))
8910 if (retval)
8911 error ("returning a value from a destructor");
8912 return NULL_TREE;
8914 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8916 if (in_function_try_handler)
8917 /* If a return statement appears in a handler of the
8918 function-try-block of a constructor, the program is ill-formed. */
8919 error ("cannot return from a handler of a function-try-block of a constructor");
8920 else if (retval)
8921 /* You can't return a value from a constructor. */
8922 error ("returning a value from a constructor");
8923 return NULL_TREE;
8926 const tree saved_retval = retval;
8928 if (processing_template_decl)
8930 current_function_returns_value = 1;
8932 if (check_for_bare_parameter_packs (retval))
8933 return error_mark_node;
8935 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
8936 || (retval != NULL_TREE
8937 && type_dependent_expression_p (retval)))
8938 return retval;
8941 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8943 /* Deduce auto return type from a return statement. */
8944 if (current_function_auto_return_pattern)
8946 tree auto_node;
8947 tree type;
8949 if (!retval && !is_auto (current_function_auto_return_pattern))
8951 /* Give a helpful error message. */
8952 error ("return-statement with no value, in function returning %qT",
8953 current_function_auto_return_pattern);
8954 inform (input_location, "only plain %<auto%> return type can be "
8955 "deduced to %<void%>");
8956 type = error_mark_node;
8958 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8960 error ("returning initializer list");
8961 type = error_mark_node;
8963 else
8965 if (!retval)
8966 retval = void_node;
8967 auto_node = type_uses_auto (current_function_auto_return_pattern);
8968 type = do_auto_deduction (current_function_auto_return_pattern,
8969 retval, auto_node);
8972 if (type == error_mark_node)
8973 /* Leave it. */;
8974 else if (functype == current_function_auto_return_pattern)
8975 apply_deduced_return_type (current_function_decl, type);
8976 else if (!same_type_p (type, functype))
8978 if (LAMBDA_FUNCTION_P (current_function_decl))
8979 error ("inconsistent types %qT and %qT deduced for "
8980 "lambda return type", functype, type);
8981 else
8982 error ("inconsistent deduction for auto return type: "
8983 "%qT and then %qT", functype, type);
8985 functype = type;
8988 result = DECL_RESULT (current_function_decl);
8989 valtype = TREE_TYPE (result);
8990 gcc_assert (valtype != NULL_TREE);
8991 fn_returns_value_p = !VOID_TYPE_P (valtype);
8993 /* Check for a return statement with no return value in a function
8994 that's supposed to return a value. */
8995 if (!retval && fn_returns_value_p)
8997 if (functype != error_mark_node)
8998 permerror (input_location, "return-statement with no value, in "
8999 "function returning %qT", valtype);
9000 /* Remember that this function did return. */
9001 current_function_returns_value = 1;
9002 /* And signal caller that TREE_NO_WARNING should be set on the
9003 RETURN_EXPR to avoid control reaches end of non-void function
9004 warnings in tree-cfg.c. */
9005 *no_warning = true;
9007 /* Check for a return statement with a value in a function that
9008 isn't supposed to return a value. */
9009 else if (retval && !fn_returns_value_p)
9011 if (VOID_TYPE_P (TREE_TYPE (retval)))
9012 /* You can return a `void' value from a function of `void'
9013 type. In that case, we have to evaluate the expression for
9014 its side-effects. */
9015 finish_expr_stmt (retval);
9016 else
9017 permerror (input_location, "return-statement with a value, in function "
9018 "returning 'void'");
9019 current_function_returns_null = 1;
9021 /* There's really no value to return, after all. */
9022 return NULL_TREE;
9024 else if (!retval)
9025 /* Remember that this function can sometimes return without a
9026 value. */
9027 current_function_returns_null = 1;
9028 else
9029 /* Remember that this function did return a value. */
9030 current_function_returns_value = 1;
9032 /* Check for erroneous operands -- but after giving ourselves a
9033 chance to provide an error about returning a value from a void
9034 function. */
9035 if (error_operand_p (retval))
9037 current_function_return_value = error_mark_node;
9038 return error_mark_node;
9041 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9042 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
9043 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
9044 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
9045 && ! flag_check_new
9046 && retval && null_ptr_cst_p (retval))
9047 warning (0, "%<operator new%> must not return NULL unless it is "
9048 "declared %<throw()%> (or -fcheck-new is in effect)");
9050 /* Effective C++ rule 15. See also start_function. */
9051 if (warn_ecpp
9052 && DECL_NAME (current_function_decl) == cp_assignment_operator_id (NOP_EXPR))
9054 bool warn = true;
9056 /* The function return type must be a reference to the current
9057 class. */
9058 if (TREE_CODE (valtype) == REFERENCE_TYPE
9059 && same_type_ignoring_top_level_qualifiers_p
9060 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
9062 /* Returning '*this' is obviously OK. */
9063 if (retval == current_class_ref)
9064 warn = false;
9065 /* If we are calling a function whose return type is the same of
9066 the current class reference, it is ok. */
9067 else if (INDIRECT_REF_P (retval)
9068 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
9069 warn = false;
9072 if (warn)
9073 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
9076 if (processing_template_decl)
9078 /* We should not have changed the return value. */
9079 gcc_assert (retval == saved_retval);
9080 return retval;
9083 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9085 [...] For a function with a class return type, if the expression
9086 in the return statement is the name of a local object, and the cv-
9087 unqualified type of the local object is the same as the function
9088 return type, an implementation is permitted to omit creating the tem-
9089 porary object to hold the function return value [...]
9091 So, if this is a value-returning function that always returns the same
9092 local variable, remember it.
9094 It might be nice to be more flexible, and choose the first suitable
9095 variable even if the function sometimes returns something else, but
9096 then we run the risk of clobbering the variable we chose if the other
9097 returned expression uses the chosen variable somehow. And people expect
9098 this restriction, anyway. (jason 2000-11-19)
9100 See finish_function and finalize_nrv for the rest of this optimization. */
9102 named_return_value_okay_p =
9103 (retval != NULL_TREE
9104 /* Must be a local, automatic variable. */
9105 && VAR_P (retval)
9106 && DECL_CONTEXT (retval) == current_function_decl
9107 && ! TREE_STATIC (retval)
9108 /* And not a lambda or anonymous union proxy. */
9109 && !DECL_HAS_VALUE_EXPR_P (retval)
9110 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
9111 /* The cv-unqualified type of the returned value must be the
9112 same as the cv-unqualified return type of the
9113 function. */
9114 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
9115 (TYPE_MAIN_VARIANT (functype)))
9116 /* And the returned value must be non-volatile. */
9117 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
9119 if (fn_returns_value_p && flag_elide_constructors)
9121 if (named_return_value_okay_p
9122 && (current_function_return_value == NULL_TREE
9123 || current_function_return_value == retval))
9124 current_function_return_value = retval;
9125 else
9126 current_function_return_value = error_mark_node;
9129 /* We don't need to do any conversions when there's nothing being
9130 returned. */
9131 if (!retval)
9132 return NULL_TREE;
9134 /* Do any required conversions. */
9135 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9136 /* No conversions are required. */
9138 else
9140 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
9142 /* The functype's return type will have been set to void, if it
9143 was an incomplete type. Just treat this as 'return;' */
9144 if (VOID_TYPE_P (functype))
9145 return error_mark_node;
9147 /* If we had an id-expression obfuscated by force_paren_expr, we need
9148 to undo it so we can try to treat it as an rvalue below. */
9149 retval = maybe_undo_parenthesized_ref (retval);
9151 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9152 treated as an rvalue for the purposes of overload resolution to
9153 favor move constructors over copy constructors.
9155 Note that these conditions are similar to, but not as strict as,
9156 the conditions for the named return value optimization. */
9157 if ((cxx_dialect != cxx98)
9158 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
9159 || TREE_CODE (retval) == PARM_DECL)
9160 && DECL_CONTEXT (retval) == current_function_decl
9161 && !TREE_STATIC (retval)
9162 /* This is only interesting for class type. */
9163 && CLASS_TYPE_P (functype))
9164 flags = flags | LOOKUP_PREFER_RVALUE;
9166 /* First convert the value to the function's return type, then
9167 to the type of return value's location to handle the
9168 case that functype is smaller than the valtype. */
9169 retval = convert_for_initialization
9170 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
9171 tf_warning_or_error);
9172 retval = convert (valtype, retval);
9174 /* If the conversion failed, treat this just like `return;'. */
9175 if (retval == error_mark_node)
9176 return retval;
9177 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9178 else if (! cfun->returns_struct
9179 && TREE_CODE (retval) == TARGET_EXPR
9180 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
9181 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9182 TREE_OPERAND (retval, 0));
9183 else if (maybe_warn_about_returning_address_of_local (retval))
9184 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9185 build_zero_cst (TREE_TYPE (retval)));
9188 /* Actually copy the value returned into the appropriate location. */
9189 if (retval && retval != result)
9190 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
9192 return retval;
9196 /* Returns nonzero if the pointer-type FROM can be converted to the
9197 pointer-type TO via a qualification conversion. If CONSTP is -1,
9198 then we return nonzero if the pointers are similar, and the
9199 cv-qualification signature of FROM is a proper subset of that of TO.
9201 If CONSTP is positive, then all outer pointers have been
9202 const-qualified. */
9204 static int
9205 comp_ptr_ttypes_real (tree to, tree from, int constp)
9207 bool to_more_cv_qualified = false;
9208 bool is_opaque_pointer = false;
9210 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9212 if (TREE_CODE (to) != TREE_CODE (from))
9213 return 0;
9215 if (TREE_CODE (from) == OFFSET_TYPE
9216 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9217 TYPE_OFFSET_BASETYPE (to)))
9218 return 0;
9220 /* Const and volatile mean something different for function types,
9221 so the usual checks are not appropriate. */
9222 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9224 if (!at_least_as_qualified_p (to, from))
9225 return 0;
9227 if (!at_least_as_qualified_p (from, to))
9229 if (constp == 0)
9230 return 0;
9231 to_more_cv_qualified = true;
9234 if (constp > 0)
9235 constp &= TYPE_READONLY (to);
9238 if (VECTOR_TYPE_P (to))
9239 is_opaque_pointer = vector_targets_convertible_p (to, from);
9241 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
9242 return ((constp >= 0 || to_more_cv_qualified)
9243 && (is_opaque_pointer
9244 || same_type_ignoring_top_level_qualifiers_p (to, from)));
9248 /* When comparing, say, char ** to char const **, this function takes
9249 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9250 types to this function. */
9253 comp_ptr_ttypes (tree to, tree from)
9255 return comp_ptr_ttypes_real (to, from, 1);
9258 /* Returns true iff FNTYPE is a non-class type that involves
9259 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9260 if a parameter type is ill-formed. */
9262 bool
9263 error_type_p (const_tree type)
9265 tree t;
9267 switch (TREE_CODE (type))
9269 case ERROR_MARK:
9270 return true;
9272 case POINTER_TYPE:
9273 case REFERENCE_TYPE:
9274 case OFFSET_TYPE:
9275 return error_type_p (TREE_TYPE (type));
9277 case FUNCTION_TYPE:
9278 case METHOD_TYPE:
9279 if (error_type_p (TREE_TYPE (type)))
9280 return true;
9281 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9282 if (error_type_p (TREE_VALUE (t)))
9283 return true;
9284 return false;
9286 case RECORD_TYPE:
9287 if (TYPE_PTRMEMFUNC_P (type))
9288 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9289 return false;
9291 default:
9292 return false;
9296 /* Returns true if to and from are (possibly multi-level) pointers to the same
9297 type or inheritance-related types, regardless of cv-quals. */
9299 bool
9300 ptr_reasonably_similar (const_tree to, const_tree from)
9302 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9304 /* Any target type is similar enough to void. */
9305 if (VOID_TYPE_P (to))
9306 return !error_type_p (from);
9307 if (VOID_TYPE_P (from))
9308 return !error_type_p (to);
9310 if (TREE_CODE (to) != TREE_CODE (from))
9311 return false;
9313 if (TREE_CODE (from) == OFFSET_TYPE
9314 && comptypes (TYPE_OFFSET_BASETYPE (to),
9315 TYPE_OFFSET_BASETYPE (from),
9316 COMPARE_BASE | COMPARE_DERIVED))
9317 continue;
9319 if (VECTOR_TYPE_P (to)
9320 && vector_types_convertible_p (to, from, false))
9321 return true;
9323 if (TREE_CODE (to) == INTEGER_TYPE
9324 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9325 return true;
9327 if (TREE_CODE (to) == FUNCTION_TYPE)
9328 return !error_type_p (to) && !error_type_p (from);
9330 if (!TYPE_PTR_P (to))
9332 /* When either type is incomplete avoid DERIVED_FROM_P,
9333 which may call complete_type (c++/57942). */
9334 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9335 return comptypes
9336 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9337 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9342 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9343 pointer-to-member types) are the same, ignoring cv-qualification at
9344 all levels. */
9346 bool
9347 comp_ptr_ttypes_const (tree to, tree from)
9349 bool is_opaque_pointer = false;
9351 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9353 if (TREE_CODE (to) != TREE_CODE (from))
9354 return false;
9356 if (TREE_CODE (from) == OFFSET_TYPE
9357 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9358 TYPE_OFFSET_BASETYPE (to)))
9359 continue;
9361 if (VECTOR_TYPE_P (to))
9362 is_opaque_pointer = vector_targets_convertible_p (to, from);
9364 if (!TYPE_PTR_P (to))
9365 return (is_opaque_pointer
9366 || same_type_ignoring_top_level_qualifiers_p (to, from));
9370 /* Returns the type qualifiers for this type, including the qualifiers on the
9371 elements for an array type. */
9374 cp_type_quals (const_tree type)
9376 int quals;
9377 /* This CONST_CAST is okay because strip_array_types returns its
9378 argument unmodified and we assign it to a const_tree. */
9379 type = strip_array_types (CONST_CAST_TREE (type));
9380 if (type == error_mark_node
9381 /* Quals on a FUNCTION_TYPE are memfn quals. */
9382 || TREE_CODE (type) == FUNCTION_TYPE)
9383 return TYPE_UNQUALIFIED;
9384 quals = TYPE_QUALS (type);
9385 /* METHOD and REFERENCE_TYPEs should never have quals. */
9386 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9387 && TREE_CODE (type) != REFERENCE_TYPE)
9388 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9389 == TYPE_UNQUALIFIED));
9390 return quals;
9393 /* Returns the function-ref-qualifier for TYPE */
9395 cp_ref_qualifier
9396 type_memfn_rqual (const_tree type)
9398 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9399 || TREE_CODE (type) == METHOD_TYPE);
9401 if (!FUNCTION_REF_QUALIFIED (type))
9402 return REF_QUAL_NONE;
9403 else if (FUNCTION_RVALUE_QUALIFIED (type))
9404 return REF_QUAL_RVALUE;
9405 else
9406 return REF_QUAL_LVALUE;
9409 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9410 METHOD_TYPE. */
9413 type_memfn_quals (const_tree type)
9415 if (TREE_CODE (type) == FUNCTION_TYPE)
9416 return TYPE_QUALS (type);
9417 else if (TREE_CODE (type) == METHOD_TYPE)
9418 return cp_type_quals (class_of_this_parm (type));
9419 else
9420 gcc_unreachable ();
9423 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9424 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9426 tree
9427 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9429 /* Could handle METHOD_TYPE here if necessary. */
9430 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9431 if (TYPE_QUALS (type) == memfn_quals
9432 && type_memfn_rqual (type) == rqual)
9433 return type;
9435 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9436 complex. */
9437 tree result = build_qualified_type (type, memfn_quals);
9438 return build_ref_qualified_type (result, rqual);
9441 /* Returns nonzero if TYPE is const or volatile. */
9443 bool
9444 cv_qualified_p (const_tree type)
9446 int quals = cp_type_quals (type);
9447 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9450 /* Returns nonzero if the TYPE contains a mutable member. */
9452 bool
9453 cp_has_mutable_p (const_tree type)
9455 /* This CONST_CAST is okay because strip_array_types returns its
9456 argument unmodified and we assign it to a const_tree. */
9457 type = strip_array_types (CONST_CAST_TREE(type));
9459 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9462 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9463 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9464 approximation. In particular, consider:
9466 int f();
9467 struct S { int i; };
9468 const S s = { f(); }
9470 Here, we will make "s" as TREE_READONLY (because it is declared
9471 "const") -- only to reverse ourselves upon seeing that the
9472 initializer is non-constant. */
9474 void
9475 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9477 tree type = TREE_TYPE (decl);
9479 if (type == error_mark_node)
9480 return;
9482 if (TREE_CODE (decl) == TYPE_DECL)
9483 return;
9485 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9486 && type_quals != TYPE_UNQUALIFIED));
9488 /* Avoid setting TREE_READONLY incorrectly. */
9489 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9490 constructor can produce constant init, so rely on cp_finish_decl to
9491 clear TREE_READONLY if the variable has non-constant init. */
9493 /* If the type has (or might have) a mutable component, that component
9494 might be modified. */
9495 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9496 type_quals &= ~TYPE_QUAL_CONST;
9498 c_apply_type_quals_to_decl (type_quals, decl);
9501 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9502 exemplar types such that casting T1 to T2 is casting away constness
9503 if and only if there is no implicit conversion from T1 to T2. */
9505 static void
9506 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9508 int quals1;
9509 int quals2;
9511 /* [expr.const.cast]
9513 For multi-level pointer to members and multi-level mixed pointers
9514 and pointers to members (conv.qual), the "member" aspect of a
9515 pointer to member level is ignored when determining if a const
9516 cv-qualifier has been cast away. */
9517 /* [expr.const.cast]
9519 For two pointer types:
9521 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9522 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9523 K is min(N,M)
9525 casting from X1 to X2 casts away constness if, for a non-pointer
9526 type T there does not exist an implicit conversion (clause
9527 _conv_) from:
9529 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9533 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9534 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9535 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9537 *t1 = cp_build_qualified_type (void_type_node,
9538 cp_type_quals (*t1));
9539 *t2 = cp_build_qualified_type (void_type_node,
9540 cp_type_quals (*t2));
9541 return;
9544 quals1 = cp_type_quals (*t1);
9545 quals2 = cp_type_quals (*t2);
9547 if (TYPE_PTRDATAMEM_P (*t1))
9548 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9549 else
9550 *t1 = TREE_TYPE (*t1);
9551 if (TYPE_PTRDATAMEM_P (*t2))
9552 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9553 else
9554 *t2 = TREE_TYPE (*t2);
9556 casts_away_constness_r (t1, t2, complain);
9557 *t1 = build_pointer_type (*t1);
9558 *t2 = build_pointer_type (*t2);
9559 *t1 = cp_build_qualified_type (*t1, quals1);
9560 *t2 = cp_build_qualified_type (*t2, quals2);
9563 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9564 constness.
9566 ??? This function returns non-zero if casting away qualifiers not
9567 just const. We would like to return to the caller exactly which
9568 qualifiers are casted away to give more accurate diagnostics.
9571 static bool
9572 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9574 if (TREE_CODE (t2) == REFERENCE_TYPE)
9576 /* [expr.const.cast]
9578 Casting from an lvalue of type T1 to an lvalue of type T2
9579 using a reference cast casts away constness if a cast from an
9580 rvalue of type "pointer to T1" to the type "pointer to T2"
9581 casts away constness. */
9582 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9583 return casts_away_constness (build_pointer_type (t1),
9584 build_pointer_type (TREE_TYPE (t2)),
9585 complain);
9588 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9589 /* [expr.const.cast]
9591 Casting from an rvalue of type "pointer to data member of X
9592 of type T1" to the type "pointer to data member of Y of type
9593 T2" casts away constness if a cast from an rvalue of type
9594 "pointer to T1" to the type "pointer to T2" casts away
9595 constness. */
9596 return casts_away_constness
9597 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9598 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9599 complain);
9601 /* Casting away constness is only something that makes sense for
9602 pointer or reference types. */
9603 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9604 return false;
9606 /* Top-level qualifiers don't matter. */
9607 t1 = TYPE_MAIN_VARIANT (t1);
9608 t2 = TYPE_MAIN_VARIANT (t2);
9609 casts_away_constness_r (&t1, &t2, complain);
9610 if (!can_convert (t2, t1, complain))
9611 return true;
9613 return false;
9616 /* If T is a REFERENCE_TYPE return the type to which T refers.
9617 Otherwise, return T itself. */
9619 tree
9620 non_reference (tree t)
9622 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9623 t = TREE_TYPE (t);
9624 return t;
9628 /* Return nonzero if REF is an lvalue valid for this language;
9629 otherwise, print an error message and return zero. USE says
9630 how the lvalue is being used and so selects the error message. */
9633 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9635 cp_lvalue_kind kind = lvalue_kind (ref);
9637 if (kind == clk_none)
9639 if (complain & tf_error)
9640 lvalue_error (input_location, use);
9641 return 0;
9643 else if (kind & (clk_rvalueref|clk_class))
9645 if (!(complain & tf_error))
9646 return 0;
9647 if (kind & clk_class)
9648 /* Make this a permerror because we used to accept it. */
9649 permerror (input_location, "using temporary as lvalue");
9650 else
9651 error ("using xvalue (rvalue reference) as lvalue");
9653 return 1;
9656 /* Return true if a user-defined literal operator is a raw operator. */
9658 bool
9659 check_raw_literal_operator (const_tree decl)
9661 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9662 tree argtype;
9663 int arity;
9664 bool maybe_raw_p = false;
9666 /* Count the number and type of arguments and check for ellipsis. */
9667 for (argtype = argtypes, arity = 0;
9668 argtype && argtype != void_list_node;
9669 ++arity, argtype = TREE_CHAIN (argtype))
9671 tree t = TREE_VALUE (argtype);
9673 if (same_type_p (t, const_string_type_node))
9674 maybe_raw_p = true;
9676 if (!argtype)
9677 return false; /* Found ellipsis. */
9679 if (!maybe_raw_p || arity != 1)
9680 return false;
9682 return true;
9686 /* Return true if a user-defined literal operator has one of the allowed
9687 argument types. */
9689 bool
9690 check_literal_operator_args (const_tree decl,
9691 bool *long_long_unsigned_p, bool *long_double_p)
9693 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9695 *long_long_unsigned_p = false;
9696 *long_double_p = false;
9697 if (processing_template_decl || processing_specialization)
9698 return argtypes == void_list_node;
9699 else
9701 tree argtype;
9702 int arity;
9703 int max_arity = 2;
9705 /* Count the number and type of arguments and check for ellipsis. */
9706 for (argtype = argtypes, arity = 0;
9707 argtype && argtype != void_list_node;
9708 argtype = TREE_CHAIN (argtype))
9710 tree t = TREE_VALUE (argtype);
9711 ++arity;
9713 if (TYPE_PTR_P (t))
9715 bool maybe_raw_p = false;
9716 t = TREE_TYPE (t);
9717 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9718 return false;
9719 t = TYPE_MAIN_VARIANT (t);
9720 if ((maybe_raw_p = same_type_p (t, char_type_node))
9721 || same_type_p (t, wchar_type_node)
9722 || same_type_p (t, char16_type_node)
9723 || same_type_p (t, char32_type_node))
9725 argtype = TREE_CHAIN (argtype);
9726 if (!argtype)
9727 return false;
9728 t = TREE_VALUE (argtype);
9729 if (maybe_raw_p && argtype == void_list_node)
9730 return true;
9731 else if (same_type_p (t, size_type_node))
9733 ++arity;
9734 continue;
9736 else
9737 return false;
9740 else if (same_type_p (t, long_long_unsigned_type_node))
9742 max_arity = 1;
9743 *long_long_unsigned_p = true;
9745 else if (same_type_p (t, long_double_type_node))
9747 max_arity = 1;
9748 *long_double_p = true;
9750 else if (same_type_p (t, char_type_node))
9751 max_arity = 1;
9752 else if (same_type_p (t, wchar_type_node))
9753 max_arity = 1;
9754 else if (same_type_p (t, char16_type_node))
9755 max_arity = 1;
9756 else if (same_type_p (t, char32_type_node))
9757 max_arity = 1;
9758 else
9759 return false;
9761 if (!argtype)
9762 return false; /* Found ellipsis. */
9764 if (arity != max_arity)
9765 return false;
9767 return true;
9771 /* Always returns false since unlike C90, C++ has no concept of implicit
9772 function declarations. */
9774 bool
9775 c_decl_implicit (const_tree)
9777 return false;